io_uring: simplify io_alloc_req()
[linux-2.6-block.git] / fs / io_uring.c
CommitLineData
2b188cc1
JA
1// 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
1e84b97b
SB
7 * 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.
2b188cc1
JA
29 *
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
c992fe29 40 * Copyright (c) 2018-2019 Christoph Hellwig
2b188cc1
JA
41 */
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>
52de1fe1 47#include <net/compat.h>
2b188cc1
JA
48#include <linux/refcount.h>
49#include <linux/uio.h>
6b47ee6e 50#include <linux/bits.h>
2b188cc1
JA
51
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>
2b188cc1
JA
58#include <linux/percpu.h>
59#include <linux/slab.h>
6c271ce2 60#include <linux/kthread.h>
2b188cc1 61#include <linux/blkdev.h>
edafccee 62#include <linux/bvec.h>
2b188cc1
JA
63#include <linux/net.h>
64#include <net/sock.h>
65#include <net/af_unix.h>
6b06314c 66#include <net/scm.h>
2b188cc1
JA
67#include <linux/anon_inodes.h>
68#include <linux/sched/mm.h>
69#include <linux/uaccess.h>
70#include <linux/nospec.h>
edafccee
JA
71#include <linux/sizes.h>
72#include <linux/hugetlb.h>
aa4c3967 73#include <linux/highmem.h>
15b71abe
JA
74#include <linux/namei.h>
75#include <linux/fsnotify.h>
4840e418 76#include <linux/fadvise.h>
3e4827b0 77#include <linux/eventpoll.h>
ff002b30 78#include <linux/fs_struct.h>
7d67af2c 79#include <linux/splice.h>
b41e9852 80#include <linux/task_work.h>
bcf5a063 81#include <linux/pagemap.h>
0f212204 82#include <linux/io_uring.h>
91d8f519 83#include <linux/blk-cgroup.h>
2b188cc1 84
c826bd7a
DD
85#define CREATE_TRACE_POINTS
86#include <trace/events/io_uring.h>
87
2b188cc1
JA
88#include <uapi/linux/io_uring.h>
89
90#include "internal.h"
561fb04a 91#include "io-wq.h"
2b188cc1 92
5277deaa 93#define IORING_MAX_ENTRIES 32768
33a107f0 94#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
65e19f54
JA
95
96/*
97 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
98 */
99#define IORING_FILE_TABLE_SHIFT 9
100#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
101#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
102#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
21b55dbc
SG
103#define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \
104 IORING_REGISTER_LAST + IORING_OP_LAST)
2b188cc1
JA
105
106struct io_uring {
107 u32 head ____cacheline_aligned_in_smp;
108 u32 tail ____cacheline_aligned_in_smp;
109};
110
1e84b97b 111/*
75b28aff
HV
112 * This data is shared with the application through the mmap at offsets
113 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
1e84b97b
SB
114 *
115 * The offsets to the member fields are published through struct
116 * io_sqring_offsets when calling io_uring_setup.
117 */
75b28aff 118struct io_rings {
1e84b97b
SB
119 /*
120 * Head and tail offsets into the ring; the offsets need to be
121 * masked to get valid indices.
122 *
75b28aff
HV
123 * The kernel controls head of the sq ring and the tail of the cq ring,
124 * and the application controls tail of the sq ring and the head of the
125 * cq ring.
1e84b97b 126 */
75b28aff 127 struct io_uring sq, cq;
1e84b97b 128 /*
75b28aff 129 * Bitmasks to apply to head and tail offsets (constant, equals
1e84b97b
SB
130 * ring_entries - 1)
131 */
75b28aff
HV
132 u32 sq_ring_mask, cq_ring_mask;
133 /* Ring sizes (constant, power of 2) */
134 u32 sq_ring_entries, cq_ring_entries;
1e84b97b
SB
135 /*
136 * Number of invalid entries dropped by the kernel due to
137 * invalid index stored in array
138 *
139 * Written by the kernel, shouldn't be modified by the
140 * application (i.e. get number of "new events" by comparing to
141 * cached value).
142 *
143 * After a new SQ head value was read by the application this
144 * counter includes all submissions that were dropped reaching
145 * the new SQ head (and possibly more).
146 */
75b28aff 147 u32 sq_dropped;
1e84b97b 148 /*
0d9b5b3a 149 * Runtime SQ flags
1e84b97b
SB
150 *
151 * Written by the kernel, shouldn't be modified by the
152 * application.
153 *
154 * The application needs a full memory barrier before checking
155 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
156 */
75b28aff 157 u32 sq_flags;
0d9b5b3a
SG
158 /*
159 * Runtime CQ flags
160 *
161 * Written by the application, shouldn't be modified by the
162 * kernel.
163 */
164 u32 cq_flags;
1e84b97b
SB
165 /*
166 * Number of completion events lost because the queue was full;
167 * this should be avoided by the application by making sure
0b4295b5 168 * there are not more requests pending than there is space in
1e84b97b
SB
169 * the completion queue.
170 *
171 * Written by the kernel, shouldn't be modified by the
172 * application (i.e. get number of "new events" by comparing to
173 * cached value).
174 *
175 * As completion events come in out of order this counter is not
176 * ordered with any other data.
177 */
75b28aff 178 u32 cq_overflow;
1e84b97b
SB
179 /*
180 * Ring buffer of completion events.
181 *
182 * The kernel writes completion events fresh every time they are
183 * produced, so the application is allowed to modify pending
184 * entries.
185 */
75b28aff 186 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
2b188cc1
JA
187};
188
edafccee
JA
189struct io_mapped_ubuf {
190 u64 ubuf;
191 size_t len;
192 struct bio_vec *bvec;
193 unsigned int nr_bvecs;
de293938 194 unsigned long acct_pages;
edafccee
JA
195};
196
65e19f54
JA
197struct fixed_file_table {
198 struct file **files;
31b51510
JA
199};
200
05589553
XW
201struct fixed_file_ref_node {
202 struct percpu_ref refs;
203 struct list_head node;
204 struct list_head file_list;
205 struct fixed_file_data *file_data;
4a38aed2 206 struct llist_node llist;
05589553
XW
207};
208
05f3fb3c
JA
209struct fixed_file_data {
210 struct fixed_file_table *table;
211 struct io_ring_ctx *ctx;
212
05589553 213 struct percpu_ref *cur_refs;
05f3fb3c 214 struct percpu_ref refs;
05f3fb3c 215 struct completion done;
05589553
XW
216 struct list_head ref_list;
217 spinlock_t lock;
05f3fb3c
JA
218};
219
5a2e745d
JA
220struct io_buffer {
221 struct list_head list;
222 __u64 addr;
223 __s32 len;
224 __u16 bid;
225};
226
21b55dbc
SG
227struct io_restriction {
228 DECLARE_BITMAP(register_op, IORING_REGISTER_LAST);
229 DECLARE_BITMAP(sqe_op, IORING_OP_LAST);
230 u8 sqe_flags_allowed;
231 u8 sqe_flags_required;
7e84e1c7 232 bool registered;
21b55dbc
SG
233};
234
534ca6d6
JA
235struct io_sq_data {
236 refcount_t refs;
69fb2131
JA
237 struct mutex lock;
238
239 /* ctx's that are using this sqd */
240 struct list_head ctx_list;
241 struct list_head ctx_new_list;
242 struct mutex ctx_lock;
243
534ca6d6
JA
244 struct task_struct *thread;
245 struct wait_queue_head wait;
246};
247
2b188cc1
JA
248struct io_ring_ctx {
249 struct {
250 struct percpu_ref refs;
251 } ____cacheline_aligned_in_smp;
252
253 struct {
254 unsigned int flags;
e1d85334 255 unsigned int compat: 1;
aad5d8da 256 unsigned int limit_mem: 1;
e1d85334
RD
257 unsigned int cq_overflow_flushed: 1;
258 unsigned int drain_next: 1;
259 unsigned int eventfd_async: 1;
21b55dbc 260 unsigned int restricted: 1;
2b188cc1 261
75b28aff
HV
262 /*
263 * Ring buffer of indices into array of io_uring_sqe, which is
264 * mmapped by the application using the IORING_OFF_SQES offset.
265 *
266 * This indirection could e.g. be used to assign fixed
267 * io_uring_sqe entries to operations and only submit them to
268 * the queue when needed.
269 *
270 * The kernel modifies neither the indices array nor the entries
271 * array.
272 */
273 u32 *sq_array;
2b188cc1
JA
274 unsigned cached_sq_head;
275 unsigned sq_entries;
276 unsigned sq_mask;
6c271ce2 277 unsigned sq_thread_idle;
498ccd9e 278 unsigned cached_sq_dropped;
206aefde 279 atomic_t cached_cq_overflow;
ad3eb2c8 280 unsigned long sq_check_overflow;
de0617e4
JA
281
282 struct list_head defer_list;
5262f567 283 struct list_head timeout_list;
1d7bb1d5 284 struct list_head cq_overflow_list;
fcb323cc
JA
285
286 wait_queue_head_t inflight_wait;
ad3eb2c8 287 struct io_uring_sqe *sq_sqes;
2b188cc1
JA
288 } ____cacheline_aligned_in_smp;
289
206aefde
JA
290 struct io_rings *rings;
291
2b188cc1 292 /* IO offload */
561fb04a 293 struct io_wq *io_wq;
2aede0e4
JA
294
295 /*
296 * For SQPOLL usage - we hold a reference to the parent task, so we
297 * have access to the ->files
298 */
299 struct task_struct *sqo_task;
300
301 /* Only used for accounting purposes */
302 struct mm_struct *mm_account;
303
91d8f519
DZ
304#ifdef CONFIG_BLK_CGROUP
305 struct cgroup_subsys_state *sqo_blkcg_css;
306#endif
307
534ca6d6
JA
308 struct io_sq_data *sq_data; /* if using sq thread polling */
309
90554200 310 struct wait_queue_head sqo_sq_wait;
6a779382 311 struct wait_queue_entry sqo_wait_entry;
69fb2131 312 struct list_head sqd_list;
75b28aff 313
6b06314c
JA
314 /*
315 * If used, fixed file set. Writers must ensure that ->refs is dead,
316 * readers must ensure that ->refs is alive as long as the file* is
317 * used. Only updated through io_uring_register(2).
318 */
05f3fb3c 319 struct fixed_file_data *file_data;
6b06314c
JA
320 unsigned nr_user_files;
321
edafccee
JA
322 /* if used, fixed mapped user buffers */
323 unsigned nr_user_bufs;
324 struct io_mapped_ubuf *user_bufs;
325
2b188cc1
JA
326 struct user_struct *user;
327
0b8c0ec7 328 const struct cred *creds;
181e448d 329
0f158b4c
JA
330 struct completion ref_comp;
331 struct completion sq_thread_comp;
206aefde 332
0ddf92e8
JA
333 /* if all else fails... */
334 struct io_kiocb *fallback_req;
335
206aefde
JA
336#if defined(CONFIG_UNIX)
337 struct socket *ring_sock;
338#endif
339
5a2e745d
JA
340 struct idr io_buffer_idr;
341
071698e1
JA
342 struct idr personality_idr;
343
206aefde
JA
344 struct {
345 unsigned cached_cq_tail;
346 unsigned cq_entries;
347 unsigned cq_mask;
348 atomic_t cq_timeouts;
ad3eb2c8 349 unsigned long cq_check_overflow;
206aefde
JA
350 struct wait_queue_head cq_wait;
351 struct fasync_struct *cq_fasync;
352 struct eventfd_ctx *cq_ev_fd;
353 } ____cacheline_aligned_in_smp;
2b188cc1
JA
354
355 struct {
356 struct mutex uring_lock;
357 wait_queue_head_t wait;
358 } ____cacheline_aligned_in_smp;
359
360 struct {
361 spinlock_t completion_lock;
e94f141b 362
def596e9 363 /*
540e32a0 364 * ->iopoll_list is protected by the ctx->uring_lock for
def596e9
JA
365 * io_uring instances that don't use IORING_SETUP_SQPOLL.
366 * For SQPOLL, only the single threaded io_sq_thread() will
367 * manipulate the list, hence no extra locking is needed there.
368 */
540e32a0 369 struct list_head iopoll_list;
78076bb6
JA
370 struct hlist_head *cancel_hash;
371 unsigned cancel_hash_bits;
e94f141b 372 bool poll_multi_file;
31b51510 373
fcb323cc
JA
374 spinlock_t inflight_lock;
375 struct list_head inflight_list;
2b188cc1 376 } ____cacheline_aligned_in_smp;
85faa7b8 377
4a38aed2
JA
378 struct delayed_work file_put_work;
379 struct llist_head file_put_llist;
380
85faa7b8 381 struct work_struct exit_work;
21b55dbc 382 struct io_restriction restrictions;
2b188cc1
JA
383};
384
09bb8394
JA
385/*
386 * First field must be the file pointer in all the
387 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
388 */
221c5eb2
JA
389struct io_poll_iocb {
390 struct file *file;
0969e783
JA
391 union {
392 struct wait_queue_head *head;
393 u64 addr;
394 };
221c5eb2 395 __poll_t events;
8c838788 396 bool done;
221c5eb2 397 bool canceled;
392edb45 398 struct wait_queue_entry wait;
221c5eb2
JA
399};
400
b5dba59e
JA
401struct io_close {
402 struct file *file;
403 struct file *put_file;
404 int fd;
405};
406
ad8a48ac
JA
407struct io_timeout_data {
408 struct io_kiocb *req;
409 struct hrtimer timer;
410 struct timespec64 ts;
411 enum hrtimer_mode mode;
412};
413
8ed8d3c3
JA
414struct io_accept {
415 struct file *file;
416 struct sockaddr __user *addr;
417 int __user *addr_len;
418 int flags;
09952e3e 419 unsigned long nofile;
8ed8d3c3
JA
420};
421
422struct io_sync {
423 struct file *file;
424 loff_t len;
425 loff_t off;
426 int flags;
d63d1b5e 427 int mode;
8ed8d3c3
JA
428};
429
fbf23849
JA
430struct io_cancel {
431 struct file *file;
432 u64 addr;
433};
434
b29472ee
JA
435struct io_timeout {
436 struct file *file;
437 u64 addr;
438 int flags;
bfe68a22
PB
439 u32 off;
440 u32 target_seq;
135fcde8 441 struct list_head list;
b29472ee
JA
442};
443
9adbd45d
JA
444struct io_rw {
445 /* NOTE: kiocb has the file as the first member, so don't do it here */
446 struct kiocb kiocb;
447 u64 addr;
448 u64 len;
449};
450
3fbb51c1
JA
451struct io_connect {
452 struct file *file;
453 struct sockaddr __user *addr;
454 int addr_len;
455};
456
e47293fd
JA
457struct io_sr_msg {
458 struct file *file;
fddaface 459 union {
270a5940 460 struct user_msghdr __user *umsg;
fddaface
JA
461 void __user *buf;
462 };
e47293fd 463 int msg_flags;
bcda7baa 464 int bgid;
fddaface 465 size_t len;
bcda7baa 466 struct io_buffer *kbuf;
e47293fd
JA
467};
468
15b71abe
JA
469struct io_open {
470 struct file *file;
471 int dfd;
15b71abe 472 struct filename *filename;
c12cedf2 473 struct open_how how;
4022e7af 474 unsigned long nofile;
15b71abe
JA
475};
476
05f3fb3c
JA
477struct io_files_update {
478 struct file *file;
479 u64 arg;
480 u32 nr_args;
481 u32 offset;
482};
483
4840e418
JA
484struct io_fadvise {
485 struct file *file;
486 u64 offset;
487 u32 len;
488 u32 advice;
489};
490
c1ca757b
JA
491struct io_madvise {
492 struct file *file;
493 u64 addr;
494 u32 len;
495 u32 advice;
496};
497
3e4827b0
JA
498struct io_epoll {
499 struct file *file;
500 int epfd;
501 int op;
502 int fd;
503 struct epoll_event event;
e47293fd
JA
504};
505
7d67af2c
PB
506struct io_splice {
507 struct file *file_out;
508 struct file *file_in;
509 loff_t off_out;
510 loff_t off_in;
511 u64 len;
512 unsigned int flags;
513};
514
ddf0322d
JA
515struct io_provide_buf {
516 struct file *file;
517 __u64 addr;
518 __s32 len;
519 __u32 bgid;
520 __u16 nbufs;
521 __u16 bid;
522};
523
1d9e1288
BM
524struct io_statx {
525 struct file *file;
526 int dfd;
527 unsigned int mask;
528 unsigned int flags;
e62753e4 529 const char __user *filename;
1d9e1288
BM
530 struct statx __user *buffer;
531};
532
3ca405eb
PB
533struct io_completion {
534 struct file *file;
535 struct list_head list;
0f7e466b 536 int cflags;
3ca405eb
PB
537};
538
f499a021
JA
539struct io_async_connect {
540 struct sockaddr_storage address;
541};
542
03b1230c
JA
543struct io_async_msghdr {
544 struct iovec fast_iov[UIO_FASTIOV];
545 struct iovec *iov;
546 struct sockaddr __user *uaddr;
547 struct msghdr msg;
b537916c 548 struct sockaddr_storage addr;
03b1230c
JA
549};
550
f67676d1
JA
551struct io_async_rw {
552 struct iovec fast_iov[UIO_FASTIOV];
ff6165b2
JA
553 const struct iovec *free_iovec;
554 struct iov_iter iter;
227c0c96 555 size_t bytes_done;
bcf5a063 556 struct wait_page_queue wpq;
f67676d1
JA
557};
558
6b47ee6e
PB
559enum {
560 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
561 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
562 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
563 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
564 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
bcda7baa 565 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
6b47ee6e 566
dea3b49c 567 REQ_F_LINK_HEAD_BIT,
6b47ee6e
PB
568 REQ_F_FAIL_LINK_BIT,
569 REQ_F_INFLIGHT_BIT,
570 REQ_F_CUR_POS_BIT,
571 REQ_F_NOWAIT_BIT,
6b47ee6e 572 REQ_F_LINK_TIMEOUT_BIT,
6b47ee6e 573 REQ_F_ISREG_BIT,
6b47ee6e 574 REQ_F_COMP_LOCKED_BIT,
99bc4c38 575 REQ_F_NEED_CLEANUP_BIT,
d7718a9d 576 REQ_F_POLLED_BIT,
bcda7baa 577 REQ_F_BUFFER_SELECTED_BIT,
5b0bbee4 578 REQ_F_NO_FILE_TABLE_BIT,
7cdaf587 579 REQ_F_WORK_INITIALIZED_BIT,
84557871
JA
580
581 /* not a real bit, just to check we're not overflowing the space */
582 __REQ_F_LAST_BIT,
6b47ee6e
PB
583};
584
585enum {
586 /* ctx owns file */
587 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
588 /* drain existing IO first */
589 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
590 /* linked sqes */
591 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
592 /* doesn't sever on completion < 0 */
593 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
594 /* IOSQE_ASYNC */
595 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
bcda7baa
JA
596 /* IOSQE_BUFFER_SELECT */
597 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
6b47ee6e 598
dea3b49c
PB
599 /* head of a link */
600 REQ_F_LINK_HEAD = BIT(REQ_F_LINK_HEAD_BIT),
6b47ee6e
PB
601 /* fail rest of links */
602 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
603 /* on inflight list */
604 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
605 /* read/write uses file position */
606 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
607 /* must not punt to workers */
608 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
6b47ee6e
PB
609 /* has linked timeout */
610 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
6b47ee6e
PB
611 /* regular file */
612 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
6b47ee6e
PB
613 /* completion under lock */
614 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
99bc4c38
PB
615 /* needs cleanup */
616 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
d7718a9d
JA
617 /* already went through poll handler */
618 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
bcda7baa
JA
619 /* buffer already selected */
620 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
5b0bbee4
JA
621 /* doesn't need file table for this request */
622 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
7cdaf587
XW
623 /* io_wq_work is initialized */
624 REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT),
d7718a9d
JA
625};
626
627struct async_poll {
628 struct io_poll_iocb poll;
807abcb0 629 struct io_poll_iocb *double_poll;
6b47ee6e
PB
630};
631
09bb8394
JA
632/*
633 * NOTE! Each of the iocb union members has the file pointer
634 * as the first entry in their struct definition. So you can
635 * access the file pointer through any of the sub-structs,
636 * or directly as just 'ki_filp' in this struct.
637 */
2b188cc1 638struct io_kiocb {
221c5eb2 639 union {
09bb8394 640 struct file *file;
9adbd45d 641 struct io_rw rw;
221c5eb2 642 struct io_poll_iocb poll;
8ed8d3c3
JA
643 struct io_accept accept;
644 struct io_sync sync;
fbf23849 645 struct io_cancel cancel;
b29472ee 646 struct io_timeout timeout;
3fbb51c1 647 struct io_connect connect;
e47293fd 648 struct io_sr_msg sr_msg;
15b71abe 649 struct io_open open;
b5dba59e 650 struct io_close close;
05f3fb3c 651 struct io_files_update files_update;
4840e418 652 struct io_fadvise fadvise;
c1ca757b 653 struct io_madvise madvise;
3e4827b0 654 struct io_epoll epoll;
7d67af2c 655 struct io_splice splice;
ddf0322d 656 struct io_provide_buf pbuf;
1d9e1288 657 struct io_statx statx;
3ca405eb
PB
658 /* use only after cleaning per-op data, see io_clean_op() */
659 struct io_completion compl;
221c5eb2 660 };
2b188cc1 661
e8c2bc1f
JA
662 /* opcode allocated if it needs to store data for async defer */
663 void *async_data;
d625c6ee 664 u8 opcode;
65a6543d
XW
665 /* polled IO has completed */
666 u8 iopoll_completed;
2b188cc1 667
4f4eeba8 668 u16 buf_index;
9cf7c104 669 u32 result;
4f4eeba8 670
010e8e6b
PB
671 struct io_ring_ctx *ctx;
672 unsigned int flags;
673 refcount_t refs;
674 struct task_struct *task;
675 u64 user_data;
d7718a9d 676
010e8e6b 677 struct list_head link_list;
fcb323cc 678
d21ffe7e
PB
679 /*
680 * 1. used with ctx->iopoll_list with reads/writes
681 * 2. to track reqs with ->files (see io_op_def::file_table)
682 */
010e8e6b
PB
683 struct list_head inflight_entry;
684
685 struct percpu_ref *fixed_file_refs;
686 struct callback_head task_work;
687 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
688 struct hlist_node hash_node;
689 struct async_poll *apoll;
690 struct io_wq_work work;
2b188cc1 691};
05589553 692
27dc8338
PB
693struct io_defer_entry {
694 struct list_head list;
695 struct io_kiocb *req;
9cf7c104 696 u32 seq;
2b188cc1
JA
697};
698
def596e9 699#define IO_IOPOLL_BATCH 8
2b188cc1 700
013538bd
JA
701struct io_comp_state {
702 unsigned int nr;
703 struct list_head list;
704 struct io_ring_ctx *ctx;
705};
706
9a56a232
JA
707struct io_submit_state {
708 struct blk_plug plug;
709
2579f913
JA
710 /*
711 * io_kiocb alloc cache
712 */
713 void *reqs[IO_IOPOLL_BATCH];
6c8a3134 714 unsigned int free_reqs;
2579f913 715
013538bd
JA
716 /*
717 * Batch completion logic
718 */
719 struct io_comp_state comp;
720
9a56a232
JA
721 /*
722 * File reference cache
723 */
724 struct file *file;
725 unsigned int fd;
726 unsigned int has_refs;
9a56a232
JA
727 unsigned int ios_left;
728};
729
d3656344 730struct io_op_def {
d3656344
JA
731 /* needs current->mm setup, does mm access */
732 unsigned needs_mm : 1;
733 /* needs req->file assigned */
734 unsigned needs_file : 1;
fd2206e4
JA
735 /* don't fail if file grab fails */
736 unsigned needs_file_no_error : 1;
d3656344
JA
737 /* hash wq insertion if file is a regular file */
738 unsigned hash_reg_file : 1;
739 /* unbound wq insertion if file is a non-regular file */
740 unsigned unbound_nonreg_file : 1;
66f4af93
JA
741 /* opcode is not supported by this kernel */
742 unsigned not_supported : 1;
f86cd20c
JA
743 /* needs file table */
744 unsigned file_table : 1;
ff002b30
JA
745 /* needs ->fs */
746 unsigned needs_fs : 1;
8a72758c
JA
747 /* set if opcode supports polled "wait" */
748 unsigned pollin : 1;
749 unsigned pollout : 1;
bcda7baa
JA
750 /* op supports buffer selection */
751 unsigned buffer_select : 1;
e8c2bc1f 752 /* needs rlimit(RLIMIT_FSIZE) assigned */
57f1a649 753 unsigned needs_fsize : 1;
e8c2bc1f
JA
754 /* must always have async data allocated */
755 unsigned needs_async_data : 1;
91d8f519
DZ
756 /* needs blkcg context, issues async io potentially */
757 unsigned needs_blkcg : 1;
e8c2bc1f
JA
758 /* size of async data needed, if any */
759 unsigned short async_size;
d3656344
JA
760};
761
738277ad 762static const struct io_op_def io_op_defs[] __read_mostly = {
0463b6c5
PB
763 [IORING_OP_NOP] = {},
764 [IORING_OP_READV] = {
d3656344
JA
765 .needs_mm = 1,
766 .needs_file = 1,
767 .unbound_nonreg_file = 1,
8a72758c 768 .pollin = 1,
4d954c25 769 .buffer_select = 1,
e8c2bc1f 770 .needs_async_data = 1,
91d8f519 771 .needs_blkcg = 1,
e8c2bc1f 772 .async_size = sizeof(struct io_async_rw),
d3656344 773 },
0463b6c5 774 [IORING_OP_WRITEV] = {
d3656344
JA
775 .needs_mm = 1,
776 .needs_file = 1,
777 .hash_reg_file = 1,
778 .unbound_nonreg_file = 1,
8a72758c 779 .pollout = 1,
57f1a649 780 .needs_fsize = 1,
e8c2bc1f 781 .needs_async_data = 1,
91d8f519 782 .needs_blkcg = 1,
e8c2bc1f 783 .async_size = sizeof(struct io_async_rw),
d3656344 784 },
0463b6c5 785 [IORING_OP_FSYNC] = {
d3656344 786 .needs_file = 1,
91d8f519 787 .needs_blkcg = 1,
d3656344 788 },
0463b6c5 789 [IORING_OP_READ_FIXED] = {
d3656344
JA
790 .needs_file = 1,
791 .unbound_nonreg_file = 1,
8a72758c 792 .pollin = 1,
91d8f519 793 .needs_blkcg = 1,
e8c2bc1f 794 .async_size = sizeof(struct io_async_rw),
d3656344 795 },
0463b6c5 796 [IORING_OP_WRITE_FIXED] = {
d3656344
JA
797 .needs_file = 1,
798 .hash_reg_file = 1,
799 .unbound_nonreg_file = 1,
8a72758c 800 .pollout = 1,
57f1a649 801 .needs_fsize = 1,
91d8f519 802 .needs_blkcg = 1,
e8c2bc1f 803 .async_size = sizeof(struct io_async_rw),
d3656344 804 },
0463b6c5 805 [IORING_OP_POLL_ADD] = {
d3656344
JA
806 .needs_file = 1,
807 .unbound_nonreg_file = 1,
808 },
0463b6c5
PB
809 [IORING_OP_POLL_REMOVE] = {},
810 [IORING_OP_SYNC_FILE_RANGE] = {
d3656344 811 .needs_file = 1,
91d8f519 812 .needs_blkcg = 1,
d3656344 813 },
0463b6c5 814 [IORING_OP_SENDMSG] = {
d3656344
JA
815 .needs_mm = 1,
816 .needs_file = 1,
817 .unbound_nonreg_file = 1,
ff002b30 818 .needs_fs = 1,
8a72758c 819 .pollout = 1,
e8c2bc1f 820 .needs_async_data = 1,
91d8f519 821 .needs_blkcg = 1,
e8c2bc1f 822 .async_size = sizeof(struct io_async_msghdr),
d3656344 823 },
0463b6c5 824 [IORING_OP_RECVMSG] = {
d3656344
JA
825 .needs_mm = 1,
826 .needs_file = 1,
827 .unbound_nonreg_file = 1,
ff002b30 828 .needs_fs = 1,
8a72758c 829 .pollin = 1,
52de1fe1 830 .buffer_select = 1,
e8c2bc1f 831 .needs_async_data = 1,
91d8f519 832 .needs_blkcg = 1,
e8c2bc1f 833 .async_size = sizeof(struct io_async_msghdr),
d3656344 834 },
0463b6c5 835 [IORING_OP_TIMEOUT] = {
d3656344 836 .needs_mm = 1,
e8c2bc1f
JA
837 .needs_async_data = 1,
838 .async_size = sizeof(struct io_timeout_data),
d3656344 839 },
0463b6c5
PB
840 [IORING_OP_TIMEOUT_REMOVE] = {},
841 [IORING_OP_ACCEPT] = {
d3656344
JA
842 .needs_mm = 1,
843 .needs_file = 1,
844 .unbound_nonreg_file = 1,
f86cd20c 845 .file_table = 1,
8a72758c 846 .pollin = 1,
d3656344 847 },
0463b6c5
PB
848 [IORING_OP_ASYNC_CANCEL] = {},
849 [IORING_OP_LINK_TIMEOUT] = {
d3656344 850 .needs_mm = 1,
e8c2bc1f
JA
851 .needs_async_data = 1,
852 .async_size = sizeof(struct io_timeout_data),
d3656344 853 },
0463b6c5 854 [IORING_OP_CONNECT] = {
d3656344
JA
855 .needs_mm = 1,
856 .needs_file = 1,
857 .unbound_nonreg_file = 1,
8a72758c 858 .pollout = 1,
e8c2bc1f
JA
859 .needs_async_data = 1,
860 .async_size = sizeof(struct io_async_connect),
d3656344 861 },
0463b6c5 862 [IORING_OP_FALLOCATE] = {
d3656344 863 .needs_file = 1,
57f1a649 864 .needs_fsize = 1,
91d8f519 865 .needs_blkcg = 1,
d3656344 866 },
0463b6c5 867 [IORING_OP_OPENAT] = {
f86cd20c 868 .file_table = 1,
ff002b30 869 .needs_fs = 1,
91d8f519 870 .needs_blkcg = 1,
d3656344 871 },
0463b6c5 872 [IORING_OP_CLOSE] = {
fd2206e4
JA
873 .needs_file = 1,
874 .needs_file_no_error = 1,
f86cd20c 875 .file_table = 1,
91d8f519 876 .needs_blkcg = 1,
d3656344 877 },
0463b6c5 878 [IORING_OP_FILES_UPDATE] = {
d3656344 879 .needs_mm = 1,
f86cd20c 880 .file_table = 1,
d3656344 881 },
0463b6c5 882 [IORING_OP_STATX] = {
d3656344 883 .needs_mm = 1,
ff002b30 884 .needs_fs = 1,
5b0bbee4 885 .file_table = 1,
91d8f519 886 .needs_blkcg = 1,
d3656344 887 },
0463b6c5 888 [IORING_OP_READ] = {
3a6820f2
JA
889 .needs_mm = 1,
890 .needs_file = 1,
891 .unbound_nonreg_file = 1,
8a72758c 892 .pollin = 1,
bcda7baa 893 .buffer_select = 1,
91d8f519 894 .needs_blkcg = 1,
e8c2bc1f 895 .async_size = sizeof(struct io_async_rw),
3a6820f2 896 },
0463b6c5 897 [IORING_OP_WRITE] = {
3a6820f2
JA
898 .needs_mm = 1,
899 .needs_file = 1,
900 .unbound_nonreg_file = 1,
8a72758c 901 .pollout = 1,
57f1a649 902 .needs_fsize = 1,
91d8f519 903 .needs_blkcg = 1,
e8c2bc1f 904 .async_size = sizeof(struct io_async_rw),
3a6820f2 905 },
0463b6c5 906 [IORING_OP_FADVISE] = {
4840e418 907 .needs_file = 1,
91d8f519 908 .needs_blkcg = 1,
4840e418 909 },
0463b6c5 910 [IORING_OP_MADVISE] = {
c1ca757b 911 .needs_mm = 1,
91d8f519 912 .needs_blkcg = 1,
c1ca757b 913 },
0463b6c5 914 [IORING_OP_SEND] = {
fddaface
JA
915 .needs_mm = 1,
916 .needs_file = 1,
917 .unbound_nonreg_file = 1,
8a72758c 918 .pollout = 1,
91d8f519 919 .needs_blkcg = 1,
fddaface 920 },
0463b6c5 921 [IORING_OP_RECV] = {
fddaface
JA
922 .needs_mm = 1,
923 .needs_file = 1,
924 .unbound_nonreg_file = 1,
8a72758c 925 .pollin = 1,
bcda7baa 926 .buffer_select = 1,
91d8f519 927 .needs_blkcg = 1,
fddaface 928 },
0463b6c5 929 [IORING_OP_OPENAT2] = {
f86cd20c 930 .file_table = 1,
ff002b30 931 .needs_fs = 1,
91d8f519 932 .needs_blkcg = 1,
cebdb986 933 },
3e4827b0
JA
934 [IORING_OP_EPOLL_CTL] = {
935 .unbound_nonreg_file = 1,
936 .file_table = 1,
937 },
7d67af2c
PB
938 [IORING_OP_SPLICE] = {
939 .needs_file = 1,
940 .hash_reg_file = 1,
941 .unbound_nonreg_file = 1,
91d8f519 942 .needs_blkcg = 1,
ddf0322d
JA
943 },
944 [IORING_OP_PROVIDE_BUFFERS] = {},
067524e9 945 [IORING_OP_REMOVE_BUFFERS] = {},
f2a8d5c7
PB
946 [IORING_OP_TEE] = {
947 .needs_file = 1,
948 .hash_reg_file = 1,
949 .unbound_nonreg_file = 1,
950 },
d3656344
JA
951};
952
2e0464d4
BM
953enum io_mem_account {
954 ACCT_LOCKED,
955 ACCT_PINNED,
956};
957
81b68a5c
PB
958static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
959 struct io_comp_state *cs);
78e19bbe 960static void io_cqring_fill_event(struct io_kiocb *req, long res);
ec9c02ad 961static void io_put_req(struct io_kiocb *req);
c40f6379 962static void io_double_put_req(struct io_kiocb *req);
978db57e 963static void __io_double_put_req(struct io_kiocb *req);
94ae5e77 964static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
7271ef3a 965static void __io_queue_linked_timeout(struct io_kiocb *req);
94ae5e77 966static void io_queue_linked_timeout(struct io_kiocb *req);
05f3fb3c
JA
967static int __io_sqe_files_update(struct io_ring_ctx *ctx,
968 struct io_uring_files_update *ip,
969 unsigned nr_args);
f56040b8 970static int io_prep_work_files(struct io_kiocb *req);
3ca405eb 971static void __io_clean_op(struct io_kiocb *req);
b41e9852
JA
972static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
973 int fd, struct file **out_file, bool fixed);
974static void __io_queue_sqe(struct io_kiocb *req,
f13fad7b
JA
975 const struct io_uring_sqe *sqe,
976 struct io_comp_state *cs);
4349f30e 977static void io_file_put_work(struct work_struct *work);
de0617e4 978
b63534c4
JA
979static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
980 struct iovec **iovec, struct iov_iter *iter,
981 bool needs_lock);
ff6165b2
JA
982static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
983 const struct iovec *fast_iov,
227c0c96 984 struct iov_iter *iter, bool force);
de0617e4 985
2b188cc1
JA
986static struct kmem_cache *req_cachep;
987
738277ad 988static const struct file_operations io_uring_fops __read_mostly;
2b188cc1
JA
989
990struct sock *io_uring_get_socket(struct file *file)
991{
992#if defined(CONFIG_UNIX)
993 if (file->f_op == &io_uring_fops) {
994 struct io_ring_ctx *ctx = file->private_data;
995
996 return ctx->ring_sock->sk;
997 }
998#endif
999 return NULL;
1000}
1001EXPORT_SYMBOL(io_uring_get_socket);
1002
3ca405eb
PB
1003static inline void io_clean_op(struct io_kiocb *req)
1004{
bb175342
PB
1005 if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED |
1006 REQ_F_INFLIGHT))
3ca405eb
PB
1007 __io_clean_op(req);
1008}
1009
4349f30e 1010static void io_sq_thread_drop_mm(void)
c40f6379
JA
1011{
1012 struct mm_struct *mm = current->mm;
1013
1014 if (mm) {
1015 kthread_unuse_mm(mm);
1016 mmput(mm);
1017 }
1018}
1019
1020static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
1021{
1022 if (!current->mm) {
cbcf7214 1023 if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL) ||
2aede0e4
JA
1024 !ctx->sqo_task->mm ||
1025 !mmget_not_zero(ctx->sqo_task->mm)))
c40f6379 1026 return -EFAULT;
2aede0e4 1027 kthread_use_mm(ctx->sqo_task->mm);
c40f6379
JA
1028 }
1029
1030 return 0;
1031}
1032
1033static int io_sq_thread_acquire_mm(struct io_ring_ctx *ctx,
1034 struct io_kiocb *req)
1035{
1036 if (!io_op_defs[req->opcode].needs_mm)
1037 return 0;
1038 return __io_sq_thread_acquire_mm(ctx);
1039}
1040
91d8f519
DZ
1041static void io_sq_thread_associate_blkcg(struct io_ring_ctx *ctx,
1042 struct cgroup_subsys_state **cur_css)
1043
1044{
1045#ifdef CONFIG_BLK_CGROUP
1046 /* puts the old one when swapping */
1047 if (*cur_css != ctx->sqo_blkcg_css) {
1048 kthread_associate_blkcg(ctx->sqo_blkcg_css);
1049 *cur_css = ctx->sqo_blkcg_css;
1050 }
1051#endif
1052}
1053
1054static void io_sq_thread_unassociate_blkcg(void)
1055{
1056#ifdef CONFIG_BLK_CGROUP
1057 kthread_associate_blkcg(NULL);
1058#endif
1059}
1060
c40f6379
JA
1061static inline void req_set_fail_links(struct io_kiocb *req)
1062{
1063 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1064 req->flags |= REQ_F_FAIL_LINK;
1065}
4a38aed2 1066
7cdaf587
XW
1067/*
1068 * Note: must call io_req_init_async() for the first time you
1069 * touch any members of io_wq_work.
1070 */
1071static inline void io_req_init_async(struct io_kiocb *req)
1072{
1073 if (req->flags & REQ_F_WORK_INITIALIZED)
1074 return;
1075
1076 memset(&req->work, 0, sizeof(req->work));
1077 req->flags |= REQ_F_WORK_INITIALIZED;
1078}
1079
0cdaf760
PB
1080static inline bool io_async_submit(struct io_ring_ctx *ctx)
1081{
1082 return ctx->flags & IORING_SETUP_SQPOLL;
1083}
1084
2b188cc1
JA
1085static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1086{
1087 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1088
0f158b4c 1089 complete(&ctx->ref_comp);
2b188cc1
JA
1090}
1091
8eb7e2d0
PB
1092static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1093{
1094 return !req->timeout.off;
1095}
1096
2b188cc1
JA
1097static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1098{
1099 struct io_ring_ctx *ctx;
78076bb6 1100 int hash_bits;
2b188cc1
JA
1101
1102 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1103 if (!ctx)
1104 return NULL;
1105
0ddf92e8
JA
1106 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
1107 if (!ctx->fallback_req)
1108 goto err;
1109
78076bb6
JA
1110 /*
1111 * Use 5 bits less than the max cq entries, that should give us around
1112 * 32 entries per hash list if totally full and uniformly spread.
1113 */
1114 hash_bits = ilog2(p->cq_entries);
1115 hash_bits -= 5;
1116 if (hash_bits <= 0)
1117 hash_bits = 1;
1118 ctx->cancel_hash_bits = hash_bits;
1119 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1120 GFP_KERNEL);
1121 if (!ctx->cancel_hash)
1122 goto err;
1123 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1124
21482896 1125 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
206aefde
JA
1126 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1127 goto err;
2b188cc1
JA
1128
1129 ctx->flags = p->flags;
90554200 1130 init_waitqueue_head(&ctx->sqo_sq_wait);
69fb2131 1131 INIT_LIST_HEAD(&ctx->sqd_list);
2b188cc1 1132 init_waitqueue_head(&ctx->cq_wait);
1d7bb1d5 1133 INIT_LIST_HEAD(&ctx->cq_overflow_list);
0f158b4c
JA
1134 init_completion(&ctx->ref_comp);
1135 init_completion(&ctx->sq_thread_comp);
5a2e745d 1136 idr_init(&ctx->io_buffer_idr);
071698e1 1137 idr_init(&ctx->personality_idr);
2b188cc1
JA
1138 mutex_init(&ctx->uring_lock);
1139 init_waitqueue_head(&ctx->wait);
1140 spin_lock_init(&ctx->completion_lock);
540e32a0 1141 INIT_LIST_HEAD(&ctx->iopoll_list);
de0617e4 1142 INIT_LIST_HEAD(&ctx->defer_list);
5262f567 1143 INIT_LIST_HEAD(&ctx->timeout_list);
fcb323cc
JA
1144 init_waitqueue_head(&ctx->inflight_wait);
1145 spin_lock_init(&ctx->inflight_lock);
1146 INIT_LIST_HEAD(&ctx->inflight_list);
4a38aed2
JA
1147 INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work);
1148 init_llist_head(&ctx->file_put_llist);
2b188cc1 1149 return ctx;
206aefde 1150err:
0ddf92e8
JA
1151 if (ctx->fallback_req)
1152 kmem_cache_free(req_cachep, ctx->fallback_req);
78076bb6 1153 kfree(ctx->cancel_hash);
206aefde
JA
1154 kfree(ctx);
1155 return NULL;
2b188cc1
JA
1156}
1157
9cf7c104 1158static bool req_need_defer(struct io_kiocb *req, u32 seq)
7adf4eaf 1159{
2bc9930e
JA
1160 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1161 struct io_ring_ctx *ctx = req->ctx;
a197f664 1162
9cf7c104 1163 return seq != ctx->cached_cq_tail
31af27c7 1164 + atomic_read(&ctx->cached_cq_overflow);
2bc9930e 1165 }
de0617e4 1166
9d858b21 1167 return false;
de0617e4
JA
1168}
1169
de0617e4 1170static void __io_commit_cqring(struct io_ring_ctx *ctx)
2b188cc1 1171{
75b28aff 1172 struct io_rings *rings = ctx->rings;
2b188cc1 1173
07910158
PB
1174 /* order cqe stores with ring update */
1175 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
2b188cc1 1176
07910158
PB
1177 if (wq_has_sleeper(&ctx->cq_wait)) {
1178 wake_up_interruptible(&ctx->cq_wait);
1179 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
2b188cc1
JA
1180 }
1181}
1182
51a4cc11
JA
1183/*
1184 * Returns true if we need to defer file table putting. This can only happen
1185 * from the error path with REQ_F_COMP_LOCKED set.
1186 */
1187static bool io_req_clean_work(struct io_kiocb *req)
18d9be1a 1188{
7cdaf587 1189 if (!(req->flags & REQ_F_WORK_INITIALIZED))
51a4cc11
JA
1190 return false;
1191
1192 req->flags &= ~REQ_F_WORK_INITIALIZED;
7cdaf587 1193
cccf0ee8
JA
1194 if (req->work.mm) {
1195 mmdrop(req->work.mm);
1196 req->work.mm = NULL;
1197 }
91d8f519
DZ
1198#ifdef CONFIG_BLK_CGROUP
1199 if (req->work.blkcg_css)
1200 css_put(req->work.blkcg_css);
1201#endif
cccf0ee8
JA
1202 if (req->work.creds) {
1203 put_cred(req->work.creds);
1204 req->work.creds = NULL;
1205 }
ff002b30
JA
1206 if (req->work.fs) {
1207 struct fs_struct *fs = req->work.fs;
1208
51a4cc11
JA
1209 if (req->flags & REQ_F_COMP_LOCKED)
1210 return true;
1211
ff002b30
JA
1212 spin_lock(&req->work.fs->lock);
1213 if (--fs->users)
1214 fs = NULL;
1215 spin_unlock(&req->work.fs->lock);
1216 if (fs)
1217 free_fs_struct(fs);
b65e0dd6 1218 req->work.fs = NULL;
ff002b30 1219 }
51a4cc11
JA
1220
1221 return false;
561fb04a
JA
1222}
1223
cbdcb435 1224static void io_prep_async_work(struct io_kiocb *req)
18d9be1a 1225{
d3656344 1226 const struct io_op_def *def = &io_op_defs[req->opcode];
54a91f3b 1227
16d59803
PB
1228 io_req_init_async(req);
1229
d3656344 1230 if (req->flags & REQ_F_ISREG) {
eefdf30f 1231 if (def->hash_reg_file || (req->ctx->flags & IORING_SETUP_IOPOLL))
8766dd51 1232 io_wq_hash_work(&req->work, file_inode(req->file));
d3656344
JA
1233 } else {
1234 if (def->unbound_nonreg_file)
3529d8c2 1235 req->work.flags |= IO_WQ_WORK_UNBOUND;
54a91f3b 1236 }
dca9cf8b
PB
1237 if (!req->work.mm && def->needs_mm) {
1238 mmgrab(current->mm);
1239 req->work.mm = current->mm;
1240 }
91d8f519
DZ
1241#ifdef CONFIG_BLK_CGROUP
1242 if (!req->work.blkcg_css && def->needs_blkcg) {
1243 rcu_read_lock();
1244 req->work.blkcg_css = blkcg_css();
1245 /*
1246 * This should be rare, either the cgroup is dying or the task
1247 * is moving cgroups. Just punt to root for the handful of ios.
1248 */
1249 if (!css_tryget_online(req->work.blkcg_css))
1250 req->work.blkcg_css = NULL;
1251 rcu_read_unlock();
1252 }
1253#endif
dca9cf8b
PB
1254 if (!req->work.creds)
1255 req->work.creds = get_current_cred();
1256 if (!req->work.fs && def->needs_fs) {
1257 spin_lock(&current->fs->lock);
1258 if (!current->fs->in_exec) {
1259 req->work.fs = current->fs;
1260 req->work.fs->users++;
1261 } else {
1262 req->work.flags |= IO_WQ_WORK_CANCEL;
1263 }
1264 spin_unlock(&current->fs->lock);
1265 }
57f1a649
PB
1266 if (def->needs_fsize)
1267 req->work.fsize = rlimit(RLIMIT_FSIZE);
1268 else
1269 req->work.fsize = RLIM_INFINITY;
561fb04a 1270}
cccf0ee8 1271
cbdcb435 1272static void io_prep_async_link(struct io_kiocb *req)
561fb04a 1273{
cbdcb435 1274 struct io_kiocb *cur;
54a91f3b 1275
cbdcb435
PB
1276 io_prep_async_work(req);
1277 if (req->flags & REQ_F_LINK_HEAD)
1278 list_for_each_entry(cur, &req->link_list, link_list)
1279 io_prep_async_work(cur);
561fb04a
JA
1280}
1281
7271ef3a 1282static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req)
561fb04a 1283{
a197f664 1284 struct io_ring_ctx *ctx = req->ctx;
cbdcb435 1285 struct io_kiocb *link = io_prep_linked_timeout(req);
561fb04a 1286
8766dd51
PB
1287 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1288 &req->work, req->flags);
1289 io_wq_enqueue(ctx->io_wq, &req->work);
7271ef3a 1290 return link;
18d9be1a
JA
1291}
1292
cbdcb435
PB
1293static void io_queue_async_work(struct io_kiocb *req)
1294{
7271ef3a
JA
1295 struct io_kiocb *link;
1296
cbdcb435
PB
1297 /* init ->work of the whole link before punting */
1298 io_prep_async_link(req);
7271ef3a
JA
1299 link = __io_queue_async_work(req);
1300
1301 if (link)
1302 io_queue_linked_timeout(link);
cbdcb435
PB
1303}
1304
5262f567
JA
1305static void io_kill_timeout(struct io_kiocb *req)
1306{
e8c2bc1f 1307 struct io_timeout_data *io = req->async_data;
5262f567
JA
1308 int ret;
1309
e8c2bc1f 1310 ret = hrtimer_try_to_cancel(&io->timer);
5262f567 1311 if (ret != -1) {
01cec8c1
PB
1312 atomic_set(&req->ctx->cq_timeouts,
1313 atomic_read(&req->ctx->cq_timeouts) + 1);
135fcde8 1314 list_del_init(&req->timeout.list);
f0e20b89 1315 req->flags |= REQ_F_COMP_LOCKED;
78e19bbe 1316 io_cqring_fill_event(req, 0);
ec9c02ad 1317 io_put_req(req);
5262f567
JA
1318 }
1319}
1320
f3606e3a
JA
1321static bool io_task_match(struct io_kiocb *req, struct task_struct *tsk)
1322{
1323 struct io_ring_ctx *ctx = req->ctx;
1324
1325 if (!tsk || req->task == tsk)
1326 return true;
534ca6d6
JA
1327 if (ctx->flags & IORING_SETUP_SQPOLL) {
1328 if (ctx->sq_data && req->task == ctx->sq_data->thread)
1329 return true;
1330 }
f3606e3a
JA
1331 return false;
1332}
1333
76e1b642
JA
1334/*
1335 * Returns true if we found and killed one or more timeouts
1336 */
1337static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk)
5262f567
JA
1338{
1339 struct io_kiocb *req, *tmp;
76e1b642 1340 int canceled = 0;
5262f567
JA
1341
1342 spin_lock_irq(&ctx->completion_lock);
f3606e3a 1343 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
76e1b642 1344 if (io_task_match(req, tsk)) {
f3606e3a 1345 io_kill_timeout(req);
76e1b642
JA
1346 canceled++;
1347 }
f3606e3a 1348 }
5262f567 1349 spin_unlock_irq(&ctx->completion_lock);
76e1b642 1350 return canceled != 0;
5262f567
JA
1351}
1352
04518945 1353static void __io_queue_deferred(struct io_ring_ctx *ctx)
de0617e4 1354{
04518945 1355 do {
27dc8338
PB
1356 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1357 struct io_defer_entry, list);
7271ef3a 1358 struct io_kiocb *link;
de0617e4 1359
9cf7c104 1360 if (req_need_defer(de->req, de->seq))
04518945 1361 break;
27dc8338 1362 list_del_init(&de->list);
cbdcb435 1363 /* punt-init is done before queueing for defer */
7271ef3a
JA
1364 link = __io_queue_async_work(de->req);
1365 if (link) {
1366 __io_queue_linked_timeout(link);
1367 /* drop submission reference */
1368 link->flags |= REQ_F_COMP_LOCKED;
1369 io_put_req(link);
1370 }
27dc8338 1371 kfree(de);
04518945
PB
1372 } while (!list_empty(&ctx->defer_list));
1373}
1374
360428f8 1375static void io_flush_timeouts(struct io_ring_ctx *ctx)
de0617e4 1376{
360428f8
PB
1377 while (!list_empty(&ctx->timeout_list)) {
1378 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
135fcde8 1379 struct io_kiocb, timeout.list);
de0617e4 1380
8eb7e2d0 1381 if (io_is_timeout_noseq(req))
360428f8 1382 break;
bfe68a22
PB
1383 if (req->timeout.target_seq != ctx->cached_cq_tail
1384 - atomic_read(&ctx->cq_timeouts))
360428f8 1385 break;
bfe68a22 1386
135fcde8 1387 list_del_init(&req->timeout.list);
5262f567 1388 io_kill_timeout(req);
360428f8
PB
1389 }
1390}
5262f567 1391
360428f8
PB
1392static void io_commit_cqring(struct io_ring_ctx *ctx)
1393{
1394 io_flush_timeouts(ctx);
de0617e4
JA
1395 __io_commit_cqring(ctx);
1396
04518945
PB
1397 if (unlikely(!list_empty(&ctx->defer_list)))
1398 __io_queue_deferred(ctx);
de0617e4
JA
1399}
1400
90554200
JA
1401static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1402{
1403 struct io_rings *r = ctx->rings;
1404
1405 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries;
1406}
1407
2b188cc1
JA
1408static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1409{
75b28aff 1410 struct io_rings *rings = ctx->rings;
2b188cc1
JA
1411 unsigned tail;
1412
1413 tail = ctx->cached_cq_tail;
115e12e5
SB
1414 /*
1415 * writes to the cq entry need to come after reading head; the
1416 * control dependency is enough as we're using WRITE_ONCE to
1417 * fill the cq entry
1418 */
75b28aff 1419 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
2b188cc1
JA
1420 return NULL;
1421
1422 ctx->cached_cq_tail++;
75b28aff 1423 return &rings->cqes[tail & ctx->cq_mask];
2b188cc1
JA
1424}
1425
f2842ab5
JA
1426static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1427{
f0b493e6
JA
1428 if (!ctx->cq_ev_fd)
1429 return false;
7e55a19c
SG
1430 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1431 return false;
f2842ab5
JA
1432 if (!ctx->eventfd_async)
1433 return true;
b41e9852 1434 return io_wq_current_is_worker();
f2842ab5
JA
1435}
1436
b41e9852 1437static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
1d7bb1d5
JA
1438{
1439 if (waitqueue_active(&ctx->wait))
1440 wake_up(&ctx->wait);
534ca6d6
JA
1441 if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait))
1442 wake_up(&ctx->sq_data->wait);
b41e9852 1443 if (io_should_trigger_evfd(ctx))
1d7bb1d5
JA
1444 eventfd_signal(ctx->cq_ev_fd, 1);
1445}
1446
46930143
PB
1447static void io_cqring_mark_overflow(struct io_ring_ctx *ctx)
1448{
1449 if (list_empty(&ctx->cq_overflow_list)) {
1450 clear_bit(0, &ctx->sq_check_overflow);
1451 clear_bit(0, &ctx->cq_check_overflow);
1452 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
1453 }
1454}
1455
e6c8aa9a
JA
1456static inline bool io_match_files(struct io_kiocb *req,
1457 struct files_struct *files)
1458{
1459 if (!files)
1460 return true;
1461 if (req->flags & REQ_F_WORK_INITIALIZED)
1462 return req->work.files == files;
1463 return false;
1464}
1465
c4a2ed72 1466/* Returns true if there are no backlogged entries after the flush */
e6c8aa9a
JA
1467static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1468 struct task_struct *tsk,
1469 struct files_struct *files)
1d7bb1d5
JA
1470{
1471 struct io_rings *rings = ctx->rings;
e6c8aa9a 1472 struct io_kiocb *req, *tmp;
1d7bb1d5 1473 struct io_uring_cqe *cqe;
1d7bb1d5
JA
1474 unsigned long flags;
1475 LIST_HEAD(list);
1476
1477 if (!force) {
1478 if (list_empty_careful(&ctx->cq_overflow_list))
c4a2ed72 1479 return true;
1d7bb1d5
JA
1480 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1481 rings->cq_ring_entries))
c4a2ed72 1482 return false;
1d7bb1d5
JA
1483 }
1484
1485 spin_lock_irqsave(&ctx->completion_lock, flags);
1486
1487 /* if force is set, the ring is going away. always drop after that */
1488 if (force)
69b3e546 1489 ctx->cq_overflow_flushed = 1;
1d7bb1d5 1490
c4a2ed72 1491 cqe = NULL;
e6c8aa9a
JA
1492 list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) {
1493 if (tsk && req->task != tsk)
1494 continue;
1495 if (!io_match_files(req, files))
1496 continue;
1497
1d7bb1d5
JA
1498 cqe = io_get_cqring(ctx);
1499 if (!cqe && !force)
1500 break;
1501
40d8ddd4 1502 list_move(&req->compl.list, &list);
1d7bb1d5
JA
1503 if (cqe) {
1504 WRITE_ONCE(cqe->user_data, req->user_data);
1505 WRITE_ONCE(cqe->res, req->result);
0f7e466b 1506 WRITE_ONCE(cqe->flags, req->compl.cflags);
1d7bb1d5
JA
1507 } else {
1508 WRITE_ONCE(ctx->rings->cq_overflow,
1509 atomic_inc_return(&ctx->cached_cq_overflow));
1510 }
1511 }
1512
1513 io_commit_cqring(ctx);
46930143
PB
1514 io_cqring_mark_overflow(ctx);
1515
1d7bb1d5
JA
1516 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1517 io_cqring_ev_posted(ctx);
1518
1519 while (!list_empty(&list)) {
40d8ddd4
PB
1520 req = list_first_entry(&list, struct io_kiocb, compl.list);
1521 list_del(&req->compl.list);
ec9c02ad 1522 io_put_req(req);
1d7bb1d5 1523 }
c4a2ed72
JA
1524
1525 return cqe != NULL;
1d7bb1d5
JA
1526}
1527
bcda7baa 1528static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
2b188cc1 1529{
78e19bbe 1530 struct io_ring_ctx *ctx = req->ctx;
2b188cc1
JA
1531 struct io_uring_cqe *cqe;
1532
78e19bbe 1533 trace_io_uring_complete(ctx, req->user_data, res);
51c3ff62 1534
2b188cc1
JA
1535 /*
1536 * If we can't get a cq entry, userspace overflowed the
1537 * submission (by quite a lot). Increment the overflow count in
1538 * the ring.
1539 */
1540 cqe = io_get_cqring(ctx);
1d7bb1d5 1541 if (likely(cqe)) {
78e19bbe 1542 WRITE_ONCE(cqe->user_data, req->user_data);
2b188cc1 1543 WRITE_ONCE(cqe->res, res);
bcda7baa 1544 WRITE_ONCE(cqe->flags, cflags);
0f212204
JA
1545 } else if (ctx->cq_overflow_flushed || req->task->io_uring->in_idle) {
1546 /*
1547 * If we're in ring overflow flush mode, or in task cancel mode,
1548 * then we cannot store the request for later flushing, we need
1549 * to drop it on the floor.
1550 */
498ccd9e
JA
1551 WRITE_ONCE(ctx->rings->cq_overflow,
1552 atomic_inc_return(&ctx->cached_cq_overflow));
1d7bb1d5 1553 } else {
ad3eb2c8
JA
1554 if (list_empty(&ctx->cq_overflow_list)) {
1555 set_bit(0, &ctx->sq_check_overflow);
1556 set_bit(0, &ctx->cq_check_overflow);
6d5f9049 1557 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
ad3eb2c8 1558 }
40d8ddd4 1559 io_clean_op(req);
1d7bb1d5 1560 req->result = res;
0f7e466b 1561 req->compl.cflags = cflags;
40d8ddd4
PB
1562 refcount_inc(&req->refs);
1563 list_add_tail(&req->compl.list, &ctx->cq_overflow_list);
2b188cc1
JA
1564 }
1565}
1566
bcda7baa
JA
1567static void io_cqring_fill_event(struct io_kiocb *req, long res)
1568{
1569 __io_cqring_fill_event(req, res, 0);
1570}
1571
e1e16097 1572static void io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
2b188cc1 1573{
78e19bbe 1574 struct io_ring_ctx *ctx = req->ctx;
2b188cc1
JA
1575 unsigned long flags;
1576
1577 spin_lock_irqsave(&ctx->completion_lock, flags);
bcda7baa 1578 __io_cqring_fill_event(req, res, cflags);
2b188cc1
JA
1579 io_commit_cqring(ctx);
1580 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1581
8c838788 1582 io_cqring_ev_posted(ctx);
2b188cc1
JA
1583}
1584
229a7b63 1585static void io_submit_flush_completions(struct io_comp_state *cs)
bcda7baa 1586{
229a7b63
JA
1587 struct io_ring_ctx *ctx = cs->ctx;
1588
1589 spin_lock_irq(&ctx->completion_lock);
1590 while (!list_empty(&cs->list)) {
1591 struct io_kiocb *req;
1592
3ca405eb
PB
1593 req = list_first_entry(&cs->list, struct io_kiocb, compl.list);
1594 list_del(&req->compl.list);
0f7e466b 1595 __io_cqring_fill_event(req, req->result, req->compl.cflags);
229a7b63
JA
1596 if (!(req->flags & REQ_F_LINK_HEAD)) {
1597 req->flags |= REQ_F_COMP_LOCKED;
1598 io_put_req(req);
1599 } else {
1600 spin_unlock_irq(&ctx->completion_lock);
1601 io_put_req(req);
1602 spin_lock_irq(&ctx->completion_lock);
1603 }
1604 }
1605 io_commit_cqring(ctx);
1606 spin_unlock_irq(&ctx->completion_lock);
1607
1608 io_cqring_ev_posted(ctx);
1609 cs->nr = 0;
1610}
1611
1612static void __io_req_complete(struct io_kiocb *req, long res, unsigned cflags,
1613 struct io_comp_state *cs)
1614{
1615 if (!cs) {
1616 io_cqring_add_event(req, res, cflags);
1617 io_put_req(req);
1618 } else {
3ca405eb 1619 io_clean_op(req);
229a7b63 1620 req->result = res;
0f7e466b 1621 req->compl.cflags = cflags;
3ca405eb 1622 list_add_tail(&req->compl.list, &cs->list);
229a7b63
JA
1623 if (++cs->nr >= 32)
1624 io_submit_flush_completions(cs);
1625 }
e1e16097
JA
1626}
1627
1628static void io_req_complete(struct io_kiocb *req, long res)
bcda7baa 1629{
229a7b63 1630 __io_req_complete(req, res, 0, NULL);
bcda7baa
JA
1631}
1632
0ddf92e8
JA
1633static inline bool io_is_fallback_req(struct io_kiocb *req)
1634{
1635 return req == (struct io_kiocb *)
1636 ((unsigned long) req->ctx->fallback_req & ~1UL);
1637}
1638
1639static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1640{
1641 struct io_kiocb *req;
1642
1643 req = ctx->fallback_req;
dd461af6 1644 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req))
0ddf92e8
JA
1645 return req;
1646
1647 return NULL;
1648}
1649
0553b8bd
PB
1650static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
1651 struct io_submit_state *state)
2b188cc1 1652{
f6b6c7d6 1653 if (!state->free_reqs) {
291b2821 1654 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
2579f913
JA
1655 size_t sz;
1656 int ret;
1657
1658 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
fd6fab2c
JA
1659 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1660
1661 /*
1662 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1663 * retry single alloc to be on the safe side.
1664 */
1665 if (unlikely(ret <= 0)) {
1666 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1667 if (!state->reqs[0])
0ddf92e8 1668 goto fallback;
fd6fab2c
JA
1669 ret = 1;
1670 }
291b2821 1671 state->free_reqs = ret;
2b188cc1
JA
1672 }
1673
291b2821
PB
1674 state->free_reqs--;
1675 return state->reqs[state->free_reqs];
0ddf92e8 1676fallback:
0553b8bd 1677 return io_get_fallback_req(ctx);
2b188cc1
JA
1678}
1679
8da11c19
PB
1680static inline void io_put_file(struct io_kiocb *req, struct file *file,
1681 bool fixed)
1682{
1683 if (fixed)
05589553 1684 percpu_ref_put(req->fixed_file_refs);
8da11c19
PB
1685 else
1686 fput(file);
1687}
1688
51a4cc11 1689static bool io_dismantle_req(struct io_kiocb *req)
2b188cc1 1690{
3ca405eb 1691 io_clean_op(req);
929a3af9 1692
e8c2bc1f
JA
1693 if (req->async_data)
1694 kfree(req->async_data);
8da11c19
PB
1695 if (req->file)
1696 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
fcb323cc 1697
51a4cc11 1698 return io_req_clean_work(req);
e65ef56d
JA
1699}
1700
51a4cc11 1701static void __io_free_req_finish(struct io_kiocb *req)
c6ca97b3 1702{
0f212204 1703 struct io_uring_task *tctx = req->task->io_uring;
51a4cc11 1704 struct io_ring_ctx *ctx = req->ctx;
c6ca97b3 1705
0f212204
JA
1706 atomic_long_inc(&tctx->req_complete);
1707 if (tctx->in_idle)
1708 wake_up(&tctx->wait);
e3bc8e9d
JA
1709 put_task_struct(req->task);
1710
b1e50e54
PB
1711 if (likely(!io_is_fallback_req(req)))
1712 kmem_cache_free(req_cachep, req);
1713 else
ecfc5177
PB
1714 clear_bit_unlock(0, (unsigned long *) &ctx->fallback_req);
1715 percpu_ref_put(&ctx->refs);
e65ef56d
JA
1716}
1717
51a4cc11
JA
1718static void io_req_task_file_table_put(struct callback_head *cb)
1719{
1720 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
1721 struct fs_struct *fs = req->work.fs;
1722
1723 spin_lock(&req->work.fs->lock);
1724 if (--fs->users)
1725 fs = NULL;
1726 spin_unlock(&req->work.fs->lock);
1727 if (fs)
1728 free_fs_struct(fs);
1729 req->work.fs = NULL;
1730 __io_free_req_finish(req);
1731}
1732
1733static void __io_free_req(struct io_kiocb *req)
1734{
1735 if (!io_dismantle_req(req)) {
1736 __io_free_req_finish(req);
1737 } else {
1738 int ret;
1739
1740 init_task_work(&req->task_work, io_req_task_file_table_put);
1741 ret = task_work_add(req->task, &req->task_work, TWA_RESUME);
1742 if (unlikely(ret)) {
1743 struct task_struct *tsk;
1744
1745 tsk = io_wq_get_task(req->ctx->io_wq);
1746 task_work_add(tsk, &req->task_work, 0);
1747 }
1748 }
1749}
1750
a197f664 1751static bool io_link_cancel_timeout(struct io_kiocb *req)
2665abfd 1752{
e8c2bc1f 1753 struct io_timeout_data *io = req->async_data;
a197f664 1754 struct io_ring_ctx *ctx = req->ctx;
2665abfd
JA
1755 int ret;
1756
e8c2bc1f 1757 ret = hrtimer_try_to_cancel(&io->timer);
2665abfd 1758 if (ret != -1) {
78e19bbe 1759 io_cqring_fill_event(req, -ECANCELED);
2665abfd 1760 io_commit_cqring(ctx);
dea3b49c 1761 req->flags &= ~REQ_F_LINK_HEAD;
ec9c02ad 1762 io_put_req(req);
2665abfd
JA
1763 return true;
1764 }
1765
1766 return false;
e65ef56d
JA
1767}
1768
ab0b6451 1769static bool __io_kill_linked_timeout(struct io_kiocb *req)
9e645e11 1770{
7c86ffee 1771 struct io_kiocb *link;
ab0b6451 1772 bool wake_ev;
7c86ffee
PB
1773
1774 if (list_empty(&req->link_list))
ab0b6451 1775 return false;
7c86ffee
PB
1776 link = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1777 if (link->opcode != IORING_OP_LINK_TIMEOUT)
ab0b6451 1778 return false;
7c86ffee
PB
1779
1780 list_del_init(&link->link_list);
9b7adba9 1781 link->flags |= REQ_F_COMP_LOCKED;
7c86ffee
PB
1782 wake_ev = io_link_cancel_timeout(link);
1783 req->flags &= ~REQ_F_LINK_TIMEOUT;
ab0b6451
JA
1784 return wake_ev;
1785}
1786
1787static void io_kill_linked_timeout(struct io_kiocb *req)
9e645e11 1788{
2665abfd 1789 struct io_ring_ctx *ctx = req->ctx;
ab0b6451 1790 bool wake_ev;
9e645e11 1791
ab0b6451
JA
1792 if (!(req->flags & REQ_F_COMP_LOCKED)) {
1793 unsigned long flags;
1794
1795 spin_lock_irqsave(&ctx->completion_lock, flags);
1796 wake_ev = __io_kill_linked_timeout(req);
7c86ffee 1797 spin_unlock_irqrestore(&ctx->completion_lock, flags);
ab0b6451
JA
1798 } else {
1799 wake_ev = __io_kill_linked_timeout(req);
1800 }
1801
7c86ffee
PB
1802 if (wake_ev)
1803 io_cqring_ev_posted(ctx);
1804}
1805
9b5f7bd9 1806static struct io_kiocb *io_req_link_next(struct io_kiocb *req)
7c86ffee
PB
1807{
1808 struct io_kiocb *nxt;
4d7dd462 1809
9e645e11
JA
1810 /*
1811 * The list should never be empty when we are called here. But could
1812 * potentially happen if the chain is messed up, check to be on the
1813 * safe side.
1814 */
7c86ffee 1815 if (unlikely(list_empty(&req->link_list)))
9b5f7bd9 1816 return NULL;
2665abfd 1817
7c86ffee
PB
1818 nxt = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1819 list_del_init(&req->link_list);
1820 if (!list_empty(&nxt->link_list))
1821 nxt->flags |= REQ_F_LINK_HEAD;
9b5f7bd9 1822 return nxt;
9e645e11
JA
1823}
1824
1825/*
dea3b49c 1826 * Called if REQ_F_LINK_HEAD is set, and we fail the head request
9e645e11 1827 */
7c86ffee 1828static void __io_fail_links(struct io_kiocb *req)
9e645e11 1829{
2665abfd 1830 struct io_ring_ctx *ctx = req->ctx;
9e645e11
JA
1831
1832 while (!list_empty(&req->link_list)) {
4493233e
PB
1833 struct io_kiocb *link = list_first_entry(&req->link_list,
1834 struct io_kiocb, link_list);
9e645e11 1835
4493233e 1836 list_del_init(&link->link_list);
c826bd7a 1837 trace_io_uring_fail_link(req, link);
2665abfd 1838
7c86ffee 1839 io_cqring_fill_event(link, -ECANCELED);
9b7adba9 1840 link->flags |= REQ_F_COMP_LOCKED;
7c86ffee 1841 __io_double_put_req(link);
5d960724 1842 req->flags &= ~REQ_F_LINK_TIMEOUT;
9e645e11 1843 }
2665abfd
JA
1844
1845 io_commit_cqring(ctx);
2665abfd 1846 io_cqring_ev_posted(ctx);
9e645e11
JA
1847}
1848
7c86ffee 1849static void io_fail_links(struct io_kiocb *req)
9e645e11 1850{
7c86ffee 1851 struct io_ring_ctx *ctx = req->ctx;
2665abfd 1852
7c86ffee 1853 if (!(req->flags & REQ_F_COMP_LOCKED)) {
2665abfd
JA
1854 unsigned long flags;
1855
2665abfd 1856 spin_lock_irqsave(&ctx->completion_lock, flags);
7c86ffee 1857 __io_fail_links(req);
2665abfd
JA
1858 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1859 } else {
7c86ffee 1860 __io_fail_links(req);
9e645e11
JA
1861 }
1862
2665abfd 1863 io_cqring_ev_posted(ctx);
9e645e11
JA
1864}
1865
3fa5e0f3 1866static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
c69f8dbe 1867{
9b0d911a 1868 req->flags &= ~REQ_F_LINK_HEAD;
7c86ffee
PB
1869 if (req->flags & REQ_F_LINK_TIMEOUT)
1870 io_kill_linked_timeout(req);
944e58bf 1871
9e645e11
JA
1872 /*
1873 * If LINK is set, we have dependent requests in this chain. If we
1874 * didn't fail this request, queue the first one up, moving any other
1875 * dependencies to the next request. In case of failure, fail the rest
1876 * of the chain.
1877 */
9b5f7bd9
PB
1878 if (likely(!(req->flags & REQ_F_FAIL_LINK)))
1879 return io_req_link_next(req);
1880 io_fail_links(req);
1881 return NULL;
4d7dd462 1882}
9e645e11 1883
3fa5e0f3
PB
1884static struct io_kiocb *io_req_find_next(struct io_kiocb *req)
1885{
1886 if (likely(!(req->flags & REQ_F_LINK_HEAD)))
1887 return NULL;
1888 return __io_req_find_next(req);
1889}
1890
fd7d6de2
JA
1891static int io_req_task_work_add(struct io_kiocb *req, struct callback_head *cb,
1892 bool twa_signal_ok)
c2c4c83c
JA
1893{
1894 struct task_struct *tsk = req->task;
1895 struct io_ring_ctx *ctx = req->ctx;
0ba9c9ed 1896 int ret, notify;
c2c4c83c 1897
6200b0ae
JA
1898 if (tsk->flags & PF_EXITING)
1899 return -ESRCH;
1900
c2c4c83c 1901 /*
0ba9c9ed
JA
1902 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
1903 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
1904 * processing task_work. There's no reliable way to tell if TWA_RESUME
1905 * will do the job.
c2c4c83c 1906 */
0ba9c9ed 1907 notify = 0;
fd7d6de2 1908 if (!(ctx->flags & IORING_SETUP_SQPOLL) && twa_signal_ok)
c2c4c83c
JA
1909 notify = TWA_SIGNAL;
1910
1911 ret = task_work_add(tsk, cb, notify);
1912 if (!ret)
1913 wake_up_process(tsk);
0ba9c9ed 1914
c2c4c83c
JA
1915 return ret;
1916}
1917
c40f6379
JA
1918static void __io_req_task_cancel(struct io_kiocb *req, int error)
1919{
1920 struct io_ring_ctx *ctx = req->ctx;
1921
1922 spin_lock_irq(&ctx->completion_lock);
1923 io_cqring_fill_event(req, error);
1924 io_commit_cqring(ctx);
1925 spin_unlock_irq(&ctx->completion_lock);
1926
1927 io_cqring_ev_posted(ctx);
1928 req_set_fail_links(req);
1929 io_double_put_req(req);
1930}
1931
1932static void io_req_task_cancel(struct callback_head *cb)
1933{
1934 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
87ceb6a6 1935 struct io_ring_ctx *ctx = req->ctx;
c40f6379
JA
1936
1937 __io_req_task_cancel(req, -ECANCELED);
87ceb6a6 1938 percpu_ref_put(&ctx->refs);
c40f6379
JA
1939}
1940
1941static void __io_req_task_submit(struct io_kiocb *req)
1942{
1943 struct io_ring_ctx *ctx = req->ctx;
1944
c40f6379
JA
1945 if (!__io_sq_thread_acquire_mm(ctx)) {
1946 mutex_lock(&ctx->uring_lock);
1947 __io_queue_sqe(req, NULL, NULL);
1948 mutex_unlock(&ctx->uring_lock);
1949 } else {
1950 __io_req_task_cancel(req, -EFAULT);
1951 }
1952}
1953
1954static void io_req_task_submit(struct callback_head *cb)
1955{
1956 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
6d816e08 1957 struct io_ring_ctx *ctx = req->ctx;
c40f6379
JA
1958
1959 __io_req_task_submit(req);
6d816e08 1960 percpu_ref_put(&ctx->refs);
c40f6379
JA
1961}
1962
1963static void io_req_task_queue(struct io_kiocb *req)
1964{
c40f6379
JA
1965 int ret;
1966
1967 init_task_work(&req->task_work, io_req_task_submit);
6d816e08 1968 percpu_ref_get(&req->ctx->refs);
c40f6379 1969
fd7d6de2 1970 ret = io_req_task_work_add(req, &req->task_work, true);
c40f6379 1971 if (unlikely(ret)) {
c2c4c83c
JA
1972 struct task_struct *tsk;
1973
c40f6379
JA
1974 init_task_work(&req->task_work, io_req_task_cancel);
1975 tsk = io_wq_get_task(req->ctx->io_wq);
c2c4c83c
JA
1976 task_work_add(tsk, &req->task_work, 0);
1977 wake_up_process(tsk);
c40f6379 1978 }
c40f6379
JA
1979}
1980
c3524383 1981static void io_queue_next(struct io_kiocb *req)
c69f8dbe 1982{
9b5f7bd9 1983 struct io_kiocb *nxt = io_req_find_next(req);
944e58bf
PB
1984
1985 if (nxt)
906a8c3f 1986 io_req_task_queue(nxt);
c69f8dbe
JL
1987}
1988
c3524383 1989static void io_free_req(struct io_kiocb *req)
7a743e22 1990{
c3524383
PB
1991 io_queue_next(req);
1992 __io_free_req(req);
1993}
8766dd51 1994
2d6500d4
PB
1995struct req_batch {
1996 void *reqs[IO_IOPOLL_BATCH];
1997 int to_free;
7a743e22 1998
5af1d13e
PB
1999 struct task_struct *task;
2000 int task_refs;
2d6500d4
PB
2001};
2002
5af1d13e
PB
2003static inline void io_init_req_batch(struct req_batch *rb)
2004{
2005 rb->to_free = 0;
2006 rb->task_refs = 0;
2007 rb->task = NULL;
2008}
2009
2d6500d4
PB
2010static void __io_req_free_batch_flush(struct io_ring_ctx *ctx,
2011 struct req_batch *rb)
2012{
2013 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
2014 percpu_ref_put_many(&ctx->refs, rb->to_free);
2015 rb->to_free = 0;
2016}
2017
2018static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
2019 struct req_batch *rb)
2020{
2021 if (rb->to_free)
2022 __io_req_free_batch_flush(ctx, rb);
5af1d13e 2023 if (rb->task) {
0f212204 2024 atomic_long_add(rb->task_refs, &rb->task->io_uring->req_complete);
5af1d13e
PB
2025 put_task_struct_many(rb->task, rb->task_refs);
2026 rb->task = NULL;
2027 }
2d6500d4
PB
2028}
2029
2030static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req)
2031{
2032 if (unlikely(io_is_fallback_req(req))) {
2033 io_free_req(req);
2034 return;
2035 }
2036 if (req->flags & REQ_F_LINK_HEAD)
2037 io_queue_next(req);
2038
e3bc8e9d 2039 if (req->task != rb->task) {
0f212204
JA
2040 if (rb->task) {
2041 atomic_long_add(rb->task_refs, &rb->task->io_uring->req_complete);
e3bc8e9d 2042 put_task_struct_many(rb->task, rb->task_refs);
0f212204 2043 }
e3bc8e9d
JA
2044 rb->task = req->task;
2045 rb->task_refs = 0;
5af1d13e 2046 }
e3bc8e9d 2047 rb->task_refs++;
5af1d13e 2048
51a4cc11 2049 WARN_ON_ONCE(io_dismantle_req(req));
2d6500d4
PB
2050 rb->reqs[rb->to_free++] = req;
2051 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
2052 __io_req_free_batch_flush(req->ctx, rb);
7a743e22
PB
2053}
2054
ba816ad6
JA
2055/*
2056 * Drop reference to request, return next in chain (if there is one) if this
2057 * was the last reference to this request.
2058 */
9b5f7bd9 2059static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
e65ef56d 2060{
9b5f7bd9
PB
2061 struct io_kiocb *nxt = NULL;
2062
2a44f467 2063 if (refcount_dec_and_test(&req->refs)) {
9b5f7bd9 2064 nxt = io_req_find_next(req);
4d7dd462 2065 __io_free_req(req);
2a44f467 2066 }
9b5f7bd9 2067 return nxt;
2b188cc1
JA
2068}
2069
e65ef56d
JA
2070static void io_put_req(struct io_kiocb *req)
2071{
2072 if (refcount_dec_and_test(&req->refs))
2073 io_free_req(req);
2b188cc1
JA
2074}
2075
f4db7182 2076static struct io_wq_work *io_steal_work(struct io_kiocb *req)
7a743e22 2077{
6df1db6b 2078 struct io_kiocb *nxt;
f4db7182 2079
7a743e22 2080 /*
f4db7182
PB
2081 * A ref is owned by io-wq in which context we're. So, if that's the
2082 * last one, it's safe to steal next work. False negatives are Ok,
2083 * it just will be re-punted async in io_put_work()
7a743e22 2084 */
f4db7182
PB
2085 if (refcount_read(&req->refs) != 1)
2086 return NULL;
7a743e22 2087
9b5f7bd9 2088 nxt = io_req_find_next(req);
6df1db6b 2089 return nxt ? &nxt->work : NULL;
7a743e22
PB
2090}
2091
978db57e
JA
2092/*
2093 * Must only be used if we don't need to care about links, usually from
2094 * within the completion handling itself.
2095 */
2096static void __io_double_put_req(struct io_kiocb *req)
78e19bbe
JA
2097{
2098 /* drop both submit and complete references */
2099 if (refcount_sub_and_test(2, &req->refs))
2100 __io_free_req(req);
2101}
2102
978db57e
JA
2103static void io_double_put_req(struct io_kiocb *req)
2104{
2105 /* drop both submit and complete references */
2106 if (refcount_sub_and_test(2, &req->refs))
2107 io_free_req(req);
2108}
2109
1d7bb1d5 2110static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
a3a0e43f 2111{
84f97dc2
JA
2112 struct io_rings *rings = ctx->rings;
2113
ad3eb2c8
JA
2114 if (test_bit(0, &ctx->cq_check_overflow)) {
2115 /*
2116 * noflush == true is from the waitqueue handler, just ensure
2117 * we wake up the task, and the next invocation will flush the
2118 * entries. We cannot safely to it from here.
2119 */
2120 if (noflush && !list_empty(&ctx->cq_overflow_list))
2121 return -1U;
1d7bb1d5 2122
e6c8aa9a 2123 io_cqring_overflow_flush(ctx, false, NULL, NULL);
ad3eb2c8 2124 }
1d7bb1d5 2125
a3a0e43f
JA
2126 /* See comment at the top of this file */
2127 smp_rmb();
ad3eb2c8 2128 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
a3a0e43f
JA
2129}
2130
fb5ccc98
PB
2131static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2132{
2133 struct io_rings *rings = ctx->rings;
2134
2135 /* make sure SQ entry isn't read before tail */
2136 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2137}
2138
8ff069bf 2139static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
e94f141b 2140{
8ff069bf 2141 unsigned int cflags;
e94f141b 2142
bcda7baa
JA
2143 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2144 cflags |= IORING_CQE_F_BUFFER;
0e1b6fe3 2145 req->flags &= ~REQ_F_BUFFER_SELECTED;
bcda7baa
JA
2146 kfree(kbuf);
2147 return cflags;
e94f141b
JA
2148}
2149
8ff069bf 2150static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
bcda7baa 2151{
4d954c25 2152 struct io_buffer *kbuf;
bcda7baa 2153
4d954c25 2154 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
8ff069bf
PB
2155 return io_put_kbuf(req, kbuf);
2156}
2157
4c6e277c
JA
2158static inline bool io_run_task_work(void)
2159{
6200b0ae
JA
2160 /*
2161 * Not safe to run on exiting task, and the task_work handling will
2162 * not add work to such a task.
2163 */
2164 if (unlikely(current->flags & PF_EXITING))
2165 return false;
4c6e277c
JA
2166 if (current->task_works) {
2167 __set_current_state(TASK_RUNNING);
2168 task_work_run();
2169 return true;
2170 }
2171
2172 return false;
bcda7baa
JA
2173}
2174
bbde017a
XW
2175static void io_iopoll_queue(struct list_head *again)
2176{
2177 struct io_kiocb *req;
2178
2179 do {
d21ffe7e
PB
2180 req = list_first_entry(again, struct io_kiocb, inflight_entry);
2181 list_del(&req->inflight_entry);
81b68a5c 2182 __io_complete_rw(req, -EAGAIN, 0, NULL);
bbde017a
XW
2183 } while (!list_empty(again));
2184}
2185
def596e9
JA
2186/*
2187 * Find and free completed poll iocbs
2188 */
2189static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
2190 struct list_head *done)
2191{
8237e045 2192 struct req_batch rb;
def596e9 2193 struct io_kiocb *req;
bbde017a
XW
2194 LIST_HEAD(again);
2195
2196 /* order with ->result store in io_complete_rw_iopoll() */
2197 smp_rmb();
def596e9 2198
5af1d13e 2199 io_init_req_batch(&rb);
def596e9 2200 while (!list_empty(done)) {
bcda7baa
JA
2201 int cflags = 0;
2202
d21ffe7e 2203 req = list_first_entry(done, struct io_kiocb, inflight_entry);
bbde017a 2204 if (READ_ONCE(req->result) == -EAGAIN) {
56450c20 2205 req->result = 0;
bbde017a 2206 req->iopoll_completed = 0;
d21ffe7e 2207 list_move_tail(&req->inflight_entry, &again);
bbde017a
XW
2208 continue;
2209 }
d21ffe7e 2210 list_del(&req->inflight_entry);
def596e9 2211
bcda7baa 2212 if (req->flags & REQ_F_BUFFER_SELECTED)
8ff069bf 2213 cflags = io_put_rw_kbuf(req);
bcda7baa
JA
2214
2215 __io_cqring_fill_event(req, req->result, cflags);
def596e9
JA
2216 (*nr_events)++;
2217
c3524383 2218 if (refcount_dec_and_test(&req->refs))
2d6500d4 2219 io_req_free_batch(&rb, req);
def596e9 2220 }
def596e9 2221
09bb8394 2222 io_commit_cqring(ctx);
32b2244a
XW
2223 if (ctx->flags & IORING_SETUP_SQPOLL)
2224 io_cqring_ev_posted(ctx);
2d6500d4 2225 io_req_free_batch_finish(ctx, &rb);
581f9810 2226
bbde017a
XW
2227 if (!list_empty(&again))
2228 io_iopoll_queue(&again);
581f9810
BM
2229}
2230
def596e9
JA
2231static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2232 long min)
2233{
2234 struct io_kiocb *req, *tmp;
2235 LIST_HEAD(done);
2236 bool spin;
2237 int ret;
2238
2239 /*
2240 * Only spin for completions if we don't have multiple devices hanging
2241 * off our complete list, and we're under the requested amount.
2242 */
2243 spin = !ctx->poll_multi_file && *nr_events < min;
2244
2245 ret = 0;
d21ffe7e 2246 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
9adbd45d 2247 struct kiocb *kiocb = &req->rw.kiocb;
def596e9
JA
2248
2249 /*
581f9810
BM
2250 * Move completed and retryable entries to our local lists.
2251 * If we find a request that requires polling, break out
2252 * and complete those lists first, if we have entries there.
def596e9 2253 */
65a6543d 2254 if (READ_ONCE(req->iopoll_completed)) {
d21ffe7e 2255 list_move_tail(&req->inflight_entry, &done);
def596e9
JA
2256 continue;
2257 }
2258 if (!list_empty(&done))
2259 break;
2260
2261 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2262 if (ret < 0)
2263 break;
2264
3aadc23e
PB
2265 /* iopoll may have completed current req */
2266 if (READ_ONCE(req->iopoll_completed))
d21ffe7e 2267 list_move_tail(&req->inflight_entry, &done);
3aadc23e 2268
def596e9
JA
2269 if (ret && spin)
2270 spin = false;
2271 ret = 0;
2272 }
2273
2274 if (!list_empty(&done))
2275 io_iopoll_complete(ctx, nr_events, &done);
2276
2277 return ret;
2278}
2279
2280/*
d195a66e 2281 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
def596e9
JA
2282 * non-spinning poll check - we'll still enter the driver poll loop, but only
2283 * as a non-spinning completion check.
2284 */
2285static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2286 long min)
2287{
540e32a0 2288 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
def596e9
JA
2289 int ret;
2290
2291 ret = io_do_iopoll(ctx, nr_events, min);
2292 if (ret < 0)
2293 return ret;
eba0a4dd 2294 if (*nr_events >= min)
def596e9
JA
2295 return 0;
2296 }
2297
2298 return 1;
2299}
2300
2301/*
2302 * We can't just wait for polled events to come to us, we have to actively
2303 * find and complete them.
2304 */
b2edc0a7 2305static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
def596e9
JA
2306{
2307 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2308 return;
2309
2310 mutex_lock(&ctx->uring_lock);
540e32a0 2311 while (!list_empty(&ctx->iopoll_list)) {
def596e9
JA
2312 unsigned int nr_events = 0;
2313
b2edc0a7 2314 io_do_iopoll(ctx, &nr_events, 0);
08f5439f 2315
b2edc0a7
PB
2316 /* let it sleep and repeat later if can't complete a request */
2317 if (nr_events == 0)
2318 break;
08f5439f
JA
2319 /*
2320 * Ensure we allow local-to-the-cpu processing to take place,
2321 * in this case we need to ensure that we reap all events.
3fcee5a6 2322 * Also let task_work, etc. to progress by releasing the mutex
08f5439f 2323 */
3fcee5a6
PB
2324 if (need_resched()) {
2325 mutex_unlock(&ctx->uring_lock);
2326 cond_resched();
2327 mutex_lock(&ctx->uring_lock);
2328 }
def596e9
JA
2329 }
2330 mutex_unlock(&ctx->uring_lock);
2331}
2332
7668b92a 2333static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
def596e9 2334{
7668b92a 2335 unsigned int nr_events = 0;
2b2ed975 2336 int iters = 0, ret = 0;
500f9fba 2337
c7849be9
XW
2338 /*
2339 * We disallow the app entering submit/complete with polling, but we
2340 * still need to lock the ring to prevent racing with polled issue
2341 * that got punted to a workqueue.
2342 */
2343 mutex_lock(&ctx->uring_lock);
def596e9 2344 do {
a3a0e43f
JA
2345 /*
2346 * Don't enter poll loop if we already have events pending.
2347 * If we do, we can potentially be spinning for commands that
2348 * already triggered a CQE (eg in error).
2349 */
1d7bb1d5 2350 if (io_cqring_events(ctx, false))
a3a0e43f
JA
2351 break;
2352
500f9fba
JA
2353 /*
2354 * If a submit got punted to a workqueue, we can have the
2355 * application entering polling for a command before it gets
2356 * issued. That app will hold the uring_lock for the duration
2357 * of the poll right here, so we need to take a breather every
2358 * now and then to ensure that the issue has a chance to add
2359 * the poll to the issued list. Otherwise we can spin here
2360 * forever, while the workqueue is stuck trying to acquire the
2361 * very same mutex.
2362 */
2363 if (!(++iters & 7)) {
2364 mutex_unlock(&ctx->uring_lock);
4c6e277c 2365 io_run_task_work();
500f9fba
JA
2366 mutex_lock(&ctx->uring_lock);
2367 }
2368
7668b92a 2369 ret = io_iopoll_getevents(ctx, &nr_events, min);
def596e9
JA
2370 if (ret <= 0)
2371 break;
2372 ret = 0;
7668b92a 2373 } while (min && !nr_events && !need_resched());
def596e9 2374
500f9fba 2375 mutex_unlock(&ctx->uring_lock);
def596e9
JA
2376 return ret;
2377}
2378
491381ce 2379static void kiocb_end_write(struct io_kiocb *req)
2b188cc1 2380{
491381ce
JA
2381 /*
2382 * Tell lockdep we inherited freeze protection from submission
2383 * thread.
2384 */
2385 if (req->flags & REQ_F_ISREG) {
2386 struct inode *inode = file_inode(req->file);
2b188cc1 2387
491381ce 2388 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
2b188cc1 2389 }
491381ce 2390 file_end_write(req->file);
2b188cc1
JA
2391}
2392
a1d7c393
JA
2393static void io_complete_rw_common(struct kiocb *kiocb, long res,
2394 struct io_comp_state *cs)
2b188cc1 2395{
9adbd45d 2396 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
bcda7baa 2397 int cflags = 0;
2b188cc1 2398
491381ce
JA
2399 if (kiocb->ki_flags & IOCB_WRITE)
2400 kiocb_end_write(req);
2b188cc1 2401
4e88d6e7
JA
2402 if (res != req->result)
2403 req_set_fail_links(req);
bcda7baa 2404 if (req->flags & REQ_F_BUFFER_SELECTED)
8ff069bf 2405 cflags = io_put_rw_kbuf(req);
a1d7c393 2406 __io_req_complete(req, res, cflags, cs);
ba816ad6
JA
2407}
2408
b63534c4
JA
2409#ifdef CONFIG_BLOCK
2410static bool io_resubmit_prep(struct io_kiocb *req, int error)
2411{
2412 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
2413 ssize_t ret = -ECANCELED;
2414 struct iov_iter iter;
2415 int rw;
2416
2417 if (error) {
2418 ret = error;
2419 goto end_req;
2420 }
2421
2422 switch (req->opcode) {
2423 case IORING_OP_READV:
2424 case IORING_OP_READ_FIXED:
2425 case IORING_OP_READ:
2426 rw = READ;
2427 break;
2428 case IORING_OP_WRITEV:
2429 case IORING_OP_WRITE_FIXED:
2430 case IORING_OP_WRITE:
2431 rw = WRITE;
2432 break;
2433 default:
2434 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2435 req->opcode);
2436 goto end_req;
2437 }
2438
e8c2bc1f 2439 if (!req->async_data) {
8f3d7496
JA
2440 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2441 if (ret < 0)
2442 goto end_req;
2443 ret = io_setup_async_rw(req, iovec, inline_vecs, &iter, false);
2444 if (!ret)
2445 return true;
2446 kfree(iovec);
2447 } else {
b63534c4 2448 return true;
8f3d7496 2449 }
b63534c4 2450end_req:
b63534c4 2451 req_set_fail_links(req);
e1e16097 2452 io_req_complete(req, ret);
b63534c4
JA
2453 return false;
2454}
b63534c4
JA
2455#endif
2456
2457static bool io_rw_reissue(struct io_kiocb *req, long res)
2458{
2459#ifdef CONFIG_BLOCK
355afaeb 2460 umode_t mode = file_inode(req->file)->i_mode;
b63534c4
JA
2461 int ret;
2462
355afaeb
JA
2463 if (!S_ISBLK(mode) && !S_ISREG(mode))
2464 return false;
b63534c4
JA
2465 if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker())
2466 return false;
2467
fdee946d 2468 ret = io_sq_thread_acquire_mm(req->ctx, req);
6d816e08 2469
fdee946d
JA
2470 if (io_resubmit_prep(req, ret)) {
2471 refcount_inc(&req->refs);
2472 io_queue_async_work(req);
b63534c4 2473 return true;
fdee946d
JA
2474 }
2475
b63534c4
JA
2476#endif
2477 return false;
2478}
2479
a1d7c393
JA
2480static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2481 struct io_comp_state *cs)
2482{
2483 if (!io_rw_reissue(req, res))
2484 io_complete_rw_common(&req->rw.kiocb, res, cs);
ba816ad6
JA
2485}
2486
2487static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2488{
9adbd45d 2489 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
ba816ad6 2490
a1d7c393 2491 __io_complete_rw(req, res, res2, NULL);
2b188cc1
JA
2492}
2493
def596e9
JA
2494static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2495{
9adbd45d 2496 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
def596e9 2497
491381ce
JA
2498 if (kiocb->ki_flags & IOCB_WRITE)
2499 kiocb_end_write(req);
def596e9 2500
2d7d6792 2501 if (res != -EAGAIN && res != req->result)
4e88d6e7 2502 req_set_fail_links(req);
bbde017a
XW
2503
2504 WRITE_ONCE(req->result, res);
2505 /* order with io_poll_complete() checking ->result */
cd664b0e
PB
2506 smp_wmb();
2507 WRITE_ONCE(req->iopoll_completed, 1);
def596e9
JA
2508}
2509
2510/*
2511 * After the iocb has been issued, it's safe to be found on the poll list.
2512 * Adding the kiocb to the list AFTER submission ensures that we don't
2513 * find it from a io_iopoll_getevents() thread before the issuer is done
2514 * accessing the kiocb cookie.
2515 */
2516static void io_iopoll_req_issued(struct io_kiocb *req)
2517{
2518 struct io_ring_ctx *ctx = req->ctx;
2519
2520 /*
2521 * Track whether we have multiple files in our lists. This will impact
2522 * how we do polling eventually, not spinning if we're on potentially
2523 * different devices.
2524 */
540e32a0 2525 if (list_empty(&ctx->iopoll_list)) {
def596e9
JA
2526 ctx->poll_multi_file = false;
2527 } else if (!ctx->poll_multi_file) {
2528 struct io_kiocb *list_req;
2529
540e32a0 2530 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
d21ffe7e 2531 inflight_entry);
9adbd45d 2532 if (list_req->file != req->file)
def596e9
JA
2533 ctx->poll_multi_file = true;
2534 }
2535
2536 /*
2537 * For fast devices, IO may have already completed. If it has, add
2538 * it to the front so we find it first.
2539 */
65a6543d 2540 if (READ_ONCE(req->iopoll_completed))
d21ffe7e 2541 list_add(&req->inflight_entry, &ctx->iopoll_list);
def596e9 2542 else
d21ffe7e 2543 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
bdcd3eab 2544
534ca6d6
JA
2545 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
2546 wq_has_sleeper(&ctx->sq_data->wait))
2547 wake_up(&ctx->sq_data->wait);
def596e9
JA
2548}
2549
9f13c35b 2550static void __io_state_file_put(struct io_submit_state *state)
9a56a232 2551{
06ef3608
PB
2552 if (state->has_refs)
2553 fput_many(state->file, state->has_refs);
9f13c35b
PB
2554 state->file = NULL;
2555}
2556
2557static inline void io_state_file_put(struct io_submit_state *state)
2558{
2559 if (state->file)
2560 __io_state_file_put(state);
9a56a232
JA
2561}
2562
2563/*
2564 * Get as many references to a file as we have IOs left in this submission,
2565 * assuming most submissions are for one file, or at least that each file
2566 * has more than one submission.
2567 */
8da11c19 2568static struct file *__io_file_get(struct io_submit_state *state, int fd)
9a56a232
JA
2569{
2570 if (!state)
2571 return fget(fd);
2572
2573 if (state->file) {
2574 if (state->fd == fd) {
06ef3608 2575 state->has_refs--;
9a56a232
JA
2576 state->ios_left--;
2577 return state->file;
2578 }
9f13c35b 2579 __io_state_file_put(state);
9a56a232
JA
2580 }
2581 state->file = fget_many(fd, state->ios_left);
2582 if (!state->file)
2583 return NULL;
2584
2585 state->fd = fd;
9a56a232 2586 state->ios_left--;
06ef3608 2587 state->has_refs = state->ios_left;
9a56a232
JA
2588 return state->file;
2589}
2590
4503b767
JA
2591static bool io_bdev_nowait(struct block_device *bdev)
2592{
2593#ifdef CONFIG_BLOCK
2594 return !bdev || queue_is_mq(bdev_get_queue(bdev));
2595#else
2596 return true;
2597#endif
2598}
2599
2b188cc1
JA
2600/*
2601 * If we tracked the file through the SCM inflight mechanism, we could support
2602 * any file. For now, just ensure that anything potentially problematic is done
2603 * inline.
2604 */
af197f50 2605static bool io_file_supports_async(struct file *file, int rw)
2b188cc1
JA
2606{
2607 umode_t mode = file_inode(file)->i_mode;
2608
4503b767
JA
2609 if (S_ISBLK(mode)) {
2610 if (io_bdev_nowait(file->f_inode->i_bdev))
2611 return true;
2612 return false;
2613 }
2614 if (S_ISCHR(mode) || S_ISSOCK(mode))
2b188cc1 2615 return true;
4503b767
JA
2616 if (S_ISREG(mode)) {
2617 if (io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
2618 file->f_op != &io_uring_fops)
2619 return true;
2620 return false;
2621 }
2b188cc1 2622
c5b85625
JA
2623 /* any ->read/write should understand O_NONBLOCK */
2624 if (file->f_flags & O_NONBLOCK)
2625 return true;
2626
af197f50
JA
2627 if (!(file->f_mode & FMODE_NOWAIT))
2628 return false;
2629
2630 if (rw == READ)
2631 return file->f_op->read_iter != NULL;
2632
2633 return file->f_op->write_iter != NULL;
2b188cc1
JA
2634}
2635
3529d8c2
JA
2636static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2637 bool force_nonblock)
2b188cc1 2638{
def596e9 2639 struct io_ring_ctx *ctx = req->ctx;
9adbd45d 2640 struct kiocb *kiocb = &req->rw.kiocb;
09bb8394
JA
2641 unsigned ioprio;
2642 int ret;
2b188cc1 2643
491381ce
JA
2644 if (S_ISREG(file_inode(req->file)->i_mode))
2645 req->flags |= REQ_F_ISREG;
2646
2b188cc1 2647 kiocb->ki_pos = READ_ONCE(sqe->off);
ba04291e
JA
2648 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2649 req->flags |= REQ_F_CUR_POS;
2650 kiocb->ki_pos = req->file->f_pos;
2651 }
2b188cc1 2652 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
3e577dcd
PB
2653 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2654 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2655 if (unlikely(ret))
2656 return ret;
2b188cc1
JA
2657
2658 ioprio = READ_ONCE(sqe->ioprio);
2659 if (ioprio) {
2660 ret = ioprio_check_cap(ioprio);
2661 if (ret)
09bb8394 2662 return ret;
2b188cc1
JA
2663
2664 kiocb->ki_ioprio = ioprio;
2665 } else
2666 kiocb->ki_ioprio = get_current_ioprio();
2667
8449eeda 2668 /* don't allow async punt if RWF_NOWAIT was requested */
c5b85625 2669 if (kiocb->ki_flags & IOCB_NOWAIT)
8449eeda
SB
2670 req->flags |= REQ_F_NOWAIT;
2671
2672 if (force_nonblock)
2b188cc1 2673 kiocb->ki_flags |= IOCB_NOWAIT;
8449eeda 2674
def596e9 2675 if (ctx->flags & IORING_SETUP_IOPOLL) {
def596e9
JA
2676 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2677 !kiocb->ki_filp->f_op->iopoll)
09bb8394 2678 return -EOPNOTSUPP;
2b188cc1 2679
def596e9
JA
2680 kiocb->ki_flags |= IOCB_HIPRI;
2681 kiocb->ki_complete = io_complete_rw_iopoll;
65a6543d 2682 req->iopoll_completed = 0;
def596e9 2683 } else {
09bb8394
JA
2684 if (kiocb->ki_flags & IOCB_HIPRI)
2685 return -EINVAL;
def596e9
JA
2686 kiocb->ki_complete = io_complete_rw;
2687 }
9adbd45d 2688
3529d8c2
JA
2689 req->rw.addr = READ_ONCE(sqe->addr);
2690 req->rw.len = READ_ONCE(sqe->len);
4f4eeba8 2691 req->buf_index = READ_ONCE(sqe->buf_index);
2b188cc1 2692 return 0;
2b188cc1
JA
2693}
2694
2695static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2696{
2697 switch (ret) {
2698 case -EIOCBQUEUED:
2699 break;
2700 case -ERESTARTSYS:
2701 case -ERESTARTNOINTR:
2702 case -ERESTARTNOHAND:
2703 case -ERESTART_RESTARTBLOCK:
2704 /*
2705 * We can't just restart the syscall, since previously
2706 * submitted sqes may already be in progress. Just fail this
2707 * IO with EINTR.
2708 */
2709 ret = -EINTR;
df561f66 2710 fallthrough;
2b188cc1
JA
2711 default:
2712 kiocb->ki_complete(kiocb, ret, 0);
2713 }
2714}
2715
a1d7c393
JA
2716static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
2717 struct io_comp_state *cs)
ba816ad6 2718{
ba04291e 2719 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
e8c2bc1f 2720 struct io_async_rw *io = req->async_data;
ba04291e 2721
227c0c96 2722 /* add previously done IO, if any */
e8c2bc1f 2723 if (io && io->bytes_done > 0) {
227c0c96 2724 if (ret < 0)
e8c2bc1f 2725 ret = io->bytes_done;
227c0c96 2726 else
e8c2bc1f 2727 ret += io->bytes_done;
227c0c96
JA
2728 }
2729
ba04291e
JA
2730 if (req->flags & REQ_F_CUR_POS)
2731 req->file->f_pos = kiocb->ki_pos;
bcaec089 2732 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
a1d7c393 2733 __io_complete_rw(req, ret, 0, cs);
ba816ad6
JA
2734 else
2735 io_rw_done(kiocb, ret);
2736}
2737
9adbd45d 2738static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
7d009165 2739 struct iov_iter *iter)
edafccee 2740{
9adbd45d
JA
2741 struct io_ring_ctx *ctx = req->ctx;
2742 size_t len = req->rw.len;
edafccee 2743 struct io_mapped_ubuf *imu;
4be1c615 2744 u16 index, buf_index = req->buf_index;
edafccee
JA
2745 size_t offset;
2746 u64 buf_addr;
2747
edafccee
JA
2748 if (unlikely(buf_index >= ctx->nr_user_bufs))
2749 return -EFAULT;
edafccee
JA
2750 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2751 imu = &ctx->user_bufs[index];
9adbd45d 2752 buf_addr = req->rw.addr;
edafccee
JA
2753
2754 /* overflow */
2755 if (buf_addr + len < buf_addr)
2756 return -EFAULT;
2757 /* not inside the mapped region */
2758 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2759 return -EFAULT;
2760
2761 /*
2762 * May not be a start of buffer, set size appropriately
2763 * and advance us to the beginning.
2764 */
2765 offset = buf_addr - imu->ubuf;
2766 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
bd11b3a3
JA
2767
2768 if (offset) {
2769 /*
2770 * Don't use iov_iter_advance() here, as it's really slow for
2771 * using the latter parts of a big fixed buffer - it iterates
2772 * over each segment manually. We can cheat a bit here, because
2773 * we know that:
2774 *
2775 * 1) it's a BVEC iter, we set it up
2776 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2777 * first and last bvec
2778 *
2779 * So just find our index, and adjust the iterator afterwards.
2780 * If the offset is within the first bvec (or the whole first
2781 * bvec, just use iov_iter_advance(). This makes it easier
2782 * since we can just skip the first segment, which may not
2783 * be PAGE_SIZE aligned.
2784 */
2785 const struct bio_vec *bvec = imu->bvec;
2786
2787 if (offset <= bvec->bv_len) {
2788 iov_iter_advance(iter, offset);
2789 } else {
2790 unsigned long seg_skip;
2791
2792 /* skip first vec */
2793 offset -= bvec->bv_len;
2794 seg_skip = 1 + (offset >> PAGE_SHIFT);
2795
2796 iter->bvec = bvec + seg_skip;
2797 iter->nr_segs -= seg_skip;
99c79f66 2798 iter->count -= bvec->bv_len + offset;
bd11b3a3 2799 iter->iov_offset = offset & ~PAGE_MASK;
bd11b3a3
JA
2800 }
2801 }
2802
5e559561 2803 return len;
edafccee
JA
2804}
2805
bcda7baa
JA
2806static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2807{
2808 if (needs_lock)
2809 mutex_unlock(&ctx->uring_lock);
2810}
2811
2812static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2813{
2814 /*
2815 * "Normal" inline submissions always hold the uring_lock, since we
2816 * grab it from the system call. Same is true for the SQPOLL offload.
2817 * The only exception is when we've detached the request and issue it
2818 * from an async worker thread, grab the lock for that case.
2819 */
2820 if (needs_lock)
2821 mutex_lock(&ctx->uring_lock);
2822}
2823
2824static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2825 int bgid, struct io_buffer *kbuf,
2826 bool needs_lock)
2827{
2828 struct io_buffer *head;
2829
2830 if (req->flags & REQ_F_BUFFER_SELECTED)
2831 return kbuf;
2832
2833 io_ring_submit_lock(req->ctx, needs_lock);
2834
2835 lockdep_assert_held(&req->ctx->uring_lock);
2836
2837 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2838 if (head) {
2839 if (!list_empty(&head->list)) {
2840 kbuf = list_last_entry(&head->list, struct io_buffer,
2841 list);
2842 list_del(&kbuf->list);
2843 } else {
2844 kbuf = head;
2845 idr_remove(&req->ctx->io_buffer_idr, bgid);
2846 }
2847 if (*len > kbuf->len)
2848 *len = kbuf->len;
2849 } else {
2850 kbuf = ERR_PTR(-ENOBUFS);
2851 }
2852
2853 io_ring_submit_unlock(req->ctx, needs_lock);
2854
2855 return kbuf;
2856}
2857
4d954c25
JA
2858static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2859 bool needs_lock)
2860{
2861 struct io_buffer *kbuf;
4f4eeba8 2862 u16 bgid;
4d954c25
JA
2863
2864 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
4f4eeba8 2865 bgid = req->buf_index;
4d954c25
JA
2866 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2867 if (IS_ERR(kbuf))
2868 return kbuf;
2869 req->rw.addr = (u64) (unsigned long) kbuf;
2870 req->flags |= REQ_F_BUFFER_SELECTED;
2871 return u64_to_user_ptr(kbuf->addr);
2872}
2873
2874#ifdef CONFIG_COMPAT
2875static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2876 bool needs_lock)
2877{
2878 struct compat_iovec __user *uiov;
2879 compat_ssize_t clen;
2880 void __user *buf;
2881 ssize_t len;
2882
2883 uiov = u64_to_user_ptr(req->rw.addr);
2884 if (!access_ok(uiov, sizeof(*uiov)))
2885 return -EFAULT;
2886 if (__get_user(clen, &uiov->iov_len))
2887 return -EFAULT;
2888 if (clen < 0)
2889 return -EINVAL;
2890
2891 len = clen;
2892 buf = io_rw_buffer_select(req, &len, needs_lock);
2893 if (IS_ERR(buf))
2894 return PTR_ERR(buf);
2895 iov[0].iov_base = buf;
2896 iov[0].iov_len = (compat_size_t) len;
2897 return 0;
2898}
2899#endif
2900
2901static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2902 bool needs_lock)
2903{
2904 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2905 void __user *buf;
2906 ssize_t len;
2907
2908 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2909 return -EFAULT;
2910
2911 len = iov[0].iov_len;
2912 if (len < 0)
2913 return -EINVAL;
2914 buf = io_rw_buffer_select(req, &len, needs_lock);
2915 if (IS_ERR(buf))
2916 return PTR_ERR(buf);
2917 iov[0].iov_base = buf;
2918 iov[0].iov_len = len;
2919 return 0;
2920}
2921
2922static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2923 bool needs_lock)
2924{
dddb3e26
JA
2925 if (req->flags & REQ_F_BUFFER_SELECTED) {
2926 struct io_buffer *kbuf;
2927
2928 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2929 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
2930 iov[0].iov_len = kbuf->len;
4d954c25 2931 return 0;
dddb3e26 2932 }
4d954c25
JA
2933 if (!req->rw.len)
2934 return 0;
2935 else if (req->rw.len > 1)
2936 return -EINVAL;
2937
2938#ifdef CONFIG_COMPAT
2939 if (req->ctx->compat)
2940 return io_compat_import(req, iov, needs_lock);
2941#endif
2942
2943 return __io_iov_buffer_select(req, iov, needs_lock);
2944}
2945
8452fd0c
JA
2946static ssize_t __io_import_iovec(int rw, struct io_kiocb *req,
2947 struct iovec **iovec, struct iov_iter *iter,
2948 bool needs_lock)
2b188cc1 2949{
9adbd45d
JA
2950 void __user *buf = u64_to_user_ptr(req->rw.addr);
2951 size_t sqe_len = req->rw.len;
4d954c25 2952 ssize_t ret;
edafccee
JA
2953 u8 opcode;
2954
d625c6ee 2955 opcode = req->opcode;
7d009165 2956 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
edafccee 2957 *iovec = NULL;
9adbd45d 2958 return io_import_fixed(req, rw, iter);
edafccee 2959 }
2b188cc1 2960
bcda7baa 2961 /* buffer index only valid with fixed read/write, or buffer select */
4f4eeba8 2962 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
9adbd45d
JA
2963 return -EINVAL;
2964
3a6820f2 2965 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
bcda7baa 2966 if (req->flags & REQ_F_BUFFER_SELECT) {
4d954c25 2967 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
867a23ea 2968 if (IS_ERR(buf))
4d954c25 2969 return PTR_ERR(buf);
3f9d6441 2970 req->rw.len = sqe_len;
bcda7baa
JA
2971 }
2972
3a6820f2
JA
2973 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2974 *iovec = NULL;
3a901598 2975 return ret < 0 ? ret : sqe_len;
3a6820f2
JA
2976 }
2977
4d954c25
JA
2978 if (req->flags & REQ_F_BUFFER_SELECT) {
2979 ret = io_iov_buffer_select(req, *iovec, needs_lock);
3f9d6441
JA
2980 if (!ret) {
2981 ret = (*iovec)->iov_len;
2982 iov_iter_init(iter, rw, *iovec, 1, ret);
2983 }
4d954c25
JA
2984 *iovec = NULL;
2985 return ret;
2986 }
2987
2b188cc1 2988#ifdef CONFIG_COMPAT
cf6fd4bd 2989 if (req->ctx->compat)
2b188cc1
JA
2990 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2991 iovec, iter);
2992#endif
2993
2994 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2995}
2996
8452fd0c
JA
2997static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
2998 struct iovec **iovec, struct iov_iter *iter,
2999 bool needs_lock)
3000{
e8c2bc1f
JA
3001 struct io_async_rw *iorw = req->async_data;
3002
3003 if (!iorw)
8452fd0c
JA
3004 return __io_import_iovec(rw, req, iovec, iter, needs_lock);
3005 *iovec = NULL;
e8c2bc1f 3006 return iov_iter_count(&iorw->iter);
8452fd0c
JA
3007}
3008
0fef9483
JA
3009static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
3010{
3011 return kiocb->ki_filp->f_mode & FMODE_STREAM ? NULL : &kiocb->ki_pos;
3012}
3013
31b51510 3014/*
32960613
JA
3015 * For files that don't have ->read_iter() and ->write_iter(), handle them
3016 * by looping over ->read() or ->write() manually.
31b51510 3017 */
32960613
JA
3018static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
3019 struct iov_iter *iter)
3020{
3021 ssize_t ret = 0;
3022
3023 /*
3024 * Don't support polled IO through this interface, and we can't
3025 * support non-blocking either. For the latter, this just causes
3026 * the kiocb to be handled from an async context.
3027 */
3028 if (kiocb->ki_flags & IOCB_HIPRI)
3029 return -EOPNOTSUPP;
3030 if (kiocb->ki_flags & IOCB_NOWAIT)
3031 return -EAGAIN;
3032
3033 while (iov_iter_count(iter)) {
311ae9e1 3034 struct iovec iovec;
32960613
JA
3035 ssize_t nr;
3036
311ae9e1
PB
3037 if (!iov_iter_is_bvec(iter)) {
3038 iovec = iov_iter_iovec(iter);
3039 } else {
3040 /* fixed buffers import bvec */
3041 iovec.iov_base = kmap(iter->bvec->bv_page)
3042 + iter->iov_offset;
3043 iovec.iov_len = min(iter->count,
3044 iter->bvec->bv_len - iter->iov_offset);
3045 }
3046
32960613
JA
3047 if (rw == READ) {
3048 nr = file->f_op->read(file, iovec.iov_base,
0fef9483 3049 iovec.iov_len, io_kiocb_ppos(kiocb));
32960613
JA
3050 } else {
3051 nr = file->f_op->write(file, iovec.iov_base,
0fef9483 3052 iovec.iov_len, io_kiocb_ppos(kiocb));
32960613
JA
3053 }
3054
311ae9e1
PB
3055 if (iov_iter_is_bvec(iter))
3056 kunmap(iter->bvec->bv_page);
3057
32960613
JA
3058 if (nr < 0) {
3059 if (!ret)
3060 ret = nr;
3061 break;
3062 }
3063 ret += nr;
3064 if (nr != iovec.iov_len)
3065 break;
3066 iov_iter_advance(iter, nr);
3067 }
3068
3069 return ret;
3070}
3071
ff6165b2
JA
3072static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3073 const struct iovec *fast_iov, struct iov_iter *iter)
f67676d1 3074{
e8c2bc1f 3075 struct io_async_rw *rw = req->async_data;
b64e3444 3076
ff6165b2 3077 memcpy(&rw->iter, iter, sizeof(*iter));
afb87658 3078 rw->free_iovec = iovec;
227c0c96 3079 rw->bytes_done = 0;
ff6165b2
JA
3080 /* can only be fixed buffers, no need to do anything */
3081 if (iter->type == ITER_BVEC)
3082 return;
b64e3444 3083 if (!iovec) {
ff6165b2
JA
3084 unsigned iov_off = 0;
3085
3086 rw->iter.iov = rw->fast_iov;
3087 if (iter->iov != fast_iov) {
3088 iov_off = iter->iov - fast_iov;
3089 rw->iter.iov += iov_off;
3090 }
3091 if (rw->fast_iov != fast_iov)
3092 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
45097dae 3093 sizeof(struct iovec) * iter->nr_segs);
99bc4c38
PB
3094 } else {
3095 req->flags |= REQ_F_NEED_CLEANUP;
f67676d1
JA
3096 }
3097}
3098
e8c2bc1f 3099static inline int __io_alloc_async_data(struct io_kiocb *req)
3d9932a8 3100{
e8c2bc1f
JA
3101 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3102 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
3103 return req->async_data == NULL;
3d9932a8
XW
3104}
3105
e8c2bc1f 3106static int io_alloc_async_data(struct io_kiocb *req)
f67676d1 3107{
e8c2bc1f 3108 if (!io_op_defs[req->opcode].needs_async_data)
d3656344 3109 return 0;
3d9932a8 3110
e8c2bc1f 3111 return __io_alloc_async_data(req);
b7bb4f7d
JA
3112}
3113
ff6165b2
JA
3114static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3115 const struct iovec *fast_iov,
227c0c96 3116 struct iov_iter *iter, bool force)
b7bb4f7d 3117{
e8c2bc1f 3118 if (!force && !io_op_defs[req->opcode].needs_async_data)
74566df3 3119 return 0;
e8c2bc1f
JA
3120 if (!req->async_data) {
3121 if (__io_alloc_async_data(req))
5d204bcf 3122 return -ENOMEM;
b7bb4f7d 3123
ff6165b2 3124 io_req_map_rw(req, iovec, fast_iov, iter);
5d204bcf 3125 }
b7bb4f7d 3126 return 0;
f67676d1
JA
3127}
3128
c3e330a4
PB
3129static inline int io_rw_prep_async(struct io_kiocb *req, int rw,
3130 bool force_nonblock)
3131{
e8c2bc1f 3132 struct io_async_rw *iorw = req->async_data;
f4bff104 3133 struct iovec *iov = iorw->fast_iov;
c3e330a4
PB
3134 ssize_t ret;
3135
c183edff 3136 ret = __io_import_iovec(rw, req, &iov, &iorw->iter, !force_nonblock);
c3e330a4
PB
3137 if (unlikely(ret < 0))
3138 return ret;
3139
ab0b196c
PB
3140 iorw->bytes_done = 0;
3141 iorw->free_iovec = iov;
3142 if (iov)
3143 req->flags |= REQ_F_NEED_CLEANUP;
c3e330a4
PB
3144 return 0;
3145}
3146
3529d8c2
JA
3147static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3148 bool force_nonblock)
f67676d1
JA
3149{
3150 ssize_t ret;
3151
3529d8c2
JA
3152 ret = io_prep_rw(req, sqe, force_nonblock);
3153 if (ret)
3154 return ret;
f67676d1 3155
3529d8c2
JA
3156 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3157 return -EBADF;
f67676d1 3158
5f798bea 3159 /* either don't need iovec imported or already have it */
e8c2bc1f 3160 if (!req->async_data || req->flags & REQ_F_NEED_CLEANUP)
3529d8c2 3161 return 0;
c3e330a4 3162 return io_rw_prep_async(req, READ, force_nonblock);
f67676d1
JA
3163}
3164
c1dd91d1
JA
3165/*
3166 * This is our waitqueue callback handler, registered through lock_page_async()
3167 * when we initially tried to do the IO with the iocb armed our waitqueue.
3168 * This gets called when the page is unlocked, and we generally expect that to
3169 * happen when the page IO is completed and the page is now uptodate. This will
3170 * queue a task_work based retry of the operation, attempting to copy the data
3171 * again. If the latter fails because the page was NOT uptodate, then we will
3172 * do a thread based blocking retry of the operation. That's the unexpected
3173 * slow path.
3174 */
bcf5a063
JA
3175static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3176 int sync, void *arg)
3177{
3178 struct wait_page_queue *wpq;
3179 struct io_kiocb *req = wait->private;
bcf5a063 3180 struct wait_page_key *key = arg;
bcf5a063
JA
3181 int ret;
3182
3183 wpq = container_of(wait, struct wait_page_queue, wait);
3184
cdc8fcb4
LT
3185 if (!wake_page_match(wpq, key))
3186 return 0;
3187
c8d317aa 3188 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
bcf5a063
JA
3189 list_del_init(&wait->entry);
3190
e7375122 3191 init_task_work(&req->task_work, io_req_task_submit);
6d816e08
JA
3192 percpu_ref_get(&req->ctx->refs);
3193
bcf5a063
JA
3194 /* submit ref gets dropped, acquire a new one */
3195 refcount_inc(&req->refs);
fd7d6de2 3196 ret = io_req_task_work_add(req, &req->task_work, true);
bcf5a063 3197 if (unlikely(ret)) {
c2c4c83c
JA
3198 struct task_struct *tsk;
3199
bcf5a063 3200 /* queue just for cancelation */
e7375122 3201 init_task_work(&req->task_work, io_req_task_cancel);
bcf5a063 3202 tsk = io_wq_get_task(req->ctx->io_wq);
e7375122 3203 task_work_add(tsk, &req->task_work, 0);
c2c4c83c 3204 wake_up_process(tsk);
bcf5a063 3205 }
bcf5a063
JA
3206 return 1;
3207}
3208
c1dd91d1
JA
3209/*
3210 * This controls whether a given IO request should be armed for async page
3211 * based retry. If we return false here, the request is handed to the async
3212 * worker threads for retry. If we're doing buffered reads on a regular file,
3213 * we prepare a private wait_page_queue entry and retry the operation. This
3214 * will either succeed because the page is now uptodate and unlocked, or it
3215 * will register a callback when the page is unlocked at IO completion. Through
3216 * that callback, io_uring uses task_work to setup a retry of the operation.
3217 * That retry will attempt the buffered read again. The retry will generally
3218 * succeed, or in rare cases where it fails, we then fall back to using the
3219 * async worker threads for a blocking retry.
3220 */
227c0c96 3221static bool io_rw_should_retry(struct io_kiocb *req)
f67676d1 3222{
e8c2bc1f
JA
3223 struct io_async_rw *rw = req->async_data;
3224 struct wait_page_queue *wait = &rw->wpq;
bcf5a063 3225 struct kiocb *kiocb = &req->rw.kiocb;
f67676d1 3226
bcf5a063
JA
3227 /* never retry for NOWAIT, we just complete with -EAGAIN */
3228 if (req->flags & REQ_F_NOWAIT)
3229 return false;
f67676d1 3230
227c0c96 3231 /* Only for buffered IO */
3b2a4439 3232 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
bcf5a063 3233 return false;
3b2a4439 3234
bcf5a063
JA
3235 /*
3236 * just use poll if we can, and don't attempt if the fs doesn't
3237 * support callback based unlocks
3238 */
3239 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3240 return false;
f67676d1 3241
3b2a4439
JA
3242 wait->wait.func = io_async_buf_func;
3243 wait->wait.private = req;
3244 wait->wait.flags = 0;
3245 INIT_LIST_HEAD(&wait->wait.entry);
3246 kiocb->ki_flags |= IOCB_WAITQ;
c8d317aa 3247 kiocb->ki_flags &= ~IOCB_NOWAIT;
3b2a4439 3248 kiocb->ki_waitq = wait;
3b2a4439 3249 return true;
bcf5a063
JA
3250}
3251
3252static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3253{
3254 if (req->file->f_op->read_iter)
3255 return call_read_iter(req->file, &req->rw.kiocb, iter);
2dd2111d
GH
3256 else if (req->file->f_op->read)
3257 return loop_rw_iter(READ, req->file, &req->rw.kiocb, iter);
3258 else
3259 return -EINVAL;
f67676d1
JA
3260}
3261
a1d7c393
JA
3262static int io_read(struct io_kiocb *req, bool force_nonblock,
3263 struct io_comp_state *cs)
2b188cc1
JA
3264{
3265 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
9adbd45d 3266 struct kiocb *kiocb = &req->rw.kiocb;
ff6165b2 3267 struct iov_iter __iter, *iter = &__iter;
e8c2bc1f 3268 struct io_async_rw *rw = req->async_data;
227c0c96 3269 ssize_t io_size, ret, ret2;
31b51510 3270 size_t iov_count;
f5cac8b1 3271 bool no_async;
ff6165b2 3272
e8c2bc1f
JA
3273 if (rw)
3274 iter = &rw->iter;
2b188cc1 3275
ff6165b2 3276 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
06b76d44
JA
3277 if (ret < 0)
3278 return ret;
eefdf30f 3279 iov_count = iov_iter_count(iter);
fa15bafb
PB
3280 io_size = ret;
3281 req->result = io_size;
227c0c96 3282 ret = 0;
2b188cc1 3283
fd6c2e4c
JA
3284 /* Ensure we clear previously set non-block flag */
3285 if (!force_nonblock)
29de5f6a 3286 kiocb->ki_flags &= ~IOCB_NOWAIT;
fd6c2e4c 3287
24c74678 3288 /* If the file doesn't support async, just async punt */
f5cac8b1
JA
3289 no_async = force_nonblock && !io_file_supports_async(req->file, READ);
3290 if (no_async)
f67676d1 3291 goto copy_iov;
9e645e11 3292
0fef9483 3293 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), iov_count);
fa15bafb
PB
3294 if (unlikely(ret))
3295 goto out_free;
2b188cc1 3296
227c0c96 3297 ret = io_iter_do_read(req, iter);
32960613 3298
227c0c96
JA
3299 if (!ret) {
3300 goto done;
3301 } else if (ret == -EIOCBQUEUED) {
3302 ret = 0;
3303 goto out_free;
3304 } else if (ret == -EAGAIN) {
eefdf30f
JA
3305 /* IOPOLL retry should happen for io-wq threads */
3306 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
f91daf56 3307 goto done;
355afaeb
JA
3308 /* no retry on NONBLOCK marked file */
3309 if (req->file->f_flags & O_NONBLOCK)
3310 goto done;
84216315
JA
3311 /* some cases will consume bytes even on error returns */
3312 iov_iter_revert(iter, iov_count - iov_iter_count(iter));
f38c7e3a
JA
3313 ret = 0;
3314 goto copy_iov;
227c0c96 3315 } else if (ret < 0) {
00d23d51
JA
3316 /* make sure -ERESTARTSYS -> -EINTR is done */
3317 goto done;
227c0c96
JA
3318 }
3319
3320 /* read it all, or we did blocking attempt. no retry. */
f91daf56
JA
3321 if (!iov_iter_count(iter) || !force_nonblock ||
3322 (req->file->f_flags & O_NONBLOCK))
227c0c96
JA
3323 goto done;
3324
3325 io_size -= ret;
3326copy_iov:
3327 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
3328 if (ret2) {
3329 ret = ret2;
3330 goto out_free;
3331 }
f5cac8b1
JA
3332 if (no_async)
3333 return -EAGAIN;
e8c2bc1f 3334 rw = req->async_data;
227c0c96
JA
3335 /* it's copied and will be cleaned with ->io */
3336 iovec = NULL;
3337 /* now use our persistent iterator, if we aren't already */
e8c2bc1f 3338 iter = &rw->iter;
227c0c96 3339retry:
e8c2bc1f 3340 rw->bytes_done += ret;
227c0c96
JA
3341 /* if we can retry, do so with the callbacks armed */
3342 if (!io_rw_should_retry(req)) {
fa15bafb
PB
3343 kiocb->ki_flags &= ~IOCB_WAITQ;
3344 return -EAGAIN;
2b188cc1 3345 }
227c0c96
JA
3346
3347 /*
3348 * Now retry read with the IOCB_WAITQ parts set in the iocb. If we
3349 * get -EIOCBQUEUED, then we'll get a notification when the desired
3350 * page gets unlocked. We can also get a partial read here, and if we
3351 * do, then just retry at the new offset.
3352 */
3353 ret = io_iter_do_read(req, iter);
3354 if (ret == -EIOCBQUEUED) {
3355 ret = 0;
3356 goto out_free;
3357 } else if (ret > 0 && ret < io_size) {
3358 /* we got some bytes, but not all. retry. */
3359 goto retry;
3360 }
3361done:
3362 kiocb_done(kiocb, ret, cs);
3363 ret = 0;
f67676d1 3364out_free:
f261c168 3365 /* it's reportedly faster than delegating the null check to kfree() */
252917c3 3366 if (iovec)
6f2cc166 3367 kfree(iovec);
2b188cc1
JA
3368 return ret;
3369}
3370
3529d8c2
JA
3371static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3372 bool force_nonblock)
f67676d1
JA
3373{
3374 ssize_t ret;
3375
3529d8c2
JA
3376 ret = io_prep_rw(req, sqe, force_nonblock);
3377 if (ret)
3378 return ret;
f67676d1 3379
3529d8c2
JA
3380 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3381 return -EBADF;
f67676d1 3382
5f798bea 3383 /* either don't need iovec imported or already have it */
e8c2bc1f 3384 if (!req->async_data || req->flags & REQ_F_NEED_CLEANUP)
3529d8c2 3385 return 0;
c3e330a4 3386 return io_rw_prep_async(req, WRITE, force_nonblock);
f67676d1
JA
3387}
3388
a1d7c393
JA
3389static int io_write(struct io_kiocb *req, bool force_nonblock,
3390 struct io_comp_state *cs)
2b188cc1
JA
3391{
3392 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
9adbd45d 3393 struct kiocb *kiocb = &req->rw.kiocb;
ff6165b2 3394 struct iov_iter __iter, *iter = &__iter;
e8c2bc1f 3395 struct io_async_rw *rw = req->async_data;
31b51510 3396 size_t iov_count;
fa15bafb 3397 ssize_t ret, ret2, io_size;
2b188cc1 3398
e8c2bc1f
JA
3399 if (rw)
3400 iter = &rw->iter;
ff6165b2
JA
3401
3402 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
06b76d44
JA
3403 if (ret < 0)
3404 return ret;
eefdf30f 3405 iov_count = iov_iter_count(iter);
fa15bafb
PB
3406 io_size = ret;
3407 req->result = io_size;
2b188cc1 3408
fd6c2e4c
JA
3409 /* Ensure we clear previously set non-block flag */
3410 if (!force_nonblock)
9adbd45d 3411 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
fd6c2e4c 3412
24c74678 3413 /* If the file doesn't support async, just async punt */
af197f50 3414 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
f67676d1 3415 goto copy_iov;
31b51510 3416
10d59345
JA
3417 /* file path doesn't support NOWAIT for non-direct_IO */
3418 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3419 (req->flags & REQ_F_ISREG))
f67676d1 3420 goto copy_iov;
31b51510 3421
0fef9483 3422 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), iov_count);
fa15bafb
PB
3423 if (unlikely(ret))
3424 goto out_free;
4ed734b0 3425
fa15bafb
PB
3426 /*
3427 * Open-code file_start_write here to grab freeze protection,
3428 * which will be released by another thread in
3429 * io_complete_rw(). Fool lockdep by telling it the lock got
3430 * released so that it doesn't complain about the held lock when
3431 * we return to userspace.
3432 */
3433 if (req->flags & REQ_F_ISREG) {
3434 __sb_start_write(file_inode(req->file)->i_sb,
3435 SB_FREEZE_WRITE, true);
3436 __sb_writers_release(file_inode(req->file)->i_sb,
3437 SB_FREEZE_WRITE);
3438 }
3439 kiocb->ki_flags |= IOCB_WRITE;
4ed734b0 3440
fa15bafb 3441 if (req->file->f_op->write_iter)
ff6165b2 3442 ret2 = call_write_iter(req->file, kiocb, iter);
2dd2111d 3443 else if (req->file->f_op->write)
ff6165b2 3444 ret2 = loop_rw_iter(WRITE, req->file, kiocb, iter);
2dd2111d
GH
3445 else
3446 ret2 = -EINVAL;
4ed734b0 3447
fa15bafb
PB
3448 /*
3449 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3450 * retry them without IOCB_NOWAIT.
3451 */
3452 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3453 ret2 = -EAGAIN;
355afaeb
JA
3454 /* no retry on NONBLOCK marked file */
3455 if (ret2 == -EAGAIN && (req->file->f_flags & O_NONBLOCK))
3456 goto done;
fa15bafb 3457 if (!force_nonblock || ret2 != -EAGAIN) {
eefdf30f
JA
3458 /* IOPOLL retry should happen for io-wq threads */
3459 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3460 goto copy_iov;
355afaeb 3461done:
fa15bafb
PB
3462 kiocb_done(kiocb, ret2, cs);
3463 } else {
f67676d1 3464copy_iov:
84216315
JA
3465 /* some cases will consume bytes even on error returns */
3466 iov_iter_revert(iter, iov_count - iov_iter_count(iter));
227c0c96 3467 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
ff6165b2
JA
3468 if (!ret)
3469 return -EAGAIN;
2b188cc1 3470 }
31b51510 3471out_free:
f261c168 3472 /* it's reportedly faster than delegating the null check to kfree() */
252917c3 3473 if (iovec)
6f2cc166 3474 kfree(iovec);
2b188cc1
JA
3475 return ret;
3476}
3477
f2a8d5c7
PB
3478static int __io_splice_prep(struct io_kiocb *req,
3479 const struct io_uring_sqe *sqe)
7d67af2c
PB
3480{
3481 struct io_splice* sp = &req->splice;
3482 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
3483 int ret;
3484
3485 if (req->flags & REQ_F_NEED_CLEANUP)
3486 return 0;
3232dd02
PB
3487 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3488 return -EINVAL;
7d67af2c
PB
3489
3490 sp->file_in = NULL;
7d67af2c
PB
3491 sp->len = READ_ONCE(sqe->len);
3492 sp->flags = READ_ONCE(sqe->splice_flags);
3493
3494 if (unlikely(sp->flags & ~valid_flags))
3495 return -EINVAL;
3496
3497 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
3498 (sp->flags & SPLICE_F_FD_IN_FIXED));
3499 if (ret)
3500 return ret;
3501 req->flags |= REQ_F_NEED_CLEANUP;
3502
7cdaf587
XW
3503 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3504 /*
3505 * Splice operation will be punted aync, and here need to
3506 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3507 */
3508 io_req_init_async(req);
7d67af2c 3509 req->work.flags |= IO_WQ_WORK_UNBOUND;
7cdaf587 3510 }
7d67af2c
PB
3511
3512 return 0;
3513}
3514
f2a8d5c7
PB
3515static int io_tee_prep(struct io_kiocb *req,
3516 const struct io_uring_sqe *sqe)
3517{
3518 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3519 return -EINVAL;
3520 return __io_splice_prep(req, sqe);
3521}
3522
3523static int io_tee(struct io_kiocb *req, bool force_nonblock)
3524{
3525 struct io_splice *sp = &req->splice;
3526 struct file *in = sp->file_in;
3527 struct file *out = sp->file_out;
3528 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3529 long ret = 0;
3530
3531 if (force_nonblock)
3532 return -EAGAIN;
3533 if (sp->len)
3534 ret = do_tee(in, out, sp->len, flags);
3535
3536 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3537 req->flags &= ~REQ_F_NEED_CLEANUP;
3538
f2a8d5c7
PB
3539 if (ret != sp->len)
3540 req_set_fail_links(req);
e1e16097 3541 io_req_complete(req, ret);
f2a8d5c7
PB
3542 return 0;
3543}
3544
3545static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3546{
3547 struct io_splice* sp = &req->splice;
3548
3549 sp->off_in = READ_ONCE(sqe->splice_off_in);
3550 sp->off_out = READ_ONCE(sqe->off);
3551 return __io_splice_prep(req, sqe);
3552}
3553
014db007 3554static int io_splice(struct io_kiocb *req, bool force_nonblock)
7d67af2c
PB
3555{
3556 struct io_splice *sp = &req->splice;
3557 struct file *in = sp->file_in;
3558 struct file *out = sp->file_out;
3559 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3560 loff_t *poff_in, *poff_out;
c9687426 3561 long ret = 0;
7d67af2c 3562
2fb3e822
PB
3563 if (force_nonblock)
3564 return -EAGAIN;
7d67af2c
PB
3565
3566 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3567 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
c9687426 3568
948a7749 3569 if (sp->len)
c9687426 3570 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
7d67af2c
PB
3571
3572 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3573 req->flags &= ~REQ_F_NEED_CLEANUP;
3574
7d67af2c
PB
3575 if (ret != sp->len)
3576 req_set_fail_links(req);
e1e16097 3577 io_req_complete(req, ret);
7d67af2c
PB
3578 return 0;
3579}
3580
2b188cc1
JA
3581/*
3582 * IORING_OP_NOP just posts a completion event, nothing else.
3583 */
229a7b63 3584static int io_nop(struct io_kiocb *req, struct io_comp_state *cs)
2b188cc1
JA
3585{
3586 struct io_ring_ctx *ctx = req->ctx;
2b188cc1 3587
def596e9
JA
3588 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3589 return -EINVAL;
3590
229a7b63 3591 __io_req_complete(req, 0, 0, cs);
2b188cc1
JA
3592 return 0;
3593}
3594
3529d8c2 3595static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
c992fe29 3596{
6b06314c 3597 struct io_ring_ctx *ctx = req->ctx;
c992fe29 3598
09bb8394
JA
3599 if (!req->file)
3600 return -EBADF;
c992fe29 3601
6b06314c 3602 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
def596e9 3603 return -EINVAL;
edafccee 3604 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
c992fe29
CH
3605 return -EINVAL;
3606
8ed8d3c3
JA
3607 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3608 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3609 return -EINVAL;
3610
3611 req->sync.off = READ_ONCE(sqe->off);
3612 req->sync.len = READ_ONCE(sqe->len);
c992fe29
CH
3613 return 0;
3614}
3615
ac45abc0 3616static int io_fsync(struct io_kiocb *req, bool force_nonblock)
8ed8d3c3 3617{
8ed8d3c3 3618 loff_t end = req->sync.off + req->sync.len;
8ed8d3c3
JA
3619 int ret;
3620
ac45abc0
PB
3621 /* fsync always requires a blocking context */
3622 if (force_nonblock)
3623 return -EAGAIN;
3624
9adbd45d 3625 ret = vfs_fsync_range(req->file, req->sync.off,
8ed8d3c3
JA
3626 end > 0 ? end : LLONG_MAX,
3627 req->sync.flags & IORING_FSYNC_DATASYNC);
3628 if (ret < 0)
3629 req_set_fail_links(req);
e1e16097 3630 io_req_complete(req, ret);
c992fe29
CH
3631 return 0;
3632}
3633
d63d1b5e
JA
3634static int io_fallocate_prep(struct io_kiocb *req,
3635 const struct io_uring_sqe *sqe)
3636{
3637 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3638 return -EINVAL;
3232dd02
PB
3639 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3640 return -EINVAL;
d63d1b5e
JA
3641
3642 req->sync.off = READ_ONCE(sqe->off);
3643 req->sync.len = READ_ONCE(sqe->addr);
3644 req->sync.mode = READ_ONCE(sqe->len);
3645 return 0;
3646}
3647
014db007 3648static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
5d17b4a4 3649{
ac45abc0
PB
3650 int ret;
3651
d63d1b5e 3652 /* fallocate always requiring blocking context */
ac45abc0 3653 if (force_nonblock)
5d17b4a4 3654 return -EAGAIN;
ac45abc0
PB
3655 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3656 req->sync.len);
ac45abc0
PB
3657 if (ret < 0)
3658 req_set_fail_links(req);
e1e16097 3659 io_req_complete(req, ret);
5d17b4a4
JA
3660 return 0;
3661}
3662
ec65fea5 3663static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
b7bb4f7d 3664{
f8748881 3665 const char __user *fname;
15b71abe 3666 int ret;
b7bb4f7d 3667
ec65fea5 3668 if (unlikely(sqe->ioprio || sqe->buf_index))
15b71abe 3669 return -EINVAL;
ec65fea5 3670 if (unlikely(req->flags & REQ_F_FIXED_FILE))
cf3040ca 3671 return -EBADF;
03b1230c 3672
ec65fea5
PB
3673 /* open.how should be already initialised */
3674 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
08a1d26e 3675 req->open.how.flags |= O_LARGEFILE;
3529d8c2 3676
25e72d10
PB
3677 req->open.dfd = READ_ONCE(sqe->fd);
3678 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
f8748881 3679 req->open.filename = getname(fname);
15b71abe
JA
3680 if (IS_ERR(req->open.filename)) {
3681 ret = PTR_ERR(req->open.filename);
3682 req->open.filename = NULL;
3683 return ret;
3684 }
4022e7af 3685 req->open.nofile = rlimit(RLIMIT_NOFILE);
8fef80bf 3686 req->flags |= REQ_F_NEED_CLEANUP;
15b71abe 3687 return 0;
03b1230c
JA
3688}
3689
ec65fea5
PB
3690static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3691{
3692 u64 flags, mode;
3693
4eb8dded
JA
3694 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3695 return -EINVAL;
ec65fea5
PB
3696 if (req->flags & REQ_F_NEED_CLEANUP)
3697 return 0;
3698 mode = READ_ONCE(sqe->len);
3699 flags = READ_ONCE(sqe->open_flags);
3700 req->open.how = build_open_how(flags, mode);
3701 return __io_openat_prep(req, sqe);
3702}
3703
cebdb986 3704static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
aa1fa28f 3705{
cebdb986 3706 struct open_how __user *how;
cebdb986 3707 size_t len;
0fa03c62
JA
3708 int ret;
3709
4eb8dded
JA
3710 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3711 return -EINVAL;
0bdbdd08
PB
3712 if (req->flags & REQ_F_NEED_CLEANUP)
3713 return 0;
cebdb986
JA
3714 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3715 len = READ_ONCE(sqe->len);
cebdb986
JA
3716 if (len < OPEN_HOW_SIZE_VER0)
3717 return -EINVAL;
3529d8c2 3718
cebdb986
JA
3719 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3720 len);
3721 if (ret)
3722 return ret;
3529d8c2 3723
ec65fea5 3724 return __io_openat_prep(req, sqe);
cebdb986
JA
3725}
3726
014db007 3727static int io_openat2(struct io_kiocb *req, bool force_nonblock)
15b71abe
JA
3728{
3729 struct open_flags op;
15b71abe
JA
3730 struct file *file;
3731 int ret;
3732
f86cd20c 3733 if (force_nonblock)
15b71abe 3734 return -EAGAIN;
15b71abe 3735
cebdb986 3736 ret = build_open_flags(&req->open.how, &op);
15b71abe
JA
3737 if (ret)
3738 goto err;
3739
4022e7af 3740 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
15b71abe
JA
3741 if (ret < 0)
3742 goto err;
3743
3744 file = do_filp_open(req->open.dfd, req->open.filename, &op);
3745 if (IS_ERR(file)) {
3746 put_unused_fd(ret);
3747 ret = PTR_ERR(file);
3748 } else {
3749 fsnotify_open(file);
3750 fd_install(ret, file);
3751 }
3752err:
3753 putname(req->open.filename);
8fef80bf 3754 req->flags &= ~REQ_F_NEED_CLEANUP;
15b71abe
JA
3755 if (ret < 0)
3756 req_set_fail_links(req);
e1e16097 3757 io_req_complete(req, ret);
15b71abe
JA
3758 return 0;
3759}
3760
014db007 3761static int io_openat(struct io_kiocb *req, bool force_nonblock)
cebdb986 3762{
014db007 3763 return io_openat2(req, force_nonblock);
cebdb986
JA
3764}
3765
067524e9
JA
3766static int io_remove_buffers_prep(struct io_kiocb *req,
3767 const struct io_uring_sqe *sqe)
3768{
3769 struct io_provide_buf *p = &req->pbuf;
3770 u64 tmp;
3771
3772 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3773 return -EINVAL;
3774
3775 tmp = READ_ONCE(sqe->fd);
3776 if (!tmp || tmp > USHRT_MAX)
3777 return -EINVAL;
3778
3779 memset(p, 0, sizeof(*p));
3780 p->nbufs = tmp;
3781 p->bgid = READ_ONCE(sqe->buf_group);
3782 return 0;
3783}
3784
3785static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3786 int bgid, unsigned nbufs)
3787{
3788 unsigned i = 0;
3789
3790 /* shouldn't happen */
3791 if (!nbufs)
3792 return 0;
3793
3794 /* the head kbuf is the list itself */
3795 while (!list_empty(&buf->list)) {
3796 struct io_buffer *nxt;
3797
3798 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3799 list_del(&nxt->list);
3800 kfree(nxt);
3801 if (++i == nbufs)
3802 return i;
3803 }
3804 i++;
3805 kfree(buf);
3806 idr_remove(&ctx->io_buffer_idr, bgid);
3807
3808 return i;
3809}
3810
229a7b63
JA
3811static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock,
3812 struct io_comp_state *cs)
067524e9
JA
3813{
3814 struct io_provide_buf *p = &req->pbuf;
3815 struct io_ring_ctx *ctx = req->ctx;
3816 struct io_buffer *head;
3817 int ret = 0;
3818
3819 io_ring_submit_lock(ctx, !force_nonblock);
3820
3821 lockdep_assert_held(&ctx->uring_lock);
3822
3823 ret = -ENOENT;
3824 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3825 if (head)
3826 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
3827
3828 io_ring_submit_lock(ctx, !force_nonblock);
3829 if (ret < 0)
3830 req_set_fail_links(req);
229a7b63 3831 __io_req_complete(req, ret, 0, cs);
067524e9
JA
3832 return 0;
3833}
3834
ddf0322d
JA
3835static int io_provide_buffers_prep(struct io_kiocb *req,
3836 const struct io_uring_sqe *sqe)
3837{
3838 struct io_provide_buf *p = &req->pbuf;
3839 u64 tmp;
3840
3841 if (sqe->ioprio || sqe->rw_flags)
3842 return -EINVAL;
3843
3844 tmp = READ_ONCE(sqe->fd);
3845 if (!tmp || tmp > USHRT_MAX)
3846 return -E2BIG;
3847 p->nbufs = tmp;
3848 p->addr = READ_ONCE(sqe->addr);
3849 p->len = READ_ONCE(sqe->len);
3850
efe68c1c 3851 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
ddf0322d
JA
3852 return -EFAULT;
3853
3854 p->bgid = READ_ONCE(sqe->buf_group);
3855 tmp = READ_ONCE(sqe->off);
3856 if (tmp > USHRT_MAX)
3857 return -E2BIG;
3858 p->bid = tmp;
3859 return 0;
3860}
3861
3862static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3863{
3864 struct io_buffer *buf;
3865 u64 addr = pbuf->addr;
3866 int i, bid = pbuf->bid;
3867
3868 for (i = 0; i < pbuf->nbufs; i++) {
3869 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3870 if (!buf)
3871 break;
3872
3873 buf->addr = addr;
3874 buf->len = pbuf->len;
3875 buf->bid = bid;
3876 addr += pbuf->len;
3877 bid++;
3878 if (!*head) {
3879 INIT_LIST_HEAD(&buf->list);
3880 *head = buf;
3881 } else {
3882 list_add_tail(&buf->list, &(*head)->list);
3883 }
3884 }
3885
3886 return i ? i : -ENOMEM;
3887}
3888
229a7b63
JA
3889static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock,
3890 struct io_comp_state *cs)
ddf0322d
JA
3891{
3892 struct io_provide_buf *p = &req->pbuf;
3893 struct io_ring_ctx *ctx = req->ctx;
3894 struct io_buffer *head, *list;
3895 int ret = 0;
3896
3897 io_ring_submit_lock(ctx, !force_nonblock);
3898
3899 lockdep_assert_held(&ctx->uring_lock);
3900
3901 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
3902
3903 ret = io_add_buffers(p, &head);
3904 if (ret < 0)
3905 goto out;
3906
3907 if (!list) {
3908 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
3909 GFP_KERNEL);
3910 if (ret < 0) {
067524e9 3911 __io_remove_buffers(ctx, head, p->bgid, -1U);
ddf0322d
JA
3912 goto out;
3913 }
3914 }
3915out:
3916 io_ring_submit_unlock(ctx, !force_nonblock);
3917 if (ret < 0)
3918 req_set_fail_links(req);
229a7b63 3919 __io_req_complete(req, ret, 0, cs);
ddf0322d 3920 return 0;
cebdb986
JA
3921}
3922
3e4827b0
JA
3923static int io_epoll_ctl_prep(struct io_kiocb *req,
3924 const struct io_uring_sqe *sqe)
3925{
3926#if defined(CONFIG_EPOLL)
3927 if (sqe->ioprio || sqe->buf_index)
3928 return -EINVAL;
6ca56f84 3929 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
3232dd02 3930 return -EINVAL;
3e4827b0
JA
3931
3932 req->epoll.epfd = READ_ONCE(sqe->fd);
3933 req->epoll.op = READ_ONCE(sqe->len);
3934 req->epoll.fd = READ_ONCE(sqe->off);
3935
3936 if (ep_op_has_event(req->epoll.op)) {
3937 struct epoll_event __user *ev;
3938
3939 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
3940 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
3941 return -EFAULT;
3942 }
3943
3944 return 0;
3945#else
3946 return -EOPNOTSUPP;
3947#endif
3948}
3949
229a7b63
JA
3950static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock,
3951 struct io_comp_state *cs)
3e4827b0
JA
3952{
3953#if defined(CONFIG_EPOLL)
3954 struct io_epoll *ie = &req->epoll;
3955 int ret;
3956
3957 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
3958 if (force_nonblock && ret == -EAGAIN)
3959 return -EAGAIN;
3960
3961 if (ret < 0)
3962 req_set_fail_links(req);
229a7b63 3963 __io_req_complete(req, ret, 0, cs);
3e4827b0
JA
3964 return 0;
3965#else
3966 return -EOPNOTSUPP;
3967#endif
3968}
3969
c1ca757b
JA
3970static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3971{
3972#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3973 if (sqe->ioprio || sqe->buf_index || sqe->off)
3974 return -EINVAL;
3232dd02
PB
3975 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3976 return -EINVAL;
c1ca757b
JA
3977
3978 req->madvise.addr = READ_ONCE(sqe->addr);
3979 req->madvise.len = READ_ONCE(sqe->len);
3980 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
3981 return 0;
3982#else
3983 return -EOPNOTSUPP;
3984#endif
3985}
3986
014db007 3987static int io_madvise(struct io_kiocb *req, bool force_nonblock)
c1ca757b
JA
3988{
3989#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3990 struct io_madvise *ma = &req->madvise;
3991 int ret;
3992
3993 if (force_nonblock)
3994 return -EAGAIN;
3995
3996 ret = do_madvise(ma->addr, ma->len, ma->advice);
3997 if (ret < 0)
3998 req_set_fail_links(req);
e1e16097 3999 io_req_complete(req, ret);
c1ca757b
JA
4000 return 0;
4001#else
4002 return -EOPNOTSUPP;
4003#endif
4004}
4005
4840e418
JA
4006static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4007{
4008 if (sqe->ioprio || sqe->buf_index || sqe->addr)
4009 return -EINVAL;
3232dd02
PB
4010 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4011 return -EINVAL;
4840e418
JA
4012
4013 req->fadvise.offset = READ_ONCE(sqe->off);
4014 req->fadvise.len = READ_ONCE(sqe->len);
4015 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4016 return 0;
4017}
4018
014db007 4019static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
4840e418
JA
4020{
4021 struct io_fadvise *fa = &req->fadvise;
4022 int ret;
4023
3e69426d
JA
4024 if (force_nonblock) {
4025 switch (fa->advice) {
4026 case POSIX_FADV_NORMAL:
4027 case POSIX_FADV_RANDOM:
4028 case POSIX_FADV_SEQUENTIAL:
4029 break;
4030 default:
4031 return -EAGAIN;
4032 }
4033 }
4840e418
JA
4034
4035 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4036 if (ret < 0)
4037 req_set_fail_links(req);
e1e16097 4038 io_req_complete(req, ret);
4840e418
JA
4039 return 0;
4040}
4041
eddc7ef5
JA
4042static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4043{
6ca56f84 4044 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
3232dd02 4045 return -EINVAL;
eddc7ef5
JA
4046 if (sqe->ioprio || sqe->buf_index)
4047 return -EINVAL;
9c280f90 4048 if (req->flags & REQ_F_FIXED_FILE)
cf3040ca 4049 return -EBADF;
eddc7ef5 4050
1d9e1288
BM
4051 req->statx.dfd = READ_ONCE(sqe->fd);
4052 req->statx.mask = READ_ONCE(sqe->len);
e62753e4 4053 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
1d9e1288
BM
4054 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4055 req->statx.flags = READ_ONCE(sqe->statx_flags);
eddc7ef5
JA
4056
4057 return 0;
4058}
4059
014db007 4060static int io_statx(struct io_kiocb *req, bool force_nonblock)
eddc7ef5 4061{
1d9e1288 4062 struct io_statx *ctx = &req->statx;
eddc7ef5
JA
4063 int ret;
4064
5b0bbee4
JA
4065 if (force_nonblock) {
4066 /* only need file table for an actual valid fd */
4067 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
4068 req->flags |= REQ_F_NO_FILE_TABLE;
eddc7ef5 4069 return -EAGAIN;
5b0bbee4 4070 }
eddc7ef5 4071
e62753e4
BM
4072 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4073 ctx->buffer);
eddc7ef5 4074
eddc7ef5
JA
4075 if (ret < 0)
4076 req_set_fail_links(req);
e1e16097 4077 io_req_complete(req, ret);
eddc7ef5
JA
4078 return 0;
4079}
4080
b5dba59e
JA
4081static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4082{
4083 /*
4084 * If we queue this for async, it must not be cancellable. That would
7cdaf587
XW
4085 * leave the 'file' in an undeterminate state, and here need to modify
4086 * io_wq_work.flags, so initialize io_wq_work firstly.
b5dba59e 4087 */
7cdaf587 4088 io_req_init_async(req);
b5dba59e
JA
4089 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
4090
3232dd02
PB
4091 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4092 return -EINVAL;
b5dba59e
JA
4093 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
4094 sqe->rw_flags || sqe->buf_index)
4095 return -EINVAL;
9c280f90 4096 if (req->flags & REQ_F_FIXED_FILE)
cf3040ca 4097 return -EBADF;
b5dba59e
JA
4098
4099 req->close.fd = READ_ONCE(sqe->fd);
0f212204 4100 if ((req->file && req->file->f_op == &io_uring_fops))
fd2206e4 4101 return -EBADF;
b5dba59e 4102
3af73b28 4103 req->close.put_file = NULL;
b5dba59e 4104 return 0;
b5dba59e
JA
4105}
4106
229a7b63
JA
4107static int io_close(struct io_kiocb *req, bool force_nonblock,
4108 struct io_comp_state *cs)
b5dba59e 4109{
3af73b28 4110 struct io_close *close = &req->close;
b5dba59e
JA
4111 int ret;
4112
3af73b28
PB
4113 /* might be already done during nonblock submission */
4114 if (!close->put_file) {
4115 ret = __close_fd_get_file(close->fd, &close->put_file);
4116 if (ret < 0)
4117 return (ret == -ENOENT) ? -EBADF : ret;
4118 }
b5dba59e
JA
4119
4120 /* if the file has a flush method, be safe and punt to async */
3af73b28 4121 if (close->put_file->f_op->flush && force_nonblock) {
24c74678
PB
4122 /* was never set, but play safe */
4123 req->flags &= ~REQ_F_NOWAIT;
0bf0eefd 4124 /* avoid grabbing files - we don't need the files */
24c74678 4125 req->flags |= REQ_F_NO_FILE_TABLE;
0bf0eefd 4126 return -EAGAIN;
a2100672 4127 }
b5dba59e 4128
3af73b28
PB
4129 /* No ->flush() or already async, safely close from here */
4130 ret = filp_close(close->put_file, req->work.files);
4131 if (ret < 0)
4132 req_set_fail_links(req);
3af73b28
PB
4133 fput(close->put_file);
4134 close->put_file = NULL;
229a7b63 4135 __io_req_complete(req, ret, 0, cs);
1a417f4e 4136 return 0;
b5dba59e
JA
4137}
4138
3529d8c2 4139static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5d17b4a4
JA
4140{
4141 struct io_ring_ctx *ctx = req->ctx;
5d17b4a4
JA
4142
4143 if (!req->file)
4144 return -EBADF;
5d17b4a4
JA
4145
4146 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4147 return -EINVAL;
4148 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
4149 return -EINVAL;
4150
8ed8d3c3
JA
4151 req->sync.off = READ_ONCE(sqe->off);
4152 req->sync.len = READ_ONCE(sqe->len);
4153 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
8ed8d3c3
JA
4154 return 0;
4155}
4156
ac45abc0 4157static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
8ed8d3c3 4158{
8ed8d3c3
JA
4159 int ret;
4160
ac45abc0
PB
4161 /* sync_file_range always requires a blocking context */
4162 if (force_nonblock)
4163 return -EAGAIN;
4164
9adbd45d 4165 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
8ed8d3c3
JA
4166 req->sync.flags);
4167 if (ret < 0)
4168 req_set_fail_links(req);
e1e16097 4169 io_req_complete(req, ret);
5d17b4a4
JA
4170 return 0;
4171}
4172
469956e8 4173#if defined(CONFIG_NET)
02d27d89
PB
4174static int io_setup_async_msg(struct io_kiocb *req,
4175 struct io_async_msghdr *kmsg)
4176{
e8c2bc1f
JA
4177 struct io_async_msghdr *async_msg = req->async_data;
4178
4179 if (async_msg)
02d27d89 4180 return -EAGAIN;
e8c2bc1f 4181 if (io_alloc_async_data(req)) {
02d27d89
PB
4182 if (kmsg->iov != kmsg->fast_iov)
4183 kfree(kmsg->iov);
4184 return -ENOMEM;
4185 }
e8c2bc1f 4186 async_msg = req->async_data;
02d27d89 4187 req->flags |= REQ_F_NEED_CLEANUP;
e8c2bc1f 4188 memcpy(async_msg, kmsg, sizeof(*kmsg));
02d27d89
PB
4189 return -EAGAIN;
4190}
4191
2ae523ed
PB
4192static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4193 struct io_async_msghdr *iomsg)
4194{
4195 iomsg->iov = iomsg->fast_iov;
4196 iomsg->msg.msg_name = &iomsg->addr;
4197 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
4198 req->sr_msg.msg_flags, &iomsg->iov);
4199}
4200
3529d8c2 4201static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
03b1230c 4202{
e8c2bc1f 4203 struct io_async_msghdr *async_msg = req->async_data;
e47293fd 4204 struct io_sr_msg *sr = &req->sr_msg;
99bc4c38 4205 int ret;
03b1230c 4206
d2b6f48b
PB
4207 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4208 return -EINVAL;
4209
e47293fd 4210 sr->msg_flags = READ_ONCE(sqe->msg_flags);
270a5940 4211 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
fddaface 4212 sr->len = READ_ONCE(sqe->len);
3529d8c2 4213
d8768362
JA
4214#ifdef CONFIG_COMPAT
4215 if (req->ctx->compat)
4216 sr->msg_flags |= MSG_CMSG_COMPAT;
4217#endif
4218
e8c2bc1f 4219 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
3529d8c2 4220 return 0;
5f798bea
PB
4221 /* iovec is already imported */
4222 if (req->flags & REQ_F_NEED_CLEANUP)
4223 return 0;
3529d8c2 4224
e8c2bc1f 4225 ret = io_sendmsg_copy_hdr(req, async_msg);
99bc4c38
PB
4226 if (!ret)
4227 req->flags |= REQ_F_NEED_CLEANUP;
4228 return ret;
03b1230c
JA
4229}
4230
229a7b63
JA
4231static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4232 struct io_comp_state *cs)
aa1fa28f 4233{
6b754c8b 4234 struct io_async_msghdr iomsg, *kmsg;
0fa03c62 4235 struct socket *sock;
7a7cacba 4236 unsigned flags;
0fa03c62
JA
4237 int ret;
4238
0fa03c62 4239 sock = sock_from_file(req->file, &ret);
7a7cacba
PB
4240 if (unlikely(!sock))
4241 return ret;
3529d8c2 4242
e8c2bc1f
JA
4243 if (req->async_data) {
4244 kmsg = req->async_data;
4245 kmsg->msg.msg_name = &kmsg->addr;
7a7cacba
PB
4246 /* if iov is set, it's allocated already */
4247 if (!kmsg->iov)
4248 kmsg->iov = kmsg->fast_iov;
4249 kmsg->msg.msg_iter.iov = kmsg->iov;
4250 } else {
4251 ret = io_sendmsg_copy_hdr(req, &iomsg);
4252 if (ret)
4253 return ret;
4254 kmsg = &iomsg;
0fa03c62 4255 }
0fa03c62 4256
7a7cacba
PB
4257 flags = req->sr_msg.msg_flags;
4258 if (flags & MSG_DONTWAIT)
4259 req->flags |= REQ_F_NOWAIT;
4260 else if (force_nonblock)
4261 flags |= MSG_DONTWAIT;
e47293fd 4262
7a7cacba
PB
4263 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
4264 if (force_nonblock && ret == -EAGAIN)
4265 return io_setup_async_msg(req, kmsg);
4266 if (ret == -ERESTARTSYS)
4267 ret = -EINTR;
0fa03c62 4268
6b754c8b 4269 if (kmsg->iov != kmsg->fast_iov)
0b416c3e 4270 kfree(kmsg->iov);
99bc4c38 4271 req->flags &= ~REQ_F_NEED_CLEANUP;
4e88d6e7
JA
4272 if (ret < 0)
4273 req_set_fail_links(req);
229a7b63 4274 __io_req_complete(req, ret, 0, cs);
5d17b4a4 4275 return 0;
03b1230c 4276}
aa1fa28f 4277
229a7b63
JA
4278static int io_send(struct io_kiocb *req, bool force_nonblock,
4279 struct io_comp_state *cs)
fddaface 4280{
7a7cacba
PB
4281 struct io_sr_msg *sr = &req->sr_msg;
4282 struct msghdr msg;
4283 struct iovec iov;
fddaface 4284 struct socket *sock;
7a7cacba 4285 unsigned flags;
fddaface
JA
4286 int ret;
4287
fddaface 4288 sock = sock_from_file(req->file, &ret);
7a7cacba
PB
4289 if (unlikely(!sock))
4290 return ret;
fddaface 4291
7a7cacba
PB
4292 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4293 if (unlikely(ret))
14db8411 4294 return ret;
fddaface 4295
7a7cacba
PB
4296 msg.msg_name = NULL;
4297 msg.msg_control = NULL;
4298 msg.msg_controllen = 0;
4299 msg.msg_namelen = 0;
fddaface 4300
7a7cacba
PB
4301 flags = req->sr_msg.msg_flags;
4302 if (flags & MSG_DONTWAIT)
4303 req->flags |= REQ_F_NOWAIT;
4304 else if (force_nonblock)
4305 flags |= MSG_DONTWAIT;
fddaface 4306
7a7cacba
PB
4307 msg.msg_flags = flags;
4308 ret = sock_sendmsg(sock, &msg);
4309 if (force_nonblock && ret == -EAGAIN)
4310 return -EAGAIN;
4311 if (ret == -ERESTARTSYS)
4312 ret = -EINTR;
fddaface 4313
fddaface
JA
4314 if (ret < 0)
4315 req_set_fail_links(req);
229a7b63 4316 __io_req_complete(req, ret, 0, cs);
fddaface 4317 return 0;
fddaface
JA
4318}
4319
1400e697
PB
4320static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4321 struct io_async_msghdr *iomsg)
52de1fe1
JA
4322{
4323 struct io_sr_msg *sr = &req->sr_msg;
4324 struct iovec __user *uiov;
4325 size_t iov_len;
4326 int ret;
4327
1400e697
PB
4328 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4329 &iomsg->uaddr, &uiov, &iov_len);
52de1fe1
JA
4330 if (ret)
4331 return ret;
4332
4333 if (req->flags & REQ_F_BUFFER_SELECT) {
4334 if (iov_len > 1)
4335 return -EINVAL;
1400e697 4336 if (copy_from_user(iomsg->iov, uiov, sizeof(*uiov)))
52de1fe1 4337 return -EFAULT;
1400e697
PB
4338 sr->len = iomsg->iov[0].iov_len;
4339 iov_iter_init(&iomsg->msg.msg_iter, READ, iomsg->iov, 1,
52de1fe1 4340 sr->len);
1400e697 4341 iomsg->iov = NULL;
52de1fe1
JA
4342 } else {
4343 ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
1400e697 4344 &iomsg->iov, &iomsg->msg.msg_iter);
52de1fe1
JA
4345 if (ret > 0)
4346 ret = 0;
4347 }
4348
4349 return ret;
4350}
4351
4352#ifdef CONFIG_COMPAT
4353static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
1400e697 4354 struct io_async_msghdr *iomsg)
52de1fe1
JA
4355{
4356 struct compat_msghdr __user *msg_compat;
4357 struct io_sr_msg *sr = &req->sr_msg;
4358 struct compat_iovec __user *uiov;
4359 compat_uptr_t ptr;
4360 compat_size_t len;
4361 int ret;
4362
270a5940 4363 msg_compat = (struct compat_msghdr __user *) sr->umsg;
1400e697 4364 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
52de1fe1
JA
4365 &ptr, &len);
4366 if (ret)
4367 return ret;
4368
4369 uiov = compat_ptr(ptr);
4370 if (req->flags & REQ_F_BUFFER_SELECT) {
4371 compat_ssize_t clen;
4372
4373 if (len > 1)
4374 return -EINVAL;
4375 if (!access_ok(uiov, sizeof(*uiov)))
4376 return -EFAULT;
4377 if (__get_user(clen, &uiov->iov_len))
4378 return -EFAULT;
4379 if (clen < 0)
4380 return -EINVAL;
1400e697
PB
4381 sr->len = iomsg->iov[0].iov_len;
4382 iomsg->iov = NULL;
52de1fe1
JA
4383 } else {
4384 ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV,
1400e697
PB
4385 &iomsg->iov,
4386 &iomsg->msg.msg_iter);
52de1fe1
JA
4387 if (ret < 0)
4388 return ret;
4389 }
4390
4391 return 0;
4392}
4393#endif
4394
1400e697
PB
4395static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4396 struct io_async_msghdr *iomsg)
52de1fe1 4397{
1400e697
PB
4398 iomsg->msg.msg_name = &iomsg->addr;
4399 iomsg->iov = iomsg->fast_iov;
52de1fe1
JA
4400
4401#ifdef CONFIG_COMPAT
4402 if (req->ctx->compat)
1400e697 4403 return __io_compat_recvmsg_copy_hdr(req, iomsg);
fddaface 4404#endif
52de1fe1 4405
1400e697 4406 return __io_recvmsg_copy_hdr(req, iomsg);
52de1fe1
JA
4407}
4408
bcda7baa 4409static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
7fbb1b54 4410 bool needs_lock)
bcda7baa
JA
4411{
4412 struct io_sr_msg *sr = &req->sr_msg;
4413 struct io_buffer *kbuf;
4414
bcda7baa
JA
4415 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4416 if (IS_ERR(kbuf))
4417 return kbuf;
4418
4419 sr->kbuf = kbuf;
4420 req->flags |= REQ_F_BUFFER_SELECTED;
bcda7baa 4421 return kbuf;
fddaface
JA
4422}
4423
7fbb1b54
PB
4424static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4425{
4426 return io_put_kbuf(req, req->sr_msg.kbuf);
4427}
4428
3529d8c2
JA
4429static int io_recvmsg_prep(struct io_kiocb *req,
4430 const struct io_uring_sqe *sqe)
aa1fa28f 4431{
e8c2bc1f 4432 struct io_async_msghdr *async_msg = req->async_data;
e47293fd 4433 struct io_sr_msg *sr = &req->sr_msg;
99bc4c38 4434 int ret;
3529d8c2 4435
d2b6f48b
PB
4436 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4437 return -EINVAL;
4438
3529d8c2 4439 sr->msg_flags = READ_ONCE(sqe->msg_flags);
270a5940 4440 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
0b7b21e4 4441 sr->len = READ_ONCE(sqe->len);
bcda7baa 4442 sr->bgid = READ_ONCE(sqe->buf_group);
06b76d44 4443
d8768362
JA
4444#ifdef CONFIG_COMPAT
4445 if (req->ctx->compat)
4446 sr->msg_flags |= MSG_CMSG_COMPAT;
4447#endif
4448
e8c2bc1f 4449 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
06b76d44 4450 return 0;
5f798bea
PB
4451 /* iovec is already imported */
4452 if (req->flags & REQ_F_NEED_CLEANUP)
4453 return 0;
03b1230c 4454
e8c2bc1f 4455 ret = io_recvmsg_copy_hdr(req, async_msg);
99bc4c38
PB
4456 if (!ret)
4457 req->flags |= REQ_F_NEED_CLEANUP;
4458 return ret;
aa1fa28f
JA
4459}
4460
229a7b63
JA
4461static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4462 struct io_comp_state *cs)
aa1fa28f 4463{
6b754c8b 4464 struct io_async_msghdr iomsg, *kmsg;
03b1230c 4465 struct socket *sock;
7fbb1b54 4466 struct io_buffer *kbuf;
7a7cacba 4467 unsigned flags;
52de1fe1 4468 int ret, cflags = 0;
03b1230c 4469
03b1230c 4470 sock = sock_from_file(req->file, &ret);
7a7cacba
PB
4471 if (unlikely(!sock))
4472 return ret;
3529d8c2 4473
e8c2bc1f
JA
4474 if (req->async_data) {
4475 kmsg = req->async_data;
4476 kmsg->msg.msg_name = &kmsg->addr;
7a7cacba
PB
4477 /* if iov is set, it's allocated already */
4478 if (!kmsg->iov)
4479 kmsg->iov = kmsg->fast_iov;
4480 kmsg->msg.msg_iter.iov = kmsg->iov;
4481 } else {
4482 ret = io_recvmsg_copy_hdr(req, &iomsg);
4483 if (ret)
681fda8d 4484 return ret;
7a7cacba
PB
4485 kmsg = &iomsg;
4486 }
03b1230c 4487
bc02ef33 4488 if (req->flags & REQ_F_BUFFER_SELECT) {
7fbb1b54 4489 kbuf = io_recv_buffer_select(req, !force_nonblock);
bc02ef33 4490 if (IS_ERR(kbuf))
52de1fe1 4491 return PTR_ERR(kbuf);
7a7cacba
PB
4492 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
4493 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
4494 1, req->sr_msg.len);
4495 }
52de1fe1 4496
7a7cacba
PB
4497 flags = req->sr_msg.msg_flags;
4498 if (flags & MSG_DONTWAIT)
4499 req->flags |= REQ_F_NOWAIT;
4500 else if (force_nonblock)
4501 flags |= MSG_DONTWAIT;
e47293fd 4502
7a7cacba
PB
4503 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4504 kmsg->uaddr, flags);
0e1b6fe3
PB
4505 if (force_nonblock && ret == -EAGAIN)
4506 return io_setup_async_msg(req, kmsg);
7a7cacba
PB
4507 if (ret == -ERESTARTSYS)
4508 ret = -EINTR;
03b1230c 4509
7fbb1b54
PB
4510 if (req->flags & REQ_F_BUFFER_SELECTED)
4511 cflags = io_put_recv_kbuf(req);
6b754c8b 4512 if (kmsg->iov != kmsg->fast_iov)
0b416c3e 4513 kfree(kmsg->iov);
99bc4c38 4514 req->flags &= ~REQ_F_NEED_CLEANUP;
4e88d6e7
JA
4515 if (ret < 0)
4516 req_set_fail_links(req);
229a7b63 4517 __io_req_complete(req, ret, cflags, cs);
03b1230c 4518 return 0;
0fa03c62 4519}
5d17b4a4 4520
229a7b63
JA
4521static int io_recv(struct io_kiocb *req, bool force_nonblock,
4522 struct io_comp_state *cs)
fddaface 4523{
6b754c8b 4524 struct io_buffer *kbuf;
7a7cacba
PB
4525 struct io_sr_msg *sr = &req->sr_msg;
4526 struct msghdr msg;
4527 void __user *buf = sr->buf;
fddaface 4528 struct socket *sock;
7a7cacba
PB
4529 struct iovec iov;
4530 unsigned flags;
bcda7baa 4531 int ret, cflags = 0;
fddaface 4532
fddaface 4533 sock = sock_from_file(req->file, &ret);
7a7cacba
PB
4534 if (unlikely(!sock))
4535 return ret;
fddaface 4536
bc02ef33 4537 if (req->flags & REQ_F_BUFFER_SELECT) {
7fbb1b54 4538 kbuf = io_recv_buffer_select(req, !force_nonblock);
bcda7baa
JA
4539 if (IS_ERR(kbuf))
4540 return PTR_ERR(kbuf);
7a7cacba 4541 buf = u64_to_user_ptr(kbuf->addr);
bc02ef33 4542 }
bcda7baa 4543
7a7cacba 4544 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
14c32eee
PB
4545 if (unlikely(ret))
4546 goto out_free;
fddaface 4547
7a7cacba
PB
4548 msg.msg_name = NULL;
4549 msg.msg_control = NULL;
4550 msg.msg_controllen = 0;
4551 msg.msg_namelen = 0;
4552 msg.msg_iocb = NULL;
4553 msg.msg_flags = 0;
fddaface 4554
7a7cacba
PB
4555 flags = req->sr_msg.msg_flags;
4556 if (flags & MSG_DONTWAIT)
4557 req->flags |= REQ_F_NOWAIT;
4558 else if (force_nonblock)
4559 flags |= MSG_DONTWAIT;
4560
4561 ret = sock_recvmsg(sock, &msg, flags);
4562 if (force_nonblock && ret == -EAGAIN)
4563 return -EAGAIN;
4564 if (ret == -ERESTARTSYS)
4565 ret = -EINTR;
14c32eee 4566out_free:
7fbb1b54
PB
4567 if (req->flags & REQ_F_BUFFER_SELECTED)
4568 cflags = io_put_recv_kbuf(req);
fddaface
JA
4569 if (ret < 0)
4570 req_set_fail_links(req);
229a7b63 4571 __io_req_complete(req, ret, cflags, cs);
fddaface 4572 return 0;
fddaface
JA
4573}
4574
3529d8c2 4575static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
17f2fe35 4576{
8ed8d3c3
JA
4577 struct io_accept *accept = &req->accept;
4578
17f2fe35
JA
4579 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4580 return -EINVAL;
8042d6ce 4581 if (sqe->ioprio || sqe->len || sqe->buf_index)
17f2fe35
JA
4582 return -EINVAL;
4583
d55e5f5b
JA
4584 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4585 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
8ed8d3c3 4586 accept->flags = READ_ONCE(sqe->accept_flags);
09952e3e 4587 accept->nofile = rlimit(RLIMIT_NOFILE);
8ed8d3c3 4588 return 0;
8ed8d3c3 4589}
17f2fe35 4590
229a7b63
JA
4591static int io_accept(struct io_kiocb *req, bool force_nonblock,
4592 struct io_comp_state *cs)
8ed8d3c3
JA
4593{
4594 struct io_accept *accept = &req->accept;
ac45abc0 4595 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
8ed8d3c3
JA
4596 int ret;
4597
e697deed
JX
4598 if (req->file->f_flags & O_NONBLOCK)
4599 req->flags |= REQ_F_NOWAIT;
4600
8ed8d3c3 4601 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
09952e3e
JA
4602 accept->addr_len, accept->flags,
4603 accept->nofile);
8ed8d3c3 4604 if (ret == -EAGAIN && force_nonblock)
17f2fe35 4605 return -EAGAIN;
ac45abc0
PB
4606 if (ret < 0) {
4607 if (ret == -ERESTARTSYS)
4608 ret = -EINTR;
4e88d6e7 4609 req_set_fail_links(req);
ac45abc0 4610 }
229a7b63 4611 __io_req_complete(req, ret, 0, cs);
17f2fe35 4612 return 0;
8ed8d3c3
JA
4613}
4614
3529d8c2 4615static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
f499a021 4616{
3529d8c2 4617 struct io_connect *conn = &req->connect;
e8c2bc1f 4618 struct io_async_connect *io = req->async_data;
f499a021 4619
3fbb51c1
JA
4620 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4621 return -EINVAL;
4622 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4623 return -EINVAL;
4624
3529d8c2
JA
4625 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4626 conn->addr_len = READ_ONCE(sqe->addr2);
4627
4628 if (!io)
4629 return 0;
4630
4631 return move_addr_to_kernel(conn->addr, conn->addr_len,
e8c2bc1f 4632 &io->address);
f499a021
JA
4633}
4634
229a7b63
JA
4635static int io_connect(struct io_kiocb *req, bool force_nonblock,
4636 struct io_comp_state *cs)
f8e85cf2 4637{
e8c2bc1f 4638 struct io_async_connect __io, *io;
f8e85cf2 4639 unsigned file_flags;
3fbb51c1 4640 int ret;
f8e85cf2 4641
e8c2bc1f
JA
4642 if (req->async_data) {
4643 io = req->async_data;
f499a021 4644 } else {
3529d8c2
JA
4645 ret = move_addr_to_kernel(req->connect.addr,
4646 req->connect.addr_len,
e8c2bc1f 4647 &__io.address);
f499a021
JA
4648 if (ret)
4649 goto out;
4650 io = &__io;
4651 }
4652
3fbb51c1
JA
4653 file_flags = force_nonblock ? O_NONBLOCK : 0;
4654
e8c2bc1f 4655 ret = __sys_connect_file(req->file, &io->address,
3fbb51c1 4656 req->connect.addr_len, file_flags);
87f80d62 4657 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
e8c2bc1f 4658 if (req->async_data)
b7bb4f7d 4659 return -EAGAIN;
e8c2bc1f 4660 if (io_alloc_async_data(req)) {
f499a021
JA
4661 ret = -ENOMEM;
4662 goto out;
4663 }
e8c2bc1f
JA
4664 io = req->async_data;
4665 memcpy(req->async_data, &__io, sizeof(__io));
f8e85cf2 4666 return -EAGAIN;
f499a021 4667 }
f8e85cf2
JA
4668 if (ret == -ERESTARTSYS)
4669 ret = -EINTR;
f499a021 4670out:
4e88d6e7
JA
4671 if (ret < 0)
4672 req_set_fail_links(req);
229a7b63 4673 __io_req_complete(req, ret, 0, cs);
f8e85cf2 4674 return 0;
469956e8
Y
4675}
4676#else /* !CONFIG_NET */
4677static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4678{
f8e85cf2 4679 return -EOPNOTSUPP;
f8e85cf2
JA
4680}
4681
1e16c2f9
RD
4682static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4683 struct io_comp_state *cs)
469956e8
Y
4684{
4685 return -EOPNOTSUPP;
4686}
4687
1e16c2f9
RD
4688static int io_send(struct io_kiocb *req, bool force_nonblock,
4689 struct io_comp_state *cs)
469956e8
Y
4690{
4691 return -EOPNOTSUPP;
4692}
4693
4694static int io_recvmsg_prep(struct io_kiocb *req,
4695 const struct io_uring_sqe *sqe)
4696{
4697 return -EOPNOTSUPP;
4698}
4699
1e16c2f9
RD
4700static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4701 struct io_comp_state *cs)
469956e8
Y
4702{
4703 return -EOPNOTSUPP;
4704}
4705
1e16c2f9
RD
4706static int io_recv(struct io_kiocb *req, bool force_nonblock,
4707 struct io_comp_state *cs)
469956e8
Y
4708{
4709 return -EOPNOTSUPP;
4710}
4711
4712static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4713{
4714 return -EOPNOTSUPP;
4715}
4716
1e16c2f9
RD
4717static int io_accept(struct io_kiocb *req, bool force_nonblock,
4718 struct io_comp_state *cs)
469956e8
Y
4719{
4720 return -EOPNOTSUPP;
4721}
ce593a6c 4722
469956e8
Y
4723static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4724{
4725 return -EOPNOTSUPP;
4726}
4727
1e16c2f9
RD
4728static int io_connect(struct io_kiocb *req, bool force_nonblock,
4729 struct io_comp_state *cs)
469956e8 4730{
f8e85cf2 4731 return -EOPNOTSUPP;
ce593a6c 4732}
469956e8 4733#endif /* CONFIG_NET */
f8e85cf2 4734
d7718a9d
JA
4735struct io_poll_table {
4736 struct poll_table_struct pt;
4737 struct io_kiocb *req;
4738 int error;
4739};
ce593a6c 4740
d7718a9d
JA
4741static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4742 __poll_t mask, task_work_func_t func)
4743{
fd7d6de2 4744 bool twa_signal_ok;
aa96bf8a 4745 int ret;
d7718a9d
JA
4746
4747 /* for instances that support it check for an event match first: */
4748 if (mask && !(mask & poll->events))
4749 return 0;
4750
4751 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4752
4753 list_del_init(&poll->wait.entry);
4754
d7718a9d
JA
4755 req->result = mask;
4756 init_task_work(&req->task_work, func);
6d816e08
JA
4757 percpu_ref_get(&req->ctx->refs);
4758
fd7d6de2
JA
4759 /*
4760 * If we using the signalfd wait_queue_head for this wakeup, then
4761 * it's not safe to use TWA_SIGNAL as we could be recursing on the
4762 * tsk->sighand->siglock on doing the wakeup. Should not be needed
4763 * either, as the normal wakeup will suffice.
4764 */
4765 twa_signal_ok = (poll->head != &req->task->sighand->signalfd_wqh);
4766
d7718a9d 4767 /*
e3aabf95
JA
4768 * If this fails, then the task is exiting. When a task exits, the
4769 * work gets canceled, so just cancel this request as well instead
4770 * of executing it. We can't safely execute it anyway, as we may not
4771 * have the needed state needed for it anyway.
d7718a9d 4772 */
fd7d6de2 4773 ret = io_req_task_work_add(req, &req->task_work, twa_signal_ok);
aa96bf8a 4774 if (unlikely(ret)) {
c2c4c83c
JA
4775 struct task_struct *tsk;
4776
e3aabf95 4777 WRITE_ONCE(poll->canceled, true);
aa96bf8a 4778 tsk = io_wq_get_task(req->ctx->io_wq);
ce593a6c
JA
4779 task_work_add(tsk, &req->task_work, 0);
4780 wake_up_process(tsk);
aa96bf8a 4781 }
d7718a9d
JA
4782 return 1;
4783}
4784
74ce6ce4
JA
4785static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4786 __acquires(&req->ctx->completion_lock)
4787{
4788 struct io_ring_ctx *ctx = req->ctx;
4789
4790 if (!req->result && !READ_ONCE(poll->canceled)) {
4791 struct poll_table_struct pt = { ._key = poll->events };
4792
4793 req->result = vfs_poll(req->file, &pt) & poll->events;
4794 }
4795
4796 spin_lock_irq(&ctx->completion_lock);
4797 if (!req->result && !READ_ONCE(poll->canceled)) {
4798 add_wait_queue(poll->head, &poll->wait);
4799 return true;
4800 }
4801
4802 return false;
4803}
4804
d4e7cd36 4805static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
18bceab1 4806{
e8c2bc1f 4807 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
d4e7cd36 4808 if (req->opcode == IORING_OP_POLL_ADD)
e8c2bc1f 4809 return req->async_data;
d4e7cd36
JA
4810 return req->apoll->double_poll;
4811}
4812
4813static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
4814{
4815 if (req->opcode == IORING_OP_POLL_ADD)
4816 return &req->poll;
4817 return &req->apoll->poll;
4818}
4819
4820static void io_poll_remove_double(struct io_kiocb *req)
4821{
4822 struct io_poll_iocb *poll = io_poll_get_double(req);
18bceab1
JA
4823
4824 lockdep_assert_held(&req->ctx->completion_lock);
4825
4826 if (poll && poll->head) {
4827 struct wait_queue_head *head = poll->head;
4828
4829 spin_lock(&head->lock);
4830 list_del_init(&poll->wait.entry);
4831 if (poll->wait.private)
4832 refcount_dec(&req->refs);
4833 poll->head = NULL;
4834 spin_unlock(&head->lock);
4835 }
4836}
4837
4838static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
4839{
4840 struct io_ring_ctx *ctx = req->ctx;
4841
d4e7cd36 4842 io_poll_remove_double(req);
18bceab1
JA
4843 req->poll.done = true;
4844 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
4845 io_commit_cqring(ctx);
4846}
4847
4848static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
4849{
4850 struct io_ring_ctx *ctx = req->ctx;
4851
4852 if (io_poll_rewait(req, &req->poll)) {
4853 spin_unlock_irq(&ctx->completion_lock);
4854 return;
4855 }
4856
4857 hash_del(&req->hash_node);
4858 io_poll_complete(req, req->result, 0);
4859 req->flags |= REQ_F_COMP_LOCKED;
9b5f7bd9 4860 *nxt = io_put_req_find_next(req);
18bceab1
JA
4861 spin_unlock_irq(&ctx->completion_lock);
4862
4863 io_cqring_ev_posted(ctx);
4864}
4865
4866static void io_poll_task_func(struct callback_head *cb)
4867{
4868 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
6d816e08 4869 struct io_ring_ctx *ctx = req->ctx;
18bceab1
JA
4870 struct io_kiocb *nxt = NULL;
4871
4872 io_poll_task_handler(req, &nxt);
ea1164e5
PB
4873 if (nxt)
4874 __io_req_task_submit(nxt);
6d816e08 4875 percpu_ref_put(&ctx->refs);
18bceab1
JA
4876}
4877
4878static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
4879 int sync, void *key)
4880{
4881 struct io_kiocb *req = wait->private;
d4e7cd36 4882 struct io_poll_iocb *poll = io_poll_get_single(req);
18bceab1
JA
4883 __poll_t mask = key_to_poll(key);
4884
4885 /* for instances that support it check for an event match first: */
4886 if (mask && !(mask & poll->events))
4887 return 0;
4888
8706e04e
JA
4889 list_del_init(&wait->entry);
4890
807abcb0 4891 if (poll && poll->head) {
18bceab1
JA
4892 bool done;
4893
807abcb0
JA
4894 spin_lock(&poll->head->lock);
4895 done = list_empty(&poll->wait.entry);
18bceab1 4896 if (!done)
807abcb0 4897 list_del_init(&poll->wait.entry);
d4e7cd36
JA
4898 /* make sure double remove sees this as being gone */
4899 wait->private = NULL;
807abcb0 4900 spin_unlock(&poll->head->lock);
18bceab1
JA
4901 if (!done)
4902 __io_async_wake(req, poll, mask, io_poll_task_func);
4903 }
4904 refcount_dec(&req->refs);
4905 return 1;
4906}
4907
4908static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
4909 wait_queue_func_t wake_func)
4910{
4911 poll->head = NULL;
4912 poll->done = false;
4913 poll->canceled = false;
4914 poll->events = events;
4915 INIT_LIST_HEAD(&poll->wait.entry);
4916 init_waitqueue_func_entry(&poll->wait, wake_func);
4917}
4918
4919static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
807abcb0
JA
4920 struct wait_queue_head *head,
4921 struct io_poll_iocb **poll_ptr)
18bceab1
JA
4922{
4923 struct io_kiocb *req = pt->req;
4924
4925 /*
4926 * If poll->head is already set, it's because the file being polled
4927 * uses multiple waitqueues for poll handling (eg one for read, one
4928 * for write). Setup a separate io_poll_iocb if this happens.
4929 */
4930 if (unlikely(poll->head)) {
4931 /* already have a 2nd entry, fail a third attempt */
807abcb0 4932 if (*poll_ptr) {
18bceab1
JA
4933 pt->error = -EINVAL;
4934 return;
4935 }
4936 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
4937 if (!poll) {
4938 pt->error = -ENOMEM;
4939 return;
4940 }
4941 io_init_poll_iocb(poll, req->poll.events, io_poll_double_wake);
4942 refcount_inc(&req->refs);
4943 poll->wait.private = req;
807abcb0 4944 *poll_ptr = poll;
18bceab1
JA
4945 }
4946
4947 pt->error = 0;
4948 poll->head = head;
a31eb4a2
JX
4949
4950 if (poll->events & EPOLLEXCLUSIVE)
4951 add_wait_queue_exclusive(head, &poll->wait);
4952 else
4953 add_wait_queue(head, &poll->wait);
18bceab1
JA
4954}
4955
4956static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
4957 struct poll_table_struct *p)
4958{
4959 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
807abcb0 4960 struct async_poll *apoll = pt->req->apoll;
18bceab1 4961
807abcb0 4962 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
18bceab1
JA
4963}
4964
d7718a9d
JA
4965static void io_async_task_func(struct callback_head *cb)
4966{
4967 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4968 struct async_poll *apoll = req->apoll;
4969 struct io_ring_ctx *ctx = req->ctx;
4970
4971 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
4972
74ce6ce4 4973 if (io_poll_rewait(req, &apoll->poll)) {
d7718a9d 4974 spin_unlock_irq(&ctx->completion_lock);
6d816e08 4975 percpu_ref_put(&ctx->refs);
74ce6ce4 4976 return;
d7718a9d
JA
4977 }
4978
31067255 4979 /* If req is still hashed, it cannot have been canceled. Don't check. */
0be0b0e3 4980 if (hash_hashed(&req->hash_node))
74ce6ce4 4981 hash_del(&req->hash_node);
2bae047e 4982
d4e7cd36 4983 io_poll_remove_double(req);
74ce6ce4
JA
4984 spin_unlock_irq(&ctx->completion_lock);
4985
0be0b0e3
PB
4986 if (!READ_ONCE(apoll->poll.canceled))
4987 __io_req_task_submit(req);
4988 else
4989 __io_req_task_cancel(req, -ECANCELED);
aa340845 4990
6d816e08 4991 percpu_ref_put(&ctx->refs);
807abcb0 4992 kfree(apoll->double_poll);
31067255 4993 kfree(apoll);
d7718a9d
JA
4994}
4995
4996static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4997 void *key)
4998{
4999 struct io_kiocb *req = wait->private;
5000 struct io_poll_iocb *poll = &req->apoll->poll;
5001
5002 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
5003 key_to_poll(key));
5004
5005 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
5006}
5007
5008static void io_poll_req_insert(struct io_kiocb *req)
5009{
5010 struct io_ring_ctx *ctx = req->ctx;
5011 struct hlist_head *list;
5012
5013 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5014 hlist_add_head(&req->hash_node, list);
5015}
5016
5017static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5018 struct io_poll_iocb *poll,
5019 struct io_poll_table *ipt, __poll_t mask,
5020 wait_queue_func_t wake_func)
5021 __acquires(&ctx->completion_lock)
5022{
5023 struct io_ring_ctx *ctx = req->ctx;
5024 bool cancel = false;
5025
18bceab1 5026 io_init_poll_iocb(poll, mask, wake_func);
b90cd197 5027 poll->file = req->file;
18bceab1 5028 poll->wait.private = req;
d7718a9d
JA
5029
5030 ipt->pt._key = mask;
5031 ipt->req = req;
5032 ipt->error = -EINVAL;
5033
d7718a9d
JA
5034 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
5035
5036 spin_lock_irq(&ctx->completion_lock);
5037 if (likely(poll->head)) {
5038 spin_lock(&poll->head->lock);
5039 if (unlikely(list_empty(&poll->wait.entry))) {
5040 if (ipt->error)
5041 cancel = true;
5042 ipt->error = 0;
5043 mask = 0;
5044 }
5045 if (mask || ipt->error)
5046 list_del_init(&poll->wait.entry);
5047 else if (cancel)
5048 WRITE_ONCE(poll->canceled, true);
5049 else if (!poll->done) /* actually waiting for an event */
5050 io_poll_req_insert(req);
5051 spin_unlock(&poll->head->lock);
5052 }
5053
5054 return mask;
5055}
5056
5057static bool io_arm_poll_handler(struct io_kiocb *req)
5058{
5059 const struct io_op_def *def = &io_op_defs[req->opcode];
5060 struct io_ring_ctx *ctx = req->ctx;
5061 struct async_poll *apoll;
5062 struct io_poll_table ipt;
5063 __poll_t mask, ret;
9dab14b8 5064 int rw;
d7718a9d
JA
5065
5066 if (!req->file || !file_can_poll(req->file))
5067 return false;
24c74678 5068 if (req->flags & REQ_F_POLLED)
d7718a9d 5069 return false;
9dab14b8
JA
5070 if (def->pollin)
5071 rw = READ;
5072 else if (def->pollout)
5073 rw = WRITE;
5074 else
5075 return false;
5076 /* if we can't nonblock try, then no point in arming a poll handler */
5077 if (!io_file_supports_async(req->file, rw))
d7718a9d
JA
5078 return false;
5079
5080 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5081 if (unlikely(!apoll))
5082 return false;
807abcb0 5083 apoll->double_poll = NULL;
d7718a9d
JA
5084
5085 req->flags |= REQ_F_POLLED;
d7718a9d
JA
5086 req->apoll = apoll;
5087 INIT_HLIST_NODE(&req->hash_node);
5088
8755d97a 5089 mask = 0;
d7718a9d 5090 if (def->pollin)
8755d97a 5091 mask |= POLLIN | POLLRDNORM;
d7718a9d
JA
5092 if (def->pollout)
5093 mask |= POLLOUT | POLLWRNORM;
5094 mask |= POLLERR | POLLPRI;
5095
5096 ipt.pt._qproc = io_async_queue_proc;
5097
5098 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5099 io_async_wake);
a36da65c 5100 if (ret || ipt.error) {
d4e7cd36 5101 io_poll_remove_double(req);
d7718a9d 5102 spin_unlock_irq(&ctx->completion_lock);
807abcb0 5103 kfree(apoll->double_poll);
d7718a9d
JA
5104 kfree(apoll);
5105 return false;
5106 }
5107 spin_unlock_irq(&ctx->completion_lock);
5108 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
5109 apoll->poll.events);
5110 return true;
5111}
5112
5113static bool __io_poll_remove_one(struct io_kiocb *req,
5114 struct io_poll_iocb *poll)
221c5eb2 5115{
b41e9852 5116 bool do_complete = false;
221c5eb2
JA
5117
5118 spin_lock(&poll->head->lock);
5119 WRITE_ONCE(poll->canceled, true);
392edb45
JA
5120 if (!list_empty(&poll->wait.entry)) {
5121 list_del_init(&poll->wait.entry);
b41e9852 5122 do_complete = true;
221c5eb2
JA
5123 }
5124 spin_unlock(&poll->head->lock);
3bfa5bcb 5125 hash_del(&req->hash_node);
d7718a9d
JA
5126 return do_complete;
5127}
5128
5129static bool io_poll_remove_one(struct io_kiocb *req)
5130{
5131 bool do_complete;
5132
d4e7cd36
JA
5133 io_poll_remove_double(req);
5134
d7718a9d
JA
5135 if (req->opcode == IORING_OP_POLL_ADD) {
5136 do_complete = __io_poll_remove_one(req, &req->poll);
5137 } else {
3bfa5bcb
JA
5138 struct async_poll *apoll = req->apoll;
5139
d7718a9d 5140 /* non-poll requests have submit ref still */
3bfa5bcb
JA
5141 do_complete = __io_poll_remove_one(req, &apoll->poll);
5142 if (do_complete) {
d7718a9d 5143 io_put_req(req);
807abcb0 5144 kfree(apoll->double_poll);
3bfa5bcb
JA
5145 kfree(apoll);
5146 }
b1f573bd
XW
5147 }
5148
b41e9852
JA
5149 if (do_complete) {
5150 io_cqring_fill_event(req, -ECANCELED);
5151 io_commit_cqring(req->ctx);
5152 req->flags |= REQ_F_COMP_LOCKED;
f254ac04 5153 req_set_fail_links(req);
b41e9852
JA
5154 io_put_req(req);
5155 }
5156
5157 return do_complete;
221c5eb2
JA
5158}
5159
76e1b642
JA
5160/*
5161 * Returns true if we found and killed one or more poll requests
5162 */
5163static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk)
221c5eb2 5164{
78076bb6 5165 struct hlist_node *tmp;
221c5eb2 5166 struct io_kiocb *req;
8e2e1faf 5167 int posted = 0, i;
221c5eb2
JA
5168
5169 spin_lock_irq(&ctx->completion_lock);
78076bb6
JA
5170 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5171 struct hlist_head *list;
5172
5173 list = &ctx->cancel_hash[i];
f3606e3a
JA
5174 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
5175 if (io_task_match(req, tsk))
5176 posted += io_poll_remove_one(req);
5177 }
221c5eb2
JA
5178 }
5179 spin_unlock_irq(&ctx->completion_lock);
b41e9852 5180
8e2e1faf
JA
5181 if (posted)
5182 io_cqring_ev_posted(ctx);
76e1b642
JA
5183
5184 return posted != 0;
221c5eb2
JA
5185}
5186
47f46768
JA
5187static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5188{
78076bb6 5189 struct hlist_head *list;
47f46768
JA
5190 struct io_kiocb *req;
5191
78076bb6
JA
5192 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5193 hlist_for_each_entry(req, list, hash_node) {
b41e9852
JA
5194 if (sqe_addr != req->user_data)
5195 continue;
5196 if (io_poll_remove_one(req))
eac406c6 5197 return 0;
b41e9852 5198 return -EALREADY;
47f46768
JA
5199 }
5200
5201 return -ENOENT;
5202}
5203
3529d8c2
JA
5204static int io_poll_remove_prep(struct io_kiocb *req,
5205 const struct io_uring_sqe *sqe)
0969e783 5206{
0969e783
JA
5207 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5208 return -EINVAL;
5209 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5210 sqe->poll_events)
5211 return -EINVAL;
5212
5213 req->poll.addr = READ_ONCE(sqe->addr);
0969e783
JA
5214 return 0;
5215}
5216
221c5eb2
JA
5217/*
5218 * Find a running poll command that matches one specified in sqe->addr,
5219 * and remove it if found.
5220 */
fc4df999 5221static int io_poll_remove(struct io_kiocb *req)
221c5eb2
JA
5222{
5223 struct io_ring_ctx *ctx = req->ctx;
0969e783 5224 u64 addr;
47f46768 5225 int ret;
221c5eb2 5226
0969e783 5227 addr = req->poll.addr;
221c5eb2 5228 spin_lock_irq(&ctx->completion_lock);
0969e783 5229 ret = io_poll_cancel(ctx, addr);
221c5eb2
JA
5230 spin_unlock_irq(&ctx->completion_lock);
5231
4e88d6e7
JA
5232 if (ret < 0)
5233 req_set_fail_links(req);
e1e16097 5234 io_req_complete(req, ret);
221c5eb2
JA
5235 return 0;
5236}
5237
221c5eb2
JA
5238static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5239 void *key)
5240{
c2f2eb7d
JA
5241 struct io_kiocb *req = wait->private;
5242 struct io_poll_iocb *poll = &req->poll;
221c5eb2 5243
d7718a9d 5244 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
221c5eb2
JA
5245}
5246
221c5eb2
JA
5247static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5248 struct poll_table_struct *p)
5249{
5250 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5251
e8c2bc1f 5252 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
eac406c6
JA
5253}
5254
3529d8c2 5255static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
221c5eb2
JA
5256{
5257 struct io_poll_iocb *poll = &req->poll;
5769a351 5258 u32 events;
221c5eb2
JA
5259
5260 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5261 return -EINVAL;
5262 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5263 return -EINVAL;
09bb8394
JA
5264 if (!poll->file)
5265 return -EBADF;
221c5eb2 5266
5769a351
JX
5267 events = READ_ONCE(sqe->poll32_events);
5268#ifdef __BIG_ENDIAN
5269 events = swahw32(events);
5270#endif
a31eb4a2
JX
5271 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5272 (events & EPOLLEXCLUSIVE);
0969e783
JA
5273 return 0;
5274}
5275
014db007 5276static int io_poll_add(struct io_kiocb *req)
0969e783
JA
5277{
5278 struct io_poll_iocb *poll = &req->poll;
5279 struct io_ring_ctx *ctx = req->ctx;
5280 struct io_poll_table ipt;
0969e783 5281 __poll_t mask;
0969e783 5282
78076bb6 5283 INIT_HLIST_NODE(&req->hash_node);
d7718a9d 5284 ipt.pt._qproc = io_poll_queue_proc;
36703247 5285
d7718a9d
JA
5286 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5287 io_poll_wake);
221c5eb2 5288
8c838788 5289 if (mask) { /* no async, we'd stolen it */
221c5eb2 5290 ipt.error = 0;
b0dd8a41 5291 io_poll_complete(req, mask, 0);
221c5eb2 5292 }
221c5eb2
JA
5293 spin_unlock_irq(&ctx->completion_lock);
5294
8c838788
JA
5295 if (mask) {
5296 io_cqring_ev_posted(ctx);
014db007 5297 io_put_req(req);
221c5eb2 5298 }
8c838788 5299 return ipt.error;
221c5eb2
JA
5300}
5301
5262f567
JA
5302static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5303{
ad8a48ac
JA
5304 struct io_timeout_data *data = container_of(timer,
5305 struct io_timeout_data, timer);
5306 struct io_kiocb *req = data->req;
5307 struct io_ring_ctx *ctx = req->ctx;
5262f567
JA
5308 unsigned long flags;
5309
5262f567 5310 spin_lock_irqsave(&ctx->completion_lock, flags);
01cec8c1
PB
5311 atomic_set(&req->ctx->cq_timeouts,
5312 atomic_read(&req->ctx->cq_timeouts) + 1);
5313
ef03681a 5314 /*
11365043
JA
5315 * We could be racing with timeout deletion. If the list is empty,
5316 * then timeout lookup already found it and will be handling it.
ef03681a 5317 */
135fcde8
PB
5318 if (!list_empty(&req->timeout.list))
5319 list_del_init(&req->timeout.list);
5262f567 5320
78e19bbe 5321 io_cqring_fill_event(req, -ETIME);
5262f567
JA
5322 io_commit_cqring(ctx);
5323 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5324
5325 io_cqring_ev_posted(ctx);
4e88d6e7 5326 req_set_fail_links(req);
5262f567
JA
5327 io_put_req(req);
5328 return HRTIMER_NORESTART;
5329}
5330
f254ac04
JA
5331static int __io_timeout_cancel(struct io_kiocb *req)
5332{
e8c2bc1f 5333 struct io_timeout_data *io = req->async_data;
f254ac04
JA
5334 int ret;
5335
5336 list_del_init(&req->timeout.list);
5337
e8c2bc1f 5338 ret = hrtimer_try_to_cancel(&io->timer);
f254ac04
JA
5339 if (ret == -1)
5340 return -EALREADY;
5341
5342 req_set_fail_links(req);
5343 req->flags |= REQ_F_COMP_LOCKED;
5344 io_cqring_fill_event(req, -ECANCELED);
5345 io_put_req(req);
5346 return 0;
5347}
5348
47f46768
JA
5349static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5350{
5351 struct io_kiocb *req;
5352 int ret = -ENOENT;
5353
135fcde8 5354 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
47f46768 5355 if (user_data == req->user_data) {
47f46768
JA
5356 ret = 0;
5357 break;
5358 }
5359 }
5360
5361 if (ret == -ENOENT)
5362 return ret;
5363
f254ac04 5364 return __io_timeout_cancel(req);
47f46768
JA
5365}
5366
3529d8c2
JA
5367static int io_timeout_remove_prep(struct io_kiocb *req,
5368 const struct io_uring_sqe *sqe)
b29472ee 5369{
b29472ee
JA
5370 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5371 return -EINVAL;
61710e43
DA
5372 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5373 return -EINVAL;
5374 if (sqe->ioprio || sqe->buf_index || sqe->len)
b29472ee
JA
5375 return -EINVAL;
5376
5377 req->timeout.addr = READ_ONCE(sqe->addr);
5378 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
5379 if (req->timeout.flags)
5380 return -EINVAL;
5381
b29472ee
JA
5382 return 0;
5383}
5384
11365043
JA
5385/*
5386 * Remove or update an existing timeout command
5387 */
fc4df999 5388static int io_timeout_remove(struct io_kiocb *req)
11365043
JA
5389{
5390 struct io_ring_ctx *ctx = req->ctx;
47f46768 5391 int ret;
11365043 5392
11365043 5393 spin_lock_irq(&ctx->completion_lock);
b29472ee 5394 ret = io_timeout_cancel(ctx, req->timeout.addr);
11365043 5395
47f46768 5396 io_cqring_fill_event(req, ret);
11365043
JA
5397 io_commit_cqring(ctx);
5398 spin_unlock_irq(&ctx->completion_lock);
5262f567 5399 io_cqring_ev_posted(ctx);
4e88d6e7
JA
5400 if (ret < 0)
5401 req_set_fail_links(req);
ec9c02ad 5402 io_put_req(req);
11365043 5403 return 0;
5262f567
JA
5404}
5405
3529d8c2 5406static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2d28390a 5407 bool is_timeout_link)
5262f567 5408{
ad8a48ac 5409 struct io_timeout_data *data;
a41525ab 5410 unsigned flags;
56080b02 5411 u32 off = READ_ONCE(sqe->off);
5262f567 5412
ad8a48ac 5413 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5262f567 5414 return -EINVAL;
ad8a48ac 5415 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
a41525ab 5416 return -EINVAL;
56080b02 5417 if (off && is_timeout_link)
2d28390a 5418 return -EINVAL;
a41525ab
JA
5419 flags = READ_ONCE(sqe->timeout_flags);
5420 if (flags & ~IORING_TIMEOUT_ABS)
5262f567 5421 return -EINVAL;
bdf20073 5422
bfe68a22 5423 req->timeout.off = off;
26a61679 5424
e8c2bc1f 5425 if (!req->async_data && io_alloc_async_data(req))
26a61679
JA
5426 return -ENOMEM;
5427
e8c2bc1f 5428 data = req->async_data;
ad8a48ac 5429 data->req = req;
ad8a48ac
JA
5430
5431 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
5262f567
JA
5432 return -EFAULT;
5433
11365043 5434 if (flags & IORING_TIMEOUT_ABS)
ad8a48ac 5435 data->mode = HRTIMER_MODE_ABS;
11365043 5436 else
ad8a48ac 5437 data->mode = HRTIMER_MODE_REL;
11365043 5438
ad8a48ac
JA
5439 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5440 return 0;
5441}
5442
fc4df999 5443static int io_timeout(struct io_kiocb *req)
ad8a48ac 5444{
ad8a48ac 5445 struct io_ring_ctx *ctx = req->ctx;
e8c2bc1f 5446 struct io_timeout_data *data = req->async_data;
ad8a48ac 5447 struct list_head *entry;
bfe68a22 5448 u32 tail, off = req->timeout.off;
ad8a48ac 5449
733f5c95 5450 spin_lock_irq(&ctx->completion_lock);
93bd25bb 5451
5262f567
JA
5452 /*
5453 * sqe->off holds how many events that need to occur for this
93bd25bb
JA
5454 * timeout event to be satisfied. If it isn't set, then this is
5455 * a pure timeout request, sequence isn't used.
5262f567 5456 */
8eb7e2d0 5457 if (io_is_timeout_noseq(req)) {
93bd25bb
JA
5458 entry = ctx->timeout_list.prev;
5459 goto add;
5460 }
5262f567 5461
bfe68a22
PB
5462 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5463 req->timeout.target_seq = tail + off;
5262f567
JA
5464
5465 /*
5466 * Insertion sort, ensuring the first entry in the list is always
5467 * the one we need first.
5468 */
5262f567 5469 list_for_each_prev(entry, &ctx->timeout_list) {
135fcde8
PB
5470 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5471 timeout.list);
5262f567 5472
8eb7e2d0 5473 if (io_is_timeout_noseq(nxt))
93bd25bb 5474 continue;
bfe68a22
PB
5475 /* nxt.seq is behind @tail, otherwise would've been completed */
5476 if (off >= nxt->timeout.target_seq - tail)
5262f567
JA
5477 break;
5478 }
93bd25bb 5479add:
135fcde8 5480 list_add(&req->timeout.list, entry);
ad8a48ac
JA
5481 data->timer.function = io_timeout_fn;
5482 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
5262f567 5483 spin_unlock_irq(&ctx->completion_lock);
5262f567
JA
5484 return 0;
5485}
5262f567 5486
62755e35
JA
5487static bool io_cancel_cb(struct io_wq_work *work, void *data)
5488{
5489 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5490
5491 return req->user_data == (unsigned long) data;
5492}
5493
e977d6d3 5494static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
62755e35 5495{
62755e35 5496 enum io_wq_cancel cancel_ret;
62755e35
JA
5497 int ret = 0;
5498
4f26bda1 5499 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
62755e35
JA
5500 switch (cancel_ret) {
5501 case IO_WQ_CANCEL_OK:
5502 ret = 0;
5503 break;
5504 case IO_WQ_CANCEL_RUNNING:
5505 ret = -EALREADY;
5506 break;
5507 case IO_WQ_CANCEL_NOTFOUND:
5508 ret = -ENOENT;
5509 break;
5510 }
5511
e977d6d3
JA
5512 return ret;
5513}
5514
47f46768
JA
5515static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5516 struct io_kiocb *req, __u64 sqe_addr,
014db007 5517 int success_ret)
47f46768
JA
5518{
5519 unsigned long flags;
5520 int ret;
5521
5522 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5523 if (ret != -ENOENT) {
5524 spin_lock_irqsave(&ctx->completion_lock, flags);
5525 goto done;
5526 }
5527
5528 spin_lock_irqsave(&ctx->completion_lock, flags);
5529 ret = io_timeout_cancel(ctx, sqe_addr);
5530 if (ret != -ENOENT)
5531 goto done;
5532 ret = io_poll_cancel(ctx, sqe_addr);
5533done:
b0dd8a41
JA
5534 if (!ret)
5535 ret = success_ret;
47f46768
JA
5536 io_cqring_fill_event(req, ret);
5537 io_commit_cqring(ctx);
5538 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5539 io_cqring_ev_posted(ctx);
5540
4e88d6e7
JA
5541 if (ret < 0)
5542 req_set_fail_links(req);
014db007 5543 io_put_req(req);
47f46768
JA
5544}
5545
3529d8c2
JA
5546static int io_async_cancel_prep(struct io_kiocb *req,
5547 const struct io_uring_sqe *sqe)
e977d6d3 5548{
fbf23849 5549 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
e977d6d3 5550 return -EINVAL;
61710e43
DA
5551 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5552 return -EINVAL;
5553 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
e977d6d3
JA
5554 return -EINVAL;
5555
fbf23849
JA
5556 req->cancel.addr = READ_ONCE(sqe->addr);
5557 return 0;
5558}
5559
014db007 5560static int io_async_cancel(struct io_kiocb *req)
fbf23849
JA
5561{
5562 struct io_ring_ctx *ctx = req->ctx;
fbf23849 5563
014db007 5564 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
5262f567
JA
5565 return 0;
5566}
5567
05f3fb3c
JA
5568static int io_files_update_prep(struct io_kiocb *req,
5569 const struct io_uring_sqe *sqe)
5570{
6ca56f84
JA
5571 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
5572 return -EINVAL;
61710e43
DA
5573 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5574 return -EINVAL;
5575 if (sqe->ioprio || sqe->rw_flags)
05f3fb3c
JA
5576 return -EINVAL;
5577
5578 req->files_update.offset = READ_ONCE(sqe->off);
5579 req->files_update.nr_args = READ_ONCE(sqe->len);
5580 if (!req->files_update.nr_args)
5581 return -EINVAL;
5582 req->files_update.arg = READ_ONCE(sqe->addr);
5583 return 0;
5584}
5585
229a7b63
JA
5586static int io_files_update(struct io_kiocb *req, bool force_nonblock,
5587 struct io_comp_state *cs)
fbf23849
JA
5588{
5589 struct io_ring_ctx *ctx = req->ctx;
05f3fb3c
JA
5590 struct io_uring_files_update up;
5591 int ret;
fbf23849 5592
f86cd20c 5593 if (force_nonblock)
05f3fb3c 5594 return -EAGAIN;
05f3fb3c
JA
5595
5596 up.offset = req->files_update.offset;
5597 up.fds = req->files_update.arg;
5598
5599 mutex_lock(&ctx->uring_lock);
5600 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
5601 mutex_unlock(&ctx->uring_lock);
5602
5603 if (ret < 0)
5604 req_set_fail_links(req);
229a7b63 5605 __io_req_complete(req, ret, 0, cs);
5262f567
JA
5606 return 0;
5607}
5608
3529d8c2
JA
5609static int io_req_defer_prep(struct io_kiocb *req,
5610 const struct io_uring_sqe *sqe)
f67676d1 5611{
e781573e 5612 ssize_t ret = 0;
f67676d1 5613
f1d96a8f
PB
5614 if (!sqe)
5615 return 0;
5616
e8c2bc1f 5617 if (io_alloc_async_data(req))
327d6d96 5618 return -EAGAIN;
f56040b8
PB
5619 ret = io_prep_work_files(req);
5620 if (unlikely(ret))
5621 return ret;
cccf0ee8 5622
202700e1
JA
5623 io_prep_async_work(req);
5624
d625c6ee 5625 switch (req->opcode) {
e781573e
JA
5626 case IORING_OP_NOP:
5627 break;
f67676d1
JA
5628 case IORING_OP_READV:
5629 case IORING_OP_READ_FIXED:
3a6820f2 5630 case IORING_OP_READ:
3529d8c2 5631 ret = io_read_prep(req, sqe, true);
f67676d1
JA
5632 break;
5633 case IORING_OP_WRITEV:
5634 case IORING_OP_WRITE_FIXED:
3a6820f2 5635 case IORING_OP_WRITE:
3529d8c2 5636 ret = io_write_prep(req, sqe, true);
f67676d1 5637 break;
0969e783 5638 case IORING_OP_POLL_ADD:
3529d8c2 5639 ret = io_poll_add_prep(req, sqe);
0969e783
JA
5640 break;
5641 case IORING_OP_POLL_REMOVE:
3529d8c2 5642 ret = io_poll_remove_prep(req, sqe);
0969e783 5643 break;
8ed8d3c3 5644 case IORING_OP_FSYNC:
3529d8c2 5645 ret = io_prep_fsync(req, sqe);
8ed8d3c3
JA
5646 break;
5647 case IORING_OP_SYNC_FILE_RANGE:
3529d8c2 5648 ret = io_prep_sfr(req, sqe);
8ed8d3c3 5649 break;
03b1230c 5650 case IORING_OP_SENDMSG:
fddaface 5651 case IORING_OP_SEND:
3529d8c2 5652 ret = io_sendmsg_prep(req, sqe);
03b1230c
JA
5653 break;
5654 case IORING_OP_RECVMSG:
fddaface 5655 case IORING_OP_RECV:
3529d8c2 5656 ret = io_recvmsg_prep(req, sqe);
03b1230c 5657 break;
f499a021 5658 case IORING_OP_CONNECT:
3529d8c2 5659 ret = io_connect_prep(req, sqe);
f499a021 5660 break;
2d28390a 5661 case IORING_OP_TIMEOUT:
3529d8c2 5662 ret = io_timeout_prep(req, sqe, false);
b7bb4f7d 5663 break;
b29472ee 5664 case IORING_OP_TIMEOUT_REMOVE:
3529d8c2 5665 ret = io_timeout_remove_prep(req, sqe);
b29472ee 5666 break;
fbf23849 5667 case IORING_OP_ASYNC_CANCEL:
3529d8c2 5668 ret = io_async_cancel_prep(req, sqe);
fbf23849 5669 break;
2d28390a 5670 case IORING_OP_LINK_TIMEOUT:
3529d8c2 5671 ret = io_timeout_prep(req, sqe, true);
b7bb4f7d 5672 break;
8ed8d3c3 5673 case IORING_OP_ACCEPT:
3529d8c2 5674 ret = io_accept_prep(req, sqe);
8ed8d3c3 5675 break;
d63d1b5e
JA
5676 case IORING_OP_FALLOCATE:
5677 ret = io_fallocate_prep(req, sqe);
5678 break;
15b71abe
JA
5679 case IORING_OP_OPENAT:
5680 ret = io_openat_prep(req, sqe);
5681 break;
b5dba59e
JA
5682 case IORING_OP_CLOSE:
5683 ret = io_close_prep(req, sqe);
5684 break;
05f3fb3c
JA
5685 case IORING_OP_FILES_UPDATE:
5686 ret = io_files_update_prep(req, sqe);
5687 break;
eddc7ef5
JA
5688 case IORING_OP_STATX:
5689 ret = io_statx_prep(req, sqe);
5690 break;
4840e418
JA
5691 case IORING_OP_FADVISE:
5692 ret = io_fadvise_prep(req, sqe);
5693 break;
c1ca757b
JA
5694 case IORING_OP_MADVISE:
5695 ret = io_madvise_prep(req, sqe);
5696 break;
cebdb986
JA
5697 case IORING_OP_OPENAT2:
5698 ret = io_openat2_prep(req, sqe);
5699 break;
3e4827b0
JA
5700 case IORING_OP_EPOLL_CTL:
5701 ret = io_epoll_ctl_prep(req, sqe);
5702 break;
7d67af2c
PB
5703 case IORING_OP_SPLICE:
5704 ret = io_splice_prep(req, sqe);
5705 break;
ddf0322d
JA
5706 case IORING_OP_PROVIDE_BUFFERS:
5707 ret = io_provide_buffers_prep(req, sqe);
5708 break;
067524e9
JA
5709 case IORING_OP_REMOVE_BUFFERS:
5710 ret = io_remove_buffers_prep(req, sqe);
5711 break;
f2a8d5c7
PB
5712 case IORING_OP_TEE:
5713 ret = io_tee_prep(req, sqe);
5714 break;
f67676d1 5715 default:
e781573e
JA
5716 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5717 req->opcode);
5718 ret = -EINVAL;
b7bb4f7d 5719 break;
f67676d1
JA
5720 }
5721
b7bb4f7d 5722 return ret;
f67676d1
JA
5723}
5724
9cf7c104
PB
5725static u32 io_get_sequence(struct io_kiocb *req)
5726{
5727 struct io_kiocb *pos;
5728 struct io_ring_ctx *ctx = req->ctx;
5729 u32 total_submitted, nr_reqs = 1;
5730
5731 if (req->flags & REQ_F_LINK_HEAD)
5732 list_for_each_entry(pos, &req->link_list, link_list)
5733 nr_reqs++;
5734
5735 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
5736 return total_submitted - nr_reqs;
5737}
5738
3529d8c2 5739static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
de0617e4 5740{
a197f664 5741 struct io_ring_ctx *ctx = req->ctx;
27dc8338 5742 struct io_defer_entry *de;
f67676d1 5743 int ret;
9cf7c104 5744 u32 seq;
de0617e4 5745
9d858b21 5746 /* Still need defer if there is pending req in defer list. */
9cf7c104
PB
5747 if (likely(list_empty_careful(&ctx->defer_list) &&
5748 !(req->flags & REQ_F_IO_DRAIN)))
5749 return 0;
5750
5751 seq = io_get_sequence(req);
5752 /* Still a chance to pass the sequence check */
5753 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
de0617e4
JA
5754 return 0;
5755
e8c2bc1f 5756 if (!req->async_data) {
650b5481 5757 ret = io_req_defer_prep(req, sqe);
327d6d96 5758 if (ret)
650b5481
PB
5759 return ret;
5760 }
cbdcb435 5761 io_prep_async_link(req);
27dc8338
PB
5762 de = kmalloc(sizeof(*de), GFP_KERNEL);
5763 if (!de)
5764 return -ENOMEM;
2d28390a 5765
de0617e4 5766 spin_lock_irq(&ctx->completion_lock);
9cf7c104 5767 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
de0617e4 5768 spin_unlock_irq(&ctx->completion_lock);
27dc8338 5769 kfree(de);
ae34817b
PB
5770 io_queue_async_work(req);
5771 return -EIOCBQUEUED;
de0617e4
JA
5772 }
5773
915967f6 5774 trace_io_uring_defer(ctx, req, req->user_data);
27dc8338 5775 de->req = req;
9cf7c104 5776 de->seq = seq;
27dc8338 5777 list_add_tail(&de->list, &ctx->defer_list);
de0617e4
JA
5778 spin_unlock_irq(&ctx->completion_lock);
5779 return -EIOCBQUEUED;
5780}
5781
f573d384
JA
5782static void io_req_drop_files(struct io_kiocb *req)
5783{
5784 struct io_ring_ctx *ctx = req->ctx;
5785 unsigned long flags;
5786
5787 spin_lock_irqsave(&ctx->inflight_lock, flags);
5788 list_del(&req->inflight_entry);
5789 if (waitqueue_active(&ctx->inflight_wait))
5790 wake_up(&ctx->inflight_wait);
5791 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
5792 req->flags &= ~REQ_F_INFLIGHT;
0f212204 5793 put_files_struct(req->work.files);
9b828492 5794 put_nsproxy(req->work.nsproxy);
f573d384
JA
5795 req->work.files = NULL;
5796}
5797
3ca405eb 5798static void __io_clean_op(struct io_kiocb *req)
99bc4c38 5799{
0e1b6fe3
PB
5800 if (req->flags & REQ_F_BUFFER_SELECTED) {
5801 switch (req->opcode) {
5802 case IORING_OP_READV:
5803 case IORING_OP_READ_FIXED:
5804 case IORING_OP_READ:
bcda7baa 5805 kfree((void *)(unsigned long)req->rw.addr);
0e1b6fe3
PB
5806 break;
5807 case IORING_OP_RECVMSG:
5808 case IORING_OP_RECV:
bcda7baa 5809 kfree(req->sr_msg.kbuf);
0e1b6fe3
PB
5810 break;
5811 }
5812 req->flags &= ~REQ_F_BUFFER_SELECTED;
99bc4c38
PB
5813 }
5814
0e1b6fe3
PB
5815 if (req->flags & REQ_F_NEED_CLEANUP) {
5816 switch (req->opcode) {
5817 case IORING_OP_READV:
5818 case IORING_OP_READ_FIXED:
5819 case IORING_OP_READ:
5820 case IORING_OP_WRITEV:
5821 case IORING_OP_WRITE_FIXED:
e8c2bc1f
JA
5822 case IORING_OP_WRITE: {
5823 struct io_async_rw *io = req->async_data;
5824 if (io->free_iovec)
5825 kfree(io->free_iovec);
0e1b6fe3 5826 break;
e8c2bc1f 5827 }
0e1b6fe3 5828 case IORING_OP_RECVMSG:
e8c2bc1f
JA
5829 case IORING_OP_SENDMSG: {
5830 struct io_async_msghdr *io = req->async_data;
5831 if (io->iov != io->fast_iov)
5832 kfree(io->iov);
0e1b6fe3 5833 break;
e8c2bc1f 5834 }
0e1b6fe3
PB
5835 case IORING_OP_SPLICE:
5836 case IORING_OP_TEE:
5837 io_put_file(req, req->splice.file_in,
5838 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5839 break;
f3cd4850
JA
5840 case IORING_OP_OPENAT:
5841 case IORING_OP_OPENAT2:
5842 if (req->open.filename)
5843 putname(req->open.filename);
5844 break;
0e1b6fe3
PB
5845 }
5846 req->flags &= ~REQ_F_NEED_CLEANUP;
99bc4c38 5847 }
bb175342 5848
f573d384
JA
5849 if (req->flags & REQ_F_INFLIGHT)
5850 io_req_drop_files(req);
99bc4c38
PB
5851}
5852
3529d8c2 5853static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
f13fad7b 5854 bool force_nonblock, struct io_comp_state *cs)
2b188cc1 5855{
a197f664 5856 struct io_ring_ctx *ctx = req->ctx;
d625c6ee 5857 int ret;
2b188cc1 5858
d625c6ee 5859 switch (req->opcode) {
2b188cc1 5860 case IORING_OP_NOP:
229a7b63 5861 ret = io_nop(req, cs);
2b188cc1
JA
5862 break;
5863 case IORING_OP_READV:
edafccee 5864 case IORING_OP_READ_FIXED:
3a6820f2 5865 case IORING_OP_READ:
3529d8c2
JA
5866 if (sqe) {
5867 ret = io_read_prep(req, sqe, force_nonblock);
5868 if (ret < 0)
5869 break;
5870 }
a1d7c393 5871 ret = io_read(req, force_nonblock, cs);
edafccee 5872 break;
3529d8c2 5873 case IORING_OP_WRITEV:
edafccee 5874 case IORING_OP_WRITE_FIXED:
3a6820f2 5875 case IORING_OP_WRITE:
3529d8c2
JA
5876 if (sqe) {
5877 ret = io_write_prep(req, sqe, force_nonblock);
5878 if (ret < 0)
5879 break;
5880 }
a1d7c393 5881 ret = io_write(req, force_nonblock, cs);
2b188cc1 5882 break;
c992fe29 5883 case IORING_OP_FSYNC:
3529d8c2
JA
5884 if (sqe) {
5885 ret = io_prep_fsync(req, sqe);
5886 if (ret < 0)
5887 break;
5888 }
014db007 5889 ret = io_fsync(req, force_nonblock);
c992fe29 5890 break;
221c5eb2 5891 case IORING_OP_POLL_ADD:
3529d8c2
JA
5892 if (sqe) {
5893 ret = io_poll_add_prep(req, sqe);
5894 if (ret)
5895 break;
5896 }
014db007 5897 ret = io_poll_add(req);
221c5eb2
JA
5898 break;
5899 case IORING_OP_POLL_REMOVE:
3529d8c2
JA
5900 if (sqe) {
5901 ret = io_poll_remove_prep(req, sqe);
5902 if (ret < 0)
5903 break;
5904 }
fc4df999 5905 ret = io_poll_remove(req);
221c5eb2 5906 break;
5d17b4a4 5907 case IORING_OP_SYNC_FILE_RANGE:
3529d8c2
JA
5908 if (sqe) {
5909 ret = io_prep_sfr(req, sqe);
5910 if (ret < 0)
5911 break;
5912 }
014db007 5913 ret = io_sync_file_range(req, force_nonblock);
5d17b4a4 5914 break;
0fa03c62 5915 case IORING_OP_SENDMSG:
fddaface 5916 case IORING_OP_SEND:
3529d8c2
JA
5917 if (sqe) {
5918 ret = io_sendmsg_prep(req, sqe);
5919 if (ret < 0)
5920 break;
5921 }
fddaface 5922 if (req->opcode == IORING_OP_SENDMSG)
229a7b63 5923 ret = io_sendmsg(req, force_nonblock, cs);
fddaface 5924 else
229a7b63 5925 ret = io_send(req, force_nonblock, cs);
0fa03c62 5926 break;
aa1fa28f 5927 case IORING_OP_RECVMSG:
fddaface 5928 case IORING_OP_RECV:
3529d8c2
JA
5929 if (sqe) {
5930 ret = io_recvmsg_prep(req, sqe);
5931 if (ret)
5932 break;
5933 }
fddaface 5934 if (req->opcode == IORING_OP_RECVMSG)
229a7b63 5935 ret = io_recvmsg(req, force_nonblock, cs);
fddaface 5936 else
229a7b63 5937 ret = io_recv(req, force_nonblock, cs);
aa1fa28f 5938 break;
5262f567 5939 case IORING_OP_TIMEOUT:
3529d8c2
JA
5940 if (sqe) {
5941 ret = io_timeout_prep(req, sqe, false);
5942 if (ret)
5943 break;
5944 }
fc4df999 5945 ret = io_timeout(req);
5262f567 5946 break;
11365043 5947 case IORING_OP_TIMEOUT_REMOVE:
3529d8c2
JA
5948 if (sqe) {
5949 ret = io_timeout_remove_prep(req, sqe);
5950 if (ret)
5951 break;
5952 }
fc4df999 5953 ret = io_timeout_remove(req);
11365043 5954 break;
17f2fe35 5955 case IORING_OP_ACCEPT:
3529d8c2
JA
5956 if (sqe) {
5957 ret = io_accept_prep(req, sqe);
5958 if (ret)
5959 break;
5960 }
229a7b63 5961 ret = io_accept(req, force_nonblock, cs);
17f2fe35 5962 break;
f8e85cf2 5963 case IORING_OP_CONNECT:
3529d8c2
JA
5964 if (sqe) {
5965 ret = io_connect_prep(req, sqe);
5966 if (ret)
5967 break;
5968 }
229a7b63 5969 ret = io_connect(req, force_nonblock, cs);
f8e85cf2 5970 break;
62755e35 5971 case IORING_OP_ASYNC_CANCEL:
3529d8c2
JA
5972 if (sqe) {
5973 ret = io_async_cancel_prep(req, sqe);
5974 if (ret)
5975 break;
5976 }
014db007 5977 ret = io_async_cancel(req);
62755e35 5978 break;
d63d1b5e
JA
5979 case IORING_OP_FALLOCATE:
5980 if (sqe) {
5981 ret = io_fallocate_prep(req, sqe);
5982 if (ret)
5983 break;
5984 }
014db007 5985 ret = io_fallocate(req, force_nonblock);
d63d1b5e 5986 break;
15b71abe
JA
5987 case IORING_OP_OPENAT:
5988 if (sqe) {
5989 ret = io_openat_prep(req, sqe);
5990 if (ret)
5991 break;
5992 }
014db007 5993 ret = io_openat(req, force_nonblock);
15b71abe 5994 break;
b5dba59e
JA
5995 case IORING_OP_CLOSE:
5996 if (sqe) {
5997 ret = io_close_prep(req, sqe);
5998 if (ret)
5999 break;
6000 }
229a7b63 6001 ret = io_close(req, force_nonblock, cs);
b5dba59e 6002 break;
05f3fb3c
JA
6003 case IORING_OP_FILES_UPDATE:
6004 if (sqe) {
6005 ret = io_files_update_prep(req, sqe);
6006 if (ret)
6007 break;
6008 }
229a7b63 6009 ret = io_files_update(req, force_nonblock, cs);
05f3fb3c 6010 break;
eddc7ef5
JA
6011 case IORING_OP_STATX:
6012 if (sqe) {
6013 ret = io_statx_prep(req, sqe);
6014 if (ret)
6015 break;
6016 }
014db007 6017 ret = io_statx(req, force_nonblock);
eddc7ef5 6018 break;
4840e418
JA
6019 case IORING_OP_FADVISE:
6020 if (sqe) {
6021 ret = io_fadvise_prep(req, sqe);
6022 if (ret)
6023 break;
6024 }
014db007 6025 ret = io_fadvise(req, force_nonblock);
4840e418 6026 break;
c1ca757b
JA
6027 case IORING_OP_MADVISE:
6028 if (sqe) {
6029 ret = io_madvise_prep(req, sqe);
6030 if (ret)
6031 break;
6032 }
014db007 6033 ret = io_madvise(req, force_nonblock);
c1ca757b 6034 break;
cebdb986
JA
6035 case IORING_OP_OPENAT2:
6036 if (sqe) {
6037 ret = io_openat2_prep(req, sqe);
6038 if (ret)
6039 break;
6040 }
014db007 6041 ret = io_openat2(req, force_nonblock);
cebdb986 6042 break;
3e4827b0
JA
6043 case IORING_OP_EPOLL_CTL:
6044 if (sqe) {
6045 ret = io_epoll_ctl_prep(req, sqe);
6046 if (ret)
6047 break;
6048 }
229a7b63 6049 ret = io_epoll_ctl(req, force_nonblock, cs);
3e4827b0 6050 break;
7d67af2c
PB
6051 case IORING_OP_SPLICE:
6052 if (sqe) {
6053 ret = io_splice_prep(req, sqe);
6054 if (ret < 0)
6055 break;
6056 }
014db007 6057 ret = io_splice(req, force_nonblock);
7d67af2c 6058 break;
ddf0322d
JA
6059 case IORING_OP_PROVIDE_BUFFERS:
6060 if (sqe) {
6061 ret = io_provide_buffers_prep(req, sqe);
6062 if (ret)
6063 break;
6064 }
229a7b63 6065 ret = io_provide_buffers(req, force_nonblock, cs);
ddf0322d 6066 break;
067524e9
JA
6067 case IORING_OP_REMOVE_BUFFERS:
6068 if (sqe) {
6069 ret = io_remove_buffers_prep(req, sqe);
6070 if (ret)
6071 break;
6072 }
229a7b63 6073 ret = io_remove_buffers(req, force_nonblock, cs);
3e4827b0 6074 break;
f2a8d5c7
PB
6075 case IORING_OP_TEE:
6076 if (sqe) {
6077 ret = io_tee_prep(req, sqe);
6078 if (ret < 0)
6079 break;
6080 }
6081 ret = io_tee(req, force_nonblock);
6082 break;
2b188cc1
JA
6083 default:
6084 ret = -EINVAL;
6085 break;
6086 }
6087
def596e9
JA
6088 if (ret)
6089 return ret;
6090
b532576e
JA
6091 /* If the op doesn't have a file, we're not polling for it */
6092 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
11ba820b
JA
6093 const bool in_async = io_wq_current_is_worker();
6094
11ba820b
JA
6095 /* workqueue context doesn't hold uring_lock, grab it now */
6096 if (in_async)
6097 mutex_lock(&ctx->uring_lock);
6098
def596e9 6099 io_iopoll_req_issued(req);
11ba820b
JA
6100
6101 if (in_async)
6102 mutex_unlock(&ctx->uring_lock);
def596e9
JA
6103 }
6104
6105 return 0;
2b188cc1
JA
6106}
6107
f4db7182 6108static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work)
2b188cc1
JA
6109{
6110 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
6df1db6b 6111 struct io_kiocb *timeout;
561fb04a 6112 int ret = 0;
2b188cc1 6113
6df1db6b
PB
6114 timeout = io_prep_linked_timeout(req);
6115 if (timeout)
6116 io_queue_linked_timeout(timeout);
d4c81f38 6117
0c9d5ccd
JA
6118 /* if NO_CANCEL is set, we must still run the work */
6119 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
6120 IO_WQ_WORK_CANCEL) {
561fb04a 6121 ret = -ECANCELED;
0c9d5ccd 6122 }
31b51510 6123
561fb04a 6124 if (!ret) {
561fb04a 6125 do {
f13fad7b 6126 ret = io_issue_sqe(req, NULL, false, NULL);
561fb04a
JA
6127 /*
6128 * We can get EAGAIN for polled IO even though we're
6129 * forcing a sync submission from here, since we can't
6130 * wait for request slots on the block side.
6131 */
6132 if (ret != -EAGAIN)
6133 break;
6134 cond_resched();
6135 } while (1);
6136 }
31b51510 6137
561fb04a 6138 if (ret) {
4e88d6e7 6139 req_set_fail_links(req);
e1e16097 6140 io_req_complete(req, ret);
edafccee 6141 }
2b188cc1 6142
f4db7182 6143 return io_steal_work(req);
2b188cc1
JA
6144}
6145
65e19f54
JA
6146static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6147 int index)
6148{
6149 struct fixed_file_table *table;
6150
05f3fb3c 6151 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
84695089 6152 return table->files[index & IORING_FILE_TABLE_MASK];
65e19f54
JA
6153}
6154
8da11c19
PB
6155static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
6156 int fd, struct file **out_file, bool fixed)
09bb8394 6157{
a197f664 6158 struct io_ring_ctx *ctx = req->ctx;
8da11c19 6159 struct file *file;
09bb8394 6160
8da11c19 6161 if (fixed) {
05f3fb3c 6162 if (unlikely(!ctx->file_data ||
09bb8394
JA
6163 (unsigned) fd >= ctx->nr_user_files))
6164 return -EBADF;
b7620121 6165 fd = array_index_nospec(fd, ctx->nr_user_files);
8da11c19 6166 file = io_file_from_index(ctx, fd);
fd2206e4
JA
6167 if (file) {
6168 req->fixed_file_refs = ctx->file_data->cur_refs;
6169 percpu_ref_get(req->fixed_file_refs);
6170 }
09bb8394 6171 } else {
c826bd7a 6172 trace_io_uring_file_get(ctx, fd);
8da11c19 6173 file = __io_file_get(state, fd);
09bb8394
JA
6174 }
6175
fd2206e4
JA
6176 if (file || io_op_defs[req->opcode].needs_file_no_error) {
6177 *out_file = file;
6178 return 0;
6179 }
6180 return -EBADF;
09bb8394
JA
6181}
6182
8da11c19 6183static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
63ff8223 6184 int fd)
8da11c19 6185{
8da11c19
PB
6186 bool fixed;
6187
63ff8223 6188 fixed = (req->flags & REQ_F_FIXED_FILE) != 0;
0cdaf760 6189 if (unlikely(!fixed && io_async_submit(req->ctx)))
8da11c19
PB
6190 return -EBADF;
6191
6192 return io_file_get(state, req, fd, &req->file, fixed);
6193}
6194
a197f664 6195static int io_grab_files(struct io_kiocb *req)
fcb323cc 6196{
a197f664 6197 struct io_ring_ctx *ctx = req->ctx;
fcb323cc 6198
f56040b8
PB
6199 io_req_init_async(req);
6200
5b0bbee4 6201 if (req->work.files || (req->flags & REQ_F_NO_FILE_TABLE))
f86cd20c 6202 return 0;
b5dba59e 6203
0f212204 6204 req->work.files = get_files_struct(current);
9b828492
JA
6205 get_nsproxy(current->nsproxy);
6206 req->work.nsproxy = current->nsproxy;
0f212204
JA
6207 req->flags |= REQ_F_INFLIGHT;
6208
fcb323cc 6209 spin_lock_irq(&ctx->inflight_lock);
0f212204 6210 list_add(&req->inflight_entry, &ctx->inflight_list);
fcb323cc 6211 spin_unlock_irq(&ctx->inflight_lock);
0f212204 6212 return 0;
fcb323cc
JA
6213}
6214
f56040b8
PB
6215static inline int io_prep_work_files(struct io_kiocb *req)
6216{
6217 if (!io_op_defs[req->opcode].file_table)
6218 return 0;
6219 return io_grab_files(req);
6220}
6221
2665abfd 6222static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
2b188cc1 6223{
ad8a48ac
JA
6224 struct io_timeout_data *data = container_of(timer,
6225 struct io_timeout_data, timer);
6226 struct io_kiocb *req = data->req;
2665abfd
JA
6227 struct io_ring_ctx *ctx = req->ctx;
6228 struct io_kiocb *prev = NULL;
6229 unsigned long flags;
2665abfd
JA
6230
6231 spin_lock_irqsave(&ctx->completion_lock, flags);
6232
6233 /*
6234 * We don't expect the list to be empty, that will only happen if we
6235 * race with the completion of the linked work.
6236 */
4493233e
PB
6237 if (!list_empty(&req->link_list)) {
6238 prev = list_entry(req->link_list.prev, struct io_kiocb,
6239 link_list);
5d960724 6240 if (refcount_inc_not_zero(&prev->refs)) {
4493233e 6241 list_del_init(&req->link_list);
5d960724
JA
6242 prev->flags &= ~REQ_F_LINK_TIMEOUT;
6243 } else
76a46e06 6244 prev = NULL;
2665abfd
JA
6245 }
6246
6247 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6248
6249 if (prev) {
4e88d6e7 6250 req_set_fail_links(prev);
014db007 6251 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
76a46e06 6252 io_put_req(prev);
47f46768 6253 } else {
e1e16097 6254 io_req_complete(req, -ETIME);
2665abfd 6255 }
2665abfd
JA
6256 return HRTIMER_NORESTART;
6257}
6258
7271ef3a 6259static void __io_queue_linked_timeout(struct io_kiocb *req)
2665abfd 6260{
76a46e06
JA
6261 /*
6262 * If the list is now empty, then our linked request finished before
6263 * we got a chance to setup the timer
6264 */
4493233e 6265 if (!list_empty(&req->link_list)) {
e8c2bc1f 6266 struct io_timeout_data *data = req->async_data;
94ae5e77 6267
ad8a48ac
JA
6268 data->timer.function = io_link_timeout_fn;
6269 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6270 data->mode);
2665abfd 6271 }
7271ef3a
JA
6272}
6273
6274static void io_queue_linked_timeout(struct io_kiocb *req)
6275{
6276 struct io_ring_ctx *ctx = req->ctx;
6277
6278 spin_lock_irq(&ctx->completion_lock);
6279 __io_queue_linked_timeout(req);
76a46e06 6280 spin_unlock_irq(&ctx->completion_lock);
2665abfd 6281
2665abfd 6282 /* drop submission reference */
76a46e06
JA
6283 io_put_req(req);
6284}
2665abfd 6285
ad8a48ac 6286static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
2665abfd
JA
6287{
6288 struct io_kiocb *nxt;
6289
dea3b49c 6290 if (!(req->flags & REQ_F_LINK_HEAD))
2665abfd 6291 return NULL;
6df1db6b 6292 if (req->flags & REQ_F_LINK_TIMEOUT)
d7718a9d 6293 return NULL;
2665abfd 6294
4493233e
PB
6295 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
6296 link_list);
d625c6ee 6297 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
76a46e06 6298 return NULL;
2665abfd 6299
76a46e06 6300 req->flags |= REQ_F_LINK_TIMEOUT;
76a46e06 6301 return nxt;
2665abfd
JA
6302}
6303
f13fad7b
JA
6304static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6305 struct io_comp_state *cs)
2b188cc1 6306{
4a0a7a18 6307 struct io_kiocb *linked_timeout;
4bc4494e 6308 struct io_kiocb *nxt;
193155c8 6309 const struct cred *old_creds = NULL;
e0c5c576 6310 int ret;
2b188cc1 6311
4a0a7a18
JA
6312again:
6313 linked_timeout = io_prep_linked_timeout(req);
6314
7cdaf587
XW
6315 if ((req->flags & REQ_F_WORK_INITIALIZED) && req->work.creds &&
6316 req->work.creds != current_cred()) {
193155c8
JA
6317 if (old_creds)
6318 revert_creds(old_creds);
6319 if (old_creds == req->work.creds)
6320 old_creds = NULL; /* restored original creds */
6321 else
6322 old_creds = override_creds(req->work.creds);
6323 }
6324
f13fad7b 6325 ret = io_issue_sqe(req, sqe, true, cs);
491381ce
JA
6326
6327 /*
6328 * We async punt it if the file wasn't marked NOWAIT, or if the file
6329 * doesn't support non-blocking read/write attempts
6330 */
24c74678 6331 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
f063c547 6332 if (!io_arm_poll_handler(req)) {
86a761f8 6333punt:
f063c547
PB
6334 ret = io_prep_work_files(req);
6335 if (unlikely(ret))
bbad27b2 6336 goto err;
f063c547
PB
6337 /*
6338 * Queued up for async execution, worker will release
6339 * submit reference when the iocb is actually submitted.
6340 */
6341 io_queue_async_work(req);
2b188cc1 6342 }
bbad27b2 6343
f063c547
PB
6344 if (linked_timeout)
6345 io_queue_linked_timeout(linked_timeout);
4bc4494e 6346 goto exit;
2b188cc1 6347 }
e65ef56d 6348
652532ad 6349 if (unlikely(ret)) {
fcb323cc 6350err:
652532ad
PB
6351 /* un-prep timeout, so it'll be killed as any other linked */
6352 req->flags &= ~REQ_F_LINK_TIMEOUT;
4e88d6e7 6353 req_set_fail_links(req);
e65ef56d 6354 io_put_req(req);
e1e16097 6355 io_req_complete(req, ret);
652532ad 6356 goto exit;
9e645e11 6357 }
652532ad
PB
6358
6359 /* drop submission reference */
6360 nxt = io_put_req_find_next(req);
6361 if (linked_timeout)
6362 io_queue_linked_timeout(linked_timeout);
6363
4a0a7a18
JA
6364 if (nxt) {
6365 req = nxt;
86a761f8
PB
6366
6367 if (req->flags & REQ_F_FORCE_ASYNC)
6368 goto punt;
4a0a7a18
JA
6369 goto again;
6370 }
4bc4494e 6371exit:
193155c8
JA
6372 if (old_creds)
6373 revert_creds(old_creds);
2b188cc1
JA
6374}
6375
f13fad7b
JA
6376static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6377 struct io_comp_state *cs)
4fe2c963
JL
6378{
6379 int ret;
6380
3529d8c2 6381 ret = io_req_defer(req, sqe);
4fe2c963
JL
6382 if (ret) {
6383 if (ret != -EIOCBQUEUED) {
1118591a 6384fail_req:
4e88d6e7 6385 req_set_fail_links(req);
e1e16097
JA
6386 io_put_req(req);
6387 io_req_complete(req, ret);
4fe2c963 6388 }
2550878f 6389 } else if (req->flags & REQ_F_FORCE_ASYNC) {
e8c2bc1f 6390 if (!req->async_data) {
bd2ab18a 6391 ret = io_req_defer_prep(req, sqe);
327d6d96 6392 if (unlikely(ret))
bd2ab18a
PB
6393 goto fail_req;
6394 }
6395
ce35a47a
JA
6396 /*
6397 * Never try inline submit of IOSQE_ASYNC is set, go straight
6398 * to async execution.
6399 */
3e863ea3 6400 io_req_init_async(req);
ce35a47a
JA
6401 req->work.flags |= IO_WQ_WORK_CONCURRENT;
6402 io_queue_async_work(req);
6403 } else {
f13fad7b 6404 __io_queue_sqe(req, sqe, cs);
ce35a47a 6405 }
4fe2c963
JL
6406}
6407
f13fad7b
JA
6408static inline void io_queue_link_head(struct io_kiocb *req,
6409 struct io_comp_state *cs)
4fe2c963 6410{
94ae5e77 6411 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
e1e16097
JA
6412 io_put_req(req);
6413 io_req_complete(req, -ECANCELED);
1b4a51b6 6414 } else
f13fad7b 6415 io_queue_sqe(req, NULL, cs);
4fe2c963
JL
6416}
6417
1d4240cc 6418static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
f13fad7b 6419 struct io_kiocb **link, struct io_comp_state *cs)
9e645e11 6420{
a197f664 6421 struct io_ring_ctx *ctx = req->ctx;
ef4ff581 6422 int ret;
9e645e11 6423
9e645e11
JA
6424 /*
6425 * If we already have a head request, queue this one for async
6426 * submittal once the head completes. If we don't have a head but
6427 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6428 * submitted sync once the chain is complete. If none of those
6429 * conditions are true (normal request), then just queue it.
6430 */
6431 if (*link) {
9d76377f 6432 struct io_kiocb *head = *link;
4e88d6e7 6433
8cdf2193
PB
6434 /*
6435 * Taking sequential execution of a link, draining both sides
6436 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6437 * requests in the link. So, it drains the head and the
6438 * next after the link request. The last one is done via
6439 * drain_next flag to persist the effect across calls.
6440 */
ef4ff581 6441 if (req->flags & REQ_F_IO_DRAIN) {
711be031
PB
6442 head->flags |= REQ_F_IO_DRAIN;
6443 ctx->drain_next = 1;
6444 }
3529d8c2 6445 ret = io_req_defer_prep(req, sqe);
327d6d96 6446 if (unlikely(ret)) {
4e88d6e7 6447 /* fail even hard links since we don't submit */
9d76377f 6448 head->flags |= REQ_F_FAIL_LINK;
1d4240cc 6449 return ret;
2d28390a 6450 }
9d76377f
PB
6451 trace_io_uring_link(ctx, req, head);
6452 list_add_tail(&req->link_list, &head->link_list);
32fe525b
PB
6453
6454 /* last request of a link, enqueue the link */
ef4ff581 6455 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
f13fad7b 6456 io_queue_link_head(head, cs);
32fe525b
PB
6457 *link = NULL;
6458 }
9e645e11 6459 } else {
711be031
PB
6460 if (unlikely(ctx->drain_next)) {
6461 req->flags |= REQ_F_IO_DRAIN;
ef4ff581 6462 ctx->drain_next = 0;
711be031 6463 }
ef4ff581 6464 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
dea3b49c 6465 req->flags |= REQ_F_LINK_HEAD;
711be031 6466 INIT_LIST_HEAD(&req->link_list);
f1d96a8f 6467
711be031 6468 ret = io_req_defer_prep(req, sqe);
327d6d96 6469 if (unlikely(ret))
711be031
PB
6470 req->flags |= REQ_F_FAIL_LINK;
6471 *link = req;
6472 } else {
f13fad7b 6473 io_queue_sqe(req, sqe, cs);
711be031 6474 }
9e645e11 6475 }
2e6e1fde 6476
1d4240cc 6477 return 0;
9e645e11
JA
6478}
6479
9a56a232
JA
6480/*
6481 * Batched submission is done, ensure local IO is flushed out.
6482 */
6483static void io_submit_state_end(struct io_submit_state *state)
6484{
f13fad7b
JA
6485 if (!list_empty(&state->comp.list))
6486 io_submit_flush_completions(&state->comp);
9a56a232 6487 blk_finish_plug(&state->plug);
9f13c35b 6488 io_state_file_put(state);
2579f913 6489 if (state->free_reqs)
6c8a3134 6490 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
9a56a232
JA
6491}
6492
6493/*
6494 * Start submission side cache.
6495 */
6496static void io_submit_state_start(struct io_submit_state *state,
013538bd 6497 struct io_ring_ctx *ctx, unsigned int max_ios)
9a56a232
JA
6498{
6499 blk_start_plug(&state->plug);
013538bd
JA
6500 state->comp.nr = 0;
6501 INIT_LIST_HEAD(&state->comp.list);
6502 state->comp.ctx = ctx;
2579f913 6503 state->free_reqs = 0;
9a56a232
JA
6504 state->file = NULL;
6505 state->ios_left = max_ios;
6506}
6507
2b188cc1
JA
6508static void io_commit_sqring(struct io_ring_ctx *ctx)
6509{
75b28aff 6510 struct io_rings *rings = ctx->rings;
2b188cc1 6511
caf582c6
PB
6512 /*
6513 * Ensure any loads from the SQEs are done at this point,
6514 * since once we write the new head, the application could
6515 * write new data to them.
6516 */
6517 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
2b188cc1
JA
6518}
6519
2b188cc1 6520/*
3529d8c2 6521 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
2b188cc1
JA
6522 * that is mapped by userspace. This means that care needs to be taken to
6523 * ensure that reads are stable, as we cannot rely on userspace always
6524 * being a good citizen. If members of the sqe are validated and then later
6525 * used, it's important that those reads are done through READ_ONCE() to
6526 * prevent a re-load down the line.
6527 */
709b302f 6528static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
2b188cc1 6529{
75b28aff 6530 u32 *sq_array = ctx->sq_array;
2b188cc1
JA
6531 unsigned head;
6532
6533 /*
6534 * The cached sq head (or cq tail) serves two purposes:
6535 *
6536 * 1) allows us to batch the cost of updating the user visible
6537 * head updates.
6538 * 2) allows the kernel side to track the head on its own, even
6539 * though the application is the one updating it.
6540 */
ee7d46d9 6541 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
709b302f
PB
6542 if (likely(head < ctx->sq_entries))
6543 return &ctx->sq_sqes[head];
2b188cc1
JA
6544
6545 /* drop invalid entries */
498ccd9e 6546 ctx->cached_sq_dropped++;
ee7d46d9 6547 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
709b302f
PB
6548 return NULL;
6549}
6550
6551static inline void io_consume_sqe(struct io_ring_ctx *ctx)
6552{
6553 ctx->cached_sq_head++;
2b188cc1
JA
6554}
6555
21b55dbc
SG
6556/*
6557 * Check SQE restrictions (opcode and flags).
6558 *
6559 * Returns 'true' if SQE is allowed, 'false' otherwise.
6560 */
6561static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6562 struct io_kiocb *req,
6563 unsigned int sqe_flags)
6564{
6565 if (!ctx->restricted)
6566 return true;
6567
6568 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6569 return false;
6570
6571 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6572 ctx->restrictions.sqe_flags_required)
6573 return false;
6574
6575 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6576 ctx->restrictions.sqe_flags_required))
6577 return false;
6578
6579 return true;
6580}
6581
ef4ff581
PB
6582#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6583 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6584 IOSQE_BUFFER_SELECT)
6585
6586static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
6587 const struct io_uring_sqe *sqe,
0cdaf760 6588 struct io_submit_state *state)
0553b8bd 6589{
ef4ff581 6590 unsigned int sqe_flags;
63ff8223 6591 int id;
ef4ff581 6592
0553b8bd
PB
6593 req->opcode = READ_ONCE(sqe->opcode);
6594 req->user_data = READ_ONCE(sqe->user_data);
e8c2bc1f 6595 req->async_data = NULL;
0553b8bd
PB
6596 req->file = NULL;
6597 req->ctx = ctx;
6598 req->flags = 0;
6599 /* one is dropped after submission, the other at completion */
6600 refcount_set(&req->refs, 2);
4dd2824d 6601 req->task = current;
e3bc8e9d 6602 get_task_struct(req->task);
0f212204 6603 atomic_long_inc(&req->task->io_uring->req_issue);
0553b8bd 6604 req->result = 0;
ef4ff581
PB
6605
6606 if (unlikely(req->opcode >= IORING_OP_LAST))
6607 return -EINVAL;
6608
9d8426a0
JA
6609 if (unlikely(io_sq_thread_acquire_mm(ctx, req)))
6610 return -EFAULT;
ef4ff581
PB
6611
6612 sqe_flags = READ_ONCE(sqe->flags);
6613 /* enforce forwards compatibility on users */
6614 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6615 return -EINVAL;
6616
21b55dbc
SG
6617 if (unlikely(!io_check_restriction(ctx, req, sqe_flags)))
6618 return -EACCES;
6619
ef4ff581
PB
6620 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6621 !io_op_defs[req->opcode].buffer_select)
6622 return -EOPNOTSUPP;
6623
6624 id = READ_ONCE(sqe->personality);
6625 if (id) {
7cdaf587 6626 io_req_init_async(req);
ef4ff581
PB
6627 req->work.creds = idr_find(&ctx->personality_idr, id);
6628 if (unlikely(!req->work.creds))
6629 return -EINVAL;
6630 get_cred(req->work.creds);
6631 }
6632
6633 /* same numerical values with corresponding REQ_F_*, safe to copy */
c11368a5 6634 req->flags |= sqe_flags;
ef4ff581 6635
63ff8223
JA
6636 if (!io_op_defs[req->opcode].needs_file)
6637 return 0;
6638
6639 return io_req_set_file(state, req, READ_ONCE(sqe->fd));
0553b8bd
PB
6640}
6641
0f212204 6642static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
6c271ce2 6643{
ac8691c4 6644 struct io_submit_state state;
9e645e11 6645 struct io_kiocb *link = NULL;
9e645e11 6646 int i, submitted = 0;
6c271ce2 6647
c4a2ed72 6648 /* if we have a backlog and couldn't flush it all, return BUSY */
ad3eb2c8
JA
6649 if (test_bit(0, &ctx->sq_check_overflow)) {
6650 if (!list_empty(&ctx->cq_overflow_list) &&
e6c8aa9a 6651 !io_cqring_overflow_flush(ctx, false, NULL, NULL))
ad3eb2c8
JA
6652 return -EBUSY;
6653 }
6c271ce2 6654
ee7d46d9
PB
6655 /* make sure SQ entry isn't read before tail */
6656 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
9ef4f124 6657
2b85edfc
PB
6658 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6659 return -EAGAIN;
6c271ce2 6660
013538bd 6661 io_submit_state_start(&state, ctx, nr);
6c271ce2
JA
6662
6663 for (i = 0; i < nr; i++) {
3529d8c2 6664 const struct io_uring_sqe *sqe;
196be95c 6665 struct io_kiocb *req;
1cb1edb2 6666 int err;
fb5ccc98 6667
b1e50e54
PB
6668 sqe = io_get_sqe(ctx);
6669 if (unlikely(!sqe)) {
6670 io_consume_sqe(ctx);
6671 break;
6672 }
ac8691c4 6673 req = io_alloc_req(ctx, &state);
196be95c
PB
6674 if (unlikely(!req)) {
6675 if (!submitted)
6676 submitted = -EAGAIN;
fb5ccc98 6677 break;
196be95c 6678 }
fb5ccc98 6679
ac8691c4 6680 err = io_init_req(ctx, req, sqe, &state);
709b302f 6681 io_consume_sqe(ctx);
d3656344
JA
6682 /* will complete beyond this point, count as submitted */
6683 submitted++;
6684
ef4ff581 6685 if (unlikely(err)) {
1cb1edb2 6686fail_req:
e1e16097
JA
6687 io_put_req(req);
6688 io_req_complete(req, err);
196be95c
PB
6689 break;
6690 }
fb5ccc98 6691
354420f7 6692 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
0cdaf760 6693 true, io_async_submit(ctx));
f13fad7b 6694 err = io_submit_sqe(req, sqe, &link, &state.comp);
1d4240cc
PB
6695 if (err)
6696 goto fail_req;
6c271ce2
JA
6697 }
6698
9466f437
PB
6699 if (unlikely(submitted != nr)) {
6700 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
6701
6702 percpu_ref_put_many(&ctx->refs, nr - ref_used);
6703 }
9e645e11 6704 if (link)
f13fad7b 6705 io_queue_link_head(link, &state.comp);
ac8691c4 6706 io_submit_state_end(&state);
6c271ce2 6707
ae9428ca
PB
6708 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6709 io_commit_sqring(ctx);
6710
6c271ce2
JA
6711 return submitted;
6712}
6713
23b3628e
XW
6714static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6715{
6716 /* Tell userspace we may need a wakeup call */
6717 spin_lock_irq(&ctx->completion_lock);
6718 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6719 spin_unlock_irq(&ctx->completion_lock);
6720}
6721
6722static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6723{
6724 spin_lock_irq(&ctx->completion_lock);
6725 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6726 spin_unlock_irq(&ctx->completion_lock);
6727}
6728
3f0e64d0
JA
6729static int io_sq_wake_function(struct wait_queue_entry *wqe, unsigned mode,
6730 int sync, void *key)
6731{
6732 struct io_ring_ctx *ctx = container_of(wqe, struct io_ring_ctx, sqo_wait_entry);
6733 int ret;
6734
6735 ret = autoremove_wake_function(wqe, mode, sync, key);
6736 if (ret) {
6737 unsigned long flags;
6738
6739 spin_lock_irqsave(&ctx->completion_lock, flags);
6740 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6741 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6742 }
6743 return ret;
6744}
6745
c8d1ba58
JA
6746enum sq_ret {
6747 SQT_IDLE = 1,
6748 SQT_SPIN = 2,
6749 SQT_DID_WORK = 4,
6750};
6751
6752static enum sq_ret __io_sq_thread(struct io_ring_ctx *ctx,
e95eee2d 6753 unsigned long start_jiffies, bool cap_entries)
6c271ce2 6754{
c8d1ba58 6755 unsigned long timeout = start_jiffies + ctx->sq_thread_idle;
534ca6d6 6756 struct io_sq_data *sqd = ctx->sq_data;
c8d1ba58 6757 unsigned int to_submit;
bdcd3eab 6758 int ret = 0;
6c271ce2 6759
c8d1ba58
JA
6760again:
6761 if (!list_empty(&ctx->iopoll_list)) {
6762 unsigned nr_events = 0;
6a779382 6763
c8d1ba58
JA
6764 mutex_lock(&ctx->uring_lock);
6765 if (!list_empty(&ctx->iopoll_list) && !need_resched())
6766 io_do_iopoll(ctx, &nr_events, 0);
6767 mutex_unlock(&ctx->uring_lock);
6768 }
a4c0b3de 6769
c8d1ba58 6770 to_submit = io_sqring_entries(ctx);
6c271ce2 6771
c8d1ba58
JA
6772 /*
6773 * If submit got -EBUSY, flag us as needing the application
6774 * to enter the kernel to reap and flush events.
6775 */
6776 if (!to_submit || ret == -EBUSY || need_resched()) {
6777 /*
6778 * Drop cur_mm before scheduling, we can't hold it for
6779 * long periods (or over schedule()). Do this before
6780 * adding ourselves to the waitqueue, as the unuse/drop
6781 * may sleep.
6782 */
6783 io_sq_thread_drop_mm();
6784
6785 /*
6786 * We're polling. If we're within the defined idle
6787 * period, then let us spin without work before going
6788 * to sleep. The exception is if we got EBUSY doing
6789 * more IO, we should wait for the application to
6790 * reap events and wake us up.
6791 */
6792 if (!list_empty(&ctx->iopoll_list) || need_resched() ||
6793 (!time_after(jiffies, timeout) && ret != -EBUSY &&
6794 !percpu_ref_is_dying(&ctx->refs)))
6795 return SQT_SPIN;
6c271ce2 6796
534ca6d6 6797 prepare_to_wait(&sqd->wait, &ctx->sqo_wait_entry,
c8d1ba58 6798 TASK_INTERRUPTIBLE);
6c271ce2 6799
c8d1ba58
JA
6800 /*
6801 * While doing polled IO, before going to sleep, we need
6802 * to check if there are new reqs added to iopoll_list,
6803 * it is because reqs may have been punted to io worker
6804 * and will be added to iopoll_list later, hence check
6805 * the iopoll_list again.
6806 */
6807 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
6808 !list_empty_careful(&ctx->iopoll_list)) {
534ca6d6 6809 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
c8d1ba58 6810 goto again;
6c271ce2
JA
6811 }
6812
fb5ccc98 6813 to_submit = io_sqring_entries(ctx);
c8d1ba58
JA
6814 if (!to_submit || ret == -EBUSY)
6815 return SQT_IDLE;
6816 }
c1edbf5f 6817
534ca6d6 6818 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
c8d1ba58 6819 io_ring_clear_wakeup_flag(ctx);
7143b5ac 6820
e95eee2d
JA
6821 /* if we're handling multiple rings, cap submit size for fairness */
6822 if (cap_entries && to_submit > 8)
6823 to_submit = 8;
6824
c8d1ba58
JA
6825 mutex_lock(&ctx->uring_lock);
6826 if (likely(!percpu_ref_is_dying(&ctx->refs)))
6827 ret = io_submit_sqes(ctx, to_submit);
6828 mutex_unlock(&ctx->uring_lock);
90554200
JA
6829
6830 if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait))
6831 wake_up(&ctx->sqo_sq_wait);
6832
c8d1ba58
JA
6833 return SQT_DID_WORK;
6834}
6c271ce2 6835
69fb2131
JA
6836static void io_sqd_init_new(struct io_sq_data *sqd)
6837{
6838 struct io_ring_ctx *ctx;
6839
6840 while (!list_empty(&sqd->ctx_new_list)) {
6841 ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list);
6842 init_wait(&ctx->sqo_wait_entry);
6843 ctx->sqo_wait_entry.func = io_sq_wake_function;
6844 list_move_tail(&ctx->sqd_list, &sqd->ctx_list);
6845 complete(&ctx->sq_thread_comp);
6846 }
6847}
6848
c8d1ba58
JA
6849static int io_sq_thread(void *data)
6850{
91d8f519 6851 struct cgroup_subsys_state *cur_css = NULL;
69fb2131
JA
6852 const struct cred *old_cred = NULL;
6853 struct io_sq_data *sqd = data;
6854 struct io_ring_ctx *ctx;
c8d1ba58 6855 unsigned long start_jiffies;
6c271ce2 6856
69fb2131
JA
6857 start_jiffies = jiffies;
6858 while (!kthread_should_stop()) {
6859 enum sq_ret ret = 0;
e95eee2d 6860 bool cap_entries;
6c271ce2 6861
69fb2131
JA
6862 /*
6863 * Any changes to the sqd lists are synchronized through the
6864 * kthread parking. This synchronizes the thread vs users,
6865 * the users are synchronized on the sqd->ctx_lock.
6866 */
6867 if (kthread_should_park())
6868 kthread_parkme();
6c271ce2 6869
69fb2131
JA
6870 if (unlikely(!list_empty(&sqd->ctx_new_list)))
6871 io_sqd_init_new(sqd);
c8d1ba58 6872
e95eee2d
JA
6873 cap_entries = !list_is_singular(&sqd->ctx_list);
6874
69fb2131
JA
6875 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6876 if (current->cred != ctx->creds) {
6877 if (old_cred)
6878 revert_creds(old_cred);
6879 old_cred = override_creds(ctx->creds);
6880 }
91d8f519 6881 io_sq_thread_associate_blkcg(ctx, &cur_css);
c8d1ba58 6882
e95eee2d 6883 ret |= __io_sq_thread(ctx, start_jiffies, cap_entries);
69fb2131
JA
6884
6885 io_sq_thread_drop_mm();
6886 }
6887
6888 if (ret & SQT_SPIN) {
c8d1ba58
JA
6889 io_run_task_work();
6890 cond_resched();
69fb2131
JA
6891 } else if (ret == SQT_IDLE) {
6892 if (kthread_should_park())
6893 continue;
6894 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6895 io_ring_set_wakeup_flag(ctx);
6896 schedule();
6897 start_jiffies = jiffies;
6898 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6899 io_ring_clear_wakeup_flag(ctx);
c8d1ba58 6900 }
6c271ce2
JA
6901 }
6902
4c6e277c 6903 io_run_task_work();
b41e9852 6904
91d8f519
DZ
6905 if (cur_css)
6906 io_sq_thread_unassociate_blkcg();
69fb2131
JA
6907 if (old_cred)
6908 revert_creds(old_cred);
06058632 6909
2bbcd6d3 6910 kthread_parkme();
06058632 6911
6c271ce2
JA
6912 return 0;
6913}
6914
bda52162
JA
6915struct io_wait_queue {
6916 struct wait_queue_entry wq;
6917 struct io_ring_ctx *ctx;
6918 unsigned to_wait;
6919 unsigned nr_timeouts;
6920};
6921
1d7bb1d5 6922static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
bda52162
JA
6923{
6924 struct io_ring_ctx *ctx = iowq->ctx;
6925
6926 /*
d195a66e 6927 * Wake up if we have enough events, or if a timeout occurred since we
bda52162
JA
6928 * started waiting. For timeouts, we always want to return to userspace,
6929 * regardless of event count.
6930 */
1d7bb1d5 6931 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
bda52162
JA
6932 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6933}
6934
6935static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6936 int wake_flags, void *key)
6937{
6938 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6939 wq);
6940
1d7bb1d5
JA
6941 /* use noflush == true, as we can't safely rely on locking context */
6942 if (!io_should_wake(iowq, true))
bda52162
JA
6943 return -1;
6944
6945 return autoremove_wake_function(curr, mode, wake_flags, key);
6946}
6947
af9c1a44
JA
6948static int io_run_task_work_sig(void)
6949{
6950 if (io_run_task_work())
6951 return 1;
6952 if (!signal_pending(current))
6953 return 0;
6954 if (current->jobctl & JOBCTL_TASK_WORK) {
6955 spin_lock_irq(&current->sighand->siglock);
6956 current->jobctl &= ~JOBCTL_TASK_WORK;
6957 recalc_sigpending();
6958 spin_unlock_irq(&current->sighand->siglock);
6959 return 1;
6960 }
6961 return -EINTR;
6962}
6963
2b188cc1
JA
6964/*
6965 * Wait until events become available, if we don't already have some. The
6966 * application must reap them itself, as they reside on the shared cq ring.
6967 */
6968static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
6969 const sigset_t __user *sig, size_t sigsz)
6970{
bda52162
JA
6971 struct io_wait_queue iowq = {
6972 .wq = {
6973 .private = current,
6974 .func = io_wake_function,
6975 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6976 },
6977 .ctx = ctx,
6978 .to_wait = min_events,
6979 };
75b28aff 6980 struct io_rings *rings = ctx->rings;
e9ffa5c2 6981 int ret = 0;
2b188cc1 6982
b41e9852
JA
6983 do {
6984 if (io_cqring_events(ctx, false) >= min_events)
6985 return 0;
4c6e277c 6986 if (!io_run_task_work())
b41e9852 6987 break;
b41e9852 6988 } while (1);
2b188cc1
JA
6989
6990 if (sig) {
9e75ad5d
AB
6991#ifdef CONFIG_COMPAT
6992 if (in_compat_syscall())
6993 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
b772434b 6994 sigsz);
9e75ad5d
AB
6995 else
6996#endif
b772434b 6997 ret = set_user_sigmask(sig, sigsz);
9e75ad5d 6998
2b188cc1
JA
6999 if (ret)
7000 return ret;
7001 }
7002
bda52162 7003 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
c826bd7a 7004 trace_io_uring_cqring_wait(ctx, min_events);
bda52162
JA
7005 do {
7006 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
7007 TASK_INTERRUPTIBLE);
ce593a6c 7008 /* make sure we run task_work before checking for signals */
af9c1a44
JA
7009 ret = io_run_task_work_sig();
7010 if (ret > 0)
4c6e277c 7011 continue;
af9c1a44 7012 else if (ret < 0)
bda52162 7013 break;
ce593a6c
JA
7014 if (io_should_wake(&iowq, false))
7015 break;
7016 schedule();
bda52162
JA
7017 } while (1);
7018 finish_wait(&ctx->wait, &iowq.wq);
7019
b7db41c9 7020 restore_saved_sigmask_unless(ret == -EINTR);
2b188cc1 7021
75b28aff 7022 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
2b188cc1
JA
7023}
7024
6b06314c
JA
7025static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
7026{
7027#if defined(CONFIG_UNIX)
7028 if (ctx->ring_sock) {
7029 struct sock *sock = ctx->ring_sock->sk;
7030 struct sk_buff *skb;
7031
7032 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
7033 kfree_skb(skb);
7034 }
7035#else
7036 int i;
7037
65e19f54
JA
7038 for (i = 0; i < ctx->nr_user_files; i++) {
7039 struct file *file;
7040
7041 file = io_file_from_index(ctx, i);
7042 if (file)
7043 fput(file);
7044 }
6b06314c
JA
7045#endif
7046}
7047
05f3fb3c
JA
7048static void io_file_ref_kill(struct percpu_ref *ref)
7049{
7050 struct fixed_file_data *data;
7051
7052 data = container_of(ref, struct fixed_file_data, refs);
7053 complete(&data->done);
7054}
7055
6b06314c
JA
7056static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
7057{
05f3fb3c 7058 struct fixed_file_data *data = ctx->file_data;
05589553 7059 struct fixed_file_ref_node *ref_node = NULL;
65e19f54
JA
7060 unsigned nr_tables, i;
7061
05f3fb3c 7062 if (!data)
6b06314c
JA
7063 return -ENXIO;
7064
6a4d07cd 7065 spin_lock(&data->lock);
05589553
XW
7066 if (!list_empty(&data->ref_list))
7067 ref_node = list_first_entry(&data->ref_list,
7068 struct fixed_file_ref_node, node);
6a4d07cd 7069 spin_unlock(&data->lock);
05589553
XW
7070 if (ref_node)
7071 percpu_ref_kill(&ref_node->refs);
7072
7073 percpu_ref_kill(&data->refs);
7074
7075 /* wait for all refs nodes to complete */
4a38aed2 7076 flush_delayed_work(&ctx->file_put_work);
2faf852d 7077 wait_for_completion(&data->done);
05f3fb3c 7078
6b06314c 7079 __io_sqe_files_unregister(ctx);
65e19f54
JA
7080 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
7081 for (i = 0; i < nr_tables; i++)
05f3fb3c
JA
7082 kfree(data->table[i].files);
7083 kfree(data->table);
05589553
XW
7084 percpu_ref_exit(&data->refs);
7085 kfree(data);
05f3fb3c 7086 ctx->file_data = NULL;
6b06314c
JA
7087 ctx->nr_user_files = 0;
7088 return 0;
7089}
7090
534ca6d6 7091static void io_put_sq_data(struct io_sq_data *sqd)
6c271ce2 7092{
534ca6d6 7093 if (refcount_dec_and_test(&sqd->refs)) {
2bbcd6d3
RP
7094 /*
7095 * The park is a bit of a work-around, without it we get
7096 * warning spews on shutdown with SQPOLL set and affinity
7097 * set to a single CPU.
7098 */
534ca6d6
JA
7099 if (sqd->thread) {
7100 kthread_park(sqd->thread);
7101 kthread_stop(sqd->thread);
7102 }
7103
7104 kfree(sqd);
7105 }
7106}
7107
aa06165d
JA
7108static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7109{
7110 struct io_ring_ctx *ctx_attach;
7111 struct io_sq_data *sqd;
7112 struct fd f;
7113
7114 f = fdget(p->wq_fd);
7115 if (!f.file)
7116 return ERR_PTR(-ENXIO);
7117 if (f.file->f_op != &io_uring_fops) {
7118 fdput(f);
7119 return ERR_PTR(-EINVAL);
7120 }
7121
7122 ctx_attach = f.file->private_data;
7123 sqd = ctx_attach->sq_data;
7124 if (!sqd) {
7125 fdput(f);
7126 return ERR_PTR(-EINVAL);
7127 }
7128
7129 refcount_inc(&sqd->refs);
7130 fdput(f);
7131 return sqd;
7132}
7133
534ca6d6
JA
7134static struct io_sq_data *io_get_sq_data(struct io_uring_params *p)
7135{
7136 struct io_sq_data *sqd;
7137
aa06165d
JA
7138 if (p->flags & IORING_SETUP_ATTACH_WQ)
7139 return io_attach_sq_data(p);
7140
534ca6d6
JA
7141 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
7142 if (!sqd)
7143 return ERR_PTR(-ENOMEM);
7144
7145 refcount_set(&sqd->refs, 1);
69fb2131
JA
7146 INIT_LIST_HEAD(&sqd->ctx_list);
7147 INIT_LIST_HEAD(&sqd->ctx_new_list);
7148 mutex_init(&sqd->ctx_lock);
7149 mutex_init(&sqd->lock);
534ca6d6
JA
7150 init_waitqueue_head(&sqd->wait);
7151 return sqd;
7152}
7153
69fb2131
JA
7154static void io_sq_thread_unpark(struct io_sq_data *sqd)
7155 __releases(&sqd->lock)
7156{
7157 if (!sqd->thread)
7158 return;
7159 kthread_unpark(sqd->thread);
7160 mutex_unlock(&sqd->lock);
7161}
7162
7163static void io_sq_thread_park(struct io_sq_data *sqd)
7164 __acquires(&sqd->lock)
7165{
7166 if (!sqd->thread)
7167 return;
7168 mutex_lock(&sqd->lock);
7169 kthread_park(sqd->thread);
7170}
7171
534ca6d6
JA
7172static void io_sq_thread_stop(struct io_ring_ctx *ctx)
7173{
7174 struct io_sq_data *sqd = ctx->sq_data;
7175
7176 if (sqd) {
7177 if (sqd->thread) {
7178 /*
7179 * We may arrive here from the error branch in
7180 * io_sq_offload_create() where the kthread is created
7181 * without being waked up, thus wake it up now to make
7182 * sure the wait will complete.
7183 */
7184 wake_up_process(sqd->thread);
7185 wait_for_completion(&ctx->sq_thread_comp);
69fb2131
JA
7186
7187 io_sq_thread_park(sqd);
7188 }
7189
7190 mutex_lock(&sqd->ctx_lock);
7191 list_del(&ctx->sqd_list);
7192 mutex_unlock(&sqd->ctx_lock);
7193
7194 if (sqd->thread) {
7195 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
7196 io_sq_thread_unpark(sqd);
534ca6d6
JA
7197 }
7198
7199 io_put_sq_data(sqd);
7200 ctx->sq_data = NULL;
6c271ce2
JA
7201 }
7202}
7203
6b06314c
JA
7204static void io_finish_async(struct io_ring_ctx *ctx)
7205{
6c271ce2
JA
7206 io_sq_thread_stop(ctx);
7207
561fb04a
JA
7208 if (ctx->io_wq) {
7209 io_wq_destroy(ctx->io_wq);
7210 ctx->io_wq = NULL;
6b06314c
JA
7211 }
7212}
7213
7214#if defined(CONFIG_UNIX)
6b06314c
JA
7215/*
7216 * Ensure the UNIX gc is aware of our file set, so we are certain that
7217 * the io_uring can be safely unregistered on process exit, even if we have
7218 * loops in the file referencing.
7219 */
7220static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7221{
7222 struct sock *sk = ctx->ring_sock->sk;
7223 struct scm_fp_list *fpl;
7224 struct sk_buff *skb;
08a45173 7225 int i, nr_files;
6b06314c 7226
6b06314c
JA
7227 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7228 if (!fpl)
7229 return -ENOMEM;
7230
7231 skb = alloc_skb(0, GFP_KERNEL);
7232 if (!skb) {
7233 kfree(fpl);
7234 return -ENOMEM;
7235 }
7236
7237 skb->sk = sk;
6b06314c 7238
08a45173 7239 nr_files = 0;
6b06314c
JA
7240 fpl->user = get_uid(ctx->user);
7241 for (i = 0; i < nr; i++) {
65e19f54
JA
7242 struct file *file = io_file_from_index(ctx, i + offset);
7243
7244 if (!file)
08a45173 7245 continue;
65e19f54 7246 fpl->fp[nr_files] = get_file(file);
08a45173
JA
7247 unix_inflight(fpl->user, fpl->fp[nr_files]);
7248 nr_files++;
6b06314c
JA
7249 }
7250
08a45173
JA
7251 if (nr_files) {
7252 fpl->max = SCM_MAX_FD;
7253 fpl->count = nr_files;
7254 UNIXCB(skb).fp = fpl;
05f3fb3c 7255 skb->destructor = unix_destruct_scm;
08a45173
JA
7256 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7257 skb_queue_head(&sk->sk_receive_queue, skb);
6b06314c 7258
08a45173
JA
7259 for (i = 0; i < nr_files; i++)
7260 fput(fpl->fp[i]);
7261 } else {
7262 kfree_skb(skb);
7263 kfree(fpl);
7264 }
6b06314c
JA
7265
7266 return 0;
7267}
7268
7269/*
7270 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7271 * causes regular reference counting to break down. We rely on the UNIX
7272 * garbage collection to take care of this problem for us.
7273 */
7274static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7275{
7276 unsigned left, total;
7277 int ret = 0;
7278
7279 total = 0;
7280 left = ctx->nr_user_files;
7281 while (left) {
7282 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
6b06314c
JA
7283
7284 ret = __io_sqe_files_scm(ctx, this_files, total);
7285 if (ret)
7286 break;
7287 left -= this_files;
7288 total += this_files;
7289 }
7290
7291 if (!ret)
7292 return 0;
7293
7294 while (total < ctx->nr_user_files) {
65e19f54
JA
7295 struct file *file = io_file_from_index(ctx, total);
7296
7297 if (file)
7298 fput(file);
6b06314c
JA
7299 total++;
7300 }
7301
7302 return ret;
7303}
7304#else
7305static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7306{
7307 return 0;
7308}
7309#endif
7310
65e19f54
JA
7311static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
7312 unsigned nr_files)
7313{
7314 int i;
7315
7316 for (i = 0; i < nr_tables; i++) {
05f3fb3c 7317 struct fixed_file_table *table = &ctx->file_data->table[i];
65e19f54
JA
7318 unsigned this_files;
7319
7320 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
7321 table->files = kcalloc(this_files, sizeof(struct file *),
7322 GFP_KERNEL);
7323 if (!table->files)
7324 break;
7325 nr_files -= this_files;
7326 }
7327
7328 if (i == nr_tables)
7329 return 0;
7330
7331 for (i = 0; i < nr_tables; i++) {
05f3fb3c 7332 struct fixed_file_table *table = &ctx->file_data->table[i];
65e19f54
JA
7333 kfree(table->files);
7334 }
7335 return 1;
7336}
7337
05f3fb3c
JA
7338static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
7339{
7340#if defined(CONFIG_UNIX)
7341 struct sock *sock = ctx->ring_sock->sk;
7342 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7343 struct sk_buff *skb;
7344 int i;
7345
7346 __skb_queue_head_init(&list);
7347
7348 /*
7349 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7350 * remove this entry and rearrange the file array.
7351 */
7352 skb = skb_dequeue(head);
7353 while (skb) {
7354 struct scm_fp_list *fp;
7355
7356 fp = UNIXCB(skb).fp;
7357 for (i = 0; i < fp->count; i++) {
7358 int left;
7359
7360 if (fp->fp[i] != file)
7361 continue;
7362
7363 unix_notinflight(fp->user, fp->fp[i]);
7364 left = fp->count - 1 - i;
7365 if (left) {
7366 memmove(&fp->fp[i], &fp->fp[i + 1],
7367 left * sizeof(struct file *));
7368 }
7369 fp->count--;
7370 if (!fp->count) {
7371 kfree_skb(skb);
7372 skb = NULL;
7373 } else {
7374 __skb_queue_tail(&list, skb);
7375 }
7376 fput(file);
7377 file = NULL;
7378 break;
7379 }
7380
7381 if (!file)
7382 break;
7383
7384 __skb_queue_tail(&list, skb);
7385
7386 skb = skb_dequeue(head);
7387 }
7388
7389 if (skb_peek(&list)) {
7390 spin_lock_irq(&head->lock);
7391 while ((skb = __skb_dequeue(&list)) != NULL)
7392 __skb_queue_tail(head, skb);
7393 spin_unlock_irq(&head->lock);
7394 }
7395#else
7396 fput(file);
7397#endif
7398}
7399
7400struct io_file_put {
05589553 7401 struct list_head list;
05f3fb3c 7402 struct file *file;
05f3fb3c
JA
7403};
7404
4a38aed2 7405static void __io_file_put_work(struct fixed_file_ref_node *ref_node)
65e19f54 7406{
4a38aed2
JA
7407 struct fixed_file_data *file_data = ref_node->file_data;
7408 struct io_ring_ctx *ctx = file_data->ctx;
05f3fb3c 7409 struct io_file_put *pfile, *tmp;
05589553
XW
7410
7411 list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) {
6a4d07cd 7412 list_del(&pfile->list);
05589553
XW
7413 io_ring_file_put(ctx, pfile->file);
7414 kfree(pfile);
65e19f54 7415 }
05589553 7416
6a4d07cd
JA
7417 spin_lock(&file_data->lock);
7418 list_del(&ref_node->node);
7419 spin_unlock(&file_data->lock);
05589553
XW
7420
7421 percpu_ref_exit(&ref_node->refs);
7422 kfree(ref_node);
7423 percpu_ref_put(&file_data->refs);
2faf852d 7424}
65e19f54 7425
4a38aed2
JA
7426static void io_file_put_work(struct work_struct *work)
7427{
7428 struct io_ring_ctx *ctx;
7429 struct llist_node *node;
7430
7431 ctx = container_of(work, struct io_ring_ctx, file_put_work.work);
7432 node = llist_del_all(&ctx->file_put_llist);
7433
7434 while (node) {
7435 struct fixed_file_ref_node *ref_node;
7436 struct llist_node *next = node->next;
7437
7438 ref_node = llist_entry(node, struct fixed_file_ref_node, llist);
7439 __io_file_put_work(ref_node);
7440 node = next;
7441 }
7442}
7443
05589553 7444static void io_file_data_ref_zero(struct percpu_ref *ref)
2faf852d 7445{
05589553 7446 struct fixed_file_ref_node *ref_node;
4a38aed2
JA
7447 struct io_ring_ctx *ctx;
7448 bool first_add;
7449 int delay = HZ;
65e19f54 7450
05589553 7451 ref_node = container_of(ref, struct fixed_file_ref_node, refs);
4a38aed2 7452 ctx = ref_node->file_data->ctx;
05589553 7453
4a38aed2
JA
7454 if (percpu_ref_is_dying(&ctx->file_data->refs))
7455 delay = 0;
05589553 7456
4a38aed2
JA
7457 first_add = llist_add(&ref_node->llist, &ctx->file_put_llist);
7458 if (!delay)
7459 mod_delayed_work(system_wq, &ctx->file_put_work, 0);
7460 else if (first_add)
7461 queue_delayed_work(system_wq, &ctx->file_put_work, delay);
05f3fb3c 7462}
65e19f54 7463
05589553
XW
7464static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
7465 struct io_ring_ctx *ctx)
05f3fb3c 7466{
05589553 7467 struct fixed_file_ref_node *ref_node;
05f3fb3c 7468
05589553
XW
7469 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7470 if (!ref_node)
7471 return ERR_PTR(-ENOMEM);
05f3fb3c 7472
05589553
XW
7473 if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero,
7474 0, GFP_KERNEL)) {
7475 kfree(ref_node);
7476 return ERR_PTR(-ENOMEM);
7477 }
7478 INIT_LIST_HEAD(&ref_node->node);
7479 INIT_LIST_HEAD(&ref_node->file_list);
05589553
XW
7480 ref_node->file_data = ctx->file_data;
7481 return ref_node;
05589553
XW
7482}
7483
7484static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node)
7485{
7486 percpu_ref_exit(&ref_node->refs);
7487 kfree(ref_node);
65e19f54
JA
7488}
7489
6b06314c
JA
7490static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7491 unsigned nr_args)
7492{
7493 __s32 __user *fds = (__s32 __user *) arg;
65e19f54 7494 unsigned nr_tables;
05f3fb3c 7495 struct file *file;
6b06314c
JA
7496 int fd, ret = 0;
7497 unsigned i;
05589553 7498 struct fixed_file_ref_node *ref_node;
6b06314c 7499
05f3fb3c 7500 if (ctx->file_data)
6b06314c
JA
7501 return -EBUSY;
7502 if (!nr_args)
7503 return -EINVAL;
7504 if (nr_args > IORING_MAX_FIXED_FILES)
7505 return -EMFILE;
7506
05f3fb3c
JA
7507 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
7508 if (!ctx->file_data)
7509 return -ENOMEM;
7510 ctx->file_data->ctx = ctx;
7511 init_completion(&ctx->file_data->done);
05589553 7512 INIT_LIST_HEAD(&ctx->file_data->ref_list);
f7fe9346 7513 spin_lock_init(&ctx->file_data->lock);
05f3fb3c 7514
65e19f54 7515 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
05f3fb3c
JA
7516 ctx->file_data->table = kcalloc(nr_tables,
7517 sizeof(struct fixed_file_table),
65e19f54 7518 GFP_KERNEL);
05f3fb3c
JA
7519 if (!ctx->file_data->table) {
7520 kfree(ctx->file_data);
7521 ctx->file_data = NULL;
6b06314c 7522 return -ENOMEM;
05f3fb3c
JA
7523 }
7524
05589553 7525 if (percpu_ref_init(&ctx->file_data->refs, io_file_ref_kill,
05f3fb3c
JA
7526 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
7527 kfree(ctx->file_data->table);
7528 kfree(ctx->file_data);
7529 ctx->file_data = NULL;
6b06314c 7530 return -ENOMEM;
05f3fb3c 7531 }
6b06314c 7532
65e19f54 7533 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
05f3fb3c
JA
7534 percpu_ref_exit(&ctx->file_data->refs);
7535 kfree(ctx->file_data->table);
7536 kfree(ctx->file_data);
7537 ctx->file_data = NULL;
65e19f54
JA
7538 return -ENOMEM;
7539 }
7540
08a45173 7541 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
65e19f54
JA
7542 struct fixed_file_table *table;
7543 unsigned index;
7544
6b06314c
JA
7545 ret = -EFAULT;
7546 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
7547 break;
08a45173
JA
7548 /* allow sparse sets */
7549 if (fd == -1) {
7550 ret = 0;
7551 continue;
7552 }
6b06314c 7553
05f3fb3c 7554 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
65e19f54 7555 index = i & IORING_FILE_TABLE_MASK;
05f3fb3c 7556 file = fget(fd);
6b06314c
JA
7557
7558 ret = -EBADF;
05f3fb3c 7559 if (!file)
6b06314c 7560 break;
05f3fb3c 7561
6b06314c
JA
7562 /*
7563 * Don't allow io_uring instances to be registered. If UNIX
7564 * isn't enabled, then this causes a reference cycle and this
7565 * instance can never get freed. If UNIX is enabled we'll
7566 * handle it just fine, but there's still no point in allowing
7567 * a ring fd as it doesn't support regular read/write anyway.
7568 */
05f3fb3c
JA
7569 if (file->f_op == &io_uring_fops) {
7570 fput(file);
6b06314c
JA
7571 break;
7572 }
6b06314c 7573 ret = 0;
05f3fb3c 7574 table->files[index] = file;
6b06314c
JA
7575 }
7576
7577 if (ret) {
65e19f54 7578 for (i = 0; i < ctx->nr_user_files; i++) {
65e19f54
JA
7579 file = io_file_from_index(ctx, i);
7580 if (file)
7581 fput(file);
7582 }
7583 for (i = 0; i < nr_tables; i++)
05f3fb3c 7584 kfree(ctx->file_data->table[i].files);
6b06314c 7585
667e57da 7586 percpu_ref_exit(&ctx->file_data->refs);
05f3fb3c
JA
7587 kfree(ctx->file_data->table);
7588 kfree(ctx->file_data);
7589 ctx->file_data = NULL;
6b06314c
JA
7590 ctx->nr_user_files = 0;
7591 return ret;
7592 }
7593
7594 ret = io_sqe_files_scm(ctx);
05589553 7595 if (ret) {
6b06314c 7596 io_sqe_files_unregister(ctx);
05589553
XW
7597 return ret;
7598 }
6b06314c 7599
05589553
XW
7600 ref_node = alloc_fixed_file_ref_node(ctx);
7601 if (IS_ERR(ref_node)) {
7602 io_sqe_files_unregister(ctx);
7603 return PTR_ERR(ref_node);
7604 }
7605
7606 ctx->file_data->cur_refs = &ref_node->refs;
6a4d07cd 7607 spin_lock(&ctx->file_data->lock);
05589553 7608 list_add(&ref_node->node, &ctx->file_data->ref_list);
6a4d07cd 7609 spin_unlock(&ctx->file_data->lock);
05589553 7610 percpu_ref_get(&ctx->file_data->refs);
6b06314c
JA
7611 return ret;
7612}
7613
c3a31e60
JA
7614static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7615 int index)
7616{
7617#if defined(CONFIG_UNIX)
7618 struct sock *sock = ctx->ring_sock->sk;
7619 struct sk_buff_head *head = &sock->sk_receive_queue;
7620 struct sk_buff *skb;
7621
7622 /*
7623 * See if we can merge this file into an existing skb SCM_RIGHTS
7624 * file set. If there's no room, fall back to allocating a new skb
7625 * and filling it in.
7626 */
7627 spin_lock_irq(&head->lock);
7628 skb = skb_peek(head);
7629 if (skb) {
7630 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7631
7632 if (fpl->count < SCM_MAX_FD) {
7633 __skb_unlink(skb, head);
7634 spin_unlock_irq(&head->lock);
7635 fpl->fp[fpl->count] = get_file(file);
7636 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7637 fpl->count++;
7638 spin_lock_irq(&head->lock);
7639 __skb_queue_head(head, skb);
7640 } else {
7641 skb = NULL;
7642 }
7643 }
7644 spin_unlock_irq(&head->lock);
7645
7646 if (skb) {
7647 fput(file);
7648 return 0;
7649 }
7650
7651 return __io_sqe_files_scm(ctx, 1, index);
7652#else
7653 return 0;
7654#endif
7655}
7656
a5318d3c 7657static int io_queue_file_removal(struct fixed_file_data *data,
05589553 7658 struct file *file)
05f3fb3c 7659{
a5318d3c 7660 struct io_file_put *pfile;
05589553
XW
7661 struct percpu_ref *refs = data->cur_refs;
7662 struct fixed_file_ref_node *ref_node;
05f3fb3c 7663
05f3fb3c 7664 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
a5318d3c
HD
7665 if (!pfile)
7666 return -ENOMEM;
05f3fb3c 7667
05589553 7668 ref_node = container_of(refs, struct fixed_file_ref_node, refs);
05f3fb3c 7669 pfile->file = file;
05589553
XW
7670 list_add(&pfile->list, &ref_node->file_list);
7671
a5318d3c 7672 return 0;
05f3fb3c
JA
7673}
7674
7675static int __io_sqe_files_update(struct io_ring_ctx *ctx,
7676 struct io_uring_files_update *up,
7677 unsigned nr_args)
7678{
7679 struct fixed_file_data *data = ctx->file_data;
05589553 7680 struct fixed_file_ref_node *ref_node;
05f3fb3c 7681 struct file *file;
c3a31e60
JA
7682 __s32 __user *fds;
7683 int fd, i, err;
7684 __u32 done;
05589553 7685 bool needs_switch = false;
c3a31e60 7686
05f3fb3c 7687 if (check_add_overflow(up->offset, nr_args, &done))
c3a31e60
JA
7688 return -EOVERFLOW;
7689 if (done > ctx->nr_user_files)
7690 return -EINVAL;
7691
05589553
XW
7692 ref_node = alloc_fixed_file_ref_node(ctx);
7693 if (IS_ERR(ref_node))
7694 return PTR_ERR(ref_node);
7695
c3a31e60 7696 done = 0;
05f3fb3c 7697 fds = u64_to_user_ptr(up->fds);
c3a31e60 7698 while (nr_args) {
65e19f54
JA
7699 struct fixed_file_table *table;
7700 unsigned index;
7701
c3a31e60
JA
7702 err = 0;
7703 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7704 err = -EFAULT;
7705 break;
7706 }
05f3fb3c
JA
7707 i = array_index_nospec(up->offset, ctx->nr_user_files);
7708 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
65e19f54
JA
7709 index = i & IORING_FILE_TABLE_MASK;
7710 if (table->files[index]) {
98dfd502 7711 file = table->files[index];
a5318d3c
HD
7712 err = io_queue_file_removal(data, file);
7713 if (err)
7714 break;
65e19f54 7715 table->files[index] = NULL;
05589553 7716 needs_switch = true;
c3a31e60
JA
7717 }
7718 if (fd != -1) {
c3a31e60
JA
7719 file = fget(fd);
7720 if (!file) {
7721 err = -EBADF;
7722 break;
7723 }
7724 /*
7725 * Don't allow io_uring instances to be registered. If
7726 * UNIX isn't enabled, then this causes a reference
7727 * cycle and this instance can never get freed. If UNIX
7728 * is enabled we'll handle it just fine, but there's
7729 * still no point in allowing a ring fd as it doesn't
7730 * support regular read/write anyway.
7731 */
7732 if (file->f_op == &io_uring_fops) {
7733 fput(file);
7734 err = -EBADF;
7735 break;
7736 }
65e19f54 7737 table->files[index] = file;
c3a31e60 7738 err = io_sqe_file_register(ctx, file, i);
f3bd9dae 7739 if (err) {
95d1c8e5 7740 table->files[index] = NULL;
f3bd9dae 7741 fput(file);
c3a31e60 7742 break;
f3bd9dae 7743 }
c3a31e60
JA
7744 }
7745 nr_args--;
7746 done++;
05f3fb3c
JA
7747 up->offset++;
7748 }
7749
05589553
XW
7750 if (needs_switch) {
7751 percpu_ref_kill(data->cur_refs);
6a4d07cd 7752 spin_lock(&data->lock);
05589553
XW
7753 list_add(&ref_node->node, &data->ref_list);
7754 data->cur_refs = &ref_node->refs;
6a4d07cd 7755 spin_unlock(&data->lock);
05589553
XW
7756 percpu_ref_get(&ctx->file_data->refs);
7757 } else
7758 destroy_fixed_file_ref_node(ref_node);
c3a31e60
JA
7759
7760 return done ? done : err;
7761}
05589553 7762
05f3fb3c
JA
7763static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
7764 unsigned nr_args)
7765{
7766 struct io_uring_files_update up;
7767
7768 if (!ctx->file_data)
7769 return -ENXIO;
7770 if (!nr_args)
7771 return -EINVAL;
7772 if (copy_from_user(&up, arg, sizeof(up)))
7773 return -EFAULT;
7774 if (up.resv)
7775 return -EINVAL;
7776
7777 return __io_sqe_files_update(ctx, &up, nr_args);
7778}
c3a31e60 7779
e9fd9396 7780static void io_free_work(struct io_wq_work *work)
7d723065
JA
7781{
7782 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7783
e9fd9396 7784 /* Consider that io_steal_work() relies on this ref */
7d723065
JA
7785 io_put_req(req);
7786}
7787
24369c2e
PB
7788static int io_init_wq_offload(struct io_ring_ctx *ctx,
7789 struct io_uring_params *p)
7790{
7791 struct io_wq_data data;
7792 struct fd f;
7793 struct io_ring_ctx *ctx_attach;
7794 unsigned int concurrency;
7795 int ret = 0;
7796
7797 data.user = ctx->user;
e9fd9396 7798 data.free_work = io_free_work;
f5fa38c5 7799 data.do_work = io_wq_submit_work;
24369c2e
PB
7800
7801 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
7802 /* Do QD, or 4 * CPUS, whatever is smallest */
7803 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
7804
7805 ctx->io_wq = io_wq_create(concurrency, &data);
7806 if (IS_ERR(ctx->io_wq)) {
7807 ret = PTR_ERR(ctx->io_wq);
7808 ctx->io_wq = NULL;
7809 }
7810 return ret;
7811 }
7812
7813 f = fdget(p->wq_fd);
7814 if (!f.file)
7815 return -EBADF;
7816
7817 if (f.file->f_op != &io_uring_fops) {
7818 ret = -EINVAL;
7819 goto out_fput;
7820 }
7821
7822 ctx_attach = f.file->private_data;
7823 /* @io_wq is protected by holding the fd */
7824 if (!io_wq_get(ctx_attach->io_wq, &data)) {
7825 ret = -EINVAL;
7826 goto out_fput;
7827 }
7828
7829 ctx->io_wq = ctx_attach->io_wq;
7830out_fput:
7831 fdput(f);
7832 return ret;
7833}
7834
0f212204
JA
7835static int io_uring_alloc_task_context(struct task_struct *task)
7836{
7837 struct io_uring_task *tctx;
7838
7839 tctx = kmalloc(sizeof(*tctx), GFP_KERNEL);
7840 if (unlikely(!tctx))
7841 return -ENOMEM;
7842
7843 xa_init(&tctx->xa);
7844 init_waitqueue_head(&tctx->wait);
7845 tctx->last = NULL;
7846 tctx->in_idle = 0;
7847 atomic_long_set(&tctx->req_issue, 0);
7848 atomic_long_set(&tctx->req_complete, 0);
7849 task->io_uring = tctx;
7850 return 0;
7851}
7852
7853void __io_uring_free(struct task_struct *tsk)
7854{
7855 struct io_uring_task *tctx = tsk->io_uring;
7856
7857 WARN_ON_ONCE(!xa_empty(&tctx->xa));
7858 xa_destroy(&tctx->xa);
7859 kfree(tctx);
7860 tsk->io_uring = NULL;
7861}
7862
7e84e1c7
SG
7863static int io_sq_offload_create(struct io_ring_ctx *ctx,
7864 struct io_uring_params *p)
2b188cc1
JA
7865{
7866 int ret;
7867
6c271ce2 7868 if (ctx->flags & IORING_SETUP_SQPOLL) {
534ca6d6
JA
7869 struct io_sq_data *sqd;
7870
3ec482d1
JA
7871 ret = -EPERM;
7872 if (!capable(CAP_SYS_ADMIN))
7873 goto err;
7874
534ca6d6
JA
7875 sqd = io_get_sq_data(p);
7876 if (IS_ERR(sqd)) {
7877 ret = PTR_ERR(sqd);
7878 goto err;
7879 }
69fb2131 7880
534ca6d6 7881 ctx->sq_data = sqd;
69fb2131
JA
7882 io_sq_thread_park(sqd);
7883 mutex_lock(&sqd->ctx_lock);
7884 list_add(&ctx->sqd_list, &sqd->ctx_new_list);
7885 mutex_unlock(&sqd->ctx_lock);
7886 io_sq_thread_unpark(sqd);
534ca6d6 7887
917257da
JA
7888 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
7889 if (!ctx->sq_thread_idle)
7890 ctx->sq_thread_idle = HZ;
7891
aa06165d
JA
7892 if (sqd->thread)
7893 goto done;
7894
6c271ce2 7895 if (p->flags & IORING_SETUP_SQ_AFF) {
44a9bd18 7896 int cpu = p->sq_thread_cpu;
6c271ce2 7897
917257da 7898 ret = -EINVAL;
44a9bd18
JA
7899 if (cpu >= nr_cpu_ids)
7900 goto err;
7889f44d 7901 if (!cpu_online(cpu))
917257da
JA
7902 goto err;
7903
69fb2131 7904 sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd,
534ca6d6 7905 cpu, "io_uring-sq");
6c271ce2 7906 } else {
69fb2131 7907 sqd->thread = kthread_create(io_sq_thread, sqd,
6c271ce2
JA
7908 "io_uring-sq");
7909 }
534ca6d6
JA
7910 if (IS_ERR(sqd->thread)) {
7911 ret = PTR_ERR(sqd->thread);
7912 sqd->thread = NULL;
6c271ce2
JA
7913 goto err;
7914 }
534ca6d6 7915 ret = io_uring_alloc_task_context(sqd->thread);
0f212204
JA
7916 if (ret)
7917 goto err;
6c271ce2
JA
7918 } else if (p->flags & IORING_SETUP_SQ_AFF) {
7919 /* Can't have SQ_AFF without SQPOLL */
7920 ret = -EINVAL;
7921 goto err;
7922 }
7923
aa06165d 7924done:
24369c2e
PB
7925 ret = io_init_wq_offload(ctx, p);
7926 if (ret)
2b188cc1 7927 goto err;
2b188cc1
JA
7928
7929 return 0;
7930err:
54a91f3b 7931 io_finish_async(ctx);
2b188cc1
JA
7932 return ret;
7933}
7934
7e84e1c7
SG
7935static void io_sq_offload_start(struct io_ring_ctx *ctx)
7936{
534ca6d6
JA
7937 struct io_sq_data *sqd = ctx->sq_data;
7938
7939 if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread)
7940 wake_up_process(sqd->thread);
7e84e1c7
SG
7941}
7942
a087e2b5
BM
7943static inline void __io_unaccount_mem(struct user_struct *user,
7944 unsigned long nr_pages)
2b188cc1
JA
7945{
7946 atomic_long_sub(nr_pages, &user->locked_vm);
7947}
7948
a087e2b5
BM
7949static inline int __io_account_mem(struct user_struct *user,
7950 unsigned long nr_pages)
2b188cc1
JA
7951{
7952 unsigned long page_limit, cur_pages, new_pages;
7953
7954 /* Don't allow more pages than we can safely lock */
7955 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
7956
7957 do {
7958 cur_pages = atomic_long_read(&user->locked_vm);
7959 new_pages = cur_pages + nr_pages;
7960 if (new_pages > page_limit)
7961 return -ENOMEM;
7962 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
7963 new_pages) != cur_pages);
7964
7965 return 0;
7966}
7967
2e0464d4
BM
7968static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7969 enum io_mem_account acct)
a087e2b5 7970{
aad5d8da 7971 if (ctx->limit_mem)
a087e2b5 7972 __io_unaccount_mem(ctx->user, nr_pages);
30975825 7973
2aede0e4 7974 if (ctx->mm_account) {
2e0464d4 7975 if (acct == ACCT_LOCKED)
2aede0e4 7976 ctx->mm_account->locked_vm -= nr_pages;
2e0464d4 7977 else if (acct == ACCT_PINNED)
2aede0e4 7978 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
2e0464d4 7979 }
a087e2b5
BM
7980}
7981
2e0464d4
BM
7982static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7983 enum io_mem_account acct)
a087e2b5 7984{
30975825
BM
7985 int ret;
7986
7987 if (ctx->limit_mem) {
7988 ret = __io_account_mem(ctx->user, nr_pages);
7989 if (ret)
7990 return ret;
7991 }
7992
2aede0e4 7993 if (ctx->mm_account) {
2e0464d4 7994 if (acct == ACCT_LOCKED)
2aede0e4 7995 ctx->mm_account->locked_vm += nr_pages;
2e0464d4 7996 else if (acct == ACCT_PINNED)
2aede0e4 7997 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
2e0464d4 7998 }
a087e2b5
BM
7999
8000 return 0;
8001}
8002
2b188cc1
JA
8003static void io_mem_free(void *ptr)
8004{
52e04ef4
MR
8005 struct page *page;
8006
8007 if (!ptr)
8008 return;
2b188cc1 8009
52e04ef4 8010 page = virt_to_head_page(ptr);
2b188cc1
JA
8011 if (put_page_testzero(page))
8012 free_compound_page(page);
8013}
8014
8015static void *io_mem_alloc(size_t size)
8016{
8017 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
8018 __GFP_NORETRY;
8019
8020 return (void *) __get_free_pages(gfp_flags, get_order(size));
8021}
8022
75b28aff
HV
8023static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
8024 size_t *sq_offset)
8025{
8026 struct io_rings *rings;
8027 size_t off, sq_array_size;
8028
8029 off = struct_size(rings, cqes, cq_entries);
8030 if (off == SIZE_MAX)
8031 return SIZE_MAX;
8032
8033#ifdef CONFIG_SMP
8034 off = ALIGN(off, SMP_CACHE_BYTES);
8035 if (off == 0)
8036 return SIZE_MAX;
8037#endif
8038
b36200f5
DV
8039 if (sq_offset)
8040 *sq_offset = off;
8041
75b28aff
HV
8042 sq_array_size = array_size(sizeof(u32), sq_entries);
8043 if (sq_array_size == SIZE_MAX)
8044 return SIZE_MAX;
8045
8046 if (check_add_overflow(off, sq_array_size, &off))
8047 return SIZE_MAX;
8048
75b28aff
HV
8049 return off;
8050}
8051
2b188cc1
JA
8052static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
8053{
75b28aff 8054 size_t pages;
2b188cc1 8055
75b28aff
HV
8056 pages = (size_t)1 << get_order(
8057 rings_size(sq_entries, cq_entries, NULL));
8058 pages += (size_t)1 << get_order(
8059 array_size(sizeof(struct io_uring_sqe), sq_entries));
2b188cc1 8060
75b28aff 8061 return pages;
2b188cc1
JA
8062}
8063
edafccee
JA
8064static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
8065{
8066 int i, j;
8067
8068 if (!ctx->user_bufs)
8069 return -ENXIO;
8070
8071 for (i = 0; i < ctx->nr_user_bufs; i++) {
8072 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8073
8074 for (j = 0; j < imu->nr_bvecs; j++)
f1f6a7dd 8075 unpin_user_page(imu->bvec[j].bv_page);
edafccee 8076
de293938
JA
8077 if (imu->acct_pages)
8078 io_unaccount_mem(ctx, imu->acct_pages, ACCT_PINNED);
d4ef6475 8079 kvfree(imu->bvec);
edafccee
JA
8080 imu->nr_bvecs = 0;
8081 }
8082
8083 kfree(ctx->user_bufs);
8084 ctx->user_bufs = NULL;
8085 ctx->nr_user_bufs = 0;
8086 return 0;
8087}
8088
8089static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
8090 void __user *arg, unsigned index)
8091{
8092 struct iovec __user *src;
8093
8094#ifdef CONFIG_COMPAT
8095 if (ctx->compat) {
8096 struct compat_iovec __user *ciovs;
8097 struct compat_iovec ciov;
8098
8099 ciovs = (struct compat_iovec __user *) arg;
8100 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8101 return -EFAULT;
8102
d55e5f5b 8103 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
edafccee
JA
8104 dst->iov_len = ciov.iov_len;
8105 return 0;
8106 }
8107#endif
8108 src = (struct iovec __user *) arg;
8109 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8110 return -EFAULT;
8111 return 0;
8112}
8113
de293938
JA
8114/*
8115 * Not super efficient, but this is just a registration time. And we do cache
8116 * the last compound head, so generally we'll only do a full search if we don't
8117 * match that one.
8118 *
8119 * We check if the given compound head page has already been accounted, to
8120 * avoid double accounting it. This allows us to account the full size of the
8121 * page, not just the constituent pages of a huge page.
8122 */
8123static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
8124 int nr_pages, struct page *hpage)
8125{
8126 int i, j;
8127
8128 /* check current page array */
8129 for (i = 0; i < nr_pages; i++) {
8130 if (!PageCompound(pages[i]))
8131 continue;
8132 if (compound_head(pages[i]) == hpage)
8133 return true;
8134 }
8135
8136 /* check previously registered pages */
8137 for (i = 0; i < ctx->nr_user_bufs; i++) {
8138 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8139
8140 for (j = 0; j < imu->nr_bvecs; j++) {
8141 if (!PageCompound(imu->bvec[j].bv_page))
8142 continue;
8143 if (compound_head(imu->bvec[j].bv_page) == hpage)
8144 return true;
8145 }
8146 }
8147
8148 return false;
8149}
8150
8151static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
8152 int nr_pages, struct io_mapped_ubuf *imu,
8153 struct page **last_hpage)
8154{
8155 int i, ret;
8156
8157 for (i = 0; i < nr_pages; i++) {
8158 if (!PageCompound(pages[i])) {
8159 imu->acct_pages++;
8160 } else {
8161 struct page *hpage;
8162
8163 hpage = compound_head(pages[i]);
8164 if (hpage == *last_hpage)
8165 continue;
8166 *last_hpage = hpage;
8167 if (headpage_already_acct(ctx, pages, i, hpage))
8168 continue;
8169 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
8170 }
8171 }
8172
8173 if (!imu->acct_pages)
8174 return 0;
8175
8176 ret = io_account_mem(ctx, imu->acct_pages, ACCT_PINNED);
8177 if (ret)
8178 imu->acct_pages = 0;
8179 return ret;
8180}
8181
edafccee
JA
8182static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
8183 unsigned nr_args)
8184{
8185 struct vm_area_struct **vmas = NULL;
8186 struct page **pages = NULL;
de293938 8187 struct page *last_hpage = NULL;
edafccee
JA
8188 int i, j, got_pages = 0;
8189 int ret = -EINVAL;
8190
8191 if (ctx->user_bufs)
8192 return -EBUSY;
8193 if (!nr_args || nr_args > UIO_MAXIOV)
8194 return -EINVAL;
8195
8196 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
8197 GFP_KERNEL);
8198 if (!ctx->user_bufs)
8199 return -ENOMEM;
8200
8201 for (i = 0; i < nr_args; i++) {
8202 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8203 unsigned long off, start, end, ubuf;
8204 int pret, nr_pages;
8205 struct iovec iov;
8206 size_t size;
8207
8208 ret = io_copy_iov(ctx, &iov, arg, i);
8209 if (ret)
a278682d 8210 goto err;
edafccee
JA
8211
8212 /*
8213 * Don't impose further limits on the size and buffer
8214 * constraints here, we'll -EINVAL later when IO is
8215 * submitted if they are wrong.
8216 */
8217 ret = -EFAULT;
8218 if (!iov.iov_base || !iov.iov_len)
8219 goto err;
8220
8221 /* arbitrary limit, but we need something */
8222 if (iov.iov_len > SZ_1G)
8223 goto err;
8224
8225 ubuf = (unsigned long) iov.iov_base;
8226 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8227 start = ubuf >> PAGE_SHIFT;
8228 nr_pages = end - start;
8229
edafccee
JA
8230 ret = 0;
8231 if (!pages || nr_pages > got_pages) {
a8c73c1a
DE
8232 kvfree(vmas);
8233 kvfree(pages);
d4ef6475 8234 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
edafccee 8235 GFP_KERNEL);
d4ef6475 8236 vmas = kvmalloc_array(nr_pages,
edafccee
JA
8237 sizeof(struct vm_area_struct *),
8238 GFP_KERNEL);
8239 if (!pages || !vmas) {
8240 ret = -ENOMEM;
edafccee
JA
8241 goto err;
8242 }
8243 got_pages = nr_pages;
8244 }
8245
d4ef6475 8246 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
edafccee
JA
8247 GFP_KERNEL);
8248 ret = -ENOMEM;
de293938 8249 if (!imu->bvec)
edafccee 8250 goto err;
edafccee
JA
8251
8252 ret = 0;
d8ed45c5 8253 mmap_read_lock(current->mm);
2113b05d 8254 pret = pin_user_pages(ubuf, nr_pages,
932f4a63
IW
8255 FOLL_WRITE | FOLL_LONGTERM,
8256 pages, vmas);
edafccee
JA
8257 if (pret == nr_pages) {
8258 /* don't support file backed memory */
8259 for (j = 0; j < nr_pages; j++) {
8260 struct vm_area_struct *vma = vmas[j];
8261
8262 if (vma->vm_file &&
8263 !is_file_hugepages(vma->vm_file)) {
8264 ret = -EOPNOTSUPP;
8265 break;
8266 }
8267 }
8268 } else {
8269 ret = pret < 0 ? pret : -EFAULT;
8270 }
d8ed45c5 8271 mmap_read_unlock(current->mm);
edafccee
JA
8272 if (ret) {
8273 /*
8274 * if we did partial map, or found file backed vmas,
8275 * release any pages we did get
8276 */
27c4d3a3 8277 if (pret > 0)
f1f6a7dd 8278 unpin_user_pages(pages, pret);
de293938
JA
8279 kvfree(imu->bvec);
8280 goto err;
8281 }
8282
8283 ret = io_buffer_account_pin(ctx, pages, pret, imu, &last_hpage);
8284 if (ret) {
8285 unpin_user_pages(pages, pret);
d4ef6475 8286 kvfree(imu->bvec);
edafccee
JA
8287 goto err;
8288 }
8289
8290 off = ubuf & ~PAGE_MASK;
8291 size = iov.iov_len;
8292 for (j = 0; j < nr_pages; j++) {
8293 size_t vec_len;
8294
8295 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8296 imu->bvec[j].bv_page = pages[j];
8297 imu->bvec[j].bv_len = vec_len;
8298 imu->bvec[j].bv_offset = off;
8299 off = 0;
8300 size -= vec_len;
8301 }
8302 /* store original address for later verification */
8303 imu->ubuf = ubuf;
8304 imu->len = iov.iov_len;
8305 imu->nr_bvecs = nr_pages;
8306
8307 ctx->nr_user_bufs++;
8308 }
d4ef6475
MR
8309 kvfree(pages);
8310 kvfree(vmas);
edafccee
JA
8311 return 0;
8312err:
d4ef6475
MR
8313 kvfree(pages);
8314 kvfree(vmas);
edafccee
JA
8315 io_sqe_buffer_unregister(ctx);
8316 return ret;
8317}
8318
9b402849
JA
8319static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
8320{
8321 __s32 __user *fds = arg;
8322 int fd;
8323
8324 if (ctx->cq_ev_fd)
8325 return -EBUSY;
8326
8327 if (copy_from_user(&fd, fds, sizeof(*fds)))
8328 return -EFAULT;
8329
8330 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
8331 if (IS_ERR(ctx->cq_ev_fd)) {
8332 int ret = PTR_ERR(ctx->cq_ev_fd);
8333 ctx->cq_ev_fd = NULL;
8334 return ret;
8335 }
8336
8337 return 0;
8338}
8339
8340static int io_eventfd_unregister(struct io_ring_ctx *ctx)
8341{
8342 if (ctx->cq_ev_fd) {
8343 eventfd_ctx_put(ctx->cq_ev_fd);
8344 ctx->cq_ev_fd = NULL;
8345 return 0;
8346 }
8347
8348 return -ENXIO;
8349}
8350
5a2e745d
JA
8351static int __io_destroy_buffers(int id, void *p, void *data)
8352{
8353 struct io_ring_ctx *ctx = data;
8354 struct io_buffer *buf = p;
8355
067524e9 8356 __io_remove_buffers(ctx, buf, id, -1U);
5a2e745d
JA
8357 return 0;
8358}
8359
8360static void io_destroy_buffers(struct io_ring_ctx *ctx)
8361{
8362 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
8363 idr_destroy(&ctx->io_buffer_idr);
8364}
8365
2b188cc1
JA
8366static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8367{
6b06314c 8368 io_finish_async(ctx);
5dbcad51 8369 io_sqe_buffer_unregister(ctx);
2aede0e4
JA
8370
8371 if (ctx->sqo_task) {
8372 put_task_struct(ctx->sqo_task);
8373 ctx->sqo_task = NULL;
8374 mmdrop(ctx->mm_account);
8375 ctx->mm_account = NULL;
30975825 8376 }
def596e9 8377
91d8f519
DZ
8378#ifdef CONFIG_BLK_CGROUP
8379 if (ctx->sqo_blkcg_css)
8380 css_put(ctx->sqo_blkcg_css);
8381#endif
8382
6b06314c 8383 io_sqe_files_unregister(ctx);
9b402849 8384 io_eventfd_unregister(ctx);
5a2e745d 8385 io_destroy_buffers(ctx);
41726c9a 8386 idr_destroy(&ctx->personality_idr);
def596e9 8387
2b188cc1 8388#if defined(CONFIG_UNIX)
355e8d26
EB
8389 if (ctx->ring_sock) {
8390 ctx->ring_sock->file = NULL; /* so that iput() is called */
2b188cc1 8391 sock_release(ctx->ring_sock);
355e8d26 8392 }
2b188cc1
JA
8393#endif
8394
75b28aff 8395 io_mem_free(ctx->rings);
2b188cc1 8396 io_mem_free(ctx->sq_sqes);
2b188cc1
JA
8397
8398 percpu_ref_exit(&ctx->refs);
2b188cc1 8399 free_uid(ctx->user);
181e448d 8400 put_cred(ctx->creds);
78076bb6 8401 kfree(ctx->cancel_hash);
0ddf92e8 8402 kmem_cache_free(req_cachep, ctx->fallback_req);
2b188cc1
JA
8403 kfree(ctx);
8404}
8405
8406static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8407{
8408 struct io_ring_ctx *ctx = file->private_data;
8409 __poll_t mask = 0;
8410
8411 poll_wait(file, &ctx->cq_wait, wait);
4f7067c3
SB
8412 /*
8413 * synchronizes with barrier from wq_has_sleeper call in
8414 * io_commit_cqring
8415 */
2b188cc1 8416 smp_rmb();
90554200 8417 if (!io_sqring_full(ctx))
2b188cc1 8418 mask |= EPOLLOUT | EPOLLWRNORM;
63e5d81f 8419 if (io_cqring_events(ctx, false))
2b188cc1
JA
8420 mask |= EPOLLIN | EPOLLRDNORM;
8421
8422 return mask;
8423}
8424
8425static int io_uring_fasync(int fd, struct file *file, int on)
8426{
8427 struct io_ring_ctx *ctx = file->private_data;
8428
8429 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8430}
8431
071698e1
JA
8432static int io_remove_personalities(int id, void *p, void *data)
8433{
8434 struct io_ring_ctx *ctx = data;
8435 const struct cred *cred;
8436
8437 cred = idr_remove(&ctx->personality_idr, id);
8438 if (cred)
8439 put_cred(cred);
8440 return 0;
8441}
8442
85faa7b8
JA
8443static void io_ring_exit_work(struct work_struct *work)
8444{
b2edc0a7
PB
8445 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
8446 exit_work);
85faa7b8 8447
56952e91
JA
8448 /*
8449 * If we're doing polled IO and end up having requests being
8450 * submitted async (out-of-line), then completions can come in while
8451 * we're waiting for refs to drop. We need to reap these manually,
8452 * as nobody else will be looking for them.
8453 */
b2edc0a7 8454 do {
56952e91 8455 if (ctx->rings)
e6c8aa9a 8456 io_cqring_overflow_flush(ctx, true, NULL, NULL);
b2edc0a7
PB
8457 io_iopoll_try_reap_events(ctx);
8458 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
85faa7b8
JA
8459 io_ring_ctx_free(ctx);
8460}
8461
2b188cc1
JA
8462static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8463{
8464 mutex_lock(&ctx->uring_lock);
8465 percpu_ref_kill(&ctx->refs);
8466 mutex_unlock(&ctx->uring_lock);
8467
f3606e3a
JA
8468 io_kill_timeouts(ctx, NULL);
8469 io_poll_remove_all(ctx, NULL);
561fb04a
JA
8470
8471 if (ctx->io_wq)
8472 io_wq_cancel_all(ctx->io_wq);
8473
15dff286
JA
8474 /* if we failed setting up the ctx, we might not have any rings */
8475 if (ctx->rings)
e6c8aa9a 8476 io_cqring_overflow_flush(ctx, true, NULL, NULL);
b2edc0a7 8477 io_iopoll_try_reap_events(ctx);
071698e1 8478 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
309fc03a
JA
8479
8480 /*
8481 * Do this upfront, so we won't have a grace period where the ring
8482 * is closed but resources aren't reaped yet. This can cause
8483 * spurious failure in setting up a new ring.
8484 */
760618f7
JA
8485 io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
8486 ACCT_LOCKED);
309fc03a 8487
85faa7b8 8488 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
fc666777
JA
8489 /*
8490 * Use system_unbound_wq to avoid spawning tons of event kworkers
8491 * if we're exiting a ton of rings at the same time. It just adds
8492 * noise and overhead, there's no discernable change in runtime
8493 * over using system_wq.
8494 */
8495 queue_work(system_unbound_wq, &ctx->exit_work);
2b188cc1
JA
8496}
8497
8498static int io_uring_release(struct inode *inode, struct file *file)
8499{
8500 struct io_ring_ctx *ctx = file->private_data;
8501
8502 file->private_data = NULL;
8503 io_ring_ctx_wait_and_kill(ctx);
8504 return 0;
8505}
8506
67c4d9e6
PB
8507static bool io_wq_files_match(struct io_wq_work *work, void *data)
8508{
8509 struct files_struct *files = data;
8510
0f212204 8511 return !files || work->files == files;
67c4d9e6
PB
8512}
8513
f254ac04
JA
8514/*
8515 * Returns true if 'preq' is the link parent of 'req'
8516 */
8517static bool io_match_link(struct io_kiocb *preq, struct io_kiocb *req)
8518{
8519 struct io_kiocb *link;
8520
8521 if (!(preq->flags & REQ_F_LINK_HEAD))
8522 return false;
8523
8524 list_for_each_entry(link, &preq->link_list, link_list) {
8525 if (link == req)
8526 return true;
8527 }
8528
8529 return false;
8530}
8531
c127a2a1
PB
8532static bool io_match_link_files(struct io_kiocb *req,
8533 struct files_struct *files)
8534{
8535 struct io_kiocb *link;
8536
8537 if (io_match_files(req, files))
8538 return true;
8539 if (req->flags & REQ_F_LINK_HEAD) {
8540 list_for_each_entry(link, &req->link_list, link_list) {
8541 if (io_match_files(link, files))
8542 return true;
8543 }
8544 }
8545 return false;
8546}
8547
f254ac04
JA
8548/*
8549 * We're looking to cancel 'req' because it's holding on to our files, but
8550 * 'req' could be a link to another request. See if it is, and cancel that
8551 * parent request if so.
8552 */
8553static bool io_poll_remove_link(struct io_ring_ctx *ctx, struct io_kiocb *req)
8554{
8555 struct hlist_node *tmp;
8556 struct io_kiocb *preq;
8557 bool found = false;
8558 int i;
8559
8560 spin_lock_irq(&ctx->completion_lock);
8561 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
8562 struct hlist_head *list;
8563
8564 list = &ctx->cancel_hash[i];
8565 hlist_for_each_entry_safe(preq, tmp, list, hash_node) {
8566 found = io_match_link(preq, req);
8567 if (found) {
8568 io_poll_remove_one(preq);
8569 break;
8570 }
8571 }
8572 }
8573 spin_unlock_irq(&ctx->completion_lock);
8574 return found;
8575}
8576
8577static bool io_timeout_remove_link(struct io_ring_ctx *ctx,
8578 struct io_kiocb *req)
8579{
8580 struct io_kiocb *preq;
8581 bool found = false;
8582
8583 spin_lock_irq(&ctx->completion_lock);
8584 list_for_each_entry(preq, &ctx->timeout_list, timeout.list) {
8585 found = io_match_link(preq, req);
8586 if (found) {
8587 __io_timeout_cancel(preq);
8588 break;
8589 }
8590 }
8591 spin_unlock_irq(&ctx->completion_lock);
8592 return found;
8593}
8594
b711d4ea
JA
8595static bool io_cancel_link_cb(struct io_wq_work *work, void *data)
8596{
8597 return io_match_link(container_of(work, struct io_kiocb, work), data);
8598}
8599
8600static void io_attempt_cancel(struct io_ring_ctx *ctx, struct io_kiocb *req)
8601{
8602 enum io_wq_cancel cret;
8603
8604 /* cancel this particular work, if it's running */
8605 cret = io_wq_cancel_work(ctx->io_wq, &req->work);
8606 if (cret != IO_WQ_CANCEL_NOTFOUND)
8607 return;
8608
8609 /* find links that hold this pending, cancel those */
8610 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_link_cb, req, true);
8611 if (cret != IO_WQ_CANCEL_NOTFOUND)
8612 return;
8613
8614 /* if we have a poll link holding this pending, cancel that */
8615 if (io_poll_remove_link(ctx, req))
8616 return;
8617
8618 /* final option, timeout link is holding this req pending */
8619 io_timeout_remove_link(ctx, req);
8620}
8621
b7ddce3c
PB
8622static void io_cancel_defer_files(struct io_ring_ctx *ctx,
8623 struct files_struct *files)
8624{
8625 struct io_defer_entry *de = NULL;
8626 LIST_HEAD(list);
8627
8628 spin_lock_irq(&ctx->completion_lock);
8629 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
c127a2a1 8630 if (io_match_link_files(de->req, files)) {
b7ddce3c
PB
8631 list_cut_position(&list, &ctx->defer_list, &de->list);
8632 break;
8633 }
8634 }
8635 spin_unlock_irq(&ctx->completion_lock);
8636
8637 while (!list_empty(&list)) {
8638 de = list_first_entry(&list, struct io_defer_entry, list);
8639 list_del_init(&de->list);
8640 req_set_fail_links(de->req);
8641 io_put_req(de->req);
8642 io_req_complete(de->req, -ECANCELED);
8643 kfree(de);
8644 }
8645}
8646
76e1b642
JA
8647/*
8648 * Returns true if we found and killed one or more files pinning requests
8649 */
8650static bool io_uring_cancel_files(struct io_ring_ctx *ctx,
fcb323cc
JA
8651 struct files_struct *files)
8652{
67c4d9e6 8653 if (list_empty_careful(&ctx->inflight_list))
76e1b642 8654 return false;
67c4d9e6 8655
b7ddce3c 8656 io_cancel_defer_files(ctx, files);
67c4d9e6
PB
8657 /* cancel all at once, should be faster than doing it one by one*/
8658 io_wq_cancel_cb(ctx->io_wq, io_wq_files_match, files, true);
8659
fcb323cc 8660 while (!list_empty_careful(&ctx->inflight_list)) {
d8f1b971
XW
8661 struct io_kiocb *cancel_req = NULL, *req;
8662 DEFINE_WAIT(wait);
fcb323cc
JA
8663
8664 spin_lock_irq(&ctx->inflight_lock);
8665 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
0f212204 8666 if (files && req->work.files != files)
768134d4
JA
8667 continue;
8668 /* req is being completed, ignore */
8669 if (!refcount_inc_not_zero(&req->refs))
8670 continue;
8671 cancel_req = req;
8672 break;
fcb323cc 8673 }
768134d4 8674 if (cancel_req)
fcb323cc 8675 prepare_to_wait(&ctx->inflight_wait, &wait,
768134d4 8676 TASK_UNINTERRUPTIBLE);
fcb323cc
JA
8677 spin_unlock_irq(&ctx->inflight_lock);
8678
768134d4
JA
8679 /* We need to keep going until we don't find a matching req */
8680 if (!cancel_req)
fcb323cc 8681 break;
bb175342
PB
8682 /* cancel this request, or head link requests */
8683 io_attempt_cancel(ctx, cancel_req);
8684 io_put_req(cancel_req);
6200b0ae
JA
8685 /* cancellations _may_ trigger task work */
8686 io_run_task_work();
fcb323cc 8687 schedule();
d8f1b971 8688 finish_wait(&ctx->inflight_wait, &wait);
fcb323cc 8689 }
76e1b642
JA
8690
8691 return true;
fcb323cc
JA
8692}
8693
801dd57b 8694static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
44e728b8 8695{
801dd57b
PB
8696 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8697 struct task_struct *task = data;
44e728b8 8698
f3606e3a 8699 return io_task_match(req, task);
44e728b8
PB
8700}
8701
0f212204
JA
8702static bool __io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8703 struct task_struct *task,
8704 struct files_struct *files)
8705{
8706 bool ret;
8707
8708 ret = io_uring_cancel_files(ctx, files);
8709 if (!files) {
8710 enum io_wq_cancel cret;
8711
8712 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, task, true);
8713 if (cret != IO_WQ_CANCEL_NOTFOUND)
8714 ret = true;
8715
8716 /* SQPOLL thread does its own polling */
8717 if (!(ctx->flags & IORING_SETUP_SQPOLL)) {
8718 while (!list_empty_careful(&ctx->iopoll_list)) {
8719 io_iopoll_try_reap_events(ctx);
8720 ret = true;
8721 }
8722 }
8723
8724 ret |= io_poll_remove_all(ctx, task);
8725 ret |= io_kill_timeouts(ctx, task);
8726 }
8727
8728 return ret;
8729}
8730
8731/*
8732 * We need to iteratively cancel requests, in case a request has dependent
8733 * hard links. These persist even for failure of cancelations, hence keep
8734 * looping until none are found.
8735 */
8736static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8737 struct files_struct *files)
8738{
8739 struct task_struct *task = current;
8740
534ca6d6
JA
8741 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data)
8742 task = ctx->sq_data->thread;
0f212204
JA
8743
8744 io_cqring_overflow_flush(ctx, true, task, files);
8745
8746 while (__io_uring_cancel_task_requests(ctx, task, files)) {
8747 io_run_task_work();
8748 cond_resched();
8749 }
8750}
8751
8752/*
8753 * Note that this task has used io_uring. We use it for cancelation purposes.
8754 */
8755static int io_uring_add_task_file(struct file *file)
8756{
8757 if (unlikely(!current->io_uring)) {
8758 int ret;
8759
8760 ret = io_uring_alloc_task_context(current);
8761 if (unlikely(ret))
8762 return ret;
8763 }
8764 if (current->io_uring->last != file) {
8765 XA_STATE(xas, &current->io_uring->xa, (unsigned long) file);
8766 void *old;
8767
8768 rcu_read_lock();
8769 old = xas_load(&xas);
8770 if (old != file) {
8771 get_file(file);
8772 xas_lock(&xas);
8773 xas_store(&xas, file);
8774 xas_unlock(&xas);
8775 }
8776 rcu_read_unlock();
8777 current->io_uring->last = file;
8778 }
8779
8780 return 0;
8781}
8782
8783/*
8784 * Remove this io_uring_file -> task mapping.
8785 */
8786static void io_uring_del_task_file(struct file *file)
8787{
8788 struct io_uring_task *tctx = current->io_uring;
8789 XA_STATE(xas, &tctx->xa, (unsigned long) file);
8790
8791 if (tctx->last == file)
8792 tctx->last = NULL;
8793
8794 xas_lock(&xas);
8795 file = xas_store(&xas, NULL);
8796 xas_unlock(&xas);
8797
8798 if (file)
8799 fput(file);
8800}
8801
8802static void __io_uring_attempt_task_drop(struct file *file)
8803{
8804 XA_STATE(xas, &current->io_uring->xa, (unsigned long) file);
8805 struct file *old;
8806
8807 rcu_read_lock();
8808 old = xas_load(&xas);
8809 rcu_read_unlock();
8810
8811 if (old == file)
8812 io_uring_del_task_file(file);
8813}
8814
8815/*
8816 * Drop task note for this file if we're the only ones that hold it after
8817 * pending fput()
8818 */
8819static void io_uring_attempt_task_drop(struct file *file, bool exiting)
8820{
8821 if (!current->io_uring)
8822 return;
8823 /*
8824 * fput() is pending, will be 2 if the only other ref is our potential
8825 * task file note. If the task is exiting, drop regardless of count.
8826 */
8827 if (!exiting && atomic_long_read(&file->f_count) != 2)
8828 return;
8829
8830 __io_uring_attempt_task_drop(file);
8831}
8832
8833void __io_uring_files_cancel(struct files_struct *files)
8834{
8835 struct io_uring_task *tctx = current->io_uring;
8836 XA_STATE(xas, &tctx->xa, 0);
8837
8838 /* make sure overflow events are dropped */
8839 tctx->in_idle = true;
8840
8841 do {
8842 struct io_ring_ctx *ctx;
8843 struct file *file;
8844
8845 xas_lock(&xas);
8846 file = xas_next_entry(&xas, ULONG_MAX);
8847 xas_unlock(&xas);
8848
8849 if (!file)
8850 break;
8851
8852 ctx = file->private_data;
8853
8854 io_uring_cancel_task_requests(ctx, files);
8855 if (files)
8856 io_uring_del_task_file(file);
8857 } while (1);
8858}
8859
8860static inline bool io_uring_task_idle(struct io_uring_task *tctx)
8861{
8862 return atomic_long_read(&tctx->req_issue) ==
8863 atomic_long_read(&tctx->req_complete);
8864}
8865
8866/*
8867 * Find any io_uring fd that this task has registered or done IO on, and cancel
8868 * requests.
8869 */
8870void __io_uring_task_cancel(void)
8871{
8872 struct io_uring_task *tctx = current->io_uring;
8873 DEFINE_WAIT(wait);
8874 long completions;
8875
8876 /* make sure overflow events are dropped */
8877 tctx->in_idle = true;
8878
8879 while (!io_uring_task_idle(tctx)) {
8880 /* read completions before cancelations */
8881 completions = atomic_long_read(&tctx->req_complete);
8882 __io_uring_files_cancel(NULL);
8883
8884 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
8885
8886 /*
8887 * If we've seen completions, retry. This avoids a race where
8888 * a completion comes in before we did prepare_to_wait().
8889 */
8890 if (completions != atomic_long_read(&tctx->req_complete))
8891 continue;
8892 if (io_uring_task_idle(tctx))
8893 break;
8894 schedule();
8895 }
8896
8897 finish_wait(&tctx->wait, &wait);
8898 tctx->in_idle = false;
8899}
8900
fcb323cc
JA
8901static int io_uring_flush(struct file *file, void *data)
8902{
8903 struct io_ring_ctx *ctx = file->private_data;
8904
6ab23144
JA
8905 /*
8906 * If the task is going away, cancel work it may have pending
8907 */
801dd57b 8908 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
0f212204 8909 data = NULL;
6ab23144 8910
0f212204
JA
8911 io_uring_cancel_task_requests(ctx, data);
8912 io_uring_attempt_task_drop(file, !data);
fcb323cc
JA
8913 return 0;
8914}
8915
6c5c240e
RP
8916static void *io_uring_validate_mmap_request(struct file *file,
8917 loff_t pgoff, size_t sz)
2b188cc1 8918{
2b188cc1 8919 struct io_ring_ctx *ctx = file->private_data;
6c5c240e 8920 loff_t offset = pgoff << PAGE_SHIFT;
2b188cc1
JA
8921 struct page *page;
8922 void *ptr;
8923
8924 switch (offset) {
8925 case IORING_OFF_SQ_RING:
75b28aff
HV
8926 case IORING_OFF_CQ_RING:
8927 ptr = ctx->rings;
2b188cc1
JA
8928 break;
8929 case IORING_OFF_SQES:
8930 ptr = ctx->sq_sqes;
8931 break;
2b188cc1 8932 default:
6c5c240e 8933 return ERR_PTR(-EINVAL);
2b188cc1
JA
8934 }
8935
8936 page = virt_to_head_page(ptr);
a50b854e 8937 if (sz > page_size(page))
6c5c240e
RP
8938 return ERR_PTR(-EINVAL);
8939
8940 return ptr;
8941}
8942
8943#ifdef CONFIG_MMU
8944
8945static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8946{
8947 size_t sz = vma->vm_end - vma->vm_start;
8948 unsigned long pfn;
8949 void *ptr;
8950
8951 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
8952 if (IS_ERR(ptr))
8953 return PTR_ERR(ptr);
2b188cc1
JA
8954
8955 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
8956 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
8957}
8958
6c5c240e
RP
8959#else /* !CONFIG_MMU */
8960
8961static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8962{
8963 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
8964}
8965
8966static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
8967{
8968 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
8969}
8970
8971static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
8972 unsigned long addr, unsigned long len,
8973 unsigned long pgoff, unsigned long flags)
8974{
8975 void *ptr;
8976
8977 ptr = io_uring_validate_mmap_request(file, pgoff, len);
8978 if (IS_ERR(ptr))
8979 return PTR_ERR(ptr);
8980
8981 return (unsigned long) ptr;
8982}
8983
8984#endif /* !CONFIG_MMU */
8985
90554200
JA
8986static void io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
8987{
8988 DEFINE_WAIT(wait);
8989
8990 do {
8991 if (!io_sqring_full(ctx))
8992 break;
8993
8994 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
8995
8996 if (!io_sqring_full(ctx))
8997 break;
8998
8999 schedule();
9000 } while (!signal_pending(current));
9001
9002 finish_wait(&ctx->sqo_sq_wait, &wait);
9003}
9004
2b188cc1
JA
9005SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
9006 u32, min_complete, u32, flags, const sigset_t __user *, sig,
9007 size_t, sigsz)
9008{
9009 struct io_ring_ctx *ctx;
9010 long ret = -EBADF;
9011 int submitted = 0;
9012 struct fd f;
9013
4c6e277c 9014 io_run_task_work();
b41e9852 9015
90554200
JA
9016 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
9017 IORING_ENTER_SQ_WAIT))
2b188cc1
JA
9018 return -EINVAL;
9019
9020 f = fdget(fd);
9021 if (!f.file)
9022 return -EBADF;
9023
9024 ret = -EOPNOTSUPP;
9025 if (f.file->f_op != &io_uring_fops)
9026 goto out_fput;
9027
9028 ret = -ENXIO;
9029 ctx = f.file->private_data;
9030 if (!percpu_ref_tryget(&ctx->refs))
9031 goto out_fput;
9032
7e84e1c7
SG
9033 ret = -EBADFD;
9034 if (ctx->flags & IORING_SETUP_R_DISABLED)
9035 goto out;
9036
6c271ce2
JA
9037 /*
9038 * For SQ polling, the thread will do all submissions and completions.
9039 * Just return the requested submit count, and wake the thread if
9040 * we were asked to.
9041 */
b2a9eada 9042 ret = 0;
6c271ce2 9043 if (ctx->flags & IORING_SETUP_SQPOLL) {
c1edbf5f 9044 if (!list_empty_careful(&ctx->cq_overflow_list))
e6c8aa9a 9045 io_cqring_overflow_flush(ctx, false, NULL, NULL);
6c271ce2 9046 if (flags & IORING_ENTER_SQ_WAKEUP)
534ca6d6 9047 wake_up(&ctx->sq_data->wait);
90554200
JA
9048 if (flags & IORING_ENTER_SQ_WAIT)
9049 io_sqpoll_wait_sq(ctx);
6c271ce2 9050 submitted = to_submit;
b2a9eada 9051 } else if (to_submit) {
0f212204
JA
9052 ret = io_uring_add_task_file(f.file);
9053 if (unlikely(ret))
9054 goto out;
2b188cc1 9055 mutex_lock(&ctx->uring_lock);
0f212204 9056 submitted = io_submit_sqes(ctx, to_submit);
2b188cc1 9057 mutex_unlock(&ctx->uring_lock);
7c504e65
PB
9058
9059 if (submitted != to_submit)
9060 goto out;
2b188cc1
JA
9061 }
9062 if (flags & IORING_ENTER_GETEVENTS) {
9063 min_complete = min(min_complete, ctx->cq_entries);
9064
32b2244a
XW
9065 /*
9066 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
9067 * space applications don't need to do io completion events
9068 * polling again, they can rely on io_sq_thread to do polling
9069 * work, which can reduce cpu usage and uring_lock contention.
9070 */
9071 if (ctx->flags & IORING_SETUP_IOPOLL &&
9072 !(ctx->flags & IORING_SETUP_SQPOLL)) {
7668b92a 9073 ret = io_iopoll_check(ctx, min_complete);
def596e9
JA
9074 } else {
9075 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
9076 }
2b188cc1
JA
9077 }
9078
7c504e65 9079out:
6805b32e 9080 percpu_ref_put(&ctx->refs);
2b188cc1
JA
9081out_fput:
9082 fdput(f);
9083 return submitted ? submitted : ret;
9084}
9085
bebdb65e 9086#ifdef CONFIG_PROC_FS
87ce955b
JA
9087static int io_uring_show_cred(int id, void *p, void *data)
9088{
9089 const struct cred *cred = p;
9090 struct seq_file *m = data;
9091 struct user_namespace *uns = seq_user_ns(m);
9092 struct group_info *gi;
9093 kernel_cap_t cap;
9094 unsigned __capi;
9095 int g;
9096
9097 seq_printf(m, "%5d\n", id);
9098 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
9099 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
9100 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
9101 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
9102 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
9103 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
9104 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
9105 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
9106 seq_puts(m, "\n\tGroups:\t");
9107 gi = cred->group_info;
9108 for (g = 0; g < gi->ngroups; g++) {
9109 seq_put_decimal_ull(m, g ? " " : "",
9110 from_kgid_munged(uns, gi->gid[g]));
9111 }
9112 seq_puts(m, "\n\tCapEff:\t");
9113 cap = cred->cap_effective;
9114 CAP_FOR_EACH_U32(__capi)
9115 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
9116 seq_putc(m, '\n');
9117 return 0;
9118}
9119
9120static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
9121{
dbbe9c64 9122 struct io_sq_data *sq = NULL;
fad8e0de 9123 bool has_lock;
87ce955b
JA
9124 int i;
9125
fad8e0de
JA
9126 /*
9127 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
9128 * since fdinfo case grabs it in the opposite direction of normal use
9129 * cases. If we fail to get the lock, we just don't iterate any
9130 * structures that could be going away outside the io_uring mutex.
9131 */
9132 has_lock = mutex_trylock(&ctx->uring_lock);
9133
dbbe9c64
JQ
9134 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL))
9135 sq = ctx->sq_data;
9136
9137 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
9138 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
87ce955b 9139 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
fad8e0de 9140 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
87ce955b
JA
9141 struct fixed_file_table *table;
9142 struct file *f;
9143
9144 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
9145 f = table->files[i & IORING_FILE_TABLE_MASK];
9146 if (f)
9147 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
9148 else
9149 seq_printf(m, "%5u: <none>\n", i);
9150 }
9151 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
fad8e0de 9152 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
87ce955b
JA
9153 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
9154
9155 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
9156 (unsigned int) buf->len);
9157 }
fad8e0de 9158 if (has_lock && !idr_is_empty(&ctx->personality_idr)) {
87ce955b
JA
9159 seq_printf(m, "Personalities:\n");
9160 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
9161 }
d7718a9d
JA
9162 seq_printf(m, "PollList:\n");
9163 spin_lock_irq(&ctx->completion_lock);
9164 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
9165 struct hlist_head *list = &ctx->cancel_hash[i];
9166 struct io_kiocb *req;
9167
9168 hlist_for_each_entry(req, list, hash_node)
9169 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
9170 req->task->task_works != NULL);
9171 }
9172 spin_unlock_irq(&ctx->completion_lock);
fad8e0de
JA
9173 if (has_lock)
9174 mutex_unlock(&ctx->uring_lock);
87ce955b
JA
9175}
9176
9177static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
9178{
9179 struct io_ring_ctx *ctx = f->private_data;
9180
9181 if (percpu_ref_tryget(&ctx->refs)) {
9182 __io_uring_show_fdinfo(ctx, m);
9183 percpu_ref_put(&ctx->refs);
9184 }
9185}
bebdb65e 9186#endif
87ce955b 9187
2b188cc1
JA
9188static const struct file_operations io_uring_fops = {
9189 .release = io_uring_release,
fcb323cc 9190 .flush = io_uring_flush,
2b188cc1 9191 .mmap = io_uring_mmap,
6c5c240e
RP
9192#ifndef CONFIG_MMU
9193 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
9194 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
9195#endif
2b188cc1
JA
9196 .poll = io_uring_poll,
9197 .fasync = io_uring_fasync,
bebdb65e 9198#ifdef CONFIG_PROC_FS
87ce955b 9199 .show_fdinfo = io_uring_show_fdinfo,
bebdb65e 9200#endif
2b188cc1
JA
9201};
9202
9203static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
9204 struct io_uring_params *p)
9205{
75b28aff
HV
9206 struct io_rings *rings;
9207 size_t size, sq_array_offset;
2b188cc1 9208
bd740481
JA
9209 /* make sure these are sane, as we already accounted them */
9210 ctx->sq_entries = p->sq_entries;
9211 ctx->cq_entries = p->cq_entries;
9212
75b28aff
HV
9213 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
9214 if (size == SIZE_MAX)
9215 return -EOVERFLOW;
9216
9217 rings = io_mem_alloc(size);
9218 if (!rings)
2b188cc1
JA
9219 return -ENOMEM;
9220
75b28aff
HV
9221 ctx->rings = rings;
9222 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
9223 rings->sq_ring_mask = p->sq_entries - 1;
9224 rings->cq_ring_mask = p->cq_entries - 1;
9225 rings->sq_ring_entries = p->sq_entries;
9226 rings->cq_ring_entries = p->cq_entries;
9227 ctx->sq_mask = rings->sq_ring_mask;
9228 ctx->cq_mask = rings->cq_ring_mask;
2b188cc1
JA
9229
9230 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
eb065d30
JA
9231 if (size == SIZE_MAX) {
9232 io_mem_free(ctx->rings);
9233 ctx->rings = NULL;
2b188cc1 9234 return -EOVERFLOW;
eb065d30 9235 }
2b188cc1
JA
9236
9237 ctx->sq_sqes = io_mem_alloc(size);
eb065d30
JA
9238 if (!ctx->sq_sqes) {
9239 io_mem_free(ctx->rings);
9240 ctx->rings = NULL;
2b188cc1 9241 return -ENOMEM;
eb065d30 9242 }
2b188cc1 9243
2b188cc1
JA
9244 return 0;
9245}
9246
9247/*
9248 * Allocate an anonymous fd, this is what constitutes the application
9249 * visible backing of an io_uring instance. The application mmaps this
9250 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
9251 * we have to tie this fd to a socket for file garbage collection purposes.
9252 */
9253static int io_uring_get_fd(struct io_ring_ctx *ctx)
9254{
9255 struct file *file;
9256 int ret;
9257
9258#if defined(CONFIG_UNIX)
9259 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
9260 &ctx->ring_sock);
9261 if (ret)
9262 return ret;
9263#endif
9264
9265 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
9266 if (ret < 0)
9267 goto err;
9268
9269 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
9270 O_RDWR | O_CLOEXEC);
9271 if (IS_ERR(file)) {
0f212204 9272err_fd:
2b188cc1
JA
9273 put_unused_fd(ret);
9274 ret = PTR_ERR(file);
9275 goto err;
9276 }
9277
9278#if defined(CONFIG_UNIX)
9279 ctx->ring_sock->file = file;
9280#endif
0f212204
JA
9281 if (unlikely(io_uring_add_task_file(file))) {
9282 file = ERR_PTR(-ENOMEM);
9283 goto err_fd;
9284 }
2b188cc1
JA
9285 fd_install(ret, file);
9286 return ret;
9287err:
9288#if defined(CONFIG_UNIX)
9289 sock_release(ctx->ring_sock);
9290 ctx->ring_sock = NULL;
9291#endif
9292 return ret;
9293}
9294
7f13657d
XW
9295static int io_uring_create(unsigned entries, struct io_uring_params *p,
9296 struct io_uring_params __user *params)
2b188cc1
JA
9297{
9298 struct user_struct *user = NULL;
9299 struct io_ring_ctx *ctx;
aad5d8da 9300 bool limit_mem;
2b188cc1
JA
9301 int ret;
9302
8110c1a6 9303 if (!entries)
2b188cc1 9304 return -EINVAL;
8110c1a6
JA
9305 if (entries > IORING_MAX_ENTRIES) {
9306 if (!(p->flags & IORING_SETUP_CLAMP))
9307 return -EINVAL;
9308 entries = IORING_MAX_ENTRIES;
9309 }
2b188cc1
JA
9310
9311 /*
9312 * Use twice as many entries for the CQ ring. It's possible for the
9313 * application to drive a higher depth than the size of the SQ ring,
9314 * since the sqes are only used at submission time. This allows for
33a107f0
JA
9315 * some flexibility in overcommitting a bit. If the application has
9316 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
9317 * of CQ ring entries manually.
2b188cc1
JA
9318 */
9319 p->sq_entries = roundup_pow_of_two(entries);
33a107f0
JA
9320 if (p->flags & IORING_SETUP_CQSIZE) {
9321 /*
9322 * If IORING_SETUP_CQSIZE is set, we do the same roundup
9323 * to a power-of-two, if it isn't already. We do NOT impose
9324 * any cq vs sq ring sizing.
9325 */
8110c1a6 9326 if (p->cq_entries < p->sq_entries)
33a107f0 9327 return -EINVAL;
8110c1a6
JA
9328 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
9329 if (!(p->flags & IORING_SETUP_CLAMP))
9330 return -EINVAL;
9331 p->cq_entries = IORING_MAX_CQ_ENTRIES;
9332 }
33a107f0
JA
9333 p->cq_entries = roundup_pow_of_two(p->cq_entries);
9334 } else {
9335 p->cq_entries = 2 * p->sq_entries;
9336 }
2b188cc1
JA
9337
9338 user = get_uid(current_user());
aad5d8da 9339 limit_mem = !capable(CAP_IPC_LOCK);
2b188cc1 9340
aad5d8da 9341 if (limit_mem) {
a087e2b5 9342 ret = __io_account_mem(user,
2b188cc1
JA
9343 ring_pages(p->sq_entries, p->cq_entries));
9344 if (ret) {
9345 free_uid(user);
9346 return ret;
9347 }
9348 }
9349
9350 ctx = io_ring_ctx_alloc(p);
9351 if (!ctx) {
aad5d8da 9352 if (limit_mem)
a087e2b5 9353 __io_unaccount_mem(user, ring_pages(p->sq_entries,
2b188cc1
JA
9354 p->cq_entries));
9355 free_uid(user);
9356 return -ENOMEM;
9357 }
9358 ctx->compat = in_compat_syscall();
2b188cc1 9359 ctx->user = user;
0b8c0ec7 9360 ctx->creds = get_current_cred();
2b188cc1 9361
2aede0e4
JA
9362 ctx->sqo_task = get_task_struct(current);
9363
9364 /*
9365 * This is just grabbed for accounting purposes. When a process exits,
9366 * the mm is exited and dropped before the files, hence we need to hang
9367 * on to this mm purely for the purposes of being able to unaccount
9368 * memory (locked/pinned vm). It's not used for anything else.
9369 */
6b7898eb 9370 mmgrab(current->mm);
2aede0e4 9371 ctx->mm_account = current->mm;
6b7898eb 9372
91d8f519
DZ
9373#ifdef CONFIG_BLK_CGROUP
9374 /*
9375 * The sq thread will belong to the original cgroup it was inited in.
9376 * If the cgroup goes offline (e.g. disabling the io controller), then
9377 * issued bios will be associated with the closest cgroup later in the
9378 * block layer.
9379 */
9380 rcu_read_lock();
9381 ctx->sqo_blkcg_css = blkcg_css();
9382 ret = css_tryget_online(ctx->sqo_blkcg_css);
9383 rcu_read_unlock();
9384 if (!ret) {
9385 /* don't init against a dying cgroup, have the user try again */
9386 ctx->sqo_blkcg_css = NULL;
9387 ret = -ENODEV;
9388 goto err;
9389 }
9390#endif
9391
f74441e6
JA
9392 /*
9393 * Account memory _before_ installing the file descriptor. Once
9394 * the descriptor is installed, it can get closed at any time. Also
9395 * do this before hitting the general error path, as ring freeing
9396 * will un-account as well.
9397 */
9398 io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
9399 ACCT_LOCKED);
9400 ctx->limit_mem = limit_mem;
9401
2b188cc1
JA
9402 ret = io_allocate_scq_urings(ctx, p);
9403 if (ret)
9404 goto err;
9405
7e84e1c7 9406 ret = io_sq_offload_create(ctx, p);
2b188cc1
JA
9407 if (ret)
9408 goto err;
9409
7e84e1c7
SG
9410 if (!(p->flags & IORING_SETUP_R_DISABLED))
9411 io_sq_offload_start(ctx);
9412
2b188cc1 9413 memset(&p->sq_off, 0, sizeof(p->sq_off));
75b28aff
HV
9414 p->sq_off.head = offsetof(struct io_rings, sq.head);
9415 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
9416 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
9417 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
9418 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
9419 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
9420 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
2b188cc1
JA
9421
9422 memset(&p->cq_off, 0, sizeof(p->cq_off));
75b28aff
HV
9423 p->cq_off.head = offsetof(struct io_rings, cq.head);
9424 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
9425 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
9426 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
9427 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
9428 p->cq_off.cqes = offsetof(struct io_rings, cqes);
0d9b5b3a 9429 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
ac90f249 9430
7f13657d
XW
9431 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9432 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
5769a351
JX
9433 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
9434 IORING_FEAT_POLL_32BITS;
7f13657d
XW
9435
9436 if (copy_to_user(params, p, sizeof(*p))) {
9437 ret = -EFAULT;
9438 goto err;
9439 }
d1719f70 9440
044c1ab3
JA
9441 /*
9442 * Install ring fd as the very last thing, so we don't risk someone
9443 * having closed it before we finish setup
9444 */
9445 ret = io_uring_get_fd(ctx);
9446 if (ret < 0)
9447 goto err;
9448
c826bd7a 9449 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
2b188cc1
JA
9450 return ret;
9451err:
9452 io_ring_ctx_wait_and_kill(ctx);
9453 return ret;
9454}
9455
9456/*
9457 * Sets up an aio uring context, and returns the fd. Applications asks for a
9458 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9459 * params structure passed in.
9460 */
9461static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9462{
9463 struct io_uring_params p;
2b188cc1
JA
9464 int i;
9465
9466 if (copy_from_user(&p, params, sizeof(p)))
9467 return -EFAULT;
9468 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9469 if (p.resv[i])
9470 return -EINVAL;
9471 }
9472
6c271ce2 9473 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
8110c1a6 9474 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
7e84e1c7
SG
9475 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
9476 IORING_SETUP_R_DISABLED))
2b188cc1
JA
9477 return -EINVAL;
9478
7f13657d 9479 return io_uring_create(entries, &p, params);
2b188cc1
JA
9480}
9481
9482SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9483 struct io_uring_params __user *, params)
9484{
9485 return io_uring_setup(entries, params);
9486}
9487
66f4af93
JA
9488static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9489{
9490 struct io_uring_probe *p;
9491 size_t size;
9492 int i, ret;
9493
9494 size = struct_size(p, ops, nr_args);
9495 if (size == SIZE_MAX)
9496 return -EOVERFLOW;
9497 p = kzalloc(size, GFP_KERNEL);
9498 if (!p)
9499 return -ENOMEM;
9500
9501 ret = -EFAULT;
9502 if (copy_from_user(p, arg, size))
9503 goto out;
9504 ret = -EINVAL;
9505 if (memchr_inv(p, 0, size))
9506 goto out;
9507
9508 p->last_op = IORING_OP_LAST - 1;
9509 if (nr_args > IORING_OP_LAST)
9510 nr_args = IORING_OP_LAST;
9511
9512 for (i = 0; i < nr_args; i++) {
9513 p->ops[i].op = i;
9514 if (!io_op_defs[i].not_supported)
9515 p->ops[i].flags = IO_URING_OP_SUPPORTED;
9516 }
9517 p->ops_len = i;
9518
9519 ret = 0;
9520 if (copy_to_user(arg, p, size))
9521 ret = -EFAULT;
9522out:
9523 kfree(p);
9524 return ret;
9525}
9526
071698e1
JA
9527static int io_register_personality(struct io_ring_ctx *ctx)
9528{
9529 const struct cred *creds = get_current_cred();
9530 int id;
9531
9532 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
9533 USHRT_MAX, GFP_KERNEL);
9534 if (id < 0)
9535 put_cred(creds);
9536 return id;
9537}
9538
9539static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
9540{
9541 const struct cred *old_creds;
9542
9543 old_creds = idr_remove(&ctx->personality_idr, id);
9544 if (old_creds) {
9545 put_cred(old_creds);
9546 return 0;
9547 }
9548
9549 return -EINVAL;
9550}
9551
21b55dbc
SG
9552static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
9553 unsigned int nr_args)
9554{
9555 struct io_uring_restriction *res;
9556 size_t size;
9557 int i, ret;
9558
7e84e1c7
SG
9559 /* Restrictions allowed only if rings started disabled */
9560 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9561 return -EBADFD;
9562
21b55dbc 9563 /* We allow only a single restrictions registration */
7e84e1c7 9564 if (ctx->restrictions.registered)
21b55dbc
SG
9565 return -EBUSY;
9566
9567 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
9568 return -EINVAL;
9569
9570 size = array_size(nr_args, sizeof(*res));
9571 if (size == SIZE_MAX)
9572 return -EOVERFLOW;
9573
9574 res = memdup_user(arg, size);
9575 if (IS_ERR(res))
9576 return PTR_ERR(res);
9577
9578 ret = 0;
9579
9580 for (i = 0; i < nr_args; i++) {
9581 switch (res[i].opcode) {
9582 case IORING_RESTRICTION_REGISTER_OP:
9583 if (res[i].register_op >= IORING_REGISTER_LAST) {
9584 ret = -EINVAL;
9585 goto out;
9586 }
9587
9588 __set_bit(res[i].register_op,
9589 ctx->restrictions.register_op);
9590 break;
9591 case IORING_RESTRICTION_SQE_OP:
9592 if (res[i].sqe_op >= IORING_OP_LAST) {
9593 ret = -EINVAL;
9594 goto out;
9595 }
9596
9597 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
9598 break;
9599 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
9600 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
9601 break;
9602 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
9603 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
9604 break;
9605 default:
9606 ret = -EINVAL;
9607 goto out;
9608 }
9609 }
9610
9611out:
9612 /* Reset all restrictions if an error happened */
9613 if (ret != 0)
9614 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
9615 else
7e84e1c7 9616 ctx->restrictions.registered = true;
21b55dbc
SG
9617
9618 kfree(res);
9619 return ret;
9620}
9621
7e84e1c7
SG
9622static int io_register_enable_rings(struct io_ring_ctx *ctx)
9623{
9624 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9625 return -EBADFD;
9626
9627 if (ctx->restrictions.registered)
9628 ctx->restricted = 1;
9629
9630 ctx->flags &= ~IORING_SETUP_R_DISABLED;
9631
9632 io_sq_offload_start(ctx);
9633
9634 return 0;
9635}
9636
071698e1
JA
9637static bool io_register_op_must_quiesce(int op)
9638{
9639 switch (op) {
9640 case IORING_UNREGISTER_FILES:
9641 case IORING_REGISTER_FILES_UPDATE:
9642 case IORING_REGISTER_PROBE:
9643 case IORING_REGISTER_PERSONALITY:
9644 case IORING_UNREGISTER_PERSONALITY:
9645 return false;
9646 default:
9647 return true;
9648 }
9649}
9650
edafccee
JA
9651static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
9652 void __user *arg, unsigned nr_args)
b19062a5
JA
9653 __releases(ctx->uring_lock)
9654 __acquires(ctx->uring_lock)
edafccee
JA
9655{
9656 int ret;
9657
35fa71a0
JA
9658 /*
9659 * We're inside the ring mutex, if the ref is already dying, then
9660 * someone else killed the ctx or is already going through
9661 * io_uring_register().
9662 */
9663 if (percpu_ref_is_dying(&ctx->refs))
9664 return -ENXIO;
9665
071698e1 9666 if (io_register_op_must_quiesce(opcode)) {
05f3fb3c 9667 percpu_ref_kill(&ctx->refs);
b19062a5 9668
05f3fb3c
JA
9669 /*
9670 * Drop uring mutex before waiting for references to exit. If
9671 * another thread is currently inside io_uring_enter() it might
9672 * need to grab the uring_lock to make progress. If we hold it
9673 * here across the drain wait, then we can deadlock. It's safe
9674 * to drop the mutex here, since no new references will come in
9675 * after we've killed the percpu ref.
9676 */
9677 mutex_unlock(&ctx->uring_lock);
af9c1a44
JA
9678 do {
9679 ret = wait_for_completion_interruptible(&ctx->ref_comp);
9680 if (!ret)
9681 break;
9682 if (io_run_task_work_sig() > 0)
9683 continue;
9684 } while (1);
9685
05f3fb3c 9686 mutex_lock(&ctx->uring_lock);
af9c1a44 9687
c150368b
JA
9688 if (ret) {
9689 percpu_ref_resurrect(&ctx->refs);
9690 ret = -EINTR;
21b55dbc
SG
9691 goto out_quiesce;
9692 }
9693 }
9694
9695 if (ctx->restricted) {
9696 if (opcode >= IORING_REGISTER_LAST) {
9697 ret = -EINVAL;
9698 goto out;
9699 }
9700
9701 if (!test_bit(opcode, ctx->restrictions.register_op)) {
9702 ret = -EACCES;
c150368b
JA
9703 goto out;
9704 }
05f3fb3c 9705 }
edafccee
JA
9706
9707 switch (opcode) {
9708 case IORING_REGISTER_BUFFERS:
9709 ret = io_sqe_buffer_register(ctx, arg, nr_args);
9710 break;
9711 case IORING_UNREGISTER_BUFFERS:
9712 ret = -EINVAL;
9713 if (arg || nr_args)
9714 break;
9715 ret = io_sqe_buffer_unregister(ctx);
9716 break;
6b06314c
JA
9717 case IORING_REGISTER_FILES:
9718 ret = io_sqe_files_register(ctx, arg, nr_args);
9719 break;
9720 case IORING_UNREGISTER_FILES:
9721 ret = -EINVAL;
9722 if (arg || nr_args)
9723 break;
9724 ret = io_sqe_files_unregister(ctx);
9725 break;
c3a31e60
JA
9726 case IORING_REGISTER_FILES_UPDATE:
9727 ret = io_sqe_files_update(ctx, arg, nr_args);
9728 break;
9b402849 9729 case IORING_REGISTER_EVENTFD:
f2842ab5 9730 case IORING_REGISTER_EVENTFD_ASYNC:
9b402849
JA
9731 ret = -EINVAL;
9732 if (nr_args != 1)
9733 break;
9734 ret = io_eventfd_register(ctx, arg);
f2842ab5
JA
9735 if (ret)
9736 break;
9737 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
9738 ctx->eventfd_async = 1;
9739 else
9740 ctx->eventfd_async = 0;
9b402849
JA
9741 break;
9742 case IORING_UNREGISTER_EVENTFD:
9743 ret = -EINVAL;
9744 if (arg || nr_args)
9745 break;
9746 ret = io_eventfd_unregister(ctx);
9747 break;
66f4af93
JA
9748 case IORING_REGISTER_PROBE:
9749 ret = -EINVAL;
9750 if (!arg || nr_args > 256)
9751 break;
9752 ret = io_probe(ctx, arg, nr_args);
9753 break;
071698e1
JA
9754 case IORING_REGISTER_PERSONALITY:
9755 ret = -EINVAL;
9756 if (arg || nr_args)
9757 break;
9758 ret = io_register_personality(ctx);
9759 break;
9760 case IORING_UNREGISTER_PERSONALITY:
9761 ret = -EINVAL;
9762 if (arg)
9763 break;
9764 ret = io_unregister_personality(ctx, nr_args);
9765 break;
7e84e1c7
SG
9766 case IORING_REGISTER_ENABLE_RINGS:
9767 ret = -EINVAL;
9768 if (arg || nr_args)
9769 break;
9770 ret = io_register_enable_rings(ctx);
9771 break;
21b55dbc
SG
9772 case IORING_REGISTER_RESTRICTIONS:
9773 ret = io_register_restrictions(ctx, arg, nr_args);
9774 break;
edafccee
JA
9775 default:
9776 ret = -EINVAL;
9777 break;
9778 }
9779
21b55dbc 9780out:
071698e1 9781 if (io_register_op_must_quiesce(opcode)) {
05f3fb3c 9782 /* bring the ctx back to life */
05f3fb3c 9783 percpu_ref_reinit(&ctx->refs);
21b55dbc 9784out_quiesce:
0f158b4c 9785 reinit_completion(&ctx->ref_comp);
05f3fb3c 9786 }
edafccee
JA
9787 return ret;
9788}
9789
9790SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
9791 void __user *, arg, unsigned int, nr_args)
9792{
9793 struct io_ring_ctx *ctx;
9794 long ret = -EBADF;
9795 struct fd f;
9796
9797 f = fdget(fd);
9798 if (!f.file)
9799 return -EBADF;
9800
9801 ret = -EOPNOTSUPP;
9802 if (f.file->f_op != &io_uring_fops)
9803 goto out_fput;
9804
9805 ctx = f.file->private_data;
9806
9807 mutex_lock(&ctx->uring_lock);
9808 ret = __io_uring_register(ctx, opcode, arg, nr_args);
9809 mutex_unlock(&ctx->uring_lock);
c826bd7a
DD
9810 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
9811 ctx->cq_ev_fd != NULL, ret);
edafccee
JA
9812out_fput:
9813 fdput(f);
9814 return ret;
9815}
9816
2b188cc1
JA
9817static int __init io_uring_init(void)
9818{
d7f62e82
SM
9819#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
9820 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
9821 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
9822} while (0)
9823
9824#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
9825 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
9826 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
9827 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
9828 BUILD_BUG_SQE_ELEM(1, __u8, flags);
9829 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
9830 BUILD_BUG_SQE_ELEM(4, __s32, fd);
9831 BUILD_BUG_SQE_ELEM(8, __u64, off);
9832 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
9833 BUILD_BUG_SQE_ELEM(16, __u64, addr);
7d67af2c 9834 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
d7f62e82
SM
9835 BUILD_BUG_SQE_ELEM(24, __u32, len);
9836 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
9837 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
9838 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
9839 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
5769a351
JX
9840 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
9841 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
d7f62e82
SM
9842 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
9843 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
9844 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
9845 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
9846 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
9847 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
9848 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
9849 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
7d67af2c 9850 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
d7f62e82
SM
9851 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
9852 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
9853 BUILD_BUG_SQE_ELEM(42, __u16, personality);
7d67af2c 9854 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
d7f62e82 9855
d3656344 9856 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
84557871 9857 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
2b188cc1
JA
9858 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
9859 return 0;
9860};
9861__initcall(io_uring_init);