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