f2fs: fix checkpoint=disable:%u%%
[linux/fpc-iii.git] / fs / io_uring.c
blob2698e9b08490b219f136a93fdf193bc65e88b76f
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Shared application/kernel submission and completion ring pairs, for
4 * supporting fast/efficient IO.
6 * A note on the read/write ordering memory barriers that are matched between
7 * the application and kernel side.
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.
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).
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.
30 * Also see the examples in the liburing library:
32 * git://git.kernel.dk/liburing
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.
39 * Copyright (C) 2018-2019 Jens Axboe
40 * Copyright (c) 2018-2019 Christoph Hellwig
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>
47 #include <net/compat.h>
48 #include <linux/refcount.h>
49 #include <linux/uio.h>
50 #include <linux/bits.h>
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>
61 #include <linux/kthread.h>
62 #include <linux/blkdev.h>
63 #include <linux/bvec.h>
64 #include <linux/net.h>
65 #include <net/sock.h>
66 #include <net/af_unix.h>
67 #include <net/scm.h>
68 #include <linux/anon_inodes.h>
69 #include <linux/sched/mm.h>
70 #include <linux/uaccess.h>
71 #include <linux/nospec.h>
72 #include <linux/sizes.h>
73 #include <linux/hugetlb.h>
74 #include <linux/highmem.h>
75 #include <linux/namei.h>
76 #include <linux/fsnotify.h>
77 #include <linux/fadvise.h>
78 #include <linux/eventpoll.h>
79 #include <linux/fs_struct.h>
80 #include <linux/splice.h>
81 #include <linux/task_work.h>
83 #define CREATE_TRACE_POINTS
84 #include <trace/events/io_uring.h>
86 #include <uapi/linux/io_uring.h>
88 #include "internal.h"
89 #include "io-wq.h"
91 #define IORING_MAX_ENTRIES 32768
92 #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
95 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
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)
102 struct io_uring {
103 u32 head ____cacheline_aligned_in_smp;
104 u32 tail ____cacheline_aligned_in_smp;
108 * This data is shared with the application through the mmap at offsets
109 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
111 * The offsets to the member fields are published through struct
112 * io_sqring_offsets when calling io_uring_setup.
114 struct io_rings {
116 * Head and tail offsets into the ring; the offsets need to be
117 * masked to get valid indices.
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.
123 struct io_uring sq, cq;
125 * Bitmasks to apply to head and tail offsets (constant, equals
126 * ring_entries - 1)
128 u32 sq_ring_mask, cq_ring_mask;
129 /* Ring sizes (constant, power of 2) */
130 u32 sq_ring_entries, cq_ring_entries;
132 * Number of invalid entries dropped by the kernel due to
133 * invalid index stored in array
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).
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).
143 u32 sq_dropped;
145 * Runtime flags
147 * Written by the kernel, shouldn't be modified by the
148 * application.
150 * The application needs a full memory barrier before checking
151 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
153 u32 sq_flags;
155 * Number of completion events lost because the queue was full;
156 * this should be avoided by the application by making sure
157 * there are not more requests pending than there is space in
158 * the completion queue.
160 * Written by the kernel, shouldn't be modified by the
161 * application (i.e. get number of "new events" by comparing to
162 * cached value).
164 * As completion events come in out of order this counter is not
165 * ordered with any other data.
167 u32 cq_overflow;
169 * Ring buffer of completion events.
171 * The kernel writes completion events fresh every time they are
172 * produced, so the application is allowed to modify pending
173 * entries.
175 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
178 struct io_mapped_ubuf {
179 u64 ubuf;
180 size_t len;
181 struct bio_vec *bvec;
182 unsigned int nr_bvecs;
185 struct fixed_file_table {
186 struct file **files;
189 struct fixed_file_ref_node {
190 struct percpu_ref refs;
191 struct list_head node;
192 struct list_head file_list;
193 struct fixed_file_data *file_data;
194 struct work_struct work;
197 struct fixed_file_data {
198 struct fixed_file_table *table;
199 struct io_ring_ctx *ctx;
201 struct percpu_ref *cur_refs;
202 struct percpu_ref refs;
203 struct completion done;
204 struct list_head ref_list;
205 spinlock_t lock;
208 struct io_buffer {
209 struct list_head list;
210 __u64 addr;
211 __s32 len;
212 __u16 bid;
215 struct io_ring_ctx {
216 struct {
217 struct percpu_ref refs;
218 } ____cacheline_aligned_in_smp;
220 struct {
221 unsigned int flags;
222 unsigned int compat: 1;
223 unsigned int account_mem: 1;
224 unsigned int cq_overflow_flushed: 1;
225 unsigned int drain_next: 1;
226 unsigned int eventfd_async: 1;
229 * Ring buffer of indices into array of io_uring_sqe, which is
230 * mmapped by the application using the IORING_OFF_SQES offset.
232 * This indirection could e.g. be used to assign fixed
233 * io_uring_sqe entries to operations and only submit them to
234 * the queue when needed.
236 * The kernel modifies neither the indices array nor the entries
237 * array.
239 u32 *sq_array;
240 unsigned cached_sq_head;
241 unsigned sq_entries;
242 unsigned sq_mask;
243 unsigned sq_thread_idle;
244 unsigned cached_sq_dropped;
245 atomic_t cached_cq_overflow;
246 unsigned long sq_check_overflow;
248 struct list_head defer_list;
249 struct list_head timeout_list;
250 struct list_head cq_overflow_list;
252 wait_queue_head_t inflight_wait;
253 struct io_uring_sqe *sq_sqes;
254 } ____cacheline_aligned_in_smp;
256 struct io_rings *rings;
258 /* IO offload */
259 struct io_wq *io_wq;
260 struct task_struct *sqo_thread; /* if using sq thread polling */
261 struct mm_struct *sqo_mm;
262 wait_queue_head_t sqo_wait;
265 * If used, fixed file set. Writers must ensure that ->refs is dead,
266 * readers must ensure that ->refs is alive as long as the file* is
267 * used. Only updated through io_uring_register(2).
269 struct fixed_file_data *file_data;
270 unsigned nr_user_files;
271 int ring_fd;
272 struct file *ring_file;
274 /* if used, fixed mapped user buffers */
275 unsigned nr_user_bufs;
276 struct io_mapped_ubuf *user_bufs;
278 struct user_struct *user;
280 const struct cred *creds;
282 /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */
283 struct completion *completions;
285 /* if all else fails... */
286 struct io_kiocb *fallback_req;
288 #if defined(CONFIG_UNIX)
289 struct socket *ring_sock;
290 #endif
292 struct idr io_buffer_idr;
294 struct idr personality_idr;
296 struct {
297 unsigned cached_cq_tail;
298 unsigned cq_entries;
299 unsigned cq_mask;
300 atomic_t cq_timeouts;
301 unsigned long cq_check_overflow;
302 struct wait_queue_head cq_wait;
303 struct fasync_struct *cq_fasync;
304 struct eventfd_ctx *cq_ev_fd;
305 } ____cacheline_aligned_in_smp;
307 struct {
308 struct mutex uring_lock;
309 wait_queue_head_t wait;
310 } ____cacheline_aligned_in_smp;
312 struct {
313 spinlock_t completion_lock;
316 * ->poll_list is protected by the ctx->uring_lock for
317 * io_uring instances that don't use IORING_SETUP_SQPOLL.
318 * For SQPOLL, only the single threaded io_sq_thread() will
319 * manipulate the list, hence no extra locking is needed there.
321 struct list_head poll_list;
322 struct hlist_head *cancel_hash;
323 unsigned cancel_hash_bits;
324 bool poll_multi_file;
326 spinlock_t inflight_lock;
327 struct list_head inflight_list;
328 } ____cacheline_aligned_in_smp;
330 struct work_struct exit_work;
334 * First field must be the file pointer in all the
335 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
337 struct io_poll_iocb {
338 struct file *file;
339 union {
340 struct wait_queue_head *head;
341 u64 addr;
343 __poll_t events;
344 bool done;
345 bool canceled;
346 struct wait_queue_entry wait;
349 struct io_close {
350 struct file *file;
351 struct file *put_file;
352 int fd;
355 struct io_timeout_data {
356 struct io_kiocb *req;
357 struct hrtimer timer;
358 struct timespec64 ts;
359 enum hrtimer_mode mode;
362 struct io_accept {
363 struct file *file;
364 struct sockaddr __user *addr;
365 int __user *addr_len;
366 int flags;
367 unsigned long nofile;
370 struct io_sync {
371 struct file *file;
372 loff_t len;
373 loff_t off;
374 int flags;
375 int mode;
378 struct io_cancel {
379 struct file *file;
380 u64 addr;
383 struct io_timeout {
384 struct file *file;
385 u64 addr;
386 int flags;
387 u32 count;
390 struct io_rw {
391 /* NOTE: kiocb has the file as the first member, so don't do it here */
392 struct kiocb kiocb;
393 u64 addr;
394 u64 len;
397 struct io_connect {
398 struct file *file;
399 struct sockaddr __user *addr;
400 int addr_len;
403 struct io_sr_msg {
404 struct file *file;
405 union {
406 struct user_msghdr __user *msg;
407 void __user *buf;
409 int msg_flags;
410 int bgid;
411 size_t len;
412 struct io_buffer *kbuf;
415 struct io_open {
416 struct file *file;
417 int dfd;
418 union {
419 unsigned mask;
421 struct filename *filename;
422 struct statx __user *buffer;
423 struct open_how how;
424 unsigned long nofile;
427 struct io_files_update {
428 struct file *file;
429 u64 arg;
430 u32 nr_args;
431 u32 offset;
434 struct io_fadvise {
435 struct file *file;
436 u64 offset;
437 u32 len;
438 u32 advice;
441 struct io_madvise {
442 struct file *file;
443 u64 addr;
444 u32 len;
445 u32 advice;
448 struct io_epoll {
449 struct file *file;
450 int epfd;
451 int op;
452 int fd;
453 struct epoll_event event;
456 struct io_splice {
457 struct file *file_out;
458 struct file *file_in;
459 loff_t off_out;
460 loff_t off_in;
461 u64 len;
462 unsigned int flags;
465 struct io_provide_buf {
466 struct file *file;
467 __u64 addr;
468 __s32 len;
469 __u32 bgid;
470 __u16 nbufs;
471 __u16 bid;
474 struct io_async_connect {
475 struct sockaddr_storage address;
478 struct io_async_msghdr {
479 struct iovec fast_iov[UIO_FASTIOV];
480 struct iovec *iov;
481 struct sockaddr __user *uaddr;
482 struct msghdr msg;
483 struct sockaddr_storage addr;
486 struct io_async_rw {
487 struct iovec fast_iov[UIO_FASTIOV];
488 struct iovec *iov;
489 ssize_t nr_segs;
490 ssize_t size;
493 struct io_async_ctx {
494 union {
495 struct io_async_rw rw;
496 struct io_async_msghdr msg;
497 struct io_async_connect connect;
498 struct io_timeout_data timeout;
502 enum {
503 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
504 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
505 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
506 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
507 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
508 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
510 REQ_F_LINK_HEAD_BIT,
511 REQ_F_LINK_NEXT_BIT,
512 REQ_F_FAIL_LINK_BIT,
513 REQ_F_INFLIGHT_BIT,
514 REQ_F_CUR_POS_BIT,
515 REQ_F_NOWAIT_BIT,
516 REQ_F_IOPOLL_COMPLETED_BIT,
517 REQ_F_LINK_TIMEOUT_BIT,
518 REQ_F_TIMEOUT_BIT,
519 REQ_F_ISREG_BIT,
520 REQ_F_MUST_PUNT_BIT,
521 REQ_F_TIMEOUT_NOSEQ_BIT,
522 REQ_F_COMP_LOCKED_BIT,
523 REQ_F_NEED_CLEANUP_BIT,
524 REQ_F_OVERFLOW_BIT,
525 REQ_F_POLLED_BIT,
526 REQ_F_BUFFER_SELECTED_BIT,
527 REQ_F_NO_FILE_TABLE_BIT,
529 /* not a real bit, just to check we're not overflowing the space */
530 __REQ_F_LAST_BIT,
533 enum {
534 /* ctx owns file */
535 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
536 /* drain existing IO first */
537 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
538 /* linked sqes */
539 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
540 /* doesn't sever on completion < 0 */
541 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
542 /* IOSQE_ASYNC */
543 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
544 /* IOSQE_BUFFER_SELECT */
545 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
547 /* head of a link */
548 REQ_F_LINK_HEAD = BIT(REQ_F_LINK_HEAD_BIT),
549 /* already grabbed next link */
550 REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT),
551 /* fail rest of links */
552 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
553 /* on inflight list */
554 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
555 /* read/write uses file position */
556 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
557 /* must not punt to workers */
558 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
559 /* polled IO has completed */
560 REQ_F_IOPOLL_COMPLETED = BIT(REQ_F_IOPOLL_COMPLETED_BIT),
561 /* has linked timeout */
562 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
563 /* timeout request */
564 REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT),
565 /* regular file */
566 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
567 /* must be punted even for NONBLOCK */
568 REQ_F_MUST_PUNT = BIT(REQ_F_MUST_PUNT_BIT),
569 /* no timeout sequence */
570 REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT),
571 /* completion under lock */
572 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
573 /* needs cleanup */
574 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
575 /* in overflow list */
576 REQ_F_OVERFLOW = BIT(REQ_F_OVERFLOW_BIT),
577 /* already went through poll handler */
578 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
579 /* buffer already selected */
580 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
581 /* doesn't need file table for this request */
582 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
585 struct async_poll {
586 struct io_poll_iocb poll;
587 struct io_wq_work work;
591 * NOTE! Each of the iocb union members has the file pointer
592 * as the first entry in their struct definition. So you can
593 * access the file pointer through any of the sub-structs,
594 * or directly as just 'ki_filp' in this struct.
596 struct io_kiocb {
597 union {
598 struct file *file;
599 struct io_rw rw;
600 struct io_poll_iocb poll;
601 struct io_accept accept;
602 struct io_sync sync;
603 struct io_cancel cancel;
604 struct io_timeout timeout;
605 struct io_connect connect;
606 struct io_sr_msg sr_msg;
607 struct io_open open;
608 struct io_close close;
609 struct io_files_update files_update;
610 struct io_fadvise fadvise;
611 struct io_madvise madvise;
612 struct io_epoll epoll;
613 struct io_splice splice;
614 struct io_provide_buf pbuf;
617 struct io_async_ctx *io;
618 int cflags;
619 bool needs_fixed_file;
620 u8 opcode;
622 u16 buf_index;
624 struct io_ring_ctx *ctx;
625 struct list_head list;
626 unsigned int flags;
627 refcount_t refs;
628 struct task_struct *task;
629 unsigned long fsize;
630 u64 user_data;
631 u32 result;
632 u32 sequence;
634 struct list_head link_list;
636 struct list_head inflight_entry;
638 struct percpu_ref *fixed_file_refs;
640 union {
642 * Only commands that never go async can use the below fields,
643 * obviously. Right now only IORING_OP_POLL_ADD uses them, and
644 * async armed poll handlers for regular commands. The latter
645 * restore the work, if needed.
647 struct {
648 struct callback_head task_work;
649 struct hlist_node hash_node;
650 struct async_poll *apoll;
652 struct io_wq_work work;
656 #define IO_PLUG_THRESHOLD 2
657 #define IO_IOPOLL_BATCH 8
659 struct io_submit_state {
660 struct blk_plug plug;
663 * io_kiocb alloc cache
665 void *reqs[IO_IOPOLL_BATCH];
666 unsigned int free_reqs;
669 * File reference cache
671 struct file *file;
672 unsigned int fd;
673 unsigned int has_refs;
674 unsigned int used_refs;
675 unsigned int ios_left;
678 struct io_op_def {
679 /* needs req->io allocated for deferral/async */
680 unsigned async_ctx : 1;
681 /* needs current->mm setup, does mm access */
682 unsigned needs_mm : 1;
683 /* needs req->file assigned */
684 unsigned needs_file : 1;
685 /* hash wq insertion if file is a regular file */
686 unsigned hash_reg_file : 1;
687 /* unbound wq insertion if file is a non-regular file */
688 unsigned unbound_nonreg_file : 1;
689 /* opcode is not supported by this kernel */
690 unsigned not_supported : 1;
691 /* needs file table */
692 unsigned file_table : 1;
693 /* needs ->fs */
694 unsigned needs_fs : 1;
695 /* set if opcode supports polled "wait" */
696 unsigned pollin : 1;
697 unsigned pollout : 1;
698 /* op supports buffer selection */
699 unsigned buffer_select : 1;
702 static const struct io_op_def io_op_defs[] = {
703 [IORING_OP_NOP] = {},
704 [IORING_OP_READV] = {
705 .async_ctx = 1,
706 .needs_mm = 1,
707 .needs_file = 1,
708 .unbound_nonreg_file = 1,
709 .pollin = 1,
710 .buffer_select = 1,
712 [IORING_OP_WRITEV] = {
713 .async_ctx = 1,
714 .needs_mm = 1,
715 .needs_file = 1,
716 .hash_reg_file = 1,
717 .unbound_nonreg_file = 1,
718 .pollout = 1,
720 [IORING_OP_FSYNC] = {
721 .needs_file = 1,
723 [IORING_OP_READ_FIXED] = {
724 .needs_file = 1,
725 .unbound_nonreg_file = 1,
726 .pollin = 1,
728 [IORING_OP_WRITE_FIXED] = {
729 .needs_file = 1,
730 .hash_reg_file = 1,
731 .unbound_nonreg_file = 1,
732 .pollout = 1,
734 [IORING_OP_POLL_ADD] = {
735 .needs_file = 1,
736 .unbound_nonreg_file = 1,
738 [IORING_OP_POLL_REMOVE] = {},
739 [IORING_OP_SYNC_FILE_RANGE] = {
740 .needs_file = 1,
742 [IORING_OP_SENDMSG] = {
743 .async_ctx = 1,
744 .needs_mm = 1,
745 .needs_file = 1,
746 .unbound_nonreg_file = 1,
747 .needs_fs = 1,
748 .pollout = 1,
750 [IORING_OP_RECVMSG] = {
751 .async_ctx = 1,
752 .needs_mm = 1,
753 .needs_file = 1,
754 .unbound_nonreg_file = 1,
755 .needs_fs = 1,
756 .pollin = 1,
757 .buffer_select = 1,
759 [IORING_OP_TIMEOUT] = {
760 .async_ctx = 1,
761 .needs_mm = 1,
763 [IORING_OP_TIMEOUT_REMOVE] = {},
764 [IORING_OP_ACCEPT] = {
765 .needs_mm = 1,
766 .needs_file = 1,
767 .unbound_nonreg_file = 1,
768 .file_table = 1,
769 .pollin = 1,
771 [IORING_OP_ASYNC_CANCEL] = {},
772 [IORING_OP_LINK_TIMEOUT] = {
773 .async_ctx = 1,
774 .needs_mm = 1,
776 [IORING_OP_CONNECT] = {
777 .async_ctx = 1,
778 .needs_mm = 1,
779 .needs_file = 1,
780 .unbound_nonreg_file = 1,
781 .pollout = 1,
783 [IORING_OP_FALLOCATE] = {
784 .needs_file = 1,
786 [IORING_OP_OPENAT] = {
787 .file_table = 1,
788 .needs_fs = 1,
790 [IORING_OP_CLOSE] = {
791 .needs_file = 1,
792 .file_table = 1,
794 [IORING_OP_FILES_UPDATE] = {
795 .needs_mm = 1,
796 .file_table = 1,
798 [IORING_OP_STATX] = {
799 .needs_mm = 1,
800 .needs_fs = 1,
801 .file_table = 1,
803 [IORING_OP_READ] = {
804 .needs_mm = 1,
805 .needs_file = 1,
806 .unbound_nonreg_file = 1,
807 .pollin = 1,
808 .buffer_select = 1,
810 [IORING_OP_WRITE] = {
811 .needs_mm = 1,
812 .needs_file = 1,
813 .unbound_nonreg_file = 1,
814 .pollout = 1,
816 [IORING_OP_FADVISE] = {
817 .needs_file = 1,
819 [IORING_OP_MADVISE] = {
820 .needs_mm = 1,
822 [IORING_OP_SEND] = {
823 .needs_mm = 1,
824 .needs_file = 1,
825 .unbound_nonreg_file = 1,
826 .pollout = 1,
828 [IORING_OP_RECV] = {
829 .needs_mm = 1,
830 .needs_file = 1,
831 .unbound_nonreg_file = 1,
832 .pollin = 1,
833 .buffer_select = 1,
835 [IORING_OP_OPENAT2] = {
836 .file_table = 1,
837 .needs_fs = 1,
839 [IORING_OP_EPOLL_CTL] = {
840 .unbound_nonreg_file = 1,
841 .file_table = 1,
843 [IORING_OP_SPLICE] = {
844 .needs_file = 1,
845 .hash_reg_file = 1,
846 .unbound_nonreg_file = 1,
848 [IORING_OP_PROVIDE_BUFFERS] = {},
849 [IORING_OP_REMOVE_BUFFERS] = {},
852 static void io_wq_submit_work(struct io_wq_work **workptr);
853 static void io_cqring_fill_event(struct io_kiocb *req, long res);
854 static void io_put_req(struct io_kiocb *req);
855 static void __io_double_put_req(struct io_kiocb *req);
856 static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
857 static void io_queue_linked_timeout(struct io_kiocb *req);
858 static int __io_sqe_files_update(struct io_ring_ctx *ctx,
859 struct io_uring_files_update *ip,
860 unsigned nr_args);
861 static int io_grab_files(struct io_kiocb *req);
862 static void io_cleanup_req(struct io_kiocb *req);
863 static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
864 int fd, struct file **out_file, bool fixed);
865 static void __io_queue_sqe(struct io_kiocb *req,
866 const struct io_uring_sqe *sqe);
868 static struct kmem_cache *req_cachep;
870 static const struct file_operations io_uring_fops;
872 struct sock *io_uring_get_socket(struct file *file)
874 #if defined(CONFIG_UNIX)
875 if (file->f_op == &io_uring_fops) {
876 struct io_ring_ctx *ctx = file->private_data;
878 return ctx->ring_sock->sk;
880 #endif
881 return NULL;
883 EXPORT_SYMBOL(io_uring_get_socket);
885 static void io_ring_ctx_ref_free(struct percpu_ref *ref)
887 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
889 complete(&ctx->completions[0]);
892 static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
894 struct io_ring_ctx *ctx;
895 int hash_bits;
897 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
898 if (!ctx)
899 return NULL;
901 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
902 if (!ctx->fallback_req)
903 goto err;
905 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
906 if (!ctx->completions)
907 goto err;
910 * Use 5 bits less than the max cq entries, that should give us around
911 * 32 entries per hash list if totally full and uniformly spread.
913 hash_bits = ilog2(p->cq_entries);
914 hash_bits -= 5;
915 if (hash_bits <= 0)
916 hash_bits = 1;
917 ctx->cancel_hash_bits = hash_bits;
918 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
919 GFP_KERNEL);
920 if (!ctx->cancel_hash)
921 goto err;
922 __hash_init(ctx->cancel_hash, 1U << hash_bits);
924 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
925 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
926 goto err;
928 ctx->flags = p->flags;
929 init_waitqueue_head(&ctx->sqo_wait);
930 init_waitqueue_head(&ctx->cq_wait);
931 INIT_LIST_HEAD(&ctx->cq_overflow_list);
932 init_completion(&ctx->completions[0]);
933 init_completion(&ctx->completions[1]);
934 idr_init(&ctx->io_buffer_idr);
935 idr_init(&ctx->personality_idr);
936 mutex_init(&ctx->uring_lock);
937 init_waitqueue_head(&ctx->wait);
938 spin_lock_init(&ctx->completion_lock);
939 INIT_LIST_HEAD(&ctx->poll_list);
940 INIT_LIST_HEAD(&ctx->defer_list);
941 INIT_LIST_HEAD(&ctx->timeout_list);
942 init_waitqueue_head(&ctx->inflight_wait);
943 spin_lock_init(&ctx->inflight_lock);
944 INIT_LIST_HEAD(&ctx->inflight_list);
945 return ctx;
946 err:
947 if (ctx->fallback_req)
948 kmem_cache_free(req_cachep, ctx->fallback_req);
949 kfree(ctx->completions);
950 kfree(ctx->cancel_hash);
951 kfree(ctx);
952 return NULL;
955 static inline bool __req_need_defer(struct io_kiocb *req)
957 struct io_ring_ctx *ctx = req->ctx;
959 return req->sequence != ctx->cached_cq_tail
960 + atomic_read(&ctx->cached_cq_overflow);
963 static inline bool req_need_defer(struct io_kiocb *req)
965 if (unlikely(req->flags & REQ_F_IO_DRAIN))
966 return __req_need_defer(req);
968 return false;
971 static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
973 struct io_kiocb *req;
975 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
976 if (req && !req_need_defer(req)) {
977 list_del_init(&req->list);
978 return req;
981 return NULL;
984 static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
986 struct io_kiocb *req;
988 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
989 if (req) {
990 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
991 return NULL;
992 if (!__req_need_defer(req)) {
993 list_del_init(&req->list);
994 return req;
998 return NULL;
1001 static void __io_commit_cqring(struct io_ring_ctx *ctx)
1003 struct io_rings *rings = ctx->rings;
1005 /* order cqe stores with ring update */
1006 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
1008 if (wq_has_sleeper(&ctx->cq_wait)) {
1009 wake_up_interruptible(&ctx->cq_wait);
1010 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1014 static inline void io_req_work_grab_env(struct io_kiocb *req,
1015 const struct io_op_def *def)
1017 if (!req->work.mm && def->needs_mm) {
1018 mmgrab(current->mm);
1019 req->work.mm = current->mm;
1021 if (!req->work.creds)
1022 req->work.creds = get_current_cred();
1023 if (!req->work.fs && def->needs_fs) {
1024 spin_lock(&current->fs->lock);
1025 if (!current->fs->in_exec) {
1026 req->work.fs = current->fs;
1027 req->work.fs->users++;
1028 } else {
1029 req->work.flags |= IO_WQ_WORK_CANCEL;
1031 spin_unlock(&current->fs->lock);
1033 if (!req->work.task_pid)
1034 req->work.task_pid = task_pid_vnr(current);
1037 static inline void io_req_work_drop_env(struct io_kiocb *req)
1039 if (req->work.mm) {
1040 mmdrop(req->work.mm);
1041 req->work.mm = NULL;
1043 if (req->work.creds) {
1044 put_cred(req->work.creds);
1045 req->work.creds = NULL;
1047 if (req->work.fs) {
1048 struct fs_struct *fs = req->work.fs;
1050 spin_lock(&req->work.fs->lock);
1051 if (--fs->users)
1052 fs = NULL;
1053 spin_unlock(&req->work.fs->lock);
1054 if (fs)
1055 free_fs_struct(fs);
1059 static inline void io_prep_async_work(struct io_kiocb *req,
1060 struct io_kiocb **link)
1062 const struct io_op_def *def = &io_op_defs[req->opcode];
1064 if (req->flags & REQ_F_ISREG) {
1065 if (def->hash_reg_file)
1066 io_wq_hash_work(&req->work, file_inode(req->file));
1067 } else {
1068 if (def->unbound_nonreg_file)
1069 req->work.flags |= IO_WQ_WORK_UNBOUND;
1072 io_req_work_grab_env(req, def);
1074 *link = io_prep_linked_timeout(req);
1077 static inline void io_queue_async_work(struct io_kiocb *req)
1079 struct io_ring_ctx *ctx = req->ctx;
1080 struct io_kiocb *link;
1082 io_prep_async_work(req, &link);
1084 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1085 &req->work, req->flags);
1086 io_wq_enqueue(ctx->io_wq, &req->work);
1088 if (link)
1089 io_queue_linked_timeout(link);
1092 static void io_kill_timeout(struct io_kiocb *req)
1094 int ret;
1096 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
1097 if (ret != -1) {
1098 atomic_inc(&req->ctx->cq_timeouts);
1099 list_del_init(&req->list);
1100 req->flags |= REQ_F_COMP_LOCKED;
1101 io_cqring_fill_event(req, 0);
1102 io_put_req(req);
1106 static void io_kill_timeouts(struct io_ring_ctx *ctx)
1108 struct io_kiocb *req, *tmp;
1110 spin_lock_irq(&ctx->completion_lock);
1111 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
1112 io_kill_timeout(req);
1113 spin_unlock_irq(&ctx->completion_lock);
1116 static void io_commit_cqring(struct io_ring_ctx *ctx)
1118 struct io_kiocb *req;
1120 while ((req = io_get_timeout_req(ctx)) != NULL)
1121 io_kill_timeout(req);
1123 __io_commit_cqring(ctx);
1125 while ((req = io_get_deferred_req(ctx)) != NULL)
1126 io_queue_async_work(req);
1129 static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1131 struct io_rings *rings = ctx->rings;
1132 unsigned tail;
1134 tail = ctx->cached_cq_tail;
1136 * writes to the cq entry need to come after reading head; the
1137 * control dependency is enough as we're using WRITE_ONCE to
1138 * fill the cq entry
1140 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
1141 return NULL;
1143 ctx->cached_cq_tail++;
1144 return &rings->cqes[tail & ctx->cq_mask];
1147 static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1149 if (!ctx->cq_ev_fd)
1150 return false;
1151 if (!ctx->eventfd_async)
1152 return true;
1153 return io_wq_current_is_worker();
1156 static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
1158 if (waitqueue_active(&ctx->wait))
1159 wake_up(&ctx->wait);
1160 if (waitqueue_active(&ctx->sqo_wait))
1161 wake_up(&ctx->sqo_wait);
1162 if (io_should_trigger_evfd(ctx))
1163 eventfd_signal(ctx->cq_ev_fd, 1);
1166 /* Returns true if there are no backlogged entries after the flush */
1167 static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
1169 struct io_rings *rings = ctx->rings;
1170 struct io_uring_cqe *cqe;
1171 struct io_kiocb *req;
1172 unsigned long flags;
1173 LIST_HEAD(list);
1175 if (!force) {
1176 if (list_empty_careful(&ctx->cq_overflow_list))
1177 return true;
1178 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1179 rings->cq_ring_entries))
1180 return false;
1183 spin_lock_irqsave(&ctx->completion_lock, flags);
1185 /* if force is set, the ring is going away. always drop after that */
1186 if (force)
1187 ctx->cq_overflow_flushed = 1;
1189 cqe = NULL;
1190 while (!list_empty(&ctx->cq_overflow_list)) {
1191 cqe = io_get_cqring(ctx);
1192 if (!cqe && !force)
1193 break;
1195 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1196 list);
1197 list_move(&req->list, &list);
1198 req->flags &= ~REQ_F_OVERFLOW;
1199 if (cqe) {
1200 WRITE_ONCE(cqe->user_data, req->user_data);
1201 WRITE_ONCE(cqe->res, req->result);
1202 WRITE_ONCE(cqe->flags, req->cflags);
1203 } else {
1204 WRITE_ONCE(ctx->rings->cq_overflow,
1205 atomic_inc_return(&ctx->cached_cq_overflow));
1209 io_commit_cqring(ctx);
1210 if (cqe) {
1211 clear_bit(0, &ctx->sq_check_overflow);
1212 clear_bit(0, &ctx->cq_check_overflow);
1214 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1215 io_cqring_ev_posted(ctx);
1217 while (!list_empty(&list)) {
1218 req = list_first_entry(&list, struct io_kiocb, list);
1219 list_del(&req->list);
1220 io_put_req(req);
1223 return cqe != NULL;
1226 static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
1228 struct io_ring_ctx *ctx = req->ctx;
1229 struct io_uring_cqe *cqe;
1231 trace_io_uring_complete(ctx, req->user_data, res);
1234 * If we can't get a cq entry, userspace overflowed the
1235 * submission (by quite a lot). Increment the overflow count in
1236 * the ring.
1238 cqe = io_get_cqring(ctx);
1239 if (likely(cqe)) {
1240 WRITE_ONCE(cqe->user_data, req->user_data);
1241 WRITE_ONCE(cqe->res, res);
1242 WRITE_ONCE(cqe->flags, cflags);
1243 } else if (ctx->cq_overflow_flushed) {
1244 WRITE_ONCE(ctx->rings->cq_overflow,
1245 atomic_inc_return(&ctx->cached_cq_overflow));
1246 } else {
1247 if (list_empty(&ctx->cq_overflow_list)) {
1248 set_bit(0, &ctx->sq_check_overflow);
1249 set_bit(0, &ctx->cq_check_overflow);
1251 req->flags |= REQ_F_OVERFLOW;
1252 refcount_inc(&req->refs);
1253 req->result = res;
1254 req->cflags = cflags;
1255 list_add_tail(&req->list, &ctx->cq_overflow_list);
1259 static void io_cqring_fill_event(struct io_kiocb *req, long res)
1261 __io_cqring_fill_event(req, res, 0);
1264 static void __io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
1266 struct io_ring_ctx *ctx = req->ctx;
1267 unsigned long flags;
1269 spin_lock_irqsave(&ctx->completion_lock, flags);
1270 __io_cqring_fill_event(req, res, cflags);
1271 io_commit_cqring(ctx);
1272 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1274 io_cqring_ev_posted(ctx);
1277 static void io_cqring_add_event(struct io_kiocb *req, long res)
1279 __io_cqring_add_event(req, res, 0);
1282 static inline bool io_is_fallback_req(struct io_kiocb *req)
1284 return req == (struct io_kiocb *)
1285 ((unsigned long) req->ctx->fallback_req & ~1UL);
1288 static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1290 struct io_kiocb *req;
1292 req = ctx->fallback_req;
1293 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req))
1294 return req;
1296 return NULL;
1299 static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
1300 struct io_submit_state *state)
1302 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
1303 struct io_kiocb *req;
1305 if (!state) {
1306 req = kmem_cache_alloc(req_cachep, gfp);
1307 if (unlikely(!req))
1308 goto fallback;
1309 } else if (!state->free_reqs) {
1310 size_t sz;
1311 int ret;
1313 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
1314 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1317 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1318 * retry single alloc to be on the safe side.
1320 if (unlikely(ret <= 0)) {
1321 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1322 if (!state->reqs[0])
1323 goto fallback;
1324 ret = 1;
1326 state->free_reqs = ret - 1;
1327 req = state->reqs[ret - 1];
1328 } else {
1329 state->free_reqs--;
1330 req = state->reqs[state->free_reqs];
1333 return req;
1334 fallback:
1335 return io_get_fallback_req(ctx);
1338 static inline void io_put_file(struct io_kiocb *req, struct file *file,
1339 bool fixed)
1341 if (fixed)
1342 percpu_ref_put(req->fixed_file_refs);
1343 else
1344 fput(file);
1347 static void __io_req_aux_free(struct io_kiocb *req)
1349 if (req->flags & REQ_F_NEED_CLEANUP)
1350 io_cleanup_req(req);
1352 kfree(req->io);
1353 if (req->file)
1354 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
1355 if (req->task)
1356 put_task_struct(req->task);
1358 io_req_work_drop_env(req);
1361 static void __io_free_req(struct io_kiocb *req)
1363 __io_req_aux_free(req);
1365 if (req->flags & REQ_F_INFLIGHT) {
1366 struct io_ring_ctx *ctx = req->ctx;
1367 unsigned long flags;
1369 spin_lock_irqsave(&ctx->inflight_lock, flags);
1370 list_del(&req->inflight_entry);
1371 if (waitqueue_active(&ctx->inflight_wait))
1372 wake_up(&ctx->inflight_wait);
1373 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1376 percpu_ref_put(&req->ctx->refs);
1377 if (likely(!io_is_fallback_req(req)))
1378 kmem_cache_free(req_cachep, req);
1379 else
1380 clear_bit_unlock(0, (unsigned long *) &req->ctx->fallback_req);
1383 struct req_batch {
1384 void *reqs[IO_IOPOLL_BATCH];
1385 int to_free;
1386 int need_iter;
1389 static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1391 if (!rb->to_free)
1392 return;
1393 if (rb->need_iter) {
1394 int i, inflight = 0;
1395 unsigned long flags;
1397 for (i = 0; i < rb->to_free; i++) {
1398 struct io_kiocb *req = rb->reqs[i];
1400 if (req->flags & REQ_F_INFLIGHT)
1401 inflight++;
1402 __io_req_aux_free(req);
1404 if (!inflight)
1405 goto do_free;
1407 spin_lock_irqsave(&ctx->inflight_lock, flags);
1408 for (i = 0; i < rb->to_free; i++) {
1409 struct io_kiocb *req = rb->reqs[i];
1411 if (req->flags & REQ_F_INFLIGHT) {
1412 list_del(&req->inflight_entry);
1413 if (!--inflight)
1414 break;
1417 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1419 if (waitqueue_active(&ctx->inflight_wait))
1420 wake_up(&ctx->inflight_wait);
1422 do_free:
1423 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
1424 percpu_ref_put_many(&ctx->refs, rb->to_free);
1425 rb->to_free = rb->need_iter = 0;
1428 static bool io_link_cancel_timeout(struct io_kiocb *req)
1430 struct io_ring_ctx *ctx = req->ctx;
1431 int ret;
1433 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
1434 if (ret != -1) {
1435 io_cqring_fill_event(req, -ECANCELED);
1436 io_commit_cqring(ctx);
1437 req->flags &= ~REQ_F_LINK_HEAD;
1438 io_put_req(req);
1439 return true;
1442 return false;
1445 static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
1447 struct io_ring_ctx *ctx = req->ctx;
1448 bool wake_ev = false;
1450 /* Already got next link */
1451 if (req->flags & REQ_F_LINK_NEXT)
1452 return;
1455 * The list should never be empty when we are called here. But could
1456 * potentially happen if the chain is messed up, check to be on the
1457 * safe side.
1459 while (!list_empty(&req->link_list)) {
1460 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1461 struct io_kiocb, link_list);
1463 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1464 (nxt->flags & REQ_F_TIMEOUT))) {
1465 list_del_init(&nxt->link_list);
1466 wake_ev |= io_link_cancel_timeout(nxt);
1467 req->flags &= ~REQ_F_LINK_TIMEOUT;
1468 continue;
1471 list_del_init(&req->link_list);
1472 if (!list_empty(&nxt->link_list))
1473 nxt->flags |= REQ_F_LINK_HEAD;
1474 *nxtptr = nxt;
1475 break;
1478 req->flags |= REQ_F_LINK_NEXT;
1479 if (wake_ev)
1480 io_cqring_ev_posted(ctx);
1484 * Called if REQ_F_LINK_HEAD is set, and we fail the head request
1486 static void io_fail_links(struct io_kiocb *req)
1488 struct io_ring_ctx *ctx = req->ctx;
1489 unsigned long flags;
1491 spin_lock_irqsave(&ctx->completion_lock, flags);
1493 while (!list_empty(&req->link_list)) {
1494 struct io_kiocb *link = list_first_entry(&req->link_list,
1495 struct io_kiocb, link_list);
1497 list_del_init(&link->link_list);
1498 trace_io_uring_fail_link(req, link);
1500 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
1501 link->opcode == IORING_OP_LINK_TIMEOUT) {
1502 io_link_cancel_timeout(link);
1503 } else {
1504 io_cqring_fill_event(link, -ECANCELED);
1505 __io_double_put_req(link);
1507 req->flags &= ~REQ_F_LINK_TIMEOUT;
1510 io_commit_cqring(ctx);
1511 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1512 io_cqring_ev_posted(ctx);
1515 static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
1517 if (likely(!(req->flags & REQ_F_LINK_HEAD)))
1518 return;
1521 * If LINK is set, we have dependent requests in this chain. If we
1522 * didn't fail this request, queue the first one up, moving any other
1523 * dependencies to the next request. In case of failure, fail the rest
1524 * of the chain.
1526 if (req->flags & REQ_F_FAIL_LINK) {
1527 io_fail_links(req);
1528 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1529 REQ_F_LINK_TIMEOUT) {
1530 struct io_ring_ctx *ctx = req->ctx;
1531 unsigned long flags;
1534 * If this is a timeout link, we could be racing with the
1535 * timeout timer. Grab the completion lock for this case to
1536 * protect against that.
1538 spin_lock_irqsave(&ctx->completion_lock, flags);
1539 io_req_link_next(req, nxt);
1540 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1541 } else {
1542 io_req_link_next(req, nxt);
1546 static void io_free_req(struct io_kiocb *req)
1548 struct io_kiocb *nxt = NULL;
1550 io_req_find_next(req, &nxt);
1551 __io_free_req(req);
1553 if (nxt)
1554 io_queue_async_work(nxt);
1557 static void io_link_work_cb(struct io_wq_work **workptr)
1559 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
1560 struct io_kiocb *link;
1562 link = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1563 io_queue_linked_timeout(link);
1564 io_wq_submit_work(workptr);
1567 static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
1569 struct io_kiocb *link;
1570 const struct io_op_def *def = &io_op_defs[nxt->opcode];
1572 if ((nxt->flags & REQ_F_ISREG) && def->hash_reg_file)
1573 io_wq_hash_work(&nxt->work, file_inode(nxt->file));
1575 *workptr = &nxt->work;
1576 link = io_prep_linked_timeout(nxt);
1577 if (link)
1578 nxt->work.func = io_link_work_cb;
1582 * Drop reference to request, return next in chain (if there is one) if this
1583 * was the last reference to this request.
1585 __attribute__((nonnull))
1586 static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
1588 if (refcount_dec_and_test(&req->refs)) {
1589 io_req_find_next(req, nxtptr);
1590 __io_free_req(req);
1594 static void io_put_req(struct io_kiocb *req)
1596 if (refcount_dec_and_test(&req->refs))
1597 io_free_req(req);
1600 static void io_steal_work(struct io_kiocb *req,
1601 struct io_wq_work **workptr)
1604 * It's in an io-wq worker, so there always should be at least
1605 * one reference, which will be dropped in io_put_work() just
1606 * after the current handler returns.
1608 * It also means, that if the counter dropped to 1, then there is
1609 * no asynchronous users left, so it's safe to steal the next work.
1611 if (refcount_read(&req->refs) == 1) {
1612 struct io_kiocb *nxt = NULL;
1614 io_req_find_next(req, &nxt);
1615 if (nxt)
1616 io_wq_assign_next(workptr, nxt);
1621 * Must only be used if we don't need to care about links, usually from
1622 * within the completion handling itself.
1624 static void __io_double_put_req(struct io_kiocb *req)
1626 /* drop both submit and complete references */
1627 if (refcount_sub_and_test(2, &req->refs))
1628 __io_free_req(req);
1631 static void io_double_put_req(struct io_kiocb *req)
1633 /* drop both submit and complete references */
1634 if (refcount_sub_and_test(2, &req->refs))
1635 io_free_req(req);
1638 static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
1640 struct io_rings *rings = ctx->rings;
1642 if (test_bit(0, &ctx->cq_check_overflow)) {
1644 * noflush == true is from the waitqueue handler, just ensure
1645 * we wake up the task, and the next invocation will flush the
1646 * entries. We cannot safely to it from here.
1648 if (noflush && !list_empty(&ctx->cq_overflow_list))
1649 return -1U;
1651 io_cqring_overflow_flush(ctx, false);
1654 /* See comment at the top of this file */
1655 smp_rmb();
1656 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
1659 static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1661 struct io_rings *rings = ctx->rings;
1663 /* make sure SQ entry isn't read before tail */
1664 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1667 static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
1669 if ((req->flags & REQ_F_LINK_HEAD) || io_is_fallback_req(req))
1670 return false;
1672 if (req->file || req->io)
1673 rb->need_iter++;
1675 rb->reqs[rb->to_free++] = req;
1676 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1677 io_free_req_many(req->ctx, rb);
1678 return true;
1681 static int io_put_kbuf(struct io_kiocb *req)
1683 struct io_buffer *kbuf;
1684 int cflags;
1686 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
1687 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
1688 cflags |= IORING_CQE_F_BUFFER;
1689 req->rw.addr = 0;
1690 kfree(kbuf);
1691 return cflags;
1695 * Find and free completed poll iocbs
1697 static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1698 struct list_head *done)
1700 struct req_batch rb;
1701 struct io_kiocb *req;
1703 rb.to_free = rb.need_iter = 0;
1704 while (!list_empty(done)) {
1705 int cflags = 0;
1707 req = list_first_entry(done, struct io_kiocb, list);
1708 list_del(&req->list);
1710 if (req->flags & REQ_F_BUFFER_SELECTED)
1711 cflags = io_put_kbuf(req);
1713 __io_cqring_fill_event(req, req->result, cflags);
1714 (*nr_events)++;
1716 if (refcount_dec_and_test(&req->refs) &&
1717 !io_req_multi_free(&rb, req))
1718 io_free_req(req);
1721 io_commit_cqring(ctx);
1722 if (ctx->flags & IORING_SETUP_SQPOLL)
1723 io_cqring_ev_posted(ctx);
1724 io_free_req_many(ctx, &rb);
1727 static void io_iopoll_queue(struct list_head *again)
1729 struct io_kiocb *req;
1731 do {
1732 req = list_first_entry(again, struct io_kiocb, list);
1733 list_del(&req->list);
1734 refcount_inc(&req->refs);
1735 io_queue_async_work(req);
1736 } while (!list_empty(again));
1739 static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1740 long min)
1742 struct io_kiocb *req, *tmp;
1743 LIST_HEAD(done);
1744 LIST_HEAD(again);
1745 bool spin;
1746 int ret;
1749 * Only spin for completions if we don't have multiple devices hanging
1750 * off our complete list, and we're under the requested amount.
1752 spin = !ctx->poll_multi_file && *nr_events < min;
1754 ret = 0;
1755 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
1756 struct kiocb *kiocb = &req->rw.kiocb;
1759 * Move completed and retryable entries to our local lists.
1760 * If we find a request that requires polling, break out
1761 * and complete those lists first, if we have entries there.
1763 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1764 list_move_tail(&req->list, &done);
1765 continue;
1767 if (!list_empty(&done))
1768 break;
1770 if (req->result == -EAGAIN) {
1771 list_move_tail(&req->list, &again);
1772 continue;
1774 if (!list_empty(&again))
1775 break;
1777 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1778 if (ret < 0)
1779 break;
1781 if (ret && spin)
1782 spin = false;
1783 ret = 0;
1786 if (!list_empty(&done))
1787 io_iopoll_complete(ctx, nr_events, &done);
1789 if (!list_empty(&again))
1790 io_iopoll_queue(&again);
1792 return ret;
1796 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
1797 * non-spinning poll check - we'll still enter the driver poll loop, but only
1798 * as a non-spinning completion check.
1800 static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1801 long min)
1803 while (!list_empty(&ctx->poll_list) && !need_resched()) {
1804 int ret;
1806 ret = io_do_iopoll(ctx, nr_events, min);
1807 if (ret < 0)
1808 return ret;
1809 if (!min || *nr_events >= min)
1810 return 0;
1813 return 1;
1817 * We can't just wait for polled events to come to us, we have to actively
1818 * find and complete them.
1820 static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1822 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1823 return;
1825 mutex_lock(&ctx->uring_lock);
1826 while (!list_empty(&ctx->poll_list)) {
1827 unsigned int nr_events = 0;
1829 io_iopoll_getevents(ctx, &nr_events, 1);
1832 * Ensure we allow local-to-the-cpu processing to take place,
1833 * in this case we need to ensure that we reap all events.
1835 cond_resched();
1837 mutex_unlock(&ctx->uring_lock);
1840 static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1841 long min)
1843 int iters = 0, ret = 0;
1846 * We disallow the app entering submit/complete with polling, but we
1847 * still need to lock the ring to prevent racing with polled issue
1848 * that got punted to a workqueue.
1850 mutex_lock(&ctx->uring_lock);
1851 do {
1852 int tmin = 0;
1855 * Don't enter poll loop if we already have events pending.
1856 * If we do, we can potentially be spinning for commands that
1857 * already triggered a CQE (eg in error).
1859 if (io_cqring_events(ctx, false))
1860 break;
1863 * If a submit got punted to a workqueue, we can have the
1864 * application entering polling for a command before it gets
1865 * issued. That app will hold the uring_lock for the duration
1866 * of the poll right here, so we need to take a breather every
1867 * now and then to ensure that the issue has a chance to add
1868 * the poll to the issued list. Otherwise we can spin here
1869 * forever, while the workqueue is stuck trying to acquire the
1870 * very same mutex.
1872 if (!(++iters & 7)) {
1873 mutex_unlock(&ctx->uring_lock);
1874 mutex_lock(&ctx->uring_lock);
1877 if (*nr_events < min)
1878 tmin = min - *nr_events;
1880 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1881 if (ret <= 0)
1882 break;
1883 ret = 0;
1884 } while (min && !*nr_events && !need_resched());
1886 mutex_unlock(&ctx->uring_lock);
1887 return ret;
1890 static void kiocb_end_write(struct io_kiocb *req)
1893 * Tell lockdep we inherited freeze protection from submission
1894 * thread.
1896 if (req->flags & REQ_F_ISREG) {
1897 struct inode *inode = file_inode(req->file);
1899 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
1901 file_end_write(req->file);
1904 static inline void req_set_fail_links(struct io_kiocb *req)
1906 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1907 req->flags |= REQ_F_FAIL_LINK;
1910 static void io_complete_rw_common(struct kiocb *kiocb, long res)
1912 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
1913 int cflags = 0;
1915 if (kiocb->ki_flags & IOCB_WRITE)
1916 kiocb_end_write(req);
1918 if (res != req->result)
1919 req_set_fail_links(req);
1920 if (req->flags & REQ_F_BUFFER_SELECTED)
1921 cflags = io_put_kbuf(req);
1922 __io_cqring_add_event(req, res, cflags);
1925 static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1927 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
1929 io_complete_rw_common(kiocb, res);
1930 io_put_req(req);
1933 static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1935 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
1937 if (kiocb->ki_flags & IOCB_WRITE)
1938 kiocb_end_write(req);
1940 if (res != req->result)
1941 req_set_fail_links(req);
1942 req->result = res;
1943 if (res != -EAGAIN)
1944 req->flags |= REQ_F_IOPOLL_COMPLETED;
1948 * After the iocb has been issued, it's safe to be found on the poll list.
1949 * Adding the kiocb to the list AFTER submission ensures that we don't
1950 * find it from a io_iopoll_getevents() thread before the issuer is done
1951 * accessing the kiocb cookie.
1953 static void io_iopoll_req_issued(struct io_kiocb *req)
1955 struct io_ring_ctx *ctx = req->ctx;
1958 * Track whether we have multiple files in our lists. This will impact
1959 * how we do polling eventually, not spinning if we're on potentially
1960 * different devices.
1962 if (list_empty(&ctx->poll_list)) {
1963 ctx->poll_multi_file = false;
1964 } else if (!ctx->poll_multi_file) {
1965 struct io_kiocb *list_req;
1967 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1968 list);
1969 if (list_req->file != req->file)
1970 ctx->poll_multi_file = true;
1974 * For fast devices, IO may have already completed. If it has, add
1975 * it to the front so we find it first.
1977 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1978 list_add(&req->list, &ctx->poll_list);
1979 else
1980 list_add_tail(&req->list, &ctx->poll_list);
1982 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
1983 wq_has_sleeper(&ctx->sqo_wait))
1984 wake_up(&ctx->sqo_wait);
1987 static void io_file_put(struct io_submit_state *state)
1989 if (state->file) {
1990 int diff = state->has_refs - state->used_refs;
1992 if (diff)
1993 fput_many(state->file, diff);
1994 state->file = NULL;
1999 * Get as many references to a file as we have IOs left in this submission,
2000 * assuming most submissions are for one file, or at least that each file
2001 * has more than one submission.
2003 static struct file *__io_file_get(struct io_submit_state *state, int fd)
2005 if (!state)
2006 return fget(fd);
2008 if (state->file) {
2009 if (state->fd == fd) {
2010 state->used_refs++;
2011 state->ios_left--;
2012 return state->file;
2014 io_file_put(state);
2016 state->file = fget_many(fd, state->ios_left);
2017 if (!state->file)
2018 return NULL;
2020 state->fd = fd;
2021 state->has_refs = state->ios_left;
2022 state->used_refs = 1;
2023 state->ios_left--;
2024 return state->file;
2028 * If we tracked the file through the SCM inflight mechanism, we could support
2029 * any file. For now, just ensure that anything potentially problematic is done
2030 * inline.
2032 static bool io_file_supports_async(struct file *file, int rw)
2034 umode_t mode = file_inode(file)->i_mode;
2036 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
2037 return true;
2038 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
2039 return true;
2041 /* any ->read/write should understand O_NONBLOCK */
2042 if (file->f_flags & O_NONBLOCK)
2043 return true;
2045 if (!(file->f_mode & FMODE_NOWAIT))
2046 return false;
2048 if (rw == READ)
2049 return file->f_op->read_iter != NULL;
2051 return file->f_op->write_iter != NULL;
2054 static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2055 bool force_nonblock)
2057 struct io_ring_ctx *ctx = req->ctx;
2058 struct kiocb *kiocb = &req->rw.kiocb;
2059 unsigned ioprio;
2060 int ret;
2062 if (S_ISREG(file_inode(req->file)->i_mode))
2063 req->flags |= REQ_F_ISREG;
2065 kiocb->ki_pos = READ_ONCE(sqe->off);
2066 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2067 req->flags |= REQ_F_CUR_POS;
2068 kiocb->ki_pos = req->file->f_pos;
2070 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
2071 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2072 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2073 if (unlikely(ret))
2074 return ret;
2076 ioprio = READ_ONCE(sqe->ioprio);
2077 if (ioprio) {
2078 ret = ioprio_check_cap(ioprio);
2079 if (ret)
2080 return ret;
2082 kiocb->ki_ioprio = ioprio;
2083 } else
2084 kiocb->ki_ioprio = get_current_ioprio();
2086 /* don't allow async punt if RWF_NOWAIT was requested */
2087 if (kiocb->ki_flags & IOCB_NOWAIT)
2088 req->flags |= REQ_F_NOWAIT;
2090 if (force_nonblock)
2091 kiocb->ki_flags |= IOCB_NOWAIT;
2093 if (ctx->flags & IORING_SETUP_IOPOLL) {
2094 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2095 !kiocb->ki_filp->f_op->iopoll)
2096 return -EOPNOTSUPP;
2098 kiocb->ki_flags |= IOCB_HIPRI;
2099 kiocb->ki_complete = io_complete_rw_iopoll;
2100 req->result = 0;
2101 } else {
2102 if (kiocb->ki_flags & IOCB_HIPRI)
2103 return -EINVAL;
2104 kiocb->ki_complete = io_complete_rw;
2107 req->rw.addr = READ_ONCE(sqe->addr);
2108 req->rw.len = READ_ONCE(sqe->len);
2109 req->buf_index = READ_ONCE(sqe->buf_index);
2110 return 0;
2113 static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2115 switch (ret) {
2116 case -EIOCBQUEUED:
2117 break;
2118 case -ERESTARTSYS:
2119 case -ERESTARTNOINTR:
2120 case -ERESTARTNOHAND:
2121 case -ERESTART_RESTARTBLOCK:
2123 * We can't just restart the syscall, since previously
2124 * submitted sqes may already be in progress. Just fail this
2125 * IO with EINTR.
2127 ret = -EINTR;
2128 /* fall through */
2129 default:
2130 kiocb->ki_complete(kiocb, ret, 0);
2134 static void kiocb_done(struct kiocb *kiocb, ssize_t ret)
2136 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
2138 if (req->flags & REQ_F_CUR_POS)
2139 req->file->f_pos = kiocb->ki_pos;
2140 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
2141 io_complete_rw(kiocb, ret, 0);
2142 else
2143 io_rw_done(kiocb, ret);
2146 static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
2147 struct iov_iter *iter)
2149 struct io_ring_ctx *ctx = req->ctx;
2150 size_t len = req->rw.len;
2151 struct io_mapped_ubuf *imu;
2152 u16 index, buf_index;
2153 size_t offset;
2154 u64 buf_addr;
2156 /* attempt to use fixed buffers without having provided iovecs */
2157 if (unlikely(!ctx->user_bufs))
2158 return -EFAULT;
2160 buf_index = req->buf_index;
2161 if (unlikely(buf_index >= ctx->nr_user_bufs))
2162 return -EFAULT;
2164 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2165 imu = &ctx->user_bufs[index];
2166 buf_addr = req->rw.addr;
2168 /* overflow */
2169 if (buf_addr + len < buf_addr)
2170 return -EFAULT;
2171 /* not inside the mapped region */
2172 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2173 return -EFAULT;
2176 * May not be a start of buffer, set size appropriately
2177 * and advance us to the beginning.
2179 offset = buf_addr - imu->ubuf;
2180 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
2182 if (offset) {
2184 * Don't use iov_iter_advance() here, as it's really slow for
2185 * using the latter parts of a big fixed buffer - it iterates
2186 * over each segment manually. We can cheat a bit here, because
2187 * we know that:
2189 * 1) it's a BVEC iter, we set it up
2190 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2191 * first and last bvec
2193 * So just find our index, and adjust the iterator afterwards.
2194 * If the offset is within the first bvec (or the whole first
2195 * bvec, just use iov_iter_advance(). This makes it easier
2196 * since we can just skip the first segment, which may not
2197 * be PAGE_SIZE aligned.
2199 const struct bio_vec *bvec = imu->bvec;
2201 if (offset <= bvec->bv_len) {
2202 iov_iter_advance(iter, offset);
2203 } else {
2204 unsigned long seg_skip;
2206 /* skip first vec */
2207 offset -= bvec->bv_len;
2208 seg_skip = 1 + (offset >> PAGE_SHIFT);
2210 iter->bvec = bvec + seg_skip;
2211 iter->nr_segs -= seg_skip;
2212 iter->count -= bvec->bv_len + offset;
2213 iter->iov_offset = offset & ~PAGE_MASK;
2217 return len;
2220 static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2222 if (needs_lock)
2223 mutex_unlock(&ctx->uring_lock);
2226 static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2229 * "Normal" inline submissions always hold the uring_lock, since we
2230 * grab it from the system call. Same is true for the SQPOLL offload.
2231 * The only exception is when we've detached the request and issue it
2232 * from an async worker thread, grab the lock for that case.
2234 if (needs_lock)
2235 mutex_lock(&ctx->uring_lock);
2238 static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2239 int bgid, struct io_buffer *kbuf,
2240 bool needs_lock)
2242 struct io_buffer *head;
2244 if (req->flags & REQ_F_BUFFER_SELECTED)
2245 return kbuf;
2247 io_ring_submit_lock(req->ctx, needs_lock);
2249 lockdep_assert_held(&req->ctx->uring_lock);
2251 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2252 if (head) {
2253 if (!list_empty(&head->list)) {
2254 kbuf = list_last_entry(&head->list, struct io_buffer,
2255 list);
2256 list_del(&kbuf->list);
2257 } else {
2258 kbuf = head;
2259 idr_remove(&req->ctx->io_buffer_idr, bgid);
2261 if (*len > kbuf->len)
2262 *len = kbuf->len;
2263 } else {
2264 kbuf = ERR_PTR(-ENOBUFS);
2267 io_ring_submit_unlock(req->ctx, needs_lock);
2269 return kbuf;
2272 static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2273 bool needs_lock)
2275 struct io_buffer *kbuf;
2276 u16 bgid;
2278 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2279 bgid = req->buf_index;
2280 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2281 if (IS_ERR(kbuf))
2282 return kbuf;
2283 req->rw.addr = (u64) (unsigned long) kbuf;
2284 req->flags |= REQ_F_BUFFER_SELECTED;
2285 return u64_to_user_ptr(kbuf->addr);
2288 #ifdef CONFIG_COMPAT
2289 static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2290 bool needs_lock)
2292 struct compat_iovec __user *uiov;
2293 compat_ssize_t clen;
2294 void __user *buf;
2295 ssize_t len;
2297 uiov = u64_to_user_ptr(req->rw.addr);
2298 if (!access_ok(uiov, sizeof(*uiov)))
2299 return -EFAULT;
2300 if (__get_user(clen, &uiov->iov_len))
2301 return -EFAULT;
2302 if (clen < 0)
2303 return -EINVAL;
2305 len = clen;
2306 buf = io_rw_buffer_select(req, &len, needs_lock);
2307 if (IS_ERR(buf))
2308 return PTR_ERR(buf);
2309 iov[0].iov_base = buf;
2310 iov[0].iov_len = (compat_size_t) len;
2311 return 0;
2313 #endif
2315 static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2316 bool needs_lock)
2318 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2319 void __user *buf;
2320 ssize_t len;
2322 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2323 return -EFAULT;
2325 len = iov[0].iov_len;
2326 if (len < 0)
2327 return -EINVAL;
2328 buf = io_rw_buffer_select(req, &len, needs_lock);
2329 if (IS_ERR(buf))
2330 return PTR_ERR(buf);
2331 iov[0].iov_base = buf;
2332 iov[0].iov_len = len;
2333 return 0;
2336 static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2337 bool needs_lock)
2339 if (req->flags & REQ_F_BUFFER_SELECTED) {
2340 struct io_buffer *kbuf;
2342 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2343 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
2344 iov[0].iov_len = kbuf->len;
2345 return 0;
2347 if (!req->rw.len)
2348 return 0;
2349 else if (req->rw.len > 1)
2350 return -EINVAL;
2352 #ifdef CONFIG_COMPAT
2353 if (req->ctx->compat)
2354 return io_compat_import(req, iov, needs_lock);
2355 #endif
2357 return __io_iov_buffer_select(req, iov, needs_lock);
2360 static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
2361 struct iovec **iovec, struct iov_iter *iter,
2362 bool needs_lock)
2364 void __user *buf = u64_to_user_ptr(req->rw.addr);
2365 size_t sqe_len = req->rw.len;
2366 ssize_t ret;
2367 u8 opcode;
2369 opcode = req->opcode;
2370 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
2371 *iovec = NULL;
2372 return io_import_fixed(req, rw, iter);
2375 /* buffer index only valid with fixed read/write, or buffer select */
2376 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
2377 return -EINVAL;
2379 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
2380 if (req->flags & REQ_F_BUFFER_SELECT) {
2381 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
2382 if (IS_ERR(buf)) {
2383 *iovec = NULL;
2384 return PTR_ERR(buf);
2386 req->rw.len = sqe_len;
2389 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2390 *iovec = NULL;
2391 return ret < 0 ? ret : sqe_len;
2394 if (req->io) {
2395 struct io_async_rw *iorw = &req->io->rw;
2397 *iovec = iorw->iov;
2398 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2399 if (iorw->iov == iorw->fast_iov)
2400 *iovec = NULL;
2401 return iorw->size;
2404 if (req->flags & REQ_F_BUFFER_SELECT) {
2405 ret = io_iov_buffer_select(req, *iovec, needs_lock);
2406 if (!ret) {
2407 ret = (*iovec)->iov_len;
2408 iov_iter_init(iter, rw, *iovec, 1, ret);
2410 *iovec = NULL;
2411 return ret;
2414 #ifdef CONFIG_COMPAT
2415 if (req->ctx->compat)
2416 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2417 iovec, iter);
2418 #endif
2420 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2424 * For files that don't have ->read_iter() and ->write_iter(), handle them
2425 * by looping over ->read() or ->write() manually.
2427 static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2428 struct iov_iter *iter)
2430 ssize_t ret = 0;
2433 * Don't support polled IO through this interface, and we can't
2434 * support non-blocking either. For the latter, this just causes
2435 * the kiocb to be handled from an async context.
2437 if (kiocb->ki_flags & IOCB_HIPRI)
2438 return -EOPNOTSUPP;
2439 if (kiocb->ki_flags & IOCB_NOWAIT)
2440 return -EAGAIN;
2442 while (iov_iter_count(iter)) {
2443 struct iovec iovec;
2444 ssize_t nr;
2446 if (!iov_iter_is_bvec(iter)) {
2447 iovec = iov_iter_iovec(iter);
2448 } else {
2449 /* fixed buffers import bvec */
2450 iovec.iov_base = kmap(iter->bvec->bv_page)
2451 + iter->iov_offset;
2452 iovec.iov_len = min(iter->count,
2453 iter->bvec->bv_len - iter->iov_offset);
2456 if (rw == READ) {
2457 nr = file->f_op->read(file, iovec.iov_base,
2458 iovec.iov_len, &kiocb->ki_pos);
2459 } else {
2460 nr = file->f_op->write(file, iovec.iov_base,
2461 iovec.iov_len, &kiocb->ki_pos);
2464 if (iov_iter_is_bvec(iter))
2465 kunmap(iter->bvec->bv_page);
2467 if (nr < 0) {
2468 if (!ret)
2469 ret = nr;
2470 break;
2472 ret += nr;
2473 if (nr != iovec.iov_len)
2474 break;
2475 iov_iter_advance(iter, nr);
2478 return ret;
2481 static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
2482 struct iovec *iovec, struct iovec *fast_iov,
2483 struct iov_iter *iter)
2485 req->io->rw.nr_segs = iter->nr_segs;
2486 req->io->rw.size = io_size;
2487 req->io->rw.iov = iovec;
2488 if (!req->io->rw.iov) {
2489 req->io->rw.iov = req->io->rw.fast_iov;
2490 if (req->io->rw.iov != fast_iov)
2491 memcpy(req->io->rw.iov, fast_iov,
2492 sizeof(struct iovec) * iter->nr_segs);
2493 } else {
2494 req->flags |= REQ_F_NEED_CLEANUP;
2498 static inline int __io_alloc_async_ctx(struct io_kiocb *req)
2500 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
2501 return req->io == NULL;
2504 static int io_alloc_async_ctx(struct io_kiocb *req)
2506 if (!io_op_defs[req->opcode].async_ctx)
2507 return 0;
2509 return __io_alloc_async_ctx(req);
2512 static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2513 struct iovec *iovec, struct iovec *fast_iov,
2514 struct iov_iter *iter)
2516 if (!io_op_defs[req->opcode].async_ctx)
2517 return 0;
2518 if (!req->io) {
2519 if (__io_alloc_async_ctx(req))
2520 return -ENOMEM;
2522 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2524 return 0;
2527 static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2528 bool force_nonblock)
2530 struct io_async_ctx *io;
2531 struct iov_iter iter;
2532 ssize_t ret;
2534 ret = io_prep_rw(req, sqe, force_nonblock);
2535 if (ret)
2536 return ret;
2538 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2539 return -EBADF;
2541 /* either don't need iovec imported or already have it */
2542 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
2543 return 0;
2545 io = req->io;
2546 io->rw.iov = io->rw.fast_iov;
2547 req->io = NULL;
2548 ret = io_import_iovec(READ, req, &io->rw.iov, &iter, !force_nonblock);
2549 req->io = io;
2550 if (ret < 0)
2551 return ret;
2553 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2554 return 0;
2557 static int io_read(struct io_kiocb *req, bool force_nonblock)
2559 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
2560 struct kiocb *kiocb = &req->rw.kiocb;
2561 struct iov_iter iter;
2562 size_t iov_count;
2563 ssize_t io_size, ret;
2565 ret = io_import_iovec(READ, req, &iovec, &iter, !force_nonblock);
2566 if (ret < 0)
2567 return ret;
2569 /* Ensure we clear previously set non-block flag */
2570 if (!force_nonblock)
2571 kiocb->ki_flags &= ~IOCB_NOWAIT;
2573 req->result = 0;
2574 io_size = ret;
2575 if (req->flags & REQ_F_LINK_HEAD)
2576 req->result = io_size;
2579 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2580 * we know to async punt it even if it was opened O_NONBLOCK
2582 if (force_nonblock && !io_file_supports_async(req->file, READ))
2583 goto copy_iov;
2585 iov_count = iov_iter_count(&iter);
2586 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
2587 if (!ret) {
2588 ssize_t ret2;
2590 if (req->file->f_op->read_iter)
2591 ret2 = call_read_iter(req->file, kiocb, &iter);
2592 else
2593 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
2595 /* Catch -EAGAIN return for forced non-blocking submission */
2596 if (!force_nonblock || ret2 != -EAGAIN) {
2597 kiocb_done(kiocb, ret2);
2598 } else {
2599 copy_iov:
2600 ret = io_setup_async_rw(req, io_size, iovec,
2601 inline_vecs, &iter);
2602 if (ret)
2603 goto out_free;
2604 /* any defer here is final, must blocking retry */
2605 if (!(req->flags & REQ_F_NOWAIT) &&
2606 !file_can_poll(req->file))
2607 req->flags |= REQ_F_MUST_PUNT;
2608 return -EAGAIN;
2611 out_free:
2612 kfree(iovec);
2613 req->flags &= ~REQ_F_NEED_CLEANUP;
2614 return ret;
2617 static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2618 bool force_nonblock)
2620 struct io_async_ctx *io;
2621 struct iov_iter iter;
2622 ssize_t ret;
2624 ret = io_prep_rw(req, sqe, force_nonblock);
2625 if (ret)
2626 return ret;
2628 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2629 return -EBADF;
2631 req->fsize = rlimit(RLIMIT_FSIZE);
2633 /* either don't need iovec imported or already have it */
2634 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
2635 return 0;
2637 io = req->io;
2638 io->rw.iov = io->rw.fast_iov;
2639 req->io = NULL;
2640 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter, !force_nonblock);
2641 req->io = io;
2642 if (ret < 0)
2643 return ret;
2645 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2646 return 0;
2649 static int io_write(struct io_kiocb *req, bool force_nonblock)
2651 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
2652 struct kiocb *kiocb = &req->rw.kiocb;
2653 struct iov_iter iter;
2654 size_t iov_count;
2655 ssize_t ret, io_size;
2657 ret = io_import_iovec(WRITE, req, &iovec, &iter, !force_nonblock);
2658 if (ret < 0)
2659 return ret;
2661 /* Ensure we clear previously set non-block flag */
2662 if (!force_nonblock)
2663 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
2665 req->result = 0;
2666 io_size = ret;
2667 if (req->flags & REQ_F_LINK_HEAD)
2668 req->result = io_size;
2671 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2672 * we know to async punt it even if it was opened O_NONBLOCK
2674 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
2675 goto copy_iov;
2677 /* file path doesn't support NOWAIT for non-direct_IO */
2678 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2679 (req->flags & REQ_F_ISREG))
2680 goto copy_iov;
2682 iov_count = iov_iter_count(&iter);
2683 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
2684 if (!ret) {
2685 ssize_t ret2;
2688 * Open-code file_start_write here to grab freeze protection,
2689 * which will be released by another thread in
2690 * io_complete_rw(). Fool lockdep by telling it the lock got
2691 * released so that it doesn't complain about the held lock when
2692 * we return to userspace.
2694 if (req->flags & REQ_F_ISREG) {
2695 __sb_start_write(file_inode(req->file)->i_sb,
2696 SB_FREEZE_WRITE, true);
2697 __sb_writers_release(file_inode(req->file)->i_sb,
2698 SB_FREEZE_WRITE);
2700 kiocb->ki_flags |= IOCB_WRITE;
2702 if (!force_nonblock)
2703 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = req->fsize;
2705 if (req->file->f_op->write_iter)
2706 ret2 = call_write_iter(req->file, kiocb, &iter);
2707 else
2708 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
2710 if (!force_nonblock)
2711 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
2714 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
2715 * retry them without IOCB_NOWAIT.
2717 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
2718 ret2 = -EAGAIN;
2719 if (!force_nonblock || ret2 != -EAGAIN) {
2720 kiocb_done(kiocb, ret2);
2721 } else {
2722 copy_iov:
2723 ret = io_setup_async_rw(req, io_size, iovec,
2724 inline_vecs, &iter);
2725 if (ret)
2726 goto out_free;
2727 /* any defer here is final, must blocking retry */
2728 if (!(req->flags & REQ_F_NOWAIT) &&
2729 !file_can_poll(req->file))
2730 req->flags |= REQ_F_MUST_PUNT;
2731 return -EAGAIN;
2734 out_free:
2735 req->flags &= ~REQ_F_NEED_CLEANUP;
2736 kfree(iovec);
2737 return ret;
2740 static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2742 struct io_splice* sp = &req->splice;
2743 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
2744 int ret;
2746 if (req->flags & REQ_F_NEED_CLEANUP)
2747 return 0;
2749 sp->file_in = NULL;
2750 sp->off_in = READ_ONCE(sqe->splice_off_in);
2751 sp->off_out = READ_ONCE(sqe->off);
2752 sp->len = READ_ONCE(sqe->len);
2753 sp->flags = READ_ONCE(sqe->splice_flags);
2755 if (unlikely(sp->flags & ~valid_flags))
2756 return -EINVAL;
2758 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
2759 (sp->flags & SPLICE_F_FD_IN_FIXED));
2760 if (ret)
2761 return ret;
2762 req->flags |= REQ_F_NEED_CLEANUP;
2764 if (!S_ISREG(file_inode(sp->file_in)->i_mode))
2765 req->work.flags |= IO_WQ_WORK_UNBOUND;
2767 return 0;
2770 static int io_splice(struct io_kiocb *req, bool force_nonblock)
2772 struct io_splice *sp = &req->splice;
2773 struct file *in = sp->file_in;
2774 struct file *out = sp->file_out;
2775 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
2776 loff_t *poff_in, *poff_out;
2777 long ret = 0;
2779 if (force_nonblock)
2780 return -EAGAIN;
2782 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
2783 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
2785 if (sp->len)
2786 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
2788 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
2789 req->flags &= ~REQ_F_NEED_CLEANUP;
2791 io_cqring_add_event(req, ret);
2792 if (ret != sp->len)
2793 req_set_fail_links(req);
2794 io_put_req(req);
2795 return 0;
2799 * IORING_OP_NOP just posts a completion event, nothing else.
2801 static int io_nop(struct io_kiocb *req)
2803 struct io_ring_ctx *ctx = req->ctx;
2805 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2806 return -EINVAL;
2808 io_cqring_add_event(req, 0);
2809 io_put_req(req);
2810 return 0;
2813 static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2815 struct io_ring_ctx *ctx = req->ctx;
2817 if (!req->file)
2818 return -EBADF;
2820 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2821 return -EINVAL;
2822 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
2823 return -EINVAL;
2825 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2826 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2827 return -EINVAL;
2829 req->sync.off = READ_ONCE(sqe->off);
2830 req->sync.len = READ_ONCE(sqe->len);
2831 return 0;
2834 static bool io_req_cancelled(struct io_kiocb *req)
2836 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2837 req_set_fail_links(req);
2838 io_cqring_add_event(req, -ECANCELED);
2839 io_put_req(req);
2840 return true;
2843 return false;
2846 static void __io_fsync(struct io_kiocb *req)
2848 loff_t end = req->sync.off + req->sync.len;
2849 int ret;
2851 ret = vfs_fsync_range(req->file, req->sync.off,
2852 end > 0 ? end : LLONG_MAX,
2853 req->sync.flags & IORING_FSYNC_DATASYNC);
2854 if (ret < 0)
2855 req_set_fail_links(req);
2856 io_cqring_add_event(req, ret);
2857 io_put_req(req);
2860 static void io_fsync_finish(struct io_wq_work **workptr)
2862 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2864 if (io_req_cancelled(req))
2865 return;
2866 __io_fsync(req);
2867 io_steal_work(req, workptr);
2870 static int io_fsync(struct io_kiocb *req, bool force_nonblock)
2872 /* fsync always requires a blocking context */
2873 if (force_nonblock) {
2874 req->work.func = io_fsync_finish;
2875 return -EAGAIN;
2877 __io_fsync(req);
2878 return 0;
2881 static void __io_fallocate(struct io_kiocb *req)
2883 int ret;
2885 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = req->fsize;
2886 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2887 req->sync.len);
2888 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
2889 if (ret < 0)
2890 req_set_fail_links(req);
2891 io_cqring_add_event(req, ret);
2892 io_put_req(req);
2895 static void io_fallocate_finish(struct io_wq_work **workptr)
2897 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2899 if (io_req_cancelled(req))
2900 return;
2901 __io_fallocate(req);
2902 io_steal_work(req, workptr);
2905 static int io_fallocate_prep(struct io_kiocb *req,
2906 const struct io_uring_sqe *sqe)
2908 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2909 return -EINVAL;
2911 req->sync.off = READ_ONCE(sqe->off);
2912 req->sync.len = READ_ONCE(sqe->addr);
2913 req->sync.mode = READ_ONCE(sqe->len);
2914 req->fsize = rlimit(RLIMIT_FSIZE);
2915 return 0;
2918 static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
2920 /* fallocate always requiring blocking context */
2921 if (force_nonblock) {
2922 req->work.func = io_fallocate_finish;
2923 return -EAGAIN;
2926 __io_fallocate(req);
2927 return 0;
2930 static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2932 const char __user *fname;
2933 int ret;
2935 if (sqe->ioprio || sqe->buf_index)
2936 return -EINVAL;
2937 if (req->flags & REQ_F_FIXED_FILE)
2938 return -EBADF;
2939 if (req->flags & REQ_F_NEED_CLEANUP)
2940 return 0;
2942 req->open.dfd = READ_ONCE(sqe->fd);
2943 req->open.how.mode = READ_ONCE(sqe->len);
2944 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2945 req->open.how.flags = READ_ONCE(sqe->open_flags);
2946 if (force_o_largefile())
2947 req->open.how.flags |= O_LARGEFILE;
2949 req->open.filename = getname(fname);
2950 if (IS_ERR(req->open.filename)) {
2951 ret = PTR_ERR(req->open.filename);
2952 req->open.filename = NULL;
2953 return ret;
2956 req->open.nofile = rlimit(RLIMIT_NOFILE);
2957 req->flags |= REQ_F_NEED_CLEANUP;
2958 return 0;
2961 static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2963 struct open_how __user *how;
2964 const char __user *fname;
2965 size_t len;
2966 int ret;
2968 if (sqe->ioprio || sqe->buf_index)
2969 return -EINVAL;
2970 if (req->flags & REQ_F_FIXED_FILE)
2971 return -EBADF;
2972 if (req->flags & REQ_F_NEED_CLEANUP)
2973 return 0;
2975 req->open.dfd = READ_ONCE(sqe->fd);
2976 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2977 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2978 len = READ_ONCE(sqe->len);
2980 if (len < OPEN_HOW_SIZE_VER0)
2981 return -EINVAL;
2983 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
2984 len);
2985 if (ret)
2986 return ret;
2988 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
2989 req->open.how.flags |= O_LARGEFILE;
2991 req->open.filename = getname(fname);
2992 if (IS_ERR(req->open.filename)) {
2993 ret = PTR_ERR(req->open.filename);
2994 req->open.filename = NULL;
2995 return ret;
2998 req->open.nofile = rlimit(RLIMIT_NOFILE);
2999 req->flags |= REQ_F_NEED_CLEANUP;
3000 return 0;
3003 static int io_openat2(struct io_kiocb *req, bool force_nonblock)
3005 struct open_flags op;
3006 struct file *file;
3007 int ret;
3009 if (force_nonblock)
3010 return -EAGAIN;
3012 ret = build_open_flags(&req->open.how, &op);
3013 if (ret)
3014 goto err;
3016 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
3017 if (ret < 0)
3018 goto err;
3020 file = do_filp_open(req->open.dfd, req->open.filename, &op);
3021 if (IS_ERR(file)) {
3022 put_unused_fd(ret);
3023 ret = PTR_ERR(file);
3024 } else {
3025 fsnotify_open(file);
3026 fd_install(ret, file);
3028 err:
3029 putname(req->open.filename);
3030 req->flags &= ~REQ_F_NEED_CLEANUP;
3031 if (ret < 0)
3032 req_set_fail_links(req);
3033 io_cqring_add_event(req, ret);
3034 io_put_req(req);
3035 return 0;
3038 static int io_openat(struct io_kiocb *req, bool force_nonblock)
3040 req->open.how = build_open_how(req->open.how.flags, req->open.how.mode);
3041 return io_openat2(req, force_nonblock);
3044 static int io_remove_buffers_prep(struct io_kiocb *req,
3045 const struct io_uring_sqe *sqe)
3047 struct io_provide_buf *p = &req->pbuf;
3048 u64 tmp;
3050 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3051 return -EINVAL;
3053 tmp = READ_ONCE(sqe->fd);
3054 if (!tmp || tmp > USHRT_MAX)
3055 return -EINVAL;
3057 memset(p, 0, sizeof(*p));
3058 p->nbufs = tmp;
3059 p->bgid = READ_ONCE(sqe->buf_group);
3060 return 0;
3063 static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3064 int bgid, unsigned nbufs)
3066 unsigned i = 0;
3068 /* shouldn't happen */
3069 if (!nbufs)
3070 return 0;
3072 /* the head kbuf is the list itself */
3073 while (!list_empty(&buf->list)) {
3074 struct io_buffer *nxt;
3076 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3077 list_del(&nxt->list);
3078 kfree(nxt);
3079 if (++i == nbufs)
3080 return i;
3082 i++;
3083 kfree(buf);
3084 idr_remove(&ctx->io_buffer_idr, bgid);
3086 return i;
3089 static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock)
3091 struct io_provide_buf *p = &req->pbuf;
3092 struct io_ring_ctx *ctx = req->ctx;
3093 struct io_buffer *head;
3094 int ret = 0;
3096 io_ring_submit_lock(ctx, !force_nonblock);
3098 lockdep_assert_held(&ctx->uring_lock);
3100 ret = -ENOENT;
3101 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3102 if (head)
3103 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
3105 io_ring_submit_lock(ctx, !force_nonblock);
3106 if (ret < 0)
3107 req_set_fail_links(req);
3108 io_cqring_add_event(req, ret);
3109 io_put_req(req);
3110 return 0;
3113 static int io_provide_buffers_prep(struct io_kiocb *req,
3114 const struct io_uring_sqe *sqe)
3116 struct io_provide_buf *p = &req->pbuf;
3117 u64 tmp;
3119 if (sqe->ioprio || sqe->rw_flags)
3120 return -EINVAL;
3122 tmp = READ_ONCE(sqe->fd);
3123 if (!tmp || tmp > USHRT_MAX)
3124 return -E2BIG;
3125 p->nbufs = tmp;
3126 p->addr = READ_ONCE(sqe->addr);
3127 p->len = READ_ONCE(sqe->len);
3129 if (!access_ok(u64_to_user_ptr(p->addr), p->len))
3130 return -EFAULT;
3132 p->bgid = READ_ONCE(sqe->buf_group);
3133 tmp = READ_ONCE(sqe->off);
3134 if (tmp > USHRT_MAX)
3135 return -E2BIG;
3136 p->bid = tmp;
3137 return 0;
3140 static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3142 struct io_buffer *buf;
3143 u64 addr = pbuf->addr;
3144 int i, bid = pbuf->bid;
3146 for (i = 0; i < pbuf->nbufs; i++) {
3147 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3148 if (!buf)
3149 break;
3151 buf->addr = addr;
3152 buf->len = pbuf->len;
3153 buf->bid = bid;
3154 addr += pbuf->len;
3155 bid++;
3156 if (!*head) {
3157 INIT_LIST_HEAD(&buf->list);
3158 *head = buf;
3159 } else {
3160 list_add_tail(&buf->list, &(*head)->list);
3164 return i ? i : -ENOMEM;
3167 static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock)
3169 struct io_provide_buf *p = &req->pbuf;
3170 struct io_ring_ctx *ctx = req->ctx;
3171 struct io_buffer *head, *list;
3172 int ret = 0;
3174 io_ring_submit_lock(ctx, !force_nonblock);
3176 lockdep_assert_held(&ctx->uring_lock);
3178 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
3180 ret = io_add_buffers(p, &head);
3181 if (ret < 0)
3182 goto out;
3184 if (!list) {
3185 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
3186 GFP_KERNEL);
3187 if (ret < 0) {
3188 __io_remove_buffers(ctx, head, p->bgid, -1U);
3189 goto out;
3192 out:
3193 io_ring_submit_unlock(ctx, !force_nonblock);
3194 if (ret < 0)
3195 req_set_fail_links(req);
3196 io_cqring_add_event(req, ret);
3197 io_put_req(req);
3198 return 0;
3201 static int io_epoll_ctl_prep(struct io_kiocb *req,
3202 const struct io_uring_sqe *sqe)
3204 #if defined(CONFIG_EPOLL)
3205 if (sqe->ioprio || sqe->buf_index)
3206 return -EINVAL;
3208 req->epoll.epfd = READ_ONCE(sqe->fd);
3209 req->epoll.op = READ_ONCE(sqe->len);
3210 req->epoll.fd = READ_ONCE(sqe->off);
3212 if (ep_op_has_event(req->epoll.op)) {
3213 struct epoll_event __user *ev;
3215 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
3216 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
3217 return -EFAULT;
3220 return 0;
3221 #else
3222 return -EOPNOTSUPP;
3223 #endif
3226 static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock)
3228 #if defined(CONFIG_EPOLL)
3229 struct io_epoll *ie = &req->epoll;
3230 int ret;
3232 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
3233 if (force_nonblock && ret == -EAGAIN)
3234 return -EAGAIN;
3236 if (ret < 0)
3237 req_set_fail_links(req);
3238 io_cqring_add_event(req, ret);
3239 io_put_req(req);
3240 return 0;
3241 #else
3242 return -EOPNOTSUPP;
3243 #endif
3246 static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3248 #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3249 if (sqe->ioprio || sqe->buf_index || sqe->off)
3250 return -EINVAL;
3252 req->madvise.addr = READ_ONCE(sqe->addr);
3253 req->madvise.len = READ_ONCE(sqe->len);
3254 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
3255 return 0;
3256 #else
3257 return -EOPNOTSUPP;
3258 #endif
3261 static int io_madvise(struct io_kiocb *req, bool force_nonblock)
3263 #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3264 struct io_madvise *ma = &req->madvise;
3265 int ret;
3267 if (force_nonblock)
3268 return -EAGAIN;
3270 ret = do_madvise(ma->addr, ma->len, ma->advice);
3271 if (ret < 0)
3272 req_set_fail_links(req);
3273 io_cqring_add_event(req, ret);
3274 io_put_req(req);
3275 return 0;
3276 #else
3277 return -EOPNOTSUPP;
3278 #endif
3281 static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3283 if (sqe->ioprio || sqe->buf_index || sqe->addr)
3284 return -EINVAL;
3286 req->fadvise.offset = READ_ONCE(sqe->off);
3287 req->fadvise.len = READ_ONCE(sqe->len);
3288 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
3289 return 0;
3292 static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
3294 struct io_fadvise *fa = &req->fadvise;
3295 int ret;
3297 if (force_nonblock) {
3298 switch (fa->advice) {
3299 case POSIX_FADV_NORMAL:
3300 case POSIX_FADV_RANDOM:
3301 case POSIX_FADV_SEQUENTIAL:
3302 break;
3303 default:
3304 return -EAGAIN;
3308 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
3309 if (ret < 0)
3310 req_set_fail_links(req);
3311 io_cqring_add_event(req, ret);
3312 io_put_req(req);
3313 return 0;
3316 static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3318 const char __user *fname;
3319 unsigned lookup_flags;
3320 int ret;
3322 if (sqe->ioprio || sqe->buf_index)
3323 return -EINVAL;
3324 if (req->flags & REQ_F_FIXED_FILE)
3325 return -EBADF;
3326 if (req->flags & REQ_F_NEED_CLEANUP)
3327 return 0;
3329 req->open.dfd = READ_ONCE(sqe->fd);
3330 req->open.mask = READ_ONCE(sqe->len);
3331 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3332 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3333 req->open.how.flags = READ_ONCE(sqe->statx_flags);
3335 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags))
3336 return -EINVAL;
3338 req->open.filename = getname_flags(fname, lookup_flags, NULL);
3339 if (IS_ERR(req->open.filename)) {
3340 ret = PTR_ERR(req->open.filename);
3341 req->open.filename = NULL;
3342 return ret;
3345 req->flags |= REQ_F_NEED_CLEANUP;
3346 return 0;
3349 static int io_statx(struct io_kiocb *req, bool force_nonblock)
3351 struct io_open *ctx = &req->open;
3352 unsigned lookup_flags;
3353 struct path path;
3354 struct kstat stat;
3355 int ret;
3357 if (force_nonblock) {
3358 /* only need file table for an actual valid fd */
3359 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
3360 req->flags |= REQ_F_NO_FILE_TABLE;
3361 return -EAGAIN;
3364 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags))
3365 return -EINVAL;
3367 retry:
3368 /* filename_lookup() drops it, keep a reference */
3369 ctx->filename->refcnt++;
3371 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
3372 NULL);
3373 if (ret)
3374 goto err;
3376 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags);
3377 path_put(&path);
3378 if (retry_estale(ret, lookup_flags)) {
3379 lookup_flags |= LOOKUP_REVAL;
3380 goto retry;
3382 if (!ret)
3383 ret = cp_statx(&stat, ctx->buffer);
3384 err:
3385 putname(ctx->filename);
3386 req->flags &= ~REQ_F_NEED_CLEANUP;
3387 if (ret < 0)
3388 req_set_fail_links(req);
3389 io_cqring_add_event(req, ret);
3390 io_put_req(req);
3391 return 0;
3394 static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3397 * If we queue this for async, it must not be cancellable. That would
3398 * leave the 'file' in an undeterminate state.
3400 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
3402 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
3403 sqe->rw_flags || sqe->buf_index)
3404 return -EINVAL;
3405 if (req->flags & REQ_F_FIXED_FILE)
3406 return -EBADF;
3408 req->close.fd = READ_ONCE(sqe->fd);
3409 if (req->file->f_op == &io_uring_fops ||
3410 req->close.fd == req->ctx->ring_fd)
3411 return -EBADF;
3413 return 0;
3416 /* only called when __close_fd_get_file() is done */
3417 static void __io_close_finish(struct io_kiocb *req)
3419 int ret;
3421 ret = filp_close(req->close.put_file, req->work.files);
3422 if (ret < 0)
3423 req_set_fail_links(req);
3424 io_cqring_add_event(req, ret);
3425 fput(req->close.put_file);
3426 io_put_req(req);
3429 static void io_close_finish(struct io_wq_work **workptr)
3431 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3433 /* not cancellable, don't do io_req_cancelled() */
3434 __io_close_finish(req);
3435 io_steal_work(req, workptr);
3438 static int io_close(struct io_kiocb *req, bool force_nonblock)
3440 int ret;
3442 req->close.put_file = NULL;
3443 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
3444 if (ret < 0)
3445 return ret;
3447 /* if the file has a flush method, be safe and punt to async */
3448 if (req->close.put_file->f_op->flush && force_nonblock) {
3449 /* submission ref will be dropped, take it for async */
3450 refcount_inc(&req->refs);
3452 req->work.func = io_close_finish;
3454 * Do manual async queue here to avoid grabbing files - we don't
3455 * need the files, and it'll cause io_close_finish() to close
3456 * the file again and cause a double CQE entry for this request
3458 io_queue_async_work(req);
3459 return 0;
3463 * No ->flush(), safely close from here and just punt the
3464 * fput() to async context.
3466 __io_close_finish(req);
3467 return 0;
3470 static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3472 struct io_ring_ctx *ctx = req->ctx;
3474 if (!req->file)
3475 return -EBADF;
3477 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3478 return -EINVAL;
3479 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
3480 return -EINVAL;
3482 req->sync.off = READ_ONCE(sqe->off);
3483 req->sync.len = READ_ONCE(sqe->len);
3484 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
3485 return 0;
3488 static void __io_sync_file_range(struct io_kiocb *req)
3490 int ret;
3492 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
3493 req->sync.flags);
3494 if (ret < 0)
3495 req_set_fail_links(req);
3496 io_cqring_add_event(req, ret);
3497 io_put_req(req);
3501 static void io_sync_file_range_finish(struct io_wq_work **workptr)
3503 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3505 if (io_req_cancelled(req))
3506 return;
3507 __io_sync_file_range(req);
3508 io_steal_work(req, workptr);
3511 static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
3513 /* sync_file_range always requires a blocking context */
3514 if (force_nonblock) {
3515 req->work.func = io_sync_file_range_finish;
3516 return -EAGAIN;
3519 __io_sync_file_range(req);
3520 return 0;
3523 #if defined(CONFIG_NET)
3524 static int io_setup_async_msg(struct io_kiocb *req,
3525 struct io_async_msghdr *kmsg)
3527 if (req->io)
3528 return -EAGAIN;
3529 if (io_alloc_async_ctx(req)) {
3530 if (kmsg->iov != kmsg->fast_iov)
3531 kfree(kmsg->iov);
3532 return -ENOMEM;
3534 req->flags |= REQ_F_NEED_CLEANUP;
3535 memcpy(&req->io->msg, kmsg, sizeof(*kmsg));
3536 return -EAGAIN;
3539 static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3541 struct io_sr_msg *sr = &req->sr_msg;
3542 struct io_async_ctx *io = req->io;
3543 int ret;
3545 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3546 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
3547 sr->len = READ_ONCE(sqe->len);
3549 #ifdef CONFIG_COMPAT
3550 if (req->ctx->compat)
3551 sr->msg_flags |= MSG_CMSG_COMPAT;
3552 #endif
3554 if (!io || req->opcode == IORING_OP_SEND)
3555 return 0;
3556 /* iovec is already imported */
3557 if (req->flags & REQ_F_NEED_CLEANUP)
3558 return 0;
3560 io->msg.iov = io->msg.fast_iov;
3561 ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
3562 &io->msg.iov);
3563 if (!ret)
3564 req->flags |= REQ_F_NEED_CLEANUP;
3565 return ret;
3568 static int io_sendmsg(struct io_kiocb *req, bool force_nonblock)
3570 struct io_async_msghdr *kmsg = NULL;
3571 struct socket *sock;
3572 int ret;
3574 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3575 return -EINVAL;
3577 sock = sock_from_file(req->file, &ret);
3578 if (sock) {
3579 struct io_async_ctx io;
3580 unsigned flags;
3582 if (req->io) {
3583 kmsg = &req->io->msg;
3584 kmsg->msg.msg_name = &req->io->msg.addr;
3585 /* if iov is set, it's allocated already */
3586 if (!kmsg->iov)
3587 kmsg->iov = kmsg->fast_iov;
3588 kmsg->msg.msg_iter.iov = kmsg->iov;
3589 } else {
3590 struct io_sr_msg *sr = &req->sr_msg;
3592 kmsg = &io.msg;
3593 kmsg->msg.msg_name = &io.msg.addr;
3595 io.msg.iov = io.msg.fast_iov;
3596 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
3597 sr->msg_flags, &io.msg.iov);
3598 if (ret)
3599 return ret;
3602 flags = req->sr_msg.msg_flags;
3603 if (flags & MSG_DONTWAIT)
3604 req->flags |= REQ_F_NOWAIT;
3605 else if (force_nonblock)
3606 flags |= MSG_DONTWAIT;
3608 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
3609 if (force_nonblock && ret == -EAGAIN)
3610 return io_setup_async_msg(req, kmsg);
3611 if (ret == -ERESTARTSYS)
3612 ret = -EINTR;
3615 if (kmsg && kmsg->iov != kmsg->fast_iov)
3616 kfree(kmsg->iov);
3617 req->flags &= ~REQ_F_NEED_CLEANUP;
3618 io_cqring_add_event(req, ret);
3619 if (ret < 0)
3620 req_set_fail_links(req);
3621 io_put_req(req);
3622 return 0;
3625 static int io_send(struct io_kiocb *req, bool force_nonblock)
3627 struct socket *sock;
3628 int ret;
3630 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3631 return -EINVAL;
3633 sock = sock_from_file(req->file, &ret);
3634 if (sock) {
3635 struct io_sr_msg *sr = &req->sr_msg;
3636 struct msghdr msg;
3637 struct iovec iov;
3638 unsigned flags;
3640 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
3641 &msg.msg_iter);
3642 if (ret)
3643 return ret;
3645 msg.msg_name = NULL;
3646 msg.msg_control = NULL;
3647 msg.msg_controllen = 0;
3648 msg.msg_namelen = 0;
3650 flags = req->sr_msg.msg_flags;
3651 if (flags & MSG_DONTWAIT)
3652 req->flags |= REQ_F_NOWAIT;
3653 else if (force_nonblock)
3654 flags |= MSG_DONTWAIT;
3656 msg.msg_flags = flags;
3657 ret = sock_sendmsg(sock, &msg);
3658 if (force_nonblock && ret == -EAGAIN)
3659 return -EAGAIN;
3660 if (ret == -ERESTARTSYS)
3661 ret = -EINTR;
3664 io_cqring_add_event(req, ret);
3665 if (ret < 0)
3666 req_set_fail_links(req);
3667 io_put_req(req);
3668 return 0;
3671 static int __io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io)
3673 struct io_sr_msg *sr = &req->sr_msg;
3674 struct iovec __user *uiov;
3675 size_t iov_len;
3676 int ret;
3678 ret = __copy_msghdr_from_user(&io->msg.msg, sr->msg, &io->msg.uaddr,
3679 &uiov, &iov_len);
3680 if (ret)
3681 return ret;
3683 if (req->flags & REQ_F_BUFFER_SELECT) {
3684 if (iov_len > 1)
3685 return -EINVAL;
3686 if (copy_from_user(io->msg.iov, uiov, sizeof(*uiov)))
3687 return -EFAULT;
3688 sr->len = io->msg.iov[0].iov_len;
3689 iov_iter_init(&io->msg.msg.msg_iter, READ, io->msg.iov, 1,
3690 sr->len);
3691 io->msg.iov = NULL;
3692 } else {
3693 ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
3694 &io->msg.iov, &io->msg.msg.msg_iter);
3695 if (ret > 0)
3696 ret = 0;
3699 return ret;
3702 #ifdef CONFIG_COMPAT
3703 static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
3704 struct io_async_ctx *io)
3706 struct compat_msghdr __user *msg_compat;
3707 struct io_sr_msg *sr = &req->sr_msg;
3708 struct compat_iovec __user *uiov;
3709 compat_uptr_t ptr;
3710 compat_size_t len;
3711 int ret;
3713 msg_compat = (struct compat_msghdr __user *) sr->msg;
3714 ret = __get_compat_msghdr(&io->msg.msg, msg_compat, &io->msg.uaddr,
3715 &ptr, &len);
3716 if (ret)
3717 return ret;
3719 uiov = compat_ptr(ptr);
3720 if (req->flags & REQ_F_BUFFER_SELECT) {
3721 compat_ssize_t clen;
3723 if (len > 1)
3724 return -EINVAL;
3725 if (!access_ok(uiov, sizeof(*uiov)))
3726 return -EFAULT;
3727 if (__get_user(clen, &uiov->iov_len))
3728 return -EFAULT;
3729 if (clen < 0)
3730 return -EINVAL;
3731 sr->len = io->msg.iov[0].iov_len;
3732 io->msg.iov = NULL;
3733 } else {
3734 ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV,
3735 &io->msg.iov,
3736 &io->msg.msg.msg_iter);
3737 if (ret < 0)
3738 return ret;
3741 return 0;
3743 #endif
3745 static int io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io)
3747 io->msg.iov = io->msg.fast_iov;
3749 #ifdef CONFIG_COMPAT
3750 if (req->ctx->compat)
3751 return __io_compat_recvmsg_copy_hdr(req, io);
3752 #endif
3754 return __io_recvmsg_copy_hdr(req, io);
3757 static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
3758 int *cflags, bool needs_lock)
3760 struct io_sr_msg *sr = &req->sr_msg;
3761 struct io_buffer *kbuf;
3763 if (!(req->flags & REQ_F_BUFFER_SELECT))
3764 return NULL;
3766 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
3767 if (IS_ERR(kbuf))
3768 return kbuf;
3770 sr->kbuf = kbuf;
3771 req->flags |= REQ_F_BUFFER_SELECTED;
3773 *cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
3774 *cflags |= IORING_CQE_F_BUFFER;
3775 return kbuf;
3778 static int io_recvmsg_prep(struct io_kiocb *req,
3779 const struct io_uring_sqe *sqe)
3781 struct io_sr_msg *sr = &req->sr_msg;
3782 struct io_async_ctx *io = req->io;
3783 int ret;
3785 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3786 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
3787 sr->len = READ_ONCE(sqe->len);
3788 sr->bgid = READ_ONCE(sqe->buf_group);
3790 #ifdef CONFIG_COMPAT
3791 if (req->ctx->compat)
3792 sr->msg_flags |= MSG_CMSG_COMPAT;
3793 #endif
3795 if (!io || req->opcode == IORING_OP_RECV)
3796 return 0;
3797 /* iovec is already imported */
3798 if (req->flags & REQ_F_NEED_CLEANUP)
3799 return 0;
3801 ret = io_recvmsg_copy_hdr(req, io);
3802 if (!ret)
3803 req->flags |= REQ_F_NEED_CLEANUP;
3804 return ret;
3807 static int io_recvmsg(struct io_kiocb *req, bool force_nonblock)
3809 struct io_async_msghdr *kmsg = NULL;
3810 struct socket *sock;
3811 int ret, cflags = 0;
3813 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3814 return -EINVAL;
3816 sock = sock_from_file(req->file, &ret);
3817 if (sock) {
3818 struct io_buffer *kbuf;
3819 struct io_async_ctx io;
3820 unsigned flags;
3822 if (req->io) {
3823 kmsg = &req->io->msg;
3824 kmsg->msg.msg_name = &req->io->msg.addr;
3825 /* if iov is set, it's allocated already */
3826 if (!kmsg->iov)
3827 kmsg->iov = kmsg->fast_iov;
3828 kmsg->msg.msg_iter.iov = kmsg->iov;
3829 } else {
3830 kmsg = &io.msg;
3831 kmsg->msg.msg_name = &io.msg.addr;
3833 ret = io_recvmsg_copy_hdr(req, &io);
3834 if (ret)
3835 return ret;
3838 kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock);
3839 if (IS_ERR(kbuf)) {
3840 return PTR_ERR(kbuf);
3841 } else if (kbuf) {
3842 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3843 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
3844 1, req->sr_msg.len);
3847 flags = req->sr_msg.msg_flags;
3848 if (flags & MSG_DONTWAIT)
3849 req->flags |= REQ_F_NOWAIT;
3850 else if (force_nonblock)
3851 flags |= MSG_DONTWAIT;
3853 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3854 kmsg->uaddr, flags);
3855 if (force_nonblock && ret == -EAGAIN)
3856 return io_setup_async_msg(req, kmsg);
3857 if (ret == -ERESTARTSYS)
3858 ret = -EINTR;
3861 if (kmsg && kmsg->iov != kmsg->fast_iov)
3862 kfree(kmsg->iov);
3863 req->flags &= ~REQ_F_NEED_CLEANUP;
3864 __io_cqring_add_event(req, ret, cflags);
3865 if (ret < 0)
3866 req_set_fail_links(req);
3867 io_put_req(req);
3868 return 0;
3871 static int io_recv(struct io_kiocb *req, bool force_nonblock)
3873 struct io_buffer *kbuf = NULL;
3874 struct socket *sock;
3875 int ret, cflags = 0;
3877 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3878 return -EINVAL;
3880 sock = sock_from_file(req->file, &ret);
3881 if (sock) {
3882 struct io_sr_msg *sr = &req->sr_msg;
3883 void __user *buf = sr->buf;
3884 struct msghdr msg;
3885 struct iovec iov;
3886 unsigned flags;
3888 kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock);
3889 if (IS_ERR(kbuf))
3890 return PTR_ERR(kbuf);
3891 else if (kbuf)
3892 buf = u64_to_user_ptr(kbuf->addr);
3894 ret = import_single_range(READ, buf, sr->len, &iov,
3895 &msg.msg_iter);
3896 if (ret) {
3897 kfree(kbuf);
3898 return ret;
3901 req->flags |= REQ_F_NEED_CLEANUP;
3902 msg.msg_name = NULL;
3903 msg.msg_control = NULL;
3904 msg.msg_controllen = 0;
3905 msg.msg_namelen = 0;
3906 msg.msg_iocb = NULL;
3907 msg.msg_flags = 0;
3909 flags = req->sr_msg.msg_flags;
3910 if (flags & MSG_DONTWAIT)
3911 req->flags |= REQ_F_NOWAIT;
3912 else if (force_nonblock)
3913 flags |= MSG_DONTWAIT;
3915 ret = sock_recvmsg(sock, &msg, flags);
3916 if (force_nonblock && ret == -EAGAIN)
3917 return -EAGAIN;
3918 if (ret == -ERESTARTSYS)
3919 ret = -EINTR;
3922 kfree(kbuf);
3923 req->flags &= ~REQ_F_NEED_CLEANUP;
3924 __io_cqring_add_event(req, ret, cflags);
3925 if (ret < 0)
3926 req_set_fail_links(req);
3927 io_put_req(req);
3928 return 0;
3931 static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3933 struct io_accept *accept = &req->accept;
3935 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3936 return -EINVAL;
3937 if (sqe->ioprio || sqe->len || sqe->buf_index)
3938 return -EINVAL;
3940 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3941 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3942 accept->flags = READ_ONCE(sqe->accept_flags);
3943 accept->nofile = rlimit(RLIMIT_NOFILE);
3944 return 0;
3947 static int __io_accept(struct io_kiocb *req, bool force_nonblock)
3949 struct io_accept *accept = &req->accept;
3950 unsigned file_flags;
3951 int ret;
3953 file_flags = force_nonblock ? O_NONBLOCK : 0;
3954 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
3955 accept->addr_len, accept->flags,
3956 accept->nofile);
3957 if (ret == -EAGAIN && force_nonblock)
3958 return -EAGAIN;
3959 if (ret == -ERESTARTSYS)
3960 ret = -EINTR;
3961 if (ret < 0)
3962 req_set_fail_links(req);
3963 io_cqring_add_event(req, ret);
3964 io_put_req(req);
3965 return 0;
3968 static void io_accept_finish(struct io_wq_work **workptr)
3970 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3972 if (io_req_cancelled(req))
3973 return;
3974 __io_accept(req, false);
3975 io_steal_work(req, workptr);
3978 static int io_accept(struct io_kiocb *req, bool force_nonblock)
3980 int ret;
3982 ret = __io_accept(req, force_nonblock);
3983 if (ret == -EAGAIN && force_nonblock) {
3984 req->work.func = io_accept_finish;
3985 return -EAGAIN;
3987 return 0;
3990 static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3992 struct io_connect *conn = &req->connect;
3993 struct io_async_ctx *io = req->io;
3995 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3996 return -EINVAL;
3997 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
3998 return -EINVAL;
4000 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4001 conn->addr_len = READ_ONCE(sqe->addr2);
4003 if (!io)
4004 return 0;
4006 return move_addr_to_kernel(conn->addr, conn->addr_len,
4007 &io->connect.address);
4010 static int io_connect(struct io_kiocb *req, bool force_nonblock)
4012 struct io_async_ctx __io, *io;
4013 unsigned file_flags;
4014 int ret;
4016 if (req->io) {
4017 io = req->io;
4018 } else {
4019 ret = move_addr_to_kernel(req->connect.addr,
4020 req->connect.addr_len,
4021 &__io.connect.address);
4022 if (ret)
4023 goto out;
4024 io = &__io;
4027 file_flags = force_nonblock ? O_NONBLOCK : 0;
4029 ret = __sys_connect_file(req->file, &io->connect.address,
4030 req->connect.addr_len, file_flags);
4031 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
4032 if (req->io)
4033 return -EAGAIN;
4034 if (io_alloc_async_ctx(req)) {
4035 ret = -ENOMEM;
4036 goto out;
4038 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
4039 return -EAGAIN;
4041 if (ret == -ERESTARTSYS)
4042 ret = -EINTR;
4043 out:
4044 if (ret < 0)
4045 req_set_fail_links(req);
4046 io_cqring_add_event(req, ret);
4047 io_put_req(req);
4048 return 0;
4050 #else /* !CONFIG_NET */
4051 static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4053 return -EOPNOTSUPP;
4056 static int io_sendmsg(struct io_kiocb *req, bool force_nonblock)
4058 return -EOPNOTSUPP;
4061 static int io_send(struct io_kiocb *req, bool force_nonblock)
4063 return -EOPNOTSUPP;
4066 static int io_recvmsg_prep(struct io_kiocb *req,
4067 const struct io_uring_sqe *sqe)
4069 return -EOPNOTSUPP;
4072 static int io_recvmsg(struct io_kiocb *req, bool force_nonblock)
4074 return -EOPNOTSUPP;
4077 static int io_recv(struct io_kiocb *req, bool force_nonblock)
4079 return -EOPNOTSUPP;
4082 static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4084 return -EOPNOTSUPP;
4087 static int io_accept(struct io_kiocb *req, bool force_nonblock)
4089 return -EOPNOTSUPP;
4092 static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4094 return -EOPNOTSUPP;
4097 static int io_connect(struct io_kiocb *req, bool force_nonblock)
4099 return -EOPNOTSUPP;
4101 #endif /* CONFIG_NET */
4103 struct io_poll_table {
4104 struct poll_table_struct pt;
4105 struct io_kiocb *req;
4106 int error;
4109 static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4110 __poll_t mask, task_work_func_t func)
4112 struct task_struct *tsk;
4113 int ret;
4115 /* for instances that support it check for an event match first: */
4116 if (mask && !(mask & poll->events))
4117 return 0;
4119 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4121 list_del_init(&poll->wait.entry);
4123 tsk = req->task;
4124 req->result = mask;
4125 init_task_work(&req->task_work, func);
4127 * If this fails, then the task is exiting. When a task exits, the
4128 * work gets canceled, so just cancel this request as well instead
4129 * of executing it. We can't safely execute it anyway, as we may not
4130 * have the needed state needed for it anyway.
4132 ret = task_work_add(tsk, &req->task_work, true);
4133 if (unlikely(ret)) {
4134 WRITE_ONCE(poll->canceled, true);
4135 tsk = io_wq_get_task(req->ctx->io_wq);
4136 task_work_add(tsk, &req->task_work, true);
4138 wake_up_process(tsk);
4139 return 1;
4142 static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4143 __acquires(&req->ctx->completion_lock)
4145 struct io_ring_ctx *ctx = req->ctx;
4147 if (!req->result && !READ_ONCE(poll->canceled)) {
4148 struct poll_table_struct pt = { ._key = poll->events };
4150 req->result = vfs_poll(req->file, &pt) & poll->events;
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;
4159 return false;
4162 static void io_poll_remove_double(struct io_kiocb *req)
4164 struct io_poll_iocb *poll = (struct io_poll_iocb *) req->io;
4166 lockdep_assert_held(&req->ctx->completion_lock);
4168 if (poll && poll->head) {
4169 struct wait_queue_head *head = poll->head;
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);
4180 static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
4182 struct io_ring_ctx *ctx = req->ctx;
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);
4190 static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
4192 struct io_ring_ctx *ctx = req->ctx;
4194 if (io_poll_rewait(req, &req->poll)) {
4195 spin_unlock_irq(&ctx->completion_lock);
4196 return;
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);
4205 io_cqring_ev_posted(ctx);
4208 static void io_poll_task_func(struct callback_head *cb)
4210 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4211 struct io_kiocb *nxt = NULL;
4213 io_poll_task_handler(req, &nxt);
4214 if (nxt) {
4215 struct io_ring_ctx *ctx = nxt->ctx;
4217 mutex_lock(&ctx->uring_lock);
4218 __io_queue_sqe(nxt, NULL);
4219 mutex_unlock(&ctx->uring_lock);
4223 static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
4224 int sync, void *key)
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);
4230 /* for instances that support it check for an event match first: */
4231 if (mask && !(mask & poll->events))
4232 return 0;
4234 if (req->poll.head) {
4235 bool done;
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);
4245 refcount_dec(&req->refs);
4246 return 1;
4249 static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
4250 wait_queue_func_t wake_func)
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);
4260 static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
4261 struct wait_queue_head *head)
4263 struct io_kiocb *req = pt->req;
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.
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;
4276 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
4277 if (!poll) {
4278 pt->error = -ENOMEM;
4279 return;
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;
4287 pt->error = 0;
4288 poll->head = head;
4289 add_wait_queue(head, &poll->wait);
4292 static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
4293 struct poll_table_struct *p)
4295 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
4297 __io_queue_proc(&pt->req->apoll->poll, pt, head);
4300 static void io_async_task_func(struct callback_head *cb)
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;
4305 bool canceled;
4307 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
4309 if (io_poll_rewait(req, &apoll->poll)) {
4310 spin_unlock_irq(&ctx->completion_lock);
4311 return;
4314 if (hash_hashed(&req->hash_node))
4315 hash_del(&req->hash_node);
4317 canceled = READ_ONCE(apoll->poll.canceled);
4318 if (canceled) {
4319 io_cqring_fill_event(req, -ECANCELED);
4320 io_commit_cqring(ctx);
4323 spin_unlock_irq(&ctx->completion_lock);
4325 /* restore ->work in case we need to retry again */
4326 memcpy(&req->work, &apoll->work, sizeof(req->work));
4328 if (canceled) {
4329 kfree(apoll);
4330 io_cqring_ev_posted(ctx);
4331 req_set_fail_links(req);
4332 io_double_put_req(req);
4333 return;
4336 __set_current_state(TASK_RUNNING);
4337 mutex_lock(&ctx->uring_lock);
4338 __io_queue_sqe(req, NULL);
4339 mutex_unlock(&ctx->uring_lock);
4341 kfree(apoll);
4344 static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4345 void *key)
4347 struct io_kiocb *req = wait->private;
4348 struct io_poll_iocb *poll = &req->apoll->poll;
4350 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
4351 key_to_poll(key));
4353 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
4356 static void io_poll_req_insert(struct io_kiocb *req)
4358 struct io_ring_ctx *ctx = req->ctx;
4359 struct hlist_head *list;
4361 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
4362 hlist_add_head(&req->hash_node, list);
4365 static __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)
4371 struct io_ring_ctx *ctx = req->ctx;
4372 bool cancel = false;
4374 poll->file = req->file;
4375 io_init_poll_iocb(poll, mask, wake_func);
4376 poll->wait.private = req;
4378 ipt->pt._key = mask;
4379 ipt->req = req;
4380 ipt->error = -EINVAL;
4382 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
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;
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);
4402 return mask;
4405 static bool io_arm_poll_handler(struct io_kiocb *req)
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;
4412 bool had_io;
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;
4421 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
4422 if (unlikely(!apoll))
4423 return false;
4425 req->flags |= REQ_F_POLLED;
4426 memcpy(&apoll->work, &req->work, sizeof(req->work));
4427 had_io = req->io != NULL;
4429 get_task_struct(current);
4430 req->task = current;
4431 req->apoll = apoll;
4432 INIT_HLIST_NODE(&req->hash_node);
4434 mask = 0;
4435 if (def->pollin)
4436 mask |= POLLIN | POLLRDNORM;
4437 if (def->pollout)
4438 mask |= POLLOUT | POLLWRNORM;
4439 mask |= POLLERR | POLLPRI;
4441 ipt.pt._qproc = io_async_queue_proc;
4443 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
4444 io_async_wake);
4445 if (ret) {
4446 ipt.error = 0;
4447 /* only remove double add if we did it here */
4448 if (!had_io)
4449 io_poll_remove_double(req);
4450 spin_unlock_irq(&ctx->completion_lock);
4451 memcpy(&req->work, &apoll->work, sizeof(req->work));
4452 kfree(apoll);
4453 return false;
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;
4461 static bool __io_poll_remove_one(struct io_kiocb *req,
4462 struct io_poll_iocb *poll)
4464 bool do_complete = false;
4466 spin_lock(&poll->head->lock);
4467 WRITE_ONCE(poll->canceled, true);
4468 if (!list_empty(&poll->wait.entry)) {
4469 list_del_init(&poll->wait.entry);
4470 do_complete = true;
4472 spin_unlock(&poll->head->lock);
4473 hash_del(&req->hash_node);
4474 return do_complete;
4477 static bool io_poll_remove_one(struct io_kiocb *req)
4479 bool do_complete;
4481 if (req->opcode == IORING_OP_POLL_ADD) {
4482 io_poll_remove_double(req);
4483 do_complete = __io_poll_remove_one(req, &req->poll);
4484 } else {
4485 struct async_poll *apoll = req->apoll;
4487 /* non-poll requests have submit ref still */
4488 do_complete = __io_poll_remove_one(req, &apoll->poll);
4489 if (do_complete) {
4490 io_put_req(req);
4492 * restore ->work because we will call
4493 * io_req_work_drop_env below when dropping the
4494 * final reference.
4496 memcpy(&req->work, &apoll->work, sizeof(req->work));
4497 kfree(apoll);
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);
4508 return do_complete;
4511 static void io_poll_remove_all(struct io_ring_ctx *ctx)
4513 struct hlist_node *tmp;
4514 struct io_kiocb *req;
4515 int posted = 0, i;
4517 spin_lock_irq(&ctx->completion_lock);
4518 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
4519 struct hlist_head *list;
4521 list = &ctx->cancel_hash[i];
4522 hlist_for_each_entry_safe(req, tmp, list, hash_node)
4523 posted += io_poll_remove_one(req);
4525 spin_unlock_irq(&ctx->completion_lock);
4527 if (posted)
4528 io_cqring_ev_posted(ctx);
4531 static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
4533 struct hlist_head *list;
4534 struct io_kiocb *req;
4536 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
4537 hlist_for_each_entry(req, list, hash_node) {
4538 if (sqe_addr != req->user_data)
4539 continue;
4540 if (io_poll_remove_one(req))
4541 return 0;
4542 return -EALREADY;
4545 return -ENOENT;
4548 static int io_poll_remove_prep(struct io_kiocb *req,
4549 const struct io_uring_sqe *sqe)
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;
4557 req->poll.addr = READ_ONCE(sqe->addr);
4558 return 0;
4562 * Find a running poll command that matches one specified in sqe->addr,
4563 * and remove it if found.
4565 static int io_poll_remove(struct io_kiocb *req)
4567 struct io_ring_ctx *ctx = req->ctx;
4568 u64 addr;
4569 int ret;
4571 addr = req->poll.addr;
4572 spin_lock_irq(&ctx->completion_lock);
4573 ret = io_poll_cancel(ctx, addr);
4574 spin_unlock_irq(&ctx->completion_lock);
4576 io_cqring_add_event(req, ret);
4577 if (ret < 0)
4578 req_set_fail_links(req);
4579 io_put_req(req);
4580 return 0;
4583 static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4584 void *key)
4586 struct io_kiocb *req = wait->private;
4587 struct io_poll_iocb *poll = &req->poll;
4589 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
4592 static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
4593 struct poll_table_struct *p)
4595 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
4597 __io_queue_proc(&pt->req->poll, pt, head);
4600 static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4602 struct io_poll_iocb *poll = &req->poll;
4603 u16 events;
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;
4609 if (!poll->file)
4610 return -EBADF;
4612 events = READ_ONCE(sqe->poll_events);
4613 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
4615 get_task_struct(current);
4616 req->task = current;
4617 return 0;
4620 static int io_poll_add(struct io_kiocb *req)
4622 struct io_poll_iocb *poll = &req->poll;
4623 struct io_ring_ctx *ctx = req->ctx;
4624 struct io_poll_table ipt;
4625 __poll_t mask;
4627 INIT_HLIST_NODE(&req->hash_node);
4628 INIT_LIST_HEAD(&req->list);
4629 ipt.pt._qproc = io_poll_queue_proc;
4631 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
4632 io_poll_wake);
4634 if (mask) { /* no async, we'd stolen it */
4635 ipt.error = 0;
4636 io_poll_complete(req, mask, 0);
4638 spin_unlock_irq(&ctx->completion_lock);
4640 if (mask) {
4641 io_cqring_ev_posted(ctx);
4642 io_put_req(req);
4644 return ipt.error;
4647 static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
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;
4653 unsigned long flags;
4655 atomic_inc(&ctx->cq_timeouts);
4657 spin_lock_irqsave(&ctx->completion_lock, flags);
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.
4662 if (!list_empty(&req->list)) {
4663 struct io_kiocb *prev;
4666 * Adjust the reqs sequence before the current one because it
4667 * will consume a slot in the cq_ring and the cq_tail
4668 * pointer will be increased, otherwise other timeout reqs may
4669 * return in advance without waiting for enough wait_nr.
4671 prev = req;
4672 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
4673 prev->sequence++;
4674 list_del_init(&req->list);
4677 io_cqring_fill_event(req, -ETIME);
4678 io_commit_cqring(ctx);
4679 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4681 io_cqring_ev_posted(ctx);
4682 req_set_fail_links(req);
4683 io_put_req(req);
4684 return HRTIMER_NORESTART;
4687 static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
4689 struct io_kiocb *req;
4690 int ret = -ENOENT;
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;
4700 if (ret == -ENOENT)
4701 return ret;
4703 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
4704 if (ret == -1)
4705 return -EALREADY;
4707 req_set_fail_links(req);
4708 io_cqring_fill_event(req, -ECANCELED);
4709 io_put_req(req);
4710 return 0;
4713 static int io_timeout_remove_prep(struct io_kiocb *req,
4714 const struct io_uring_sqe *sqe)
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;
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;
4726 return 0;
4730 * Remove or update an existing timeout command
4732 static int io_timeout_remove(struct io_kiocb *req)
4734 struct io_ring_ctx *ctx = req->ctx;
4735 int ret;
4737 spin_lock_irq(&ctx->completion_lock);
4738 ret = io_timeout_cancel(ctx, req->timeout.addr);
4740 io_cqring_fill_event(req, ret);
4741 io_commit_cqring(ctx);
4742 spin_unlock_irq(&ctx->completion_lock);
4743 io_cqring_ev_posted(ctx);
4744 if (ret < 0)
4745 req_set_fail_links(req);
4746 io_put_req(req);
4747 return 0;
4750 static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4751 bool is_timeout_link)
4753 struct io_timeout_data *data;
4754 unsigned flags;
4756 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4757 return -EINVAL;
4758 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
4759 return -EINVAL;
4760 if (sqe->off && is_timeout_link)
4761 return -EINVAL;
4762 flags = READ_ONCE(sqe->timeout_flags);
4763 if (flags & ~IORING_TIMEOUT_ABS)
4764 return -EINVAL;
4766 req->timeout.count = READ_ONCE(sqe->off);
4768 if (!req->io && io_alloc_async_ctx(req))
4769 return -ENOMEM;
4771 data = &req->io->timeout;
4772 data->req = req;
4773 req->flags |= REQ_F_TIMEOUT;
4775 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
4776 return -EFAULT;
4778 if (flags & IORING_TIMEOUT_ABS)
4779 data->mode = HRTIMER_MODE_ABS;
4780 else
4781 data->mode = HRTIMER_MODE_REL;
4783 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
4784 return 0;
4787 static int io_timeout(struct io_kiocb *req)
4789 struct io_ring_ctx *ctx = req->ctx;
4790 struct io_timeout_data *data;
4791 struct list_head *entry;
4792 unsigned span = 0;
4793 u32 count = req->timeout.count;
4794 u32 seq = req->sequence;
4796 data = &req->io->timeout;
4799 * sqe->off holds how many events that need to occur for this
4800 * timeout event to be satisfied. If it isn't set, then this is
4801 * a pure timeout request, sequence isn't used.
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;
4810 req->sequence = seq + count;
4813 * Insertion sort, ensuring the first entry in the list is always
4814 * the one we need first.
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);
4819 unsigned nxt_seq;
4820 long long tmp, tmp_nxt;
4821 u32 nxt_offset = nxt->timeout.count;
4823 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
4824 continue;
4827 * Since seq + count can overflow, use type long
4828 * long to store it.
4830 tmp = (long long)seq + count;
4831 nxt_seq = nxt->sequence - nxt_offset;
4832 tmp_nxt = (long long)nxt_seq + nxt_offset;
4835 * cached_sq_head may overflow, and it will never overflow twice
4836 * once there is some timeout req still be valid.
4838 if (seq < nxt_seq)
4839 tmp += UINT_MAX;
4841 if (tmp > tmp_nxt)
4842 break;
4845 * Sequence of reqs after the insert one and itself should
4846 * be adjusted because each timeout req consumes a slot.
4848 span++;
4849 nxt->sequence++;
4851 req->sequence -= span;
4852 add:
4853 list_add(&req->list, entry);
4854 data->timer.function = io_timeout_fn;
4855 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
4856 spin_unlock_irq(&ctx->completion_lock);
4857 return 0;
4860 static bool io_cancel_cb(struct io_wq_work *work, void *data)
4862 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
4864 return req->user_data == (unsigned long) data;
4867 static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
4869 enum io_wq_cancel cancel_ret;
4870 int ret = 0;
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;
4885 return ret;
4888 static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
4889 struct io_kiocb *req, __u64 sqe_addr,
4890 int success_ret)
4892 unsigned long flags;
4893 int ret;
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;
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);
4906 done:
4907 if (!ret)
4908 ret = success_ret;
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);
4914 if (ret < 0)
4915 req_set_fail_links(req);
4916 io_put_req(req);
4919 static int io_async_cancel_prep(struct io_kiocb *req,
4920 const struct io_uring_sqe *sqe)
4922 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4923 return -EINVAL;
4924 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
4925 sqe->cancel_flags)
4926 return -EINVAL;
4928 req->cancel.addr = READ_ONCE(sqe->addr);
4929 return 0;
4932 static int io_async_cancel(struct io_kiocb *req)
4934 struct io_ring_ctx *ctx = req->ctx;
4936 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
4937 return 0;
4940 static int io_files_update_prep(struct io_kiocb *req,
4941 const struct io_uring_sqe *sqe)
4943 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
4944 return -EINVAL;
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;
4954 static int io_files_update(struct io_kiocb *req, bool force_nonblock)
4956 struct io_ring_ctx *ctx = req->ctx;
4957 struct io_uring_files_update up;
4958 int ret;
4960 if (force_nonblock)
4961 return -EAGAIN;
4963 up.offset = req->files_update.offset;
4964 up.fds = req->files_update.arg;
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);
4970 if (ret < 0)
4971 req_set_fail_links(req);
4972 io_cqring_add_event(req, ret);
4973 io_put_req(req);
4974 return 0;
4977 static int io_req_defer_prep(struct io_kiocb *req,
4978 const struct io_uring_sqe *sqe)
4980 ssize_t ret = 0;
4982 if (!sqe)
4983 return 0;
4985 if (io_op_defs[req->opcode].file_table) {
4986 ret = io_grab_files(req);
4987 if (unlikely(ret))
4988 return ret;
4991 io_req_work_grab_env(req, &io_op_defs[req->opcode]);
4993 switch (req->opcode) {
4994 case IORING_OP_NOP:
4995 break;
4996 case IORING_OP_READV:
4997 case IORING_OP_READ_FIXED:
4998 case IORING_OP_READ:
4999 ret = io_read_prep(req, sqe, true);
5000 break;
5001 case IORING_OP_WRITEV:
5002 case IORING_OP_WRITE_FIXED:
5003 case IORING_OP_WRITE:
5004 ret = io_write_prep(req, sqe, true);
5005 break;
5006 case IORING_OP_POLL_ADD:
5007 ret = io_poll_add_prep(req, sqe);
5008 break;
5009 case IORING_OP_POLL_REMOVE:
5010 ret = io_poll_remove_prep(req, sqe);
5011 break;
5012 case IORING_OP_FSYNC:
5013 ret = io_prep_fsync(req, sqe);
5014 break;
5015 case IORING_OP_SYNC_FILE_RANGE:
5016 ret = io_prep_sfr(req, sqe);
5017 break;
5018 case IORING_OP_SENDMSG:
5019 case IORING_OP_SEND:
5020 ret = io_sendmsg_prep(req, sqe);
5021 break;
5022 case IORING_OP_RECVMSG:
5023 case IORING_OP_RECV:
5024 ret = io_recvmsg_prep(req, sqe);
5025 break;
5026 case IORING_OP_CONNECT:
5027 ret = io_connect_prep(req, sqe);
5028 break;
5029 case IORING_OP_TIMEOUT:
5030 ret = io_timeout_prep(req, sqe, false);
5031 break;
5032 case IORING_OP_TIMEOUT_REMOVE:
5033 ret = io_timeout_remove_prep(req, sqe);
5034 break;
5035 case IORING_OP_ASYNC_CANCEL:
5036 ret = io_async_cancel_prep(req, sqe);
5037 break;
5038 case IORING_OP_LINK_TIMEOUT:
5039 ret = io_timeout_prep(req, sqe, true);
5040 break;
5041 case IORING_OP_ACCEPT:
5042 ret = io_accept_prep(req, sqe);
5043 break;
5044 case IORING_OP_FALLOCATE:
5045 ret = io_fallocate_prep(req, sqe);
5046 break;
5047 case IORING_OP_OPENAT:
5048 ret = io_openat_prep(req, sqe);
5049 break;
5050 case IORING_OP_CLOSE:
5051 ret = io_close_prep(req, sqe);
5052 break;
5053 case IORING_OP_FILES_UPDATE:
5054 ret = io_files_update_prep(req, sqe);
5055 break;
5056 case IORING_OP_STATX:
5057 ret = io_statx_prep(req, sqe);
5058 break;
5059 case IORING_OP_FADVISE:
5060 ret = io_fadvise_prep(req, sqe);
5061 break;
5062 case IORING_OP_MADVISE:
5063 ret = io_madvise_prep(req, sqe);
5064 break;
5065 case IORING_OP_OPENAT2:
5066 ret = io_openat2_prep(req, sqe);
5067 break;
5068 case IORING_OP_EPOLL_CTL:
5069 ret = io_epoll_ctl_prep(req, sqe);
5070 break;
5071 case IORING_OP_SPLICE:
5072 ret = io_splice_prep(req, sqe);
5073 break;
5074 case IORING_OP_PROVIDE_BUFFERS:
5075 ret = io_provide_buffers_prep(req, sqe);
5076 break;
5077 case IORING_OP_REMOVE_BUFFERS:
5078 ret = io_remove_buffers_prep(req, sqe);
5079 break;
5080 default:
5081 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5082 req->opcode);
5083 ret = -EINVAL;
5084 break;
5087 return ret;
5090 static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5092 struct io_ring_ctx *ctx = req->ctx;
5093 int ret;
5095 /* Still need defer if there is pending req in defer list. */
5096 if (!req_need_defer(req) && list_empty_careful(&ctx->defer_list))
5097 return 0;
5099 if (!req->io) {
5100 if (io_alloc_async_ctx(req))
5101 return -EAGAIN;
5102 ret = io_req_defer_prep(req, sqe);
5103 if (ret < 0)
5104 return ret;
5107 spin_lock_irq(&ctx->completion_lock);
5108 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
5109 spin_unlock_irq(&ctx->completion_lock);
5110 return 0;
5113 trace_io_uring_defer(ctx, req, req->user_data);
5114 list_add_tail(&req->list, &ctx->defer_list);
5115 spin_unlock_irq(&ctx->completion_lock);
5116 return -EIOCBQUEUED;
5119 static void io_cleanup_req(struct io_kiocb *req)
5121 struct io_async_ctx *io = req->io;
5123 switch (req->opcode) {
5124 case IORING_OP_READV:
5125 case IORING_OP_READ_FIXED:
5126 case IORING_OP_READ:
5127 if (req->flags & REQ_F_BUFFER_SELECTED)
5128 kfree((void *)(unsigned long)req->rw.addr);
5129 /* fallthrough */
5130 case IORING_OP_WRITEV:
5131 case IORING_OP_WRITE_FIXED:
5132 case IORING_OP_WRITE:
5133 if (io->rw.iov != io->rw.fast_iov)
5134 kfree(io->rw.iov);
5135 break;
5136 case IORING_OP_RECVMSG:
5137 if (req->flags & REQ_F_BUFFER_SELECTED)
5138 kfree(req->sr_msg.kbuf);
5139 /* fallthrough */
5140 case IORING_OP_SENDMSG:
5141 if (io->msg.iov != io->msg.fast_iov)
5142 kfree(io->msg.iov);
5143 break;
5144 case IORING_OP_RECV:
5145 if (req->flags & REQ_F_BUFFER_SELECTED)
5146 kfree(req->sr_msg.kbuf);
5147 break;
5148 case IORING_OP_OPENAT:
5149 case IORING_OP_OPENAT2:
5150 case IORING_OP_STATX:
5151 putname(req->open.filename);
5152 break;
5153 case IORING_OP_SPLICE:
5154 io_put_file(req, req->splice.file_in,
5155 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5156 break;
5159 req->flags &= ~REQ_F_NEED_CLEANUP;
5162 static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
5163 bool force_nonblock)
5165 struct io_ring_ctx *ctx = req->ctx;
5166 int ret;
5168 switch (req->opcode) {
5169 case IORING_OP_NOP:
5170 ret = io_nop(req);
5171 break;
5172 case IORING_OP_READV:
5173 case IORING_OP_READ_FIXED:
5174 case IORING_OP_READ:
5175 if (sqe) {
5176 ret = io_read_prep(req, sqe, force_nonblock);
5177 if (ret < 0)
5178 break;
5180 ret = io_read(req, force_nonblock);
5181 break;
5182 case IORING_OP_WRITEV:
5183 case IORING_OP_WRITE_FIXED:
5184 case IORING_OP_WRITE:
5185 if (sqe) {
5186 ret = io_write_prep(req, sqe, force_nonblock);
5187 if (ret < 0)
5188 break;
5190 ret = io_write(req, force_nonblock);
5191 break;
5192 case IORING_OP_FSYNC:
5193 if (sqe) {
5194 ret = io_prep_fsync(req, sqe);
5195 if (ret < 0)
5196 break;
5198 ret = io_fsync(req, force_nonblock);
5199 break;
5200 case IORING_OP_POLL_ADD:
5201 if (sqe) {
5202 ret = io_poll_add_prep(req, sqe);
5203 if (ret)
5204 break;
5206 ret = io_poll_add(req);
5207 break;
5208 case IORING_OP_POLL_REMOVE:
5209 if (sqe) {
5210 ret = io_poll_remove_prep(req, sqe);
5211 if (ret < 0)
5212 break;
5214 ret = io_poll_remove(req);
5215 break;
5216 case IORING_OP_SYNC_FILE_RANGE:
5217 if (sqe) {
5218 ret = io_prep_sfr(req, sqe);
5219 if (ret < 0)
5220 break;
5222 ret = io_sync_file_range(req, force_nonblock);
5223 break;
5224 case IORING_OP_SENDMSG:
5225 case IORING_OP_SEND:
5226 if (sqe) {
5227 ret = io_sendmsg_prep(req, sqe);
5228 if (ret < 0)
5229 break;
5231 if (req->opcode == IORING_OP_SENDMSG)
5232 ret = io_sendmsg(req, force_nonblock);
5233 else
5234 ret = io_send(req, force_nonblock);
5235 break;
5236 case IORING_OP_RECVMSG:
5237 case IORING_OP_RECV:
5238 if (sqe) {
5239 ret = io_recvmsg_prep(req, sqe);
5240 if (ret)
5241 break;
5243 if (req->opcode == IORING_OP_RECVMSG)
5244 ret = io_recvmsg(req, force_nonblock);
5245 else
5246 ret = io_recv(req, force_nonblock);
5247 break;
5248 case IORING_OP_TIMEOUT:
5249 if (sqe) {
5250 ret = io_timeout_prep(req, sqe, false);
5251 if (ret)
5252 break;
5254 ret = io_timeout(req);
5255 break;
5256 case IORING_OP_TIMEOUT_REMOVE:
5257 if (sqe) {
5258 ret = io_timeout_remove_prep(req, sqe);
5259 if (ret)
5260 break;
5262 ret = io_timeout_remove(req);
5263 break;
5264 case IORING_OP_ACCEPT:
5265 if (sqe) {
5266 ret = io_accept_prep(req, sqe);
5267 if (ret)
5268 break;
5270 ret = io_accept(req, force_nonblock);
5271 break;
5272 case IORING_OP_CONNECT:
5273 if (sqe) {
5274 ret = io_connect_prep(req, sqe);
5275 if (ret)
5276 break;
5278 ret = io_connect(req, force_nonblock);
5279 break;
5280 case IORING_OP_ASYNC_CANCEL:
5281 if (sqe) {
5282 ret = io_async_cancel_prep(req, sqe);
5283 if (ret)
5284 break;
5286 ret = io_async_cancel(req);
5287 break;
5288 case IORING_OP_FALLOCATE:
5289 if (sqe) {
5290 ret = io_fallocate_prep(req, sqe);
5291 if (ret)
5292 break;
5294 ret = io_fallocate(req, force_nonblock);
5295 break;
5296 case IORING_OP_OPENAT:
5297 if (sqe) {
5298 ret = io_openat_prep(req, sqe);
5299 if (ret)
5300 break;
5302 ret = io_openat(req, force_nonblock);
5303 break;
5304 case IORING_OP_CLOSE:
5305 if (sqe) {
5306 ret = io_close_prep(req, sqe);
5307 if (ret)
5308 break;
5310 ret = io_close(req, force_nonblock);
5311 break;
5312 case IORING_OP_FILES_UPDATE:
5313 if (sqe) {
5314 ret = io_files_update_prep(req, sqe);
5315 if (ret)
5316 break;
5318 ret = io_files_update(req, force_nonblock);
5319 break;
5320 case IORING_OP_STATX:
5321 if (sqe) {
5322 ret = io_statx_prep(req, sqe);
5323 if (ret)
5324 break;
5326 ret = io_statx(req, force_nonblock);
5327 break;
5328 case IORING_OP_FADVISE:
5329 if (sqe) {
5330 ret = io_fadvise_prep(req, sqe);
5331 if (ret)
5332 break;
5334 ret = io_fadvise(req, force_nonblock);
5335 break;
5336 case IORING_OP_MADVISE:
5337 if (sqe) {
5338 ret = io_madvise_prep(req, sqe);
5339 if (ret)
5340 break;
5342 ret = io_madvise(req, force_nonblock);
5343 break;
5344 case IORING_OP_OPENAT2:
5345 if (sqe) {
5346 ret = io_openat2_prep(req, sqe);
5347 if (ret)
5348 break;
5350 ret = io_openat2(req, force_nonblock);
5351 break;
5352 case IORING_OP_EPOLL_CTL:
5353 if (sqe) {
5354 ret = io_epoll_ctl_prep(req, sqe);
5355 if (ret)
5356 break;
5358 ret = io_epoll_ctl(req, force_nonblock);
5359 break;
5360 case IORING_OP_SPLICE:
5361 if (sqe) {
5362 ret = io_splice_prep(req, sqe);
5363 if (ret < 0)
5364 break;
5366 ret = io_splice(req, force_nonblock);
5367 break;
5368 case IORING_OP_PROVIDE_BUFFERS:
5369 if (sqe) {
5370 ret = io_provide_buffers_prep(req, sqe);
5371 if (ret)
5372 break;
5374 ret = io_provide_buffers(req, force_nonblock);
5375 break;
5376 case IORING_OP_REMOVE_BUFFERS:
5377 if (sqe) {
5378 ret = io_remove_buffers_prep(req, sqe);
5379 if (ret)
5380 break;
5382 ret = io_remove_buffers(req, force_nonblock);
5383 break;
5384 default:
5385 ret = -EINVAL;
5386 break;
5389 if (ret)
5390 return ret;
5392 /* If the op doesn't have a file, we're not polling for it */
5393 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
5394 const bool in_async = io_wq_current_is_worker();
5396 if (req->result == -EAGAIN)
5397 return -EAGAIN;
5399 /* workqueue context doesn't hold uring_lock, grab it now */
5400 if (in_async)
5401 mutex_lock(&ctx->uring_lock);
5403 io_iopoll_req_issued(req);
5405 if (in_async)
5406 mutex_unlock(&ctx->uring_lock);
5409 return 0;
5412 static void io_wq_submit_work(struct io_wq_work **workptr)
5414 struct io_wq_work *work = *workptr;
5415 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5416 int ret = 0;
5418 /* if NO_CANCEL is set, we must still run the work */
5419 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
5420 IO_WQ_WORK_CANCEL) {
5421 ret = -ECANCELED;
5424 if (!ret) {
5425 do {
5426 ret = io_issue_sqe(req, NULL, false);
5428 * We can get EAGAIN for polled IO even though we're
5429 * forcing a sync submission from here, since we can't
5430 * wait for request slots on the block side.
5432 if (ret != -EAGAIN)
5433 break;
5434 cond_resched();
5435 } while (1);
5438 if (ret) {
5439 req_set_fail_links(req);
5440 io_cqring_add_event(req, ret);
5441 io_put_req(req);
5444 io_steal_work(req, workptr);
5447 static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
5448 int index)
5450 struct fixed_file_table *table;
5452 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
5453 return table->files[index & IORING_FILE_TABLE_MASK];;
5456 static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
5457 int fd, struct file **out_file, bool fixed)
5459 struct io_ring_ctx *ctx = req->ctx;
5460 struct file *file;
5462 if (fixed) {
5463 if (unlikely(!ctx->file_data ||
5464 (unsigned) fd >= ctx->nr_user_files))
5465 return -EBADF;
5466 fd = array_index_nospec(fd, ctx->nr_user_files);
5467 file = io_file_from_index(ctx, fd);
5468 if (!file)
5469 return -EBADF;
5470 req->fixed_file_refs = ctx->file_data->cur_refs;
5471 percpu_ref_get(req->fixed_file_refs);
5472 } else {
5473 trace_io_uring_file_get(ctx, fd);
5474 file = __io_file_get(state, fd);
5475 if (unlikely(!file))
5476 return -EBADF;
5479 *out_file = file;
5480 return 0;
5483 static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
5484 int fd)
5486 bool fixed;
5488 fixed = (req->flags & REQ_F_FIXED_FILE) != 0;
5489 if (unlikely(!fixed && req->needs_fixed_file))
5490 return -EBADF;
5492 return io_file_get(state, req, fd, &req->file, fixed);
5495 static int io_grab_files(struct io_kiocb *req)
5497 int ret = -EBADF;
5498 struct io_ring_ctx *ctx = req->ctx;
5500 if (req->work.files || (req->flags & REQ_F_NO_FILE_TABLE))
5501 return 0;
5502 if (!ctx->ring_file)
5503 return -EBADF;
5505 rcu_read_lock();
5506 spin_lock_irq(&ctx->inflight_lock);
5508 * We use the f_ops->flush() handler to ensure that we can flush
5509 * out work accessing these files if the fd is closed. Check if
5510 * the fd has changed since we started down this path, and disallow
5511 * this operation if it has.
5513 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
5514 list_add(&req->inflight_entry, &ctx->inflight_list);
5515 req->flags |= REQ_F_INFLIGHT;
5516 req->work.files = current->files;
5517 ret = 0;
5519 spin_unlock_irq(&ctx->inflight_lock);
5520 rcu_read_unlock();
5522 return ret;
5525 static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
5527 struct io_timeout_data *data = container_of(timer,
5528 struct io_timeout_data, timer);
5529 struct io_kiocb *req = data->req;
5530 struct io_ring_ctx *ctx = req->ctx;
5531 struct io_kiocb *prev = NULL;
5532 unsigned long flags;
5534 spin_lock_irqsave(&ctx->completion_lock, flags);
5537 * We don't expect the list to be empty, that will only happen if we
5538 * race with the completion of the linked work.
5540 if (!list_empty(&req->link_list)) {
5541 prev = list_entry(req->link_list.prev, struct io_kiocb,
5542 link_list);
5543 if (refcount_inc_not_zero(&prev->refs)) {
5544 list_del_init(&req->link_list);
5545 prev->flags &= ~REQ_F_LINK_TIMEOUT;
5546 } else
5547 prev = NULL;
5550 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5552 if (prev) {
5553 req_set_fail_links(prev);
5554 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
5555 io_put_req(prev);
5556 } else {
5557 io_cqring_add_event(req, -ETIME);
5558 io_put_req(req);
5560 return HRTIMER_NORESTART;
5563 static void io_queue_linked_timeout(struct io_kiocb *req)
5565 struct io_ring_ctx *ctx = req->ctx;
5568 * If the list is now empty, then our linked request finished before
5569 * we got a chance to setup the timer
5571 spin_lock_irq(&ctx->completion_lock);
5572 if (!list_empty(&req->link_list)) {
5573 struct io_timeout_data *data = &req->io->timeout;
5575 data->timer.function = io_link_timeout_fn;
5576 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
5577 data->mode);
5579 spin_unlock_irq(&ctx->completion_lock);
5581 /* drop submission reference */
5582 io_put_req(req);
5585 static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
5587 struct io_kiocb *nxt;
5589 if (!(req->flags & REQ_F_LINK_HEAD))
5590 return NULL;
5591 /* for polled retry, if flag is set, we already went through here */
5592 if (req->flags & REQ_F_POLLED)
5593 return NULL;
5595 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
5596 link_list);
5597 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
5598 return NULL;
5600 req->flags |= REQ_F_LINK_TIMEOUT;
5601 return nxt;
5604 static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5606 struct io_kiocb *linked_timeout;
5607 struct io_kiocb *nxt;
5608 const struct cred *old_creds = NULL;
5609 int ret;
5611 again:
5612 linked_timeout = io_prep_linked_timeout(req);
5614 if (req->work.creds && req->work.creds != current_cred()) {
5615 if (old_creds)
5616 revert_creds(old_creds);
5617 if (old_creds == req->work.creds)
5618 old_creds = NULL; /* restored original creds */
5619 else
5620 old_creds = override_creds(req->work.creds);
5623 ret = io_issue_sqe(req, sqe, true);
5626 * We async punt it if the file wasn't marked NOWAIT, or if the file
5627 * doesn't support non-blocking read/write attempts
5629 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
5630 (req->flags & REQ_F_MUST_PUNT))) {
5631 if (io_arm_poll_handler(req)) {
5632 if (linked_timeout)
5633 io_queue_linked_timeout(linked_timeout);
5634 goto exit;
5636 punt:
5637 if (io_op_defs[req->opcode].file_table) {
5638 ret = io_grab_files(req);
5639 if (ret)
5640 goto err;
5644 * Queued up for async execution, worker will release
5645 * submit reference when the iocb is actually submitted.
5647 io_queue_async_work(req);
5648 goto exit;
5651 err:
5652 nxt = NULL;
5653 /* drop submission reference */
5654 io_put_req_find_next(req, &nxt);
5656 if (linked_timeout) {
5657 if (!ret)
5658 io_queue_linked_timeout(linked_timeout);
5659 else
5660 io_put_req(linked_timeout);
5663 /* and drop final reference, if we failed */
5664 if (ret) {
5665 io_cqring_add_event(req, ret);
5666 req_set_fail_links(req);
5667 io_put_req(req);
5669 if (nxt) {
5670 req = nxt;
5672 if (req->flags & REQ_F_FORCE_ASYNC)
5673 goto punt;
5674 goto again;
5676 exit:
5677 if (old_creds)
5678 revert_creds(old_creds);
5681 static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5683 int ret;
5685 ret = io_req_defer(req, sqe);
5686 if (ret) {
5687 if (ret != -EIOCBQUEUED) {
5688 fail_req:
5689 io_cqring_add_event(req, ret);
5690 req_set_fail_links(req);
5691 io_double_put_req(req);
5693 } else if (req->flags & REQ_F_FORCE_ASYNC) {
5694 if (!req->io) {
5695 ret = -EAGAIN;
5696 if (io_alloc_async_ctx(req))
5697 goto fail_req;
5698 ret = io_req_defer_prep(req, sqe);
5699 if (unlikely(ret < 0))
5700 goto fail_req;
5704 * Never try inline submit of IOSQE_ASYNC is set, go straight
5705 * to async execution.
5707 req->work.flags |= IO_WQ_WORK_CONCURRENT;
5708 io_queue_async_work(req);
5709 } else {
5710 __io_queue_sqe(req, sqe);
5714 static inline void io_queue_link_head(struct io_kiocb *req)
5716 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
5717 io_cqring_add_event(req, -ECANCELED);
5718 io_double_put_req(req);
5719 } else
5720 io_queue_sqe(req, NULL);
5723 static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
5724 struct io_submit_state *state, struct io_kiocb **link)
5726 struct io_ring_ctx *ctx = req->ctx;
5727 int ret;
5730 * If we already have a head request, queue this one for async
5731 * submittal once the head completes. If we don't have a head but
5732 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
5733 * submitted sync once the chain is complete. If none of those
5734 * conditions are true (normal request), then just queue it.
5736 if (*link) {
5737 struct io_kiocb *head = *link;
5740 * Taking sequential execution of a link, draining both sides
5741 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
5742 * requests in the link. So, it drains the head and the
5743 * next after the link request. The last one is done via
5744 * drain_next flag to persist the effect across calls.
5746 if (req->flags & REQ_F_IO_DRAIN) {
5747 head->flags |= REQ_F_IO_DRAIN;
5748 ctx->drain_next = 1;
5750 if (io_alloc_async_ctx(req))
5751 return -EAGAIN;
5753 ret = io_req_defer_prep(req, sqe);
5754 if (ret) {
5755 /* fail even hard links since we don't submit */
5756 head->flags |= REQ_F_FAIL_LINK;
5757 return ret;
5759 trace_io_uring_link(ctx, req, head);
5760 list_add_tail(&req->link_list, &head->link_list);
5762 /* last request of a link, enqueue the link */
5763 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
5764 io_queue_link_head(head);
5765 *link = NULL;
5767 } else {
5768 if (unlikely(ctx->drain_next)) {
5769 req->flags |= REQ_F_IO_DRAIN;
5770 ctx->drain_next = 0;
5772 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
5773 req->flags |= REQ_F_LINK_HEAD;
5774 INIT_LIST_HEAD(&req->link_list);
5776 if (io_alloc_async_ctx(req))
5777 return -EAGAIN;
5779 ret = io_req_defer_prep(req, sqe);
5780 if (ret)
5781 req->flags |= REQ_F_FAIL_LINK;
5782 *link = req;
5783 } else {
5784 io_queue_sqe(req, sqe);
5788 return 0;
5792 * Batched submission is done, ensure local IO is flushed out.
5794 static void io_submit_state_end(struct io_submit_state *state)
5796 blk_finish_plug(&state->plug);
5797 io_file_put(state);
5798 if (state->free_reqs)
5799 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
5803 * Start submission side cache.
5805 static void io_submit_state_start(struct io_submit_state *state,
5806 unsigned int max_ios)
5808 blk_start_plug(&state->plug);
5809 state->free_reqs = 0;
5810 state->file = NULL;
5811 state->ios_left = max_ios;
5814 static void io_commit_sqring(struct io_ring_ctx *ctx)
5816 struct io_rings *rings = ctx->rings;
5819 * Ensure any loads from the SQEs are done at this point,
5820 * since once we write the new head, the application could
5821 * write new data to them.
5823 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
5827 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
5828 * that is mapped by userspace. This means that care needs to be taken to
5829 * ensure that reads are stable, as we cannot rely on userspace always
5830 * being a good citizen. If members of the sqe are validated and then later
5831 * used, it's important that those reads are done through READ_ONCE() to
5832 * prevent a re-load down the line.
5834 static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
5836 u32 *sq_array = ctx->sq_array;
5837 unsigned head;
5840 * The cached sq head (or cq tail) serves two purposes:
5842 * 1) allows us to batch the cost of updating the user visible
5843 * head updates.
5844 * 2) allows the kernel side to track the head on its own, even
5845 * though the application is the one updating it.
5847 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
5848 if (likely(head < ctx->sq_entries))
5849 return &ctx->sq_sqes[head];
5851 /* drop invalid entries */
5852 ctx->cached_sq_dropped++;
5853 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
5854 return NULL;
5857 static inline void io_consume_sqe(struct io_ring_ctx *ctx)
5859 ctx->cached_sq_head++;
5862 #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
5863 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
5864 IOSQE_BUFFER_SELECT)
5866 static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
5867 const struct io_uring_sqe *sqe,
5868 struct io_submit_state *state, bool async)
5870 unsigned int sqe_flags;
5871 int id;
5874 * All io need record the previous position, if LINK vs DARIN,
5875 * it can be used to mark the position of the first IO in the
5876 * link list.
5878 req->sequence = ctx->cached_sq_head - ctx->cached_sq_dropped;
5879 req->opcode = READ_ONCE(sqe->opcode);
5880 req->user_data = READ_ONCE(sqe->user_data);
5881 req->io = NULL;
5882 req->file = NULL;
5883 req->ctx = ctx;
5884 req->flags = 0;
5885 /* one is dropped after submission, the other at completion */
5886 refcount_set(&req->refs, 2);
5887 req->task = NULL;
5888 req->result = 0;
5889 req->needs_fixed_file = async;
5890 INIT_IO_WORK(&req->work, io_wq_submit_work);
5892 if (unlikely(req->opcode >= IORING_OP_LAST))
5893 return -EINVAL;
5895 if (io_op_defs[req->opcode].needs_mm && !current->mm) {
5896 if (unlikely(!mmget_not_zero(ctx->sqo_mm)))
5897 return -EFAULT;
5898 use_mm(ctx->sqo_mm);
5901 sqe_flags = READ_ONCE(sqe->flags);
5902 /* enforce forwards compatibility on users */
5903 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
5904 return -EINVAL;
5906 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
5907 !io_op_defs[req->opcode].buffer_select)
5908 return -EOPNOTSUPP;
5910 id = READ_ONCE(sqe->personality);
5911 if (id) {
5912 req->work.creds = idr_find(&ctx->personality_idr, id);
5913 if (unlikely(!req->work.creds))
5914 return -EINVAL;
5915 get_cred(req->work.creds);
5918 /* same numerical values with corresponding REQ_F_*, safe to copy */
5919 req->flags |= sqe_flags & (IOSQE_IO_DRAIN | IOSQE_IO_HARDLINK |
5920 IOSQE_ASYNC | IOSQE_FIXED_FILE |
5921 IOSQE_BUFFER_SELECT | IOSQE_IO_LINK);
5923 if (!io_op_defs[req->opcode].needs_file)
5924 return 0;
5926 return io_req_set_file(state, req, READ_ONCE(sqe->fd));
5929 static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
5930 struct file *ring_file, int ring_fd, bool async)
5932 struct io_submit_state state, *statep = NULL;
5933 struct io_kiocb *link = NULL;
5934 int i, submitted = 0;
5936 /* if we have a backlog and couldn't flush it all, return BUSY */
5937 if (test_bit(0, &ctx->sq_check_overflow)) {
5938 if (!list_empty(&ctx->cq_overflow_list) &&
5939 !io_cqring_overflow_flush(ctx, false))
5940 return -EBUSY;
5943 /* make sure SQ entry isn't read before tail */
5944 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
5946 if (!percpu_ref_tryget_many(&ctx->refs, nr))
5947 return -EAGAIN;
5949 if (nr > IO_PLUG_THRESHOLD) {
5950 io_submit_state_start(&state, nr);
5951 statep = &state;
5954 ctx->ring_fd = ring_fd;
5955 ctx->ring_file = ring_file;
5957 for (i = 0; i < nr; i++) {
5958 const struct io_uring_sqe *sqe;
5959 struct io_kiocb *req;
5960 int err;
5962 sqe = io_get_sqe(ctx);
5963 if (unlikely(!sqe)) {
5964 io_consume_sqe(ctx);
5965 break;
5967 req = io_alloc_req(ctx, statep);
5968 if (unlikely(!req)) {
5969 if (!submitted)
5970 submitted = -EAGAIN;
5971 break;
5974 err = io_init_req(ctx, req, sqe, statep, async);
5975 io_consume_sqe(ctx);
5976 /* will complete beyond this point, count as submitted */
5977 submitted++;
5979 if (unlikely(err)) {
5980 fail_req:
5981 io_cqring_add_event(req, err);
5982 io_double_put_req(req);
5983 break;
5986 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
5987 true, async);
5988 err = io_submit_sqe(req, sqe, statep, &link);
5989 if (err)
5990 goto fail_req;
5993 if (unlikely(submitted != nr)) {
5994 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
5996 percpu_ref_put_many(&ctx->refs, nr - ref_used);
5998 if (link)
5999 io_queue_link_head(link);
6000 if (statep)
6001 io_submit_state_end(&state);
6003 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6004 io_commit_sqring(ctx);
6006 return submitted;
6009 static inline void io_sq_thread_drop_mm(struct io_ring_ctx *ctx)
6011 struct mm_struct *mm = current->mm;
6013 if (mm) {
6014 unuse_mm(mm);
6015 mmput(mm);
6019 static int io_sq_thread(void *data)
6021 struct io_ring_ctx *ctx = data;
6022 const struct cred *old_cred;
6023 mm_segment_t old_fs;
6024 DEFINE_WAIT(wait);
6025 unsigned long timeout;
6026 int ret = 0;
6028 complete(&ctx->completions[1]);
6030 old_fs = get_fs();
6031 set_fs(USER_DS);
6032 old_cred = override_creds(ctx->creds);
6034 timeout = jiffies + ctx->sq_thread_idle;
6035 while (!kthread_should_park()) {
6036 unsigned int to_submit;
6038 if (!list_empty(&ctx->poll_list)) {
6039 unsigned nr_events = 0;
6041 mutex_lock(&ctx->uring_lock);
6042 if (!list_empty(&ctx->poll_list))
6043 io_iopoll_getevents(ctx, &nr_events, 0);
6044 else
6045 timeout = jiffies + ctx->sq_thread_idle;
6046 mutex_unlock(&ctx->uring_lock);
6049 to_submit = io_sqring_entries(ctx);
6052 * If submit got -EBUSY, flag us as needing the application
6053 * to enter the kernel to reap and flush events.
6055 if (!to_submit || ret == -EBUSY) {
6057 * Drop cur_mm before scheduling, we can't hold it for
6058 * long periods (or over schedule()). Do this before
6059 * adding ourselves to the waitqueue, as the unuse/drop
6060 * may sleep.
6062 io_sq_thread_drop_mm(ctx);
6065 * We're polling. If we're within the defined idle
6066 * period, then let us spin without work before going
6067 * to sleep. The exception is if we got EBUSY doing
6068 * more IO, we should wait for the application to
6069 * reap events and wake us up.
6071 if (!list_empty(&ctx->poll_list) ||
6072 (!time_after(jiffies, timeout) && ret != -EBUSY &&
6073 !percpu_ref_is_dying(&ctx->refs))) {
6074 if (current->task_works)
6075 task_work_run();
6076 cond_resched();
6077 continue;
6080 prepare_to_wait(&ctx->sqo_wait, &wait,
6081 TASK_INTERRUPTIBLE);
6084 * While doing polled IO, before going to sleep, we need
6085 * to check if there are new reqs added to poll_list, it
6086 * is because reqs may have been punted to io worker and
6087 * will be added to poll_list later, hence check the
6088 * poll_list again.
6090 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
6091 !list_empty_careful(&ctx->poll_list)) {
6092 finish_wait(&ctx->sqo_wait, &wait);
6093 continue;
6096 /* Tell userspace we may need a wakeup call */
6097 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6098 /* make sure to read SQ tail after writing flags */
6099 smp_mb();
6101 to_submit = io_sqring_entries(ctx);
6102 if (!to_submit || ret == -EBUSY) {
6103 if (kthread_should_park()) {
6104 finish_wait(&ctx->sqo_wait, &wait);
6105 break;
6107 if (current->task_works) {
6108 task_work_run();
6109 finish_wait(&ctx->sqo_wait, &wait);
6110 continue;
6112 if (signal_pending(current))
6113 flush_signals(current);
6114 schedule();
6115 finish_wait(&ctx->sqo_wait, &wait);
6117 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6118 ret = 0;
6119 continue;
6121 finish_wait(&ctx->sqo_wait, &wait);
6123 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6126 mutex_lock(&ctx->uring_lock);
6127 ret = io_submit_sqes(ctx, to_submit, NULL, -1, true);
6128 mutex_unlock(&ctx->uring_lock);
6129 timeout = jiffies + ctx->sq_thread_idle;
6132 if (current->task_works)
6133 task_work_run();
6135 set_fs(old_fs);
6136 io_sq_thread_drop_mm(ctx);
6137 revert_creds(old_cred);
6139 kthread_parkme();
6141 return 0;
6144 struct io_wait_queue {
6145 struct wait_queue_entry wq;
6146 struct io_ring_ctx *ctx;
6147 unsigned to_wait;
6148 unsigned nr_timeouts;
6151 static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
6153 struct io_ring_ctx *ctx = iowq->ctx;
6156 * Wake up if we have enough events, or if a timeout occurred since we
6157 * started waiting. For timeouts, we always want to return to userspace,
6158 * regardless of event count.
6160 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
6161 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6164 static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6165 int wake_flags, void *key)
6167 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6168 wq);
6170 /* use noflush == true, as we can't safely rely on locking context */
6171 if (!io_should_wake(iowq, true))
6172 return -1;
6174 return autoremove_wake_function(curr, mode, wake_flags, key);
6178 * Wait until events become available, if we don't already have some. The
6179 * application must reap them itself, as they reside on the shared cq ring.
6181 static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
6182 const sigset_t __user *sig, size_t sigsz)
6184 struct io_wait_queue iowq = {
6185 .wq = {
6186 .private = current,
6187 .func = io_wake_function,
6188 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6190 .ctx = ctx,
6191 .to_wait = min_events,
6193 struct io_rings *rings = ctx->rings;
6194 int ret = 0;
6196 do {
6197 if (io_cqring_events(ctx, false) >= min_events)
6198 return 0;
6199 if (!current->task_works)
6200 break;
6201 task_work_run();
6202 } while (1);
6204 if (sig) {
6205 #ifdef CONFIG_COMPAT
6206 if (in_compat_syscall())
6207 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
6208 sigsz);
6209 else
6210 #endif
6211 ret = set_user_sigmask(sig, sigsz);
6213 if (ret)
6214 return ret;
6217 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
6218 trace_io_uring_cqring_wait(ctx, min_events);
6219 do {
6220 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
6221 TASK_INTERRUPTIBLE);
6222 if (current->task_works)
6223 task_work_run();
6224 if (io_should_wake(&iowq, false))
6225 break;
6226 schedule();
6227 if (signal_pending(current)) {
6228 ret = -EINTR;
6229 break;
6231 } while (1);
6232 finish_wait(&ctx->wait, &iowq.wq);
6234 restore_saved_sigmask_unless(ret == -EINTR);
6236 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
6239 static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
6241 #if defined(CONFIG_UNIX)
6242 if (ctx->ring_sock) {
6243 struct sock *sock = ctx->ring_sock->sk;
6244 struct sk_buff *skb;
6246 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
6247 kfree_skb(skb);
6249 #else
6250 int i;
6252 for (i = 0; i < ctx->nr_user_files; i++) {
6253 struct file *file;
6255 file = io_file_from_index(ctx, i);
6256 if (file)
6257 fput(file);
6259 #endif
6262 static void io_file_ref_kill(struct percpu_ref *ref)
6264 struct fixed_file_data *data;
6266 data = container_of(ref, struct fixed_file_data, refs);
6267 complete(&data->done);
6270 static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
6272 struct fixed_file_data *data = ctx->file_data;
6273 struct fixed_file_ref_node *ref_node = NULL;
6274 unsigned nr_tables, i;
6275 unsigned long flags;
6277 if (!data)
6278 return -ENXIO;
6280 spin_lock_irqsave(&data->lock, flags);
6281 if (!list_empty(&data->ref_list))
6282 ref_node = list_first_entry(&data->ref_list,
6283 struct fixed_file_ref_node, node);
6284 spin_unlock_irqrestore(&data->lock, flags);
6285 if (ref_node)
6286 percpu_ref_kill(&ref_node->refs);
6288 percpu_ref_kill(&data->refs);
6290 /* wait for all refs nodes to complete */
6291 wait_for_completion(&data->done);
6293 __io_sqe_files_unregister(ctx);
6294 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
6295 for (i = 0; i < nr_tables; i++)
6296 kfree(data->table[i].files);
6297 kfree(data->table);
6298 percpu_ref_exit(&data->refs);
6299 kfree(data);
6300 ctx->file_data = NULL;
6301 ctx->nr_user_files = 0;
6302 return 0;
6305 static void io_sq_thread_stop(struct io_ring_ctx *ctx)
6307 if (ctx->sqo_thread) {
6308 wait_for_completion(&ctx->completions[1]);
6310 * The park is a bit of a work-around, without it we get
6311 * warning spews on shutdown with SQPOLL set and affinity
6312 * set to a single CPU.
6314 kthread_park(ctx->sqo_thread);
6315 kthread_stop(ctx->sqo_thread);
6316 ctx->sqo_thread = NULL;
6320 static void io_finish_async(struct io_ring_ctx *ctx)
6322 io_sq_thread_stop(ctx);
6324 if (ctx->io_wq) {
6325 io_wq_destroy(ctx->io_wq);
6326 ctx->io_wq = NULL;
6330 #if defined(CONFIG_UNIX)
6332 * Ensure the UNIX gc is aware of our file set, so we are certain that
6333 * the io_uring can be safely unregistered on process exit, even if we have
6334 * loops in the file referencing.
6336 static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
6338 struct sock *sk = ctx->ring_sock->sk;
6339 struct scm_fp_list *fpl;
6340 struct sk_buff *skb;
6341 int i, nr_files;
6343 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
6344 if (!fpl)
6345 return -ENOMEM;
6347 skb = alloc_skb(0, GFP_KERNEL);
6348 if (!skb) {
6349 kfree(fpl);
6350 return -ENOMEM;
6353 skb->sk = sk;
6355 nr_files = 0;
6356 fpl->user = get_uid(ctx->user);
6357 for (i = 0; i < nr; i++) {
6358 struct file *file = io_file_from_index(ctx, i + offset);
6360 if (!file)
6361 continue;
6362 fpl->fp[nr_files] = get_file(file);
6363 unix_inflight(fpl->user, fpl->fp[nr_files]);
6364 nr_files++;
6367 if (nr_files) {
6368 fpl->max = SCM_MAX_FD;
6369 fpl->count = nr_files;
6370 UNIXCB(skb).fp = fpl;
6371 skb->destructor = unix_destruct_scm;
6372 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
6373 skb_queue_head(&sk->sk_receive_queue, skb);
6375 for (i = 0; i < nr_files; i++)
6376 fput(fpl->fp[i]);
6377 } else {
6378 kfree_skb(skb);
6379 kfree(fpl);
6382 return 0;
6386 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
6387 * causes regular reference counting to break down. We rely on the UNIX
6388 * garbage collection to take care of this problem for us.
6390 static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6392 unsigned left, total;
6393 int ret = 0;
6395 total = 0;
6396 left = ctx->nr_user_files;
6397 while (left) {
6398 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
6400 ret = __io_sqe_files_scm(ctx, this_files, total);
6401 if (ret)
6402 break;
6403 left -= this_files;
6404 total += this_files;
6407 if (!ret)
6408 return 0;
6410 while (total < ctx->nr_user_files) {
6411 struct file *file = io_file_from_index(ctx, total);
6413 if (file)
6414 fput(file);
6415 total++;
6418 return ret;
6420 #else
6421 static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6423 return 0;
6425 #endif
6427 static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
6428 unsigned nr_files)
6430 int i;
6432 for (i = 0; i < nr_tables; i++) {
6433 struct fixed_file_table *table = &ctx->file_data->table[i];
6434 unsigned this_files;
6436 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
6437 table->files = kcalloc(this_files, sizeof(struct file *),
6438 GFP_KERNEL);
6439 if (!table->files)
6440 break;
6441 nr_files -= this_files;
6444 if (i == nr_tables)
6445 return 0;
6447 for (i = 0; i < nr_tables; i++) {
6448 struct fixed_file_table *table = &ctx->file_data->table[i];
6449 kfree(table->files);
6451 return 1;
6454 static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
6456 #if defined(CONFIG_UNIX)
6457 struct sock *sock = ctx->ring_sock->sk;
6458 struct sk_buff_head list, *head = &sock->sk_receive_queue;
6459 struct sk_buff *skb;
6460 int i;
6462 __skb_queue_head_init(&list);
6465 * Find the skb that holds this file in its SCM_RIGHTS. When found,
6466 * remove this entry and rearrange the file array.
6468 skb = skb_dequeue(head);
6469 while (skb) {
6470 struct scm_fp_list *fp;
6472 fp = UNIXCB(skb).fp;
6473 for (i = 0; i < fp->count; i++) {
6474 int left;
6476 if (fp->fp[i] != file)
6477 continue;
6479 unix_notinflight(fp->user, fp->fp[i]);
6480 left = fp->count - 1 - i;
6481 if (left) {
6482 memmove(&fp->fp[i], &fp->fp[i + 1],
6483 left * sizeof(struct file *));
6485 fp->count--;
6486 if (!fp->count) {
6487 kfree_skb(skb);
6488 skb = NULL;
6489 } else {
6490 __skb_queue_tail(&list, skb);
6492 fput(file);
6493 file = NULL;
6494 break;
6497 if (!file)
6498 break;
6500 __skb_queue_tail(&list, skb);
6502 skb = skb_dequeue(head);
6505 if (skb_peek(&list)) {
6506 spin_lock_irq(&head->lock);
6507 while ((skb = __skb_dequeue(&list)) != NULL)
6508 __skb_queue_tail(head, skb);
6509 spin_unlock_irq(&head->lock);
6511 #else
6512 fput(file);
6513 #endif
6516 struct io_file_put {
6517 struct list_head list;
6518 struct file *file;
6521 static void io_file_put_work(struct work_struct *work)
6523 struct fixed_file_ref_node *ref_node;
6524 struct fixed_file_data *file_data;
6525 struct io_ring_ctx *ctx;
6526 struct io_file_put *pfile, *tmp;
6527 unsigned long flags;
6529 ref_node = container_of(work, struct fixed_file_ref_node, work);
6530 file_data = ref_node->file_data;
6531 ctx = file_data->ctx;
6533 list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) {
6534 list_del_init(&pfile->list);
6535 io_ring_file_put(ctx, pfile->file);
6536 kfree(pfile);
6539 spin_lock_irqsave(&file_data->lock, flags);
6540 list_del_init(&ref_node->node);
6541 spin_unlock_irqrestore(&file_data->lock, flags);
6543 percpu_ref_exit(&ref_node->refs);
6544 kfree(ref_node);
6545 percpu_ref_put(&file_data->refs);
6548 static void io_file_data_ref_zero(struct percpu_ref *ref)
6550 struct fixed_file_ref_node *ref_node;
6552 ref_node = container_of(ref, struct fixed_file_ref_node, refs);
6554 queue_work(system_wq, &ref_node->work);
6557 static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
6558 struct io_ring_ctx *ctx)
6560 struct fixed_file_ref_node *ref_node;
6562 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
6563 if (!ref_node)
6564 return ERR_PTR(-ENOMEM);
6566 if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero,
6567 0, GFP_KERNEL)) {
6568 kfree(ref_node);
6569 return ERR_PTR(-ENOMEM);
6571 INIT_LIST_HEAD(&ref_node->node);
6572 INIT_LIST_HEAD(&ref_node->file_list);
6573 INIT_WORK(&ref_node->work, io_file_put_work);
6574 ref_node->file_data = ctx->file_data;
6575 return ref_node;
6579 static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node)
6581 percpu_ref_exit(&ref_node->refs);
6582 kfree(ref_node);
6585 static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
6586 unsigned nr_args)
6588 __s32 __user *fds = (__s32 __user *) arg;
6589 unsigned nr_tables;
6590 struct file *file;
6591 int fd, ret = 0;
6592 unsigned i;
6593 struct fixed_file_ref_node *ref_node;
6594 unsigned long flags;
6596 if (ctx->file_data)
6597 return -EBUSY;
6598 if (!nr_args)
6599 return -EINVAL;
6600 if (nr_args > IORING_MAX_FIXED_FILES)
6601 return -EMFILE;
6603 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
6604 if (!ctx->file_data)
6605 return -ENOMEM;
6606 ctx->file_data->ctx = ctx;
6607 init_completion(&ctx->file_data->done);
6608 INIT_LIST_HEAD(&ctx->file_data->ref_list);
6609 spin_lock_init(&ctx->file_data->lock);
6611 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
6612 ctx->file_data->table = kcalloc(nr_tables,
6613 sizeof(struct fixed_file_table),
6614 GFP_KERNEL);
6615 if (!ctx->file_data->table) {
6616 kfree(ctx->file_data);
6617 ctx->file_data = NULL;
6618 return -ENOMEM;
6621 if (percpu_ref_init(&ctx->file_data->refs, io_file_ref_kill,
6622 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
6623 kfree(ctx->file_data->table);
6624 kfree(ctx->file_data);
6625 ctx->file_data = NULL;
6626 return -ENOMEM;
6629 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
6630 percpu_ref_exit(&ctx->file_data->refs);
6631 kfree(ctx->file_data->table);
6632 kfree(ctx->file_data);
6633 ctx->file_data = NULL;
6634 return -ENOMEM;
6637 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
6638 struct fixed_file_table *table;
6639 unsigned index;
6641 ret = -EFAULT;
6642 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
6643 break;
6644 /* allow sparse sets */
6645 if (fd == -1) {
6646 ret = 0;
6647 continue;
6650 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
6651 index = i & IORING_FILE_TABLE_MASK;
6652 file = fget(fd);
6654 ret = -EBADF;
6655 if (!file)
6656 break;
6659 * Don't allow io_uring instances to be registered. If UNIX
6660 * isn't enabled, then this causes a reference cycle and this
6661 * instance can never get freed. If UNIX is enabled we'll
6662 * handle it just fine, but there's still no point in allowing
6663 * a ring fd as it doesn't support regular read/write anyway.
6665 if (file->f_op == &io_uring_fops) {
6666 fput(file);
6667 break;
6669 ret = 0;
6670 table->files[index] = file;
6673 if (ret) {
6674 for (i = 0; i < ctx->nr_user_files; i++) {
6675 file = io_file_from_index(ctx, i);
6676 if (file)
6677 fput(file);
6679 for (i = 0; i < nr_tables; i++)
6680 kfree(ctx->file_data->table[i].files);
6682 kfree(ctx->file_data->table);
6683 kfree(ctx->file_data);
6684 ctx->file_data = NULL;
6685 ctx->nr_user_files = 0;
6686 return ret;
6689 ret = io_sqe_files_scm(ctx);
6690 if (ret) {
6691 io_sqe_files_unregister(ctx);
6692 return ret;
6695 ref_node = alloc_fixed_file_ref_node(ctx);
6696 if (IS_ERR(ref_node)) {
6697 io_sqe_files_unregister(ctx);
6698 return PTR_ERR(ref_node);
6701 ctx->file_data->cur_refs = &ref_node->refs;
6702 spin_lock_irqsave(&ctx->file_data->lock, flags);
6703 list_add(&ref_node->node, &ctx->file_data->ref_list);
6704 spin_unlock_irqrestore(&ctx->file_data->lock, flags);
6705 percpu_ref_get(&ctx->file_data->refs);
6706 return ret;
6709 static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
6710 int index)
6712 #if defined(CONFIG_UNIX)
6713 struct sock *sock = ctx->ring_sock->sk;
6714 struct sk_buff_head *head = &sock->sk_receive_queue;
6715 struct sk_buff *skb;
6718 * See if we can merge this file into an existing skb SCM_RIGHTS
6719 * file set. If there's no room, fall back to allocating a new skb
6720 * and filling it in.
6722 spin_lock_irq(&head->lock);
6723 skb = skb_peek(head);
6724 if (skb) {
6725 struct scm_fp_list *fpl = UNIXCB(skb).fp;
6727 if (fpl->count < SCM_MAX_FD) {
6728 __skb_unlink(skb, head);
6729 spin_unlock_irq(&head->lock);
6730 fpl->fp[fpl->count] = get_file(file);
6731 unix_inflight(fpl->user, fpl->fp[fpl->count]);
6732 fpl->count++;
6733 spin_lock_irq(&head->lock);
6734 __skb_queue_head(head, skb);
6735 } else {
6736 skb = NULL;
6739 spin_unlock_irq(&head->lock);
6741 if (skb) {
6742 fput(file);
6743 return 0;
6746 return __io_sqe_files_scm(ctx, 1, index);
6747 #else
6748 return 0;
6749 #endif
6752 static int io_queue_file_removal(struct fixed_file_data *data,
6753 struct file *file)
6755 struct io_file_put *pfile;
6756 struct percpu_ref *refs = data->cur_refs;
6757 struct fixed_file_ref_node *ref_node;
6759 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
6760 if (!pfile)
6761 return -ENOMEM;
6763 ref_node = container_of(refs, struct fixed_file_ref_node, refs);
6764 pfile->file = file;
6765 list_add(&pfile->list, &ref_node->file_list);
6767 return 0;
6770 static int __io_sqe_files_update(struct io_ring_ctx *ctx,
6771 struct io_uring_files_update *up,
6772 unsigned nr_args)
6774 struct fixed_file_data *data = ctx->file_data;
6775 struct fixed_file_ref_node *ref_node;
6776 struct file *file;
6777 __s32 __user *fds;
6778 int fd, i, err;
6779 __u32 done;
6780 unsigned long flags;
6781 bool needs_switch = false;
6783 if (check_add_overflow(up->offset, nr_args, &done))
6784 return -EOVERFLOW;
6785 if (done > ctx->nr_user_files)
6786 return -EINVAL;
6788 ref_node = alloc_fixed_file_ref_node(ctx);
6789 if (IS_ERR(ref_node))
6790 return PTR_ERR(ref_node);
6792 done = 0;
6793 fds = u64_to_user_ptr(up->fds);
6794 while (nr_args) {
6795 struct fixed_file_table *table;
6796 unsigned index;
6798 err = 0;
6799 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
6800 err = -EFAULT;
6801 break;
6803 i = array_index_nospec(up->offset, ctx->nr_user_files);
6804 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
6805 index = i & IORING_FILE_TABLE_MASK;
6806 if (table->files[index]) {
6807 file = io_file_from_index(ctx, index);
6808 err = io_queue_file_removal(data, file);
6809 if (err)
6810 break;
6811 table->files[index] = NULL;
6812 needs_switch = true;
6814 if (fd != -1) {
6815 file = fget(fd);
6816 if (!file) {
6817 err = -EBADF;
6818 break;
6821 * Don't allow io_uring instances to be registered. If
6822 * UNIX isn't enabled, then this causes a reference
6823 * cycle and this instance can never get freed. If UNIX
6824 * is enabled we'll handle it just fine, but there's
6825 * still no point in allowing a ring fd as it doesn't
6826 * support regular read/write anyway.
6828 if (file->f_op == &io_uring_fops) {
6829 fput(file);
6830 err = -EBADF;
6831 break;
6833 table->files[index] = file;
6834 err = io_sqe_file_register(ctx, file, i);
6835 if (err)
6836 break;
6838 nr_args--;
6839 done++;
6840 up->offset++;
6843 if (needs_switch) {
6844 percpu_ref_kill(data->cur_refs);
6845 spin_lock_irqsave(&data->lock, flags);
6846 list_add(&ref_node->node, &data->ref_list);
6847 data->cur_refs = &ref_node->refs;
6848 spin_unlock_irqrestore(&data->lock, flags);
6849 percpu_ref_get(&ctx->file_data->refs);
6850 } else
6851 destroy_fixed_file_ref_node(ref_node);
6853 return done ? done : err;
6856 static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
6857 unsigned nr_args)
6859 struct io_uring_files_update up;
6861 if (!ctx->file_data)
6862 return -ENXIO;
6863 if (!nr_args)
6864 return -EINVAL;
6865 if (copy_from_user(&up, arg, sizeof(up)))
6866 return -EFAULT;
6867 if (up.resv)
6868 return -EINVAL;
6870 return __io_sqe_files_update(ctx, &up, nr_args);
6873 static void io_free_work(struct io_wq_work *work)
6875 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
6877 /* Consider that io_steal_work() relies on this ref */
6878 io_put_req(req);
6881 static int io_init_wq_offload(struct io_ring_ctx *ctx,
6882 struct io_uring_params *p)
6884 struct io_wq_data data;
6885 struct fd f;
6886 struct io_ring_ctx *ctx_attach;
6887 unsigned int concurrency;
6888 int ret = 0;
6890 data.user = ctx->user;
6891 data.free_work = io_free_work;
6893 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
6894 /* Do QD, or 4 * CPUS, whatever is smallest */
6895 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
6897 ctx->io_wq = io_wq_create(concurrency, &data);
6898 if (IS_ERR(ctx->io_wq)) {
6899 ret = PTR_ERR(ctx->io_wq);
6900 ctx->io_wq = NULL;
6902 return ret;
6905 f = fdget(p->wq_fd);
6906 if (!f.file)
6907 return -EBADF;
6909 if (f.file->f_op != &io_uring_fops) {
6910 ret = -EINVAL;
6911 goto out_fput;
6914 ctx_attach = f.file->private_data;
6915 /* @io_wq is protected by holding the fd */
6916 if (!io_wq_get(ctx_attach->io_wq, &data)) {
6917 ret = -EINVAL;
6918 goto out_fput;
6921 ctx->io_wq = ctx_attach->io_wq;
6922 out_fput:
6923 fdput(f);
6924 return ret;
6927 static int io_sq_offload_start(struct io_ring_ctx *ctx,
6928 struct io_uring_params *p)
6930 int ret;
6932 mmgrab(current->mm);
6933 ctx->sqo_mm = current->mm;
6935 if (ctx->flags & IORING_SETUP_SQPOLL) {
6936 ret = -EPERM;
6937 if (!capable(CAP_SYS_ADMIN))
6938 goto err;
6940 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
6941 if (!ctx->sq_thread_idle)
6942 ctx->sq_thread_idle = HZ;
6944 if (p->flags & IORING_SETUP_SQ_AFF) {
6945 int cpu = p->sq_thread_cpu;
6947 ret = -EINVAL;
6948 if (cpu >= nr_cpu_ids)
6949 goto err;
6950 if (!cpu_online(cpu))
6951 goto err;
6953 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
6954 ctx, cpu,
6955 "io_uring-sq");
6956 } else {
6957 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
6958 "io_uring-sq");
6960 if (IS_ERR(ctx->sqo_thread)) {
6961 ret = PTR_ERR(ctx->sqo_thread);
6962 ctx->sqo_thread = NULL;
6963 goto err;
6965 wake_up_process(ctx->sqo_thread);
6966 } else if (p->flags & IORING_SETUP_SQ_AFF) {
6967 /* Can't have SQ_AFF without SQPOLL */
6968 ret = -EINVAL;
6969 goto err;
6972 ret = io_init_wq_offload(ctx, p);
6973 if (ret)
6974 goto err;
6976 return 0;
6977 err:
6978 io_finish_async(ctx);
6979 mmdrop(ctx->sqo_mm);
6980 ctx->sqo_mm = NULL;
6981 return ret;
6984 static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
6986 atomic_long_sub(nr_pages, &user->locked_vm);
6989 static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
6991 unsigned long page_limit, cur_pages, new_pages;
6993 /* Don't allow more pages than we can safely lock */
6994 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
6996 do {
6997 cur_pages = atomic_long_read(&user->locked_vm);
6998 new_pages = cur_pages + nr_pages;
6999 if (new_pages > page_limit)
7000 return -ENOMEM;
7001 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
7002 new_pages) != cur_pages);
7004 return 0;
7007 static void io_mem_free(void *ptr)
7009 struct page *page;
7011 if (!ptr)
7012 return;
7014 page = virt_to_head_page(ptr);
7015 if (put_page_testzero(page))
7016 free_compound_page(page);
7019 static void *io_mem_alloc(size_t size)
7021 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
7022 __GFP_NORETRY;
7024 return (void *) __get_free_pages(gfp_flags, get_order(size));
7027 static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
7028 size_t *sq_offset)
7030 struct io_rings *rings;
7031 size_t off, sq_array_size;
7033 off = struct_size(rings, cqes, cq_entries);
7034 if (off == SIZE_MAX)
7035 return SIZE_MAX;
7037 #ifdef CONFIG_SMP
7038 off = ALIGN(off, SMP_CACHE_BYTES);
7039 if (off == 0)
7040 return SIZE_MAX;
7041 #endif
7043 sq_array_size = array_size(sizeof(u32), sq_entries);
7044 if (sq_array_size == SIZE_MAX)
7045 return SIZE_MAX;
7047 if (check_add_overflow(off, sq_array_size, &off))
7048 return SIZE_MAX;
7050 if (sq_offset)
7051 *sq_offset = off;
7053 return off;
7056 static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
7058 size_t pages;
7060 pages = (size_t)1 << get_order(
7061 rings_size(sq_entries, cq_entries, NULL));
7062 pages += (size_t)1 << get_order(
7063 array_size(sizeof(struct io_uring_sqe), sq_entries));
7065 return pages;
7068 static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
7070 int i, j;
7072 if (!ctx->user_bufs)
7073 return -ENXIO;
7075 for (i = 0; i < ctx->nr_user_bufs; i++) {
7076 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7078 for (j = 0; j < imu->nr_bvecs; j++)
7079 unpin_user_page(imu->bvec[j].bv_page);
7081 if (ctx->account_mem)
7082 io_unaccount_mem(ctx->user, imu->nr_bvecs);
7083 kvfree(imu->bvec);
7084 imu->nr_bvecs = 0;
7087 kfree(ctx->user_bufs);
7088 ctx->user_bufs = NULL;
7089 ctx->nr_user_bufs = 0;
7090 return 0;
7093 static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
7094 void __user *arg, unsigned index)
7096 struct iovec __user *src;
7098 #ifdef CONFIG_COMPAT
7099 if (ctx->compat) {
7100 struct compat_iovec __user *ciovs;
7101 struct compat_iovec ciov;
7103 ciovs = (struct compat_iovec __user *) arg;
7104 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
7105 return -EFAULT;
7107 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
7108 dst->iov_len = ciov.iov_len;
7109 return 0;
7111 #endif
7112 src = (struct iovec __user *) arg;
7113 if (copy_from_user(dst, &src[index], sizeof(*dst)))
7114 return -EFAULT;
7115 return 0;
7118 static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
7119 unsigned nr_args)
7121 struct vm_area_struct **vmas = NULL;
7122 struct page **pages = NULL;
7123 int i, j, got_pages = 0;
7124 int ret = -EINVAL;
7126 if (ctx->user_bufs)
7127 return -EBUSY;
7128 if (!nr_args || nr_args > UIO_MAXIOV)
7129 return -EINVAL;
7131 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
7132 GFP_KERNEL);
7133 if (!ctx->user_bufs)
7134 return -ENOMEM;
7136 for (i = 0; i < nr_args; i++) {
7137 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7138 unsigned long off, start, end, ubuf;
7139 int pret, nr_pages;
7140 struct iovec iov;
7141 size_t size;
7143 ret = io_copy_iov(ctx, &iov, arg, i);
7144 if (ret)
7145 goto err;
7148 * Don't impose further limits on the size and buffer
7149 * constraints here, we'll -EINVAL later when IO is
7150 * submitted if they are wrong.
7152 ret = -EFAULT;
7153 if (!iov.iov_base || !iov.iov_len)
7154 goto err;
7156 /* arbitrary limit, but we need something */
7157 if (iov.iov_len > SZ_1G)
7158 goto err;
7160 ubuf = (unsigned long) iov.iov_base;
7161 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
7162 start = ubuf >> PAGE_SHIFT;
7163 nr_pages = end - start;
7165 if (ctx->account_mem) {
7166 ret = io_account_mem(ctx->user, nr_pages);
7167 if (ret)
7168 goto err;
7171 ret = 0;
7172 if (!pages || nr_pages > got_pages) {
7173 kvfree(vmas);
7174 kvfree(pages);
7175 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
7176 GFP_KERNEL);
7177 vmas = kvmalloc_array(nr_pages,
7178 sizeof(struct vm_area_struct *),
7179 GFP_KERNEL);
7180 if (!pages || !vmas) {
7181 ret = -ENOMEM;
7182 if (ctx->account_mem)
7183 io_unaccount_mem(ctx->user, nr_pages);
7184 goto err;
7186 got_pages = nr_pages;
7189 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
7190 GFP_KERNEL);
7191 ret = -ENOMEM;
7192 if (!imu->bvec) {
7193 if (ctx->account_mem)
7194 io_unaccount_mem(ctx->user, nr_pages);
7195 goto err;
7198 ret = 0;
7199 down_read(&current->mm->mmap_sem);
7200 pret = pin_user_pages(ubuf, nr_pages,
7201 FOLL_WRITE | FOLL_LONGTERM,
7202 pages, vmas);
7203 if (pret == nr_pages) {
7204 /* don't support file backed memory */
7205 for (j = 0; j < nr_pages; j++) {
7206 struct vm_area_struct *vma = vmas[j];
7208 if (vma->vm_file &&
7209 !is_file_hugepages(vma->vm_file)) {
7210 ret = -EOPNOTSUPP;
7211 break;
7214 } else {
7215 ret = pret < 0 ? pret : -EFAULT;
7217 up_read(&current->mm->mmap_sem);
7218 if (ret) {
7220 * if we did partial map, or found file backed vmas,
7221 * release any pages we did get
7223 if (pret > 0)
7224 unpin_user_pages(pages, pret);
7225 if (ctx->account_mem)
7226 io_unaccount_mem(ctx->user, nr_pages);
7227 kvfree(imu->bvec);
7228 goto err;
7231 off = ubuf & ~PAGE_MASK;
7232 size = iov.iov_len;
7233 for (j = 0; j < nr_pages; j++) {
7234 size_t vec_len;
7236 vec_len = min_t(size_t, size, PAGE_SIZE - off);
7237 imu->bvec[j].bv_page = pages[j];
7238 imu->bvec[j].bv_len = vec_len;
7239 imu->bvec[j].bv_offset = off;
7240 off = 0;
7241 size -= vec_len;
7243 /* store original address for later verification */
7244 imu->ubuf = ubuf;
7245 imu->len = iov.iov_len;
7246 imu->nr_bvecs = nr_pages;
7248 ctx->nr_user_bufs++;
7250 kvfree(pages);
7251 kvfree(vmas);
7252 return 0;
7253 err:
7254 kvfree(pages);
7255 kvfree(vmas);
7256 io_sqe_buffer_unregister(ctx);
7257 return ret;
7260 static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
7262 __s32 __user *fds = arg;
7263 int fd;
7265 if (ctx->cq_ev_fd)
7266 return -EBUSY;
7268 if (copy_from_user(&fd, fds, sizeof(*fds)))
7269 return -EFAULT;
7271 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
7272 if (IS_ERR(ctx->cq_ev_fd)) {
7273 int ret = PTR_ERR(ctx->cq_ev_fd);
7274 ctx->cq_ev_fd = NULL;
7275 return ret;
7278 return 0;
7281 static int io_eventfd_unregister(struct io_ring_ctx *ctx)
7283 if (ctx->cq_ev_fd) {
7284 eventfd_ctx_put(ctx->cq_ev_fd);
7285 ctx->cq_ev_fd = NULL;
7286 return 0;
7289 return -ENXIO;
7292 static int __io_destroy_buffers(int id, void *p, void *data)
7294 struct io_ring_ctx *ctx = data;
7295 struct io_buffer *buf = p;
7297 __io_remove_buffers(ctx, buf, id, -1U);
7298 return 0;
7301 static void io_destroy_buffers(struct io_ring_ctx *ctx)
7303 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
7304 idr_destroy(&ctx->io_buffer_idr);
7307 static void io_ring_ctx_free(struct io_ring_ctx *ctx)
7309 io_finish_async(ctx);
7310 if (ctx->sqo_mm)
7311 mmdrop(ctx->sqo_mm);
7313 io_iopoll_reap_events(ctx);
7314 io_sqe_buffer_unregister(ctx);
7315 io_sqe_files_unregister(ctx);
7316 io_eventfd_unregister(ctx);
7317 io_destroy_buffers(ctx);
7318 idr_destroy(&ctx->personality_idr);
7320 #if defined(CONFIG_UNIX)
7321 if (ctx->ring_sock) {
7322 ctx->ring_sock->file = NULL; /* so that iput() is called */
7323 sock_release(ctx->ring_sock);
7325 #endif
7327 io_mem_free(ctx->rings);
7328 io_mem_free(ctx->sq_sqes);
7330 percpu_ref_exit(&ctx->refs);
7331 if (ctx->account_mem)
7332 io_unaccount_mem(ctx->user,
7333 ring_pages(ctx->sq_entries, ctx->cq_entries));
7334 free_uid(ctx->user);
7335 put_cred(ctx->creds);
7336 kfree(ctx->completions);
7337 kfree(ctx->cancel_hash);
7338 kmem_cache_free(req_cachep, ctx->fallback_req);
7339 kfree(ctx);
7342 static __poll_t io_uring_poll(struct file *file, poll_table *wait)
7344 struct io_ring_ctx *ctx = file->private_data;
7345 __poll_t mask = 0;
7347 poll_wait(file, &ctx->cq_wait, wait);
7349 * synchronizes with barrier from wq_has_sleeper call in
7350 * io_commit_cqring
7352 smp_rmb();
7353 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
7354 ctx->rings->sq_ring_entries)
7355 mask |= EPOLLOUT | EPOLLWRNORM;
7356 if (io_cqring_events(ctx, false))
7357 mask |= EPOLLIN | EPOLLRDNORM;
7359 return mask;
7362 static int io_uring_fasync(int fd, struct file *file, int on)
7364 struct io_ring_ctx *ctx = file->private_data;
7366 return fasync_helper(fd, file, on, &ctx->cq_fasync);
7369 static int io_remove_personalities(int id, void *p, void *data)
7371 struct io_ring_ctx *ctx = data;
7372 const struct cred *cred;
7374 cred = idr_remove(&ctx->personality_idr, id);
7375 if (cred)
7376 put_cred(cred);
7377 return 0;
7380 static void io_ring_exit_work(struct work_struct *work)
7382 struct io_ring_ctx *ctx;
7384 ctx = container_of(work, struct io_ring_ctx, exit_work);
7385 if (ctx->rings)
7386 io_cqring_overflow_flush(ctx, true);
7388 wait_for_completion(&ctx->completions[0]);
7389 io_ring_ctx_free(ctx);
7392 static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
7394 mutex_lock(&ctx->uring_lock);
7395 percpu_ref_kill(&ctx->refs);
7396 mutex_unlock(&ctx->uring_lock);
7399 * Wait for sq thread to idle, if we have one. It won't spin on new
7400 * work after we've killed the ctx ref above. This is important to do
7401 * before we cancel existing commands, as the thread could otherwise
7402 * be queueing new work post that. If that's work we need to cancel,
7403 * it could cause shutdown to hang.
7405 while (ctx->sqo_thread && !wq_has_sleeper(&ctx->sqo_wait))
7406 cond_resched();
7408 io_kill_timeouts(ctx);
7409 io_poll_remove_all(ctx);
7411 if (ctx->io_wq)
7412 io_wq_cancel_all(ctx->io_wq);
7414 io_iopoll_reap_events(ctx);
7415 /* if we failed setting up the ctx, we might not have any rings */
7416 if (ctx->rings)
7417 io_cqring_overflow_flush(ctx, true);
7418 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
7419 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
7420 queue_work(system_wq, &ctx->exit_work);
7423 static int io_uring_release(struct inode *inode, struct file *file)
7425 struct io_ring_ctx *ctx = file->private_data;
7427 file->private_data = NULL;
7428 io_ring_ctx_wait_and_kill(ctx);
7429 return 0;
7432 static void io_uring_cancel_files(struct io_ring_ctx *ctx,
7433 struct files_struct *files)
7435 while (!list_empty_careful(&ctx->inflight_list)) {
7436 struct io_kiocb *cancel_req = NULL, *req;
7437 DEFINE_WAIT(wait);
7439 spin_lock_irq(&ctx->inflight_lock);
7440 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
7441 if (req->work.files != files)
7442 continue;
7443 /* req is being completed, ignore */
7444 if (!refcount_inc_not_zero(&req->refs))
7445 continue;
7446 cancel_req = req;
7447 break;
7449 if (cancel_req)
7450 prepare_to_wait(&ctx->inflight_wait, &wait,
7451 TASK_UNINTERRUPTIBLE);
7452 spin_unlock_irq(&ctx->inflight_lock);
7454 /* We need to keep going until we don't find a matching req */
7455 if (!cancel_req)
7456 break;
7458 if (cancel_req->flags & REQ_F_OVERFLOW) {
7459 spin_lock_irq(&ctx->completion_lock);
7460 list_del(&cancel_req->list);
7461 cancel_req->flags &= ~REQ_F_OVERFLOW;
7462 if (list_empty(&ctx->cq_overflow_list)) {
7463 clear_bit(0, &ctx->sq_check_overflow);
7464 clear_bit(0, &ctx->cq_check_overflow);
7466 spin_unlock_irq(&ctx->completion_lock);
7468 WRITE_ONCE(ctx->rings->cq_overflow,
7469 atomic_inc_return(&ctx->cached_cq_overflow));
7472 * Put inflight ref and overflow ref. If that's
7473 * all we had, then we're done with this request.
7475 if (refcount_sub_and_test(2, &cancel_req->refs)) {
7476 io_free_req(cancel_req);
7477 finish_wait(&ctx->inflight_wait, &wait);
7478 continue;
7480 } else {
7481 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
7482 io_put_req(cancel_req);
7485 schedule();
7486 finish_wait(&ctx->inflight_wait, &wait);
7490 static int io_uring_flush(struct file *file, void *data)
7492 struct io_ring_ctx *ctx = file->private_data;
7494 io_uring_cancel_files(ctx, data);
7497 * If the task is going away, cancel work it may have pending
7499 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
7500 io_wq_cancel_pid(ctx->io_wq, task_pid_vnr(current));
7502 return 0;
7505 static void *io_uring_validate_mmap_request(struct file *file,
7506 loff_t pgoff, size_t sz)
7508 struct io_ring_ctx *ctx = file->private_data;
7509 loff_t offset = pgoff << PAGE_SHIFT;
7510 struct page *page;
7511 void *ptr;
7513 switch (offset) {
7514 case IORING_OFF_SQ_RING:
7515 case IORING_OFF_CQ_RING:
7516 ptr = ctx->rings;
7517 break;
7518 case IORING_OFF_SQES:
7519 ptr = ctx->sq_sqes;
7520 break;
7521 default:
7522 return ERR_PTR(-EINVAL);
7525 page = virt_to_head_page(ptr);
7526 if (sz > page_size(page))
7527 return ERR_PTR(-EINVAL);
7529 return ptr;
7532 #ifdef CONFIG_MMU
7534 static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7536 size_t sz = vma->vm_end - vma->vm_start;
7537 unsigned long pfn;
7538 void *ptr;
7540 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
7541 if (IS_ERR(ptr))
7542 return PTR_ERR(ptr);
7544 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
7545 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
7548 #else /* !CONFIG_MMU */
7550 static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7552 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
7555 static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
7557 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
7560 static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
7561 unsigned long addr, unsigned long len,
7562 unsigned long pgoff, unsigned long flags)
7564 void *ptr;
7566 ptr = io_uring_validate_mmap_request(file, pgoff, len);
7567 if (IS_ERR(ptr))
7568 return PTR_ERR(ptr);
7570 return (unsigned long) ptr;
7573 #endif /* !CONFIG_MMU */
7575 SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
7576 u32, min_complete, u32, flags, const sigset_t __user *, sig,
7577 size_t, sigsz)
7579 struct io_ring_ctx *ctx;
7580 long ret = -EBADF;
7581 int submitted = 0;
7582 struct fd f;
7584 if (current->task_works)
7585 task_work_run();
7587 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
7588 return -EINVAL;
7590 f = fdget(fd);
7591 if (!f.file)
7592 return -EBADF;
7594 ret = -EOPNOTSUPP;
7595 if (f.file->f_op != &io_uring_fops)
7596 goto out_fput;
7598 ret = -ENXIO;
7599 ctx = f.file->private_data;
7600 if (!percpu_ref_tryget(&ctx->refs))
7601 goto out_fput;
7604 * For SQ polling, the thread will do all submissions and completions.
7605 * Just return the requested submit count, and wake the thread if
7606 * we were asked to.
7608 ret = 0;
7609 if (ctx->flags & IORING_SETUP_SQPOLL) {
7610 if (!list_empty_careful(&ctx->cq_overflow_list))
7611 io_cqring_overflow_flush(ctx, false);
7612 if (flags & IORING_ENTER_SQ_WAKEUP)
7613 wake_up(&ctx->sqo_wait);
7614 submitted = to_submit;
7615 } else if (to_submit) {
7616 mutex_lock(&ctx->uring_lock);
7617 submitted = io_submit_sqes(ctx, to_submit, f.file, fd, false);
7618 mutex_unlock(&ctx->uring_lock);
7620 if (submitted != to_submit)
7621 goto out;
7623 if (flags & IORING_ENTER_GETEVENTS) {
7624 unsigned nr_events = 0;
7626 min_complete = min(min_complete, ctx->cq_entries);
7629 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
7630 * space applications don't need to do io completion events
7631 * polling again, they can rely on io_sq_thread to do polling
7632 * work, which can reduce cpu usage and uring_lock contention.
7634 if (ctx->flags & IORING_SETUP_IOPOLL &&
7635 !(ctx->flags & IORING_SETUP_SQPOLL)) {
7636 ret = io_iopoll_check(ctx, &nr_events, min_complete);
7637 } else {
7638 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
7642 out:
7643 percpu_ref_put(&ctx->refs);
7644 out_fput:
7645 fdput(f);
7646 return submitted ? submitted : ret;
7649 #ifdef CONFIG_PROC_FS
7650 static int io_uring_show_cred(int id, void *p, void *data)
7652 const struct cred *cred = p;
7653 struct seq_file *m = data;
7654 struct user_namespace *uns = seq_user_ns(m);
7655 struct group_info *gi;
7656 kernel_cap_t cap;
7657 unsigned __capi;
7658 int g;
7660 seq_printf(m, "%5d\n", id);
7661 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
7662 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
7663 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
7664 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
7665 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
7666 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
7667 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
7668 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
7669 seq_puts(m, "\n\tGroups:\t");
7670 gi = cred->group_info;
7671 for (g = 0; g < gi->ngroups; g++) {
7672 seq_put_decimal_ull(m, g ? " " : "",
7673 from_kgid_munged(uns, gi->gid[g]));
7675 seq_puts(m, "\n\tCapEff:\t");
7676 cap = cred->cap_effective;
7677 CAP_FOR_EACH_U32(__capi)
7678 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
7679 seq_putc(m, '\n');
7680 return 0;
7683 static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
7685 int i;
7687 mutex_lock(&ctx->uring_lock);
7688 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
7689 for (i = 0; i < ctx->nr_user_files; i++) {
7690 struct fixed_file_table *table;
7691 struct file *f;
7693 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7694 f = table->files[i & IORING_FILE_TABLE_MASK];
7695 if (f)
7696 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
7697 else
7698 seq_printf(m, "%5u: <none>\n", i);
7700 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
7701 for (i = 0; i < ctx->nr_user_bufs; i++) {
7702 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
7704 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
7705 (unsigned int) buf->len);
7707 if (!idr_is_empty(&ctx->personality_idr)) {
7708 seq_printf(m, "Personalities:\n");
7709 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
7711 seq_printf(m, "PollList:\n");
7712 spin_lock_irq(&ctx->completion_lock);
7713 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
7714 struct hlist_head *list = &ctx->cancel_hash[i];
7715 struct io_kiocb *req;
7717 hlist_for_each_entry(req, list, hash_node)
7718 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
7719 req->task->task_works != NULL);
7721 spin_unlock_irq(&ctx->completion_lock);
7722 mutex_unlock(&ctx->uring_lock);
7725 static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
7727 struct io_ring_ctx *ctx = f->private_data;
7729 if (percpu_ref_tryget(&ctx->refs)) {
7730 __io_uring_show_fdinfo(ctx, m);
7731 percpu_ref_put(&ctx->refs);
7734 #endif
7736 static const struct file_operations io_uring_fops = {
7737 .release = io_uring_release,
7738 .flush = io_uring_flush,
7739 .mmap = io_uring_mmap,
7740 #ifndef CONFIG_MMU
7741 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
7742 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
7743 #endif
7744 .poll = io_uring_poll,
7745 .fasync = io_uring_fasync,
7746 #ifdef CONFIG_PROC_FS
7747 .show_fdinfo = io_uring_show_fdinfo,
7748 #endif
7751 static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
7752 struct io_uring_params *p)
7754 struct io_rings *rings;
7755 size_t size, sq_array_offset;
7757 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
7758 if (size == SIZE_MAX)
7759 return -EOVERFLOW;
7761 rings = io_mem_alloc(size);
7762 if (!rings)
7763 return -ENOMEM;
7765 ctx->rings = rings;
7766 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
7767 rings->sq_ring_mask = p->sq_entries - 1;
7768 rings->cq_ring_mask = p->cq_entries - 1;
7769 rings->sq_ring_entries = p->sq_entries;
7770 rings->cq_ring_entries = p->cq_entries;
7771 ctx->sq_mask = rings->sq_ring_mask;
7772 ctx->cq_mask = rings->cq_ring_mask;
7773 ctx->sq_entries = rings->sq_ring_entries;
7774 ctx->cq_entries = rings->cq_ring_entries;
7776 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
7777 if (size == SIZE_MAX) {
7778 io_mem_free(ctx->rings);
7779 ctx->rings = NULL;
7780 return -EOVERFLOW;
7783 ctx->sq_sqes = io_mem_alloc(size);
7784 if (!ctx->sq_sqes) {
7785 io_mem_free(ctx->rings);
7786 ctx->rings = NULL;
7787 return -ENOMEM;
7790 return 0;
7794 * Allocate an anonymous fd, this is what constitutes the application
7795 * visible backing of an io_uring instance. The application mmaps this
7796 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
7797 * we have to tie this fd to a socket for file garbage collection purposes.
7799 static int io_uring_get_fd(struct io_ring_ctx *ctx)
7801 struct file *file;
7802 int ret;
7804 #if defined(CONFIG_UNIX)
7805 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
7806 &ctx->ring_sock);
7807 if (ret)
7808 return ret;
7809 #endif
7811 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
7812 if (ret < 0)
7813 goto err;
7815 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
7816 O_RDWR | O_CLOEXEC);
7817 if (IS_ERR(file)) {
7818 put_unused_fd(ret);
7819 ret = PTR_ERR(file);
7820 goto err;
7823 #if defined(CONFIG_UNIX)
7824 ctx->ring_sock->file = file;
7825 #endif
7826 fd_install(ret, file);
7827 return ret;
7828 err:
7829 #if defined(CONFIG_UNIX)
7830 sock_release(ctx->ring_sock);
7831 ctx->ring_sock = NULL;
7832 #endif
7833 return ret;
7836 static int io_uring_create(unsigned entries, struct io_uring_params *p,
7837 struct io_uring_params __user *params)
7839 struct user_struct *user = NULL;
7840 struct io_ring_ctx *ctx;
7841 bool account_mem;
7842 int ret;
7844 if (!entries)
7845 return -EINVAL;
7846 if (entries > IORING_MAX_ENTRIES) {
7847 if (!(p->flags & IORING_SETUP_CLAMP))
7848 return -EINVAL;
7849 entries = IORING_MAX_ENTRIES;
7853 * Use twice as many entries for the CQ ring. It's possible for the
7854 * application to drive a higher depth than the size of the SQ ring,
7855 * since the sqes are only used at submission time. This allows for
7856 * some flexibility in overcommitting a bit. If the application has
7857 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
7858 * of CQ ring entries manually.
7860 p->sq_entries = roundup_pow_of_two(entries);
7861 if (p->flags & IORING_SETUP_CQSIZE) {
7863 * If IORING_SETUP_CQSIZE is set, we do the same roundup
7864 * to a power-of-two, if it isn't already. We do NOT impose
7865 * any cq vs sq ring sizing.
7867 if (p->cq_entries < p->sq_entries)
7868 return -EINVAL;
7869 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
7870 if (!(p->flags & IORING_SETUP_CLAMP))
7871 return -EINVAL;
7872 p->cq_entries = IORING_MAX_CQ_ENTRIES;
7874 p->cq_entries = roundup_pow_of_two(p->cq_entries);
7875 } else {
7876 p->cq_entries = 2 * p->sq_entries;
7879 user = get_uid(current_user());
7880 account_mem = !capable(CAP_IPC_LOCK);
7882 if (account_mem) {
7883 ret = io_account_mem(user,
7884 ring_pages(p->sq_entries, p->cq_entries));
7885 if (ret) {
7886 free_uid(user);
7887 return ret;
7891 ctx = io_ring_ctx_alloc(p);
7892 if (!ctx) {
7893 if (account_mem)
7894 io_unaccount_mem(user, ring_pages(p->sq_entries,
7895 p->cq_entries));
7896 free_uid(user);
7897 return -ENOMEM;
7899 ctx->compat = in_compat_syscall();
7900 ctx->account_mem = account_mem;
7901 ctx->user = user;
7902 ctx->creds = get_current_cred();
7904 ret = io_allocate_scq_urings(ctx, p);
7905 if (ret)
7906 goto err;
7908 ret = io_sq_offload_start(ctx, p);
7909 if (ret)
7910 goto err;
7912 memset(&p->sq_off, 0, sizeof(p->sq_off));
7913 p->sq_off.head = offsetof(struct io_rings, sq.head);
7914 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
7915 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
7916 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
7917 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
7918 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
7919 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
7921 memset(&p->cq_off, 0, sizeof(p->cq_off));
7922 p->cq_off.head = offsetof(struct io_rings, cq.head);
7923 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
7924 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
7925 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
7926 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
7927 p->cq_off.cqes = offsetof(struct io_rings, cqes);
7929 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
7930 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
7931 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL;
7933 if (copy_to_user(params, p, sizeof(*p))) {
7934 ret = -EFAULT;
7935 goto err;
7938 * Install ring fd as the very last thing, so we don't risk someone
7939 * having closed it before we finish setup
7941 ret = io_uring_get_fd(ctx);
7942 if (ret < 0)
7943 goto err;
7945 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
7946 return ret;
7947 err:
7948 io_ring_ctx_wait_and_kill(ctx);
7949 return ret;
7953 * Sets up an aio uring context, and returns the fd. Applications asks for a
7954 * ring size, we return the actual sq/cq ring sizes (among other things) in the
7955 * params structure passed in.
7957 static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
7959 struct io_uring_params p;
7960 int i;
7962 if (copy_from_user(&p, params, sizeof(p)))
7963 return -EFAULT;
7964 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
7965 if (p.resv[i])
7966 return -EINVAL;
7969 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
7970 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
7971 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
7972 return -EINVAL;
7974 return io_uring_create(entries, &p, params);
7977 SYSCALL_DEFINE2(io_uring_setup, u32, entries,
7978 struct io_uring_params __user *, params)
7980 return io_uring_setup(entries, params);
7983 static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
7985 struct io_uring_probe *p;
7986 size_t size;
7987 int i, ret;
7989 size = struct_size(p, ops, nr_args);
7990 if (size == SIZE_MAX)
7991 return -EOVERFLOW;
7992 p = kzalloc(size, GFP_KERNEL);
7993 if (!p)
7994 return -ENOMEM;
7996 ret = -EFAULT;
7997 if (copy_from_user(p, arg, size))
7998 goto out;
7999 ret = -EINVAL;
8000 if (memchr_inv(p, 0, size))
8001 goto out;
8003 p->last_op = IORING_OP_LAST - 1;
8004 if (nr_args > IORING_OP_LAST)
8005 nr_args = IORING_OP_LAST;
8007 for (i = 0; i < nr_args; i++) {
8008 p->ops[i].op = i;
8009 if (!io_op_defs[i].not_supported)
8010 p->ops[i].flags = IO_URING_OP_SUPPORTED;
8012 p->ops_len = i;
8014 ret = 0;
8015 if (copy_to_user(arg, p, size))
8016 ret = -EFAULT;
8017 out:
8018 kfree(p);
8019 return ret;
8022 static int io_register_personality(struct io_ring_ctx *ctx)
8024 const struct cred *creds = get_current_cred();
8025 int id;
8027 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
8028 USHRT_MAX, GFP_KERNEL);
8029 if (id < 0)
8030 put_cred(creds);
8031 return id;
8034 static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
8036 const struct cred *old_creds;
8038 old_creds = idr_remove(&ctx->personality_idr, id);
8039 if (old_creds) {
8040 put_cred(old_creds);
8041 return 0;
8044 return -EINVAL;
8047 static bool io_register_op_must_quiesce(int op)
8049 switch (op) {
8050 case IORING_UNREGISTER_FILES:
8051 case IORING_REGISTER_FILES_UPDATE:
8052 case IORING_REGISTER_PROBE:
8053 case IORING_REGISTER_PERSONALITY:
8054 case IORING_UNREGISTER_PERSONALITY:
8055 return false;
8056 default:
8057 return true;
8061 static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
8062 void __user *arg, unsigned nr_args)
8063 __releases(ctx->uring_lock)
8064 __acquires(ctx->uring_lock)
8066 int ret;
8069 * We're inside the ring mutex, if the ref is already dying, then
8070 * someone else killed the ctx or is already going through
8071 * io_uring_register().
8073 if (percpu_ref_is_dying(&ctx->refs))
8074 return -ENXIO;
8076 if (io_register_op_must_quiesce(opcode)) {
8077 percpu_ref_kill(&ctx->refs);
8080 * Drop uring mutex before waiting for references to exit. If
8081 * another thread is currently inside io_uring_enter() it might
8082 * need to grab the uring_lock to make progress. If we hold it
8083 * here across the drain wait, then we can deadlock. It's safe
8084 * to drop the mutex here, since no new references will come in
8085 * after we've killed the percpu ref.
8087 mutex_unlock(&ctx->uring_lock);
8088 ret = wait_for_completion_interruptible(&ctx->completions[0]);
8089 mutex_lock(&ctx->uring_lock);
8090 if (ret) {
8091 percpu_ref_resurrect(&ctx->refs);
8092 ret = -EINTR;
8093 goto out;
8097 switch (opcode) {
8098 case IORING_REGISTER_BUFFERS:
8099 ret = io_sqe_buffer_register(ctx, arg, nr_args);
8100 break;
8101 case IORING_UNREGISTER_BUFFERS:
8102 ret = -EINVAL;
8103 if (arg || nr_args)
8104 break;
8105 ret = io_sqe_buffer_unregister(ctx);
8106 break;
8107 case IORING_REGISTER_FILES:
8108 ret = io_sqe_files_register(ctx, arg, nr_args);
8109 break;
8110 case IORING_UNREGISTER_FILES:
8111 ret = -EINVAL;
8112 if (arg || nr_args)
8113 break;
8114 ret = io_sqe_files_unregister(ctx);
8115 break;
8116 case IORING_REGISTER_FILES_UPDATE:
8117 ret = io_sqe_files_update(ctx, arg, nr_args);
8118 break;
8119 case IORING_REGISTER_EVENTFD:
8120 case IORING_REGISTER_EVENTFD_ASYNC:
8121 ret = -EINVAL;
8122 if (nr_args != 1)
8123 break;
8124 ret = io_eventfd_register(ctx, arg);
8125 if (ret)
8126 break;
8127 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
8128 ctx->eventfd_async = 1;
8129 else
8130 ctx->eventfd_async = 0;
8131 break;
8132 case IORING_UNREGISTER_EVENTFD:
8133 ret = -EINVAL;
8134 if (arg || nr_args)
8135 break;
8136 ret = io_eventfd_unregister(ctx);
8137 break;
8138 case IORING_REGISTER_PROBE:
8139 ret = -EINVAL;
8140 if (!arg || nr_args > 256)
8141 break;
8142 ret = io_probe(ctx, arg, nr_args);
8143 break;
8144 case IORING_REGISTER_PERSONALITY:
8145 ret = -EINVAL;
8146 if (arg || nr_args)
8147 break;
8148 ret = io_register_personality(ctx);
8149 break;
8150 case IORING_UNREGISTER_PERSONALITY:
8151 ret = -EINVAL;
8152 if (arg)
8153 break;
8154 ret = io_unregister_personality(ctx, nr_args);
8155 break;
8156 default:
8157 ret = -EINVAL;
8158 break;
8161 if (io_register_op_must_quiesce(opcode)) {
8162 /* bring the ctx back to life */
8163 percpu_ref_reinit(&ctx->refs);
8164 out:
8165 reinit_completion(&ctx->completions[0]);
8167 return ret;
8170 SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
8171 void __user *, arg, unsigned int, nr_args)
8173 struct io_ring_ctx *ctx;
8174 long ret = -EBADF;
8175 struct fd f;
8177 f = fdget(fd);
8178 if (!f.file)
8179 return -EBADF;
8181 ret = -EOPNOTSUPP;
8182 if (f.file->f_op != &io_uring_fops)
8183 goto out_fput;
8185 ctx = f.file->private_data;
8187 mutex_lock(&ctx->uring_lock);
8188 ret = __io_uring_register(ctx, opcode, arg, nr_args);
8189 mutex_unlock(&ctx->uring_lock);
8190 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
8191 ctx->cq_ev_fd != NULL, ret);
8192 out_fput:
8193 fdput(f);
8194 return ret;
8197 static int __init io_uring_init(void)
8199 #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
8200 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
8201 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
8202 } while (0)
8204 #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
8205 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
8206 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
8207 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
8208 BUILD_BUG_SQE_ELEM(1, __u8, flags);
8209 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
8210 BUILD_BUG_SQE_ELEM(4, __s32, fd);
8211 BUILD_BUG_SQE_ELEM(8, __u64, off);
8212 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
8213 BUILD_BUG_SQE_ELEM(16, __u64, addr);
8214 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
8215 BUILD_BUG_SQE_ELEM(24, __u32, len);
8216 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
8217 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
8218 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
8219 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
8220 BUILD_BUG_SQE_ELEM(28, __u16, poll_events);
8221 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
8222 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
8223 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
8224 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
8225 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
8226 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
8227 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
8228 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
8229 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
8230 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
8231 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
8232 BUILD_BUG_SQE_ELEM(42, __u16, personality);
8233 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
8235 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
8236 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
8237 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
8238 return 0;
8240 __initcall(io_uring_init);