blob: 14ce789927e43130a280a7dfc3c442e79f2a43b1 [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
107struct io_uring {
108 u32 head ____cacheline_aligned_in_smp;
109 u32 tail ____cacheline_aligned_in_smp;
110};
111
Stefan Bühler1e84b972019-04-24 23:54:16 +0200112/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000113 * This data is shared with the application through the mmap at offsets
114 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200115 *
116 * The offsets to the member fields are published through struct
117 * io_sqring_offsets when calling io_uring_setup.
118 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000119struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200120 /*
121 * Head and tail offsets into the ring; the offsets need to be
122 * masked to get valid indices.
123 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000124 * The kernel controls head of the sq ring and the tail of the cq ring,
125 * and the application controls tail of the sq ring and the head of the
126 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200127 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000128 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200129 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000130 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200131 * ring_entries - 1)
132 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000133 u32 sq_ring_mask, cq_ring_mask;
134 /* Ring sizes (constant, power of 2) */
135 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200136 /*
137 * Number of invalid entries dropped by the kernel due to
138 * invalid index stored in array
139 *
140 * Written by the kernel, shouldn't be modified by the
141 * application (i.e. get number of "new events" by comparing to
142 * cached value).
143 *
144 * After a new SQ head value was read by the application this
145 * counter includes all submissions that were dropped reaching
146 * the new SQ head (and possibly more).
147 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000148 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200149 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200150 * Runtime SQ flags
Stefan Bühler1e84b972019-04-24 23:54:16 +0200151 *
152 * Written by the kernel, shouldn't be modified by the
153 * application.
154 *
155 * The application needs a full memory barrier before checking
156 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
157 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000158 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200159 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200160 * Runtime CQ flags
161 *
162 * Written by the application, shouldn't be modified by the
163 * kernel.
164 */
165 u32 cq_flags;
166 /*
Stefan Bühler1e84b972019-04-24 23:54:16 +0200167 * Number of completion events lost because the queue was full;
168 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800169 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200170 * the completion queue.
171 *
172 * Written by the kernel, shouldn't be modified by the
173 * application (i.e. get number of "new events" by comparing to
174 * cached value).
175 *
176 * As completion events come in out of order this counter is not
177 * ordered with any other data.
178 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000179 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200180 /*
181 * Ring buffer of completion events.
182 *
183 * The kernel writes completion events fresh every time they are
184 * produced, so the application is allowed to modify pending
185 * entries.
186 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000187 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700188};
189
Pavel Begunkov45d189c2021-02-10 00:03:07 +0000190enum io_uring_cmd_flags {
191 IO_URING_F_NONBLOCK = 1,
Pavel Begunkov889fca72021-02-10 00:03:09 +0000192 IO_URING_F_COMPLETE_DEFER = 2,
Pavel Begunkov45d189c2021-02-10 00:03:07 +0000193};
194
Jens Axboeedafcce2019-01-09 09:16:05 -0700195struct io_mapped_ubuf {
196 u64 ubuf;
197 size_t len;
198 struct bio_vec *bvec;
199 unsigned int nr_bvecs;
Jens Axboede293932020-09-17 16:19:16 -0600200 unsigned long acct_pages;
Jens Axboeedafcce2019-01-09 09:16:05 -0700201};
202
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000203struct io_ring_ctx;
204
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000205struct io_rsrc_put {
206 struct list_head list;
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000207 union {
208 void *rsrc;
209 struct file *file;
210 };
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000211};
212
213struct fixed_rsrc_table {
Jens Axboe65e19f52019-10-26 07:20:21 -0600214 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700215};
216
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000217struct fixed_rsrc_ref_node {
Xiaoguang Wang05589552020-03-31 14:05:18 +0800218 struct percpu_ref refs;
219 struct list_head node;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000220 struct list_head rsrc_list;
221 struct fixed_rsrc_data *rsrc_data;
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000222 void (*rsrc_put)(struct io_ring_ctx *ctx,
223 struct io_rsrc_put *prsrc);
Jens Axboe4a38aed22020-05-14 17:21:15 -0600224 struct llist_node llist;
Pavel Begunkove2978222020-11-18 14:56:26 +0000225 bool done;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800226};
227
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000228struct fixed_rsrc_data {
229 struct fixed_rsrc_table *table;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700230 struct io_ring_ctx *ctx;
231
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000232 struct fixed_rsrc_ref_node *node;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700233 struct percpu_ref refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700234 struct completion done;
235};
236
Jens Axboe5a2e7452020-02-23 16:23:11 -0700237struct io_buffer {
238 struct list_head list;
239 __u64 addr;
240 __s32 len;
241 __u16 bid;
242};
243
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200244struct io_restriction {
245 DECLARE_BITMAP(register_op, IORING_REGISTER_LAST);
246 DECLARE_BITMAP(sqe_op, IORING_OP_LAST);
247 u8 sqe_flags_allowed;
248 u8 sqe_flags_required;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +0200249 bool registered;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200250};
251
Jens Axboe534ca6d2020-09-02 13:52:19 -0600252struct io_sq_data {
253 refcount_t refs;
Jens Axboe69fb2132020-09-14 11:16:23 -0600254 struct mutex lock;
255
256 /* ctx's that are using this sqd */
257 struct list_head ctx_list;
258 struct list_head ctx_new_list;
259 struct mutex ctx_lock;
260
Jens Axboe534ca6d2020-09-02 13:52:19 -0600261 struct task_struct *thread;
262 struct wait_queue_head wait;
Xiaoguang Wang08369242020-11-03 14:15:59 +0800263
264 unsigned sq_thread_idle;
Jens Axboe534ca6d2020-09-02 13:52:19 -0600265};
266
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000267#define IO_IOPOLL_BATCH 8
Pavel Begunkov6dd0be12021-02-10 00:03:13 +0000268#define IO_COMPL_BATCH 32
Pavel Begunkov6ff119a2021-02-10 00:03:18 +0000269#define IO_REQ_CACHE_SIZE 32
Pavel Begunkovbf019da2021-02-10 00:03:17 +0000270#define IO_REQ_ALLOC_BATCH 8
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000271
272struct io_comp_state {
Pavel Begunkov6dd0be12021-02-10 00:03:13 +0000273 struct io_kiocb *reqs[IO_COMPL_BATCH];
Jens Axboe1b4c3512021-02-10 00:03:19 +0000274 unsigned int nr;
Jens Axboec7dae4b2021-02-09 19:53:37 -0700275 unsigned int locked_free_nr;
276 /* inline/task_work completion list, under ->uring_lock */
Jens Axboe1b4c3512021-02-10 00:03:19 +0000277 struct list_head free_list;
Jens Axboec7dae4b2021-02-09 19:53:37 -0700278 /* IRQ completion list, under ->completion_lock */
279 struct list_head locked_free_list;
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000280};
281
282struct io_submit_state {
283 struct blk_plug plug;
284
285 /*
286 * io_kiocb alloc cache
287 */
Pavel Begunkovbf019da2021-02-10 00:03:17 +0000288 void *reqs[IO_REQ_CACHE_SIZE];
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000289 unsigned int free_reqs;
290
291 bool plug_started;
292
293 /*
294 * Batch completion logic
295 */
296 struct io_comp_state comp;
297
298 /*
299 * File reference cache
300 */
301 struct file *file;
302 unsigned int fd;
303 unsigned int file_refs;
304 unsigned int ios_left;
305};
306
Jens Axboe2b188cc2019-01-07 10:46:33 -0700307struct io_ring_ctx {
308 struct {
309 struct percpu_ref refs;
310 } ____cacheline_aligned_in_smp;
311
312 struct {
313 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800314 unsigned int compat: 1;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -0700315 unsigned int limit_mem: 1;
Randy Dunlape1d85332020-02-05 20:57:10 -0800316 unsigned int cq_overflow_flushed: 1;
317 unsigned int drain_next: 1;
318 unsigned int eventfd_async: 1;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200319 unsigned int restricted: 1;
Pavel Begunkovd9d05212021-01-08 20:57:25 +0000320 unsigned int sqo_dead: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700321
Hristo Venev75b28af2019-08-26 17:23:46 +0000322 /*
323 * Ring buffer of indices into array of io_uring_sqe, which is
324 * mmapped by the application using the IORING_OFF_SQES offset.
325 *
326 * This indirection could e.g. be used to assign fixed
327 * io_uring_sqe entries to operations and only submit them to
328 * the queue when needed.
329 *
330 * The kernel modifies neither the indices array nor the entries
331 * array.
332 */
333 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700334 unsigned cached_sq_head;
335 unsigned sq_entries;
336 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700337 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600338 unsigned cached_sq_dropped;
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +0100339 unsigned cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700340 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600341
342 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600343 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700344 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700345
Jens Axboead3eb2c2019-12-18 17:12:20 -0700346 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700347 } ____cacheline_aligned_in_smp;
348
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700349 struct {
350 struct mutex uring_lock;
351 wait_queue_head_t wait;
352 } ____cacheline_aligned_in_smp;
353
354 struct io_submit_state submit_state;
355
Hristo Venev75b28af2019-08-26 17:23:46 +0000356 struct io_rings *rings;
357
Jens Axboe2b188cc2019-01-07 10:46:33 -0700358 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600359 struct io_wq *io_wq;
Jens Axboe2aede0e2020-09-14 10:45:53 -0600360
361 /*
362 * For SQPOLL usage - we hold a reference to the parent task, so we
363 * have access to the ->files
364 */
365 struct task_struct *sqo_task;
366
367 /* Only used for accounting purposes */
368 struct mm_struct *mm_account;
369
Dennis Zhou91d8f512020-09-16 13:41:05 -0700370#ifdef CONFIG_BLK_CGROUP
371 struct cgroup_subsys_state *sqo_blkcg_css;
372#endif
373
Jens Axboe534ca6d2020-09-02 13:52:19 -0600374 struct io_sq_data *sq_data; /* if using sq thread polling */
375
Jens Axboe90554202020-09-03 12:12:41 -0600376 struct wait_queue_head sqo_sq_wait;
Jens Axboe69fb2132020-09-14 11:16:23 -0600377 struct list_head sqd_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700378
Jens Axboe6b063142019-01-10 22:13:58 -0700379 /*
380 * If used, fixed file set. Writers must ensure that ->refs is dead,
381 * readers must ensure that ->refs is alive as long as the file* is
382 * used. Only updated through io_uring_register(2).
383 */
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000384 struct fixed_rsrc_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700385 unsigned nr_user_files;
386
Jens Axboeedafcce2019-01-09 09:16:05 -0700387 /* if used, fixed mapped user buffers */
388 unsigned nr_user_bufs;
389 struct io_mapped_ubuf *user_bufs;
390
Jens Axboe2b188cc2019-01-07 10:46:33 -0700391 struct user_struct *user;
392
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700393 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700394
Jens Axboe4ea33a92020-10-15 13:46:44 -0600395#ifdef CONFIG_AUDIT
396 kuid_t loginuid;
397 unsigned int sessionid;
398#endif
399
Jens Axboe0f158b42020-05-14 17:18:39 -0600400 struct completion ref_comp;
401 struct completion sq_thread_comp;
Jens Axboe206aefd2019-11-07 18:27:42 -0700402
403#if defined(CONFIG_UNIX)
404 struct socket *ring_sock;
405#endif
406
Jens Axboe5a2e7452020-02-23 16:23:11 -0700407 struct idr io_buffer_idr;
408
Jens Axboe071698e2020-01-28 10:04:42 -0700409 struct idr personality_idr;
410
Jens Axboe206aefd2019-11-07 18:27:42 -0700411 struct {
412 unsigned cached_cq_tail;
413 unsigned cq_entries;
414 unsigned cq_mask;
415 atomic_t cq_timeouts;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -0500416 unsigned cq_last_tm_flush;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700417 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700418 struct wait_queue_head cq_wait;
419 struct fasync_struct *cq_fasync;
420 struct eventfd_ctx *cq_ev_fd;
421 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700422
423 struct {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700424 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700425
Jens Axboedef596e2019-01-09 08:59:42 -0700426 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300427 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700428 * io_uring instances that don't use IORING_SETUP_SQPOLL.
429 * For SQPOLL, only the single threaded io_sq_thread() will
430 * manipulate the list, hence no extra locking is needed there.
431 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300432 struct list_head iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700433 struct hlist_head *cancel_hash;
434 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700435 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600436
437 spinlock_t inflight_lock;
438 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700439 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600440
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000441 struct delayed_work rsrc_put_work;
442 struct llist_head rsrc_put_llist;
Bijan Mottahedehd67d2262021-01-15 17:37:46 +0000443 struct list_head rsrc_ref_list;
444 spinlock_t rsrc_ref_lock;
Jens Axboe4a38aed22020-05-14 17:21:15 -0600445
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200446 struct io_restriction restrictions;
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700447
448 /* Keep this last, we don't need it for the fast path */
449 struct work_struct exit_work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700450};
451
Jens Axboe09bb8392019-03-13 12:39:28 -0600452/*
453 * First field must be the file pointer in all the
454 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
455 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700456struct io_poll_iocb {
457 struct file *file;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000458 struct wait_queue_head *head;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700459 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600460 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700461 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700462 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700463};
464
Pavel Begunkov018043b2020-10-27 23:17:18 +0000465struct io_poll_remove {
466 struct file *file;
467 u64 addr;
468};
469
Jens Axboeb5dba592019-12-11 14:02:38 -0700470struct io_close {
471 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -0700472 int fd;
473};
474
Jens Axboead8a48a2019-11-15 08:49:11 -0700475struct io_timeout_data {
476 struct io_kiocb *req;
477 struct hrtimer timer;
478 struct timespec64 ts;
479 enum hrtimer_mode mode;
480};
481
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700482struct io_accept {
483 struct file *file;
484 struct sockaddr __user *addr;
485 int __user *addr_len;
486 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600487 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700488};
489
490struct io_sync {
491 struct file *file;
492 loff_t len;
493 loff_t off;
494 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700495 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700496};
497
Jens Axboefbf23842019-12-17 18:45:56 -0700498struct io_cancel {
499 struct file *file;
500 u64 addr;
501};
502
Jens Axboeb29472e2019-12-17 18:50:29 -0700503struct io_timeout {
504 struct file *file;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300505 u32 off;
506 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300507 struct list_head list;
Pavel Begunkov90cd7e42020-10-27 23:25:36 +0000508 /* head of the link, used by linked timeouts only */
509 struct io_kiocb *head;
Jens Axboeb29472e2019-12-17 18:50:29 -0700510};
511
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100512struct io_timeout_rem {
513 struct file *file;
514 u64 addr;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000515
516 /* timeout update */
517 struct timespec64 ts;
518 u32 flags;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100519};
520
Jens Axboe9adbd452019-12-20 08:45:55 -0700521struct io_rw {
522 /* NOTE: kiocb has the file as the first member, so don't do it here */
523 struct kiocb kiocb;
524 u64 addr;
525 u64 len;
526};
527
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700528struct io_connect {
529 struct file *file;
530 struct sockaddr __user *addr;
531 int addr_len;
532};
533
Jens Axboee47293f2019-12-20 08:58:21 -0700534struct io_sr_msg {
535 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700536 union {
Pavel Begunkov270a5942020-07-12 20:41:04 +0300537 struct user_msghdr __user *umsg;
Jens Axboefddafac2020-01-04 20:19:44 -0700538 void __user *buf;
539 };
Jens Axboee47293f2019-12-20 08:58:21 -0700540 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700541 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700542 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700543 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700544};
545
Jens Axboe15b71ab2019-12-11 11:20:36 -0700546struct io_open {
547 struct file *file;
548 int dfd;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700549 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700550 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600551 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700552};
553
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000554struct io_rsrc_update {
Jens Axboe05f3fb32019-12-09 11:22:50 -0700555 struct file *file;
556 u64 arg;
557 u32 nr_args;
558 u32 offset;
559};
560
Jens Axboe4840e412019-12-25 22:03:45 -0700561struct io_fadvise {
562 struct file *file;
563 u64 offset;
564 u32 len;
565 u32 advice;
566};
567
Jens Axboec1ca7572019-12-25 22:18:28 -0700568struct io_madvise {
569 struct file *file;
570 u64 addr;
571 u32 len;
572 u32 advice;
573};
574
Jens Axboe3e4827b2020-01-08 15:18:09 -0700575struct io_epoll {
576 struct file *file;
577 int epfd;
578 int op;
579 int fd;
580 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700581};
582
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300583struct io_splice {
584 struct file *file_out;
585 struct file *file_in;
586 loff_t off_out;
587 loff_t off_in;
588 u64 len;
589 unsigned int flags;
590};
591
Jens Axboeddf0322d2020-02-23 16:41:33 -0700592struct io_provide_buf {
593 struct file *file;
594 __u64 addr;
595 __s32 len;
596 __u32 bgid;
597 __u16 nbufs;
598 __u16 bid;
599};
600
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700601struct io_statx {
602 struct file *file;
603 int dfd;
604 unsigned int mask;
605 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700606 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700607 struct statx __user *buffer;
608};
609
Jens Axboe36f4fa62020-09-05 11:14:22 -0600610struct io_shutdown {
611 struct file *file;
612 int how;
613};
614
Jens Axboe80a261f2020-09-28 14:23:58 -0600615struct io_rename {
616 struct file *file;
617 int old_dfd;
618 int new_dfd;
619 struct filename *oldpath;
620 struct filename *newpath;
621 int flags;
622};
623
Jens Axboe14a11432020-09-28 14:27:37 -0600624struct io_unlink {
625 struct file *file;
626 int dfd;
627 int flags;
628 struct filename *filename;
629};
630
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300631struct io_completion {
632 struct file *file;
633 struct list_head list;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +0300634 int cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300635};
636
Jens Axboef499a022019-12-02 16:28:46 -0700637struct io_async_connect {
638 struct sockaddr_storage address;
639};
640
Jens Axboe03b12302019-12-02 18:50:25 -0700641struct io_async_msghdr {
642 struct iovec fast_iov[UIO_FASTIOV];
Pavel Begunkov257e84a2021-02-05 00:58:00 +0000643 /* points to an allocated iov, if NULL we use fast_iov instead */
644 struct iovec *free_iov;
Jens Axboe03b12302019-12-02 18:50:25 -0700645 struct sockaddr __user *uaddr;
646 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700647 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700648};
649
Jens Axboef67676d2019-12-02 11:03:47 -0700650struct io_async_rw {
651 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600652 const struct iovec *free_iovec;
653 struct iov_iter iter;
Jens Axboe227c0c92020-08-13 11:51:40 -0600654 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600655 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700656};
657
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300658enum {
659 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
660 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
661 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
662 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
663 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700664 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300665
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300666 REQ_F_FAIL_LINK_BIT,
667 REQ_F_INFLIGHT_BIT,
668 REQ_F_CUR_POS_BIT,
669 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300670 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300671 REQ_F_ISREG_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300672 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700673 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700674 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600675 REQ_F_NO_FILE_TABLE_BIT,
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800676 REQ_F_WORK_INITIALIZED_BIT,
Pavel Begunkov900fad42020-10-19 16:39:16 +0100677 REQ_F_LTIMEOUT_ACTIVE_BIT,
Pavel Begunkove342c802021-01-19 13:32:47 +0000678 REQ_F_COMPLETE_INLINE_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700679
680 /* not a real bit, just to check we're not overflowing the space */
681 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300682};
683
684enum {
685 /* ctx owns file */
686 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
687 /* drain existing IO first */
688 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
689 /* linked sqes */
690 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
691 /* doesn't sever on completion < 0 */
692 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
693 /* IOSQE_ASYNC */
694 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700695 /* IOSQE_BUFFER_SELECT */
696 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300697
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300698 /* fail rest of links */
699 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
700 /* on inflight list */
701 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
702 /* read/write uses file position */
703 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
704 /* must not punt to workers */
705 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100706 /* has or had linked timeout */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300707 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300708 /* regular file */
709 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300710 /* needs cleanup */
711 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700712 /* already went through poll handler */
713 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700714 /* buffer already selected */
715 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600716 /* doesn't need file table for this request */
717 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800718 /* io_wq_work is initialized */
719 REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100720 /* linked timeout is active, i.e. prepared by link's head */
721 REQ_F_LTIMEOUT_ACTIVE = BIT(REQ_F_LTIMEOUT_ACTIVE_BIT),
Pavel Begunkove342c802021-01-19 13:32:47 +0000722 /* completion is deferred through io_comp_state */
723 REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700724};
725
726struct async_poll {
727 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600728 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300729};
730
Jens Axboe7cbf1722021-02-10 00:03:20 +0000731struct io_task_work {
732 struct io_wq_work_node node;
733 task_work_func_t func;
734};
735
Jens Axboe09bb8392019-03-13 12:39:28 -0600736/*
737 * NOTE! Each of the iocb union members has the file pointer
738 * as the first entry in their struct definition. So you can
739 * access the file pointer through any of the sub-structs,
740 * or directly as just 'ki_filp' in this struct.
741 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700742struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700743 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600744 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700745 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700746 struct io_poll_iocb poll;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000747 struct io_poll_remove poll_remove;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700748 struct io_accept accept;
749 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700750 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700751 struct io_timeout timeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100752 struct io_timeout_rem timeout_rem;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700753 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700754 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700755 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700756 struct io_close close;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000757 struct io_rsrc_update rsrc_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700758 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700759 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700760 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300761 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700762 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700763 struct io_statx statx;
Jens Axboe36f4fa62020-09-05 11:14:22 -0600764 struct io_shutdown shutdown;
Jens Axboe80a261f2020-09-28 14:23:58 -0600765 struct io_rename rename;
Jens Axboe14a11432020-09-28 14:27:37 -0600766 struct io_unlink unlink;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300767 /* use only after cleaning per-op data, see io_clean_op() */
768 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700769 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700770
Jens Axboee8c2bc12020-08-15 18:44:09 -0700771 /* opcode allocated if it needs to store data for async defer */
772 void *async_data;
Jens Axboed625c6e2019-12-17 19:53:05 -0700773 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800774 /* polled IO has completed */
775 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700776
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700777 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300778 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700779
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300780 struct io_ring_ctx *ctx;
781 unsigned int flags;
782 refcount_t refs;
783 struct task_struct *task;
784 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700785
Pavel Begunkovf2f87372020-10-27 23:25:37 +0000786 struct io_kiocb *link;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000787 struct percpu_ref *fixed_rsrc_refs;
Jens Axboed7718a92020-02-14 22:23:12 -0700788
Pavel Begunkovd21ffe72020-07-13 23:37:10 +0300789 /*
790 * 1. used with ctx->iopoll_list with reads/writes
791 * 2. to track reqs with ->files (see io_op_def::file_table)
792 */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300793 struct list_head inflight_entry;
Jens Axboe7cbf1722021-02-10 00:03:20 +0000794 union {
795 struct io_task_work io_task_work;
796 struct callback_head task_work;
797 };
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300798 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
799 struct hlist_node hash_node;
800 struct async_poll *apoll;
801 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700802};
803
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300804struct io_defer_entry {
805 struct list_head list;
806 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300807 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300808};
809
Jens Axboed3656342019-12-18 09:50:26 -0700810struct io_op_def {
Jens Axboed3656342019-12-18 09:50:26 -0700811 /* needs req->file assigned */
812 unsigned needs_file : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700813 /* hash wq insertion if file is a regular file */
814 unsigned hash_reg_file : 1;
815 /* unbound wq insertion if file is a non-regular file */
816 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700817 /* opcode is not supported by this kernel */
818 unsigned not_supported : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700819 /* set if opcode supports polled "wait" */
820 unsigned pollin : 1;
821 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700822 /* op supports buffer selection */
823 unsigned buffer_select : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700824 /* must always have async data allocated */
825 unsigned needs_async_data : 1;
Jens Axboe27926b62020-10-28 09:33:23 -0600826 /* should block plug */
827 unsigned plug : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700828 /* size of async data needed, if any */
829 unsigned short async_size;
Jens Axboe0f203762020-10-14 09:23:55 -0600830 unsigned work_flags;
Jens Axboed3656342019-12-18 09:50:26 -0700831};
832
Jens Axboe09186822020-10-13 15:01:40 -0600833static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300834 [IORING_OP_NOP] = {},
835 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700836 .needs_file = 1,
837 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700838 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700839 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700840 .needs_async_data = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600841 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700842 .async_size = sizeof(struct io_async_rw),
Jens Axboe0f203762020-10-14 09:23:55 -0600843 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700844 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300845 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700846 .needs_file = 1,
847 .hash_reg_file = 1,
848 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700849 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700850 .needs_async_data = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600851 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700852 .async_size = sizeof(struct io_async_rw),
Jens Axboe69228332020-10-20 14:28:41 -0600853 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
854 IO_WQ_WORK_FSIZE,
Jens Axboed3656342019-12-18 09:50:26 -0700855 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300856 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700857 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600858 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700859 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300860 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700861 .needs_file = 1,
862 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700863 .pollin = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600864 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700865 .async_size = sizeof(struct io_async_rw),
Jens Axboe4017eb92020-10-22 14:14:12 -0600866 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700867 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300868 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700869 .needs_file = 1,
870 .hash_reg_file = 1,
871 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700872 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600873 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700874 .async_size = sizeof(struct io_async_rw),
Jens Axboe4017eb92020-10-22 14:14:12 -0600875 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE |
876 IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700877 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300878 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700879 .needs_file = 1,
880 .unbound_nonreg_file = 1,
881 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300882 [IORING_OP_POLL_REMOVE] = {},
883 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700884 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600885 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700886 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300887 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700888 .needs_file = 1,
889 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700890 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700891 .needs_async_data = 1,
892 .async_size = sizeof(struct io_async_msghdr),
Jens Axboe92c75f72021-02-10 12:37:58 -0700893 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
894 IO_WQ_WORK_FS,
Jens Axboed3656342019-12-18 09:50:26 -0700895 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300896 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700897 .needs_file = 1,
898 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700899 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700900 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700901 .needs_async_data = 1,
902 .async_size = sizeof(struct io_async_msghdr),
Jens Axboe92c75f72021-02-10 12:37:58 -0700903 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
904 IO_WQ_WORK_FS,
Jens Axboed3656342019-12-18 09:50:26 -0700905 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300906 [IORING_OP_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700907 .needs_async_data = 1,
908 .async_size = sizeof(struct io_timeout_data),
Jens Axboe0f203762020-10-14 09:23:55 -0600909 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700910 },
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000911 [IORING_OP_TIMEOUT_REMOVE] = {
912 /* used by timeout updates' prep() */
913 .work_flags = IO_WQ_WORK_MM,
914 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300915 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700916 .needs_file = 1,
917 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700918 .pollin = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600919 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES,
Jens Axboed3656342019-12-18 09:50:26 -0700920 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300921 [IORING_OP_ASYNC_CANCEL] = {},
922 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700923 .needs_async_data = 1,
924 .async_size = sizeof(struct io_timeout_data),
Jens Axboe0f203762020-10-14 09:23:55 -0600925 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700926 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300927 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700928 .needs_file = 1,
929 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700930 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700931 .needs_async_data = 1,
932 .async_size = sizeof(struct io_async_connect),
Jens Axboe0f203762020-10-14 09:23:55 -0600933 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700934 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300935 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700936 .needs_file = 1,
Jens Axboe69228332020-10-20 14:28:41 -0600937 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE,
Jens Axboed3656342019-12-18 09:50:26 -0700938 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300939 [IORING_OP_OPENAT] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600940 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG |
Jens Axboe14587a462020-09-05 11:36:08 -0600941 IO_WQ_WORK_FS | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700942 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300943 [IORING_OP_CLOSE] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600944 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700945 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300946 [IORING_OP_FILES_UPDATE] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600947 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700948 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300949 [IORING_OP_STATX] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600950 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM |
951 IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700952 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300953 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700954 .needs_file = 1,
955 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700956 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700957 .buffer_select = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600958 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700959 .async_size = sizeof(struct io_async_rw),
Jens Axboe0f203762020-10-14 09:23:55 -0600960 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700961 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300962 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700963 .needs_file = 1,
964 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700965 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600966 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700967 .async_size = sizeof(struct io_async_rw),
Jens Axboe69228332020-10-20 14:28:41 -0600968 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
969 IO_WQ_WORK_FSIZE,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700970 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300971 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700972 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600973 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboe4840e412019-12-25 22:03:45 -0700974 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300975 [IORING_OP_MADVISE] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600976 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboec1ca7572019-12-25 22:18:28 -0700977 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300978 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700979 .needs_file = 1,
980 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700981 .pollout = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600982 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboefddafac2020-01-04 20:19:44 -0700983 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300984 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700985 .needs_file = 1,
986 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700987 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700988 .buffer_select = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600989 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboefddafac2020-01-04 20:19:44 -0700990 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300991 [IORING_OP_OPENAT2] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600992 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_FS |
Jens Axboe14587a462020-09-05 11:36:08 -0600993 IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM,
Jens Axboecebdb982020-01-08 17:59:24 -0700994 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700995 [IORING_OP_EPOLL_CTL] = {
996 .unbound_nonreg_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600997 .work_flags = IO_WQ_WORK_FILES,
Jens Axboe3e4827b2020-01-08 15:18:09 -0700998 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300999 [IORING_OP_SPLICE] = {
1000 .needs_file = 1,
1001 .hash_reg_file = 1,
1002 .unbound_nonreg_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -06001003 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboeddf0322d2020-02-23 16:41:33 -07001004 },
1005 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -07001006 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03001007 [IORING_OP_TEE] = {
1008 .needs_file = 1,
1009 .hash_reg_file = 1,
1010 .unbound_nonreg_file = 1,
1011 },
Jens Axboe36f4fa62020-09-05 11:14:22 -06001012 [IORING_OP_SHUTDOWN] = {
1013 .needs_file = 1,
1014 },
Jens Axboe80a261f2020-09-28 14:23:58 -06001015 [IORING_OP_RENAMEAT] = {
1016 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES |
1017 IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG,
1018 },
Jens Axboe14a11432020-09-28 14:27:37 -06001019 [IORING_OP_UNLINKAT] = {
1020 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES |
1021 IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG,
1022 },
Jens Axboed3656342019-12-18 09:50:26 -07001023};
1024
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00001025static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
1026 struct task_struct *task,
1027 struct files_struct *files);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001028static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node);
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00001029static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node(
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00001030 struct io_ring_ctx *ctx);
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00001031static void init_fixed_file_ref_node(struct io_ring_ctx *ctx,
1032 struct fixed_rsrc_ref_node *ref_node);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00001033
Pavel Begunkov23faba32021-02-11 18:28:22 +00001034static bool io_rw_reissue(struct io_kiocb *req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001035static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001036static void io_put_req(struct io_kiocb *req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001037static void io_put_req_deferred(struct io_kiocb *req, int nr);
Jens Axboec40f6372020-06-25 15:39:59 -06001038static void io_double_put_req(struct io_kiocb *req);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001039static void io_dismantle_req(struct io_kiocb *req);
1040static void io_put_task(struct task_struct *task, int nr);
1041static void io_queue_next(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001042static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001043static void __io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001044static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001045static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001046 struct io_uring_rsrc_update *ip,
Jens Axboe05f3fb32019-12-09 11:22:50 -07001047 unsigned nr_args);
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001048static void __io_clean_op(struct io_kiocb *req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01001049static struct file *io_file_get(struct io_submit_state *state,
1050 struct io_kiocb *req, int fd, bool fixed);
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00001051static void __io_queue_sqe(struct io_kiocb *req);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001052static void io_rsrc_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -06001053
Pavel Begunkov847595d2021-02-04 13:52:06 +00001054static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec,
1055 struct iov_iter *iter, bool needs_lock);
Jens Axboeff6165b2020-08-13 09:47:43 -06001056static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
1057 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06001058 struct iov_iter *iter, bool force);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001059static void io_req_task_queue(struct io_kiocb *req);
Jens Axboe65453d12021-02-10 00:03:21 +00001060static void io_submit_flush_completions(struct io_comp_state *cs,
1061 struct io_ring_ctx *ctx);
Jens Axboe9a56a232019-01-09 09:06:50 -07001062
Jens Axboe2b188cc2019-01-07 10:46:33 -07001063static struct kmem_cache *req_cachep;
1064
Jens Axboe09186822020-10-13 15:01:40 -06001065static const struct file_operations io_uring_fops;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001066
1067struct sock *io_uring_get_socket(struct file *file)
1068{
1069#if defined(CONFIG_UNIX)
1070 if (file->f_op == &io_uring_fops) {
1071 struct io_ring_ctx *ctx = file->private_data;
1072
1073 return ctx->ring_sock->sk;
1074 }
1075#endif
1076 return NULL;
1077}
1078EXPORT_SYMBOL(io_uring_get_socket);
1079
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001080#define io_for_each_link(pos, head) \
1081 for (pos = (head); pos; pos = pos->link)
1082
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001083static inline void io_clean_op(struct io_kiocb *req)
1084{
Pavel Begunkov9d5c8192021-01-24 15:08:14 +00001085 if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED))
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001086 __io_clean_op(req);
1087}
1088
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001089static inline void io_set_resource_node(struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001090{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001091 struct io_ring_ctx *ctx = req->ctx;
1092
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001093 if (!req->fixed_rsrc_refs) {
1094 req->fixed_rsrc_refs = &ctx->file_data->node->refs;
1095 percpu_ref_get(req->fixed_rsrc_refs);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001096 }
1097}
1098
Pavel Begunkov08d23632020-11-06 13:00:22 +00001099static bool io_match_task(struct io_kiocb *head,
1100 struct task_struct *task,
1101 struct files_struct *files)
1102{
1103 struct io_kiocb *req;
1104
Jens Axboe84965ff2021-01-23 15:51:11 -07001105 if (task && head->task != task) {
1106 /* in terms of cancelation, always match if req task is dead */
1107 if (head->task->flags & PF_EXITING)
1108 return true;
Pavel Begunkov08d23632020-11-06 13:00:22 +00001109 return false;
Jens Axboe84965ff2021-01-23 15:51:11 -07001110 }
Pavel Begunkov08d23632020-11-06 13:00:22 +00001111 if (!files)
1112 return true;
1113
1114 io_for_each_link(req, head) {
Jens Axboe02a13672021-01-23 15:49:31 -07001115 if (!(req->flags & REQ_F_WORK_INITIALIZED))
1116 continue;
1117 if (req->file && req->file->f_op == &io_uring_fops)
1118 return true;
1119 if ((req->work.flags & IO_WQ_WORK_FILES) &&
Pavel Begunkov08d23632020-11-06 13:00:22 +00001120 req->work.identity->files == files)
1121 return true;
1122 }
1123 return false;
1124}
1125
Jens Axboe28cea78a2020-09-14 10:51:17 -06001126static void io_sq_thread_drop_mm_files(void)
Jens Axboec40f6372020-06-25 15:39:59 -06001127{
Jens Axboe28cea78a2020-09-14 10:51:17 -06001128 struct files_struct *files = current->files;
Jens Axboec40f6372020-06-25 15:39:59 -06001129 struct mm_struct *mm = current->mm;
1130
1131 if (mm) {
1132 kthread_unuse_mm(mm);
1133 mmput(mm);
Jens Axboe4b70cf92020-11-02 10:39:05 -07001134 current->mm = NULL;
Jens Axboec40f6372020-06-25 15:39:59 -06001135 }
Jens Axboe28cea78a2020-09-14 10:51:17 -06001136 if (files) {
1137 struct nsproxy *nsproxy = current->nsproxy;
1138
1139 task_lock(current);
1140 current->files = NULL;
1141 current->nsproxy = NULL;
1142 task_unlock(current);
1143 put_files_struct(files);
1144 put_nsproxy(nsproxy);
1145 }
1146}
1147
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001148static int __io_sq_thread_acquire_files(struct io_ring_ctx *ctx)
Jens Axboe28cea78a2020-09-14 10:51:17 -06001149{
1150 if (!current->files) {
1151 struct files_struct *files;
1152 struct nsproxy *nsproxy;
1153
1154 task_lock(ctx->sqo_task);
1155 files = ctx->sqo_task->files;
1156 if (!files) {
1157 task_unlock(ctx->sqo_task);
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001158 return -EOWNERDEAD;
Jens Axboe28cea78a2020-09-14 10:51:17 -06001159 }
1160 atomic_inc(&files->count);
1161 get_nsproxy(ctx->sqo_task->nsproxy);
1162 nsproxy = ctx->sqo_task->nsproxy;
1163 task_unlock(ctx->sqo_task);
1164
1165 task_lock(current);
1166 current->files = files;
1167 current->nsproxy = nsproxy;
1168 task_unlock(current);
1169 }
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001170 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001171}
1172
1173static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
1174{
Jens Axboe4b70cf92020-11-02 10:39:05 -07001175 struct mm_struct *mm;
1176
1177 if (current->mm)
1178 return 0;
1179
Jens Axboe4b70cf92020-11-02 10:39:05 -07001180 task_lock(ctx->sqo_task);
1181 mm = ctx->sqo_task->mm;
1182 if (unlikely(!mm || !mmget_not_zero(mm)))
1183 mm = NULL;
1184 task_unlock(ctx->sqo_task);
1185
1186 if (mm) {
1187 kthread_use_mm(mm);
1188 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001189 }
1190
Jens Axboe4b70cf92020-11-02 10:39:05 -07001191 return -EFAULT;
Jens Axboec40f6372020-06-25 15:39:59 -06001192}
1193
Pavel Begunkov4e326352021-02-12 03:23:52 +00001194static int __io_sq_thread_acquire_mm_files(struct io_ring_ctx *ctx,
1195 struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001196{
Jens Axboe28cea78a2020-09-14 10:51:17 -06001197 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001198 int ret;
Jens Axboe28cea78a2020-09-14 10:51:17 -06001199
1200 if (def->work_flags & IO_WQ_WORK_MM) {
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001201 ret = __io_sq_thread_acquire_mm(ctx);
Jens Axboe28cea78a2020-09-14 10:51:17 -06001202 if (unlikely(ret))
1203 return ret;
1204 }
1205
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001206 if (def->needs_file || (def->work_flags & IO_WQ_WORK_FILES)) {
1207 ret = __io_sq_thread_acquire_files(ctx);
1208 if (unlikely(ret))
1209 return ret;
1210 }
Jens Axboe28cea78a2020-09-14 10:51:17 -06001211
1212 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001213}
1214
Pavel Begunkov4e326352021-02-12 03:23:52 +00001215static inline int io_sq_thread_acquire_mm_files(struct io_ring_ctx *ctx,
1216 struct io_kiocb *req)
1217{
Pavel Begunkov4e326352021-02-12 03:23:52 +00001218 if (!(ctx->flags & IORING_SETUP_SQPOLL))
1219 return 0;
1220 return __io_sq_thread_acquire_mm_files(ctx, req);
1221}
1222
Dennis Zhou91d8f512020-09-16 13:41:05 -07001223static void io_sq_thread_associate_blkcg(struct io_ring_ctx *ctx,
1224 struct cgroup_subsys_state **cur_css)
1225
1226{
1227#ifdef CONFIG_BLK_CGROUP
1228 /* puts the old one when swapping */
1229 if (*cur_css != ctx->sqo_blkcg_css) {
1230 kthread_associate_blkcg(ctx->sqo_blkcg_css);
1231 *cur_css = ctx->sqo_blkcg_css;
1232 }
1233#endif
1234}
1235
1236static void io_sq_thread_unassociate_blkcg(void)
1237{
1238#ifdef CONFIG_BLK_CGROUP
1239 kthread_associate_blkcg(NULL);
1240#endif
1241}
1242
Jens Axboec40f6372020-06-25 15:39:59 -06001243static inline void req_set_fail_links(struct io_kiocb *req)
1244{
1245 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1246 req->flags |= REQ_F_FAIL_LINK;
1247}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001248
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001249/*
Jens Axboe1e6fa522020-10-15 08:46:24 -06001250 * None of these are dereferenced, they are simply used to check if any of
1251 * them have changed. If we're under current and check they are still the
1252 * same, we're fine to grab references to them for actual out-of-line use.
1253 */
1254static void io_init_identity(struct io_identity *id)
1255{
1256 id->files = current->files;
1257 id->mm = current->mm;
1258#ifdef CONFIG_BLK_CGROUP
1259 rcu_read_lock();
1260 id->blkcg_css = blkcg_css();
1261 rcu_read_unlock();
1262#endif
1263 id->creds = current_cred();
1264 id->nsproxy = current->nsproxy;
1265 id->fs = current->fs;
1266 id->fsize = rlimit(RLIMIT_FSIZE);
Jens Axboe4ea33a92020-10-15 13:46:44 -06001267#ifdef CONFIG_AUDIT
1268 id->loginuid = current->loginuid;
1269 id->sessionid = current->sessionid;
1270#endif
Jens Axboe1e6fa522020-10-15 08:46:24 -06001271 refcount_set(&id->count, 1);
1272}
1273
Pavel Begunkovec99ca62020-10-18 10:17:38 +01001274static inline void __io_req_init_async(struct io_kiocb *req)
1275{
1276 memset(&req->work, 0, sizeof(req->work));
1277 req->flags |= REQ_F_WORK_INITIALIZED;
1278}
1279
Jens Axboe1e6fa522020-10-15 08:46:24 -06001280/*
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001281 * Note: must call io_req_init_async() for the first time you
1282 * touch any members of io_wq_work.
1283 */
1284static inline void io_req_init_async(struct io_kiocb *req)
1285{
Jens Axboe500a3732020-10-15 17:38:03 -06001286 struct io_uring_task *tctx = current->io_uring;
1287
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001288 if (req->flags & REQ_F_WORK_INITIALIZED)
1289 return;
1290
Pavel Begunkovec99ca62020-10-18 10:17:38 +01001291 __io_req_init_async(req);
Jens Axboe500a3732020-10-15 17:38:03 -06001292
1293 /* Grab a ref if this isn't our static identity */
1294 req->work.identity = tctx->identity;
1295 if (tctx->identity != &tctx->__identity)
1296 refcount_inc(&req->work.identity->count);
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001297}
1298
Jens Axboe2b188cc2019-01-07 10:46:33 -07001299static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1300{
1301 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1302
Jens Axboe0f158b42020-05-14 17:18:39 -06001303 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001304}
1305
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001306static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1307{
1308 return !req->timeout.off;
1309}
1310
Jens Axboe2b188cc2019-01-07 10:46:33 -07001311static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1312{
1313 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001314 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001315
1316 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1317 if (!ctx)
1318 return NULL;
1319
Jens Axboe78076bb2019-12-04 19:56:40 -07001320 /*
1321 * Use 5 bits less than the max cq entries, that should give us around
1322 * 32 entries per hash list if totally full and uniformly spread.
1323 */
1324 hash_bits = ilog2(p->cq_entries);
1325 hash_bits -= 5;
1326 if (hash_bits <= 0)
1327 hash_bits = 1;
1328 ctx->cancel_hash_bits = hash_bits;
1329 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1330 GFP_KERNEL);
1331 if (!ctx->cancel_hash)
1332 goto err;
1333 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1334
Roman Gushchin21482892019-05-07 10:01:48 -07001335 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001336 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1337 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001338
1339 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001340 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001341 INIT_LIST_HEAD(&ctx->sqd_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001342 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001343 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001344 init_completion(&ctx->ref_comp);
1345 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001346 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001347 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001348 mutex_init(&ctx->uring_lock);
1349 init_waitqueue_head(&ctx->wait);
1350 spin_lock_init(&ctx->completion_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001351 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001352 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001353 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001354 spin_lock_init(&ctx->inflight_lock);
1355 INIT_LIST_HEAD(&ctx->inflight_list);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00001356 spin_lock_init(&ctx->rsrc_ref_lock);
1357 INIT_LIST_HEAD(&ctx->rsrc_ref_list);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001358 INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work);
1359 init_llist_head(&ctx->rsrc_put_llist);
Jens Axboe1b4c3512021-02-10 00:03:19 +00001360 INIT_LIST_HEAD(&ctx->submit_state.comp.free_list);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001361 INIT_LIST_HEAD(&ctx->submit_state.comp.locked_free_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001362 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001363err:
Jens Axboe78076bb2019-12-04 19:56:40 -07001364 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001365 kfree(ctx);
1366 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001367}
1368
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001369static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001370{
Jens Axboe2bc99302020-07-09 09:43:27 -06001371 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1372 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001373
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001374 return seq != ctx->cached_cq_tail
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001375 + READ_ONCE(ctx->cached_cq_overflow);
Jens Axboe2bc99302020-07-09 09:43:27 -06001376 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001377
Bob Liu9d858b22019-11-13 18:06:25 +08001378 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001379}
1380
Jens Axboe5c3462c2020-10-15 09:02:33 -06001381static void io_put_identity(struct io_uring_task *tctx, struct io_kiocb *req)
Jens Axboe1e6fa522020-10-15 08:46:24 -06001382{
Jens Axboe500a3732020-10-15 17:38:03 -06001383 if (req->work.identity == &tctx->__identity)
Jens Axboe1e6fa522020-10-15 08:46:24 -06001384 return;
1385 if (refcount_dec_and_test(&req->work.identity->count))
1386 kfree(req->work.identity);
1387}
1388
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001389static void io_req_clean_work(struct io_kiocb *req)
Jens Axboecccf0ee2020-01-27 16:34:48 -07001390{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001391 if (!(req->flags & REQ_F_WORK_INITIALIZED))
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001392 return;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001393
Pavel Begunkove86d0042021-02-01 18:59:54 +00001394 if (req->work.flags & IO_WQ_WORK_MM)
Jens Axboe98447d62020-10-14 10:48:51 -06001395 mmdrop(req->work.identity->mm);
Dennis Zhou91d8f512020-09-16 13:41:05 -07001396#ifdef CONFIG_BLK_CGROUP
Pavel Begunkove86d0042021-02-01 18:59:54 +00001397 if (req->work.flags & IO_WQ_WORK_BLKCG)
Jens Axboe98447d62020-10-14 10:48:51 -06001398 css_put(req->work.identity->blkcg_css);
Jens Axboedfead8a2020-10-14 10:12:37 -06001399#endif
Pavel Begunkove86d0042021-02-01 18:59:54 +00001400 if (req->work.flags & IO_WQ_WORK_CREDS)
Jens Axboe98447d62020-10-14 10:48:51 -06001401 put_cred(req->work.identity->creds);
Jens Axboedfead8a2020-10-14 10:12:37 -06001402 if (req->work.flags & IO_WQ_WORK_FS) {
Jens Axboe98447d62020-10-14 10:48:51 -06001403 struct fs_struct *fs = req->work.identity->fs;
Jens Axboeff002b32020-02-07 16:05:21 -07001404
Jens Axboe98447d62020-10-14 10:48:51 -06001405 spin_lock(&req->work.identity->fs->lock);
Jens Axboeff002b32020-02-07 16:05:21 -07001406 if (--fs->users)
1407 fs = NULL;
Jens Axboe98447d62020-10-14 10:48:51 -06001408 spin_unlock(&req->work.identity->fs->lock);
Jens Axboeff002b32020-02-07 16:05:21 -07001409 if (fs)
1410 free_fs_struct(fs);
1411 }
Pavel Begunkov34e08fe2021-02-01 18:59:53 +00001412 if (req->work.flags & IO_WQ_WORK_FILES) {
1413 put_files_struct(req->work.identity->files);
1414 put_nsproxy(req->work.identity->nsproxy);
Pavel Begunkov34e08fe2021-02-01 18:59:53 +00001415 }
1416 if (req->flags & REQ_F_INFLIGHT) {
1417 struct io_ring_ctx *ctx = req->ctx;
1418 struct io_uring_task *tctx = req->task->io_uring;
1419 unsigned long flags;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001420
Pavel Begunkov34e08fe2021-02-01 18:59:53 +00001421 spin_lock_irqsave(&ctx->inflight_lock, flags);
1422 list_del(&req->inflight_entry);
1423 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1424 req->flags &= ~REQ_F_INFLIGHT;
1425 if (atomic_read(&tctx->in_idle))
1426 wake_up(&tctx->wait);
1427 }
Jens Axboe51a4cc12020-08-10 10:55:56 -06001428
Pavel Begunkove86d0042021-02-01 18:59:54 +00001429 req->flags &= ~REQ_F_WORK_INITIALIZED;
1430 req->work.flags &= ~(IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | IO_WQ_WORK_FS |
1431 IO_WQ_WORK_CREDS | IO_WQ_WORK_FILES);
Jens Axboe5c3462c2020-10-15 09:02:33 -06001432 io_put_identity(req->task->io_uring, req);
Jens Axboe1e6fa522020-10-15 08:46:24 -06001433}
1434
1435/*
1436 * Create a private copy of io_identity, since some fields don't match
1437 * the current context.
1438 */
1439static bool io_identity_cow(struct io_kiocb *req)
1440{
Jens Axboe5c3462c2020-10-15 09:02:33 -06001441 struct io_uring_task *tctx = current->io_uring;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001442 const struct cred *creds = NULL;
1443 struct io_identity *id;
1444
1445 if (req->work.flags & IO_WQ_WORK_CREDS)
1446 creds = req->work.identity->creds;
1447
1448 id = kmemdup(req->work.identity, sizeof(*id), GFP_KERNEL);
1449 if (unlikely(!id)) {
1450 req->work.flags |= IO_WQ_WORK_CANCEL;
1451 return false;
1452 }
1453
1454 /*
1455 * We can safely just re-init the creds we copied Either the field
1456 * matches the current one, or we haven't grabbed it yet. The only
1457 * exception is ->creds, through registered personalities, so handle
1458 * that one separately.
1459 */
1460 io_init_identity(id);
1461 if (creds)
Pavel Begunkove8c954d2020-12-06 22:22:46 +00001462 id->creds = creds;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001463
1464 /* add one for this request */
1465 refcount_inc(&id->count);
1466
Jens Axboecb8a8ae2020-11-03 12:19:07 -07001467 /* drop tctx and req identity references, if needed */
1468 if (tctx->identity != &tctx->__identity &&
1469 refcount_dec_and_test(&tctx->identity->count))
1470 kfree(tctx->identity);
1471 if (req->work.identity != &tctx->__identity &&
1472 refcount_dec_and_test(&req->work.identity->count))
Jens Axboe1e6fa522020-10-15 08:46:24 -06001473 kfree(req->work.identity);
1474
1475 req->work.identity = id;
Jens Axboe500a3732020-10-15 17:38:03 -06001476 tctx->identity = id;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001477 return true;
1478}
1479
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001480static void io_req_track_inflight(struct io_kiocb *req)
1481{
1482 struct io_ring_ctx *ctx = req->ctx;
1483
1484 if (!(req->flags & REQ_F_INFLIGHT)) {
1485 io_req_init_async(req);
1486 req->flags |= REQ_F_INFLIGHT;
1487
1488 spin_lock_irq(&ctx->inflight_lock);
1489 list_add(&req->inflight_entry, &ctx->inflight_list);
1490 spin_unlock_irq(&ctx->inflight_lock);
1491 }
1492}
1493
Jens Axboe1e6fa522020-10-15 08:46:24 -06001494static bool io_grab_identity(struct io_kiocb *req)
1495{
1496 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe5c3462c2020-10-15 09:02:33 -06001497 struct io_identity *id = req->work.identity;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001498
Jens Axboe69228332020-10-20 14:28:41 -06001499 if (def->work_flags & IO_WQ_WORK_FSIZE) {
1500 if (id->fsize != rlimit(RLIMIT_FSIZE))
1501 return false;
1502 req->work.flags |= IO_WQ_WORK_FSIZE;
1503 }
Jens Axboe1e6fa522020-10-15 08:46:24 -06001504#ifdef CONFIG_BLK_CGROUP
1505 if (!(req->work.flags & IO_WQ_WORK_BLKCG) &&
1506 (def->work_flags & IO_WQ_WORK_BLKCG)) {
1507 rcu_read_lock();
1508 if (id->blkcg_css != blkcg_css()) {
1509 rcu_read_unlock();
1510 return false;
1511 }
1512 /*
1513 * This should be rare, either the cgroup is dying or the task
1514 * is moving cgroups. Just punt to root for the handful of ios.
1515 */
1516 if (css_tryget_online(id->blkcg_css))
1517 req->work.flags |= IO_WQ_WORK_BLKCG;
1518 rcu_read_unlock();
1519 }
1520#endif
1521 if (!(req->work.flags & IO_WQ_WORK_CREDS)) {
1522 if (id->creds != current_cred())
1523 return false;
1524 get_cred(id->creds);
1525 req->work.flags |= IO_WQ_WORK_CREDS;
1526 }
Jens Axboe4ea33a92020-10-15 13:46:44 -06001527#ifdef CONFIG_AUDIT
1528 if (!uid_eq(current->loginuid, id->loginuid) ||
1529 current->sessionid != id->sessionid)
1530 return false;
1531#endif
Jens Axboe1e6fa522020-10-15 08:46:24 -06001532 if (!(req->work.flags & IO_WQ_WORK_FS) &&
1533 (def->work_flags & IO_WQ_WORK_FS)) {
1534 if (current->fs != id->fs)
1535 return false;
1536 spin_lock(&id->fs->lock);
1537 if (!id->fs->in_exec) {
1538 id->fs->users++;
1539 req->work.flags |= IO_WQ_WORK_FS;
1540 } else {
1541 req->work.flags |= IO_WQ_WORK_CANCEL;
1542 }
1543 spin_unlock(&current->fs->lock);
1544 }
Pavel Begunkovaf604702020-11-25 18:41:28 +00001545 if (!(req->work.flags & IO_WQ_WORK_FILES) &&
1546 (def->work_flags & IO_WQ_WORK_FILES) &&
1547 !(req->flags & REQ_F_NO_FILE_TABLE)) {
1548 if (id->files != current->files ||
1549 id->nsproxy != current->nsproxy)
1550 return false;
1551 atomic_inc(&id->files->count);
1552 get_nsproxy(id->nsproxy);
Pavel Begunkovaf604702020-11-25 18:41:28 +00001553 req->work.flags |= IO_WQ_WORK_FILES;
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001554 io_req_track_inflight(req);
Pavel Begunkovaf604702020-11-25 18:41:28 +00001555 }
Jens Axboe77788772020-12-29 10:50:46 -07001556 if (!(req->work.flags & IO_WQ_WORK_MM) &&
1557 (def->work_flags & IO_WQ_WORK_MM)) {
1558 if (id->mm != current->mm)
1559 return false;
1560 mmgrab(id->mm);
1561 req->work.flags |= IO_WQ_WORK_MM;
1562 }
Jens Axboe1e6fa522020-10-15 08:46:24 -06001563
1564 return true;
Jens Axboe561fb042019-10-24 07:25:42 -06001565}
1566
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001567static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001568{
Jens Axboed3656342019-12-18 09:50:26 -07001569 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov23329512020-10-10 18:34:06 +01001570 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe54a91f32019-09-10 09:15:04 -06001571
Pavel Begunkov16d59802020-07-12 16:16:47 +03001572 io_req_init_async(req);
1573
Pavel Begunkovfeaadc42020-10-22 16:47:16 +01001574 if (req->flags & REQ_F_FORCE_ASYNC)
1575 req->work.flags |= IO_WQ_WORK_CONCURRENT;
1576
Jens Axboed3656342019-12-18 09:50:26 -07001577 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov23329512020-10-10 18:34:06 +01001578 if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001579 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001580 } else {
1581 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001582 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001583 }
Pavel Begunkov23329512020-10-10 18:34:06 +01001584
Jens Axboe1e6fa522020-10-15 08:46:24 -06001585 /* if we fail grabbing identity, we must COW, regrab, and retry */
1586 if (io_grab_identity(req))
1587 return;
1588
1589 if (!io_identity_cow(req))
1590 return;
1591
1592 /* can't fail at this point */
1593 if (!io_grab_identity(req))
1594 WARN_ON(1);
Jens Axboe561fb042019-10-24 07:25:42 -06001595}
1596
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001597static void io_prep_async_link(struct io_kiocb *req)
1598{
1599 struct io_kiocb *cur;
1600
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001601 io_for_each_link(cur, req)
1602 io_prep_async_work(cur);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001603}
1604
Jens Axboe7271ef32020-08-10 09:55:22 -06001605static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001606{
Jackie Liua197f662019-11-08 08:09:12 -07001607 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001608 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001609
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001610 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1611 &req->work, req->flags);
1612 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001613 return link;
Jens Axboe18d9be12019-09-10 09:13:05 -06001614}
1615
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001616static void io_queue_async_work(struct io_kiocb *req)
1617{
Jens Axboe7271ef32020-08-10 09:55:22 -06001618 struct io_kiocb *link;
1619
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001620 /* init ->work of the whole link before punting */
1621 io_prep_async_link(req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001622 link = __io_queue_async_work(req);
1623
1624 if (link)
1625 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001626}
1627
Jens Axboe5262f562019-09-17 12:26:57 -06001628static void io_kill_timeout(struct io_kiocb *req)
1629{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001630 struct io_timeout_data *io = req->async_data;
Jens Axboe5262f562019-09-17 12:26:57 -06001631 int ret;
1632
Jens Axboee8c2bc12020-08-15 18:44:09 -07001633 ret = hrtimer_try_to_cancel(&io->timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001634 if (ret != -1) {
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001635 atomic_set(&req->ctx->cq_timeouts,
1636 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001637 list_del_init(&req->timeout.list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001638 io_cqring_fill_event(req, 0);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001639 io_put_req_deferred(req, 1);
Jens Axboe5262f562019-09-17 12:26:57 -06001640 }
1641}
1642
Jens Axboe76e1b642020-09-26 15:05:03 -06001643/*
1644 * Returns true if we found and killed one or more timeouts
1645 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00001646static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk,
1647 struct files_struct *files)
Jens Axboe5262f562019-09-17 12:26:57 -06001648{
1649 struct io_kiocb *req, *tmp;
Jens Axboe76e1b642020-09-26 15:05:03 -06001650 int canceled = 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001651
1652 spin_lock_irq(&ctx->completion_lock);
Jens Axboef3606e32020-09-22 08:18:24 -06001653 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Pavel Begunkov6b819282020-11-06 13:00:25 +00001654 if (io_match_task(req, tsk, files)) {
Jens Axboef3606e32020-09-22 08:18:24 -06001655 io_kill_timeout(req);
Jens Axboe76e1b642020-09-26 15:05:03 -06001656 canceled++;
1657 }
Jens Axboef3606e32020-09-22 08:18:24 -06001658 }
Jens Axboe5262f562019-09-17 12:26:57 -06001659 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe76e1b642020-09-26 15:05:03 -06001660 return canceled != 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001661}
1662
Pavel Begunkov04518942020-05-26 20:34:05 +03001663static void __io_queue_deferred(struct io_ring_ctx *ctx)
1664{
1665 do {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001666 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1667 struct io_defer_entry, list);
Pavel Begunkov04518942020-05-26 20:34:05 +03001668
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001669 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001670 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001671 list_del_init(&de->list);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001672 io_req_task_queue(de->req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001673 kfree(de);
Pavel Begunkov04518942020-05-26 20:34:05 +03001674 } while (!list_empty(&ctx->defer_list));
1675}
1676
Pavel Begunkov360428f2020-05-30 14:54:17 +03001677static void io_flush_timeouts(struct io_ring_ctx *ctx)
1678{
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001679 u32 seq;
1680
1681 if (list_empty(&ctx->timeout_list))
1682 return;
1683
1684 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
1685
1686 do {
1687 u32 events_needed, events_got;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001688 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001689 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001690
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001691 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001692 break;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001693
1694 /*
1695 * Since seq can easily wrap around over time, subtract
1696 * the last seq at which timeouts were flushed before comparing.
1697 * Assuming not more than 2^31-1 events have happened since,
1698 * these subtractions won't have wrapped, so we can check if
1699 * target is in [last_seq, current_seq] by comparing the two.
1700 */
1701 events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush;
1702 events_got = seq - ctx->cq_last_tm_flush;
1703 if (events_got < events_needed)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001704 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001705
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001706 list_del_init(&req->timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001707 io_kill_timeout(req);
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001708 } while (!list_empty(&ctx->timeout_list));
1709
1710 ctx->cq_last_tm_flush = seq;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001711}
1712
Jens Axboede0617e2019-04-06 21:51:27 -06001713static void io_commit_cqring(struct io_ring_ctx *ctx)
1714{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001715 io_flush_timeouts(ctx);
Pavel Begunkovec30e042021-01-19 13:32:38 +00001716
1717 /* order cqe stores with ring update */
1718 smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail);
Jens Axboede0617e2019-04-06 21:51:27 -06001719
Pavel Begunkov04518942020-05-26 20:34:05 +03001720 if (unlikely(!list_empty(&ctx->defer_list)))
1721 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001722}
1723
Jens Axboe90554202020-09-03 12:12:41 -06001724static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1725{
1726 struct io_rings *r = ctx->rings;
1727
1728 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries;
1729}
1730
Pavel Begunkov888aae22021-01-19 13:32:39 +00001731static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx)
1732{
1733 return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head);
1734}
1735
Jens Axboe2b188cc2019-01-07 10:46:33 -07001736static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1737{
Hristo Venev75b28af2019-08-26 17:23:46 +00001738 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001739 unsigned tail;
1740
Stefan Bühler115e12e2019-04-24 23:54:18 +02001741 /*
1742 * writes to the cq entry need to come after reading head; the
1743 * control dependency is enough as we're using WRITE_ONCE to
1744 * fill the cq entry
1745 */
Pavel Begunkov888aae22021-01-19 13:32:39 +00001746 if (__io_cqring_events(ctx) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001747 return NULL;
1748
Pavel Begunkov888aae22021-01-19 13:32:39 +00001749 tail = ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001750 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001751}
1752
Jens Axboef2842ab2020-01-08 11:04:00 -07001753static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1754{
Jens Axboef0b493e2020-02-01 21:30:11 -07001755 if (!ctx->cq_ev_fd)
1756 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001757 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1758 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001759 if (!ctx->eventfd_async)
1760 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001761 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001762}
1763
Jens Axboeb41e9852020-02-17 09:52:41 -07001764static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001765{
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001766 /* see waitqueue_active() comment */
1767 smp_mb();
1768
Jens Axboe8c838782019-03-12 15:48:16 -06001769 if (waitqueue_active(&ctx->wait))
1770 wake_up(&ctx->wait);
Jens Axboe534ca6d2020-09-02 13:52:19 -06001771 if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait))
1772 wake_up(&ctx->sq_data->wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001773 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001774 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001775 if (waitqueue_active(&ctx->cq_wait)) {
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001776 wake_up_interruptible(&ctx->cq_wait);
1777 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1778 }
Jens Axboe8c838782019-03-12 15:48:16 -06001779}
1780
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001781static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx)
1782{
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001783 /* see waitqueue_active() comment */
1784 smp_mb();
1785
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001786 if (ctx->flags & IORING_SETUP_SQPOLL) {
1787 if (waitqueue_active(&ctx->wait))
1788 wake_up(&ctx->wait);
1789 }
1790 if (io_should_trigger_evfd(ctx))
1791 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001792 if (waitqueue_active(&ctx->cq_wait)) {
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001793 wake_up_interruptible(&ctx->cq_wait);
1794 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1795 }
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001796}
1797
Jens Axboec4a2ed72019-11-21 21:01:26 -07001798/* Returns true if there are no backlogged entries after the flush */
Pavel Begunkov6c503152021-01-04 20:36:36 +00001799static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1800 struct task_struct *tsk,
1801 struct files_struct *files)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001802{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001803 struct io_rings *rings = ctx->rings;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001804 struct io_kiocb *req, *tmp;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001805 struct io_uring_cqe *cqe;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001806 unsigned long flags;
Jens Axboeb18032b2021-01-24 16:58:56 -07001807 bool all_flushed, posted;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001808 LIST_HEAD(list);
1809
Pavel Begunkove23de152020-12-17 00:24:37 +00001810 if (!force && __io_cqring_events(ctx) == rings->cq_ring_entries)
1811 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001812
Jens Axboeb18032b2021-01-24 16:58:56 -07001813 posted = false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001814 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboee6c8aa92020-09-28 13:10:13 -06001815 list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) {
Pavel Begunkov08d23632020-11-06 13:00:22 +00001816 if (!io_match_task(req, tsk, files))
Jens Axboee6c8aa92020-09-28 13:10:13 -06001817 continue;
1818
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001819 cqe = io_get_cqring(ctx);
1820 if (!cqe && !force)
1821 break;
1822
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001823 list_move(&req->compl.list, &list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001824 if (cqe) {
1825 WRITE_ONCE(cqe->user_data, req->user_data);
1826 WRITE_ONCE(cqe->res, req->result);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001827 WRITE_ONCE(cqe->flags, req->compl.cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001828 } else {
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001829 ctx->cached_cq_overflow++;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001830 WRITE_ONCE(ctx->rings->cq_overflow,
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001831 ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001832 }
Jens Axboeb18032b2021-01-24 16:58:56 -07001833 posted = true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001834 }
1835
Pavel Begunkov09e88402020-12-17 00:24:38 +00001836 all_flushed = list_empty(&ctx->cq_overflow_list);
1837 if (all_flushed) {
1838 clear_bit(0, &ctx->sq_check_overflow);
1839 clear_bit(0, &ctx->cq_check_overflow);
1840 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
1841 }
Pavel Begunkov46930142020-07-30 18:43:49 +03001842
Jens Axboeb18032b2021-01-24 16:58:56 -07001843 if (posted)
1844 io_commit_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001845 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeb18032b2021-01-24 16:58:56 -07001846 if (posted)
1847 io_cqring_ev_posted(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001848
1849 while (!list_empty(&list)) {
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001850 req = list_first_entry(&list, struct io_kiocb, compl.list);
1851 list_del(&req->compl.list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001852 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001853 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001854
Pavel Begunkov09e88402020-12-17 00:24:38 +00001855 return all_flushed;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001856}
1857
Pavel Begunkov6c503152021-01-04 20:36:36 +00001858static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1859 struct task_struct *tsk,
1860 struct files_struct *files)
1861{
1862 if (test_bit(0, &ctx->cq_check_overflow)) {
1863 /* iopoll syncs against uring_lock, not completion_lock */
1864 if (ctx->flags & IORING_SETUP_IOPOLL)
1865 mutex_lock(&ctx->uring_lock);
1866 __io_cqring_overflow_flush(ctx, force, tsk, files);
1867 if (ctx->flags & IORING_SETUP_IOPOLL)
1868 mutex_unlock(&ctx->uring_lock);
1869 }
1870}
1871
Jens Axboebcda7ba2020-02-23 16:42:51 -07001872static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001873{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001874 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001875 struct io_uring_cqe *cqe;
1876
Jens Axboe78e19bb2019-11-06 15:21:34 -07001877 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001878
Jens Axboe2b188cc2019-01-07 10:46:33 -07001879 /*
1880 * If we can't get a cq entry, userspace overflowed the
1881 * submission (by quite a lot). Increment the overflow count in
1882 * the ring.
1883 */
1884 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001885 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001886 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001887 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001888 WRITE_ONCE(cqe->flags, cflags);
Jens Axboefdaf0832020-10-30 09:37:30 -06001889 } else if (ctx->cq_overflow_flushed ||
1890 atomic_read(&req->task->io_uring->in_idle)) {
Jens Axboe0f212202020-09-13 13:09:39 -06001891 /*
1892 * If we're in ring overflow flush mode, or in task cancel mode,
1893 * then we cannot store the request for later flushing, we need
1894 * to drop it on the floor.
1895 */
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001896 ctx->cached_cq_overflow++;
1897 WRITE_ONCE(ctx->rings->cq_overflow, ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001898 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001899 if (list_empty(&ctx->cq_overflow_list)) {
1900 set_bit(0, &ctx->sq_check_overflow);
1901 set_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001902 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001903 }
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001904 io_clean_op(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001905 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001906 req->compl.cflags = cflags;
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001907 refcount_inc(&req->refs);
1908 list_add_tail(&req->compl.list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001909 }
1910}
1911
Jens Axboebcda7ba2020-02-23 16:42:51 -07001912static void io_cqring_fill_event(struct io_kiocb *req, long res)
1913{
1914 __io_cqring_fill_event(req, res, 0);
1915}
1916
Jens Axboec7dae4b2021-02-09 19:53:37 -07001917static inline void io_req_complete_post(struct io_kiocb *req, long res,
1918 unsigned int cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001919{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001920 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001921 unsigned long flags;
1922
1923 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001924 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001925 io_commit_cqring(ctx);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001926 /*
1927 * If we're the last reference to this request, add to our locked
1928 * free_list cache.
1929 */
1930 if (refcount_dec_and_test(&req->refs)) {
1931 struct io_comp_state *cs = &ctx->submit_state.comp;
1932
1933 io_dismantle_req(req);
1934 io_put_task(req->task, 1);
1935 list_add(&req->compl.list, &cs->locked_free_list);
1936 cs->locked_free_nr++;
1937 } else
1938 req = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001939 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1940
Jens Axboe8c838782019-03-12 15:48:16 -06001941 io_cqring_ev_posted(ctx);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001942 if (req) {
1943 io_queue_next(req);
1944 percpu_ref_put(&ctx->refs);
1945 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001946}
1947
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001948static void io_req_complete_state(struct io_kiocb *req, long res,
Pavel Begunkov889fca72021-02-10 00:03:09 +00001949 unsigned int cflags)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001950{
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001951 io_clean_op(req);
1952 req->result = res;
1953 req->compl.cflags = cflags;
Pavel Begunkove342c802021-01-19 13:32:47 +00001954 req->flags |= REQ_F_COMPLETE_INLINE;
Jens Axboee1e16092020-06-22 09:17:17 -06001955}
Jens Axboe2b188cc2019-01-07 10:46:33 -07001956
Pavel Begunkov889fca72021-02-10 00:03:09 +00001957static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags,
1958 long res, unsigned cflags)
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001959{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001960 if (issue_flags & IO_URING_F_COMPLETE_DEFER)
1961 io_req_complete_state(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001962 else
Jens Axboec7dae4b2021-02-09 19:53:37 -07001963 io_req_complete_post(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001964}
Jens Axboebcda7ba2020-02-23 16:42:51 -07001965
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001966static inline void io_req_complete(struct io_kiocb *req, long res)
Jens Axboee1e16092020-06-22 09:17:17 -06001967{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001968 __io_req_complete(req, 0, res, 0);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001969}
1970
Jens Axboec7dae4b2021-02-09 19:53:37 -07001971static bool io_flush_cached_reqs(struct io_ring_ctx *ctx)
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001972{
Jens Axboec7dae4b2021-02-09 19:53:37 -07001973 struct io_submit_state *state = &ctx->submit_state;
1974 struct io_comp_state *cs = &state->comp;
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001975 struct io_kiocb *req = NULL;
1976
Jens Axboec7dae4b2021-02-09 19:53:37 -07001977 /*
1978 * If we have more than a batch's worth of requests in our IRQ side
1979 * locked cache, grab the lock and move them over to our submission
1980 * side cache.
1981 */
1982 if (READ_ONCE(cs->locked_free_nr) > IO_COMPL_BATCH) {
1983 spin_lock_irq(&ctx->completion_lock);
1984 list_splice_init(&cs->locked_free_list, &cs->free_list);
1985 cs->locked_free_nr = 0;
1986 spin_unlock_irq(&ctx->completion_lock);
1987 }
1988
1989 while (!list_empty(&cs->free_list)) {
1990 req = list_first_entry(&cs->free_list, struct io_kiocb,
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001991 compl.list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001992 list_del(&req->compl.list);
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001993 state->reqs[state->free_reqs++] = req;
1994 if (state->free_reqs == ARRAY_SIZE(state->reqs))
1995 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001996 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001997
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001998 return req != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001999}
2000
Pavel Begunkov258b29a2021-02-10 00:03:10 +00002001static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002002{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00002003 struct io_submit_state *state = &ctx->submit_state;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002004
Pavel Begunkovbf019da2021-02-10 00:03:17 +00002005 BUILD_BUG_ON(IO_REQ_ALLOC_BATCH > ARRAY_SIZE(state->reqs));
Jens Axboe2b188cc2019-01-07 10:46:33 -07002006
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03002007 if (!state->free_reqs) {
Pavel Begunkov291b2822020-09-30 22:57:01 +03002008 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2579f912019-01-09 09:10:43 -07002009 int ret;
2010
Jens Axboec7dae4b2021-02-09 19:53:37 -07002011 if (io_flush_cached_reqs(ctx))
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00002012 goto got_req;
2013
Pavel Begunkovbf019da2021-02-10 00:03:17 +00002014 ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH,
2015 state->reqs);
Jens Axboefd6fab22019-03-14 16:30:06 -06002016
2017 /*
2018 * Bulk alloc is all-or-nothing. If we fail to get a batch,
2019 * retry single alloc to be on the safe side.
2020 */
2021 if (unlikely(ret <= 0)) {
2022 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
2023 if (!state->reqs[0])
Pavel Begunkov3893f392021-02-10 00:03:15 +00002024 return NULL;
Jens Axboefd6fab22019-03-14 16:30:06 -06002025 ret = 1;
2026 }
Pavel Begunkov291b2822020-09-30 22:57:01 +03002027 state->free_reqs = ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002028 }
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00002029got_req:
Pavel Begunkov291b2822020-09-30 22:57:01 +03002030 state->free_reqs--;
2031 return state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07002032}
2033
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002034static inline void io_put_file(struct io_kiocb *req, struct file *file,
2035 bool fixed)
2036{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00002037 if (!fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002038 fput(file);
2039}
2040
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002041static void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002042{
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03002043 io_clean_op(req);
Pavel Begunkov929a3af2020-02-19 00:19:09 +03002044
Jens Axboee8c2bc12020-08-15 18:44:09 -07002045 if (req->async_data)
2046 kfree(req->async_data);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002047 if (req->file)
2048 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00002049 if (req->fixed_rsrc_refs)
2050 percpu_ref_put(req->fixed_rsrc_refs);
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002051 io_req_clean_work(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03002052}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03002053
Pavel Begunkov7c660732021-01-25 11:42:21 +00002054static inline void io_put_task(struct task_struct *task, int nr)
2055{
2056 struct io_uring_task *tctx = task->io_uring;
2057
2058 percpu_counter_sub(&tctx->inflight, nr);
2059 if (unlikely(atomic_read(&tctx->in_idle)))
2060 wake_up(&tctx->wait);
2061 put_task_struct_many(task, nr);
2062}
2063
Pavel Begunkov216578e2020-10-13 09:44:00 +01002064static void __io_free_req(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03002065{
Jens Axboe51a4cc12020-08-10 10:55:56 -06002066 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03002067
Pavel Begunkov216578e2020-10-13 09:44:00 +01002068 io_dismantle_req(req);
Pavel Begunkov7c660732021-01-25 11:42:21 +00002069 io_put_task(req->task, 1);
Pavel Begunkove6543a82020-06-28 12:52:30 +03002070
Pavel Begunkov3893f392021-02-10 00:03:15 +00002071 kmem_cache_free(req_cachep, req);
Pavel Begunkovecfc5172020-06-29 13:13:03 +03002072 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06002073}
2074
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002075static inline void io_remove_next_linked(struct io_kiocb *req)
2076{
2077 struct io_kiocb *nxt = req->link;
2078
2079 req->link = nxt->link;
2080 nxt->link = NULL;
2081}
2082
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002083static void io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002084{
Jackie Liua197f662019-11-08 08:09:12 -07002085 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002086 struct io_kiocb *link;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002087 bool cancelled = false;
2088 unsigned long flags;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002089
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002090 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002091 link = req->link;
2092
Pavel Begunkov900fad42020-10-19 16:39:16 +01002093 /*
2094 * Can happen if a linked timeout fired and link had been like
2095 * req -> link t-out -> link t-out [-> ...]
2096 */
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002097 if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) {
2098 struct io_timeout_data *io = link->async_data;
2099 int ret;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002100
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002101 io_remove_next_linked(req);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00002102 link->timeout.head = NULL;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002103 ret = hrtimer_try_to_cancel(&io->timer);
2104 if (ret != -1) {
2105 io_cqring_fill_event(link, -ECANCELED);
2106 io_commit_cqring(ctx);
2107 cancelled = true;
2108 }
2109 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002110 req->flags &= ~REQ_F_LINK_TIMEOUT;
Pavel Begunkov216578e2020-10-13 09:44:00 +01002111 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeab0b6452020-06-30 08:43:15 -06002112
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002113 if (cancelled) {
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002114 io_cqring_ev_posted(ctx);
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002115 io_put_req(link);
2116 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002117}
2118
Jens Axboe4d7dd462019-11-20 13:03:52 -07002119
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002120static void io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002121{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002122 struct io_kiocb *link, *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07002123 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002124 unsigned long flags;
Jens Axboe9e645e112019-05-10 16:07:28 -06002125
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002126 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002127 link = req->link;
2128 req->link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002129
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002130 while (link) {
2131 nxt = link->link;
2132 link->link = NULL;
2133
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002134 trace_io_uring_fail_link(req, link);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002135 io_cqring_fill_event(link, -ECANCELED);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002136
2137 /*
2138 * It's ok to free under spinlock as they're not linked anymore,
2139 * but avoid REQ_F_WORK_INITIALIZED because it may deadlock on
2140 * work.fs->lock.
2141 */
2142 if (link->flags & REQ_F_WORK_INITIALIZED)
2143 io_put_req_deferred(link, 2);
2144 else
2145 io_double_put_req(link);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002146 link = nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06002147 }
Jens Axboe2665abf2019-11-05 12:40:47 -07002148 io_commit_cqring(ctx);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002149 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002150
Jens Axboe2665abf2019-11-05 12:40:47 -07002151 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06002152}
2153
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002154static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002155{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002156 if (req->flags & REQ_F_LINK_TIMEOUT)
2157 io_kill_linked_timeout(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002158
Jens Axboe9e645e112019-05-10 16:07:28 -06002159 /*
2160 * If LINK is set, we have dependent requests in this chain. If we
2161 * didn't fail this request, queue the first one up, moving any other
2162 * dependencies to the next request. In case of failure, fail the rest
2163 * of the chain.
2164 */
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002165 if (likely(!(req->flags & REQ_F_FAIL_LINK))) {
2166 struct io_kiocb *nxt = req->link;
2167
2168 req->link = NULL;
2169 return nxt;
2170 }
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002171 io_fail_links(req);
2172 return NULL;
Jens Axboe4d7dd462019-11-20 13:03:52 -07002173}
Jens Axboe2665abf2019-11-05 12:40:47 -07002174
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002175static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req)
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002176{
Pavel Begunkovcdbff982021-02-12 18:41:16 +00002177 if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK))))
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002178 return NULL;
2179 return __io_req_find_next(req);
2180}
2181
Jens Axboe7cbf1722021-02-10 00:03:20 +00002182static bool __tctx_task_work(struct io_uring_task *tctx)
2183{
Jens Axboe65453d12021-02-10 00:03:21 +00002184 struct io_ring_ctx *ctx = NULL;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002185 struct io_wq_work_list list;
2186 struct io_wq_work_node *node;
2187
2188 if (wq_list_empty(&tctx->task_list))
2189 return false;
2190
Jens Axboe0b81e802021-02-16 10:33:53 -07002191 spin_lock_irq(&tctx->task_lock);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002192 list = tctx->task_list;
2193 INIT_WQ_LIST(&tctx->task_list);
Jens Axboe0b81e802021-02-16 10:33:53 -07002194 spin_unlock_irq(&tctx->task_lock);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002195
2196 node = list.first;
2197 while (node) {
2198 struct io_wq_work_node *next = node->next;
Jens Axboe65453d12021-02-10 00:03:21 +00002199 struct io_ring_ctx *this_ctx;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002200 struct io_kiocb *req;
2201
2202 req = container_of(node, struct io_kiocb, io_task_work.node);
Jens Axboe65453d12021-02-10 00:03:21 +00002203 this_ctx = req->ctx;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002204 req->task_work.func(&req->task_work);
2205 node = next;
Jens Axboe65453d12021-02-10 00:03:21 +00002206
2207 if (!ctx) {
2208 ctx = this_ctx;
2209 } else if (ctx != this_ctx) {
2210 mutex_lock(&ctx->uring_lock);
2211 io_submit_flush_completions(&ctx->submit_state.comp, ctx);
2212 mutex_unlock(&ctx->uring_lock);
2213 ctx = this_ctx;
2214 }
2215 }
2216
2217 if (ctx && ctx->submit_state.comp.nr) {
2218 mutex_lock(&ctx->uring_lock);
2219 io_submit_flush_completions(&ctx->submit_state.comp, ctx);
2220 mutex_unlock(&ctx->uring_lock);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002221 }
2222
2223 return list.first != NULL;
2224}
2225
2226static void tctx_task_work(struct callback_head *cb)
2227{
2228 struct io_uring_task *tctx = container_of(cb, struct io_uring_task, task_work);
2229
2230 while (__tctx_task_work(tctx))
2231 cond_resched();
2232
2233 clear_bit(0, &tctx->task_state);
2234}
2235
2236static int io_task_work_add(struct task_struct *tsk, struct io_kiocb *req,
2237 enum task_work_notify_mode notify)
2238{
2239 struct io_uring_task *tctx = tsk->io_uring;
2240 struct io_wq_work_node *node, *prev;
Jens Axboe0b81e802021-02-16 10:33:53 -07002241 unsigned long flags;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002242 int ret;
2243
2244 WARN_ON_ONCE(!tctx);
2245
Jens Axboe0b81e802021-02-16 10:33:53 -07002246 spin_lock_irqsave(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002247 wq_list_add_tail(&req->io_task_work.node, &tctx->task_list);
Jens Axboe0b81e802021-02-16 10:33:53 -07002248 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002249
2250 /* task_work already pending, we're done */
2251 if (test_bit(0, &tctx->task_state) ||
2252 test_and_set_bit(0, &tctx->task_state))
2253 return 0;
2254
2255 if (!task_work_add(tsk, &tctx->task_work, notify))
2256 return 0;
2257
2258 /*
2259 * Slow path - we failed, find and delete work. if the work is not
2260 * in the list, it got run and we're fine.
2261 */
2262 ret = 0;
Jens Axboe0b81e802021-02-16 10:33:53 -07002263 spin_lock_irqsave(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002264 wq_list_for_each(node, prev, &tctx->task_list) {
2265 if (&req->io_task_work.node == node) {
2266 wq_list_del(&tctx->task_list, node, prev);
2267 ret = 1;
2268 break;
2269 }
2270 }
Jens Axboe0b81e802021-02-16 10:33:53 -07002271 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002272 clear_bit(0, &tctx->task_state);
2273 return ret;
2274}
2275
Jens Axboe355fb9e2020-10-22 20:19:35 -06002276static int io_req_task_work_add(struct io_kiocb *req)
Jens Axboec2c4c832020-07-01 15:37:11 -06002277{
2278 struct task_struct *tsk = req->task;
2279 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe91989c72020-10-16 09:02:26 -06002280 enum task_work_notify_mode notify;
2281 int ret;
Jens Axboec2c4c832020-07-01 15:37:11 -06002282
Jens Axboe6200b0a2020-09-13 14:38:30 -06002283 if (tsk->flags & PF_EXITING)
2284 return -ESRCH;
2285
Jens Axboec2c4c832020-07-01 15:37:11 -06002286 /*
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06002287 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
2288 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
2289 * processing task_work. There's no reliable way to tell if TWA_RESUME
2290 * will do the job.
Jens Axboec2c4c832020-07-01 15:37:11 -06002291 */
Jens Axboe91989c72020-10-16 09:02:26 -06002292 notify = TWA_NONE;
Jens Axboe355fb9e2020-10-22 20:19:35 -06002293 if (!(ctx->flags & IORING_SETUP_SQPOLL))
Jens Axboec2c4c832020-07-01 15:37:11 -06002294 notify = TWA_SIGNAL;
2295
Jens Axboe7cbf1722021-02-10 00:03:20 +00002296 ret = io_task_work_add(tsk, req, notify);
Jens Axboec2c4c832020-07-01 15:37:11 -06002297 if (!ret)
2298 wake_up_process(tsk);
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06002299
Jens Axboec2c4c832020-07-01 15:37:11 -06002300 return ret;
2301}
2302
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002303static void io_req_task_work_add_fallback(struct io_kiocb *req,
Jens Axboe7cbf1722021-02-10 00:03:20 +00002304 task_work_func_t cb)
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002305{
2306 struct task_struct *tsk = io_wq_get_task(req->ctx->io_wq);
2307
2308 init_task_work(&req->task_work, cb);
2309 task_work_add(tsk, &req->task_work, TWA_NONE);
2310 wake_up_process(tsk);
2311}
2312
Jens Axboec40f6372020-06-25 15:39:59 -06002313static void __io_req_task_cancel(struct io_kiocb *req, int error)
2314{
2315 struct io_ring_ctx *ctx = req->ctx;
2316
2317 spin_lock_irq(&ctx->completion_lock);
2318 io_cqring_fill_event(req, error);
2319 io_commit_cqring(ctx);
2320 spin_unlock_irq(&ctx->completion_lock);
2321
2322 io_cqring_ev_posted(ctx);
2323 req_set_fail_links(req);
2324 io_double_put_req(req);
2325}
2326
2327static void io_req_task_cancel(struct callback_head *cb)
2328{
2329 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002330 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002331
2332 __io_req_task_cancel(req, -ECANCELED);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002333 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002334}
2335
2336static void __io_req_task_submit(struct io_kiocb *req)
2337{
2338 struct io_ring_ctx *ctx = req->ctx;
2339
Pavel Begunkov04fc6c82021-02-12 03:23:54 +00002340 /* ctx stays valid until unlock, even if we drop all ours ctx->refs */
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002341 mutex_lock(&ctx->uring_lock);
Pavel Begunkovdc0eced2021-02-12 18:41:15 +00002342 if (!ctx->sqo_dead && !(current->flags & PF_EXITING) &&
2343 !io_sq_thread_acquire_mm_files(ctx, req))
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00002344 __io_queue_sqe(req);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002345 else
Jens Axboec40f6372020-06-25 15:39:59 -06002346 __io_req_task_cancel(req, -EFAULT);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002347 mutex_unlock(&ctx->uring_lock);
Pavel Begunkovaec18a52021-02-04 19:22:46 +00002348
2349 if (ctx->flags & IORING_SETUP_SQPOLL)
2350 io_sq_thread_drop_mm_files();
Jens Axboe9e645e112019-05-10 16:07:28 -06002351}
2352
Jens Axboec40f6372020-06-25 15:39:59 -06002353static void io_req_task_submit(struct callback_head *cb)
2354{
2355 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2356
2357 __io_req_task_submit(req);
2358}
2359
2360static void io_req_task_queue(struct io_kiocb *req)
2361{
Jens Axboec40f6372020-06-25 15:39:59 -06002362 int ret;
2363
Jens Axboe7cbf1722021-02-10 00:03:20 +00002364 req->task_work.func = io_req_task_submit;
Jens Axboe355fb9e2020-10-22 20:19:35 -06002365 ret = io_req_task_work_add(req);
Jens Axboec40f6372020-06-25 15:39:59 -06002366 if (unlikely(ret)) {
Pavel Begunkov04fc6c82021-02-12 03:23:54 +00002367 percpu_ref_get(&req->ctx->refs);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002368 io_req_task_work_add_fallback(req, io_req_task_cancel);
Jens Axboec40f6372020-06-25 15:39:59 -06002369 }
Jens Axboec40f6372020-06-25 15:39:59 -06002370}
2371
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002372static inline void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08002373{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002374 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03002375
Pavel Begunkov906a8c32020-06-27 14:04:55 +03002376 if (nxt)
2377 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08002378}
2379
Jens Axboe9e645e112019-05-10 16:07:28 -06002380static void io_free_req(struct io_kiocb *req)
2381{
Pavel Begunkovc3524382020-06-28 12:52:32 +03002382 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002383 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002384}
2385
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002386struct req_batch {
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002387 struct task_struct *task;
2388 int task_refs;
Jens Axboe1b4c3512021-02-10 00:03:19 +00002389 int ctx_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002390};
2391
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002392static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002393{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002394 rb->task_refs = 0;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002395 rb->ctx_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002396 rb->task = NULL;
2397}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03002398
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002399static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
2400 struct req_batch *rb)
2401{
Pavel Begunkov6e833d52021-02-11 18:28:20 +00002402 if (rb->task)
Pavel Begunkov7c660732021-01-25 11:42:21 +00002403 io_put_task(rb->task, rb->task_refs);
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002404 if (rb->ctx_refs)
2405 percpu_ref_put_many(&ctx->refs, rb->ctx_refs);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002406}
2407
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002408static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req,
2409 struct io_submit_state *state)
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002410{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002411 io_queue_next(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002412
Jens Axboee3bc8e92020-09-24 08:45:57 -06002413 if (req->task != rb->task) {
Pavel Begunkov7c660732021-01-25 11:42:21 +00002414 if (rb->task)
2415 io_put_task(rb->task, rb->task_refs);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002416 rb->task = req->task;
2417 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002418 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002419 rb->task_refs++;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002420 rb->ctx_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002421
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002422 io_dismantle_req(req);
Pavel Begunkovbd759042021-02-12 03:23:50 +00002423 if (state->free_reqs != ARRAY_SIZE(state->reqs))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002424 state->reqs[state->free_reqs++] = req;
Pavel Begunkovbd759042021-02-12 03:23:50 +00002425 else
2426 list_add(&req->compl.list, &state->comp.free_list);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002427}
2428
Pavel Begunkov905c1722021-02-10 00:03:14 +00002429static void io_submit_flush_completions(struct io_comp_state *cs,
2430 struct io_ring_ctx *ctx)
2431{
2432 int i, nr = cs->nr;
2433 struct io_kiocb *req;
2434 struct req_batch rb;
2435
2436 io_init_req_batch(&rb);
2437 spin_lock_irq(&ctx->completion_lock);
2438 for (i = 0; i < nr; i++) {
2439 req = cs->reqs[i];
2440 __io_cqring_fill_event(req, req->result, req->compl.cflags);
2441 }
2442 io_commit_cqring(ctx);
2443 spin_unlock_irq(&ctx->completion_lock);
2444
2445 io_cqring_ev_posted(ctx);
2446 for (i = 0; i < nr; i++) {
2447 req = cs->reqs[i];
2448
2449 /* submission and completion refs */
2450 if (refcount_sub_and_test(2, &req->refs))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002451 io_req_free_batch(&rb, req, &ctx->submit_state);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002452 }
2453
2454 io_req_free_batch_finish(ctx, &rb);
2455 cs->nr = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06002456}
2457
Jens Axboeba816ad2019-09-28 11:36:45 -06002458/*
2459 * Drop reference to request, return next in chain (if there is one) if this
2460 * was the last reference to this request.
2461 */
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002462static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002463{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002464 struct io_kiocb *nxt = NULL;
2465
Jens Axboe2a44f462020-02-25 13:25:41 -07002466 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002467 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002468 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002469 }
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002470 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002471}
2472
Jens Axboe2b188cc2019-01-07 10:46:33 -07002473static void io_put_req(struct io_kiocb *req)
2474{
Jens Axboedef596e2019-01-09 08:59:42 -07002475 if (refcount_dec_and_test(&req->refs))
2476 io_free_req(req);
2477}
2478
Pavel Begunkov216578e2020-10-13 09:44:00 +01002479static void io_put_req_deferred_cb(struct callback_head *cb)
2480{
2481 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2482
2483 io_free_req(req);
2484}
2485
2486static void io_free_req_deferred(struct io_kiocb *req)
2487{
2488 int ret;
2489
Jens Axboe7cbf1722021-02-10 00:03:20 +00002490 req->task_work.func = io_put_req_deferred_cb;
Jens Axboe355fb9e2020-10-22 20:19:35 -06002491 ret = io_req_task_work_add(req);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002492 if (unlikely(ret))
2493 io_req_task_work_add_fallback(req, io_put_req_deferred_cb);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002494}
2495
2496static inline void io_put_req_deferred(struct io_kiocb *req, int refs)
2497{
2498 if (refcount_sub_and_test(refs, &req->refs))
2499 io_free_req_deferred(req);
2500}
2501
Jens Axboe978db572019-11-14 22:39:04 -07002502static void io_double_put_req(struct io_kiocb *req)
2503{
2504 /* drop both submit and complete references */
2505 if (refcount_sub_and_test(2, &req->refs))
2506 io_free_req(req);
2507}
2508
Pavel Begunkov6c503152021-01-04 20:36:36 +00002509static unsigned io_cqring_events(struct io_ring_ctx *ctx)
Jens Axboea3a0e432019-08-20 11:03:11 -06002510{
2511 /* See comment at the top of this file */
2512 smp_rmb();
Pavel Begunkove23de152020-12-17 00:24:37 +00002513 return __io_cqring_events(ctx);
Jens Axboea3a0e432019-08-20 11:03:11 -06002514}
2515
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002516static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2517{
2518 struct io_rings *rings = ctx->rings;
2519
2520 /* make sure SQ entry isn't read before tail */
2521 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2522}
2523
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002524static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002525{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002526 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002527
Jens Axboebcda7ba2020-02-23 16:42:51 -07002528 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2529 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002530 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002531 kfree(kbuf);
2532 return cflags;
2533}
2534
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002535static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2536{
2537 struct io_buffer *kbuf;
2538
2539 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2540 return io_put_kbuf(req, kbuf);
2541}
2542
Jens Axboe4c6e2772020-07-01 11:29:10 -06002543static inline bool io_run_task_work(void)
2544{
Jens Axboe6200b0a2020-09-13 14:38:30 -06002545 /*
2546 * Not safe to run on exiting task, and the task_work handling will
2547 * not add work to such a task.
2548 */
2549 if (unlikely(current->flags & PF_EXITING))
2550 return false;
Jens Axboe4c6e2772020-07-01 11:29:10 -06002551 if (current->task_works) {
2552 __set_current_state(TASK_RUNNING);
2553 task_work_run();
2554 return true;
2555 }
2556
2557 return false;
2558}
2559
Jens Axboedef596e2019-01-09 08:59:42 -07002560/*
2561 * Find and free completed poll iocbs
2562 */
2563static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
2564 struct list_head *done)
2565{
Jens Axboe8237e042019-12-28 10:48:22 -07002566 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002567 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002568
2569 /* order with ->result store in io_complete_rw_iopoll() */
2570 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002571
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002572 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002573 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002574 int cflags = 0;
2575
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002576 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002577 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002578
Pavel Begunkovf1613402021-02-11 18:28:21 +00002579 if (READ_ONCE(req->result) == -EAGAIN) {
2580 req->iopoll_completed = 0;
Pavel Begunkov23faba32021-02-11 18:28:22 +00002581 if (io_rw_reissue(req))
Pavel Begunkovf1613402021-02-11 18:28:21 +00002582 continue;
2583 }
2584
Jens Axboebcda7ba2020-02-23 16:42:51 -07002585 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002586 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002587
2588 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002589 (*nr_events)++;
2590
Pavel Begunkovc3524382020-06-28 12:52:32 +03002591 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002592 io_req_free_batch(&rb, req, &ctx->submit_state);
Jens Axboedef596e2019-01-09 08:59:42 -07002593 }
Jens Axboedef596e2019-01-09 08:59:42 -07002594
Jens Axboe09bb8392019-03-13 12:39:28 -06002595 io_commit_cqring(ctx);
Pavel Begunkov80c18e42021-01-07 03:15:41 +00002596 io_cqring_ev_posted_iopoll(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002597 io_req_free_batch_finish(ctx, &rb);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002598}
2599
Jens Axboedef596e2019-01-09 08:59:42 -07002600static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2601 long min)
2602{
2603 struct io_kiocb *req, *tmp;
2604 LIST_HEAD(done);
2605 bool spin;
2606 int ret;
2607
2608 /*
2609 * Only spin for completions if we don't have multiple devices hanging
2610 * off our complete list, and we're under the requested amount.
2611 */
2612 spin = !ctx->poll_multi_file && *nr_events < min;
2613
2614 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002615 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002616 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002617
2618 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002619 * Move completed and retryable entries to our local lists.
2620 * If we find a request that requires polling, break out
2621 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002622 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002623 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002624 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002625 continue;
2626 }
2627 if (!list_empty(&done))
2628 break;
2629
2630 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2631 if (ret < 0)
2632 break;
2633
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002634 /* iopoll may have completed current req */
2635 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002636 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002637
Jens Axboedef596e2019-01-09 08:59:42 -07002638 if (ret && spin)
2639 spin = false;
2640 ret = 0;
2641 }
2642
2643 if (!list_empty(&done))
2644 io_iopoll_complete(ctx, nr_events, &done);
2645
2646 return ret;
2647}
2648
2649/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002650 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002651 * non-spinning poll check - we'll still enter the driver poll loop, but only
2652 * as a non-spinning completion check.
2653 */
2654static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2655 long min)
2656{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002657 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002658 int ret;
2659
2660 ret = io_do_iopoll(ctx, nr_events, min);
2661 if (ret < 0)
2662 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002663 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002664 return 0;
2665 }
2666
2667 return 1;
2668}
2669
2670/*
2671 * We can't just wait for polled events to come to us, we have to actively
2672 * find and complete them.
2673 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002674static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002675{
2676 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2677 return;
2678
2679 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002680 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002681 unsigned int nr_events = 0;
2682
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002683 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002684
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002685 /* let it sleep and repeat later if can't complete a request */
2686 if (nr_events == 0)
2687 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002688 /*
2689 * Ensure we allow local-to-the-cpu processing to take place,
2690 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002691 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002692 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002693 if (need_resched()) {
2694 mutex_unlock(&ctx->uring_lock);
2695 cond_resched();
2696 mutex_lock(&ctx->uring_lock);
2697 }
Jens Axboedef596e2019-01-09 08:59:42 -07002698 }
2699 mutex_unlock(&ctx->uring_lock);
2700}
2701
Pavel Begunkov7668b922020-07-07 16:36:21 +03002702static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002703{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002704 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002705 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002706
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002707 /*
2708 * We disallow the app entering submit/complete with polling, but we
2709 * still need to lock the ring to prevent racing with polled issue
2710 * that got punted to a workqueue.
2711 */
2712 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002713 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002714 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002715 * Don't enter poll loop if we already have events pending.
2716 * If we do, we can potentially be spinning for commands that
2717 * already triggered a CQE (eg in error).
2718 */
Pavel Begunkov6c503152021-01-04 20:36:36 +00002719 if (test_bit(0, &ctx->cq_check_overflow))
2720 __io_cqring_overflow_flush(ctx, false, NULL, NULL);
2721 if (io_cqring_events(ctx))
Jens Axboea3a0e432019-08-20 11:03:11 -06002722 break;
2723
2724 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002725 * If a submit got punted to a workqueue, we can have the
2726 * application entering polling for a command before it gets
2727 * issued. That app will hold the uring_lock for the duration
2728 * of the poll right here, so we need to take a breather every
2729 * now and then to ensure that the issue has a chance to add
2730 * the poll to the issued list. Otherwise we can spin here
2731 * forever, while the workqueue is stuck trying to acquire the
2732 * very same mutex.
2733 */
2734 if (!(++iters & 7)) {
2735 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002736 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002737 mutex_lock(&ctx->uring_lock);
2738 }
2739
Pavel Begunkov7668b922020-07-07 16:36:21 +03002740 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002741 if (ret <= 0)
2742 break;
2743 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002744 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002745
Jens Axboe500f9fb2019-08-19 12:15:59 -06002746 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002747 return ret;
2748}
2749
Jens Axboe491381ce2019-10-17 09:20:46 -06002750static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002751{
Jens Axboe491381ce2019-10-17 09:20:46 -06002752 /*
2753 * Tell lockdep we inherited freeze protection from submission
2754 * thread.
2755 */
2756 if (req->flags & REQ_F_ISREG) {
2757 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002758
Jens Axboe491381ce2019-10-17 09:20:46 -06002759 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002760 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002761 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002762}
2763
Jens Axboeb63534c2020-06-04 11:28:00 -06002764#ifdef CONFIG_BLOCK
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002765static bool io_resubmit_prep(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002766{
2767 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Colin Ian King4a245472021-02-10 20:00:07 +00002768 int rw, ret;
Jens Axboeb63534c2020-06-04 11:28:00 -06002769 struct iov_iter iter;
Jens Axboeb63534c2020-06-04 11:28:00 -06002770
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002771 /* already prepared */
2772 if (req->async_data)
2773 return true;
Jens Axboeb63534c2020-06-04 11:28:00 -06002774
2775 switch (req->opcode) {
2776 case IORING_OP_READV:
2777 case IORING_OP_READ_FIXED:
2778 case IORING_OP_READ:
2779 rw = READ;
2780 break;
2781 case IORING_OP_WRITEV:
2782 case IORING_OP_WRITE_FIXED:
2783 case IORING_OP_WRITE:
2784 rw = WRITE;
2785 break;
2786 default:
2787 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2788 req->opcode);
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002789 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002790 }
2791
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002792 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2793 if (ret < 0)
2794 return false;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00002795 return !io_setup_async_rw(req, iovec, inline_vecs, &iter, false);
Jens Axboeb63534c2020-06-04 11:28:00 -06002796}
Jens Axboeb63534c2020-06-04 11:28:00 -06002797#endif
2798
Pavel Begunkov23faba32021-02-11 18:28:22 +00002799static bool io_rw_reissue(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002800{
2801#ifdef CONFIG_BLOCK
Jens Axboe355afae2020-09-02 09:30:31 -06002802 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboeb63534c2020-06-04 11:28:00 -06002803 int ret;
2804
Jens Axboe355afae2020-09-02 09:30:31 -06002805 if (!S_ISBLK(mode) && !S_ISREG(mode))
2806 return false;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002807 if ((req->flags & REQ_F_NOWAIT) || io_wq_current_is_worker())
Jens Axboeb63534c2020-06-04 11:28:00 -06002808 return false;
2809
Pavel Begunkov55e6ac12021-01-08 20:57:22 +00002810 lockdep_assert_held(&req->ctx->uring_lock);
2811
Jens Axboe28cea78a2020-09-14 10:51:17 -06002812 ret = io_sq_thread_acquire_mm_files(req->ctx, req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002813
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002814 if (!ret && io_resubmit_prep(req)) {
Jens Axboefdee9462020-08-27 16:46:24 -06002815 refcount_inc(&req->refs);
2816 io_queue_async_work(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002817 return true;
Jens Axboefdee9462020-08-27 16:46:24 -06002818 }
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002819 req_set_fail_links(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002820#endif
2821 return false;
2822}
2823
Jens Axboea1d7c392020-06-22 11:09:46 -06002824static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002825 unsigned int issue_flags)
Jens Axboea1d7c392020-06-22 11:09:46 -06002826{
Pavel Begunkov2f8e45f2021-02-11 18:28:23 +00002827 int cflags = 0;
2828
Pavel Begunkov23faba32021-02-11 18:28:22 +00002829 if ((res == -EAGAIN || res == -EOPNOTSUPP) && io_rw_reissue(req))
2830 return;
Pavel Begunkov2f8e45f2021-02-11 18:28:23 +00002831 if (res != req->result)
2832 req_set_fail_links(req);
Pavel Begunkov23faba32021-02-11 18:28:22 +00002833
Pavel Begunkov2f8e45f2021-02-11 18:28:23 +00002834 if (req->rw.kiocb.ki_flags & IOCB_WRITE)
2835 kiocb_end_write(req);
2836 if (req->flags & REQ_F_BUFFER_SELECTED)
2837 cflags = io_put_rw_kbuf(req);
2838 __io_req_complete(req, issue_flags, res, cflags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002839}
2840
2841static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2842{
Jens Axboe9adbd452019-12-20 08:45:55 -07002843 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002844
Pavel Begunkov889fca72021-02-10 00:03:09 +00002845 __io_complete_rw(req, res, res2, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002846}
2847
Jens Axboedef596e2019-01-09 08:59:42 -07002848static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2849{
Jens Axboe9adbd452019-12-20 08:45:55 -07002850 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002851
Jens Axboe491381ce2019-10-17 09:20:46 -06002852 if (kiocb->ki_flags & IOCB_WRITE)
2853 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002854
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002855 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002856 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002857
2858 WRITE_ONCE(req->result, res);
2859 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002860 smp_wmb();
2861 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002862}
2863
2864/*
2865 * After the iocb has been issued, it's safe to be found on the poll list.
2866 * Adding the kiocb to the list AFTER submission ensures that we don't
2867 * find it from a io_iopoll_getevents() thread before the issuer is done
2868 * accessing the kiocb cookie.
2869 */
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08002870static void io_iopoll_req_issued(struct io_kiocb *req, bool in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002871{
2872 struct io_ring_ctx *ctx = req->ctx;
2873
2874 /*
2875 * Track whether we have multiple files in our lists. This will impact
2876 * how we do polling eventually, not spinning if we're on potentially
2877 * different devices.
2878 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002879 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002880 ctx->poll_multi_file = false;
2881 } else if (!ctx->poll_multi_file) {
2882 struct io_kiocb *list_req;
2883
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002884 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002885 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002886 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002887 ctx->poll_multi_file = true;
2888 }
2889
2890 /*
2891 * For fast devices, IO may have already completed. If it has, add
2892 * it to the front so we find it first.
2893 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002894 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002895 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002896 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002897 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002898
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08002899 /*
2900 * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread
2901 * task context or in io worker task context. If current task context is
2902 * sq thread, we don't need to check whether should wake up sq thread.
2903 */
2904 if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) &&
Jens Axboe534ca6d2020-09-02 13:52:19 -06002905 wq_has_sleeper(&ctx->sq_data->wait))
2906 wake_up(&ctx->sq_data->wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002907}
2908
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002909static inline void io_state_file_put(struct io_submit_state *state)
2910{
Pavel Begunkov02b23a92021-01-19 13:32:41 +00002911 if (state->file_refs) {
2912 fput_many(state->file, state->file_refs);
2913 state->file_refs = 0;
2914 }
Jens Axboe9a56a232019-01-09 09:06:50 -07002915}
2916
2917/*
2918 * Get as many references to a file as we have IOs left in this submission,
2919 * assuming most submissions are for one file, or at least that each file
2920 * has more than one submission.
2921 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002922static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002923{
2924 if (!state)
2925 return fget(fd);
2926
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002927 if (state->file_refs) {
Jens Axboe9a56a232019-01-09 09:06:50 -07002928 if (state->fd == fd) {
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002929 state->file_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002930 return state->file;
2931 }
Pavel Begunkov02b23a92021-01-19 13:32:41 +00002932 io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002933 }
2934 state->file = fget_many(fd, state->ios_left);
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002935 if (unlikely(!state->file))
Jens Axboe9a56a232019-01-09 09:06:50 -07002936 return NULL;
2937
2938 state->fd = fd;
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002939 state->file_refs = state->ios_left - 1;
Jens Axboe9a56a232019-01-09 09:06:50 -07002940 return state->file;
2941}
2942
Jens Axboe4503b762020-06-01 10:00:27 -06002943static bool io_bdev_nowait(struct block_device *bdev)
2944{
Jeffle Xu9ba0d0c2020-10-19 16:59:42 +08002945 return !bdev || blk_queue_nowait(bdev_get_queue(bdev));
Jens Axboe4503b762020-06-01 10:00:27 -06002946}
2947
Jens Axboe2b188cc2019-01-07 10:46:33 -07002948/*
2949 * If we tracked the file through the SCM inflight mechanism, we could support
2950 * any file. For now, just ensure that anything potentially problematic is done
2951 * inline.
2952 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002953static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002954{
2955 umode_t mode = file_inode(file)->i_mode;
2956
Jens Axboe4503b762020-06-01 10:00:27 -06002957 if (S_ISBLK(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002958 if (IS_ENABLED(CONFIG_BLOCK) &&
2959 io_bdev_nowait(I_BDEV(file->f_mapping->host)))
Jens Axboe4503b762020-06-01 10:00:27 -06002960 return true;
2961 return false;
2962 }
2963 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002964 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002965 if (S_ISREG(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002966 if (IS_ENABLED(CONFIG_BLOCK) &&
2967 io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
Jens Axboe4503b762020-06-01 10:00:27 -06002968 file->f_op != &io_uring_fops)
2969 return true;
2970 return false;
2971 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002972
Jens Axboec5b85622020-06-09 19:23:05 -06002973 /* any ->read/write should understand O_NONBLOCK */
2974 if (file->f_flags & O_NONBLOCK)
2975 return true;
2976
Jens Axboeaf197f52020-04-28 13:15:06 -06002977 if (!(file->f_mode & FMODE_NOWAIT))
2978 return false;
2979
2980 if (rw == READ)
2981 return file->f_op->read_iter != NULL;
2982
2983 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002984}
2985
Pavel Begunkova88fc402020-09-30 22:57:53 +03002986static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002987{
Jens Axboedef596e2019-01-09 08:59:42 -07002988 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002989 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002990 struct file *file = req->file;
Jens Axboe09bb8392019-03-13 12:39:28 -06002991 unsigned ioprio;
2992 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002993
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002994 if (S_ISREG(file_inode(file)->i_mode))
Jens Axboe491381ce2019-10-17 09:20:46 -06002995 req->flags |= REQ_F_ISREG;
2996
Jens Axboe2b188cc2019-01-07 10:46:33 -07002997 kiocb->ki_pos = READ_ONCE(sqe->off);
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002998 if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) {
Jens Axboeba042912019-12-25 16:33:42 -07002999 req->flags |= REQ_F_CUR_POS;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003000 kiocb->ki_pos = file->f_pos;
Jens Axboeba042912019-12-25 16:33:42 -07003001 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003002 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03003003 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
3004 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
3005 if (unlikely(ret))
3006 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003007
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003008 /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */
3009 if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK))
3010 req->flags |= REQ_F_NOWAIT;
3011
Jens Axboe2b188cc2019-01-07 10:46:33 -07003012 ioprio = READ_ONCE(sqe->ioprio);
3013 if (ioprio) {
3014 ret = ioprio_check_cap(ioprio);
3015 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06003016 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003017
3018 kiocb->ki_ioprio = ioprio;
3019 } else
3020 kiocb->ki_ioprio = get_current_ioprio();
3021
Jens Axboedef596e2019-01-09 08:59:42 -07003022 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07003023 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
3024 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06003025 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003026
Jens Axboedef596e2019-01-09 08:59:42 -07003027 kiocb->ki_flags |= IOCB_HIPRI;
3028 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08003029 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07003030 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06003031 if (kiocb->ki_flags & IOCB_HIPRI)
3032 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07003033 kiocb->ki_complete = io_complete_rw;
3034 }
Jens Axboe9adbd452019-12-20 08:45:55 -07003035
Jens Axboe3529d8c2019-12-19 18:24:38 -07003036 req->rw.addr = READ_ONCE(sqe->addr);
3037 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003038 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003039 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003040}
3041
3042static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
3043{
3044 switch (ret) {
3045 case -EIOCBQUEUED:
3046 break;
3047 case -ERESTARTSYS:
3048 case -ERESTARTNOINTR:
3049 case -ERESTARTNOHAND:
3050 case -ERESTART_RESTARTBLOCK:
3051 /*
3052 * We can't just restart the syscall, since previously
3053 * submitted sqes may already be in progress. Just fail this
3054 * IO with EINTR.
3055 */
3056 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003057 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003058 default:
3059 kiocb->ki_complete(kiocb, ret, 0);
3060 }
3061}
3062
Jens Axboea1d7c392020-06-22 11:09:46 -06003063static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
Pavel Begunkov889fca72021-02-10 00:03:09 +00003064 unsigned int issue_flags)
Jens Axboeba816ad2019-09-28 11:36:45 -06003065{
Jens Axboeba042912019-12-25 16:33:42 -07003066 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboee8c2bc12020-08-15 18:44:09 -07003067 struct io_async_rw *io = req->async_data;
Jens Axboeba042912019-12-25 16:33:42 -07003068
Jens Axboe227c0c92020-08-13 11:51:40 -06003069 /* add previously done IO, if any */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003070 if (io && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06003071 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07003072 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003073 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07003074 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003075 }
3076
Jens Axboeba042912019-12-25 16:33:42 -07003077 if (req->flags & REQ_F_CUR_POS)
3078 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03003079 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Pavel Begunkov889fca72021-02-10 00:03:09 +00003080 __io_complete_rw(req, ret, 0, issue_flags);
Jens Axboeba816ad2019-09-28 11:36:45 -06003081 else
3082 io_rw_done(kiocb, ret);
3083}
3084
Pavel Begunkov847595d2021-02-04 13:52:06 +00003085static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07003086{
Jens Axboe9adbd452019-12-20 08:45:55 -07003087 struct io_ring_ctx *ctx = req->ctx;
3088 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07003089 struct io_mapped_ubuf *imu;
Pavel Begunkov4be1c612020-09-06 00:45:48 +03003090 u16 index, buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07003091 size_t offset;
3092 u64 buf_addr;
3093
Jens Axboeedafcce2019-01-09 09:16:05 -07003094 if (unlikely(buf_index >= ctx->nr_user_bufs))
3095 return -EFAULT;
Jens Axboeedafcce2019-01-09 09:16:05 -07003096 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
3097 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07003098 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07003099
3100 /* overflow */
3101 if (buf_addr + len < buf_addr)
3102 return -EFAULT;
3103 /* not inside the mapped region */
3104 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
3105 return -EFAULT;
3106
3107 /*
3108 * May not be a start of buffer, set size appropriately
3109 * and advance us to the beginning.
3110 */
3111 offset = buf_addr - imu->ubuf;
3112 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06003113
3114 if (offset) {
3115 /*
3116 * Don't use iov_iter_advance() here, as it's really slow for
3117 * using the latter parts of a big fixed buffer - it iterates
3118 * over each segment manually. We can cheat a bit here, because
3119 * we know that:
3120 *
3121 * 1) it's a BVEC iter, we set it up
3122 * 2) all bvecs are PAGE_SIZE in size, except potentially the
3123 * first and last bvec
3124 *
3125 * So just find our index, and adjust the iterator afterwards.
3126 * If the offset is within the first bvec (or the whole first
3127 * bvec, just use iov_iter_advance(). This makes it easier
3128 * since we can just skip the first segment, which may not
3129 * be PAGE_SIZE aligned.
3130 */
3131 const struct bio_vec *bvec = imu->bvec;
3132
3133 if (offset <= bvec->bv_len) {
3134 iov_iter_advance(iter, offset);
3135 } else {
3136 unsigned long seg_skip;
3137
3138 /* skip first vec */
3139 offset -= bvec->bv_len;
3140 seg_skip = 1 + (offset >> PAGE_SHIFT);
3141
3142 iter->bvec = bvec + seg_skip;
3143 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02003144 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003145 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003146 }
3147 }
3148
Pavel Begunkov847595d2021-02-04 13:52:06 +00003149 return 0;
Jens Axboeedafcce2019-01-09 09:16:05 -07003150}
3151
Jens Axboebcda7ba2020-02-23 16:42:51 -07003152static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
3153{
3154 if (needs_lock)
3155 mutex_unlock(&ctx->uring_lock);
3156}
3157
3158static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
3159{
3160 /*
3161 * "Normal" inline submissions always hold the uring_lock, since we
3162 * grab it from the system call. Same is true for the SQPOLL offload.
3163 * The only exception is when we've detached the request and issue it
3164 * from an async worker thread, grab the lock for that case.
3165 */
3166 if (needs_lock)
3167 mutex_lock(&ctx->uring_lock);
3168}
3169
3170static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
3171 int bgid, struct io_buffer *kbuf,
3172 bool needs_lock)
3173{
3174 struct io_buffer *head;
3175
3176 if (req->flags & REQ_F_BUFFER_SELECTED)
3177 return kbuf;
3178
3179 io_ring_submit_lock(req->ctx, needs_lock);
3180
3181 lockdep_assert_held(&req->ctx->uring_lock);
3182
3183 head = idr_find(&req->ctx->io_buffer_idr, bgid);
3184 if (head) {
3185 if (!list_empty(&head->list)) {
3186 kbuf = list_last_entry(&head->list, struct io_buffer,
3187 list);
3188 list_del(&kbuf->list);
3189 } else {
3190 kbuf = head;
3191 idr_remove(&req->ctx->io_buffer_idr, bgid);
3192 }
3193 if (*len > kbuf->len)
3194 *len = kbuf->len;
3195 } else {
3196 kbuf = ERR_PTR(-ENOBUFS);
3197 }
3198
3199 io_ring_submit_unlock(req->ctx, needs_lock);
3200
3201 return kbuf;
3202}
3203
Jens Axboe4d954c22020-02-27 07:31:19 -07003204static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
3205 bool needs_lock)
3206{
3207 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003208 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07003209
3210 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003211 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07003212 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
3213 if (IS_ERR(kbuf))
3214 return kbuf;
3215 req->rw.addr = (u64) (unsigned long) kbuf;
3216 req->flags |= REQ_F_BUFFER_SELECTED;
3217 return u64_to_user_ptr(kbuf->addr);
3218}
3219
3220#ifdef CONFIG_COMPAT
3221static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
3222 bool needs_lock)
3223{
3224 struct compat_iovec __user *uiov;
3225 compat_ssize_t clen;
3226 void __user *buf;
3227 ssize_t len;
3228
3229 uiov = u64_to_user_ptr(req->rw.addr);
3230 if (!access_ok(uiov, sizeof(*uiov)))
3231 return -EFAULT;
3232 if (__get_user(clen, &uiov->iov_len))
3233 return -EFAULT;
3234 if (clen < 0)
3235 return -EINVAL;
3236
3237 len = clen;
3238 buf = io_rw_buffer_select(req, &len, needs_lock);
3239 if (IS_ERR(buf))
3240 return PTR_ERR(buf);
3241 iov[0].iov_base = buf;
3242 iov[0].iov_len = (compat_size_t) len;
3243 return 0;
3244}
3245#endif
3246
3247static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3248 bool needs_lock)
3249{
3250 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
3251 void __user *buf;
3252 ssize_t len;
3253
3254 if (copy_from_user(iov, uiov, sizeof(*uiov)))
3255 return -EFAULT;
3256
3257 len = iov[0].iov_len;
3258 if (len < 0)
3259 return -EINVAL;
3260 buf = io_rw_buffer_select(req, &len, needs_lock);
3261 if (IS_ERR(buf))
3262 return PTR_ERR(buf);
3263 iov[0].iov_base = buf;
3264 iov[0].iov_len = len;
3265 return 0;
3266}
3267
3268static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3269 bool needs_lock)
3270{
Jens Axboedddb3e22020-06-04 11:27:01 -06003271 if (req->flags & REQ_F_BUFFER_SELECTED) {
3272 struct io_buffer *kbuf;
3273
3274 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
3275 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3276 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003277 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06003278 }
Pavel Begunkovdd201662020-12-19 03:15:43 +00003279 if (req->rw.len != 1)
Jens Axboe4d954c22020-02-27 07:31:19 -07003280 return -EINVAL;
3281
3282#ifdef CONFIG_COMPAT
3283 if (req->ctx->compat)
3284 return io_compat_import(req, iov, needs_lock);
3285#endif
3286
3287 return __io_iov_buffer_select(req, iov, needs_lock);
3288}
3289
Pavel Begunkov847595d2021-02-04 13:52:06 +00003290static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec,
3291 struct iov_iter *iter, bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003292{
Jens Axboe9adbd452019-12-20 08:45:55 -07003293 void __user *buf = u64_to_user_ptr(req->rw.addr);
3294 size_t sqe_len = req->rw.len;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003295 u8 opcode = req->opcode;
Jens Axboe4d954c22020-02-27 07:31:19 -07003296 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07003297
Pavel Begunkov7d009162019-11-25 23:14:40 +03003298 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07003299 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07003300 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07003301 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003302
Jens Axboebcda7ba2020-02-23 16:42:51 -07003303 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003304 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07003305 return -EINVAL;
3306
Jens Axboe3a6820f2019-12-22 15:19:35 -07003307 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07003308 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07003309 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03003310 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07003311 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003312 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003313 }
3314
Jens Axboe3a6820f2019-12-22 15:19:35 -07003315 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
3316 *iovec = NULL;
David Laight10fc72e2020-11-07 13:16:25 +00003317 return ret;
Jens Axboe3a6820f2019-12-22 15:19:35 -07003318 }
3319
Jens Axboe4d954c22020-02-27 07:31:19 -07003320 if (req->flags & REQ_F_BUFFER_SELECT) {
3321 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Pavel Begunkov847595d2021-02-04 13:52:06 +00003322 if (!ret)
3323 iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len);
Jens Axboe4d954c22020-02-27 07:31:19 -07003324 *iovec = NULL;
3325 return ret;
3326 }
3327
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02003328 return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter,
3329 req->ctx->compat);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003330}
3331
Jens Axboe0fef9482020-08-26 10:36:20 -06003332static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
3333{
Pavel Begunkov5b09e372020-09-30 22:57:15 +03003334 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
Jens Axboe0fef9482020-08-26 10:36:20 -06003335}
3336
Jens Axboe32960612019-09-23 11:05:34 -06003337/*
3338 * For files that don't have ->read_iter() and ->write_iter(), handle them
3339 * by looping over ->read() or ->write() manually.
3340 */
Jens Axboe4017eb92020-10-22 14:14:12 -06003341static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter)
Jens Axboe32960612019-09-23 11:05:34 -06003342{
Jens Axboe4017eb92020-10-22 14:14:12 -06003343 struct kiocb *kiocb = &req->rw.kiocb;
3344 struct file *file = req->file;
Jens Axboe32960612019-09-23 11:05:34 -06003345 ssize_t ret = 0;
3346
3347 /*
3348 * Don't support polled IO through this interface, and we can't
3349 * support non-blocking either. For the latter, this just causes
3350 * the kiocb to be handled from an async context.
3351 */
3352 if (kiocb->ki_flags & IOCB_HIPRI)
3353 return -EOPNOTSUPP;
3354 if (kiocb->ki_flags & IOCB_NOWAIT)
3355 return -EAGAIN;
3356
3357 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003358 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06003359 ssize_t nr;
3360
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003361 if (!iov_iter_is_bvec(iter)) {
3362 iovec = iov_iter_iovec(iter);
3363 } else {
Jens Axboe4017eb92020-10-22 14:14:12 -06003364 iovec.iov_base = u64_to_user_ptr(req->rw.addr);
3365 iovec.iov_len = req->rw.len;
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003366 }
3367
Jens Axboe32960612019-09-23 11:05:34 -06003368 if (rw == READ) {
3369 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003370 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003371 } else {
3372 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003373 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003374 }
3375
3376 if (nr < 0) {
3377 if (!ret)
3378 ret = nr;
3379 break;
3380 }
3381 ret += nr;
3382 if (nr != iovec.iov_len)
3383 break;
Jens Axboe4017eb92020-10-22 14:14:12 -06003384 req->rw.len -= nr;
3385 req->rw.addr += nr;
Jens Axboe32960612019-09-23 11:05:34 -06003386 iov_iter_advance(iter, nr);
3387 }
3388
3389 return ret;
3390}
3391
Jens Axboeff6165b2020-08-13 09:47:43 -06003392static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3393 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003394{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003395 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003396
Jens Axboeff6165b2020-08-13 09:47:43 -06003397 memcpy(&rw->iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003398 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003399 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003400 /* can only be fixed buffers, no need to do anything */
Pavel Begunkov9c3a2052020-11-23 23:20:27 +00003401 if (iov_iter_is_bvec(iter))
Jens Axboeff6165b2020-08-13 09:47:43 -06003402 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003403 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003404 unsigned iov_off = 0;
3405
3406 rw->iter.iov = rw->fast_iov;
3407 if (iter->iov != fast_iov) {
3408 iov_off = iter->iov - fast_iov;
3409 rw->iter.iov += iov_off;
3410 }
3411 if (rw->fast_iov != fast_iov)
3412 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003413 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003414 } else {
3415 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003416 }
3417}
3418
Jens Axboee8c2bc12020-08-15 18:44:09 -07003419static inline int __io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003420{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003421 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3422 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
3423 return req->async_data == NULL;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003424}
3425
Jens Axboee8c2bc12020-08-15 18:44:09 -07003426static int io_alloc_async_data(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07003427{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003428 if (!io_op_defs[req->opcode].needs_async_data)
Jens Axboed3656342019-12-18 09:50:26 -07003429 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003430
Jens Axboee8c2bc12020-08-15 18:44:09 -07003431 return __io_alloc_async_data(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003432}
3433
Jens Axboeff6165b2020-08-13 09:47:43 -06003434static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3435 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003436 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003437{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003438 if (!force && !io_op_defs[req->opcode].needs_async_data)
Jens Axboe74566df2020-01-13 19:23:24 -07003439 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003440 if (!req->async_data) {
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003441 if (__io_alloc_async_data(req)) {
3442 kfree(iovec);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003443 return -ENOMEM;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003444 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003445
Jens Axboeff6165b2020-08-13 09:47:43 -06003446 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003447 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003448 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003449}
3450
Pavel Begunkov73debe62020-09-30 22:57:54 +03003451static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003452{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003453 struct io_async_rw *iorw = req->async_data;
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003454 struct iovec *iov = iorw->fast_iov;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003455 int ret;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003456
Pavel Begunkov2846c482020-11-07 13:16:27 +00003457 ret = io_import_iovec(rw, req, &iov, &iorw->iter, false);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003458 if (unlikely(ret < 0))
3459 return ret;
3460
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003461 iorw->bytes_done = 0;
3462 iorw->free_iovec = iov;
3463 if (iov)
3464 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003465 return 0;
3466}
3467
Pavel Begunkov73debe62020-09-30 22:57:54 +03003468static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003469{
3470 ssize_t ret;
3471
Pavel Begunkova88fc402020-09-30 22:57:53 +03003472 ret = io_prep_rw(req, sqe);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003473 if (ret)
3474 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003475
Jens Axboe3529d8c2019-12-19 18:24:38 -07003476 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3477 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003478
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003479 /* either don't need iovec imported or already have it */
Pavel Begunkov2d199892020-09-30 22:57:35 +03003480 if (!req->async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003481 return 0;
Pavel Begunkov73debe62020-09-30 22:57:54 +03003482 return io_rw_prep_async(req, READ);
Jens Axboef67676d2019-12-02 11:03:47 -07003483}
3484
Jens Axboec1dd91d2020-08-03 16:43:59 -06003485/*
3486 * This is our waitqueue callback handler, registered through lock_page_async()
3487 * when we initially tried to do the IO with the iocb armed our waitqueue.
3488 * This gets called when the page is unlocked, and we generally expect that to
3489 * happen when the page IO is completed and the page is now uptodate. This will
3490 * queue a task_work based retry of the operation, attempting to copy the data
3491 * again. If the latter fails because the page was NOT uptodate, then we will
3492 * do a thread based blocking retry of the operation. That's the unexpected
3493 * slow path.
3494 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003495static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3496 int sync, void *arg)
3497{
3498 struct wait_page_queue *wpq;
3499 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003500 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003501
3502 wpq = container_of(wait, struct wait_page_queue, wait);
3503
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003504 if (!wake_page_match(wpq, key))
3505 return 0;
3506
Hao Xuc8d317a2020-09-29 20:00:45 +08003507 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003508 list_del_init(&wait->entry);
3509
Jens Axboebcf5a062020-05-22 09:24:42 -06003510 /* submit ref gets dropped, acquire a new one */
3511 refcount_inc(&req->refs);
Pavel Begunkov921b9052021-02-12 03:23:53 +00003512 io_req_task_queue(req);
Jens Axboebcf5a062020-05-22 09:24:42 -06003513 return 1;
3514}
3515
Jens Axboec1dd91d2020-08-03 16:43:59 -06003516/*
3517 * This controls whether a given IO request should be armed for async page
3518 * based retry. If we return false here, the request is handed to the async
3519 * worker threads for retry. If we're doing buffered reads on a regular file,
3520 * we prepare a private wait_page_queue entry and retry the operation. This
3521 * will either succeed because the page is now uptodate and unlocked, or it
3522 * will register a callback when the page is unlocked at IO completion. Through
3523 * that callback, io_uring uses task_work to setup a retry of the operation.
3524 * That retry will attempt the buffered read again. The retry will generally
3525 * succeed, or in rare cases where it fails, we then fall back to using the
3526 * async worker threads for a blocking retry.
3527 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003528static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003529{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003530 struct io_async_rw *rw = req->async_data;
3531 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003532 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003533
3534 /* never retry for NOWAIT, we just complete with -EAGAIN */
3535 if (req->flags & REQ_F_NOWAIT)
3536 return false;
3537
Jens Axboe227c0c92020-08-13 11:51:40 -06003538 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003539 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003540 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003541
Jens Axboebcf5a062020-05-22 09:24:42 -06003542 /*
3543 * just use poll if we can, and don't attempt if the fs doesn't
3544 * support callback based unlocks
3545 */
3546 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3547 return false;
3548
Jens Axboe3b2a4432020-08-16 10:58:43 -07003549 wait->wait.func = io_async_buf_func;
3550 wait->wait.private = req;
3551 wait->wait.flags = 0;
3552 INIT_LIST_HEAD(&wait->wait.entry);
3553 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003554 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003555 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003556 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003557}
3558
3559static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3560{
3561 if (req->file->f_op->read_iter)
3562 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003563 else if (req->file->f_op->read)
Jens Axboe4017eb92020-10-22 14:14:12 -06003564 return loop_rw_iter(READ, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003565 else
3566 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003567}
3568
Pavel Begunkov889fca72021-02-10 00:03:09 +00003569static int io_read(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003570{
3571 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003572 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003573 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003574 struct io_async_rw *rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003575 ssize_t io_size, ret, ret2;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003576 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003577
Pavel Begunkov2846c482020-11-07 13:16:27 +00003578 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003579 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003580 iovec = NULL;
3581 } else {
3582 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
3583 if (ret < 0)
3584 return ret;
3585 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003586 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003587 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003588
Jens Axboefd6c2e42019-12-18 12:19:41 -07003589 /* Ensure we clear previously set non-block flag */
3590 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003591 kiocb->ki_flags &= ~IOCB_NOWAIT;
Pavel Begunkova88fc402020-09-30 22:57:53 +03003592 else
3593 kiocb->ki_flags |= IOCB_NOWAIT;
3594
Pavel Begunkov24c74672020-06-21 13:09:51 +03003595 /* If the file doesn't support async, just async punt */
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003596 if (force_nonblock && !io_file_supports_async(req->file, READ)) {
3597 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003598 return ret ?: -EAGAIN;
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003599 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003600
Pavel Begunkov632546c2020-11-07 13:16:26 +00003601 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003602 if (unlikely(ret)) {
3603 kfree(iovec);
3604 return ret;
3605 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003606
Jens Axboe227c0c92020-08-13 11:51:40 -06003607 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003608
Pavel Begunkov57cd6572021-02-01 18:59:56 +00003609 if (ret == -EIOCBQUEUED) {
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003610 /* it's faster to check here then delegate to kfree */
3611 if (iovec)
3612 kfree(iovec);
3613 return 0;
Jens Axboe227c0c92020-08-13 11:51:40 -06003614 } else if (ret == -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003615 /* IOPOLL retry should happen for io-wq threads */
3616 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003617 goto done;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003618 /* no retry on NONBLOCK nor RWF_NOWAIT */
3619 if (req->flags & REQ_F_NOWAIT)
Jens Axboe355afae2020-09-02 09:30:31 -06003620 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003621 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003622 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003623 ret = 0;
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003624 } else if (ret <= 0 || ret == io_size || !force_nonblock ||
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003625 (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) {
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003626 /* read all, failed, already did sync or don't want to retry */
Jens Axboe00d23d52020-08-25 12:59:22 -06003627 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003628 }
3629
Jens Axboe227c0c92020-08-13 11:51:40 -06003630 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003631 if (ret2)
3632 return ret2;
3633
Jens Axboee8c2bc12020-08-15 18:44:09 -07003634 rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003635 /* now use our persistent iterator, if we aren't already */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003636 iter = &rw->iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003637
Pavel Begunkovb23df912021-02-04 13:52:04 +00003638 do {
3639 io_size -= ret;
3640 rw->bytes_done += ret;
3641 /* if we can retry, do so with the callbacks armed */
3642 if (!io_rw_should_retry(req)) {
3643 kiocb->ki_flags &= ~IOCB_WAITQ;
3644 return -EAGAIN;
3645 }
3646
3647 /*
3648 * Now retry read with the IOCB_WAITQ parts set in the iocb. If
3649 * we get -EIOCBQUEUED, then we'll get a notification when the
3650 * desired page gets unlocked. We can also get a partial read
3651 * here, and if we do, then just retry at the new offset.
3652 */
3653 ret = io_iter_do_read(req, iter);
3654 if (ret == -EIOCBQUEUED)
3655 return 0;
Jens Axboe227c0c92020-08-13 11:51:40 -06003656 /* we got some bytes, but not all. retry. */
Pavel Begunkovb23df912021-02-04 13:52:04 +00003657 } while (ret > 0 && ret < io_size);
Jens Axboe227c0c92020-08-13 11:51:40 -06003658done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003659 kiocb_done(kiocb, ret, issue_flags);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003660 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003661}
3662
Pavel Begunkov73debe62020-09-30 22:57:54 +03003663static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003664{
3665 ssize_t ret;
3666
Pavel Begunkova88fc402020-09-30 22:57:53 +03003667 ret = io_prep_rw(req, sqe);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003668 if (ret)
3669 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003670
Jens Axboe3529d8c2019-12-19 18:24:38 -07003671 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3672 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003673
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003674 /* either don't need iovec imported or already have it */
Pavel Begunkov2d199892020-09-30 22:57:35 +03003675 if (!req->async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003676 return 0;
Pavel Begunkov73debe62020-09-30 22:57:54 +03003677 return io_rw_prep_async(req, WRITE);
Jens Axboef67676d2019-12-02 11:03:47 -07003678}
3679
Pavel Begunkov889fca72021-02-10 00:03:09 +00003680static int io_write(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003681{
3682 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003683 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003684 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003685 struct io_async_rw *rw = req->async_data;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003686 ssize_t ret, ret2, io_size;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003687 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003688
Pavel Begunkov2846c482020-11-07 13:16:27 +00003689 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003690 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003691 iovec = NULL;
3692 } else {
3693 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
3694 if (ret < 0)
3695 return ret;
3696 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003697 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003698 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003699
Jens Axboefd6c2e42019-12-18 12:19:41 -07003700 /* Ensure we clear previously set non-block flag */
3701 if (!force_nonblock)
Pavel Begunkova88fc402020-09-30 22:57:53 +03003702 kiocb->ki_flags &= ~IOCB_NOWAIT;
3703 else
3704 kiocb->ki_flags |= IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003705
Pavel Begunkov24c74672020-06-21 13:09:51 +03003706 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003707 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003708 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003709
Jens Axboe10d59342019-12-09 20:16:22 -07003710 /* file path doesn't support NOWAIT for non-direct_IO */
3711 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3712 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003713 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003714
Pavel Begunkov632546c2020-11-07 13:16:26 +00003715 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003716 if (unlikely(ret))
3717 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003718
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003719 /*
3720 * Open-code file_start_write here to grab freeze protection,
3721 * which will be released by another thread in
3722 * io_complete_rw(). Fool lockdep by telling it the lock got
3723 * released so that it doesn't complain about the held lock when
3724 * we return to userspace.
3725 */
3726 if (req->flags & REQ_F_ISREG) {
Darrick J. Wong8a3c84b2020-11-10 16:50:21 -08003727 sb_start_write(file_inode(req->file)->i_sb);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003728 __sb_writers_release(file_inode(req->file)->i_sb,
3729 SB_FREEZE_WRITE);
3730 }
3731 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003732
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003733 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003734 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003735 else if (req->file->f_op->write)
Jens Axboe4017eb92020-10-22 14:14:12 -06003736 ret2 = loop_rw_iter(WRITE, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003737 else
3738 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003739
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003740 /*
3741 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3742 * retry them without IOCB_NOWAIT.
3743 */
3744 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3745 ret2 = -EAGAIN;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003746 /* no retry on NONBLOCK nor RWF_NOWAIT */
3747 if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT))
Jens Axboe355afae2020-09-02 09:30:31 -06003748 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003749 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003750 /* IOPOLL retry should happen for io-wq threads */
3751 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3752 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003753done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003754 kiocb_done(kiocb, ret2, issue_flags);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003755 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003756copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003757 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003758 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003759 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003760 return ret ?: -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003761 }
Jens Axboe31b51512019-01-18 22:56:34 -07003762out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003763 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003764 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003765 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003766 return ret;
3767}
3768
Jens Axboe80a261f2020-09-28 14:23:58 -06003769static int io_renameat_prep(struct io_kiocb *req,
3770 const struct io_uring_sqe *sqe)
3771{
3772 struct io_rename *ren = &req->rename;
3773 const char __user *oldf, *newf;
3774
3775 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3776 return -EBADF;
3777
3778 ren->old_dfd = READ_ONCE(sqe->fd);
3779 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3780 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3781 ren->new_dfd = READ_ONCE(sqe->len);
3782 ren->flags = READ_ONCE(sqe->rename_flags);
3783
3784 ren->oldpath = getname(oldf);
3785 if (IS_ERR(ren->oldpath))
3786 return PTR_ERR(ren->oldpath);
3787
3788 ren->newpath = getname(newf);
3789 if (IS_ERR(ren->newpath)) {
3790 putname(ren->oldpath);
3791 return PTR_ERR(ren->newpath);
3792 }
3793
3794 req->flags |= REQ_F_NEED_CLEANUP;
3795 return 0;
3796}
3797
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003798static int io_renameat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe80a261f2020-09-28 14:23:58 -06003799{
3800 struct io_rename *ren = &req->rename;
3801 int ret;
3802
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003803 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe80a261f2020-09-28 14:23:58 -06003804 return -EAGAIN;
3805
3806 ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd,
3807 ren->newpath, ren->flags);
3808
3809 req->flags &= ~REQ_F_NEED_CLEANUP;
3810 if (ret < 0)
3811 req_set_fail_links(req);
3812 io_req_complete(req, ret);
3813 return 0;
3814}
3815
Jens Axboe14a11432020-09-28 14:27:37 -06003816static int io_unlinkat_prep(struct io_kiocb *req,
3817 const struct io_uring_sqe *sqe)
3818{
3819 struct io_unlink *un = &req->unlink;
3820 const char __user *fname;
3821
3822 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3823 return -EBADF;
3824
3825 un->dfd = READ_ONCE(sqe->fd);
3826
3827 un->flags = READ_ONCE(sqe->unlink_flags);
3828 if (un->flags & ~AT_REMOVEDIR)
3829 return -EINVAL;
3830
3831 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3832 un->filename = getname(fname);
3833 if (IS_ERR(un->filename))
3834 return PTR_ERR(un->filename);
3835
3836 req->flags |= REQ_F_NEED_CLEANUP;
3837 return 0;
3838}
3839
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003840static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe14a11432020-09-28 14:27:37 -06003841{
3842 struct io_unlink *un = &req->unlink;
3843 int ret;
3844
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003845 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe14a11432020-09-28 14:27:37 -06003846 return -EAGAIN;
3847
3848 if (un->flags & AT_REMOVEDIR)
3849 ret = do_rmdir(un->dfd, un->filename);
3850 else
3851 ret = do_unlinkat(un->dfd, un->filename);
3852
3853 req->flags &= ~REQ_F_NEED_CLEANUP;
3854 if (ret < 0)
3855 req_set_fail_links(req);
3856 io_req_complete(req, ret);
3857 return 0;
3858}
3859
Jens Axboe36f4fa62020-09-05 11:14:22 -06003860static int io_shutdown_prep(struct io_kiocb *req,
3861 const struct io_uring_sqe *sqe)
3862{
3863#if defined(CONFIG_NET)
3864 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3865 return -EINVAL;
3866 if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags ||
3867 sqe->buf_index)
3868 return -EINVAL;
3869
3870 req->shutdown.how = READ_ONCE(sqe->len);
3871 return 0;
3872#else
3873 return -EOPNOTSUPP;
3874#endif
3875}
3876
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003877static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003878{
3879#if defined(CONFIG_NET)
3880 struct socket *sock;
3881 int ret;
3882
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003883 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003884 return -EAGAIN;
3885
Linus Torvalds48aba792020-12-16 12:44:05 -08003886 sock = sock_from_file(req->file);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003887 if (unlikely(!sock))
Linus Torvalds48aba792020-12-16 12:44:05 -08003888 return -ENOTSOCK;
Jens Axboe36f4fa62020-09-05 11:14:22 -06003889
3890 ret = __sys_shutdown_sock(sock, req->shutdown.how);
Jens Axboea1464682020-12-14 20:57:27 -07003891 if (ret < 0)
3892 req_set_fail_links(req);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003893 io_req_complete(req, ret);
3894 return 0;
3895#else
3896 return -EOPNOTSUPP;
3897#endif
3898}
3899
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003900static int __io_splice_prep(struct io_kiocb *req,
3901 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003902{
3903 struct io_splice* sp = &req->splice;
3904 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003905
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003906 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3907 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003908
3909 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003910 sp->len = READ_ONCE(sqe->len);
3911 sp->flags = READ_ONCE(sqe->splice_flags);
3912
3913 if (unlikely(sp->flags & ~valid_flags))
3914 return -EINVAL;
3915
Pavel Begunkov8371adf2020-10-10 18:34:08 +01003916 sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in),
3917 (sp->flags & SPLICE_F_FD_IN_FIXED));
3918 if (!sp->file_in)
3919 return -EBADF;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003920 req->flags |= REQ_F_NEED_CLEANUP;
3921
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003922 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3923 /*
3924 * Splice operation will be punted aync, and here need to
3925 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3926 */
3927 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003928 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003929 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003930
3931 return 0;
3932}
3933
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003934static int io_tee_prep(struct io_kiocb *req,
3935 const struct io_uring_sqe *sqe)
3936{
3937 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3938 return -EINVAL;
3939 return __io_splice_prep(req, sqe);
3940}
3941
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003942static int io_tee(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003943{
3944 struct io_splice *sp = &req->splice;
3945 struct file *in = sp->file_in;
3946 struct file *out = sp->file_out;
3947 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3948 long ret = 0;
3949
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003950 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003951 return -EAGAIN;
3952 if (sp->len)
3953 ret = do_tee(in, out, sp->len, flags);
3954
3955 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3956 req->flags &= ~REQ_F_NEED_CLEANUP;
3957
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003958 if (ret != sp->len)
3959 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003960 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003961 return 0;
3962}
3963
3964static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3965{
3966 struct io_splice* sp = &req->splice;
3967
3968 sp->off_in = READ_ONCE(sqe->splice_off_in);
3969 sp->off_out = READ_ONCE(sqe->off);
3970 return __io_splice_prep(req, sqe);
3971}
3972
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003973static int io_splice(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003974{
3975 struct io_splice *sp = &req->splice;
3976 struct file *in = sp->file_in;
3977 struct file *out = sp->file_out;
3978 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3979 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003980 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003981
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003982 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003983 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003984
3985 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3986 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003987
Jens Axboe948a7742020-05-17 14:21:38 -06003988 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003989 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003990
3991 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3992 req->flags &= ~REQ_F_NEED_CLEANUP;
3993
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003994 if (ret != sp->len)
3995 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003996 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003997 return 0;
3998}
3999
Jens Axboe2b188cc2019-01-07 10:46:33 -07004000/*
4001 * IORING_OP_NOP just posts a completion event, nothing else.
4002 */
Pavel Begunkov889fca72021-02-10 00:03:09 +00004003static int io_nop(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004004{
4005 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004006
Jens Axboedef596e2019-01-09 08:59:42 -07004007 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4008 return -EINVAL;
4009
Pavel Begunkov889fca72021-02-10 00:03:09 +00004010 __io_req_complete(req, issue_flags, 0, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004011 return 0;
4012}
4013
Jens Axboe3529d8c2019-12-19 18:24:38 -07004014static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004015{
Jens Axboe6b063142019-01-10 22:13:58 -07004016 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004017
Jens Axboe09bb8392019-03-13 12:39:28 -06004018 if (!req->file)
4019 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004020
Jens Axboe6b063142019-01-10 22:13:58 -07004021 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07004022 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07004023 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004024 return -EINVAL;
4025
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004026 req->sync.flags = READ_ONCE(sqe->fsync_flags);
4027 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
4028 return -EINVAL;
4029
4030 req->sync.off = READ_ONCE(sqe->off);
4031 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004032 return 0;
4033}
4034
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004035static int io_fsync(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe78912932020-01-14 22:09:06 -07004036{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004037 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004038 int ret;
4039
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004040 /* fsync always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004041 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004042 return -EAGAIN;
4043
Jens Axboe9adbd452019-12-20 08:45:55 -07004044 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004045 end > 0 ? end : LLONG_MAX,
4046 req->sync.flags & IORING_FSYNC_DATASYNC);
4047 if (ret < 0)
4048 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004049 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004050 return 0;
4051}
4052
Jens Axboed63d1b52019-12-10 10:38:56 -07004053static int io_fallocate_prep(struct io_kiocb *req,
4054 const struct io_uring_sqe *sqe)
4055{
4056 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
4057 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004058 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4059 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07004060
4061 req->sync.off = READ_ONCE(sqe->off);
4062 req->sync.len = READ_ONCE(sqe->addr);
4063 req->sync.mode = READ_ONCE(sqe->len);
4064 return 0;
4065}
4066
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004067static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboed63d1b52019-12-10 10:38:56 -07004068{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004069 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07004070
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004071 /* fallocate always requiring blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004072 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004073 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004074 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
4075 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004076 if (ret < 0)
4077 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004078 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07004079 return 0;
4080}
4081
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004082static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004083{
Jens Axboef8748882020-01-08 17:47:02 -07004084 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004085 int ret;
4086
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004087 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07004088 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004089 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07004090 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004091
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004092 /* open.how should be already initialised */
4093 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06004094 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004095
Pavel Begunkov25e72d12020-06-03 18:03:23 +03004096 req->open.dfd = READ_ONCE(sqe->fd);
4097 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07004098 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004099 if (IS_ERR(req->open.filename)) {
4100 ret = PTR_ERR(req->open.filename);
4101 req->open.filename = NULL;
4102 return ret;
4103 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06004104 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004105 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -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)
4110{
4111 u64 flags, mode;
4112
Jens Axboe14587a462020-09-05 11:36:08 -06004113 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06004114 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004115 mode = READ_ONCE(sqe->len);
4116 flags = READ_ONCE(sqe->open_flags);
4117 req->open.how = build_open_how(flags, mode);
4118 return __io_openat_prep(req, sqe);
4119}
4120
Jens Axboecebdb982020-01-08 17:59:24 -07004121static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4122{
4123 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07004124 size_t len;
4125 int ret;
4126
Jens Axboe14587a462020-09-05 11:36:08 -06004127 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06004128 return -EINVAL;
Jens Axboecebdb982020-01-08 17:59:24 -07004129 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4130 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07004131 if (len < OPEN_HOW_SIZE_VER0)
4132 return -EINVAL;
4133
4134 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
4135 len);
4136 if (ret)
4137 return ret;
4138
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004139 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07004140}
4141
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004142static int io_openat2(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004143{
4144 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004145 struct file *file;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004146 bool nonblock_set;
4147 bool resolve_nonblock;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004148 int ret;
4149
Jens Axboecebdb982020-01-08 17:59:24 -07004150 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004151 if (ret)
4152 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004153 nonblock_set = op.open_flag & O_NONBLOCK;
4154 resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004155 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004156 /*
4157 * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open,
4158 * it'll always -EAGAIN
4159 */
4160 if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE))
4161 return -EAGAIN;
4162 op.lookup_flags |= LOOKUP_CACHED;
4163 op.open_flag |= O_NONBLOCK;
4164 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07004165
Jens Axboe4022e7a2020-03-19 19:23:18 -06004166 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004167 if (ret < 0)
4168 goto err;
4169
4170 file = do_filp_open(req->open.dfd, req->open.filename, &op);
Jens Axboe3a81fd02020-12-10 12:25:36 -07004171 /* only retry if RESOLVE_CACHED wasn't already set by application */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004172 if ((!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)) &&
4173 file == ERR_PTR(-EAGAIN)) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004174 /*
4175 * We could hang on to this 'fd', but seems like marginal
4176 * gain for something that is now known to be a slower path.
4177 * So just put it, and we'll get a new one when we retry.
4178 */
4179 put_unused_fd(ret);
4180 return -EAGAIN;
4181 }
4182
Jens Axboe15b71ab2019-12-11 11:20:36 -07004183 if (IS_ERR(file)) {
4184 put_unused_fd(ret);
4185 ret = PTR_ERR(file);
4186 } else {
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004187 if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set)
Jens Axboe3a81fd02020-12-10 12:25:36 -07004188 file->f_flags &= ~O_NONBLOCK;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004189 fsnotify_open(file);
4190 fd_install(ret, file);
4191 }
4192err:
4193 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004194 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004195 if (ret < 0)
4196 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004197 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004198 return 0;
4199}
4200
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004201static int io_openat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboecebdb982020-01-08 17:59:24 -07004202{
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004203 return io_openat2(req, issue_flags & IO_URING_F_NONBLOCK);
Jens Axboecebdb982020-01-08 17:59:24 -07004204}
4205
Jens Axboe067524e2020-03-02 16:32:28 -07004206static int io_remove_buffers_prep(struct io_kiocb *req,
4207 const struct io_uring_sqe *sqe)
4208{
4209 struct io_provide_buf *p = &req->pbuf;
4210 u64 tmp;
4211
4212 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
4213 return -EINVAL;
4214
4215 tmp = READ_ONCE(sqe->fd);
4216 if (!tmp || tmp > USHRT_MAX)
4217 return -EINVAL;
4218
4219 memset(p, 0, sizeof(*p));
4220 p->nbufs = tmp;
4221 p->bgid = READ_ONCE(sqe->buf_group);
4222 return 0;
4223}
4224
4225static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
4226 int bgid, unsigned nbufs)
4227{
4228 unsigned i = 0;
4229
4230 /* shouldn't happen */
4231 if (!nbufs)
4232 return 0;
4233
4234 /* the head kbuf is the list itself */
4235 while (!list_empty(&buf->list)) {
4236 struct io_buffer *nxt;
4237
4238 nxt = list_first_entry(&buf->list, struct io_buffer, list);
4239 list_del(&nxt->list);
4240 kfree(nxt);
4241 if (++i == nbufs)
4242 return i;
4243 }
4244 i++;
4245 kfree(buf);
4246 idr_remove(&ctx->io_buffer_idr, bgid);
4247
4248 return i;
4249}
4250
Pavel Begunkov889fca72021-02-10 00:03:09 +00004251static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe067524e2020-03-02 16:32:28 -07004252{
4253 struct io_provide_buf *p = &req->pbuf;
4254 struct io_ring_ctx *ctx = req->ctx;
4255 struct io_buffer *head;
4256 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004257 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe067524e2020-03-02 16:32:28 -07004258
4259 io_ring_submit_lock(ctx, !force_nonblock);
4260
4261 lockdep_assert_held(&ctx->uring_lock);
4262
4263 ret = -ENOENT;
4264 head = idr_find(&ctx->io_buffer_idr, p->bgid);
4265 if (head)
4266 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
Jens Axboe067524e2020-03-02 16:32:28 -07004267 if (ret < 0)
4268 req_set_fail_links(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004269
4270 /* need to hold the lock to complete IOPOLL requests */
4271 if (ctx->flags & IORING_SETUP_IOPOLL) {
Pavel Begunkov889fca72021-02-10 00:03:09 +00004272 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004273 io_ring_submit_unlock(ctx, !force_nonblock);
4274 } else {
4275 io_ring_submit_unlock(ctx, !force_nonblock);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004276 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004277 }
Jens Axboe067524e2020-03-02 16:32:28 -07004278 return 0;
4279}
4280
Jens Axboeddf0322d2020-02-23 16:41:33 -07004281static int io_provide_buffers_prep(struct io_kiocb *req,
4282 const struct io_uring_sqe *sqe)
4283{
4284 struct io_provide_buf *p = &req->pbuf;
4285 u64 tmp;
4286
4287 if (sqe->ioprio || sqe->rw_flags)
4288 return -EINVAL;
4289
4290 tmp = READ_ONCE(sqe->fd);
4291 if (!tmp || tmp > USHRT_MAX)
4292 return -E2BIG;
4293 p->nbufs = tmp;
4294 p->addr = READ_ONCE(sqe->addr);
4295 p->len = READ_ONCE(sqe->len);
4296
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07004297 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07004298 return -EFAULT;
4299
4300 p->bgid = READ_ONCE(sqe->buf_group);
4301 tmp = READ_ONCE(sqe->off);
4302 if (tmp > USHRT_MAX)
4303 return -E2BIG;
4304 p->bid = tmp;
4305 return 0;
4306}
4307
4308static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
4309{
4310 struct io_buffer *buf;
4311 u64 addr = pbuf->addr;
4312 int i, bid = pbuf->bid;
4313
4314 for (i = 0; i < pbuf->nbufs; i++) {
4315 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
4316 if (!buf)
4317 break;
4318
4319 buf->addr = addr;
4320 buf->len = pbuf->len;
4321 buf->bid = bid;
4322 addr += pbuf->len;
4323 bid++;
4324 if (!*head) {
4325 INIT_LIST_HEAD(&buf->list);
4326 *head = buf;
4327 } else {
4328 list_add_tail(&buf->list, &(*head)->list);
4329 }
4330 }
4331
4332 return i ? i : -ENOMEM;
4333}
4334
Pavel Begunkov889fca72021-02-10 00:03:09 +00004335static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004336{
4337 struct io_provide_buf *p = &req->pbuf;
4338 struct io_ring_ctx *ctx = req->ctx;
4339 struct io_buffer *head, *list;
4340 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004341 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004342
4343 io_ring_submit_lock(ctx, !force_nonblock);
4344
4345 lockdep_assert_held(&ctx->uring_lock);
4346
4347 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
4348
4349 ret = io_add_buffers(p, &head);
4350 if (ret < 0)
4351 goto out;
4352
4353 if (!list) {
4354 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
4355 GFP_KERNEL);
4356 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07004357 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004358 goto out;
4359 }
4360 }
4361out:
Jens Axboeddf0322d2020-02-23 16:41:33 -07004362 if (ret < 0)
4363 req_set_fail_links(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004364
4365 /* need to hold the lock to complete IOPOLL requests */
4366 if (ctx->flags & IORING_SETUP_IOPOLL) {
Pavel Begunkov889fca72021-02-10 00:03:09 +00004367 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004368 io_ring_submit_unlock(ctx, !force_nonblock);
4369 } else {
4370 io_ring_submit_unlock(ctx, !force_nonblock);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004371 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004372 }
Jens Axboeddf0322d2020-02-23 16:41:33 -07004373 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004374}
4375
Jens Axboe3e4827b2020-01-08 15:18:09 -07004376static int io_epoll_ctl_prep(struct io_kiocb *req,
4377 const struct io_uring_sqe *sqe)
4378{
4379#if defined(CONFIG_EPOLL)
4380 if (sqe->ioprio || sqe->buf_index)
4381 return -EINVAL;
Jens Axboe6ca56f82020-09-18 16:51:19 -06004382 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004383 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004384
4385 req->epoll.epfd = READ_ONCE(sqe->fd);
4386 req->epoll.op = READ_ONCE(sqe->len);
4387 req->epoll.fd = READ_ONCE(sqe->off);
4388
4389 if (ep_op_has_event(req->epoll.op)) {
4390 struct epoll_event __user *ev;
4391
4392 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
4393 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
4394 return -EFAULT;
4395 }
4396
4397 return 0;
4398#else
4399 return -EOPNOTSUPP;
4400#endif
4401}
4402
Pavel Begunkov889fca72021-02-10 00:03:09 +00004403static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004404{
4405#if defined(CONFIG_EPOLL)
4406 struct io_epoll *ie = &req->epoll;
4407 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004408 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004409
4410 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
4411 if (force_nonblock && ret == -EAGAIN)
4412 return -EAGAIN;
4413
4414 if (ret < 0)
4415 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004416 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004417 return 0;
4418#else
4419 return -EOPNOTSUPP;
4420#endif
4421}
4422
Jens Axboec1ca7572019-12-25 22:18:28 -07004423static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4424{
4425#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4426 if (sqe->ioprio || sqe->buf_index || sqe->off)
4427 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004428 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4429 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07004430
4431 req->madvise.addr = READ_ONCE(sqe->addr);
4432 req->madvise.len = READ_ONCE(sqe->len);
4433 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
4434 return 0;
4435#else
4436 return -EOPNOTSUPP;
4437#endif
4438}
4439
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004440static int io_madvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboec1ca7572019-12-25 22:18:28 -07004441{
4442#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4443 struct io_madvise *ma = &req->madvise;
4444 int ret;
4445
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004446 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboec1ca7572019-12-25 22:18:28 -07004447 return -EAGAIN;
4448
Minchan Kim0726b012020-10-17 16:14:50 -07004449 ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
Jens Axboec1ca7572019-12-25 22:18:28 -07004450 if (ret < 0)
4451 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004452 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07004453 return 0;
4454#else
4455 return -EOPNOTSUPP;
4456#endif
4457}
4458
Jens Axboe4840e412019-12-25 22:03:45 -07004459static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4460{
4461 if (sqe->ioprio || sqe->buf_index || sqe->addr)
4462 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004463 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4464 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07004465
4466 req->fadvise.offset = READ_ONCE(sqe->off);
4467 req->fadvise.len = READ_ONCE(sqe->len);
4468 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4469 return 0;
4470}
4471
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004472static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe4840e412019-12-25 22:03:45 -07004473{
4474 struct io_fadvise *fa = &req->fadvise;
4475 int ret;
4476
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004477 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3e694262020-02-01 09:22:49 -07004478 switch (fa->advice) {
4479 case POSIX_FADV_NORMAL:
4480 case POSIX_FADV_RANDOM:
4481 case POSIX_FADV_SEQUENTIAL:
4482 break;
4483 default:
4484 return -EAGAIN;
4485 }
4486 }
Jens Axboe4840e412019-12-25 22:03:45 -07004487
4488 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4489 if (ret < 0)
4490 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004491 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07004492 return 0;
4493}
4494
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004495static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4496{
Jens Axboe6ca56f82020-09-18 16:51:19 -06004497 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004498 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004499 if (sqe->ioprio || sqe->buf_index)
4500 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004501 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004502 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004503
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004504 req->statx.dfd = READ_ONCE(sqe->fd);
4505 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004506 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004507 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4508 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004509
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004510 return 0;
4511}
4512
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004513static int io_statx(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004514{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004515 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004516 int ret;
4517
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004518 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004519 /* only need file table for an actual valid fd */
4520 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
4521 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004522 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004523 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004524
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004525 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4526 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004527
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004528 if (ret < 0)
4529 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004530 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004531 return 0;
4532}
4533
Jens Axboeb5dba592019-12-11 14:02:38 -07004534static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4535{
Jens Axboe14587a462020-09-05 11:36:08 -06004536 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004537 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004538 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
4539 sqe->rw_flags || sqe->buf_index)
4540 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004541 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004542 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004543
4544 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboeb5dba592019-12-11 14:02:38 -07004545 return 0;
4546}
4547
Pavel Begunkov889fca72021-02-10 00:03:09 +00004548static int io_close(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb5dba592019-12-11 14:02:38 -07004549{
Jens Axboe9eac1902021-01-19 15:50:37 -07004550 struct files_struct *files = current->files;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004551 struct io_close *close = &req->close;
Jens Axboe9eac1902021-01-19 15:50:37 -07004552 struct fdtable *fdt;
4553 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -07004554 int ret;
4555
Jens Axboe9eac1902021-01-19 15:50:37 -07004556 file = NULL;
4557 ret = -EBADF;
4558 spin_lock(&files->file_lock);
4559 fdt = files_fdtable(files);
4560 if (close->fd >= fdt->max_fds) {
4561 spin_unlock(&files->file_lock);
4562 goto err;
4563 }
4564 file = fdt->fd[close->fd];
4565 if (!file) {
4566 spin_unlock(&files->file_lock);
4567 goto err;
4568 }
4569
4570 if (file->f_op == &io_uring_fops) {
4571 spin_unlock(&files->file_lock);
4572 file = NULL;
4573 goto err;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004574 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004575
4576 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004577 if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004578 spin_unlock(&files->file_lock);
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004579 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004580 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004581
Jens Axboe9eac1902021-01-19 15:50:37 -07004582 ret = __close_fd_get_file(close->fd, &file);
4583 spin_unlock(&files->file_lock);
4584 if (ret < 0) {
4585 if (ret == -ENOENT)
4586 ret = -EBADF;
4587 goto err;
4588 }
4589
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004590 /* No ->flush() or already async, safely close from here */
Jens Axboe9eac1902021-01-19 15:50:37 -07004591 ret = filp_close(file, current->files);
4592err:
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004593 if (ret < 0)
4594 req_set_fail_links(req);
Jens Axboe9eac1902021-01-19 15:50:37 -07004595 if (file)
4596 fput(file);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004597 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe1a417f42020-01-31 17:16:48 -07004598 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004599}
4600
Jens Axboe3529d8c2019-12-19 18:24:38 -07004601static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004602{
4603 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004604
4605 if (!req->file)
4606 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004607
4608 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4609 return -EINVAL;
4610 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
4611 return -EINVAL;
4612
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004613 req->sync.off = READ_ONCE(sqe->off);
4614 req->sync.len = READ_ONCE(sqe->len);
4615 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004616 return 0;
4617}
4618
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004619static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004620{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004621 int ret;
4622
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004623 /* sync_file_range always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004624 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004625 return -EAGAIN;
4626
Jens Axboe9adbd452019-12-20 08:45:55 -07004627 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004628 req->sync.flags);
4629 if (ret < 0)
4630 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004631 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004632 return 0;
4633}
4634
YueHaibing469956e2020-03-04 15:53:52 +08004635#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004636static int io_setup_async_msg(struct io_kiocb *req,
4637 struct io_async_msghdr *kmsg)
4638{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004639 struct io_async_msghdr *async_msg = req->async_data;
4640
4641 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004642 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004643 if (io_alloc_async_data(req)) {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004644 kfree(kmsg->free_iov);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004645 return -ENOMEM;
4646 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004647 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004648 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004649 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov2a780802021-02-05 00:57:58 +00004650 async_msg->msg.msg_name = &async_msg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004651 /* if were using fast_iov, set it to the new one */
4652 if (!async_msg->free_iov)
4653 async_msg->msg.msg_iter.iov = async_msg->fast_iov;
4654
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004655 return -EAGAIN;
4656}
4657
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004658static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4659 struct io_async_msghdr *iomsg)
4660{
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004661 iomsg->msg.msg_name = &iomsg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004662 iomsg->free_iov = iomsg->fast_iov;
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004663 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004664 req->sr_msg.msg_flags, &iomsg->free_iov);
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004665}
4666
Jens Axboe3529d8c2019-12-19 18:24:38 -07004667static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004668{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004669 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004670 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004671 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004672
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004673 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4674 return -EINVAL;
4675
Jens Axboee47293f2019-12-20 08:58:21 -07004676 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004677 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004678 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004679
Jens Axboed8768362020-02-27 14:17:49 -07004680#ifdef CONFIG_COMPAT
4681 if (req->ctx->compat)
4682 sr->msg_flags |= MSG_CMSG_COMPAT;
4683#endif
4684
Jens Axboee8c2bc12020-08-15 18:44:09 -07004685 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07004686 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004687 ret = io_sendmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004688 if (!ret)
4689 req->flags |= REQ_F_NEED_CLEANUP;
4690 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004691}
4692
Pavel Begunkov889fca72021-02-10 00:03:09 +00004693static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004694{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004695 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004696 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004697 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004698 int ret;
4699
Florent Revestdba4a922020-12-04 12:36:04 +01004700 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004701 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004702 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004703
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004704 kmsg = req->async_data;
4705 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004706 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004707 if (ret)
4708 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004709 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004710 }
4711
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004712 flags = req->sr_msg.msg_flags;
4713 if (flags & MSG_DONTWAIT)
4714 req->flags |= REQ_F_NOWAIT;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004715 else if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004716 flags |= MSG_DONTWAIT;
4717
4718 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004719 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004720 return io_setup_async_msg(req, kmsg);
4721 if (ret == -ERESTARTSYS)
4722 ret = -EINTR;
4723
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004724 /* fast path, check for non-NULL to avoid function call */
4725 if (kmsg->free_iov)
4726 kfree(kmsg->free_iov);
Jens Axboe03b12302019-12-02 18:50:25 -07004727 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004728 if (ret < 0)
4729 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004730 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboefddafac2020-01-04 20:19:44 -07004731 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004732}
4733
Pavel Begunkov889fca72021-02-10 00:03:09 +00004734static int io_send(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004735{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004736 struct io_sr_msg *sr = &req->sr_msg;
4737 struct msghdr msg;
4738 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004739 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004740 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004741 int ret;
4742
Florent Revestdba4a922020-12-04 12:36:04 +01004743 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004744 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004745 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004746
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004747 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4748 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004749 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004750
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004751 msg.msg_name = NULL;
4752 msg.msg_control = NULL;
4753 msg.msg_controllen = 0;
4754 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004755
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004756 flags = req->sr_msg.msg_flags;
4757 if (flags & MSG_DONTWAIT)
4758 req->flags |= REQ_F_NOWAIT;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004759 else if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004760 flags |= MSG_DONTWAIT;
Jens Axboe03b12302019-12-02 18:50:25 -07004761
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004762 msg.msg_flags = flags;
4763 ret = sock_sendmsg(sock, &msg);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004764 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004765 return -EAGAIN;
4766 if (ret == -ERESTARTSYS)
4767 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004768
Jens Axboe03b12302019-12-02 18:50:25 -07004769 if (ret < 0)
4770 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004771 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe03b12302019-12-02 18:50:25 -07004772 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004773}
4774
Pavel Begunkov1400e692020-07-12 20:41:05 +03004775static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4776 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004777{
4778 struct io_sr_msg *sr = &req->sr_msg;
4779 struct iovec __user *uiov;
4780 size_t iov_len;
4781 int ret;
4782
Pavel Begunkov1400e692020-07-12 20:41:05 +03004783 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4784 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004785 if (ret)
4786 return ret;
4787
4788 if (req->flags & REQ_F_BUFFER_SELECT) {
4789 if (iov_len > 1)
4790 return -EINVAL;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004791 if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004792 return -EFAULT;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004793 sr->len = iomsg->fast_iov[0].iov_len;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004794 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004795 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004796 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004797 ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004798 &iomsg->free_iov, &iomsg->msg.msg_iter,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004799 false);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004800 if (ret > 0)
4801 ret = 0;
4802 }
4803
4804 return ret;
4805}
4806
4807#ifdef CONFIG_COMPAT
4808static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004809 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004810{
4811 struct compat_msghdr __user *msg_compat;
4812 struct io_sr_msg *sr = &req->sr_msg;
4813 struct compat_iovec __user *uiov;
4814 compat_uptr_t ptr;
4815 compat_size_t len;
4816 int ret;
4817
Pavel Begunkov270a5942020-07-12 20:41:04 +03004818 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004819 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004820 &ptr, &len);
4821 if (ret)
4822 return ret;
4823
4824 uiov = compat_ptr(ptr);
4825 if (req->flags & REQ_F_BUFFER_SELECT) {
4826 compat_ssize_t clen;
4827
4828 if (len > 1)
4829 return -EINVAL;
4830 if (!access_ok(uiov, sizeof(*uiov)))
4831 return -EFAULT;
4832 if (__get_user(clen, &uiov->iov_len))
4833 return -EFAULT;
4834 if (clen < 0)
4835 return -EINVAL;
Pavel Begunkov2d280bc2020-11-29 18:33:32 +00004836 sr->len = clen;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004837 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004838 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004839 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004840 ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004841 UIO_FASTIOV, &iomsg->free_iov,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004842 &iomsg->msg.msg_iter, true);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004843 if (ret < 0)
4844 return ret;
4845 }
4846
4847 return 0;
4848}
Jens Axboe03b12302019-12-02 18:50:25 -07004849#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004850
Pavel Begunkov1400e692020-07-12 20:41:05 +03004851static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4852 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004853{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004854 iomsg->msg.msg_name = &iomsg->addr;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004855
4856#ifdef CONFIG_COMPAT
4857 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004858 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004859#endif
4860
Pavel Begunkov1400e692020-07-12 20:41:05 +03004861 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004862}
4863
Jens Axboebcda7ba2020-02-23 16:42:51 -07004864static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004865 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004866{
4867 struct io_sr_msg *sr = &req->sr_msg;
4868 struct io_buffer *kbuf;
4869
Jens Axboebcda7ba2020-02-23 16:42:51 -07004870 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4871 if (IS_ERR(kbuf))
4872 return kbuf;
4873
4874 sr->kbuf = kbuf;
4875 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004876 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004877}
4878
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004879static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4880{
4881 return io_put_kbuf(req, req->sr_msg.kbuf);
4882}
4883
Jens Axboe3529d8c2019-12-19 18:24:38 -07004884static int io_recvmsg_prep(struct io_kiocb *req,
4885 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07004886{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004887 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004888 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004889 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004890
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004891 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4892 return -EINVAL;
4893
Jens Axboe3529d8c2019-12-19 18:24:38 -07004894 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004895 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004896 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004897 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004898
Jens Axboed8768362020-02-27 14:17:49 -07004899#ifdef CONFIG_COMPAT
4900 if (req->ctx->compat)
4901 sr->msg_flags |= MSG_CMSG_COMPAT;
4902#endif
4903
Jens Axboee8c2bc12020-08-15 18:44:09 -07004904 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe06b76d42019-12-19 14:44:26 -07004905 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004906 ret = io_recvmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004907 if (!ret)
4908 req->flags |= REQ_F_NEED_CLEANUP;
4909 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004910}
4911
Pavel Begunkov889fca72021-02-10 00:03:09 +00004912static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004913{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004914 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004915 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004916 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004917 unsigned flags;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004918 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004919 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004920
Florent Revestdba4a922020-12-04 12:36:04 +01004921 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004922 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004923 return -ENOTSOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004924
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004925 kmsg = req->async_data;
4926 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004927 ret = io_recvmsg_copy_hdr(req, &iomsg);
4928 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004929 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004930 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004931 }
4932
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004933 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004934 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004935 if (IS_ERR(kbuf))
4936 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004937 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004938 kmsg->fast_iov[0].iov_len = req->sr_msg.len;
4939 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov,
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004940 1, req->sr_msg.len);
4941 }
4942
4943 flags = req->sr_msg.msg_flags;
4944 if (flags & MSG_DONTWAIT)
4945 req->flags |= REQ_F_NOWAIT;
4946 else if (force_nonblock)
4947 flags |= MSG_DONTWAIT;
4948
4949 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4950 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004951 if (force_nonblock && ret == -EAGAIN)
4952 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004953 if (ret == -ERESTARTSYS)
4954 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004955
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004956 if (req->flags & REQ_F_BUFFER_SELECTED)
4957 cflags = io_put_recv_kbuf(req);
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004958 /* fast path, check for non-NULL to avoid function call */
4959 if (kmsg->free_iov)
4960 kfree(kmsg->free_iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004961 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004962 if (ret < 0)
4963 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004964 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004965 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004966}
4967
Pavel Begunkov889fca72021-02-10 00:03:09 +00004968static int io_recv(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefddafac2020-01-04 20:19:44 -07004969{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004970 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004971 struct io_sr_msg *sr = &req->sr_msg;
4972 struct msghdr msg;
4973 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004974 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004975 struct iovec iov;
4976 unsigned flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004977 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004978 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004979
Florent Revestdba4a922020-12-04 12:36:04 +01004980 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004981 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004982 return -ENOTSOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004983
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004984 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004985 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004986 if (IS_ERR(kbuf))
4987 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004988 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004989 }
4990
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004991 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004992 if (unlikely(ret))
4993 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004994
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004995 msg.msg_name = NULL;
4996 msg.msg_control = NULL;
4997 msg.msg_controllen = 0;
4998 msg.msg_namelen = 0;
4999 msg.msg_iocb = NULL;
5000 msg.msg_flags = 0;
5001
5002 flags = req->sr_msg.msg_flags;
5003 if (flags & MSG_DONTWAIT)
5004 req->flags |= REQ_F_NOWAIT;
5005 else if (force_nonblock)
5006 flags |= MSG_DONTWAIT;
5007
5008 ret = sock_recvmsg(sock, &msg, flags);
5009 if (force_nonblock && ret == -EAGAIN)
5010 return -EAGAIN;
5011 if (ret == -ERESTARTSYS)
5012 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03005013out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03005014 if (req->flags & REQ_F_BUFFER_SELECTED)
5015 cflags = io_put_recv_kbuf(req);
Jens Axboefddafac2020-01-04 20:19:44 -07005016 if (ret < 0)
5017 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005018 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07005019 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07005020}
5021
Jens Axboe3529d8c2019-12-19 18:24:38 -07005022static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06005023{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005024 struct io_accept *accept = &req->accept;
5025
Jens Axboe14587a462020-09-05 11:36:08 -06005026 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe17f2fe32019-10-17 14:42:58 -06005027 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05005028 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06005029 return -EINVAL;
5030
Jens Axboed55e5f52019-12-11 16:12:15 -07005031 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
5032 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005033 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06005034 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005035 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005036}
Jens Axboe17f2fe32019-10-17 14:42:58 -06005037
Pavel Begunkov889fca72021-02-10 00:03:09 +00005038static int io_accept(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005039{
5040 struct io_accept *accept = &req->accept;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005041 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005042 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005043 int ret;
5044
Jiufei Xuee697dee2020-06-10 13:41:59 +08005045 if (req->file->f_flags & O_NONBLOCK)
5046 req->flags |= REQ_F_NOWAIT;
5047
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005048 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06005049 accept->addr_len, accept->flags,
5050 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005051 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06005052 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005053 if (ret < 0) {
5054 if (ret == -ERESTARTSYS)
5055 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005056 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005057 }
Pavel Begunkov889fca72021-02-10 00:03:09 +00005058 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005059 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005060}
5061
Jens Axboe3529d8c2019-12-19 18:24:38 -07005062static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07005063{
Jens Axboe3529d8c2019-12-19 18:24:38 -07005064 struct io_connect *conn = &req->connect;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005065 struct io_async_connect *io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07005066
Jens Axboe14587a462020-09-05 11:36:08 -06005067 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005068 return -EINVAL;
5069 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
5070 return -EINVAL;
5071
Jens Axboe3529d8c2019-12-19 18:24:38 -07005072 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
5073 conn->addr_len = READ_ONCE(sqe->addr2);
5074
5075 if (!io)
5076 return 0;
5077
5078 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07005079 &io->address);
Jens Axboef499a022019-12-02 16:28:46 -07005080}
5081
Pavel Begunkov889fca72021-02-10 00:03:09 +00005082static int io_connect(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboef8e85cf2019-11-23 14:24:24 -07005083{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005084 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005085 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005086 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005087 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005088
Jens Axboee8c2bc12020-08-15 18:44:09 -07005089 if (req->async_data) {
5090 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07005091 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005092 ret = move_addr_to_kernel(req->connect.addr,
5093 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07005094 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07005095 if (ret)
5096 goto out;
5097 io = &__io;
5098 }
5099
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005100 file_flags = force_nonblock ? O_NONBLOCK : 0;
5101
Jens Axboee8c2bc12020-08-15 18:44:09 -07005102 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005103 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07005104 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07005105 if (req->async_data)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005106 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005107 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07005108 ret = -ENOMEM;
5109 goto out;
5110 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07005111 io = req->async_data;
5112 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07005113 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07005114 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07005115 if (ret == -ERESTARTSYS)
5116 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07005117out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005118 if (ret < 0)
5119 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005120 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005121 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005122}
YueHaibing469956e2020-03-04 15:53:52 +08005123#else /* !CONFIG_NET */
5124static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5125{
Jens Axboef8e85cf2019-11-23 14:24:24 -07005126 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005127}
5128
Pavel Begunkov889fca72021-02-10 00:03:09 +00005129static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005130{
YueHaibing469956e2020-03-04 15:53:52 +08005131 return -EOPNOTSUPP;
5132}
5133
Pavel Begunkov889fca72021-02-10 00:03:09 +00005134static int io_send(struct io_kiocb *req, unsigned int issue_flags)
YueHaibing469956e2020-03-04 15:53:52 +08005135{
5136 return -EOPNOTSUPP;
5137}
5138
5139static int io_recvmsg_prep(struct io_kiocb *req,
5140 const struct io_uring_sqe *sqe)
5141{
5142 return -EOPNOTSUPP;
5143}
5144
Pavel Begunkov889fca72021-02-10 00:03:09 +00005145static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
YueHaibing469956e2020-03-04 15:53:52 +08005146{
5147 return -EOPNOTSUPP;
5148}
5149
Pavel Begunkov889fca72021-02-10 00:03:09 +00005150static int io_recv(struct io_kiocb *req, unsigned int issue_flags)
YueHaibing469956e2020-03-04 15:53:52 +08005151{
5152 return -EOPNOTSUPP;
5153}
5154
5155static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5156{
5157 return -EOPNOTSUPP;
5158}
5159
Pavel Begunkov889fca72021-02-10 00:03:09 +00005160static int io_accept(struct io_kiocb *req, unsigned int issue_flags)
YueHaibing469956e2020-03-04 15:53:52 +08005161{
5162 return -EOPNOTSUPP;
5163}
5164
5165static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5166{
5167 return -EOPNOTSUPP;
5168}
5169
Pavel Begunkov889fca72021-02-10 00:03:09 +00005170static int io_connect(struct io_kiocb *req, unsigned int issue_flags)
YueHaibing469956e2020-03-04 15:53:52 +08005171{
5172 return -EOPNOTSUPP;
5173}
5174#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07005175
Jens Axboed7718a92020-02-14 22:23:12 -07005176struct io_poll_table {
5177 struct poll_table_struct pt;
5178 struct io_kiocb *req;
5179 int error;
5180};
5181
Jens Axboed7718a92020-02-14 22:23:12 -07005182static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
5183 __poll_t mask, task_work_func_t func)
5184{
Jens Axboeaa96bf82020-04-03 11:26:26 -06005185 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07005186
5187 /* for instances that support it check for an event match first: */
5188 if (mask && !(mask & poll->events))
5189 return 0;
5190
5191 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
5192
5193 list_del_init(&poll->wait.entry);
5194
Jens Axboed7718a92020-02-14 22:23:12 -07005195 req->result = mask;
Jens Axboe7cbf1722021-02-10 00:03:20 +00005196 req->task_work.func = func;
Jens Axboe6d816e02020-08-11 08:04:14 -06005197 percpu_ref_get(&req->ctx->refs);
5198
Jens Axboed7718a92020-02-14 22:23:12 -07005199 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06005200 * If this fails, then the task is exiting. When a task exits, the
5201 * work gets canceled, so just cancel this request as well instead
5202 * of executing it. We can't safely execute it anyway, as we may not
5203 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07005204 */
Jens Axboe355fb9e2020-10-22 20:19:35 -06005205 ret = io_req_task_work_add(req);
Jens Axboeaa96bf82020-04-03 11:26:26 -06005206 if (unlikely(ret)) {
Jens Axboee3aabf92020-05-18 11:04:17 -06005207 WRITE_ONCE(poll->canceled, true);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00005208 io_req_task_work_add_fallback(req, func);
Jens Axboeaa96bf82020-04-03 11:26:26 -06005209 }
Jens Axboed7718a92020-02-14 22:23:12 -07005210 return 1;
5211}
5212
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005213static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
5214 __acquires(&req->ctx->completion_lock)
5215{
5216 struct io_ring_ctx *ctx = req->ctx;
5217
5218 if (!req->result && !READ_ONCE(poll->canceled)) {
5219 struct poll_table_struct pt = { ._key = poll->events };
5220
5221 req->result = vfs_poll(req->file, &pt) & poll->events;
5222 }
5223
5224 spin_lock_irq(&ctx->completion_lock);
5225 if (!req->result && !READ_ONCE(poll->canceled)) {
5226 add_wait_queue(poll->head, &poll->wait);
5227 return true;
5228 }
5229
5230 return false;
5231}
5232
Jens Axboed4e7cd32020-08-15 11:44:50 -07005233static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06005234{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005235 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07005236 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07005237 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005238 return req->apoll->double_poll;
5239}
5240
5241static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
5242{
5243 if (req->opcode == IORING_OP_POLL_ADD)
5244 return &req->poll;
5245 return &req->apoll->poll;
5246}
5247
5248static void io_poll_remove_double(struct io_kiocb *req)
5249{
5250 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005251
5252 lockdep_assert_held(&req->ctx->completion_lock);
5253
5254 if (poll && poll->head) {
5255 struct wait_queue_head *head = poll->head;
5256
5257 spin_lock(&head->lock);
5258 list_del_init(&poll->wait.entry);
5259 if (poll->wait.private)
5260 refcount_dec(&req->refs);
5261 poll->head = NULL;
5262 spin_unlock(&head->lock);
5263 }
5264}
5265
5266static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
5267{
5268 struct io_ring_ctx *ctx = req->ctx;
5269
Jens Axboed4e7cd32020-08-15 11:44:50 -07005270 io_poll_remove_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005271 req->poll.done = true;
5272 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
5273 io_commit_cqring(ctx);
5274}
5275
Jens Axboe18bceab2020-05-15 11:56:54 -06005276static void io_poll_task_func(struct callback_head *cb)
5277{
5278 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06005279 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005280 struct io_kiocb *nxt;
Jens Axboe18bceab2020-05-15 11:56:54 -06005281
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005282 if (io_poll_rewait(req, &req->poll)) {
5283 spin_unlock_irq(&ctx->completion_lock);
5284 } else {
5285 hash_del(&req->hash_node);
5286 io_poll_complete(req, req->result, 0);
5287 spin_unlock_irq(&ctx->completion_lock);
5288
5289 nxt = io_put_req_find_next(req);
5290 io_cqring_ev_posted(ctx);
5291 if (nxt)
5292 __io_req_task_submit(nxt);
5293 }
5294
Jens Axboe6d816e02020-08-11 08:04:14 -06005295 percpu_ref_put(&ctx->refs);
Jens Axboe18bceab2020-05-15 11:56:54 -06005296}
5297
5298static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
5299 int sync, void *key)
5300{
5301 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005302 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005303 __poll_t mask = key_to_poll(key);
5304
5305 /* for instances that support it check for an event match first: */
5306 if (mask && !(mask & poll->events))
5307 return 0;
5308
Jens Axboe8706e042020-09-28 08:38:54 -06005309 list_del_init(&wait->entry);
5310
Jens Axboe807abcb2020-07-17 17:09:27 -06005311 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005312 bool done;
5313
Jens Axboe807abcb2020-07-17 17:09:27 -06005314 spin_lock(&poll->head->lock);
5315 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06005316 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06005317 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005318 /* make sure double remove sees this as being gone */
5319 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06005320 spin_unlock(&poll->head->lock);
Jens Axboec8b5e262020-10-25 13:53:26 -06005321 if (!done) {
5322 /* use wait func handler, so it matches the rq type */
5323 poll->wait.func(&poll->wait, mode, sync, key);
5324 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005325 }
5326 refcount_dec(&req->refs);
5327 return 1;
5328}
5329
5330static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
5331 wait_queue_func_t wake_func)
5332{
5333 poll->head = NULL;
5334 poll->done = false;
5335 poll->canceled = false;
5336 poll->events = events;
5337 INIT_LIST_HEAD(&poll->wait.entry);
5338 init_waitqueue_func_entry(&poll->wait, wake_func);
5339}
5340
5341static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06005342 struct wait_queue_head *head,
5343 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06005344{
5345 struct io_kiocb *req = pt->req;
5346
5347 /*
5348 * If poll->head is already set, it's because the file being polled
5349 * uses multiple waitqueues for poll handling (eg one for read, one
5350 * for write). Setup a separate io_poll_iocb if this happens.
5351 */
5352 if (unlikely(poll->head)) {
Pavel Begunkov58852d42020-10-16 20:55:56 +01005353 struct io_poll_iocb *poll_one = poll;
5354
Jens Axboe18bceab2020-05-15 11:56:54 -06005355 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06005356 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005357 pt->error = -EINVAL;
5358 return;
5359 }
5360 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
5361 if (!poll) {
5362 pt->error = -ENOMEM;
5363 return;
5364 }
Pavel Begunkov58852d42020-10-16 20:55:56 +01005365 io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake);
Jens Axboe18bceab2020-05-15 11:56:54 -06005366 refcount_inc(&req->refs);
5367 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06005368 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005369 }
5370
5371 pt->error = 0;
5372 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005373
5374 if (poll->events & EPOLLEXCLUSIVE)
5375 add_wait_queue_exclusive(head, &poll->wait);
5376 else
5377 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06005378}
5379
5380static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
5381 struct poll_table_struct *p)
5382{
5383 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06005384 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005385
Jens Axboe807abcb2020-07-17 17:09:27 -06005386 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06005387}
5388
Jens Axboed7718a92020-02-14 22:23:12 -07005389static void io_async_task_func(struct callback_head *cb)
5390{
5391 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
5392 struct async_poll *apoll = req->apoll;
5393 struct io_ring_ctx *ctx = req->ctx;
5394
5395 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
5396
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005397 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07005398 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6d816e02020-08-11 08:04:14 -06005399 percpu_ref_put(&ctx->refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005400 return;
Jens Axboed7718a92020-02-14 22:23:12 -07005401 }
5402
Jens Axboe31067252020-05-17 17:43:31 -06005403 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005404 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005405 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06005406
Jens Axboed4e7cd32020-08-15 11:44:50 -07005407 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005408 spin_unlock_irq(&ctx->completion_lock);
5409
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005410 if (!READ_ONCE(apoll->poll.canceled))
5411 __io_req_task_submit(req);
5412 else
5413 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03005414
Jens Axboe6d816e02020-08-11 08:04:14 -06005415 percpu_ref_put(&ctx->refs);
Jens Axboe807abcb2020-07-17 17:09:27 -06005416 kfree(apoll->double_poll);
Jens Axboe31067252020-05-17 17:43:31 -06005417 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07005418}
5419
5420static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5421 void *key)
5422{
5423 struct io_kiocb *req = wait->private;
5424 struct io_poll_iocb *poll = &req->apoll->poll;
5425
5426 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
5427 key_to_poll(key));
5428
5429 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
5430}
5431
5432static void io_poll_req_insert(struct io_kiocb *req)
5433{
5434 struct io_ring_ctx *ctx = req->ctx;
5435 struct hlist_head *list;
5436
5437 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5438 hlist_add_head(&req->hash_node, list);
5439}
5440
5441static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5442 struct io_poll_iocb *poll,
5443 struct io_poll_table *ipt, __poll_t mask,
5444 wait_queue_func_t wake_func)
5445 __acquires(&ctx->completion_lock)
5446{
5447 struct io_ring_ctx *ctx = req->ctx;
5448 bool cancel = false;
5449
Pavel Begunkov4d52f332020-10-18 10:17:43 +01005450 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe18bceab2020-05-15 11:56:54 -06005451 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03005452 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06005453 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07005454
5455 ipt->pt._key = mask;
5456 ipt->req = req;
5457 ipt->error = -EINVAL;
5458
Jens Axboed7718a92020-02-14 22:23:12 -07005459 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
5460
5461 spin_lock_irq(&ctx->completion_lock);
5462 if (likely(poll->head)) {
5463 spin_lock(&poll->head->lock);
5464 if (unlikely(list_empty(&poll->wait.entry))) {
5465 if (ipt->error)
5466 cancel = true;
5467 ipt->error = 0;
5468 mask = 0;
5469 }
5470 if (mask || ipt->error)
5471 list_del_init(&poll->wait.entry);
5472 else if (cancel)
5473 WRITE_ONCE(poll->canceled, true);
5474 else if (!poll->done) /* actually waiting for an event */
5475 io_poll_req_insert(req);
5476 spin_unlock(&poll->head->lock);
5477 }
5478
5479 return mask;
5480}
5481
5482static bool io_arm_poll_handler(struct io_kiocb *req)
5483{
5484 const struct io_op_def *def = &io_op_defs[req->opcode];
5485 struct io_ring_ctx *ctx = req->ctx;
5486 struct async_poll *apoll;
5487 struct io_poll_table ipt;
5488 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005489 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07005490
5491 if (!req->file || !file_can_poll(req->file))
5492 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03005493 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07005494 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005495 if (def->pollin)
5496 rw = READ;
5497 else if (def->pollout)
5498 rw = WRITE;
5499 else
5500 return false;
5501 /* if we can't nonblock try, then no point in arming a poll handler */
5502 if (!io_file_supports_async(req->file, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07005503 return false;
5504
5505 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5506 if (unlikely(!apoll))
5507 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06005508 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005509
5510 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005511 req->apoll = apoll;
Jens Axboed7718a92020-02-14 22:23:12 -07005512
Nathan Chancellor8755d972020-03-02 16:01:19 -07005513 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005514 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07005515 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07005516 if (def->pollout)
5517 mask |= POLLOUT | POLLWRNORM;
Luke Hsiao901341b2020-08-21 21:41:05 -07005518
5519 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5520 if ((req->opcode == IORING_OP_RECVMSG) &&
5521 (req->sr_msg.msg_flags & MSG_ERRQUEUE))
5522 mask &= ~POLLIN;
5523
Jens Axboed7718a92020-02-14 22:23:12 -07005524 mask |= POLLERR | POLLPRI;
5525
5526 ipt.pt._qproc = io_async_queue_proc;
5527
5528 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5529 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06005530 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07005531 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005532 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe807abcb2020-07-17 17:09:27 -06005533 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07005534 kfree(apoll);
5535 return false;
5536 }
5537 spin_unlock_irq(&ctx->completion_lock);
5538 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
5539 apoll->poll.events);
5540 return true;
5541}
5542
5543static bool __io_poll_remove_one(struct io_kiocb *req,
5544 struct io_poll_iocb *poll)
5545{
Jens Axboeb41e9852020-02-17 09:52:41 -07005546 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005547
5548 spin_lock(&poll->head->lock);
5549 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005550 if (!list_empty(&poll->wait.entry)) {
5551 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005552 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005553 }
5554 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005555 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005556 return do_complete;
5557}
5558
5559static bool io_poll_remove_one(struct io_kiocb *req)
5560{
5561 bool do_complete;
5562
Jens Axboed4e7cd32020-08-15 11:44:50 -07005563 io_poll_remove_double(req);
5564
Jens Axboed7718a92020-02-14 22:23:12 -07005565 if (req->opcode == IORING_OP_POLL_ADD) {
5566 do_complete = __io_poll_remove_one(req, &req->poll);
5567 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005568 struct async_poll *apoll = req->apoll;
5569
Jens Axboed7718a92020-02-14 22:23:12 -07005570 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005571 do_complete = __io_poll_remove_one(req, &apoll->poll);
5572 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07005573 io_put_req(req);
Jens Axboe807abcb2020-07-17 17:09:27 -06005574 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005575 kfree(apoll);
5576 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005577 }
5578
Jens Axboeb41e9852020-02-17 09:52:41 -07005579 if (do_complete) {
5580 io_cqring_fill_event(req, -ECANCELED);
5581 io_commit_cqring(req->ctx);
Jens Axboef254ac02020-08-12 17:33:30 -06005582 req_set_fail_links(req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01005583 io_put_req_deferred(req, 1);
Jens Axboeb41e9852020-02-17 09:52:41 -07005584 }
5585
5586 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005587}
5588
Jens Axboe76e1b642020-09-26 15:05:03 -06005589/*
5590 * Returns true if we found and killed one or more poll requests
5591 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00005592static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk,
5593 struct files_struct *files)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005594{
Jens Axboe78076bb2019-12-04 19:56:40 -07005595 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005596 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005597 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005598
5599 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005600 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5601 struct hlist_head *list;
5602
5603 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005604 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
Pavel Begunkov6b819282020-11-06 13:00:25 +00005605 if (io_match_task(req, tsk, files))
Jens Axboef3606e32020-09-22 08:18:24 -06005606 posted += io_poll_remove_one(req);
5607 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005608 }
5609 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005610
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005611 if (posted)
5612 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005613
5614 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005615}
5616
Jens Axboe47f46762019-11-09 17:43:02 -07005617static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5618{
Jens Axboe78076bb2019-12-04 19:56:40 -07005619 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005620 struct io_kiocb *req;
5621
Jens Axboe78076bb2019-12-04 19:56:40 -07005622 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5623 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005624 if (sqe_addr != req->user_data)
5625 continue;
5626 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07005627 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07005628 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07005629 }
5630
5631 return -ENOENT;
5632}
5633
Jens Axboe3529d8c2019-12-19 18:24:38 -07005634static int io_poll_remove_prep(struct io_kiocb *req,
5635 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005636{
Jens Axboe221c5eb2019-01-17 09:41:58 -07005637 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5638 return -EINVAL;
5639 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5640 sqe->poll_events)
5641 return -EINVAL;
5642
Pavel Begunkov018043b2020-10-27 23:17:18 +00005643 req->poll_remove.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07005644 return 0;
5645}
5646
5647/*
5648 * Find a running poll command that matches one specified in sqe->addr,
5649 * and remove it if found.
5650 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00005651static int io_poll_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005652{
5653 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe0969e782019-12-17 18:40:57 -07005654 int ret;
5655
Jens Axboe221c5eb2019-01-17 09:41:58 -07005656 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov018043b2020-10-27 23:17:18 +00005657 ret = io_poll_cancel(ctx, req->poll_remove.addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005658 spin_unlock_irq(&ctx->completion_lock);
5659
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005660 if (ret < 0)
5661 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005662 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005663 return 0;
5664}
5665
Jens Axboe221c5eb2019-01-17 09:41:58 -07005666static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5667 void *key)
5668{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005669 struct io_kiocb *req = wait->private;
5670 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005671
Jens Axboed7718a92020-02-14 22:23:12 -07005672 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005673}
5674
Jens Axboe221c5eb2019-01-17 09:41:58 -07005675static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5676 struct poll_table_struct *p)
5677{
5678 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5679
Jens Axboee8c2bc12020-08-15 18:44:09 -07005680 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005681}
5682
Jens Axboe3529d8c2019-12-19 18:24:38 -07005683static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005684{
5685 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08005686 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005687
5688 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5689 return -EINVAL;
5690 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5691 return -EINVAL;
5692
Jiufei Xue5769a352020-06-17 17:53:55 +08005693 events = READ_ONCE(sqe->poll32_events);
5694#ifdef __BIG_ENDIAN
5695 events = swahw32(events);
5696#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005697 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5698 (events & EPOLLEXCLUSIVE);
Jens Axboe0969e782019-12-17 18:40:57 -07005699 return 0;
5700}
5701
Pavel Begunkov61e98202021-02-10 00:03:08 +00005702static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005703{
5704 struct io_poll_iocb *poll = &req->poll;
5705 struct io_ring_ctx *ctx = req->ctx;
5706 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005707 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005708
Jens Axboed7718a92020-02-14 22:23:12 -07005709 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005710
Jens Axboed7718a92020-02-14 22:23:12 -07005711 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5712 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005713
Jens Axboe8c838782019-03-12 15:48:16 -06005714 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005715 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005716 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06005717 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005718 spin_unlock_irq(&ctx->completion_lock);
5719
Jens Axboe8c838782019-03-12 15:48:16 -06005720 if (mask) {
5721 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03005722 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005723 }
Jens Axboe8c838782019-03-12 15:48:16 -06005724 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005725}
5726
Jens Axboe5262f562019-09-17 12:26:57 -06005727static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5728{
Jens Axboead8a48a2019-11-15 08:49:11 -07005729 struct io_timeout_data *data = container_of(timer,
5730 struct io_timeout_data, timer);
5731 struct io_kiocb *req = data->req;
5732 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005733 unsigned long flags;
5734
Jens Axboe5262f562019-09-17 12:26:57 -06005735 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkova71976f2020-10-10 18:34:11 +01005736 list_del_init(&req->timeout.list);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005737 atomic_set(&req->ctx->cq_timeouts,
5738 atomic_read(&req->ctx->cq_timeouts) + 1);
5739
Jens Axboe78e19bb2019-11-06 15:21:34 -07005740 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005741 io_commit_cqring(ctx);
5742 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5743
5744 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005745 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005746 io_put_req(req);
5747 return HRTIMER_NORESTART;
5748}
5749
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005750static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
5751 __u64 user_data)
Jens Axboe47f46762019-11-09 17:43:02 -07005752{
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005753 struct io_timeout_data *io;
Jens Axboef254ac02020-08-12 17:33:30 -06005754 struct io_kiocb *req;
5755 int ret = -ENOENT;
5756
5757 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5758 if (user_data == req->user_data) {
5759 ret = 0;
5760 break;
5761 }
5762 }
5763
5764 if (ret == -ENOENT)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005765 return ERR_PTR(ret);
Jens Axboef254ac02020-08-12 17:33:30 -06005766
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005767 io = req->async_data;
5768 ret = hrtimer_try_to_cancel(&io->timer);
5769 if (ret == -1)
5770 return ERR_PTR(-EALREADY);
5771 list_del_init(&req->timeout.list);
5772 return req;
5773}
5774
5775static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5776{
5777 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5778
5779 if (IS_ERR(req))
5780 return PTR_ERR(req);
5781
5782 req_set_fail_links(req);
5783 io_cqring_fill_event(req, -ECANCELED);
5784 io_put_req_deferred(req, 1);
5785 return 0;
Jens Axboef254ac02020-08-12 17:33:30 -06005786}
5787
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005788static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
5789 struct timespec64 *ts, enum hrtimer_mode mode)
5790{
5791 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5792 struct io_timeout_data *data;
5793
5794 if (IS_ERR(req))
5795 return PTR_ERR(req);
5796
5797 req->timeout.off = 0; /* noseq */
5798 data = req->async_data;
5799 list_add_tail(&req->timeout.list, &ctx->timeout_list);
5800 hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode);
5801 data->timer.function = io_timeout_fn;
5802 hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode);
5803 return 0;
Jens Axboe47f46762019-11-09 17:43:02 -07005804}
5805
Jens Axboe3529d8c2019-12-19 18:24:38 -07005806static int io_timeout_remove_prep(struct io_kiocb *req,
5807 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005808{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005809 struct io_timeout_rem *tr = &req->timeout_rem;
5810
Jens Axboeb29472e2019-12-17 18:50:29 -07005811 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5812 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005813 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5814 return -EINVAL;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005815 if (sqe->ioprio || sqe->buf_index || sqe->len)
Jens Axboeb29472e2019-12-17 18:50:29 -07005816 return -EINVAL;
5817
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005818 tr->addr = READ_ONCE(sqe->addr);
5819 tr->flags = READ_ONCE(sqe->timeout_flags);
5820 if (tr->flags & IORING_TIMEOUT_UPDATE) {
5821 if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS))
5822 return -EINVAL;
5823 if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2)))
5824 return -EFAULT;
5825 } else if (tr->flags) {
5826 /* timeout removal doesn't support flags */
5827 return -EINVAL;
5828 }
5829
Jens Axboeb29472e2019-12-17 18:50:29 -07005830 return 0;
5831}
5832
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005833static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags)
5834{
5835 return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS
5836 : HRTIMER_MODE_REL;
5837}
5838
Jens Axboe11365042019-10-16 09:08:32 -06005839/*
5840 * Remove or update an existing timeout command
5841 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00005842static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe11365042019-10-16 09:08:32 -06005843{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005844 struct io_timeout_rem *tr = &req->timeout_rem;
Jens Axboe11365042019-10-16 09:08:32 -06005845 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005846 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005847
Jens Axboe11365042019-10-16 09:08:32 -06005848 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005849 if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE))
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005850 ret = io_timeout_cancel(ctx, tr->addr);
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005851 else
5852 ret = io_timeout_update(ctx, tr->addr, &tr->ts,
5853 io_translate_timeout_mode(tr->flags));
Jens Axboe11365042019-10-16 09:08:32 -06005854
Jens Axboe47f46762019-11-09 17:43:02 -07005855 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005856 io_commit_cqring(ctx);
5857 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005858 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005859 if (ret < 0)
5860 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005861 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005862 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005863}
5864
Jens Axboe3529d8c2019-12-19 18:24:38 -07005865static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005866 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005867{
Jens Axboead8a48a2019-11-15 08:49:11 -07005868 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005869 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005870 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005871
Jens Axboead8a48a2019-11-15 08:49:11 -07005872 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005873 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005874 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005875 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005876 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005877 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005878 flags = READ_ONCE(sqe->timeout_flags);
5879 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005880 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005881
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005882 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005883
Jens Axboee8c2bc12020-08-15 18:44:09 -07005884 if (!req->async_data && io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005885 return -ENOMEM;
5886
Jens Axboee8c2bc12020-08-15 18:44:09 -07005887 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005888 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005889
5890 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005891 return -EFAULT;
5892
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005893 data->mode = io_translate_timeout_mode(flags);
Jens Axboead8a48a2019-11-15 08:49:11 -07005894 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5895 return 0;
5896}
5897
Pavel Begunkov61e98202021-02-10 00:03:08 +00005898static int io_timeout(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboead8a48a2019-11-15 08:49:11 -07005899{
Jens Axboead8a48a2019-11-15 08:49:11 -07005900 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005901 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005902 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005903 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005904
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005905 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005906
Jens Axboe5262f562019-09-17 12:26:57 -06005907 /*
5908 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005909 * timeout event to be satisfied. If it isn't set, then this is
5910 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005911 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005912 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005913 entry = ctx->timeout_list.prev;
5914 goto add;
5915 }
Jens Axboe5262f562019-09-17 12:26:57 -06005916
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005917 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5918 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005919
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05005920 /* Update the last seq here in case io_flush_timeouts() hasn't.
5921 * This is safe because ->completion_lock is held, and submissions
5922 * and completions are never mixed in the same ->completion_lock section.
5923 */
5924 ctx->cq_last_tm_flush = tail;
5925
Jens Axboe5262f562019-09-17 12:26:57 -06005926 /*
5927 * Insertion sort, ensuring the first entry in the list is always
5928 * the one we need first.
5929 */
Jens Axboe5262f562019-09-17 12:26:57 -06005930 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005931 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5932 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005933
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005934 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005935 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005936 /* nxt.seq is behind @tail, otherwise would've been completed */
5937 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005938 break;
5939 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005940add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005941 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005942 data->timer.function = io_timeout_fn;
5943 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005944 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005945 return 0;
5946}
5947
Jens Axboe62755e32019-10-28 21:49:21 -06005948static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005949{
Jens Axboe62755e32019-10-28 21:49:21 -06005950 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005951
Jens Axboe62755e32019-10-28 21:49:21 -06005952 return req->user_data == (unsigned long) data;
5953}
5954
Jens Axboee977d6d2019-11-05 12:39:45 -07005955static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005956{
Jens Axboe62755e32019-10-28 21:49:21 -06005957 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005958 int ret = 0;
5959
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005960 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005961 switch (cancel_ret) {
5962 case IO_WQ_CANCEL_OK:
5963 ret = 0;
5964 break;
5965 case IO_WQ_CANCEL_RUNNING:
5966 ret = -EALREADY;
5967 break;
5968 case IO_WQ_CANCEL_NOTFOUND:
5969 ret = -ENOENT;
5970 break;
5971 }
5972
Jens Axboee977d6d2019-11-05 12:39:45 -07005973 return ret;
5974}
5975
Jens Axboe47f46762019-11-09 17:43:02 -07005976static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5977 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005978 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005979{
5980 unsigned long flags;
5981 int ret;
5982
5983 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5984 if (ret != -ENOENT) {
5985 spin_lock_irqsave(&ctx->completion_lock, flags);
5986 goto done;
5987 }
5988
5989 spin_lock_irqsave(&ctx->completion_lock, flags);
5990 ret = io_timeout_cancel(ctx, sqe_addr);
5991 if (ret != -ENOENT)
5992 goto done;
5993 ret = io_poll_cancel(ctx, sqe_addr);
5994done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005995 if (!ret)
5996 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005997 io_cqring_fill_event(req, ret);
5998 io_commit_cqring(ctx);
5999 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6000 io_cqring_ev_posted(ctx);
6001
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006002 if (ret < 0)
6003 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03006004 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07006005}
6006
Jens Axboe3529d8c2019-12-19 18:24:38 -07006007static int io_async_cancel_prep(struct io_kiocb *req,
6008 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07006009{
Jens Axboefbf23842019-12-17 18:45:56 -07006010 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07006011 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06006012 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
6013 return -EINVAL;
6014 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07006015 return -EINVAL;
6016
Jens Axboefbf23842019-12-17 18:45:56 -07006017 req->cancel.addr = READ_ONCE(sqe->addr);
6018 return 0;
6019}
6020
Pavel Begunkov61e98202021-02-10 00:03:08 +00006021static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefbf23842019-12-17 18:45:56 -07006022{
6023 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07006024
Pavel Begunkov014db002020-03-03 21:33:12 +03006025 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06006026 return 0;
6027}
6028
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006029static int io_rsrc_update_prep(struct io_kiocb *req,
Jens Axboe05f3fb32019-12-09 11:22:50 -07006030 const struct io_uring_sqe *sqe)
6031{
Jens Axboe6ca56f82020-09-18 16:51:19 -06006032 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
6033 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06006034 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
6035 return -EINVAL;
6036 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006037 return -EINVAL;
6038
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006039 req->rsrc_update.offset = READ_ONCE(sqe->off);
6040 req->rsrc_update.nr_args = READ_ONCE(sqe->len);
6041 if (!req->rsrc_update.nr_args)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006042 return -EINVAL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006043 req->rsrc_update.arg = READ_ONCE(sqe->addr);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006044 return 0;
6045}
6046
Pavel Begunkov889fca72021-02-10 00:03:09 +00006047static int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006048{
6049 struct io_ring_ctx *ctx = req->ctx;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006050 struct io_uring_rsrc_update up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006051 int ret;
6052
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006053 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006054 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006055
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006056 up.offset = req->rsrc_update.offset;
6057 up.data = req->rsrc_update.arg;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006058
6059 mutex_lock(&ctx->uring_lock);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006060 ret = __io_sqe_files_update(ctx, &up, req->rsrc_update.nr_args);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006061 mutex_unlock(&ctx->uring_lock);
6062
6063 if (ret < 0)
6064 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00006065 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006066 return 0;
6067}
6068
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006069static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07006070{
Jens Axboed625c6e2019-12-17 19:53:05 -07006071 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07006072 case IORING_OP_NOP:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006073 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07006074 case IORING_OP_READV:
6075 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006076 case IORING_OP_READ:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006077 return io_read_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006078 case IORING_OP_WRITEV:
6079 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006080 case IORING_OP_WRITE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006081 return io_write_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006082 case IORING_OP_POLL_ADD:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006083 return io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006084 case IORING_OP_POLL_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006085 return io_poll_remove_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006086 case IORING_OP_FSYNC:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006087 return io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006088 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006089 return io_prep_sfr(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006090 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006091 case IORING_OP_SEND:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006092 return io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006093 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006094 case IORING_OP_RECV:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006095 return io_recvmsg_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07006096 case IORING_OP_CONNECT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006097 return io_connect_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006098 case IORING_OP_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006099 return io_timeout_prep(req, sqe, false);
Jens Axboeb29472e2019-12-17 18:50:29 -07006100 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006101 return io_timeout_remove_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07006102 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006103 return io_async_cancel_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006104 case IORING_OP_LINK_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006105 return io_timeout_prep(req, sqe, true);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006106 case IORING_OP_ACCEPT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006107 return io_accept_prep(req, sqe);
Jens Axboed63d1b52019-12-10 10:38:56 -07006108 case IORING_OP_FALLOCATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006109 return io_fallocate_prep(req, sqe);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006110 case IORING_OP_OPENAT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006111 return io_openat_prep(req, sqe);
Jens Axboeb5dba592019-12-11 14:02:38 -07006112 case IORING_OP_CLOSE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006113 return io_close_prep(req, sqe);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006114 case IORING_OP_FILES_UPDATE:
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006115 return io_rsrc_update_prep(req, sqe);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006116 case IORING_OP_STATX:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006117 return io_statx_prep(req, sqe);
Jens Axboe4840e412019-12-25 22:03:45 -07006118 case IORING_OP_FADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006119 return io_fadvise_prep(req, sqe);
Jens Axboec1ca7572019-12-25 22:18:28 -07006120 case IORING_OP_MADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006121 return io_madvise_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07006122 case IORING_OP_OPENAT2:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006123 return io_openat2_prep(req, sqe);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006124 case IORING_OP_EPOLL_CTL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006125 return io_epoll_ctl_prep(req, sqe);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006126 case IORING_OP_SPLICE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006127 return io_splice_prep(req, sqe);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006128 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006129 return io_provide_buffers_prep(req, sqe);
Jens Axboe067524e2020-03-02 16:32:28 -07006130 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006131 return io_remove_buffers_prep(req, sqe);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006132 case IORING_OP_TEE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006133 return io_tee_prep(req, sqe);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006134 case IORING_OP_SHUTDOWN:
6135 return io_shutdown_prep(req, sqe);
Jens Axboe80a261f2020-09-28 14:23:58 -06006136 case IORING_OP_RENAMEAT:
6137 return io_renameat_prep(req, sqe);
Jens Axboe14a11432020-09-28 14:27:37 -06006138 case IORING_OP_UNLINKAT:
6139 return io_unlinkat_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006140 }
6141
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006142 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
6143 req->opcode);
6144 return-EINVAL;
6145}
6146
Jens Axboedef596e2019-01-09 08:59:42 -07006147static int io_req_defer_prep(struct io_kiocb *req,
6148 const struct io_uring_sqe *sqe)
Jens Axboedef596e2019-01-09 08:59:42 -07006149{
Jens Axboedef596e2019-01-09 08:59:42 -07006150 if (!sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006151 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006152 if (io_alloc_async_data(req))
Jens Axboeb76da702019-11-20 13:05:32 -07006153 return -EAGAIN;
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006154 return io_req_prep(req, sqe);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006155}
6156
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006157static u32 io_get_sequence(struct io_kiocb *req)
6158{
6159 struct io_kiocb *pos;
6160 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006161 u32 total_submitted, nr_reqs = 0;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006162
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006163 io_for_each_link(pos, req)
6164 nr_reqs++;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006165
6166 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
6167 return total_submitted - nr_reqs;
6168}
6169
Jens Axboe3529d8c2019-12-19 18:24:38 -07006170static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006171{
6172 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006173 struct io_defer_entry *de;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006174 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006175 u32 seq;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006176
6177 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006178 if (likely(list_empty_careful(&ctx->defer_list) &&
6179 !(req->flags & REQ_F_IO_DRAIN)))
6180 return 0;
6181
6182 seq = io_get_sequence(req);
6183 /* Still a chance to pass the sequence check */
6184 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006185 return 0;
6186
Jens Axboee8c2bc12020-08-15 18:44:09 -07006187 if (!req->async_data) {
Pavel Begunkov650b5482020-05-17 14:02:11 +03006188 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006189 if (ret)
Pavel Begunkov650b5482020-05-17 14:02:11 +03006190 return ret;
6191 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03006192 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006193 de = kmalloc(sizeof(*de), GFP_KERNEL);
6194 if (!de)
6195 return -ENOMEM;
Jens Axboe31b51512019-01-18 22:56:34 -07006196
6197 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006198 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboe31b51512019-01-18 22:56:34 -07006199 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006200 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03006201 io_queue_async_work(req);
6202 return -EIOCBQUEUED;
Jens Axboe31b51512019-01-18 22:56:34 -07006203 }
6204
6205 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006206 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006207 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006208 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboe31b51512019-01-18 22:56:34 -07006209 spin_unlock_irq(&ctx->completion_lock);
6210 return -EIOCBQUEUED;
6211}
Jens Axboeedafcce2019-01-09 09:16:05 -07006212
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03006213static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006214{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006215 if (req->flags & REQ_F_BUFFER_SELECTED) {
6216 switch (req->opcode) {
6217 case IORING_OP_READV:
6218 case IORING_OP_READ_FIXED:
6219 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07006220 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006221 break;
6222 case IORING_OP_RECVMSG:
6223 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07006224 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006225 break;
6226 }
6227 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006228 }
6229
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006230 if (req->flags & REQ_F_NEED_CLEANUP) {
6231 switch (req->opcode) {
6232 case IORING_OP_READV:
6233 case IORING_OP_READ_FIXED:
6234 case IORING_OP_READ:
6235 case IORING_OP_WRITEV:
6236 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006237 case IORING_OP_WRITE: {
6238 struct io_async_rw *io = req->async_data;
6239 if (io->free_iovec)
6240 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006241 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006242 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006243 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006244 case IORING_OP_SENDMSG: {
6245 struct io_async_msghdr *io = req->async_data;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00006246
6247 kfree(io->free_iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006248 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006249 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006250 case IORING_OP_SPLICE:
6251 case IORING_OP_TEE:
6252 io_put_file(req, req->splice.file_in,
6253 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
6254 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06006255 case IORING_OP_OPENAT:
6256 case IORING_OP_OPENAT2:
6257 if (req->open.filename)
6258 putname(req->open.filename);
6259 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006260 case IORING_OP_RENAMEAT:
6261 putname(req->rename.oldpath);
6262 putname(req->rename.newpath);
6263 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006264 case IORING_OP_UNLINKAT:
6265 putname(req->unlink.filename);
6266 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006267 }
6268 req->flags &= ~REQ_F_NEED_CLEANUP;
6269 }
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006270}
6271
Pavel Begunkov889fca72021-02-10 00:03:09 +00006272static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeedafcce2019-01-09 09:16:05 -07006273{
Jens Axboeedafcce2019-01-09 09:16:05 -07006274 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07006275 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07006276
Jens Axboed625c6e2019-12-17 19:53:05 -07006277 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07006278 case IORING_OP_NOP:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006279 ret = io_nop(req, issue_flags);
Jens Axboe31b51512019-01-18 22:56:34 -07006280 break;
6281 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07006282 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006283 case IORING_OP_READ:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006284 ret = io_read(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006285 break;
6286 case IORING_OP_WRITEV:
Jens Axboe2b188cc2019-01-07 10:46:33 -07006287 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006288 case IORING_OP_WRITE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006289 ret = io_write(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006290 break;
6291 case IORING_OP_FSYNC:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006292 ret = io_fsync(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006293 break;
6294 case IORING_OP_POLL_ADD:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006295 ret = io_poll_add(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006296 break;
6297 case IORING_OP_POLL_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006298 ret = io_poll_remove(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006299 break;
6300 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006301 ret = io_sync_file_range(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006302 break;
6303 case IORING_OP_SENDMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006304 ret = io_sendmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006305 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006306 case IORING_OP_SEND:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006307 ret = io_send(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006308 break;
6309 case IORING_OP_RECVMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006310 ret = io_recvmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006311 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006312 case IORING_OP_RECV:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006313 ret = io_recv(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006314 break;
6315 case IORING_OP_TIMEOUT:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006316 ret = io_timeout(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006317 break;
6318 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006319 ret = io_timeout_remove(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006320 break;
6321 case IORING_OP_ACCEPT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006322 ret = io_accept(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006323 break;
6324 case IORING_OP_CONNECT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006325 ret = io_connect(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006326 break;
6327 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006328 ret = io_async_cancel(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006329 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07006330 case IORING_OP_FALLOCATE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006331 ret = io_fallocate(req, issue_flags);
Jens Axboed63d1b52019-12-10 10:38:56 -07006332 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07006333 case IORING_OP_OPENAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006334 ret = io_openat(req, issue_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006335 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07006336 case IORING_OP_CLOSE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006337 ret = io_close(req, issue_flags);
Jens Axboeb5dba592019-12-11 14:02:38 -07006338 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006339 case IORING_OP_FILES_UPDATE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006340 ret = io_files_update(req, issue_flags);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006341 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006342 case IORING_OP_STATX:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006343 ret = io_statx(req, issue_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006344 break;
Jens Axboe4840e412019-12-25 22:03:45 -07006345 case IORING_OP_FADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006346 ret = io_fadvise(req, issue_flags);
Jens Axboe4840e412019-12-25 22:03:45 -07006347 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07006348 case IORING_OP_MADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006349 ret = io_madvise(req, issue_flags);
Jens Axboec1ca7572019-12-25 22:18:28 -07006350 break;
Jens Axboecebdb982020-01-08 17:59:24 -07006351 case IORING_OP_OPENAT2:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006352 ret = io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07006353 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07006354 case IORING_OP_EPOLL_CTL:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006355 ret = io_epoll_ctl(req, issue_flags);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006356 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006357 case IORING_OP_SPLICE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006358 ret = io_splice(req, issue_flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006359 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07006360 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006361 ret = io_provide_buffers(req, issue_flags);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006362 break;
Jens Axboe067524e2020-03-02 16:32:28 -07006363 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006364 ret = io_remove_buffers(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006365 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006366 case IORING_OP_TEE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006367 ret = io_tee(req, issue_flags);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006368 break;
Jens Axboe36f4fa62020-09-05 11:14:22 -06006369 case IORING_OP_SHUTDOWN:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006370 ret = io_shutdown(req, issue_flags);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006371 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006372 case IORING_OP_RENAMEAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006373 ret = io_renameat(req, issue_flags);
Jens Axboe80a261f2020-09-28 14:23:58 -06006374 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006375 case IORING_OP_UNLINKAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006376 ret = io_unlinkat(req, issue_flags);
Jens Axboe14a11432020-09-28 14:27:37 -06006377 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006378 default:
6379 ret = -EINVAL;
6380 break;
Jens Axboe31b51512019-01-18 22:56:34 -07006381 }
6382
6383 if (ret)
Jens Axboeedafcce2019-01-09 09:16:05 -07006384 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006385
Jens Axboeb5325762020-05-19 21:20:27 -06006386 /* If the op doesn't have a file, we're not polling for it */
6387 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07006388 const bool in_async = io_wq_current_is_worker();
6389
Jens Axboe11ba8202020-01-15 21:51:17 -07006390 /* workqueue context doesn't hold uring_lock, grab it now */
6391 if (in_async)
6392 mutex_lock(&ctx->uring_lock);
6393
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08006394 io_iopoll_req_issued(req, in_async);
Jens Axboe11ba8202020-01-15 21:51:17 -07006395
6396 if (in_async)
6397 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006398 }
6399
6400 return 0;
6401}
6402
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00006403static void io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006404{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006405 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006406 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006407 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006408
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006409 timeout = io_prep_linked_timeout(req);
6410 if (timeout)
6411 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006412
Jens Axboe4014d942021-01-19 15:53:54 -07006413 if (work->flags & IO_WQ_WORK_CANCEL)
Jens Axboe561fb042019-10-24 07:25:42 -06006414 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07006415
Jens Axboe561fb042019-10-24 07:25:42 -06006416 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006417 do {
Pavel Begunkov889fca72021-02-10 00:03:09 +00006418 ret = io_issue_sqe(req, 0);
Jens Axboe561fb042019-10-24 07:25:42 -06006419 /*
6420 * We can get EAGAIN for polled IO even though we're
6421 * forcing a sync submission from here, since we can't
6422 * wait for request slots on the block side.
6423 */
6424 if (ret != -EAGAIN)
6425 break;
6426 cond_resched();
6427 } while (1);
6428 }
Jens Axboe31b51512019-01-18 22:56:34 -07006429
Jens Axboe561fb042019-10-24 07:25:42 -06006430 if (ret) {
Xiaoguang Wangc07e6712020-12-14 23:49:41 +08006431 struct io_ring_ctx *lock_ctx = NULL;
Xiaoguang Wangdad1b122020-12-06 22:22:42 +00006432
Xiaoguang Wangc07e6712020-12-14 23:49:41 +08006433 if (req->ctx->flags & IORING_SETUP_IOPOLL)
6434 lock_ctx = req->ctx;
6435
6436 /*
6437 * io_iopoll_complete() does not hold completion_lock to
6438 * complete polled io, so here for polled io, we can not call
6439 * io_req_complete() directly, otherwise there maybe concurrent
6440 * access to cqring, defer_list, etc, which is not safe. Given
6441 * that io_iopoll_complete() is always called under uring_lock,
6442 * so here for polled io, we also get uring_lock to complete
6443 * it.
6444 */
6445 if (lock_ctx)
6446 mutex_lock(&lock_ctx->uring_lock);
6447
6448 req_set_fail_links(req);
6449 io_req_complete(req, ret);
6450
6451 if (lock_ctx)
6452 mutex_unlock(&lock_ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07006453 }
Jens Axboe31b51512019-01-18 22:56:34 -07006454}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006455
Jens Axboe65e19f52019-10-26 07:20:21 -06006456static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6457 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06006458{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006459 struct fixed_rsrc_table *table;
Jens Axboe65e19f52019-10-26 07:20:21 -06006460
Jens Axboe05f3fb32019-12-09 11:22:50 -07006461 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08006462 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06006463}
6464
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006465static struct file *io_file_get(struct io_submit_state *state,
6466 struct io_kiocb *req, int fd, bool fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006467{
6468 struct io_ring_ctx *ctx = req->ctx;
6469 struct file *file;
6470
6471 if (fixed) {
Pavel Begunkov479f5172020-10-10 18:34:07 +01006472 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006473 return NULL;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006474 fd = array_index_nospec(fd, ctx->nr_user_files);
6475 file = io_file_from_index(ctx, fd);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00006476 io_set_resource_node(req);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006477 } else {
6478 trace_io_uring_file_get(ctx, fd);
6479 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006480 }
6481
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00006482 if (file && unlikely(file->f_op == &io_uring_fops))
6483 io_req_track_inflight(req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006484 return file;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006485}
6486
Jens Axboe2665abf2019-11-05 12:40:47 -07006487static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6488{
Jens Axboead8a48a2019-11-15 08:49:11 -07006489 struct io_timeout_data *data = container_of(timer,
6490 struct io_timeout_data, timer);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006491 struct io_kiocb *prev, *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006492 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07006493 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006494
6495 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006496 prev = req->timeout.head;
6497 req->timeout.head = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006498
6499 /*
6500 * We don't expect the list to be empty, that will only happen if we
6501 * race with the completion of the linked work.
6502 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006503 if (prev && refcount_inc_not_zero(&prev->refs))
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006504 io_remove_next_linked(prev);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006505 else
6506 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006507 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6508
6509 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006510 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03006511 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00006512 io_put_req_deferred(prev, 1);
Jens Axboe47f46762019-11-09 17:43:02 -07006513 } else {
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00006514 io_req_complete_post(req, -ETIME, 0);
6515 io_put_req_deferred(req, 1);
Jens Axboe2665abf2019-11-05 12:40:47 -07006516 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006517 return HRTIMER_NORESTART;
6518}
6519
Jens Axboe7271ef32020-08-10 09:55:22 -06006520static void __io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006521{
Jens Axboe76a46e02019-11-10 23:34:16 -07006522 /*
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006523 * If the back reference is NULL, then our linked request finished
6524 * before we got a chance to setup the timer
Jens Axboe76a46e02019-11-10 23:34:16 -07006525 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006526 if (req->timeout.head) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006527 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006528
Jens Axboead8a48a2019-11-15 08:49:11 -07006529 data->timer.function = io_link_timeout_fn;
6530 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6531 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006532 }
Jens Axboe7271ef32020-08-10 09:55:22 -06006533}
6534
6535static void io_queue_linked_timeout(struct io_kiocb *req)
6536{
6537 struct io_ring_ctx *ctx = req->ctx;
6538
6539 spin_lock_irq(&ctx->completion_lock);
6540 __io_queue_linked_timeout(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07006541 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006542
Jens Axboe2665abf2019-11-05 12:40:47 -07006543 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006544 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006545}
6546
Jens Axboead8a48a2019-11-15 08:49:11 -07006547static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006548{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006549 struct io_kiocb *nxt = req->link;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006550
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006551 if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) ||
6552 nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006553 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006554
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006555 nxt->timeout.head = req;
Pavel Begunkov900fad42020-10-19 16:39:16 +01006556 nxt->flags |= REQ_F_LTIMEOUT_ACTIVE;
Jens Axboe76a46e02019-11-10 23:34:16 -07006557 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006558 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006559}
6560
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006561static void __io_queue_sqe(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006562{
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006563 struct io_kiocb *linked_timeout = io_prep_linked_timeout(req);
Jens Axboe193155c2020-02-22 23:22:19 -07006564 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006565 int ret;
6566
Pavel Begunkov2e5aa6c2020-10-18 10:17:37 +01006567 if ((req->flags & REQ_F_WORK_INITIALIZED) &&
6568 (req->work.flags & IO_WQ_WORK_CREDS) &&
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006569 req->work.identity->creds != current_cred())
6570 old_creds = override_creds(req->work.identity->creds);
Jens Axboe193155c2020-02-22 23:22:19 -07006571
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006572 ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER);
Jens Axboe491381ce2019-10-17 09:20:46 -06006573
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006574 if (old_creds)
6575 revert_creds(old_creds);
Jens Axboe491381ce2019-10-17 09:20:46 -06006576
6577 /*
6578 * We async punt it if the file wasn't marked NOWAIT, or if the file
6579 * doesn't support non-blocking read/write attempts
6580 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03006581 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006582 if (!io_arm_poll_handler(req)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006583 /*
6584 * Queued up for async execution, worker will release
6585 * submit reference when the iocb is actually submitted.
6586 */
6587 io_queue_async_work(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006588 }
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006589 } else if (likely(!ret)) {
6590 /* drop submission reference */
Pavel Begunkove342c802021-01-19 13:32:47 +00006591 if (req->flags & REQ_F_COMPLETE_INLINE) {
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006592 struct io_ring_ctx *ctx = req->ctx;
6593 struct io_comp_state *cs = &ctx->submit_state.comp;
Jens Axboee65ef562019-03-12 10:16:44 -06006594
Pavel Begunkov6dd0be12021-02-10 00:03:13 +00006595 cs->reqs[cs->nr++] = req;
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006596 if (cs->nr == ARRAY_SIZE(cs->reqs))
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006597 io_submit_flush_completions(cs, ctx);
Pavel Begunkov9affd662021-01-19 13:32:46 +00006598 } else {
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006599 io_put_req(req);
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006600 }
6601 } else {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006602 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06006603 io_put_req(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006604 io_req_complete(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06006605 }
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006606 if (linked_timeout)
6607 io_queue_linked_timeout(linked_timeout);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006608}
6609
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006610static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006611{
6612 int ret;
6613
Jens Axboe3529d8c2019-12-19 18:24:38 -07006614 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006615 if (ret) {
6616 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006617fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006618 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006619 io_put_req(req);
6620 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006621 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006622 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006623 if (!req->async_data) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006624 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006625 if (unlikely(ret))
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006626 goto fail_req;
6627 }
Jens Axboece35a472019-12-17 08:04:44 -07006628 io_queue_async_work(req);
6629 } else {
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006630 if (sqe) {
6631 ret = io_req_prep(req, sqe);
6632 if (unlikely(ret))
6633 goto fail_req;
6634 }
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006635 __io_queue_sqe(req);
Jens Axboece35a472019-12-17 08:04:44 -07006636 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006637}
6638
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006639static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006640{
Jens Axboe94ae5e72019-11-14 19:39:52 -07006641 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Jens Axboee1e16092020-06-22 09:17:17 -06006642 io_put_req(req);
6643 io_req_complete(req, -ECANCELED);
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006644 } else
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006645 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006646}
6647
Pavel Begunkov863e0562020-10-27 23:25:35 +00006648struct io_submit_link {
6649 struct io_kiocb *head;
6650 struct io_kiocb *last;
6651};
6652
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006653static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006654 struct io_submit_link *link)
Jens Axboe9e645e112019-05-10 16:07:28 -06006655{
Jackie Liua197f662019-11-08 08:09:12 -07006656 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006657 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06006658
Jens Axboe9e645e112019-05-10 16:07:28 -06006659 /*
6660 * If we already have a head request, queue this one for async
6661 * submittal once the head completes. If we don't have a head but
6662 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6663 * submitted sync once the chain is complete. If none of those
6664 * conditions are true (normal request), then just queue it.
6665 */
Pavel Begunkov863e0562020-10-27 23:25:35 +00006666 if (link->head) {
6667 struct io_kiocb *head = link->head;
Jens Axboe9e645e112019-05-10 16:07:28 -06006668
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006669 /*
6670 * Taking sequential execution of a link, draining both sides
6671 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6672 * requests in the link. So, it drains the head and the
6673 * next after the link request. The last one is done via
6674 * drain_next flag to persist the effect across calls.
6675 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006676 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006677 head->flags |= REQ_F_IO_DRAIN;
6678 ctx->drain_next = 1;
6679 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07006680 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006681 if (unlikely(ret)) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006682 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03006683 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006684 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07006685 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03006686 trace_io_uring_link(ctx, req, head);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006687 link->last->link = req;
Pavel Begunkov863e0562020-10-27 23:25:35 +00006688 link->last = req;
Jens Axboe9e645e112019-05-10 16:07:28 -06006689
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006690 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006691 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006692 io_queue_link_head(head);
Pavel Begunkov863e0562020-10-27 23:25:35 +00006693 link->head = NULL;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006694 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006695 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03006696 if (unlikely(ctx->drain_next)) {
6697 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006698 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03006699 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006700 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006701 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006702 if (unlikely(ret))
Pavel Begunkov711be032020-01-17 03:57:59 +03006703 req->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov863e0562020-10-27 23:25:35 +00006704 link->head = req;
6705 link->last = req;
Pavel Begunkov711be032020-01-17 03:57:59 +03006706 } else {
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006707 io_queue_sqe(req, sqe);
Pavel Begunkov711be032020-01-17 03:57:59 +03006708 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006709 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006710
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006711 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06006712}
6713
Jens Axboe9a56a232019-01-09 09:06:50 -07006714/*
6715 * Batched submission is done, ensure local IO is flushed out.
6716 */
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006717static void io_submit_state_end(struct io_submit_state *state,
6718 struct io_ring_ctx *ctx)
Jens Axboe9a56a232019-01-09 09:06:50 -07006719{
Pavel Begunkov6dd0be12021-02-10 00:03:13 +00006720 if (state->comp.nr)
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006721 io_submit_flush_completions(&state->comp, ctx);
Jens Axboe27926b62020-10-28 09:33:23 -06006722 if (state->plug_started)
6723 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006724 io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07006725}
6726
6727/*
6728 * Start submission side cache.
6729 */
6730static void io_submit_state_start(struct io_submit_state *state,
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006731 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006732{
Jens Axboe27926b62020-10-28 09:33:23 -06006733 state->plug_started = false;
Jens Axboe9a56a232019-01-09 09:06:50 -07006734 state->ios_left = max_ios;
6735}
6736
Jens Axboe2b188cc2019-01-07 10:46:33 -07006737static void io_commit_sqring(struct io_ring_ctx *ctx)
6738{
Hristo Venev75b28af2019-08-26 17:23:46 +00006739 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006740
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006741 /*
6742 * Ensure any loads from the SQEs are done at this point,
6743 * since once we write the new head, the application could
6744 * write new data to them.
6745 */
6746 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006747}
6748
6749/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006750 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006751 * that is mapped by userspace. This means that care needs to be taken to
6752 * ensure that reads are stable, as we cannot rely on userspace always
6753 * being a good citizen. If members of the sqe are validated and then later
6754 * used, it's important that those reads are done through READ_ONCE() to
6755 * prevent a re-load down the line.
6756 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006757static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006758{
Hristo Venev75b28af2019-08-26 17:23:46 +00006759 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006760 unsigned head;
6761
6762 /*
6763 * The cached sq head (or cq tail) serves two purposes:
6764 *
6765 * 1) allows us to batch the cost of updating the user visible
6766 * head updates.
6767 * 2) allows the kernel side to track the head on its own, even
6768 * though the application is the one updating it.
6769 */
Pavel Begunkov4fccfcb2021-02-12 11:55:17 +00006770 head = READ_ONCE(sq_array[ctx->cached_sq_head++ & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006771 if (likely(head < ctx->sq_entries))
6772 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006773
6774 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006775 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006776 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006777 return NULL;
6778}
6779
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006780/*
6781 * Check SQE restrictions (opcode and flags).
6782 *
6783 * Returns 'true' if SQE is allowed, 'false' otherwise.
6784 */
6785static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6786 struct io_kiocb *req,
6787 unsigned int sqe_flags)
6788{
6789 if (!ctx->restricted)
6790 return true;
6791
6792 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6793 return false;
6794
6795 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6796 ctx->restrictions.sqe_flags_required)
6797 return false;
6798
6799 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6800 ctx->restrictions.sqe_flags_required))
6801 return false;
6802
6803 return true;
6804}
6805
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006806#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6807 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6808 IOSQE_BUFFER_SELECT)
6809
6810static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006811 const struct io_uring_sqe *sqe)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006812{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006813 struct io_submit_state *state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006814 unsigned int sqe_flags;
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006815 int id, ret = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006816
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006817 req->opcode = READ_ONCE(sqe->opcode);
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006818 /* same numerical values with corresponding REQ_F_*, safe to copy */
6819 req->flags = sqe_flags = READ_ONCE(sqe->flags);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006820 req->user_data = READ_ONCE(sqe->user_data);
Jens Axboee8c2bc12020-08-15 18:44:09 -07006821 req->async_data = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006822 req->file = NULL;
6823 req->ctx = ctx;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006824 req->link = NULL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006825 req->fixed_rsrc_refs = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006826 /* one is dropped after submission, the other at completion */
6827 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006828 req->task = current;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006829 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006830
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006831 /* enforce forwards compatibility on users */
6832 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6833 return -EINVAL;
6834
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006835 if (unlikely(req->opcode >= IORING_OP_LAST))
6836 return -EINVAL;
6837
Jens Axboe28cea78a2020-09-14 10:51:17 -06006838 if (unlikely(io_sq_thread_acquire_mm_files(ctx, req)))
Jens Axboe9d8426a2020-06-16 18:42:49 -06006839 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006840
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006841 if (unlikely(!io_check_restriction(ctx, req, sqe_flags)))
6842 return -EACCES;
6843
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006844 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6845 !io_op_defs[req->opcode].buffer_select)
6846 return -EOPNOTSUPP;
6847
6848 id = READ_ONCE(sqe->personality);
6849 if (id) {
Jens Axboe1e6fa522020-10-15 08:46:24 -06006850 struct io_identity *iod;
6851
Jens Axboe1e6fa522020-10-15 08:46:24 -06006852 iod = idr_find(&ctx->personality_idr, id);
6853 if (unlikely(!iod))
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006854 return -EINVAL;
Jens Axboe1e6fa522020-10-15 08:46:24 -06006855 refcount_inc(&iod->count);
Pavel Begunkovec99ca62020-10-18 10:17:38 +01006856
6857 __io_req_init_async(req);
Jens Axboe1e6fa522020-10-15 08:46:24 -06006858 get_cred(iod->creds);
6859 req->work.identity = iod;
Jens Axboedfead8a2020-10-14 10:12:37 -06006860 req->work.flags |= IO_WQ_WORK_CREDS;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006861 }
6862
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006863 state = &ctx->submit_state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006864
Jens Axboe27926b62020-10-28 09:33:23 -06006865 /*
6866 * Plug now if we have more than 1 IO left after this, and the target
6867 * is potentially a read/write to block based storage.
6868 */
6869 if (!state->plug_started && state->ios_left > 1 &&
6870 io_op_defs[req->opcode].plug) {
6871 blk_start_plug(&state->plug);
6872 state->plug_started = true;
6873 }
Jens Axboe63ff8222020-05-07 14:56:15 -06006874
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006875 if (io_op_defs[req->opcode].needs_file) {
6876 bool fixed = req->flags & REQ_F_FIXED_FILE;
Jens Axboe63ff8222020-05-07 14:56:15 -06006877
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006878 req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed);
Pavel Begunkovba13e232021-02-01 18:59:52 +00006879 if (unlikely(!req->file))
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006880 ret = -EBADF;
6881 }
6882
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006883 state->ios_left--;
6884 return ret;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006885}
6886
Jens Axboe0f212202020-09-13 13:09:39 -06006887static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006888{
Pavel Begunkov863e0562020-10-27 23:25:35 +00006889 struct io_submit_link link;
Jens Axboe9e645e112019-05-10 16:07:28 -06006890 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006891
Jens Axboec4a2ed72019-11-21 21:01:26 -07006892 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006893 if (test_bit(0, &ctx->sq_check_overflow)) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00006894 if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL))
Jens Axboead3eb2c2019-12-18 17:12:20 -07006895 return -EBUSY;
6896 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006897
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006898 /* make sure SQ entry isn't read before tail */
6899 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006900
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006901 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6902 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006903
Jens Axboed8a6df12020-10-15 16:24:45 -06006904 percpu_counter_add(&current->io_uring->inflight, nr);
Jens Axboefaf7b512020-10-07 12:48:53 -06006905 refcount_add(nr, &current->usage);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006906
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006907 io_submit_state_start(&ctx->submit_state, nr);
Pavel Begunkov863e0562020-10-27 23:25:35 +00006908 link.head = NULL;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006909
Jens Axboe6c271ce2019-01-10 11:22:30 -07006910 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006911 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006912 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006913 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006914
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006915 req = io_alloc_req(ctx);
Pavel Begunkov196be952019-11-07 01:41:06 +03006916 if (unlikely(!req)) {
6917 if (!submitted)
6918 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006919 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006920 }
Pavel Begunkov4fccfcb2021-02-12 11:55:17 +00006921 sqe = io_get_sqe(ctx);
6922 if (unlikely(!sqe)) {
6923 kmem_cache_free(req_cachep, req);
6924 break;
6925 }
Jens Axboed3656342019-12-18 09:50:26 -07006926 /* will complete beyond this point, count as submitted */
6927 submitted++;
6928
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006929 err = io_init_req(ctx, req, sqe);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006930 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006931fail_req:
Jens Axboee1e16092020-06-22 09:17:17 -06006932 io_put_req(req);
6933 io_req_complete(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006934 break;
6935 }
6936
Jens Axboe354420f2020-01-08 18:55:15 -07006937 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov2d7e9352021-01-19 13:32:37 +00006938 true, ctx->flags & IORING_SETUP_SQPOLL);
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006939 err = io_submit_sqe(req, sqe, &link);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006940 if (err)
6941 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006942 }
6943
Pavel Begunkov9466f432020-01-25 22:34:01 +03006944 if (unlikely(submitted != nr)) {
6945 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
Jens Axboed8a6df12020-10-15 16:24:45 -06006946 struct io_uring_task *tctx = current->io_uring;
6947 int unused = nr - ref_used;
Pavel Begunkov9466f432020-01-25 22:34:01 +03006948
Jens Axboed8a6df12020-10-15 16:24:45 -06006949 percpu_ref_put_many(&ctx->refs, unused);
6950 percpu_counter_sub(&tctx->inflight, unused);
6951 put_task_struct_many(current, unused);
Pavel Begunkov9466f432020-01-25 22:34:01 +03006952 }
Pavel Begunkov863e0562020-10-27 23:25:35 +00006953 if (link.head)
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006954 io_queue_link_head(link.head);
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006955 io_submit_state_end(&ctx->submit_state, ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006956
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006957 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6958 io_commit_sqring(ctx);
6959
Jens Axboe6c271ce2019-01-10 11:22:30 -07006960 return submitted;
6961}
6962
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006963static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6964{
6965 /* Tell userspace we may need a wakeup call */
6966 spin_lock_irq(&ctx->completion_lock);
6967 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6968 spin_unlock_irq(&ctx->completion_lock);
6969}
6970
6971static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6972{
6973 spin_lock_irq(&ctx->completion_lock);
6974 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6975 spin_unlock_irq(&ctx->completion_lock);
6976}
6977
Xiaoguang Wang08369242020-11-03 14:15:59 +08006978static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006979{
Jens Axboec8d1ba52020-09-14 11:07:26 -06006980 unsigned int to_submit;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006981 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006982
Jens Axboec8d1ba52020-09-14 11:07:26 -06006983 to_submit = io_sqring_entries(ctx);
Jens Axboee95eee22020-09-08 09:11:32 -06006984 /* if we're handling multiple rings, cap submit size for fairness */
6985 if (cap_entries && to_submit > 8)
6986 to_submit = 8;
6987
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006988 if (!list_empty(&ctx->iopoll_list) || to_submit) {
6989 unsigned nr_events = 0;
6990
Xiaoguang Wang08369242020-11-03 14:15:59 +08006991 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006992 if (!list_empty(&ctx->iopoll_list))
6993 io_do_iopoll(ctx, &nr_events, 0);
6994
Pavel Begunkovd9d05212021-01-08 20:57:25 +00006995 if (to_submit && !ctx->sqo_dead &&
6996 likely(!percpu_ref_is_dying(&ctx->refs)))
Xiaoguang Wang08369242020-11-03 14:15:59 +08006997 ret = io_submit_sqes(ctx, to_submit);
6998 mutex_unlock(&ctx->uring_lock);
6999 }
Jens Axboe90554202020-09-03 12:12:41 -06007000
7001 if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait))
7002 wake_up(&ctx->sqo_sq_wait);
7003
Xiaoguang Wang08369242020-11-03 14:15:59 +08007004 return ret;
7005}
7006
7007static void io_sqd_update_thread_idle(struct io_sq_data *sqd)
7008{
7009 struct io_ring_ctx *ctx;
7010 unsigned sq_thread_idle = 0;
7011
7012 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
7013 if (sq_thread_idle < ctx->sq_thread_idle)
7014 sq_thread_idle = ctx->sq_thread_idle;
7015 }
7016
7017 sqd->sq_thread_idle = sq_thread_idle;
Jens Axboec8d1ba52020-09-14 11:07:26 -06007018}
7019
Jens Axboe69fb2132020-09-14 11:16:23 -06007020static void io_sqd_init_new(struct io_sq_data *sqd)
7021{
7022 struct io_ring_ctx *ctx;
7023
7024 while (!list_empty(&sqd->ctx_new_list)) {
7025 ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06007026 list_move_tail(&ctx->sqd_list, &sqd->ctx_list);
7027 complete(&ctx->sq_thread_comp);
7028 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08007029
7030 io_sqd_update_thread_idle(sqd);
Jens Axboe69fb2132020-09-14 11:16:23 -06007031}
7032
Jens Axboe6c271ce2019-01-10 11:22:30 -07007033static int io_sq_thread(void *data)
7034{
Dennis Zhou91d8f512020-09-16 13:41:05 -07007035 struct cgroup_subsys_state *cur_css = NULL;
Jens Axboe28cea78a2020-09-14 10:51:17 -06007036 struct files_struct *old_files = current->files;
7037 struct nsproxy *old_nsproxy = current->nsproxy;
Jens Axboe69fb2132020-09-14 11:16:23 -06007038 const struct cred *old_cred = NULL;
7039 struct io_sq_data *sqd = data;
7040 struct io_ring_ctx *ctx;
Xiaoguang Wanga0d92052020-11-12 14:55:59 +08007041 unsigned long timeout = 0;
Xiaoguang Wang08369242020-11-03 14:15:59 +08007042 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007043
Jens Axboe28cea78a2020-09-14 10:51:17 -06007044 task_lock(current);
7045 current->files = NULL;
7046 current->nsproxy = NULL;
7047 task_unlock(current);
7048
Jens Axboe69fb2132020-09-14 11:16:23 -06007049 while (!kthread_should_stop()) {
Xiaoguang Wang08369242020-11-03 14:15:59 +08007050 int ret;
7051 bool cap_entries, sqt_spin, needs_sched;
Jens Axboec1edbf52019-11-10 16:56:04 -07007052
7053 /*
Jens Axboe69fb2132020-09-14 11:16:23 -06007054 * Any changes to the sqd lists are synchronized through the
7055 * kthread parking. This synchronizes the thread vs users,
7056 * the users are synchronized on the sqd->ctx_lock.
Jens Axboec1edbf52019-11-10 16:56:04 -07007057 */
Xiaoguang Wang65b2b212020-11-19 17:44:46 +08007058 if (kthread_should_park()) {
Jens Axboe69fb2132020-09-14 11:16:23 -06007059 kthread_parkme();
Xiaoguang Wang65b2b212020-11-19 17:44:46 +08007060 /*
7061 * When sq thread is unparked, in case the previous park operation
7062 * comes from io_put_sq_data(), which means that sq thread is going
7063 * to be stopped, so here needs to have a check.
7064 */
7065 if (kthread_should_stop())
7066 break;
7067 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007068
Xiaoguang Wang08369242020-11-03 14:15:59 +08007069 if (unlikely(!list_empty(&sqd->ctx_new_list))) {
Jens Axboe69fb2132020-09-14 11:16:23 -06007070 io_sqd_init_new(sqd);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007071 timeout = jiffies + sqd->sq_thread_idle;
7072 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007073
Xiaoguang Wang08369242020-11-03 14:15:59 +08007074 sqt_spin = false;
Jens Axboee95eee22020-09-08 09:11:32 -06007075 cap_entries = !list_is_singular(&sqd->ctx_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06007076 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
7077 if (current->cred != ctx->creds) {
7078 if (old_cred)
7079 revert_creds(old_cred);
7080 old_cred = override_creds(ctx->creds);
7081 }
Dennis Zhou91d8f512020-09-16 13:41:05 -07007082 io_sq_thread_associate_blkcg(ctx, &cur_css);
Jens Axboe4ea33a92020-10-15 13:46:44 -06007083#ifdef CONFIG_AUDIT
7084 current->loginuid = ctx->loginuid;
7085 current->sessionid = ctx->sessionid;
7086#endif
Jens Axboe69fb2132020-09-14 11:16:23 -06007087
Xiaoguang Wang08369242020-11-03 14:15:59 +08007088 ret = __io_sq_thread(ctx, cap_entries);
7089 if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list)))
7090 sqt_spin = true;
Jens Axboe69fb2132020-09-14 11:16:23 -06007091
Jens Axboe28cea78a2020-09-14 10:51:17 -06007092 io_sq_thread_drop_mm_files();
Jens Axboe6c271ce2019-01-10 11:22:30 -07007093 }
7094
Xiaoguang Wang08369242020-11-03 14:15:59 +08007095 if (sqt_spin || !time_after(jiffies, timeout)) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06007096 io_run_task_work();
Pavel Begunkovd434ab62021-01-11 04:00:30 +00007097 io_sq_thread_drop_mm_files();
Jens Axboec8d1ba52020-09-14 11:07:26 -06007098 cond_resched();
Xiaoguang Wang08369242020-11-03 14:15:59 +08007099 if (sqt_spin)
7100 timeout = jiffies + sqd->sq_thread_idle;
7101 continue;
7102 }
7103
Xiaoguang Wang08369242020-11-03 14:15:59 +08007104 needs_sched = true;
7105 prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE);
7106 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
7107 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
7108 !list_empty_careful(&ctx->iopoll_list)) {
7109 needs_sched = false;
7110 break;
7111 }
7112 if (io_sqring_entries(ctx)) {
7113 needs_sched = false;
7114 break;
7115 }
7116 }
7117
Hao Xu8b28fdf2021-01-31 22:39:04 +08007118 if (needs_sched && !kthread_should_park()) {
Jens Axboe69fb2132020-09-14 11:16:23 -06007119 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7120 io_ring_set_wakeup_flag(ctx);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007121
Jens Axboe69fb2132020-09-14 11:16:23 -06007122 schedule();
Jens Axboe69fb2132020-09-14 11:16:23 -06007123 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7124 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007125 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08007126
7127 finish_wait(&sqd->wait, &wait);
7128 timeout = jiffies + sqd->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007129 }
7130
Jens Axboe4c6e2772020-07-01 11:29:10 -06007131 io_run_task_work();
Pavel Begunkovd434ab62021-01-11 04:00:30 +00007132 io_sq_thread_drop_mm_files();
Jens Axboeb41e9852020-02-17 09:52:41 -07007133
Dennis Zhou91d8f512020-09-16 13:41:05 -07007134 if (cur_css)
7135 io_sq_thread_unassociate_blkcg();
Jens Axboe69fb2132020-09-14 11:16:23 -06007136 if (old_cred)
7137 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06007138
Jens Axboe28cea78a2020-09-14 10:51:17 -06007139 task_lock(current);
7140 current->files = old_files;
7141 current->nsproxy = old_nsproxy;
7142 task_unlock(current);
7143
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02007144 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06007145
Jens Axboe6c271ce2019-01-10 11:22:30 -07007146 return 0;
7147}
7148
Jens Axboebda52162019-09-24 13:47:15 -06007149struct io_wait_queue {
7150 struct wait_queue_entry wq;
7151 struct io_ring_ctx *ctx;
7152 unsigned to_wait;
7153 unsigned nr_timeouts;
7154};
7155
Pavel Begunkov6c503152021-01-04 20:36:36 +00007156static inline bool io_should_wake(struct io_wait_queue *iowq)
Jens Axboebda52162019-09-24 13:47:15 -06007157{
7158 struct io_ring_ctx *ctx = iowq->ctx;
7159
7160 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08007161 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06007162 * started waiting. For timeouts, we always want to return to userspace,
7163 * regardless of event count.
7164 */
Pavel Begunkov6c503152021-01-04 20:36:36 +00007165 return io_cqring_events(ctx) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06007166 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
7167}
7168
7169static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
7170 int wake_flags, void *key)
7171{
7172 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
7173 wq);
7174
Pavel Begunkov6c503152021-01-04 20:36:36 +00007175 /*
7176 * Cannot safely flush overflowed CQEs from here, ensure we wake up
7177 * the task, and the next invocation will do it.
7178 */
7179 if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow))
7180 return autoremove_wake_function(curr, mode, wake_flags, key);
7181 return -1;
Jens Axboebda52162019-09-24 13:47:15 -06007182}
7183
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007184static int io_run_task_work_sig(void)
7185{
7186 if (io_run_task_work())
7187 return 1;
7188 if (!signal_pending(current))
7189 return 0;
Jens Axboe792ee0f62020-10-22 20:17:18 -06007190 if (test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL))
7191 return -ERESTARTSYS;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007192 return -EINTR;
7193}
7194
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007195/* when returns >0, the caller should retry */
7196static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,
7197 struct io_wait_queue *iowq,
7198 signed long *timeout)
7199{
7200 int ret;
7201
7202 /* make sure we run task_work before checking for signals */
7203 ret = io_run_task_work_sig();
7204 if (ret || io_should_wake(iowq))
7205 return ret;
7206 /* let the caller flush overflows, retry */
7207 if (test_bit(0, &ctx->cq_check_overflow))
7208 return 1;
7209
7210 *timeout = schedule_timeout(*timeout);
7211 return !*timeout ? -ETIME : 1;
7212}
7213
Jens Axboe2b188cc2019-01-07 10:46:33 -07007214/*
7215 * Wait until events become available, if we don't already have some. The
7216 * application must reap them itself, as they reside on the shared cq ring.
7217 */
7218static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
Hao Xuc73ebb62020-11-03 10:54:37 +08007219 const sigset_t __user *sig, size_t sigsz,
7220 struct __kernel_timespec __user *uts)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007221{
Jens Axboebda52162019-09-24 13:47:15 -06007222 struct io_wait_queue iowq = {
7223 .wq = {
7224 .private = current,
7225 .func = io_wake_function,
7226 .entry = LIST_HEAD_INIT(iowq.wq.entry),
7227 },
7228 .ctx = ctx,
7229 .to_wait = min_events,
7230 };
Hristo Venev75b28af2019-08-26 17:23:46 +00007231 struct io_rings *rings = ctx->rings;
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007232 signed long timeout = MAX_SCHEDULE_TIMEOUT;
7233 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007234
Jens Axboeb41e9852020-02-17 09:52:41 -07007235 do {
Pavel Begunkov6c503152021-01-04 20:36:36 +00007236 io_cqring_overflow_flush(ctx, false, NULL, NULL);
7237 if (io_cqring_events(ctx) >= min_events)
Jens Axboeb41e9852020-02-17 09:52:41 -07007238 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06007239 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07007240 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07007241 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007242
7243 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007244#ifdef CONFIG_COMPAT
7245 if (in_compat_syscall())
7246 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07007247 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007248 else
7249#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07007250 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007251
Jens Axboe2b188cc2019-01-07 10:46:33 -07007252 if (ret)
7253 return ret;
7254 }
7255
Hao Xuc73ebb62020-11-03 10:54:37 +08007256 if (uts) {
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007257 struct timespec64 ts;
7258
Hao Xuc73ebb62020-11-03 10:54:37 +08007259 if (get_timespec64(&ts, uts))
7260 return -EFAULT;
7261 timeout = timespec64_to_jiffies(&ts);
7262 }
7263
Jens Axboebda52162019-09-24 13:47:15 -06007264 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007265 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06007266 do {
Pavel Begunkov6c503152021-01-04 20:36:36 +00007267 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboebda52162019-09-24 13:47:15 -06007268 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
7269 TASK_INTERRUPTIBLE);
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007270 ret = io_cqring_wait_schedule(ctx, &iowq, &timeout);
7271 finish_wait(&ctx->wait, &iowq.wq);
7272 } while (ret > 0);
Jens Axboebda52162019-09-24 13:47:15 -06007273
Jens Axboeb7db41c2020-07-04 08:55:50 -06007274 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007275
Hristo Venev75b28af2019-08-26 17:23:46 +00007276 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007277}
7278
Jens Axboe6b063142019-01-10 22:13:58 -07007279static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
7280{
7281#if defined(CONFIG_UNIX)
7282 if (ctx->ring_sock) {
7283 struct sock *sock = ctx->ring_sock->sk;
7284 struct sk_buff *skb;
7285
7286 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
7287 kfree_skb(skb);
7288 }
7289#else
7290 int i;
7291
Jens Axboe65e19f52019-10-26 07:20:21 -06007292 for (i = 0; i < ctx->nr_user_files; i++) {
7293 struct file *file;
7294
7295 file = io_file_from_index(ctx, i);
7296 if (file)
7297 fput(file);
7298 }
Jens Axboe6b063142019-01-10 22:13:58 -07007299#endif
7300}
7301
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007302static void io_rsrc_data_ref_zero(struct percpu_ref *ref)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007303{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007304 struct fixed_rsrc_data *data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007305
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007306 data = container_of(ref, struct fixed_rsrc_data, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007307 complete(&data->done);
7308}
7309
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007310static inline void io_rsrc_ref_lock(struct io_ring_ctx *ctx)
Pavel Begunkov1642b442020-12-30 21:34:14 +00007311{
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007312 spin_lock_bh(&ctx->rsrc_ref_lock);
Pavel Begunkov1642b442020-12-30 21:34:14 +00007313}
7314
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007315static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx)
Jens Axboe6b063142019-01-10 22:13:58 -07007316{
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007317 spin_unlock_bh(&ctx->rsrc_ref_lock);
7318}
7319
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007320static void io_sqe_rsrc_set_node(struct io_ring_ctx *ctx,
7321 struct fixed_rsrc_data *rsrc_data,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007322 struct fixed_rsrc_ref_node *ref_node)
Jens Axboe6b063142019-01-10 22:13:58 -07007323{
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007324 io_rsrc_ref_lock(ctx);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007325 rsrc_data->node = ref_node;
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007326 list_add_tail(&ref_node->node, &ctx->rsrc_ref_list);
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007327 io_rsrc_ref_unlock(ctx);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007328 percpu_ref_get(&rsrc_data->refs);
Jens Axboe6b063142019-01-10 22:13:58 -07007329}
7330
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007331static int io_rsrc_ref_quiesce(struct fixed_rsrc_data *data,
7332 struct io_ring_ctx *ctx,
7333 struct fixed_rsrc_ref_node *backup_node)
Jens Axboe6b063142019-01-10 22:13:58 -07007334{
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007335 struct fixed_rsrc_ref_node *ref_node;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007336 int ret;
Jens Axboe65e19f52019-10-26 07:20:21 -06007337
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007338 io_rsrc_ref_lock(ctx);
Pavel Begunkov1e5d7702020-11-18 14:56:25 +00007339 ref_node = data->node;
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007340 io_rsrc_ref_unlock(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007341 if (ref_node)
7342 percpu_ref_kill(&ref_node->refs);
7343
7344 percpu_ref_kill(&data->refs);
7345
7346 /* wait for all refs nodes to complete */
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007347 flush_delayed_work(&ctx->rsrc_put_work);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007348 do {
7349 ret = wait_for_completion_interruptible(&data->done);
7350 if (!ret)
7351 break;
7352 ret = io_run_task_work_sig();
7353 if (ret < 0) {
7354 percpu_ref_resurrect(&data->refs);
7355 reinit_completion(&data->done);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007356 io_sqe_rsrc_set_node(ctx, data, backup_node);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007357 return ret;
7358 }
7359 } while (1);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007360
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007361 destroy_fixed_rsrc_ref_node(backup_node);
7362 return 0;
7363}
7364
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007365static struct fixed_rsrc_data *alloc_fixed_rsrc_data(struct io_ring_ctx *ctx)
7366{
7367 struct fixed_rsrc_data *data;
7368
7369 data = kzalloc(sizeof(*data), GFP_KERNEL);
7370 if (!data)
7371 return NULL;
7372
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007373 if (percpu_ref_init(&data->refs, io_rsrc_data_ref_zero,
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007374 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
7375 kfree(data);
7376 return NULL;
7377 }
7378 data->ctx = ctx;
7379 init_completion(&data->done);
7380 return data;
7381}
7382
7383static void free_fixed_rsrc_data(struct fixed_rsrc_data *data)
7384{
7385 percpu_ref_exit(&data->refs);
7386 kfree(data->table);
7387 kfree(data);
7388}
7389
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007390static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
7391{
7392 struct fixed_rsrc_data *data = ctx->file_data;
7393 struct fixed_rsrc_ref_node *backup_node;
7394 unsigned nr_tables, i;
7395 int ret;
7396
7397 if (!data)
7398 return -ENXIO;
7399 backup_node = alloc_fixed_rsrc_ref_node(ctx);
7400 if (!backup_node)
7401 return -ENOMEM;
7402 init_fixed_file_ref_node(ctx, backup_node);
7403
7404 ret = io_rsrc_ref_quiesce(data, ctx, backup_node);
7405 if (ret)
7406 return ret;
7407
Jens Axboe6b063142019-01-10 22:13:58 -07007408 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06007409 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
7410 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007411 kfree(data->table[i].files);
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007412 free_fixed_rsrc_data(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007413 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007414 ctx->nr_user_files = 0;
7415 return 0;
7416}
7417
Jens Axboe534ca6d2020-09-02 13:52:19 -06007418static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007419{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007420 if (refcount_dec_and_test(&sqd->refs)) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02007421 /*
7422 * The park is a bit of a work-around, without it we get
7423 * warning spews on shutdown with SQPOLL set and affinity
7424 * set to a single CPU.
7425 */
Jens Axboe534ca6d2020-09-02 13:52:19 -06007426 if (sqd->thread) {
7427 kthread_park(sqd->thread);
7428 kthread_stop(sqd->thread);
7429 }
7430
7431 kfree(sqd);
7432 }
7433}
7434
Jens Axboeaa061652020-09-02 14:50:27 -06007435static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7436{
7437 struct io_ring_ctx *ctx_attach;
7438 struct io_sq_data *sqd;
7439 struct fd f;
7440
7441 f = fdget(p->wq_fd);
7442 if (!f.file)
7443 return ERR_PTR(-ENXIO);
7444 if (f.file->f_op != &io_uring_fops) {
7445 fdput(f);
7446 return ERR_PTR(-EINVAL);
7447 }
7448
7449 ctx_attach = f.file->private_data;
7450 sqd = ctx_attach->sq_data;
7451 if (!sqd) {
7452 fdput(f);
7453 return ERR_PTR(-EINVAL);
7454 }
7455
7456 refcount_inc(&sqd->refs);
7457 fdput(f);
7458 return sqd;
7459}
7460
Jens Axboe534ca6d2020-09-02 13:52:19 -06007461static struct io_sq_data *io_get_sq_data(struct io_uring_params *p)
7462{
7463 struct io_sq_data *sqd;
7464
Jens Axboeaa061652020-09-02 14:50:27 -06007465 if (p->flags & IORING_SETUP_ATTACH_WQ)
7466 return io_attach_sq_data(p);
7467
Jens Axboe534ca6d2020-09-02 13:52:19 -06007468 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
7469 if (!sqd)
7470 return ERR_PTR(-ENOMEM);
7471
7472 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06007473 INIT_LIST_HEAD(&sqd->ctx_list);
7474 INIT_LIST_HEAD(&sqd->ctx_new_list);
7475 mutex_init(&sqd->ctx_lock);
7476 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007477 init_waitqueue_head(&sqd->wait);
7478 return sqd;
7479}
7480
Jens Axboe69fb2132020-09-14 11:16:23 -06007481static void io_sq_thread_unpark(struct io_sq_data *sqd)
7482 __releases(&sqd->lock)
7483{
7484 if (!sqd->thread)
7485 return;
7486 kthread_unpark(sqd->thread);
7487 mutex_unlock(&sqd->lock);
7488}
7489
7490static void io_sq_thread_park(struct io_sq_data *sqd)
7491 __acquires(&sqd->lock)
7492{
7493 if (!sqd->thread)
7494 return;
7495 mutex_lock(&sqd->lock);
7496 kthread_park(sqd->thread);
7497}
7498
Jens Axboe534ca6d2020-09-02 13:52:19 -06007499static void io_sq_thread_stop(struct io_ring_ctx *ctx)
7500{
7501 struct io_sq_data *sqd = ctx->sq_data;
7502
7503 if (sqd) {
7504 if (sqd->thread) {
7505 /*
7506 * We may arrive here from the error branch in
7507 * io_sq_offload_create() where the kthread is created
7508 * without being waked up, thus wake it up now to make
7509 * sure the wait will complete.
7510 */
7511 wake_up_process(sqd->thread);
7512 wait_for_completion(&ctx->sq_thread_comp);
Jens Axboe69fb2132020-09-14 11:16:23 -06007513
7514 io_sq_thread_park(sqd);
7515 }
7516
7517 mutex_lock(&sqd->ctx_lock);
7518 list_del(&ctx->sqd_list);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007519 io_sqd_update_thread_idle(sqd);
Jens Axboe69fb2132020-09-14 11:16:23 -06007520 mutex_unlock(&sqd->ctx_lock);
7521
Xiaoguang Wang08369242020-11-03 14:15:59 +08007522 if (sqd->thread)
Jens Axboe69fb2132020-09-14 11:16:23 -06007523 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007524
7525 io_put_sq_data(sqd);
7526 ctx->sq_data = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007527 }
7528}
7529
Jens Axboe6b063142019-01-10 22:13:58 -07007530static void io_finish_async(struct io_ring_ctx *ctx)
7531{
Jens Axboe6c271ce2019-01-10 11:22:30 -07007532 io_sq_thread_stop(ctx);
7533
Jens Axboe561fb042019-10-24 07:25:42 -06007534 if (ctx->io_wq) {
7535 io_wq_destroy(ctx->io_wq);
7536 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007537 }
7538}
7539
7540#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007541/*
7542 * Ensure the UNIX gc is aware of our file set, so we are certain that
7543 * the io_uring can be safely unregistered on process exit, even if we have
7544 * loops in the file referencing.
7545 */
7546static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7547{
7548 struct sock *sk = ctx->ring_sock->sk;
7549 struct scm_fp_list *fpl;
7550 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007551 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007552
Jens Axboe6b063142019-01-10 22:13:58 -07007553 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7554 if (!fpl)
7555 return -ENOMEM;
7556
7557 skb = alloc_skb(0, GFP_KERNEL);
7558 if (!skb) {
7559 kfree(fpl);
7560 return -ENOMEM;
7561 }
7562
7563 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07007564
Jens Axboe08a45172019-10-03 08:11:03 -06007565 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07007566 fpl->user = get_uid(ctx->user);
7567 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007568 struct file *file = io_file_from_index(ctx, i + offset);
7569
7570 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007571 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007572 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007573 unix_inflight(fpl->user, fpl->fp[nr_files]);
7574 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007575 }
7576
Jens Axboe08a45172019-10-03 08:11:03 -06007577 if (nr_files) {
7578 fpl->max = SCM_MAX_FD;
7579 fpl->count = nr_files;
7580 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007581 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007582 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7583 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007584
Jens Axboe08a45172019-10-03 08:11:03 -06007585 for (i = 0; i < nr_files; i++)
7586 fput(fpl->fp[i]);
7587 } else {
7588 kfree_skb(skb);
7589 kfree(fpl);
7590 }
Jens Axboe6b063142019-01-10 22:13:58 -07007591
7592 return 0;
7593}
7594
7595/*
7596 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7597 * causes regular reference counting to break down. We rely on the UNIX
7598 * garbage collection to take care of this problem for us.
7599 */
7600static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7601{
7602 unsigned left, total;
7603 int ret = 0;
7604
7605 total = 0;
7606 left = ctx->nr_user_files;
7607 while (left) {
7608 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07007609
7610 ret = __io_sqe_files_scm(ctx, this_files, total);
7611 if (ret)
7612 break;
7613 left -= this_files;
7614 total += this_files;
7615 }
7616
7617 if (!ret)
7618 return 0;
7619
7620 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007621 struct file *file = io_file_from_index(ctx, total);
7622
7623 if (file)
7624 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07007625 total++;
7626 }
7627
7628 return ret;
7629}
7630#else
7631static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7632{
7633 return 0;
7634}
7635#endif
7636
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007637static int io_sqe_alloc_file_tables(struct fixed_rsrc_data *file_data,
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007638 unsigned nr_tables, unsigned nr_files)
Jens Axboe65e19f52019-10-26 07:20:21 -06007639{
7640 int i;
7641
7642 for (i = 0; i < nr_tables; i++) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007643 struct fixed_rsrc_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007644 unsigned this_files;
7645
7646 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
7647 table->files = kcalloc(this_files, sizeof(struct file *),
7648 GFP_KERNEL);
7649 if (!table->files)
7650 break;
7651 nr_files -= this_files;
7652 }
7653
7654 if (i == nr_tables)
7655 return 0;
7656
7657 for (i = 0; i < nr_tables; i++) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007658 struct fixed_rsrc_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007659 kfree(table->files);
7660 }
7661 return 1;
7662}
7663
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007664static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
Jens Axboec3a31e62019-10-03 13:59:56 -06007665{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007666 struct file *file = prsrc->file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007667#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007668 struct sock *sock = ctx->ring_sock->sk;
7669 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7670 struct sk_buff *skb;
7671 int i;
7672
7673 __skb_queue_head_init(&list);
7674
7675 /*
7676 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7677 * remove this entry and rearrange the file array.
7678 */
7679 skb = skb_dequeue(head);
7680 while (skb) {
7681 struct scm_fp_list *fp;
7682
7683 fp = UNIXCB(skb).fp;
7684 for (i = 0; i < fp->count; i++) {
7685 int left;
7686
7687 if (fp->fp[i] != file)
7688 continue;
7689
7690 unix_notinflight(fp->user, fp->fp[i]);
7691 left = fp->count - 1 - i;
7692 if (left) {
7693 memmove(&fp->fp[i], &fp->fp[i + 1],
7694 left * sizeof(struct file *));
7695 }
7696 fp->count--;
7697 if (!fp->count) {
7698 kfree_skb(skb);
7699 skb = NULL;
7700 } else {
7701 __skb_queue_tail(&list, skb);
7702 }
7703 fput(file);
7704 file = NULL;
7705 break;
7706 }
7707
7708 if (!file)
7709 break;
7710
7711 __skb_queue_tail(&list, skb);
7712
7713 skb = skb_dequeue(head);
7714 }
7715
7716 if (skb_peek(&list)) {
7717 spin_lock_irq(&head->lock);
7718 while ((skb = __skb_dequeue(&list)) != NULL)
7719 __skb_queue_tail(head, skb);
7720 spin_unlock_irq(&head->lock);
7721 }
7722#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007723 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007724#endif
7725}
7726
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007727static void __io_rsrc_put_work(struct fixed_rsrc_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007728{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007729 struct fixed_rsrc_data *rsrc_data = ref_node->rsrc_data;
7730 struct io_ring_ctx *ctx = rsrc_data->ctx;
7731 struct io_rsrc_put *prsrc, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007732
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007733 list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) {
7734 list_del(&prsrc->list);
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007735 ref_node->rsrc_put(ctx, prsrc);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007736 kfree(prsrc);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007737 }
7738
Xiaoguang Wang05589552020-03-31 14:05:18 +08007739 percpu_ref_exit(&ref_node->refs);
7740 kfree(ref_node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007741 percpu_ref_put(&rsrc_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007742}
7743
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007744static void io_rsrc_put_work(struct work_struct *work)
Jens Axboe4a38aed22020-05-14 17:21:15 -06007745{
7746 struct io_ring_ctx *ctx;
7747 struct llist_node *node;
7748
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007749 ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work);
7750 node = llist_del_all(&ctx->rsrc_put_llist);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007751
7752 while (node) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007753 struct fixed_rsrc_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007754 struct llist_node *next = node->next;
7755
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007756 ref_node = llist_entry(node, struct fixed_rsrc_ref_node, llist);
7757 __io_rsrc_put_work(ref_node);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007758 node = next;
7759 }
7760}
7761
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007762static struct file **io_fixed_file_slot(struct fixed_rsrc_data *file_data,
7763 unsigned i)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007764{
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007765 struct fixed_rsrc_table *table;
7766
7767 table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7768 return &table->files[i & IORING_FILE_TABLE_MASK];
7769}
7770
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007771static void io_rsrc_node_ref_zero(struct percpu_ref *ref)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007772{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007773 struct fixed_rsrc_ref_node *ref_node;
7774 struct fixed_rsrc_data *data;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007775 struct io_ring_ctx *ctx;
Pavel Begunkove2978222020-11-18 14:56:26 +00007776 bool first_add = false;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007777 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007778
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007779 ref_node = container_of(ref, struct fixed_rsrc_ref_node, refs);
7780 data = ref_node->rsrc_data;
Pavel Begunkove2978222020-11-18 14:56:26 +00007781 ctx = data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007782
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007783 io_rsrc_ref_lock(ctx);
Pavel Begunkove2978222020-11-18 14:56:26 +00007784 ref_node->done = true;
7785
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007786 while (!list_empty(&ctx->rsrc_ref_list)) {
7787 ref_node = list_first_entry(&ctx->rsrc_ref_list,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007788 struct fixed_rsrc_ref_node, node);
Pavel Begunkove2978222020-11-18 14:56:26 +00007789 /* recycle ref nodes in order */
7790 if (!ref_node->done)
7791 break;
7792 list_del(&ref_node->node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007793 first_add |= llist_add(&ref_node->llist, &ctx->rsrc_put_llist);
Pavel Begunkove2978222020-11-18 14:56:26 +00007794 }
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007795 io_rsrc_ref_unlock(ctx);
Pavel Begunkove2978222020-11-18 14:56:26 +00007796
7797 if (percpu_ref_is_dying(&data->refs))
Jens Axboe4a38aed22020-05-14 17:21:15 -06007798 delay = 0;
7799
Jens Axboe4a38aed22020-05-14 17:21:15 -06007800 if (!delay)
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007801 mod_delayed_work(system_wq, &ctx->rsrc_put_work, 0);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007802 else if (first_add)
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007803 queue_delayed_work(system_wq, &ctx->rsrc_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007804}
7805
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007806static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node(
Xiaoguang Wang05589552020-03-31 14:05:18 +08007807 struct io_ring_ctx *ctx)
7808{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007809 struct fixed_rsrc_ref_node *ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007810
7811 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7812 if (!ref_node)
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007813 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007814
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007815 if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007816 0, GFP_KERNEL)) {
7817 kfree(ref_node);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007818 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007819 }
7820 INIT_LIST_HEAD(&ref_node->node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007821 INIT_LIST_HEAD(&ref_node->rsrc_list);
Pavel Begunkove2978222020-11-18 14:56:26 +00007822 ref_node->done = false;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007823 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007824}
7825
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007826static void init_fixed_file_ref_node(struct io_ring_ctx *ctx,
7827 struct fixed_rsrc_ref_node *ref_node)
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007828{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007829 ref_node->rsrc_data = ctx->file_data;
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007830 ref_node->rsrc_put = io_ring_file_put;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007831}
7832
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007833static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node)
Xiaoguang Wang05589552020-03-31 14:05:18 +08007834{
7835 percpu_ref_exit(&ref_node->refs);
7836 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007837}
7838
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007839
Jens Axboe05f3fb32019-12-09 11:22:50 -07007840static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7841 unsigned nr_args)
7842{
7843 __s32 __user *fds = (__s32 __user *) arg;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007844 unsigned nr_tables, i;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007845 struct file *file;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007846 int fd, ret = -ENOMEM;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007847 struct fixed_rsrc_ref_node *ref_node;
7848 struct fixed_rsrc_data *file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007849
7850 if (ctx->file_data)
7851 return -EBUSY;
7852 if (!nr_args)
7853 return -EINVAL;
7854 if (nr_args > IORING_MAX_FIXED_FILES)
7855 return -EMFILE;
7856
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007857 file_data = alloc_fixed_rsrc_data(ctx);
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007858 if (!file_data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007859 return -ENOMEM;
Dan Carpenter13770a72021-02-01 15:23:42 +03007860 ctx->file_data = file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007861
7862 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
Colin Ian King035fbaf2020-10-12 15:03:41 +01007863 file_data->table = kcalloc(nr_tables, sizeof(*file_data->table),
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007864 GFP_KERNEL);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007865 if (!file_data->table)
7866 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007867
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007868 if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args))
Jens Axboe05f3fb32019-12-09 11:22:50 -07007869 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007870
Jens Axboe05f3fb32019-12-09 11:22:50 -07007871 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007872 if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
7873 ret = -EFAULT;
7874 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007875 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007876 /* allow sparse sets */
7877 if (fd == -1)
7878 continue;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007879
Jens Axboe05f3fb32019-12-09 11:22:50 -07007880 file = fget(fd);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007881 ret = -EBADF;
7882 if (!file)
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007883 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007884
7885 /*
7886 * Don't allow io_uring instances to be registered. If UNIX
7887 * isn't enabled, then this causes a reference cycle and this
7888 * instance can never get freed. If UNIX is enabled we'll
7889 * handle it just fine, but there's still no point in allowing
7890 * a ring fd as it doesn't support regular read/write anyway.
7891 */
7892 if (file->f_op == &io_uring_fops) {
7893 fput(file);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007894 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007895 }
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007896 *io_fixed_file_slot(file_data, i) = file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007897 }
7898
Jens Axboe05f3fb32019-12-09 11:22:50 -07007899 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007900 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007901 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007902 return ret;
7903 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007904
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007905 ref_node = alloc_fixed_rsrc_ref_node(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007906 if (!ref_node) {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007907 io_sqe_files_unregister(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007908 return -ENOMEM;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007909 }
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007910 init_fixed_file_ref_node(ctx, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007911
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007912 io_sqe_rsrc_set_node(ctx, file_data, ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007913 return ret;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007914out_fput:
7915 for (i = 0; i < ctx->nr_user_files; i++) {
7916 file = io_file_from_index(ctx, i);
7917 if (file)
7918 fput(file);
7919 }
7920 for (i = 0; i < nr_tables; i++)
7921 kfree(file_data->table[i].files);
7922 ctx->nr_user_files = 0;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007923out_free:
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007924 free_fixed_rsrc_data(ctx->file_data);
Jens Axboe55cbc252020-10-14 07:35:57 -06007925 ctx->file_data = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007926 return ret;
7927}
7928
Jens Axboec3a31e62019-10-03 13:59:56 -06007929static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7930 int index)
7931{
7932#if defined(CONFIG_UNIX)
7933 struct sock *sock = ctx->ring_sock->sk;
7934 struct sk_buff_head *head = &sock->sk_receive_queue;
7935 struct sk_buff *skb;
7936
7937 /*
7938 * See if we can merge this file into an existing skb SCM_RIGHTS
7939 * file set. If there's no room, fall back to allocating a new skb
7940 * and filling it in.
7941 */
7942 spin_lock_irq(&head->lock);
7943 skb = skb_peek(head);
7944 if (skb) {
7945 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7946
7947 if (fpl->count < SCM_MAX_FD) {
7948 __skb_unlink(skb, head);
7949 spin_unlock_irq(&head->lock);
7950 fpl->fp[fpl->count] = get_file(file);
7951 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7952 fpl->count++;
7953 spin_lock_irq(&head->lock);
7954 __skb_queue_head(head, skb);
7955 } else {
7956 skb = NULL;
7957 }
7958 }
7959 spin_unlock_irq(&head->lock);
7960
7961 if (skb) {
7962 fput(file);
7963 return 0;
7964 }
7965
7966 return __io_sqe_files_scm(ctx, 1, index);
7967#else
7968 return 0;
7969#endif
7970}
7971
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007972static int io_queue_rsrc_removal(struct fixed_rsrc_data *data, void *rsrc)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007973{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007974 struct io_rsrc_put *prsrc;
7975 struct fixed_rsrc_ref_node *ref_node = data->node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007976
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007977 prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL);
7978 if (!prsrc)
Hillf Dantona5318d32020-03-23 17:47:15 +08007979 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007980
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007981 prsrc->rsrc = rsrc;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007982 list_add(&prsrc->list, &ref_node->rsrc_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007983
Hillf Dantona5318d32020-03-23 17:47:15 +08007984 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007985}
7986
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007987static inline int io_queue_file_removal(struct fixed_rsrc_data *data,
7988 struct file *file)
7989{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007990 return io_queue_rsrc_removal(data, (void *)file);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007991}
7992
Jens Axboe05f3fb32019-12-09 11:22:50 -07007993static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007994 struct io_uring_rsrc_update *up,
Jens Axboe05f3fb32019-12-09 11:22:50 -07007995 unsigned nr_args)
7996{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007997 struct fixed_rsrc_data *data = ctx->file_data;
7998 struct fixed_rsrc_ref_node *ref_node;
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007999 struct file *file, **file_slot;
Jens Axboec3a31e62019-10-03 13:59:56 -06008000 __s32 __user *fds;
8001 int fd, i, err;
8002 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008003 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06008004
Jens Axboe05f3fb32019-12-09 11:22:50 -07008005 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06008006 return -EOVERFLOW;
8007 if (done > ctx->nr_user_files)
8008 return -EINVAL;
8009
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00008010 ref_node = alloc_fixed_rsrc_ref_node(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00008011 if (!ref_node)
8012 return -ENOMEM;
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00008013 init_fixed_file_ref_node(ctx, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08008014
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008015 fds = u64_to_user_ptr(up->data);
Pavel Begunkov67973b92021-01-26 13:51:09 +00008016 for (done = 0; done < nr_args; done++) {
Jens Axboec3a31e62019-10-03 13:59:56 -06008017 err = 0;
8018 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
8019 err = -EFAULT;
8020 break;
8021 }
noah4e0377a2021-01-26 15:23:28 -05008022 if (fd == IORING_REGISTER_FILES_SKIP)
8023 continue;
8024
Pavel Begunkov67973b92021-01-26 13:51:09 +00008025 i = array_index_nospec(up->offset + done, ctx->nr_user_files);
Pavel Begunkovea64ec022021-02-04 13:52:07 +00008026 file_slot = io_fixed_file_slot(ctx->file_data, i);
8027
8028 if (*file_slot) {
8029 err = io_queue_file_removal(data, *file_slot);
Hillf Dantona5318d32020-03-23 17:47:15 +08008030 if (err)
8031 break;
Pavel Begunkovea64ec022021-02-04 13:52:07 +00008032 *file_slot = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008033 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06008034 }
8035 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06008036 file = fget(fd);
8037 if (!file) {
8038 err = -EBADF;
8039 break;
8040 }
8041 /*
8042 * Don't allow io_uring instances to be registered. If
8043 * UNIX isn't enabled, then this causes a reference
8044 * cycle and this instance can never get freed. If UNIX
8045 * is enabled we'll handle it just fine, but there's
8046 * still no point in allowing a ring fd as it doesn't
8047 * support regular read/write anyway.
8048 */
8049 if (file->f_op == &io_uring_fops) {
8050 fput(file);
8051 err = -EBADF;
8052 break;
8053 }
Jens Axboee68a3ff2021-02-11 07:45:08 -07008054 *file_slot = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06008055 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008056 if (err) {
Jens Axboee68a3ff2021-02-11 07:45:08 -07008057 *file_slot = NULL;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008058 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008059 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008060 }
Jens Axboec3a31e62019-10-03 13:59:56 -06008061 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008062 }
8063
Xiaoguang Wang05589552020-03-31 14:05:18 +08008064 if (needs_switch) {
Pavel Begunkovb2e96852020-10-10 18:34:16 +01008065 percpu_ref_kill(&data->node->refs);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00008066 io_sqe_rsrc_set_node(ctx, data, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08008067 } else
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008068 destroy_fixed_rsrc_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06008069
8070 return done ? done : err;
8071}
Xiaoguang Wang05589552020-03-31 14:05:18 +08008072
Jens Axboe05f3fb32019-12-09 11:22:50 -07008073static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
8074 unsigned nr_args)
8075{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008076 struct io_uring_rsrc_update up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008077
8078 if (!ctx->file_data)
8079 return -ENXIO;
8080 if (!nr_args)
8081 return -EINVAL;
8082 if (copy_from_user(&up, arg, sizeof(up)))
8083 return -EFAULT;
8084 if (up.resv)
8085 return -EINVAL;
8086
8087 return __io_sqe_files_update(ctx, &up, nr_args);
8088}
Jens Axboec3a31e62019-10-03 13:59:56 -06008089
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00008090static struct io_wq_work *io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07008091{
8092 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8093
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00008094 req = io_put_req_find_next(req);
8095 return req ? &req->work : NULL;
Jens Axboe7d723062019-11-12 22:31:31 -07008096}
8097
Pavel Begunkov24369c22020-01-28 03:15:48 +03008098static int io_init_wq_offload(struct io_ring_ctx *ctx,
8099 struct io_uring_params *p)
8100{
8101 struct io_wq_data data;
8102 struct fd f;
8103 struct io_ring_ctx *ctx_attach;
8104 unsigned int concurrency;
8105 int ret = 0;
8106
8107 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03008108 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03008109 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008110
8111 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
8112 /* Do QD, or 4 * CPUS, whatever is smallest */
8113 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
8114
8115 ctx->io_wq = io_wq_create(concurrency, &data);
8116 if (IS_ERR(ctx->io_wq)) {
8117 ret = PTR_ERR(ctx->io_wq);
8118 ctx->io_wq = NULL;
8119 }
8120 return ret;
8121 }
8122
8123 f = fdget(p->wq_fd);
8124 if (!f.file)
8125 return -EBADF;
8126
8127 if (f.file->f_op != &io_uring_fops) {
8128 ret = -EINVAL;
8129 goto out_fput;
8130 }
8131
8132 ctx_attach = f.file->private_data;
8133 /* @io_wq is protected by holding the fd */
8134 if (!io_wq_get(ctx_attach->io_wq, &data)) {
8135 ret = -EINVAL;
8136 goto out_fput;
8137 }
8138
8139 ctx->io_wq = ctx_attach->io_wq;
8140out_fput:
8141 fdput(f);
8142 return ret;
8143}
8144
Jens Axboe0f212202020-09-13 13:09:39 -06008145static int io_uring_alloc_task_context(struct task_struct *task)
8146{
8147 struct io_uring_task *tctx;
Jens Axboed8a6df12020-10-15 16:24:45 -06008148 int ret;
Jens Axboe0f212202020-09-13 13:09:39 -06008149
8150 tctx = kmalloc(sizeof(*tctx), GFP_KERNEL);
8151 if (unlikely(!tctx))
8152 return -ENOMEM;
8153
Jens Axboed8a6df12020-10-15 16:24:45 -06008154 ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
8155 if (unlikely(ret)) {
8156 kfree(tctx);
8157 return ret;
8158 }
8159
Jens Axboe0f212202020-09-13 13:09:39 -06008160 xa_init(&tctx->xa);
8161 init_waitqueue_head(&tctx->wait);
8162 tctx->last = NULL;
Jens Axboefdaf0832020-10-30 09:37:30 -06008163 atomic_set(&tctx->in_idle, 0);
8164 tctx->sqpoll = false;
Jens Axboe500a3732020-10-15 17:38:03 -06008165 io_init_identity(&tctx->__identity);
8166 tctx->identity = &tctx->__identity;
Jens Axboe0f212202020-09-13 13:09:39 -06008167 task->io_uring = tctx;
Jens Axboe7cbf1722021-02-10 00:03:20 +00008168 spin_lock_init(&tctx->task_lock);
8169 INIT_WQ_LIST(&tctx->task_list);
8170 tctx->task_state = 0;
8171 init_task_work(&tctx->task_work, tctx_task_work);
Jens Axboe0f212202020-09-13 13:09:39 -06008172 return 0;
8173}
8174
8175void __io_uring_free(struct task_struct *tsk)
8176{
8177 struct io_uring_task *tctx = tsk->io_uring;
8178
8179 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Jens Axboe500a3732020-10-15 17:38:03 -06008180 WARN_ON_ONCE(refcount_read(&tctx->identity->count) != 1);
8181 if (tctx->identity != &tctx->__identity)
8182 kfree(tctx->identity);
Jens Axboed8a6df12020-10-15 16:24:45 -06008183 percpu_counter_destroy(&tctx->inflight);
Jens Axboe0f212202020-09-13 13:09:39 -06008184 kfree(tctx);
8185 tsk->io_uring = NULL;
8186}
8187
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008188static int io_sq_offload_create(struct io_ring_ctx *ctx,
8189 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008190{
8191 int ret;
8192
Jens Axboe6c271ce2019-01-10 11:22:30 -07008193 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06008194 struct io_sq_data *sqd;
8195
Jens Axboe3ec482d2019-04-08 10:51:01 -06008196 ret = -EPERM;
Jens Axboece59fc62020-09-02 13:28:09 -06008197 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE))
Jens Axboe3ec482d2019-04-08 10:51:01 -06008198 goto err;
8199
Jens Axboe534ca6d2020-09-02 13:52:19 -06008200 sqd = io_get_sq_data(p);
8201 if (IS_ERR(sqd)) {
8202 ret = PTR_ERR(sqd);
8203 goto err;
8204 }
Jens Axboe69fb2132020-09-14 11:16:23 -06008205
Jens Axboe534ca6d2020-09-02 13:52:19 -06008206 ctx->sq_data = sqd;
Jens Axboe69fb2132020-09-14 11:16:23 -06008207 io_sq_thread_park(sqd);
8208 mutex_lock(&sqd->ctx_lock);
8209 list_add(&ctx->sqd_list, &sqd->ctx_new_list);
8210 mutex_unlock(&sqd->ctx_lock);
8211 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008212
Jens Axboe917257d2019-04-13 09:28:55 -06008213 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
8214 if (!ctx->sq_thread_idle)
8215 ctx->sq_thread_idle = HZ;
8216
Jens Axboeaa061652020-09-02 14:50:27 -06008217 if (sqd->thread)
8218 goto done;
8219
Jens Axboe6c271ce2019-01-10 11:22:30 -07008220 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06008221 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008222
Jens Axboe917257d2019-04-13 09:28:55 -06008223 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06008224 if (cpu >= nr_cpu_ids)
8225 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08008226 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06008227 goto err;
8228
Jens Axboe69fb2132020-09-14 11:16:23 -06008229 sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd,
Jens Axboe534ca6d2020-09-02 13:52:19 -06008230 cpu, "io_uring-sq");
Jens Axboe6c271ce2019-01-10 11:22:30 -07008231 } else {
Jens Axboe69fb2132020-09-14 11:16:23 -06008232 sqd->thread = kthread_create(io_sq_thread, sqd,
Jens Axboe6c271ce2019-01-10 11:22:30 -07008233 "io_uring-sq");
8234 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06008235 if (IS_ERR(sqd->thread)) {
8236 ret = PTR_ERR(sqd->thread);
8237 sqd->thread = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008238 goto err;
8239 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06008240 ret = io_uring_alloc_task_context(sqd->thread);
Jens Axboe0f212202020-09-13 13:09:39 -06008241 if (ret)
8242 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008243 } else if (p->flags & IORING_SETUP_SQ_AFF) {
8244 /* Can't have SQ_AFF without SQPOLL */
8245 ret = -EINVAL;
8246 goto err;
8247 }
8248
Jens Axboeaa061652020-09-02 14:50:27 -06008249done:
Pavel Begunkov24369c22020-01-28 03:15:48 +03008250 ret = io_init_wq_offload(ctx, p);
8251 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008252 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008253
8254 return 0;
8255err:
Jens Axboe54a91f32019-09-10 09:15:04 -06008256 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008257 return ret;
8258}
8259
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008260static void io_sq_offload_start(struct io_ring_ctx *ctx)
8261{
Jens Axboe534ca6d2020-09-02 13:52:19 -06008262 struct io_sq_data *sqd = ctx->sq_data;
8263
8264 if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread)
8265 wake_up_process(sqd->thread);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008266}
8267
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008268static inline void __io_unaccount_mem(struct user_struct *user,
8269 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008270{
8271 atomic_long_sub(nr_pages, &user->locked_vm);
8272}
8273
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008274static inline int __io_account_mem(struct user_struct *user,
8275 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008276{
8277 unsigned long page_limit, cur_pages, new_pages;
8278
8279 /* Don't allow more pages than we can safely lock */
8280 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
8281
8282 do {
8283 cur_pages = atomic_long_read(&user->locked_vm);
8284 new_pages = cur_pages + nr_pages;
8285 if (new_pages > page_limit)
8286 return -ENOMEM;
8287 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
8288 new_pages) != cur_pages);
8289
8290 return 0;
8291}
8292
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008293static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008294{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008295 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008296 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008297
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008298 if (ctx->mm_account)
8299 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008300}
8301
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008302static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008303{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008304 int ret;
8305
8306 if (ctx->limit_mem) {
8307 ret = __io_account_mem(ctx->user, nr_pages);
8308 if (ret)
8309 return ret;
8310 }
8311
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008312 if (ctx->mm_account)
8313 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008314
8315 return 0;
8316}
8317
Jens Axboe2b188cc2019-01-07 10:46:33 -07008318static void io_mem_free(void *ptr)
8319{
Mark Rutland52e04ef2019-04-30 17:30:21 +01008320 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008321
Mark Rutland52e04ef2019-04-30 17:30:21 +01008322 if (!ptr)
8323 return;
8324
8325 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008326 if (put_page_testzero(page))
8327 free_compound_page(page);
8328}
8329
8330static void *io_mem_alloc(size_t size)
8331{
8332 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008333 __GFP_NORETRY | __GFP_ACCOUNT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008334
8335 return (void *) __get_free_pages(gfp_flags, get_order(size));
8336}
8337
Hristo Venev75b28af2019-08-26 17:23:46 +00008338static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
8339 size_t *sq_offset)
8340{
8341 struct io_rings *rings;
8342 size_t off, sq_array_size;
8343
8344 off = struct_size(rings, cqes, cq_entries);
8345 if (off == SIZE_MAX)
8346 return SIZE_MAX;
8347
8348#ifdef CONFIG_SMP
8349 off = ALIGN(off, SMP_CACHE_BYTES);
8350 if (off == 0)
8351 return SIZE_MAX;
8352#endif
8353
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02008354 if (sq_offset)
8355 *sq_offset = off;
8356
Hristo Venev75b28af2019-08-26 17:23:46 +00008357 sq_array_size = array_size(sizeof(u32), sq_entries);
8358 if (sq_array_size == SIZE_MAX)
8359 return SIZE_MAX;
8360
8361 if (check_add_overflow(off, sq_array_size, &off))
8362 return SIZE_MAX;
8363
Hristo Venev75b28af2019-08-26 17:23:46 +00008364 return off;
8365}
8366
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008367static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
Jens Axboeedafcce2019-01-09 09:16:05 -07008368{
8369 int i, j;
8370
8371 if (!ctx->user_bufs)
8372 return -ENXIO;
8373
8374 for (i = 0; i < ctx->nr_user_bufs; i++) {
8375 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8376
8377 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008378 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07008379
Jens Axboede293932020-09-17 16:19:16 -06008380 if (imu->acct_pages)
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008381 io_unaccount_mem(ctx, imu->acct_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008382 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008383 imu->nr_bvecs = 0;
8384 }
8385
8386 kfree(ctx->user_bufs);
8387 ctx->user_bufs = NULL;
8388 ctx->nr_user_bufs = 0;
8389 return 0;
8390}
8391
8392static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
8393 void __user *arg, unsigned index)
8394{
8395 struct iovec __user *src;
8396
8397#ifdef CONFIG_COMPAT
8398 if (ctx->compat) {
8399 struct compat_iovec __user *ciovs;
8400 struct compat_iovec ciov;
8401
8402 ciovs = (struct compat_iovec __user *) arg;
8403 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8404 return -EFAULT;
8405
Jens Axboed55e5f52019-12-11 16:12:15 -07008406 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008407 dst->iov_len = ciov.iov_len;
8408 return 0;
8409 }
8410#endif
8411 src = (struct iovec __user *) arg;
8412 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8413 return -EFAULT;
8414 return 0;
8415}
8416
Jens Axboede293932020-09-17 16:19:16 -06008417/*
8418 * Not super efficient, but this is just a registration time. And we do cache
8419 * the last compound head, so generally we'll only do a full search if we don't
8420 * match that one.
8421 *
8422 * We check if the given compound head page has already been accounted, to
8423 * avoid double accounting it. This allows us to account the full size of the
8424 * page, not just the constituent pages of a huge page.
8425 */
8426static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
8427 int nr_pages, struct page *hpage)
8428{
8429 int i, j;
8430
8431 /* check current page array */
8432 for (i = 0; i < nr_pages; i++) {
8433 if (!PageCompound(pages[i]))
8434 continue;
8435 if (compound_head(pages[i]) == hpage)
8436 return true;
8437 }
8438
8439 /* check previously registered pages */
8440 for (i = 0; i < ctx->nr_user_bufs; i++) {
8441 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8442
8443 for (j = 0; j < imu->nr_bvecs; j++) {
8444 if (!PageCompound(imu->bvec[j].bv_page))
8445 continue;
8446 if (compound_head(imu->bvec[j].bv_page) == hpage)
8447 return true;
8448 }
8449 }
8450
8451 return false;
8452}
8453
8454static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
8455 int nr_pages, struct io_mapped_ubuf *imu,
8456 struct page **last_hpage)
8457{
8458 int i, ret;
8459
8460 for (i = 0; i < nr_pages; i++) {
8461 if (!PageCompound(pages[i])) {
8462 imu->acct_pages++;
8463 } else {
8464 struct page *hpage;
8465
8466 hpage = compound_head(pages[i]);
8467 if (hpage == *last_hpage)
8468 continue;
8469 *last_hpage = hpage;
8470 if (headpage_already_acct(ctx, pages, i, hpage))
8471 continue;
8472 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
8473 }
8474 }
8475
8476 if (!imu->acct_pages)
8477 return 0;
8478
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008479 ret = io_account_mem(ctx, imu->acct_pages);
Jens Axboede293932020-09-17 16:19:16 -06008480 if (ret)
8481 imu->acct_pages = 0;
8482 return ret;
8483}
8484
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008485static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
8486 struct io_mapped_ubuf *imu,
8487 struct page **last_hpage)
Jens Axboeedafcce2019-01-09 09:16:05 -07008488{
8489 struct vm_area_struct **vmas = NULL;
8490 struct page **pages = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008491 unsigned long off, start, end, ubuf;
8492 size_t size;
8493 int ret, pret, nr_pages, i;
Jens Axboeedafcce2019-01-09 09:16:05 -07008494
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008495 ubuf = (unsigned long) iov->iov_base;
8496 end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8497 start = ubuf >> PAGE_SHIFT;
8498 nr_pages = end - start;
8499
8500 ret = -ENOMEM;
8501
8502 pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
8503 if (!pages)
8504 goto done;
8505
8506 vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *),
8507 GFP_KERNEL);
8508 if (!vmas)
8509 goto done;
8510
8511 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
8512 GFP_KERNEL);
8513 if (!imu->bvec)
8514 goto done;
8515
8516 ret = 0;
8517 mmap_read_lock(current->mm);
8518 pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
8519 pages, vmas);
8520 if (pret == nr_pages) {
8521 /* don't support file backed memory */
8522 for (i = 0; i < nr_pages; i++) {
8523 struct vm_area_struct *vma = vmas[i];
8524
8525 if (vma->vm_file &&
8526 !is_file_hugepages(vma->vm_file)) {
8527 ret = -EOPNOTSUPP;
8528 break;
8529 }
8530 }
8531 } else {
8532 ret = pret < 0 ? pret : -EFAULT;
8533 }
8534 mmap_read_unlock(current->mm);
8535 if (ret) {
8536 /*
8537 * if we did partial map, or found file backed vmas,
8538 * release any pages we did get
8539 */
8540 if (pret > 0)
8541 unpin_user_pages(pages, pret);
8542 kvfree(imu->bvec);
8543 goto done;
8544 }
8545
8546 ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage);
8547 if (ret) {
8548 unpin_user_pages(pages, pret);
8549 kvfree(imu->bvec);
8550 goto done;
8551 }
8552
8553 off = ubuf & ~PAGE_MASK;
8554 size = iov->iov_len;
8555 for (i = 0; i < nr_pages; i++) {
8556 size_t vec_len;
8557
8558 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8559 imu->bvec[i].bv_page = pages[i];
8560 imu->bvec[i].bv_len = vec_len;
8561 imu->bvec[i].bv_offset = off;
8562 off = 0;
8563 size -= vec_len;
8564 }
8565 /* store original address for later verification */
8566 imu->ubuf = ubuf;
8567 imu->len = iov->iov_len;
8568 imu->nr_bvecs = nr_pages;
8569 ret = 0;
8570done:
8571 kvfree(pages);
8572 kvfree(vmas);
8573 return ret;
8574}
8575
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008576static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008577{
Jens Axboeedafcce2019-01-09 09:16:05 -07008578 if (ctx->user_bufs)
8579 return -EBUSY;
8580 if (!nr_args || nr_args > UIO_MAXIOV)
8581 return -EINVAL;
8582
8583 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
8584 GFP_KERNEL);
8585 if (!ctx->user_bufs)
8586 return -ENOMEM;
8587
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008588 return 0;
8589}
8590
8591static int io_buffer_validate(struct iovec *iov)
8592{
8593 /*
8594 * Don't impose further limits on the size and buffer
8595 * constraints here, we'll -EINVAL later when IO is
8596 * submitted if they are wrong.
8597 */
8598 if (!iov->iov_base || !iov->iov_len)
8599 return -EFAULT;
8600
8601 /* arbitrary limit, but we need something */
8602 if (iov->iov_len > SZ_1G)
8603 return -EFAULT;
8604
8605 return 0;
8606}
8607
8608static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
8609 unsigned int nr_args)
8610{
8611 int i, ret;
8612 struct iovec iov;
8613 struct page *last_hpage = NULL;
8614
8615 ret = io_buffers_map_alloc(ctx, nr_args);
8616 if (ret)
8617 return ret;
8618
Jens Axboeedafcce2019-01-09 09:16:05 -07008619 for (i = 0; i < nr_args; i++) {
8620 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
Jens Axboeedafcce2019-01-09 09:16:05 -07008621
8622 ret = io_copy_iov(ctx, &iov, arg, i);
8623 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008624 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008625
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008626 ret = io_buffer_validate(&iov);
8627 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008628 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008629
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008630 ret = io_sqe_buffer_register(ctx, &iov, imu, &last_hpage);
8631 if (ret)
8632 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008633
8634 ctx->nr_user_bufs++;
8635 }
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008636
8637 if (ret)
8638 io_sqe_buffers_unregister(ctx);
8639
Jens Axboeedafcce2019-01-09 09:16:05 -07008640 return ret;
8641}
8642
Jens Axboe9b402842019-04-11 11:45:41 -06008643static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
8644{
8645 __s32 __user *fds = arg;
8646 int fd;
8647
8648 if (ctx->cq_ev_fd)
8649 return -EBUSY;
8650
8651 if (copy_from_user(&fd, fds, sizeof(*fds)))
8652 return -EFAULT;
8653
8654 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
8655 if (IS_ERR(ctx->cq_ev_fd)) {
8656 int ret = PTR_ERR(ctx->cq_ev_fd);
8657 ctx->cq_ev_fd = NULL;
8658 return ret;
8659 }
8660
8661 return 0;
8662}
8663
8664static int io_eventfd_unregister(struct io_ring_ctx *ctx)
8665{
8666 if (ctx->cq_ev_fd) {
8667 eventfd_ctx_put(ctx->cq_ev_fd);
8668 ctx->cq_ev_fd = NULL;
8669 return 0;
8670 }
8671
8672 return -ENXIO;
8673}
8674
Jens Axboe5a2e7452020-02-23 16:23:11 -07008675static int __io_destroy_buffers(int id, void *p, void *data)
8676{
8677 struct io_ring_ctx *ctx = data;
8678 struct io_buffer *buf = p;
8679
Jens Axboe067524e2020-03-02 16:32:28 -07008680 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008681 return 0;
8682}
8683
8684static void io_destroy_buffers(struct io_ring_ctx *ctx)
8685{
8686 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
8687 idr_destroy(&ctx->io_buffer_idr);
8688}
8689
Jens Axboe68e68ee2021-02-13 09:00:02 -07008690static void io_req_cache_free(struct list_head *list, struct task_struct *tsk)
Jens Axboe1b4c3512021-02-10 00:03:19 +00008691{
Jens Axboe68e68ee2021-02-13 09:00:02 -07008692 struct io_kiocb *req, *nxt;
Jens Axboe1b4c3512021-02-10 00:03:19 +00008693
Jens Axboe68e68ee2021-02-13 09:00:02 -07008694 list_for_each_entry_safe(req, nxt, list, compl.list) {
8695 if (tsk && req->task != tsk)
8696 continue;
Jens Axboe1b4c3512021-02-10 00:03:19 +00008697 list_del(&req->compl.list);
8698 kmem_cache_free(req_cachep, req);
8699 }
8700}
8701
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008702static void io_req_caches_free(struct io_ring_ctx *ctx, struct task_struct *tsk)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008703{
Pavel Begunkovbf019da2021-02-10 00:03:17 +00008704 struct io_submit_state *submit_state = &ctx->submit_state;
8705
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008706 mutex_lock(&ctx->uring_lock);
8707
8708 if (submit_state->free_reqs)
8709 kmem_cache_free_bulk(req_cachep, submit_state->free_reqs,
8710 submit_state->reqs);
8711
8712 io_req_cache_free(&submit_state->comp.free_list, NULL);
8713
8714 spin_lock_irq(&ctx->completion_lock);
8715 io_req_cache_free(&submit_state->comp.locked_free_list, NULL);
8716 spin_unlock_irq(&ctx->completion_lock);
8717
8718 mutex_unlock(&ctx->uring_lock);
8719}
8720
Jens Axboe2b188cc2019-01-07 10:46:33 -07008721static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8722{
Pavel Begunkov04fc6c82021-02-12 03:23:54 +00008723 /*
8724 * Some may use context even when all refs and requests have been put,
8725 * and they are free to do so while still holding uring_lock, see
8726 * __io_req_task_submit(). Wait for them to finish.
8727 */
8728 mutex_lock(&ctx->uring_lock);
8729 mutex_unlock(&ctx->uring_lock);
8730
Jens Axboe6b063142019-01-10 22:13:58 -07008731 io_finish_async(ctx);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008732 io_sqe_buffers_unregister(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008733
8734 if (ctx->sqo_task) {
8735 put_task_struct(ctx->sqo_task);
8736 ctx->sqo_task = NULL;
8737 mmdrop(ctx->mm_account);
8738 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008739 }
Jens Axboedef596e2019-01-09 08:59:42 -07008740
Dennis Zhou91d8f512020-09-16 13:41:05 -07008741#ifdef CONFIG_BLK_CGROUP
8742 if (ctx->sqo_blkcg_css)
8743 css_put(ctx->sqo_blkcg_css);
8744#endif
8745
Jens Axboe6b063142019-01-10 22:13:58 -07008746 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06008747 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008748 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07008749 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07008750
Jens Axboe2b188cc2019-01-07 10:46:33 -07008751#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07008752 if (ctx->ring_sock) {
8753 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008754 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07008755 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008756#endif
8757
Hristo Venev75b28af2019-08-26 17:23:46 +00008758 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008759 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008760
8761 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008762 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07008763 put_cred(ctx->creds);
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008764 io_req_caches_free(ctx, NULL);
Jens Axboe78076bb2019-12-04 19:56:40 -07008765 kfree(ctx->cancel_hash);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008766 kfree(ctx);
8767}
8768
8769static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8770{
8771 struct io_ring_ctx *ctx = file->private_data;
8772 __poll_t mask = 0;
8773
8774 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02008775 /*
8776 * synchronizes with barrier from wq_has_sleeper call in
8777 * io_commit_cqring
8778 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008779 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06008780 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008781 mask |= EPOLLOUT | EPOLLWRNORM;
Hao Xued670c32021-02-05 16:34:21 +08008782
8783 /*
8784 * Don't flush cqring overflow list here, just do a simple check.
8785 * Otherwise there could possible be ABBA deadlock:
8786 * CPU0 CPU1
8787 * ---- ----
8788 * lock(&ctx->uring_lock);
8789 * lock(&ep->mtx);
8790 * lock(&ctx->uring_lock);
8791 * lock(&ep->mtx);
8792 *
8793 * Users may get EPOLLIN meanwhile seeing nothing in cqring, this
8794 * pushs them to do the flush.
8795 */
8796 if (io_cqring_events(ctx) || test_bit(0, &ctx->cq_check_overflow))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008797 mask |= EPOLLIN | EPOLLRDNORM;
8798
8799 return mask;
8800}
8801
8802static int io_uring_fasync(int fd, struct file *file, int on)
8803{
8804 struct io_ring_ctx *ctx = file->private_data;
8805
8806 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8807}
8808
Yejune Deng0bead8c2020-12-24 11:02:20 +08008809static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
Jens Axboe071698e2020-01-28 10:04:42 -07008810{
Jens Axboe1e6fa522020-10-15 08:46:24 -06008811 struct io_identity *iod;
Jens Axboe071698e2020-01-28 10:04:42 -07008812
Jens Axboe1e6fa522020-10-15 08:46:24 -06008813 iod = idr_remove(&ctx->personality_idr, id);
8814 if (iod) {
8815 put_cred(iod->creds);
8816 if (refcount_dec_and_test(&iod->count))
8817 kfree(iod);
Yejune Deng0bead8c2020-12-24 11:02:20 +08008818 return 0;
Jens Axboe1e6fa522020-10-15 08:46:24 -06008819 }
Yejune Deng0bead8c2020-12-24 11:02:20 +08008820
8821 return -EINVAL;
8822}
8823
8824static int io_remove_personalities(int id, void *p, void *data)
8825{
8826 struct io_ring_ctx *ctx = data;
8827
8828 io_unregister_personality(ctx, id);
Jens Axboe071698e2020-01-28 10:04:42 -07008829 return 0;
8830}
8831
Jens Axboe85faa7b2020-04-09 18:14:00 -06008832static void io_ring_exit_work(struct work_struct *work)
8833{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008834 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
8835 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06008836
Jens Axboe56952e92020-06-17 15:00:04 -06008837 /*
8838 * If we're doing polled IO and end up having requests being
8839 * submitted async (out-of-line), then completions can come in while
8840 * we're waiting for refs to drop. We need to reap these manually,
8841 * as nobody else will be looking for them.
8842 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008843 do {
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008844 io_uring_try_cancel_requests(ctx, NULL, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008845 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06008846 io_ring_ctx_free(ctx);
8847}
8848
Jens Axboe00c18642020-12-20 10:45:02 -07008849static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data)
8850{
8851 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8852
8853 return req->ctx == data;
8854}
8855
Jens Axboe2b188cc2019-01-07 10:46:33 -07008856static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8857{
8858 mutex_lock(&ctx->uring_lock);
8859 percpu_ref_kill(&ctx->refs);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008860
8861 if (WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && !ctx->sqo_dead))
8862 ctx->sqo_dead = 1;
8863
Pavel Begunkovcda286f2020-12-17 00:24:35 +00008864 /* if force is set, the ring is going away. always drop after that */
8865 ctx->cq_overflow_flushed = 1;
Pavel Begunkov634578f2020-12-06 22:22:44 +00008866 if (ctx->rings)
Pavel Begunkov6c503152021-01-04 20:36:36 +00008867 __io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkov5c766a92021-01-19 13:32:36 +00008868 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008869 mutex_unlock(&ctx->uring_lock);
8870
Pavel Begunkov6b819282020-11-06 13:00:25 +00008871 io_kill_timeouts(ctx, NULL, NULL);
8872 io_poll_remove_all(ctx, NULL, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06008873
8874 if (ctx->io_wq)
Jens Axboe00c18642020-12-20 10:45:02 -07008875 io_wq_cancel_cb(ctx->io_wq, io_cancel_ctx_cb, ctx, true);
Jens Axboe561fb042019-10-24 07:25:42 -06008876
Jens Axboe15dff282019-11-13 09:09:23 -07008877 /* if we failed setting up the ctx, we might not have any rings */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008878 io_iopoll_try_reap_events(ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06008879
Jens Axboe85faa7b2020-04-09 18:14:00 -06008880 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008881 /*
8882 * Use system_unbound_wq to avoid spawning tons of event kworkers
8883 * if we're exiting a ton of rings at the same time. It just adds
8884 * noise and overhead, there's no discernable change in runtime
8885 * over using system_wq.
8886 */
8887 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008888}
8889
8890static int io_uring_release(struct inode *inode, struct file *file)
8891{
8892 struct io_ring_ctx *ctx = file->private_data;
8893
8894 file->private_data = NULL;
8895 io_ring_ctx_wait_and_kill(ctx);
8896 return 0;
8897}
8898
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008899struct io_task_cancel {
8900 struct task_struct *task;
8901 struct files_struct *files;
8902};
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008903
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008904static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Jens Axboeb711d4e2020-08-16 08:23:05 -07008905{
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008906 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008907 struct io_task_cancel *cancel = data;
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008908 bool ret;
8909
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008910 if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) {
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008911 unsigned long flags;
8912 struct io_ring_ctx *ctx = req->ctx;
8913
8914 /* protect against races with linked timeouts */
8915 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008916 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008917 spin_unlock_irqrestore(&ctx->completion_lock, flags);
8918 } else {
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008919 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008920 }
8921 return ret;
Jens Axboeb711d4e2020-08-16 08:23:05 -07008922}
8923
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008924static void io_cancel_defer_files(struct io_ring_ctx *ctx,
Pavel Begunkovef9865a2020-11-05 14:06:19 +00008925 struct task_struct *task,
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008926 struct files_struct *files)
8927{
8928 struct io_defer_entry *de = NULL;
8929 LIST_HEAD(list);
8930
8931 spin_lock_irq(&ctx->completion_lock);
8932 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkov08d23632020-11-06 13:00:22 +00008933 if (io_match_task(de->req, task, files)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008934 list_cut_position(&list, &ctx->defer_list, &de->list);
8935 break;
8936 }
8937 }
8938 spin_unlock_irq(&ctx->completion_lock);
8939
8940 while (!list_empty(&list)) {
8941 de = list_first_entry(&list, struct io_defer_entry, list);
8942 list_del_init(&de->list);
8943 req_set_fail_links(de->req);
8944 io_put_req(de->req);
8945 io_req_complete(de->req, -ECANCELED);
8946 kfree(de);
8947 }
8948}
8949
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008950static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
8951 struct task_struct *task,
8952 struct files_struct *files)
8953{
8954 struct io_task_cancel cancel = { .task = task, .files = files, };
8955
8956 while (1) {
8957 enum io_wq_cancel cret;
8958 bool ret = false;
8959
8960 if (ctx->io_wq) {
8961 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb,
8962 &cancel, true);
8963 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
8964 }
8965
8966 /* SQPOLL thread does its own polling */
8967 if (!(ctx->flags & IORING_SETUP_SQPOLL) && !files) {
8968 while (!list_empty_careful(&ctx->iopoll_list)) {
8969 io_iopoll_try_reap_events(ctx);
8970 ret = true;
8971 }
8972 }
8973
8974 ret |= io_poll_remove_all(ctx, task, files);
8975 ret |= io_kill_timeouts(ctx, task, files);
8976 ret |= io_run_task_work();
8977 io_cqring_overflow_flush(ctx, true, task, files);
8978 if (!ret)
8979 break;
8980 cond_resched();
8981 }
8982}
8983
Pavel Begunkovca70f002021-01-26 15:28:27 +00008984static int io_uring_count_inflight(struct io_ring_ctx *ctx,
8985 struct task_struct *task,
8986 struct files_struct *files)
8987{
8988 struct io_kiocb *req;
8989 int cnt = 0;
8990
8991 spin_lock_irq(&ctx->inflight_lock);
8992 list_for_each_entry(req, &ctx->inflight_list, inflight_entry)
8993 cnt += io_match_task(req, task, files);
8994 spin_unlock_irq(&ctx->inflight_lock);
8995 return cnt;
8996}
8997
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008998static void io_uring_cancel_files(struct io_ring_ctx *ctx,
Pavel Begunkovdf9923f2020-11-06 13:00:23 +00008999 struct task_struct *task,
Jens Axboefcb323c2019-10-24 12:39:47 -06009000 struct files_struct *files)
9001{
Jens Axboefcb323c2019-10-24 12:39:47 -06009002 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08009003 DEFINE_WAIT(wait);
Pavel Begunkovca70f002021-01-26 15:28:27 +00009004 int inflight;
Jens Axboefcb323c2019-10-24 12:39:47 -06009005
Pavel Begunkovca70f002021-01-26 15:28:27 +00009006 inflight = io_uring_count_inflight(ctx, task, files);
9007 if (!inflight)
Jens Axboefcb323c2019-10-24 12:39:47 -06009008 break;
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009009
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009010 io_uring_try_cancel_requests(ctx, task, files);
Pavel Begunkovca70f002021-01-26 15:28:27 +00009011
Pavel Begunkov34343782021-02-10 11:45:42 +00009012 if (ctx->sq_data)
9013 io_sq_thread_unpark(ctx->sq_data);
Pavel Begunkovca70f002021-01-26 15:28:27 +00009014 prepare_to_wait(&task->io_uring->wait, &wait,
9015 TASK_UNINTERRUPTIBLE);
9016 if (inflight == io_uring_count_inflight(ctx, task, files))
9017 schedule();
Pavel Begunkovc98de082020-11-15 12:56:32 +00009018 finish_wait(&task->io_uring->wait, &wait);
Pavel Begunkov34343782021-02-10 11:45:42 +00009019 if (ctx->sq_data)
9020 io_sq_thread_park(ctx->sq_data);
Jens Axboe0f212202020-09-13 13:09:39 -06009021 }
Jens Axboe0f212202020-09-13 13:09:39 -06009022}
9023
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009024static void io_disable_sqo_submit(struct io_ring_ctx *ctx)
9025{
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009026 mutex_lock(&ctx->uring_lock);
9027 ctx->sqo_dead = 1;
9028 mutex_unlock(&ctx->uring_lock);
9029
9030 /* make sure callers enter the ring to get error */
Pavel Begunkovb4411612021-01-13 12:42:24 +00009031 if (ctx->rings)
9032 io_ring_set_wakeup_flag(ctx);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009033}
9034
Jens Axboe0f212202020-09-13 13:09:39 -06009035/*
9036 * We need to iteratively cancel requests, in case a request has dependent
9037 * hard links. These persist even for failure of cancelations, hence keep
9038 * looping until none are found.
9039 */
9040static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
9041 struct files_struct *files)
9042{
9043 struct task_struct *task = current;
9044
Jens Axboefdaf0832020-10-30 09:37:30 -06009045 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009046 io_disable_sqo_submit(ctx);
Jens Axboe534ca6d2020-09-02 13:52:19 -06009047 task = ctx->sq_data->thread;
Jens Axboefdaf0832020-10-30 09:37:30 -06009048 atomic_inc(&task->io_uring->in_idle);
9049 io_sq_thread_park(ctx->sq_data);
9050 }
Jens Axboe0f212202020-09-13 13:09:39 -06009051
Pavel Begunkovdf9923f2020-11-06 13:00:23 +00009052 io_cancel_defer_files(ctx, task, files);
Jens Axboe0f212202020-09-13 13:09:39 -06009053
Pavel Begunkov3a7efd12021-01-28 23:23:42 +00009054 io_uring_cancel_files(ctx, task, files);
Pavel Begunkovb52fda02020-11-06 13:00:24 +00009055 if (!files)
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009056 io_uring_try_cancel_requests(ctx, task, NULL);
Jens Axboefdaf0832020-10-30 09:37:30 -06009057
9058 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
9059 atomic_dec(&task->io_uring->in_idle);
Jens Axboefdaf0832020-10-30 09:37:30 -06009060 io_sq_thread_unpark(ctx->sq_data);
9061 }
Jens Axboe0f212202020-09-13 13:09:39 -06009062}
9063
9064/*
9065 * Note that this task has used io_uring. We use it for cancelation purposes.
9066 */
Jens Axboefdaf0832020-10-30 09:37:30 -06009067static int io_uring_add_task_file(struct io_ring_ctx *ctx, struct file *file)
Jens Axboe0f212202020-09-13 13:09:39 -06009068{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009069 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkova528b042020-12-21 18:34:04 +00009070 int ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009071
9072 if (unlikely(!tctx)) {
Jens Axboe0f212202020-09-13 13:09:39 -06009073 ret = io_uring_alloc_task_context(current);
9074 if (unlikely(ret))
9075 return ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009076 tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06009077 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009078 if (tctx->last != file) {
9079 void *old = xa_load(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06009080
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009081 if (!old) {
Jens Axboe0f212202020-09-13 13:09:39 -06009082 get_file(file);
Pavel Begunkova528b042020-12-21 18:34:04 +00009083 ret = xa_err(xa_store(&tctx->xa, (unsigned long)file,
9084 file, GFP_KERNEL));
9085 if (ret) {
9086 fput(file);
9087 return ret;
9088 }
Pavel Begunkovecfc8492021-01-25 11:42:20 +00009089
9090 /* one and only SQPOLL file note, held by sqo_task */
9091 WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) &&
9092 current != ctx->sqo_task);
Jens Axboe0f212202020-09-13 13:09:39 -06009093 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009094 tctx->last = file;
Jens Axboe0f212202020-09-13 13:09:39 -06009095 }
9096
Jens Axboefdaf0832020-10-30 09:37:30 -06009097 /*
9098 * This is race safe in that the task itself is doing this, hence it
9099 * cannot be going through the exit/cancel paths at the same time.
9100 * This cannot be modified while exit/cancel is running.
9101 */
9102 if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL))
9103 tctx->sqpoll = true;
9104
Jens Axboe0f212202020-09-13 13:09:39 -06009105 return 0;
9106}
9107
9108/*
9109 * Remove this io_uring_file -> task mapping.
9110 */
9111static void io_uring_del_task_file(struct file *file)
9112{
9113 struct io_uring_task *tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06009114
9115 if (tctx->last == file)
9116 tctx->last = NULL;
Matthew Wilcox (Oracle)5e2ed8c2020-10-09 13:49:53 +01009117 file = xa_erase(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06009118 if (file)
9119 fput(file);
9120}
9121
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009122static void io_uring_remove_task_files(struct io_uring_task *tctx)
9123{
9124 struct file *file;
9125 unsigned long index;
9126
9127 xa_for_each(&tctx->xa, index, file)
9128 io_uring_del_task_file(file);
9129}
9130
Jens Axboe0f212202020-09-13 13:09:39 -06009131void __io_uring_files_cancel(struct files_struct *files)
9132{
9133 struct io_uring_task *tctx = current->io_uring;
Matthew Wilcox (Oracle)ce765372020-10-09 13:49:51 +01009134 struct file *file;
9135 unsigned long index;
Jens Axboe0f212202020-09-13 13:09:39 -06009136
9137 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06009138 atomic_inc(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009139 xa_for_each(&tctx->xa, index, file)
9140 io_uring_cancel_task_requests(file->private_data, files);
Jens Axboefdaf0832020-10-30 09:37:30 -06009141 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009142
9143 if (files)
9144 io_uring_remove_task_files(tctx);
Jens Axboefdaf0832020-10-30 09:37:30 -06009145}
9146
9147static s64 tctx_inflight(struct io_uring_task *tctx)
9148{
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009149 return percpu_counter_sum(&tctx->inflight);
9150}
9151
9152static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx)
9153{
9154 struct io_uring_task *tctx;
Jens Axboefdaf0832020-10-30 09:37:30 -06009155 s64 inflight;
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009156 DEFINE_WAIT(wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06009157
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009158 if (!ctx->sq_data)
9159 return;
9160 tctx = ctx->sq_data->thread->io_uring;
9161 io_disable_sqo_submit(ctx);
Jens Axboefdaf0832020-10-30 09:37:30 -06009162
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009163 atomic_inc(&tctx->in_idle);
9164 do {
9165 /* read completions before cancelations */
9166 inflight = tctx_inflight(tctx);
9167 if (!inflight)
9168 break;
9169 io_uring_cancel_task_requests(ctx, NULL);
Jens Axboefdaf0832020-10-30 09:37:30 -06009170
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009171 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
9172 /*
9173 * If we've seen completions, retry without waiting. This
9174 * avoids a race where a completion comes in before we did
9175 * prepare_to_wait().
9176 */
9177 if (inflight == tctx_inflight(tctx))
9178 schedule();
9179 finish_wait(&tctx->wait, &wait);
9180 } while (1);
9181 atomic_dec(&tctx->in_idle);
Jens Axboe0f212202020-09-13 13:09:39 -06009182}
9183
Jens Axboe0f212202020-09-13 13:09:39 -06009184/*
9185 * Find any io_uring fd that this task has registered or done IO on, and cancel
9186 * requests.
9187 */
9188void __io_uring_task_cancel(void)
9189{
9190 struct io_uring_task *tctx = current->io_uring;
9191 DEFINE_WAIT(wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009192 s64 inflight;
Jens Axboe0f212202020-09-13 13:09:39 -06009193
9194 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06009195 atomic_inc(&tctx->in_idle);
Jens Axboe0f212202020-09-13 13:09:39 -06009196
Pavel Begunkov0b5cd6c2021-01-17 02:29:56 +00009197 /* trigger io_disable_sqo_submit() */
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009198 if (tctx->sqpoll) {
9199 struct file *file;
9200 unsigned long index;
9201
9202 xa_for_each(&tctx->xa, index, file)
9203 io_uring_cancel_sqpoll(file->private_data);
9204 }
Pavel Begunkov0b5cd6c2021-01-17 02:29:56 +00009205
Jens Axboed8a6df12020-10-15 16:24:45 -06009206 do {
Jens Axboe0f212202020-09-13 13:09:39 -06009207 /* read completions before cancelations */
Jens Axboefdaf0832020-10-30 09:37:30 -06009208 inflight = tctx_inflight(tctx);
Jens Axboed8a6df12020-10-15 16:24:45 -06009209 if (!inflight)
9210 break;
Jens Axboe0f212202020-09-13 13:09:39 -06009211 __io_uring_files_cancel(NULL);
9212
9213 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
9214
9215 /*
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009216 * If we've seen completions, retry without waiting. This
9217 * avoids a race where a completion comes in before we did
9218 * prepare_to_wait().
Jens Axboe0f212202020-09-13 13:09:39 -06009219 */
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009220 if (inflight == tctx_inflight(tctx))
9221 schedule();
Pavel Begunkovf57555e2020-12-20 13:21:44 +00009222 finish_wait(&tctx->wait, &wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009223 } while (1);
Jens Axboe0f212202020-09-13 13:09:39 -06009224
Jens Axboefdaf0832020-10-30 09:37:30 -06009225 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009226
9227 io_uring_remove_task_files(tctx);
Pavel Begunkov44e728b2020-06-15 10:24:04 +03009228}
9229
Jens Axboefcb323c2019-10-24 12:39:47 -06009230static int io_uring_flush(struct file *file, void *data)
9231{
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009232 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009233 struct io_ring_ctx *ctx = file->private_data;
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009234
Jens Axboe41be53e2021-02-13 09:11:04 -07009235 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
Jens Axboe84965ff2021-01-23 15:51:11 -07009236 io_uring_cancel_task_requests(ctx, NULL);
Jens Axboe41be53e2021-02-13 09:11:04 -07009237 io_req_caches_free(ctx, current);
9238 }
Jens Axboe84965ff2021-01-23 15:51:11 -07009239
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009240 if (!tctx)
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009241 return 0;
9242
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009243 /* we should have cancelled and erased it before PF_EXITING */
9244 WARN_ON_ONCE((current->flags & PF_EXITING) &&
9245 xa_load(&tctx->xa, (unsigned long)file));
9246
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009247 /*
9248 * fput() is pending, will be 2 if the only other ref is our potential
9249 * task file note. If the task is exiting, drop regardless of count.
9250 */
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009251 if (atomic_long_read(&file->f_count) != 2)
9252 return 0;
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009253
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009254 if (ctx->flags & IORING_SETUP_SQPOLL) {
9255 /* there is only one file note, which is owned by sqo_task */
Pavel Begunkov4325cb42021-01-16 05:32:30 +00009256 WARN_ON_ONCE(ctx->sqo_task != current &&
9257 xa_load(&tctx->xa, (unsigned long)file));
9258 /* sqo_dead check is for when this happens after cancellation */
9259 WARN_ON_ONCE(ctx->sqo_task == current && !ctx->sqo_dead &&
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009260 !xa_load(&tctx->xa, (unsigned long)file));
9261
9262 io_disable_sqo_submit(ctx);
9263 }
9264
9265 if (!(ctx->flags & IORING_SETUP_SQPOLL) || ctx->sqo_task == current)
9266 io_uring_del_task_file(file);
Jens Axboefcb323c2019-10-24 12:39:47 -06009267 return 0;
9268}
9269
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009270static void *io_uring_validate_mmap_request(struct file *file,
9271 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009272{
Jens Axboe2b188cc2019-01-07 10:46:33 -07009273 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009274 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009275 struct page *page;
9276 void *ptr;
9277
9278 switch (offset) {
9279 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00009280 case IORING_OFF_CQ_RING:
9281 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009282 break;
9283 case IORING_OFF_SQES:
9284 ptr = ctx->sq_sqes;
9285 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009286 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009287 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009288 }
9289
9290 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07009291 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009292 return ERR_PTR(-EINVAL);
9293
9294 return ptr;
9295}
9296
9297#ifdef CONFIG_MMU
9298
9299static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9300{
9301 size_t sz = vma->vm_end - vma->vm_start;
9302 unsigned long pfn;
9303 void *ptr;
9304
9305 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
9306 if (IS_ERR(ptr))
9307 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009308
9309 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
9310 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
9311}
9312
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009313#else /* !CONFIG_MMU */
9314
9315static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9316{
9317 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
9318}
9319
9320static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
9321{
9322 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
9323}
9324
9325static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
9326 unsigned long addr, unsigned long len,
9327 unsigned long pgoff, unsigned long flags)
9328{
9329 void *ptr;
9330
9331 ptr = io_uring_validate_mmap_request(file, pgoff, len);
9332 if (IS_ERR(ptr))
9333 return PTR_ERR(ptr);
9334
9335 return (unsigned long) ptr;
9336}
9337
9338#endif /* !CONFIG_MMU */
9339
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009340static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
Jens Axboe90554202020-09-03 12:12:41 -06009341{
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009342 int ret = 0;
Jens Axboe90554202020-09-03 12:12:41 -06009343 DEFINE_WAIT(wait);
9344
9345 do {
9346 if (!io_sqring_full(ctx))
9347 break;
9348
9349 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
9350
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009351 if (unlikely(ctx->sqo_dead)) {
9352 ret = -EOWNERDEAD;
9353 goto out;
9354 }
9355
Jens Axboe90554202020-09-03 12:12:41 -06009356 if (!io_sqring_full(ctx))
9357 break;
9358
9359 schedule();
9360 } while (!signal_pending(current));
9361
9362 finish_wait(&ctx->sqo_sq_wait, &wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009363out:
9364 return ret;
Jens Axboe90554202020-09-03 12:12:41 -06009365}
9366
Hao Xuc73ebb62020-11-03 10:54:37 +08009367static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz,
9368 struct __kernel_timespec __user **ts,
9369 const sigset_t __user **sig)
9370{
9371 struct io_uring_getevents_arg arg;
9372
9373 /*
9374 * If EXT_ARG isn't set, then we have no timespec and the argp pointer
9375 * is just a pointer to the sigset_t.
9376 */
9377 if (!(flags & IORING_ENTER_EXT_ARG)) {
9378 *sig = (const sigset_t __user *) argp;
9379 *ts = NULL;
9380 return 0;
9381 }
9382
9383 /*
9384 * EXT_ARG is set - ensure we agree on the size of it and copy in our
9385 * timespec and sigset_t pointers if good.
9386 */
9387 if (*argsz != sizeof(arg))
9388 return -EINVAL;
9389 if (copy_from_user(&arg, argp, sizeof(arg)))
9390 return -EFAULT;
9391 *sig = u64_to_user_ptr(arg.sigmask);
9392 *argsz = arg.sigmask_sz;
9393 *ts = u64_to_user_ptr(arg.ts);
9394 return 0;
9395}
9396
Jens Axboe2b188cc2019-01-07 10:46:33 -07009397SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
Hao Xuc73ebb62020-11-03 10:54:37 +08009398 u32, min_complete, u32, flags, const void __user *, argp,
9399 size_t, argsz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009400{
9401 struct io_ring_ctx *ctx;
9402 long ret = -EBADF;
9403 int submitted = 0;
9404 struct fd f;
9405
Jens Axboe4c6e2772020-07-01 11:29:10 -06009406 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07009407
Jens Axboe90554202020-09-03 12:12:41 -06009408 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
Hao Xuc73ebb62020-11-03 10:54:37 +08009409 IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009410 return -EINVAL;
9411
9412 f = fdget(fd);
9413 if (!f.file)
9414 return -EBADF;
9415
9416 ret = -EOPNOTSUPP;
9417 if (f.file->f_op != &io_uring_fops)
9418 goto out_fput;
9419
9420 ret = -ENXIO;
9421 ctx = f.file->private_data;
9422 if (!percpu_ref_tryget(&ctx->refs))
9423 goto out_fput;
9424
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009425 ret = -EBADFD;
9426 if (ctx->flags & IORING_SETUP_R_DISABLED)
9427 goto out;
9428
Jens Axboe6c271ce2019-01-10 11:22:30 -07009429 /*
9430 * For SQ polling, the thread will do all submissions and completions.
9431 * Just return the requested submit count, and wake the thread if
9432 * we were asked to.
9433 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009434 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009435 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00009436 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Pavel Begunkov89448c42020-12-17 00:24:39 +00009437
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009438 ret = -EOWNERDEAD;
9439 if (unlikely(ctx->sqo_dead))
9440 goto out;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009441 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06009442 wake_up(&ctx->sq_data->wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009443 if (flags & IORING_ENTER_SQ_WAIT) {
9444 ret = io_sqpoll_wait_sq(ctx);
9445 if (ret)
9446 goto out;
9447 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009448 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009449 } else if (to_submit) {
Jens Axboefdaf0832020-10-30 09:37:30 -06009450 ret = io_uring_add_task_file(ctx, f.file);
Jens Axboe0f212202020-09-13 13:09:39 -06009451 if (unlikely(ret))
9452 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009453 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009454 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009455 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009456
9457 if (submitted != to_submit)
9458 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009459 }
9460 if (flags & IORING_ENTER_GETEVENTS) {
Hao Xuc73ebb62020-11-03 10:54:37 +08009461 const sigset_t __user *sig;
9462 struct __kernel_timespec __user *ts;
9463
9464 ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig);
9465 if (unlikely(ret))
9466 goto out;
9467
Jens Axboe2b188cc2019-01-07 10:46:33 -07009468 min_complete = min(min_complete, ctx->cq_entries);
9469
Xiaoguang Wang32b22442020-03-11 09:26:09 +08009470 /*
9471 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
9472 * space applications don't need to do io completion events
9473 * polling again, they can rely on io_sq_thread to do polling
9474 * work, which can reduce cpu usage and uring_lock contention.
9475 */
9476 if (ctx->flags & IORING_SETUP_IOPOLL &&
9477 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03009478 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07009479 } else {
Hao Xuc73ebb62020-11-03 10:54:37 +08009480 ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts);
Jens Axboedef596e2019-01-09 08:59:42 -07009481 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009482 }
9483
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009484out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03009485 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009486out_fput:
9487 fdput(f);
9488 return submitted ? submitted : ret;
9489}
9490
Tobias Klauserbebdb652020-02-26 18:38:32 +01009491#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009492static int io_uring_show_cred(int id, void *p, void *data)
9493{
Jens Axboe6b47ab82020-11-05 09:50:16 -07009494 struct io_identity *iod = p;
9495 const struct cred *cred = iod->creds;
Jens Axboe87ce9552020-01-30 08:25:34 -07009496 struct seq_file *m = data;
9497 struct user_namespace *uns = seq_user_ns(m);
9498 struct group_info *gi;
9499 kernel_cap_t cap;
9500 unsigned __capi;
9501 int g;
9502
9503 seq_printf(m, "%5d\n", id);
9504 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
9505 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
9506 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
9507 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
9508 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
9509 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
9510 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
9511 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
9512 seq_puts(m, "\n\tGroups:\t");
9513 gi = cred->group_info;
9514 for (g = 0; g < gi->ngroups; g++) {
9515 seq_put_decimal_ull(m, g ? " " : "",
9516 from_kgid_munged(uns, gi->gid[g]));
9517 }
9518 seq_puts(m, "\n\tCapEff:\t");
9519 cap = cred->cap_effective;
9520 CAP_FOR_EACH_U32(__capi)
9521 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
9522 seq_putc(m, '\n');
9523 return 0;
9524}
9525
9526static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
9527{
Joseph Qidbbe9c62020-09-29 09:01:22 -06009528 struct io_sq_data *sq = NULL;
Jens Axboefad8e0d2020-09-28 08:57:48 -06009529 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07009530 int i;
9531
Jens Axboefad8e0d2020-09-28 08:57:48 -06009532 /*
9533 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
9534 * since fdinfo case grabs it in the opposite direction of normal use
9535 * cases. If we fail to get the lock, we just don't iterate any
9536 * structures that could be going away outside the io_uring mutex.
9537 */
9538 has_lock = mutex_trylock(&ctx->uring_lock);
9539
Joseph Qidbbe9c62020-09-29 09:01:22 -06009540 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL))
9541 sq = ctx->sq_data;
9542
9543 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
9544 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -07009545 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009546 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Pavel Begunkovea64ec022021-02-04 13:52:07 +00009547 struct file *f = *io_fixed_file_slot(ctx->file_data, i);
Jens Axboe87ce9552020-01-30 08:25:34 -07009548
Jens Axboe87ce9552020-01-30 08:25:34 -07009549 if (f)
9550 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
9551 else
9552 seq_printf(m, "%5u: <none>\n", i);
9553 }
9554 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009555 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009556 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
9557
9558 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
9559 (unsigned int) buf->len);
9560 }
Jens Axboefad8e0d2020-09-28 08:57:48 -06009561 if (has_lock && !idr_is_empty(&ctx->personality_idr)) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009562 seq_printf(m, "Personalities:\n");
9563 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
9564 }
Jens Axboed7718a92020-02-14 22:23:12 -07009565 seq_printf(m, "PollList:\n");
9566 spin_lock_irq(&ctx->completion_lock);
9567 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
9568 struct hlist_head *list = &ctx->cancel_hash[i];
9569 struct io_kiocb *req;
9570
9571 hlist_for_each_entry(req, list, hash_node)
9572 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
9573 req->task->task_works != NULL);
9574 }
9575 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009576 if (has_lock)
9577 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07009578}
9579
9580static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
9581{
9582 struct io_ring_ctx *ctx = f->private_data;
9583
9584 if (percpu_ref_tryget(&ctx->refs)) {
9585 __io_uring_show_fdinfo(ctx, m);
9586 percpu_ref_put(&ctx->refs);
9587 }
9588}
Tobias Klauserbebdb652020-02-26 18:38:32 +01009589#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07009590
Jens Axboe2b188cc2019-01-07 10:46:33 -07009591static const struct file_operations io_uring_fops = {
9592 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06009593 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009594 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009595#ifndef CONFIG_MMU
9596 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
9597 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
9598#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009599 .poll = io_uring_poll,
9600 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009601#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009602 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009603#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009604};
9605
9606static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
9607 struct io_uring_params *p)
9608{
Hristo Venev75b28af2019-08-26 17:23:46 +00009609 struct io_rings *rings;
9610 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009611
Jens Axboebd740482020-08-05 12:58:23 -06009612 /* make sure these are sane, as we already accounted them */
9613 ctx->sq_entries = p->sq_entries;
9614 ctx->cq_entries = p->cq_entries;
9615
Hristo Venev75b28af2019-08-26 17:23:46 +00009616 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
9617 if (size == SIZE_MAX)
9618 return -EOVERFLOW;
9619
9620 rings = io_mem_alloc(size);
9621 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009622 return -ENOMEM;
9623
Hristo Venev75b28af2019-08-26 17:23:46 +00009624 ctx->rings = rings;
9625 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
9626 rings->sq_ring_mask = p->sq_entries - 1;
9627 rings->cq_ring_mask = p->cq_entries - 1;
9628 rings->sq_ring_entries = p->sq_entries;
9629 rings->cq_ring_entries = p->cq_entries;
9630 ctx->sq_mask = rings->sq_ring_mask;
9631 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009632
9633 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07009634 if (size == SIZE_MAX) {
9635 io_mem_free(ctx->rings);
9636 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009637 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07009638 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009639
9640 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07009641 if (!ctx->sq_sqes) {
9642 io_mem_free(ctx->rings);
9643 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009644 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07009645 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009646
Jens Axboe2b188cc2019-01-07 10:46:33 -07009647 return 0;
9648}
9649
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009650static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file)
9651{
9652 int ret, fd;
9653
9654 fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
9655 if (fd < 0)
9656 return fd;
9657
9658 ret = io_uring_add_task_file(ctx, file);
9659 if (ret) {
9660 put_unused_fd(fd);
9661 return ret;
9662 }
9663 fd_install(fd, file);
9664 return fd;
9665}
9666
Jens Axboe2b188cc2019-01-07 10:46:33 -07009667/*
9668 * Allocate an anonymous fd, this is what constitutes the application
9669 * visible backing of an io_uring instance. The application mmaps this
9670 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
9671 * we have to tie this fd to a socket for file garbage collection purposes.
9672 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009673static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009674{
9675 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009676#if defined(CONFIG_UNIX)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009677 int ret;
9678
Jens Axboe2b188cc2019-01-07 10:46:33 -07009679 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
9680 &ctx->ring_sock);
9681 if (ret)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009682 return ERR_PTR(ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009683#endif
9684
Jens Axboe2b188cc2019-01-07 10:46:33 -07009685 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
9686 O_RDWR | O_CLOEXEC);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009687#if defined(CONFIG_UNIX)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009688 if (IS_ERR(file)) {
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009689 sock_release(ctx->ring_sock);
9690 ctx->ring_sock = NULL;
9691 } else {
9692 ctx->ring_sock->file = file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009693 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009694#endif
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009695 return file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009696}
9697
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009698static int io_uring_create(unsigned entries, struct io_uring_params *p,
9699 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009700{
9701 struct user_struct *user = NULL;
9702 struct io_ring_ctx *ctx;
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009703 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009704 int ret;
9705
Jens Axboe8110c1a2019-12-28 15:39:54 -07009706 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009707 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009708 if (entries > IORING_MAX_ENTRIES) {
9709 if (!(p->flags & IORING_SETUP_CLAMP))
9710 return -EINVAL;
9711 entries = IORING_MAX_ENTRIES;
9712 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009713
9714 /*
9715 * Use twice as many entries for the CQ ring. It's possible for the
9716 * application to drive a higher depth than the size of the SQ ring,
9717 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06009718 * some flexibility in overcommitting a bit. If the application has
9719 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
9720 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07009721 */
9722 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06009723 if (p->flags & IORING_SETUP_CQSIZE) {
9724 /*
9725 * If IORING_SETUP_CQSIZE is set, we do the same roundup
9726 * to a power-of-two, if it isn't already. We do NOT impose
9727 * any cq vs sq ring sizing.
9728 */
Joseph Qieb2667b32020-11-24 15:03:03 +08009729 if (!p->cq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06009730 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009731 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
9732 if (!(p->flags & IORING_SETUP_CLAMP))
9733 return -EINVAL;
9734 p->cq_entries = IORING_MAX_CQ_ENTRIES;
9735 }
Joseph Qieb2667b32020-11-24 15:03:03 +08009736 p->cq_entries = roundup_pow_of_two(p->cq_entries);
9737 if (p->cq_entries < p->sq_entries)
9738 return -EINVAL;
Jens Axboe33a107f2019-10-04 12:10:03 -06009739 } else {
9740 p->cq_entries = 2 * p->sq_entries;
9741 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009742
9743 user = get_uid(current_user());
Jens Axboe2b188cc2019-01-07 10:46:33 -07009744
9745 ctx = io_ring_ctx_alloc(p);
9746 if (!ctx) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07009747 free_uid(user);
9748 return -ENOMEM;
9749 }
9750 ctx->compat = in_compat_syscall();
Jens Axboe26bfa89e2021-02-09 20:14:12 -07009751 ctx->limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009752 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07009753 ctx->creds = get_current_cred();
Jens Axboe4ea33a92020-10-15 13:46:44 -06009754#ifdef CONFIG_AUDIT
9755 ctx->loginuid = current->loginuid;
9756 ctx->sessionid = current->sessionid;
9757#endif
Jens Axboe2aede0e2020-09-14 10:45:53 -06009758 ctx->sqo_task = get_task_struct(current);
9759
9760 /*
9761 * This is just grabbed for accounting purposes. When a process exits,
9762 * the mm is exited and dropped before the files, hence we need to hang
9763 * on to this mm purely for the purposes of being able to unaccount
9764 * memory (locked/pinned vm). It's not used for anything else.
9765 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06009766 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009767 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06009768
Dennis Zhou91d8f512020-09-16 13:41:05 -07009769#ifdef CONFIG_BLK_CGROUP
9770 /*
9771 * The sq thread will belong to the original cgroup it was inited in.
9772 * If the cgroup goes offline (e.g. disabling the io controller), then
9773 * issued bios will be associated with the closest cgroup later in the
9774 * block layer.
9775 */
9776 rcu_read_lock();
9777 ctx->sqo_blkcg_css = blkcg_css();
9778 ret = css_tryget_online(ctx->sqo_blkcg_css);
9779 rcu_read_unlock();
9780 if (!ret) {
9781 /* don't init against a dying cgroup, have the user try again */
9782 ctx->sqo_blkcg_css = NULL;
9783 ret = -ENODEV;
9784 goto err;
9785 }
9786#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009787 ret = io_allocate_scq_urings(ctx, p);
9788 if (ret)
9789 goto err;
9790
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009791 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009792 if (ret)
9793 goto err;
9794
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009795 if (!(p->flags & IORING_SETUP_R_DISABLED))
9796 io_sq_offload_start(ctx);
9797
Jens Axboe2b188cc2019-01-07 10:46:33 -07009798 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009799 p->sq_off.head = offsetof(struct io_rings, sq.head);
9800 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
9801 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
9802 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
9803 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
9804 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
9805 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009806
9807 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009808 p->cq_off.head = offsetof(struct io_rings, cq.head);
9809 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
9810 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
9811 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
9812 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
9813 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02009814 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06009815
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009816 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9817 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08009818 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
Hao Xuc73ebb62020-11-03 10:54:37 +08009819 IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED |
9820 IORING_FEAT_EXT_ARG;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009821
9822 if (copy_to_user(params, p, sizeof(*p))) {
9823 ret = -EFAULT;
9824 goto err;
9825 }
Jens Axboed1719f72020-07-30 13:43:53 -06009826
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009827 file = io_uring_get_file(ctx);
9828 if (IS_ERR(file)) {
9829 ret = PTR_ERR(file);
9830 goto err;
9831 }
9832
Jens Axboed1719f72020-07-30 13:43:53 -06009833 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06009834 * Install ring fd as the very last thing, so we don't risk someone
9835 * having closed it before we finish setup
9836 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009837 ret = io_uring_install_fd(ctx, file);
9838 if (ret < 0) {
Pavel Begunkov06585c42021-01-13 12:42:25 +00009839 io_disable_sqo_submit(ctx);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009840 /* fput will clean it up */
9841 fput(file);
9842 return ret;
9843 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06009844
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009845 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009846 return ret;
9847err:
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009848 io_disable_sqo_submit(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009849 io_ring_ctx_wait_and_kill(ctx);
9850 return ret;
9851}
9852
9853/*
9854 * Sets up an aio uring context, and returns the fd. Applications asks for a
9855 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9856 * params structure passed in.
9857 */
9858static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9859{
9860 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009861 int i;
9862
9863 if (copy_from_user(&p, params, sizeof(p)))
9864 return -EFAULT;
9865 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9866 if (p.resv[i])
9867 return -EINVAL;
9868 }
9869
Jens Axboe6c271ce2019-01-10 11:22:30 -07009870 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07009871 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009872 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
9873 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009874 return -EINVAL;
9875
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009876 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009877}
9878
9879SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9880 struct io_uring_params __user *, params)
9881{
9882 return io_uring_setup(entries, params);
9883}
9884
Jens Axboe66f4af92020-01-16 15:36:52 -07009885static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9886{
9887 struct io_uring_probe *p;
9888 size_t size;
9889 int i, ret;
9890
9891 size = struct_size(p, ops, nr_args);
9892 if (size == SIZE_MAX)
9893 return -EOVERFLOW;
9894 p = kzalloc(size, GFP_KERNEL);
9895 if (!p)
9896 return -ENOMEM;
9897
9898 ret = -EFAULT;
9899 if (copy_from_user(p, arg, size))
9900 goto out;
9901 ret = -EINVAL;
9902 if (memchr_inv(p, 0, size))
9903 goto out;
9904
9905 p->last_op = IORING_OP_LAST - 1;
9906 if (nr_args > IORING_OP_LAST)
9907 nr_args = IORING_OP_LAST;
9908
9909 for (i = 0; i < nr_args; i++) {
9910 p->ops[i].op = i;
9911 if (!io_op_defs[i].not_supported)
9912 p->ops[i].flags = IO_URING_OP_SUPPORTED;
9913 }
9914 p->ops_len = i;
9915
9916 ret = 0;
9917 if (copy_to_user(arg, p, size))
9918 ret = -EFAULT;
9919out:
9920 kfree(p);
9921 return ret;
9922}
9923
Jens Axboe071698e2020-01-28 10:04:42 -07009924static int io_register_personality(struct io_ring_ctx *ctx)
9925{
Jens Axboe1e6fa522020-10-15 08:46:24 -06009926 struct io_identity *id;
9927 int ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009928
Jens Axboe1e6fa522020-10-15 08:46:24 -06009929 id = kmalloc(sizeof(*id), GFP_KERNEL);
9930 if (unlikely(!id))
9931 return -ENOMEM;
9932
9933 io_init_identity(id);
9934 id->creds = get_current_cred();
9935
9936 ret = idr_alloc_cyclic(&ctx->personality_idr, id, 1, USHRT_MAX, GFP_KERNEL);
9937 if (ret < 0) {
9938 put_cred(id->creds);
9939 kfree(id);
9940 }
9941 return ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009942}
9943
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009944static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
9945 unsigned int nr_args)
9946{
9947 struct io_uring_restriction *res;
9948 size_t size;
9949 int i, ret;
9950
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009951 /* Restrictions allowed only if rings started disabled */
9952 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9953 return -EBADFD;
9954
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009955 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009956 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009957 return -EBUSY;
9958
9959 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
9960 return -EINVAL;
9961
9962 size = array_size(nr_args, sizeof(*res));
9963 if (size == SIZE_MAX)
9964 return -EOVERFLOW;
9965
9966 res = memdup_user(arg, size);
9967 if (IS_ERR(res))
9968 return PTR_ERR(res);
9969
9970 ret = 0;
9971
9972 for (i = 0; i < nr_args; i++) {
9973 switch (res[i].opcode) {
9974 case IORING_RESTRICTION_REGISTER_OP:
9975 if (res[i].register_op >= IORING_REGISTER_LAST) {
9976 ret = -EINVAL;
9977 goto out;
9978 }
9979
9980 __set_bit(res[i].register_op,
9981 ctx->restrictions.register_op);
9982 break;
9983 case IORING_RESTRICTION_SQE_OP:
9984 if (res[i].sqe_op >= IORING_OP_LAST) {
9985 ret = -EINVAL;
9986 goto out;
9987 }
9988
9989 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
9990 break;
9991 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
9992 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
9993 break;
9994 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
9995 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
9996 break;
9997 default:
9998 ret = -EINVAL;
9999 goto out;
10000 }
10001 }
10002
10003out:
10004 /* Reset all restrictions if an error happened */
10005 if (ret != 0)
10006 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
10007 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010008 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010009
10010 kfree(res);
10011 return ret;
10012}
10013
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010014static int io_register_enable_rings(struct io_ring_ctx *ctx)
10015{
10016 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
10017 return -EBADFD;
10018
10019 if (ctx->restrictions.registered)
10020 ctx->restricted = 1;
10021
10022 ctx->flags &= ~IORING_SETUP_R_DISABLED;
10023
10024 io_sq_offload_start(ctx);
10025
10026 return 0;
10027}
10028
Jens Axboe071698e2020-01-28 10:04:42 -070010029static bool io_register_op_must_quiesce(int op)
10030{
10031 switch (op) {
10032 case IORING_UNREGISTER_FILES:
10033 case IORING_REGISTER_FILES_UPDATE:
10034 case IORING_REGISTER_PROBE:
10035 case IORING_REGISTER_PERSONALITY:
10036 case IORING_UNREGISTER_PERSONALITY:
10037 return false;
10038 default:
10039 return true;
10040 }
10041}
10042
Jens Axboeedafcce2019-01-09 09:16:05 -070010043static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
10044 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -060010045 __releases(ctx->uring_lock)
10046 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -070010047{
10048 int ret;
10049
Jens Axboe35fa71a2019-04-22 10:23:23 -060010050 /*
10051 * We're inside the ring mutex, if the ref is already dying, then
10052 * someone else killed the ctx or is already going through
10053 * io_uring_register().
10054 */
10055 if (percpu_ref_is_dying(&ctx->refs))
10056 return -ENXIO;
10057
Jens Axboe071698e2020-01-28 10:04:42 -070010058 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -070010059 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -060010060
Jens Axboe05f3fb32019-12-09 11:22:50 -070010061 /*
10062 * Drop uring mutex before waiting for references to exit. If
10063 * another thread is currently inside io_uring_enter() it might
10064 * need to grab the uring_lock to make progress. If we hold it
10065 * here across the drain wait, then we can deadlock. It's safe
10066 * to drop the mutex here, since no new references will come in
10067 * after we've killed the percpu ref.
10068 */
10069 mutex_unlock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -060010070 do {
10071 ret = wait_for_completion_interruptible(&ctx->ref_comp);
10072 if (!ret)
10073 break;
Jens Axboeed6930c2020-10-08 19:09:46 -060010074 ret = io_run_task_work_sig();
10075 if (ret < 0)
10076 break;
Jens Axboeaf9c1a42020-09-24 13:32:18 -060010077 } while (1);
10078
Jens Axboe05f3fb32019-12-09 11:22:50 -070010079 mutex_lock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -060010080
Jens Axboec1503682020-01-08 08:26:07 -070010081 if (ret) {
10082 percpu_ref_resurrect(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010083 goto out_quiesce;
10084 }
10085 }
10086
10087 if (ctx->restricted) {
10088 if (opcode >= IORING_REGISTER_LAST) {
10089 ret = -EINVAL;
10090 goto out;
10091 }
10092
10093 if (!test_bit(opcode, ctx->restrictions.register_op)) {
10094 ret = -EACCES;
Jens Axboec1503682020-01-08 08:26:07 -070010095 goto out;
10096 }
Jens Axboe05f3fb32019-12-09 11:22:50 -070010097 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010098
10099 switch (opcode) {
10100 case IORING_REGISTER_BUFFERS:
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -080010101 ret = io_sqe_buffers_register(ctx, arg, nr_args);
Jens Axboeedafcce2019-01-09 09:16:05 -070010102 break;
10103 case IORING_UNREGISTER_BUFFERS:
10104 ret = -EINVAL;
10105 if (arg || nr_args)
10106 break;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -080010107 ret = io_sqe_buffers_unregister(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -070010108 break;
Jens Axboe6b063142019-01-10 22:13:58 -070010109 case IORING_REGISTER_FILES:
10110 ret = io_sqe_files_register(ctx, arg, nr_args);
10111 break;
10112 case IORING_UNREGISTER_FILES:
10113 ret = -EINVAL;
10114 if (arg || nr_args)
10115 break;
10116 ret = io_sqe_files_unregister(ctx);
10117 break;
Jens Axboec3a31e62019-10-03 13:59:56 -060010118 case IORING_REGISTER_FILES_UPDATE:
10119 ret = io_sqe_files_update(ctx, arg, nr_args);
10120 break;
Jens Axboe9b402842019-04-11 11:45:41 -060010121 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -070010122 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -060010123 ret = -EINVAL;
10124 if (nr_args != 1)
10125 break;
10126 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -070010127 if (ret)
10128 break;
10129 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
10130 ctx->eventfd_async = 1;
10131 else
10132 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -060010133 break;
10134 case IORING_UNREGISTER_EVENTFD:
10135 ret = -EINVAL;
10136 if (arg || nr_args)
10137 break;
10138 ret = io_eventfd_unregister(ctx);
10139 break;
Jens Axboe66f4af92020-01-16 15:36:52 -070010140 case IORING_REGISTER_PROBE:
10141 ret = -EINVAL;
10142 if (!arg || nr_args > 256)
10143 break;
10144 ret = io_probe(ctx, arg, nr_args);
10145 break;
Jens Axboe071698e2020-01-28 10:04:42 -070010146 case IORING_REGISTER_PERSONALITY:
10147 ret = -EINVAL;
10148 if (arg || nr_args)
10149 break;
10150 ret = io_register_personality(ctx);
10151 break;
10152 case IORING_UNREGISTER_PERSONALITY:
10153 ret = -EINVAL;
10154 if (arg)
10155 break;
10156 ret = io_unregister_personality(ctx, nr_args);
10157 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010158 case IORING_REGISTER_ENABLE_RINGS:
10159 ret = -EINVAL;
10160 if (arg || nr_args)
10161 break;
10162 ret = io_register_enable_rings(ctx);
10163 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010164 case IORING_REGISTER_RESTRICTIONS:
10165 ret = io_register_restrictions(ctx, arg, nr_args);
10166 break;
Jens Axboeedafcce2019-01-09 09:16:05 -070010167 default:
10168 ret = -EINVAL;
10169 break;
10170 }
10171
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010172out:
Jens Axboe071698e2020-01-28 10:04:42 -070010173 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -070010174 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -070010175 percpu_ref_reinit(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010176out_quiesce:
Jens Axboe0f158b42020-05-14 17:18:39 -060010177 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -070010178 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010179 return ret;
10180}
10181
10182SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
10183 void __user *, arg, unsigned int, nr_args)
10184{
10185 struct io_ring_ctx *ctx;
10186 long ret = -EBADF;
10187 struct fd f;
10188
10189 f = fdget(fd);
10190 if (!f.file)
10191 return -EBADF;
10192
10193 ret = -EOPNOTSUPP;
10194 if (f.file->f_op != &io_uring_fops)
10195 goto out_fput;
10196
10197 ctx = f.file->private_data;
10198
10199 mutex_lock(&ctx->uring_lock);
10200 ret = __io_uring_register(ctx, opcode, arg, nr_args);
10201 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020010202 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
10203 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -070010204out_fput:
10205 fdput(f);
10206 return ret;
10207}
10208
Jens Axboe2b188cc2019-01-07 10:46:33 -070010209static int __init io_uring_init(void)
10210{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010211#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
10212 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
10213 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
10214} while (0)
10215
10216#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
10217 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
10218 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
10219 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
10220 BUILD_BUG_SQE_ELEM(1, __u8, flags);
10221 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
10222 BUILD_BUG_SQE_ELEM(4, __s32, fd);
10223 BUILD_BUG_SQE_ELEM(8, __u64, off);
10224 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
10225 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010226 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010227 BUILD_BUG_SQE_ELEM(24, __u32, len);
10228 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
10229 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
10230 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
10231 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +080010232 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
10233 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010234 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
10235 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
10236 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
10237 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
10238 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
10239 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
10240 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
10241 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010242 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010243 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
10244 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
10245 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010246 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010247
Jens Axboed3656342019-12-18 09:50:26 -070010248 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -070010249 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe91f245d2021-02-09 13:48:50 -070010250 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC |
10251 SLAB_ACCOUNT);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010252 return 0;
10253};
10254__initcall(io_uring_init);