1 // SPDX-License-Identifier: GPL-2.0
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
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
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
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>
54 #include <linux/file.h>
55 #include <linux/fdtable.h>
57 #include <linux/mman.h>
58 #include <linux/percpu.h>
59 #include <linux/slab.h>
60 #include <linux/kthread.h>
61 #include <linux/blkdev.h>
62 #include <linux/bvec.h>
63 #include <linux/net.h>
65 #include <net/af_unix.h>
67 #include <linux/anon_inodes.h>
68 #include <linux/sched/mm.h>
69 #include <linux/uaccess.h>
70 #include <linux/nospec.h>
71 #include <linux/sizes.h>
72 #include <linux/hugetlb.h>
73 #include <linux/highmem.h>
74 #include <linux/namei.h>
75 #include <linux/fsnotify.h>
76 #include <linux/fadvise.h>
77 #include <linux/eventpoll.h>
78 #include <linux/fs_struct.h>
79 #include <linux/splice.h>
80 #include <linux/task_work.h>
81 #include <linux/pagemap.h>
82 #include <linux/io_uring.h>
83 #include <linux/blk-cgroup.h>
84 #include <linux/audit.h>
86 #define CREATE_TRACE_POINTS
87 #include <trace/events/io_uring.h>
89 #include <uapi/linux/io_uring.h>
94 #define IORING_MAX_ENTRIES 32768
95 #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
98 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
100 #define IORING_FILE_TABLE_SHIFT 9
101 #define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
102 #define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
103 #define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
104 #define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \
105 IORING_REGISTER_LAST + IORING_OP_LAST)
108 u32 head ____cacheline_aligned_in_smp
;
109 u32 tail ____cacheline_aligned_in_smp
;
113 * This data is shared with the application through the mmap at offsets
114 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
116 * The offsets to the member fields are published through struct
117 * io_sqring_offsets when calling io_uring_setup.
121 * Head and tail offsets into the ring; the offsets need to be
122 * masked to get valid indices.
124 * The kernel controls head of the sq ring and the tail of the cq ring,
125 * and the application controls tail of the sq ring and the head of the
128 struct io_uring sq
, cq
;
130 * Bitmasks to apply to head and tail offsets (constant, equals
133 u32 sq_ring_mask
, cq_ring_mask
;
134 /* Ring sizes (constant, power of 2) */
135 u32 sq_ring_entries
, cq_ring_entries
;
137 * Number of invalid entries dropped by the kernel due to
138 * invalid index stored in array
140 * Written by the kernel, shouldn't be modified by the
141 * application (i.e. get number of "new events" by comparing to
144 * After a new SQ head value was read by the application this
145 * counter includes all submissions that were dropped reaching
146 * the new SQ head (and possibly more).
152 * Written by the kernel, shouldn't be modified by the
155 * The application needs a full memory barrier before checking
156 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
162 * Written by the application, shouldn't be modified by the
167 * Number of completion events lost because the queue was full;
168 * this should be avoided by the application by making sure
169 * there are not more requests pending than there is space in
170 * the completion queue.
172 * Written by the kernel, shouldn't be modified by the
173 * application (i.e. get number of "new events" by comparing to
176 * As completion events come in out of order this counter is not
177 * ordered with any other data.
181 * Ring buffer of completion events.
183 * The kernel writes completion events fresh every time they are
184 * produced, so the application is allowed to modify pending
187 struct io_uring_cqe cqes
[] ____cacheline_aligned_in_smp
;
190 struct io_mapped_ubuf
{
193 struct bio_vec
*bvec
;
194 unsigned int nr_bvecs
;
195 unsigned long acct_pages
;
198 struct fixed_file_table
{
202 struct fixed_file_ref_node
{
203 struct percpu_ref refs
;
204 struct list_head node
;
205 struct list_head file_list
;
206 struct fixed_file_data
*file_data
;
207 struct llist_node llist
;
211 struct fixed_file_data
{
212 struct fixed_file_table
*table
;
213 struct io_ring_ctx
*ctx
;
215 struct fixed_file_ref_node
*node
;
216 struct percpu_ref refs
;
217 struct completion done
;
218 struct list_head ref_list
;
223 struct list_head list
;
229 struct io_restriction
{
230 DECLARE_BITMAP(register_op
, IORING_REGISTER_LAST
);
231 DECLARE_BITMAP(sqe_op
, IORING_OP_LAST
);
232 u8 sqe_flags_allowed
;
233 u8 sqe_flags_required
;
241 /* ctx's that are using this sqd */
242 struct list_head ctx_list
;
243 struct list_head ctx_new_list
;
244 struct mutex ctx_lock
;
246 struct task_struct
*thread
;
247 struct wait_queue_head wait
;
249 unsigned sq_thread_idle
;
254 struct percpu_ref refs
;
255 } ____cacheline_aligned_in_smp
;
259 unsigned int compat
: 1;
260 unsigned int limit_mem
: 1;
261 unsigned int cq_overflow_flushed
: 1;
262 unsigned int drain_next
: 1;
263 unsigned int eventfd_async
: 1;
264 unsigned int restricted
: 1;
265 unsigned int sqo_dead
: 1;
268 * Ring buffer of indices into array of io_uring_sqe, which is
269 * mmapped by the application using the IORING_OFF_SQES offset.
271 * This indirection could e.g. be used to assign fixed
272 * io_uring_sqe entries to operations and only submit them to
273 * the queue when needed.
275 * The kernel modifies neither the indices array nor the entries
279 unsigned cached_sq_head
;
282 unsigned sq_thread_idle
;
283 unsigned cached_sq_dropped
;
284 unsigned cached_cq_overflow
;
285 unsigned long sq_check_overflow
;
287 struct list_head defer_list
;
288 struct list_head timeout_list
;
289 struct list_head cq_overflow_list
;
291 struct io_uring_sqe
*sq_sqes
;
292 } ____cacheline_aligned_in_smp
;
294 struct io_rings
*rings
;
300 * For SQPOLL usage - we hold a reference to the parent task, so we
301 * have access to the ->files
303 struct task_struct
*sqo_task
;
305 /* Only used for accounting purposes */
306 struct mm_struct
*mm_account
;
308 #ifdef CONFIG_BLK_CGROUP
309 struct cgroup_subsys_state
*sqo_blkcg_css
;
312 struct io_sq_data
*sq_data
; /* if using sq thread polling */
314 struct wait_queue_head sqo_sq_wait
;
315 struct list_head sqd_list
;
318 * If used, fixed file set. Writers must ensure that ->refs is dead,
319 * readers must ensure that ->refs is alive as long as the file* is
320 * used. Only updated through io_uring_register(2).
322 struct fixed_file_data
*file_data
;
323 unsigned nr_user_files
;
325 /* if used, fixed mapped user buffers */
326 unsigned nr_user_bufs
;
327 struct io_mapped_ubuf
*user_bufs
;
329 struct user_struct
*user
;
331 const struct cred
*creds
;
335 unsigned int sessionid
;
338 struct completion ref_comp
;
339 struct completion sq_thread_comp
;
341 /* if all else fails... */
342 struct io_kiocb
*fallback_req
;
344 #if defined(CONFIG_UNIX)
345 struct socket
*ring_sock
;
348 struct idr io_buffer_idr
;
350 struct idr personality_idr
;
353 unsigned cached_cq_tail
;
356 atomic_t cq_timeouts
;
357 unsigned cq_last_tm_flush
;
358 unsigned long cq_check_overflow
;
359 struct wait_queue_head cq_wait
;
360 struct fasync_struct
*cq_fasync
;
361 struct eventfd_ctx
*cq_ev_fd
;
362 } ____cacheline_aligned_in_smp
;
365 struct mutex uring_lock
;
366 wait_queue_head_t wait
;
367 } ____cacheline_aligned_in_smp
;
370 spinlock_t completion_lock
;
373 * ->iopoll_list is protected by the ctx->uring_lock for
374 * io_uring instances that don't use IORING_SETUP_SQPOLL.
375 * For SQPOLL, only the single threaded io_sq_thread() will
376 * manipulate the list, hence no extra locking is needed there.
378 struct list_head iopoll_list
;
379 struct hlist_head
*cancel_hash
;
380 unsigned cancel_hash_bits
;
381 bool poll_multi_file
;
383 spinlock_t inflight_lock
;
384 struct list_head inflight_list
;
385 } ____cacheline_aligned_in_smp
;
387 struct delayed_work file_put_work
;
388 struct llist_head file_put_llist
;
390 struct work_struct exit_work
;
391 struct io_restriction restrictions
;
395 * First field must be the file pointer in all the
396 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
398 struct io_poll_iocb
{
400 struct wait_queue_head
*head
;
404 struct wait_queue_entry wait
;
407 struct io_poll_remove
{
414 struct file
*put_file
;
418 struct io_timeout_data
{
419 struct io_kiocb
*req
;
420 struct hrtimer timer
;
421 struct timespec64 ts
;
422 enum hrtimer_mode mode
;
427 struct sockaddr __user
*addr
;
428 int __user
*addr_len
;
430 unsigned long nofile
;
450 struct list_head list
;
451 /* head of the link, used by linked timeouts only */
452 struct io_kiocb
*head
;
455 struct io_timeout_rem
{
460 struct timespec64 ts
;
465 /* NOTE: kiocb has the file as the first member, so don't do it here */
473 struct sockaddr __user
*addr
;
480 struct user_msghdr __user
*umsg
;
486 struct io_buffer
*kbuf
;
492 bool ignore_nonblock
;
493 struct filename
*filename
;
495 unsigned long nofile
;
498 struct io_files_update
{
524 struct epoll_event event
;
528 struct file
*file_out
;
529 struct file
*file_in
;
536 struct io_provide_buf
{
550 const char __user
*filename
;
551 struct statx __user
*buffer
;
563 struct filename
*oldpath
;
564 struct filename
*newpath
;
572 struct filename
*filename
;
575 struct io_completion
{
577 struct list_head list
;
581 struct io_async_connect
{
582 struct sockaddr_storage address
;
585 struct io_async_msghdr
{
586 struct iovec fast_iov
[UIO_FASTIOV
];
588 struct sockaddr __user
*uaddr
;
590 struct sockaddr_storage addr
;
594 struct iovec fast_iov
[UIO_FASTIOV
];
595 const struct iovec
*free_iovec
;
596 struct iov_iter iter
;
598 struct wait_page_queue wpq
;
602 REQ_F_FIXED_FILE_BIT
= IOSQE_FIXED_FILE_BIT
,
603 REQ_F_IO_DRAIN_BIT
= IOSQE_IO_DRAIN_BIT
,
604 REQ_F_LINK_BIT
= IOSQE_IO_LINK_BIT
,
605 REQ_F_HARDLINK_BIT
= IOSQE_IO_HARDLINK_BIT
,
606 REQ_F_FORCE_ASYNC_BIT
= IOSQE_ASYNC_BIT
,
607 REQ_F_BUFFER_SELECT_BIT
= IOSQE_BUFFER_SELECT_BIT
,
613 REQ_F_LINK_TIMEOUT_BIT
,
615 REQ_F_NEED_CLEANUP_BIT
,
617 REQ_F_BUFFER_SELECTED_BIT
,
618 REQ_F_NO_FILE_TABLE_BIT
,
619 REQ_F_WORK_INITIALIZED_BIT
,
620 REQ_F_LTIMEOUT_ACTIVE_BIT
,
622 /* not a real bit, just to check we're not overflowing the space */
628 REQ_F_FIXED_FILE
= BIT(REQ_F_FIXED_FILE_BIT
),
629 /* drain existing IO first */
630 REQ_F_IO_DRAIN
= BIT(REQ_F_IO_DRAIN_BIT
),
632 REQ_F_LINK
= BIT(REQ_F_LINK_BIT
),
633 /* doesn't sever on completion < 0 */
634 REQ_F_HARDLINK
= BIT(REQ_F_HARDLINK_BIT
),
636 REQ_F_FORCE_ASYNC
= BIT(REQ_F_FORCE_ASYNC_BIT
),
637 /* IOSQE_BUFFER_SELECT */
638 REQ_F_BUFFER_SELECT
= BIT(REQ_F_BUFFER_SELECT_BIT
),
640 /* fail rest of links */
641 REQ_F_FAIL_LINK
= BIT(REQ_F_FAIL_LINK_BIT
),
642 /* on inflight list */
643 REQ_F_INFLIGHT
= BIT(REQ_F_INFLIGHT_BIT
),
644 /* read/write uses file position */
645 REQ_F_CUR_POS
= BIT(REQ_F_CUR_POS_BIT
),
646 /* must not punt to workers */
647 REQ_F_NOWAIT
= BIT(REQ_F_NOWAIT_BIT
),
648 /* has or had linked timeout */
649 REQ_F_LINK_TIMEOUT
= BIT(REQ_F_LINK_TIMEOUT_BIT
),
651 REQ_F_ISREG
= BIT(REQ_F_ISREG_BIT
),
653 REQ_F_NEED_CLEANUP
= BIT(REQ_F_NEED_CLEANUP_BIT
),
654 /* already went through poll handler */
655 REQ_F_POLLED
= BIT(REQ_F_POLLED_BIT
),
656 /* buffer already selected */
657 REQ_F_BUFFER_SELECTED
= BIT(REQ_F_BUFFER_SELECTED_BIT
),
658 /* doesn't need file table for this request */
659 REQ_F_NO_FILE_TABLE
= BIT(REQ_F_NO_FILE_TABLE_BIT
),
660 /* io_wq_work is initialized */
661 REQ_F_WORK_INITIALIZED
= BIT(REQ_F_WORK_INITIALIZED_BIT
),
662 /* linked timeout is active, i.e. prepared by link's head */
663 REQ_F_LTIMEOUT_ACTIVE
= BIT(REQ_F_LTIMEOUT_ACTIVE_BIT
),
667 struct io_poll_iocb poll
;
668 struct io_poll_iocb
*double_poll
;
672 * NOTE! Each of the iocb union members has the file pointer
673 * as the first entry in their struct definition. So you can
674 * access the file pointer through any of the sub-structs,
675 * or directly as just 'ki_filp' in this struct.
681 struct io_poll_iocb poll
;
682 struct io_poll_remove poll_remove
;
683 struct io_accept accept
;
685 struct io_cancel cancel
;
686 struct io_timeout timeout
;
687 struct io_timeout_rem timeout_rem
;
688 struct io_connect connect
;
689 struct io_sr_msg sr_msg
;
691 struct io_close close
;
692 struct io_files_update files_update
;
693 struct io_fadvise fadvise
;
694 struct io_madvise madvise
;
695 struct io_epoll epoll
;
696 struct io_splice splice
;
697 struct io_provide_buf pbuf
;
698 struct io_statx statx
;
699 struct io_shutdown shutdown
;
700 struct io_rename rename
;
701 struct io_unlink unlink
;
702 /* use only after cleaning per-op data, see io_clean_op() */
703 struct io_completion
compl;
706 /* opcode allocated if it needs to store data for async defer */
709 /* polled IO has completed */
715 struct io_ring_ctx
*ctx
;
718 struct task_struct
*task
;
721 struct io_kiocb
*link
;
722 struct percpu_ref
*fixed_file_refs
;
725 * 1. used with ctx->iopoll_list with reads/writes
726 * 2. to track reqs with ->files (see io_op_def::file_table)
728 struct list_head inflight_entry
;
729 struct callback_head task_work
;
730 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
731 struct hlist_node hash_node
;
732 struct async_poll
*apoll
;
733 struct io_wq_work work
;
736 struct io_defer_entry
{
737 struct list_head list
;
738 struct io_kiocb
*req
;
742 #define IO_IOPOLL_BATCH 8
744 struct io_comp_state
{
746 struct list_head list
;
747 struct io_ring_ctx
*ctx
;
750 struct io_submit_state
{
751 struct blk_plug plug
;
754 * io_kiocb alloc cache
756 void *reqs
[IO_IOPOLL_BATCH
];
757 unsigned int free_reqs
;
762 * Batch completion logic
764 struct io_comp_state comp
;
767 * File reference cache
771 unsigned int file_refs
;
772 unsigned int ios_left
;
776 /* needs req->file assigned */
777 unsigned needs_file
: 1;
778 /* don't fail if file grab fails */
779 unsigned needs_file_no_error
: 1;
780 /* hash wq insertion if file is a regular file */
781 unsigned hash_reg_file
: 1;
782 /* unbound wq insertion if file is a non-regular file */
783 unsigned unbound_nonreg_file
: 1;
784 /* opcode is not supported by this kernel */
785 unsigned not_supported
: 1;
786 /* set if opcode supports polled "wait" */
788 unsigned pollout
: 1;
789 /* op supports buffer selection */
790 unsigned buffer_select
: 1;
791 /* must always have async data allocated */
792 unsigned needs_async_data
: 1;
793 /* should block plug */
795 /* size of async data needed, if any */
796 unsigned short async_size
;
800 static const struct io_op_def io_op_defs
[] = {
801 [IORING_OP_NOP
] = {},
802 [IORING_OP_READV
] = {
804 .unbound_nonreg_file
= 1,
807 .needs_async_data
= 1,
809 .async_size
= sizeof(struct io_async_rw
),
810 .work_flags
= IO_WQ_WORK_MM
| IO_WQ_WORK_BLKCG
,
812 [IORING_OP_WRITEV
] = {
815 .unbound_nonreg_file
= 1,
817 .needs_async_data
= 1,
819 .async_size
= sizeof(struct io_async_rw
),
820 .work_flags
= IO_WQ_WORK_MM
| IO_WQ_WORK_BLKCG
|
823 [IORING_OP_FSYNC
] = {
825 .work_flags
= IO_WQ_WORK_BLKCG
,
827 [IORING_OP_READ_FIXED
] = {
829 .unbound_nonreg_file
= 1,
832 .async_size
= sizeof(struct io_async_rw
),
833 .work_flags
= IO_WQ_WORK_BLKCG
| IO_WQ_WORK_MM
,
835 [IORING_OP_WRITE_FIXED
] = {
838 .unbound_nonreg_file
= 1,
841 .async_size
= sizeof(struct io_async_rw
),
842 .work_flags
= IO_WQ_WORK_BLKCG
| IO_WQ_WORK_FSIZE
|
845 [IORING_OP_POLL_ADD
] = {
847 .unbound_nonreg_file
= 1,
849 [IORING_OP_POLL_REMOVE
] = {},
850 [IORING_OP_SYNC_FILE_RANGE
] = {
852 .work_flags
= IO_WQ_WORK_BLKCG
,
854 [IORING_OP_SENDMSG
] = {
856 .unbound_nonreg_file
= 1,
858 .needs_async_data
= 1,
859 .async_size
= sizeof(struct io_async_msghdr
),
860 .work_flags
= IO_WQ_WORK_MM
| IO_WQ_WORK_BLKCG
,
862 [IORING_OP_RECVMSG
] = {
864 .unbound_nonreg_file
= 1,
867 .needs_async_data
= 1,
868 .async_size
= sizeof(struct io_async_msghdr
),
869 .work_flags
= IO_WQ_WORK_MM
| IO_WQ_WORK_BLKCG
,
871 [IORING_OP_TIMEOUT
] = {
872 .needs_async_data
= 1,
873 .async_size
= sizeof(struct io_timeout_data
),
874 .work_flags
= IO_WQ_WORK_MM
,
876 [IORING_OP_TIMEOUT_REMOVE
] = {
877 /* used by timeout updates' prep() */
878 .work_flags
= IO_WQ_WORK_MM
,
880 [IORING_OP_ACCEPT
] = {
882 .unbound_nonreg_file
= 1,
884 .work_flags
= IO_WQ_WORK_MM
| IO_WQ_WORK_FILES
,
886 [IORING_OP_ASYNC_CANCEL
] = {},
887 [IORING_OP_LINK_TIMEOUT
] = {
888 .needs_async_data
= 1,
889 .async_size
= sizeof(struct io_timeout_data
),
890 .work_flags
= IO_WQ_WORK_MM
,
892 [IORING_OP_CONNECT
] = {
894 .unbound_nonreg_file
= 1,
896 .needs_async_data
= 1,
897 .async_size
= sizeof(struct io_async_connect
),
898 .work_flags
= IO_WQ_WORK_MM
,
900 [IORING_OP_FALLOCATE
] = {
902 .work_flags
= IO_WQ_WORK_BLKCG
| IO_WQ_WORK_FSIZE
,
904 [IORING_OP_OPENAT
] = {
905 .work_flags
= IO_WQ_WORK_FILES
| IO_WQ_WORK_BLKCG
|
906 IO_WQ_WORK_FS
| IO_WQ_WORK_MM
,
908 [IORING_OP_CLOSE
] = {
910 .needs_file_no_error
= 1,
911 .work_flags
= IO_WQ_WORK_FILES
| IO_WQ_WORK_BLKCG
,
913 [IORING_OP_FILES_UPDATE
] = {
914 .work_flags
= IO_WQ_WORK_FILES
| IO_WQ_WORK_MM
,
916 [IORING_OP_STATX
] = {
917 .work_flags
= IO_WQ_WORK_FILES
| IO_WQ_WORK_MM
|
918 IO_WQ_WORK_FS
| IO_WQ_WORK_BLKCG
,
922 .unbound_nonreg_file
= 1,
926 .async_size
= sizeof(struct io_async_rw
),
927 .work_flags
= IO_WQ_WORK_MM
| IO_WQ_WORK_BLKCG
,
929 [IORING_OP_WRITE
] = {
931 .unbound_nonreg_file
= 1,
934 .async_size
= sizeof(struct io_async_rw
),
935 .work_flags
= IO_WQ_WORK_MM
| IO_WQ_WORK_BLKCG
|
938 [IORING_OP_FADVISE
] = {
940 .work_flags
= IO_WQ_WORK_BLKCG
,
942 [IORING_OP_MADVISE
] = {
943 .work_flags
= IO_WQ_WORK_MM
| IO_WQ_WORK_BLKCG
,
947 .unbound_nonreg_file
= 1,
949 .work_flags
= IO_WQ_WORK_MM
| IO_WQ_WORK_BLKCG
,
953 .unbound_nonreg_file
= 1,
956 .work_flags
= IO_WQ_WORK_MM
| IO_WQ_WORK_BLKCG
,
958 [IORING_OP_OPENAT2
] = {
959 .work_flags
= IO_WQ_WORK_FILES
| IO_WQ_WORK_FS
|
960 IO_WQ_WORK_BLKCG
| IO_WQ_WORK_MM
,
962 [IORING_OP_EPOLL_CTL
] = {
963 .unbound_nonreg_file
= 1,
964 .work_flags
= IO_WQ_WORK_FILES
,
966 [IORING_OP_SPLICE
] = {
969 .unbound_nonreg_file
= 1,
970 .work_flags
= IO_WQ_WORK_BLKCG
,
972 [IORING_OP_PROVIDE_BUFFERS
] = {},
973 [IORING_OP_REMOVE_BUFFERS
] = {},
977 .unbound_nonreg_file
= 1,
979 [IORING_OP_SHUTDOWN
] = {
982 [IORING_OP_RENAMEAT
] = {
983 .work_flags
= IO_WQ_WORK_MM
| IO_WQ_WORK_FILES
|
984 IO_WQ_WORK_FS
| IO_WQ_WORK_BLKCG
,
986 [IORING_OP_UNLINKAT
] = {
987 .work_flags
= IO_WQ_WORK_MM
| IO_WQ_WORK_FILES
|
988 IO_WQ_WORK_FS
| IO_WQ_WORK_BLKCG
,
992 enum io_mem_account
{
997 static void __io_uring_cancel_task_requests(struct io_ring_ctx
*ctx
,
998 struct task_struct
*task
);
1000 static void destroy_fixed_file_ref_node(struct fixed_file_ref_node
*ref_node
);
1001 static struct fixed_file_ref_node
*alloc_fixed_file_ref_node(
1002 struct io_ring_ctx
*ctx
);
1004 static void __io_complete_rw(struct io_kiocb
*req
, long res
, long res2
,
1005 struct io_comp_state
*cs
);
1006 static void io_cqring_fill_event(struct io_kiocb
*req
, long res
);
1007 static void io_put_req(struct io_kiocb
*req
);
1008 static void io_put_req_deferred(struct io_kiocb
*req
, int nr
);
1009 static void io_double_put_req(struct io_kiocb
*req
);
1010 static struct io_kiocb
*io_prep_linked_timeout(struct io_kiocb
*req
);
1011 static void __io_queue_linked_timeout(struct io_kiocb
*req
);
1012 static void io_queue_linked_timeout(struct io_kiocb
*req
);
1013 static int __io_sqe_files_update(struct io_ring_ctx
*ctx
,
1014 struct io_uring_files_update
*ip
,
1016 static void __io_clean_op(struct io_kiocb
*req
);
1017 static struct file
*io_file_get(struct io_submit_state
*state
,
1018 struct io_kiocb
*req
, int fd
, bool fixed
);
1019 static void __io_queue_sqe(struct io_kiocb
*req
, struct io_comp_state
*cs
);
1020 static void io_file_put_work(struct work_struct
*work
);
1022 static ssize_t
io_import_iovec(int rw
, struct io_kiocb
*req
,
1023 struct iovec
**iovec
, struct iov_iter
*iter
,
1025 static int io_setup_async_rw(struct io_kiocb
*req
, const struct iovec
*iovec
,
1026 const struct iovec
*fast_iov
,
1027 struct iov_iter
*iter
, bool force
);
1028 static void io_req_drop_files(struct io_kiocb
*req
);
1029 static void io_req_task_queue(struct io_kiocb
*req
);
1031 static struct kmem_cache
*req_cachep
;
1033 static const struct file_operations io_uring_fops
;
1035 struct sock
*io_uring_get_socket(struct file
*file
)
1037 #if defined(CONFIG_UNIX)
1038 if (file
->f_op
== &io_uring_fops
) {
1039 struct io_ring_ctx
*ctx
= file
->private_data
;
1041 return ctx
->ring_sock
->sk
;
1046 EXPORT_SYMBOL(io_uring_get_socket
);
1048 #define io_for_each_link(pos, head) \
1049 for (pos = (head); pos; pos = pos->link)
1051 static inline void io_clean_op(struct io_kiocb
*req
)
1053 if (req
->flags
& (REQ_F_NEED_CLEANUP
| REQ_F_BUFFER_SELECTED
))
1057 static inline void io_set_resource_node(struct io_kiocb
*req
)
1059 struct io_ring_ctx
*ctx
= req
->ctx
;
1061 if (!req
->fixed_file_refs
) {
1062 req
->fixed_file_refs
= &ctx
->file_data
->node
->refs
;
1063 percpu_ref_get(req
->fixed_file_refs
);
1067 static bool io_match_task(struct io_kiocb
*head
,
1068 struct task_struct
*task
,
1069 struct files_struct
*files
)
1071 struct io_kiocb
*req
;
1073 if (task
&& head
->task
!= task
) {
1074 /* in terms of cancelation, always match if req task is dead */
1075 if (head
->task
->flags
& PF_EXITING
)
1082 io_for_each_link(req
, head
) {
1083 if (!(req
->flags
& REQ_F_WORK_INITIALIZED
))
1085 if (req
->file
&& req
->file
->f_op
== &io_uring_fops
)
1087 if ((req
->work
.flags
& IO_WQ_WORK_FILES
) &&
1088 req
->work
.identity
->files
== files
)
1094 static void io_sq_thread_drop_mm_files(void)
1096 struct files_struct
*files
= current
->files
;
1097 struct mm_struct
*mm
= current
->mm
;
1100 kthread_unuse_mm(mm
);
1105 struct nsproxy
*nsproxy
= current
->nsproxy
;
1108 current
->files
= NULL
;
1109 current
->nsproxy
= NULL
;
1110 task_unlock(current
);
1111 put_files_struct(files
);
1112 put_nsproxy(nsproxy
);
1116 static int __io_sq_thread_acquire_files(struct io_ring_ctx
*ctx
)
1118 if (current
->flags
& PF_EXITING
)
1121 if (!current
->files
) {
1122 struct files_struct
*files
;
1123 struct nsproxy
*nsproxy
;
1125 task_lock(ctx
->sqo_task
);
1126 files
= ctx
->sqo_task
->files
;
1128 task_unlock(ctx
->sqo_task
);
1131 atomic_inc(&files
->count
);
1132 get_nsproxy(ctx
->sqo_task
->nsproxy
);
1133 nsproxy
= ctx
->sqo_task
->nsproxy
;
1134 task_unlock(ctx
->sqo_task
);
1137 current
->files
= files
;
1138 current
->nsproxy
= nsproxy
;
1139 task_unlock(current
);
1144 static int __io_sq_thread_acquire_mm(struct io_ring_ctx
*ctx
)
1146 struct mm_struct
*mm
;
1148 if (current
->flags
& PF_EXITING
)
1153 /* Should never happen */
1154 if (unlikely(!(ctx
->flags
& IORING_SETUP_SQPOLL
)))
1157 task_lock(ctx
->sqo_task
);
1158 mm
= ctx
->sqo_task
->mm
;
1159 if (unlikely(!mm
|| !mmget_not_zero(mm
)))
1161 task_unlock(ctx
->sqo_task
);
1171 static int io_sq_thread_acquire_mm_files(struct io_ring_ctx
*ctx
,
1172 struct io_kiocb
*req
)
1174 const struct io_op_def
*def
= &io_op_defs
[req
->opcode
];
1177 if (def
->work_flags
& IO_WQ_WORK_MM
) {
1178 ret
= __io_sq_thread_acquire_mm(ctx
);
1183 if (def
->needs_file
|| (def
->work_flags
& IO_WQ_WORK_FILES
)) {
1184 ret
= __io_sq_thread_acquire_files(ctx
);
1192 static void io_sq_thread_associate_blkcg(struct io_ring_ctx
*ctx
,
1193 struct cgroup_subsys_state
**cur_css
)
1196 #ifdef CONFIG_BLK_CGROUP
1197 /* puts the old one when swapping */
1198 if (*cur_css
!= ctx
->sqo_blkcg_css
) {
1199 kthread_associate_blkcg(ctx
->sqo_blkcg_css
);
1200 *cur_css
= ctx
->sqo_blkcg_css
;
1205 static void io_sq_thread_unassociate_blkcg(void)
1207 #ifdef CONFIG_BLK_CGROUP
1208 kthread_associate_blkcg(NULL
);
1212 static inline void req_set_fail_links(struct io_kiocb
*req
)
1214 if ((req
->flags
& (REQ_F_LINK
| REQ_F_HARDLINK
)) == REQ_F_LINK
)
1215 req
->flags
|= REQ_F_FAIL_LINK
;
1219 * None of these are dereferenced, they are simply used to check if any of
1220 * them have changed. If we're under current and check they are still the
1221 * same, we're fine to grab references to them for actual out-of-line use.
1223 static void io_init_identity(struct io_identity
*id
)
1225 id
->files
= current
->files
;
1226 id
->mm
= current
->mm
;
1227 #ifdef CONFIG_BLK_CGROUP
1229 id
->blkcg_css
= blkcg_css();
1232 id
->creds
= current_cred();
1233 id
->nsproxy
= current
->nsproxy
;
1234 id
->fs
= current
->fs
;
1235 id
->fsize
= rlimit(RLIMIT_FSIZE
);
1237 id
->loginuid
= current
->loginuid
;
1238 id
->sessionid
= current
->sessionid
;
1240 refcount_set(&id
->count
, 1);
1243 static inline void __io_req_init_async(struct io_kiocb
*req
)
1245 memset(&req
->work
, 0, sizeof(req
->work
));
1246 req
->flags
|= REQ_F_WORK_INITIALIZED
;
1250 * Note: must call io_req_init_async() for the first time you
1251 * touch any members of io_wq_work.
1253 static inline void io_req_init_async(struct io_kiocb
*req
)
1255 struct io_uring_task
*tctx
= current
->io_uring
;
1257 if (req
->flags
& REQ_F_WORK_INITIALIZED
)
1260 __io_req_init_async(req
);
1262 /* Grab a ref if this isn't our static identity */
1263 req
->work
.identity
= tctx
->identity
;
1264 if (tctx
->identity
!= &tctx
->__identity
)
1265 refcount_inc(&req
->work
.identity
->count
);
1268 static inline bool io_async_submit(struct io_ring_ctx
*ctx
)
1270 return ctx
->flags
& IORING_SETUP_SQPOLL
;
1273 static void io_ring_ctx_ref_free(struct percpu_ref
*ref
)
1275 struct io_ring_ctx
*ctx
= container_of(ref
, struct io_ring_ctx
, refs
);
1277 complete(&ctx
->ref_comp
);
1280 static inline bool io_is_timeout_noseq(struct io_kiocb
*req
)
1282 return !req
->timeout
.off
;
1285 static struct io_ring_ctx
*io_ring_ctx_alloc(struct io_uring_params
*p
)
1287 struct io_ring_ctx
*ctx
;
1290 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
1294 ctx
->fallback_req
= kmem_cache_alloc(req_cachep
, GFP_KERNEL
);
1295 if (!ctx
->fallback_req
)
1299 * Use 5 bits less than the max cq entries, that should give us around
1300 * 32 entries per hash list if totally full and uniformly spread.
1302 hash_bits
= ilog2(p
->cq_entries
);
1306 ctx
->cancel_hash_bits
= hash_bits
;
1307 ctx
->cancel_hash
= kmalloc((1U << hash_bits
) * sizeof(struct hlist_head
),
1309 if (!ctx
->cancel_hash
)
1311 __hash_init(ctx
->cancel_hash
, 1U << hash_bits
);
1313 if (percpu_ref_init(&ctx
->refs
, io_ring_ctx_ref_free
,
1314 PERCPU_REF_ALLOW_REINIT
, GFP_KERNEL
))
1317 ctx
->flags
= p
->flags
;
1318 init_waitqueue_head(&ctx
->sqo_sq_wait
);
1319 INIT_LIST_HEAD(&ctx
->sqd_list
);
1320 init_waitqueue_head(&ctx
->cq_wait
);
1321 INIT_LIST_HEAD(&ctx
->cq_overflow_list
);
1322 init_completion(&ctx
->ref_comp
);
1323 init_completion(&ctx
->sq_thread_comp
);
1324 idr_init(&ctx
->io_buffer_idr
);
1325 idr_init(&ctx
->personality_idr
);
1326 mutex_init(&ctx
->uring_lock
);
1327 init_waitqueue_head(&ctx
->wait
);
1328 spin_lock_init(&ctx
->completion_lock
);
1329 INIT_LIST_HEAD(&ctx
->iopoll_list
);
1330 INIT_LIST_HEAD(&ctx
->defer_list
);
1331 INIT_LIST_HEAD(&ctx
->timeout_list
);
1332 spin_lock_init(&ctx
->inflight_lock
);
1333 INIT_LIST_HEAD(&ctx
->inflight_list
);
1334 INIT_DELAYED_WORK(&ctx
->file_put_work
, io_file_put_work
);
1335 init_llist_head(&ctx
->file_put_llist
);
1338 if (ctx
->fallback_req
)
1339 kmem_cache_free(req_cachep
, ctx
->fallback_req
);
1340 kfree(ctx
->cancel_hash
);
1345 static bool req_need_defer(struct io_kiocb
*req
, u32 seq
)
1347 if (unlikely(req
->flags
& REQ_F_IO_DRAIN
)) {
1348 struct io_ring_ctx
*ctx
= req
->ctx
;
1350 return seq
!= ctx
->cached_cq_tail
1351 + READ_ONCE(ctx
->cached_cq_overflow
);
1357 static void __io_commit_cqring(struct io_ring_ctx
*ctx
)
1359 struct io_rings
*rings
= ctx
->rings
;
1361 /* order cqe stores with ring update */
1362 smp_store_release(&rings
->cq
.tail
, ctx
->cached_cq_tail
);
1365 static void io_put_identity(struct io_uring_task
*tctx
, struct io_kiocb
*req
)
1367 if (req
->work
.identity
== &tctx
->__identity
)
1369 if (refcount_dec_and_test(&req
->work
.identity
->count
))
1370 kfree(req
->work
.identity
);
1373 static void io_req_clean_work(struct io_kiocb
*req
)
1375 if (!(req
->flags
& REQ_F_WORK_INITIALIZED
))
1378 req
->flags
&= ~REQ_F_WORK_INITIALIZED
;
1380 if (req
->work
.flags
& IO_WQ_WORK_MM
) {
1381 mmdrop(req
->work
.identity
->mm
);
1382 req
->work
.flags
&= ~IO_WQ_WORK_MM
;
1384 #ifdef CONFIG_BLK_CGROUP
1385 if (req
->work
.flags
& IO_WQ_WORK_BLKCG
) {
1386 css_put(req
->work
.identity
->blkcg_css
);
1387 req
->work
.flags
&= ~IO_WQ_WORK_BLKCG
;
1390 if (req
->work
.flags
& IO_WQ_WORK_CREDS
) {
1391 put_cred(req
->work
.identity
->creds
);
1392 req
->work
.flags
&= ~IO_WQ_WORK_CREDS
;
1394 if (req
->work
.flags
& IO_WQ_WORK_FS
) {
1395 struct fs_struct
*fs
= req
->work
.identity
->fs
;
1397 spin_lock(&req
->work
.identity
->fs
->lock
);
1400 spin_unlock(&req
->work
.identity
->fs
->lock
);
1403 req
->work
.flags
&= ~IO_WQ_WORK_FS
;
1405 if (req
->flags
& REQ_F_INFLIGHT
)
1406 io_req_drop_files(req
);
1408 io_put_identity(req
->task
->io_uring
, req
);
1412 * Create a private copy of io_identity, since some fields don't match
1413 * the current context.
1415 static bool io_identity_cow(struct io_kiocb
*req
)
1417 struct io_uring_task
*tctx
= current
->io_uring
;
1418 const struct cred
*creds
= NULL
;
1419 struct io_identity
*id
;
1421 if (req
->work
.flags
& IO_WQ_WORK_CREDS
)
1422 creds
= req
->work
.identity
->creds
;
1424 id
= kmemdup(req
->work
.identity
, sizeof(*id
), GFP_KERNEL
);
1425 if (unlikely(!id
)) {
1426 req
->work
.flags
|= IO_WQ_WORK_CANCEL
;
1431 * We can safely just re-init the creds we copied Either the field
1432 * matches the current one, or we haven't grabbed it yet. The only
1433 * exception is ->creds, through registered personalities, so handle
1434 * that one separately.
1436 io_init_identity(id
);
1440 /* add one for this request */
1441 refcount_inc(&id
->count
);
1443 /* drop tctx and req identity references, if needed */
1444 if (tctx
->identity
!= &tctx
->__identity
&&
1445 refcount_dec_and_test(&tctx
->identity
->count
))
1446 kfree(tctx
->identity
);
1447 if (req
->work
.identity
!= &tctx
->__identity
&&
1448 refcount_dec_and_test(&req
->work
.identity
->count
))
1449 kfree(req
->work
.identity
);
1451 req
->work
.identity
= id
;
1452 tctx
->identity
= id
;
1456 static bool io_grab_identity(struct io_kiocb
*req
)
1458 const struct io_op_def
*def
= &io_op_defs
[req
->opcode
];
1459 struct io_identity
*id
= req
->work
.identity
;
1460 struct io_ring_ctx
*ctx
= req
->ctx
;
1462 if (def
->work_flags
& IO_WQ_WORK_FSIZE
) {
1463 if (id
->fsize
!= rlimit(RLIMIT_FSIZE
))
1465 req
->work
.flags
|= IO_WQ_WORK_FSIZE
;
1467 #ifdef CONFIG_BLK_CGROUP
1468 if (!(req
->work
.flags
& IO_WQ_WORK_BLKCG
) &&
1469 (def
->work_flags
& IO_WQ_WORK_BLKCG
)) {
1471 if (id
->blkcg_css
!= blkcg_css()) {
1476 * This should be rare, either the cgroup is dying or the task
1477 * is moving cgroups. Just punt to root for the handful of ios.
1479 if (css_tryget_online(id
->blkcg_css
))
1480 req
->work
.flags
|= IO_WQ_WORK_BLKCG
;
1484 if (!(req
->work
.flags
& IO_WQ_WORK_CREDS
)) {
1485 if (id
->creds
!= current_cred())
1487 get_cred(id
->creds
);
1488 req
->work
.flags
|= IO_WQ_WORK_CREDS
;
1491 if (!uid_eq(current
->loginuid
, id
->loginuid
) ||
1492 current
->sessionid
!= id
->sessionid
)
1495 if (!(req
->work
.flags
& IO_WQ_WORK_FS
) &&
1496 (def
->work_flags
& IO_WQ_WORK_FS
)) {
1497 if (current
->fs
!= id
->fs
)
1499 spin_lock(&id
->fs
->lock
);
1500 if (!id
->fs
->in_exec
) {
1502 req
->work
.flags
|= IO_WQ_WORK_FS
;
1504 req
->work
.flags
|= IO_WQ_WORK_CANCEL
;
1506 spin_unlock(¤t
->fs
->lock
);
1508 if (!(req
->work
.flags
& IO_WQ_WORK_FILES
) &&
1509 (def
->work_flags
& IO_WQ_WORK_FILES
) &&
1510 !(req
->flags
& REQ_F_NO_FILE_TABLE
)) {
1511 if (id
->files
!= current
->files
||
1512 id
->nsproxy
!= current
->nsproxy
)
1514 atomic_inc(&id
->files
->count
);
1515 get_nsproxy(id
->nsproxy
);
1517 if (!(req
->flags
& REQ_F_INFLIGHT
)) {
1518 req
->flags
|= REQ_F_INFLIGHT
;
1520 spin_lock_irq(&ctx
->inflight_lock
);
1521 list_add(&req
->inflight_entry
, &ctx
->inflight_list
);
1522 spin_unlock_irq(&ctx
->inflight_lock
);
1524 req
->work
.flags
|= IO_WQ_WORK_FILES
;
1526 if (!(req
->work
.flags
& IO_WQ_WORK_MM
) &&
1527 (def
->work_flags
& IO_WQ_WORK_MM
)) {
1528 if (id
->mm
!= current
->mm
)
1531 req
->work
.flags
|= IO_WQ_WORK_MM
;
1537 static void io_prep_async_work(struct io_kiocb
*req
)
1539 const struct io_op_def
*def
= &io_op_defs
[req
->opcode
];
1540 struct io_ring_ctx
*ctx
= req
->ctx
;
1542 io_req_init_async(req
);
1544 if (req
->flags
& REQ_F_FORCE_ASYNC
)
1545 req
->work
.flags
|= IO_WQ_WORK_CONCURRENT
;
1547 if (req
->flags
& REQ_F_ISREG
) {
1548 if (def
->hash_reg_file
|| (ctx
->flags
& IORING_SETUP_IOPOLL
))
1549 io_wq_hash_work(&req
->work
, file_inode(req
->file
));
1551 if (def
->unbound_nonreg_file
)
1552 req
->work
.flags
|= IO_WQ_WORK_UNBOUND
;
1555 /* if we fail grabbing identity, we must COW, regrab, and retry */
1556 if (io_grab_identity(req
))
1559 if (!io_identity_cow(req
))
1562 /* can't fail at this point */
1563 if (!io_grab_identity(req
))
1567 static void io_prep_async_link(struct io_kiocb
*req
)
1569 struct io_kiocb
*cur
;
1571 io_for_each_link(cur
, req
)
1572 io_prep_async_work(cur
);
1575 static struct io_kiocb
*__io_queue_async_work(struct io_kiocb
*req
)
1577 struct io_ring_ctx
*ctx
= req
->ctx
;
1578 struct io_kiocb
*link
= io_prep_linked_timeout(req
);
1580 trace_io_uring_queue_async_work(ctx
, io_wq_is_hashed(&req
->work
), req
,
1581 &req
->work
, req
->flags
);
1582 io_wq_enqueue(ctx
->io_wq
, &req
->work
);
1586 static void io_queue_async_work(struct io_kiocb
*req
)
1588 struct io_kiocb
*link
;
1590 /* init ->work of the whole link before punting */
1591 io_prep_async_link(req
);
1592 link
= __io_queue_async_work(req
);
1595 io_queue_linked_timeout(link
);
1598 static void io_kill_timeout(struct io_kiocb
*req
)
1600 struct io_timeout_data
*io
= req
->async_data
;
1603 ret
= hrtimer_try_to_cancel(&io
->timer
);
1605 atomic_set(&req
->ctx
->cq_timeouts
,
1606 atomic_read(&req
->ctx
->cq_timeouts
) + 1);
1607 list_del_init(&req
->timeout
.list
);
1608 io_cqring_fill_event(req
, 0);
1609 io_put_req_deferred(req
, 1);
1614 * Returns true if we found and killed one or more timeouts
1616 static bool io_kill_timeouts(struct io_ring_ctx
*ctx
, struct task_struct
*tsk
,
1617 struct files_struct
*files
)
1619 struct io_kiocb
*req
, *tmp
;
1622 spin_lock_irq(&ctx
->completion_lock
);
1623 list_for_each_entry_safe(req
, tmp
, &ctx
->timeout_list
, timeout
.list
) {
1624 if (io_match_task(req
, tsk
, files
)) {
1625 io_kill_timeout(req
);
1629 spin_unlock_irq(&ctx
->completion_lock
);
1630 return canceled
!= 0;
1633 static void __io_queue_deferred(struct io_ring_ctx
*ctx
)
1636 struct io_defer_entry
*de
= list_first_entry(&ctx
->defer_list
,
1637 struct io_defer_entry
, list
);
1639 if (req_need_defer(de
->req
, de
->seq
))
1641 list_del_init(&de
->list
);
1642 io_req_task_queue(de
->req
);
1644 } while (!list_empty(&ctx
->defer_list
));
1647 static void io_flush_timeouts(struct io_ring_ctx
*ctx
)
1651 if (list_empty(&ctx
->timeout_list
))
1654 seq
= ctx
->cached_cq_tail
- atomic_read(&ctx
->cq_timeouts
);
1657 u32 events_needed
, events_got
;
1658 struct io_kiocb
*req
= list_first_entry(&ctx
->timeout_list
,
1659 struct io_kiocb
, timeout
.list
);
1661 if (io_is_timeout_noseq(req
))
1665 * Since seq can easily wrap around over time, subtract
1666 * the last seq at which timeouts were flushed before comparing.
1667 * Assuming not more than 2^31-1 events have happened since,
1668 * these subtractions won't have wrapped, so we can check if
1669 * target is in [last_seq, current_seq] by comparing the two.
1671 events_needed
= req
->timeout
.target_seq
- ctx
->cq_last_tm_flush
;
1672 events_got
= seq
- ctx
->cq_last_tm_flush
;
1673 if (events_got
< events_needed
)
1676 list_del_init(&req
->timeout
.list
);
1677 io_kill_timeout(req
);
1678 } while (!list_empty(&ctx
->timeout_list
));
1680 ctx
->cq_last_tm_flush
= seq
;
1683 static void io_commit_cqring(struct io_ring_ctx
*ctx
)
1685 io_flush_timeouts(ctx
);
1686 __io_commit_cqring(ctx
);
1688 if (unlikely(!list_empty(&ctx
->defer_list
)))
1689 __io_queue_deferred(ctx
);
1692 static inline bool io_sqring_full(struct io_ring_ctx
*ctx
)
1694 struct io_rings
*r
= ctx
->rings
;
1696 return READ_ONCE(r
->sq
.tail
) - ctx
->cached_sq_head
== r
->sq_ring_entries
;
1699 static struct io_uring_cqe
*io_get_cqring(struct io_ring_ctx
*ctx
)
1701 struct io_rings
*rings
= ctx
->rings
;
1704 tail
= ctx
->cached_cq_tail
;
1706 * writes to the cq entry need to come after reading head; the
1707 * control dependency is enough as we're using WRITE_ONCE to
1710 if (tail
- READ_ONCE(rings
->cq
.head
) == rings
->cq_ring_entries
)
1713 ctx
->cached_cq_tail
++;
1714 return &rings
->cqes
[tail
& ctx
->cq_mask
];
1717 static inline bool io_should_trigger_evfd(struct io_ring_ctx
*ctx
)
1721 if (READ_ONCE(ctx
->rings
->cq_flags
) & IORING_CQ_EVENTFD_DISABLED
)
1723 if (!ctx
->eventfd_async
)
1725 return io_wq_current_is_worker();
1728 static inline unsigned __io_cqring_events(struct io_ring_ctx
*ctx
)
1730 return ctx
->cached_cq_tail
- READ_ONCE(ctx
->rings
->cq
.head
);
1733 static void io_cqring_ev_posted(struct io_ring_ctx
*ctx
)
1735 /* see waitqueue_active() comment */
1738 if (waitqueue_active(&ctx
->wait
))
1739 wake_up(&ctx
->wait
);
1740 if (ctx
->sq_data
&& waitqueue_active(&ctx
->sq_data
->wait
))
1741 wake_up(&ctx
->sq_data
->wait
);
1742 if (io_should_trigger_evfd(ctx
))
1743 eventfd_signal(ctx
->cq_ev_fd
, 1);
1744 if (waitqueue_active(&ctx
->cq_wait
)) {
1745 wake_up_interruptible(&ctx
->cq_wait
);
1746 kill_fasync(&ctx
->cq_fasync
, SIGIO
, POLL_IN
);
1750 static void io_cqring_ev_posted_iopoll(struct io_ring_ctx
*ctx
)
1752 /* see waitqueue_active() comment */
1755 if (ctx
->flags
& IORING_SETUP_SQPOLL
) {
1756 if (waitqueue_active(&ctx
->wait
))
1757 wake_up(&ctx
->wait
);
1759 if (io_should_trigger_evfd(ctx
))
1760 eventfd_signal(ctx
->cq_ev_fd
, 1);
1761 if (waitqueue_active(&ctx
->cq_wait
)) {
1762 wake_up_interruptible(&ctx
->cq_wait
);
1763 kill_fasync(&ctx
->cq_fasync
, SIGIO
, POLL_IN
);
1767 /* Returns true if there are no backlogged entries after the flush */
1768 static bool __io_cqring_overflow_flush(struct io_ring_ctx
*ctx
, bool force
,
1769 struct task_struct
*tsk
,
1770 struct files_struct
*files
)
1772 struct io_rings
*rings
= ctx
->rings
;
1773 struct io_kiocb
*req
, *tmp
;
1774 struct io_uring_cqe
*cqe
;
1775 unsigned long flags
;
1776 bool all_flushed
, posted
;
1779 if (!force
&& __io_cqring_events(ctx
) == rings
->cq_ring_entries
)
1783 spin_lock_irqsave(&ctx
->completion_lock
, flags
);
1784 list_for_each_entry_safe(req
, tmp
, &ctx
->cq_overflow_list
, compl.list
) {
1785 if (!io_match_task(req
, tsk
, files
))
1788 cqe
= io_get_cqring(ctx
);
1792 list_move(&req
->compl.list
, &list
);
1794 WRITE_ONCE(cqe
->user_data
, req
->user_data
);
1795 WRITE_ONCE(cqe
->res
, req
->result
);
1796 WRITE_ONCE(cqe
->flags
, req
->compl.cflags
);
1798 ctx
->cached_cq_overflow
++;
1799 WRITE_ONCE(ctx
->rings
->cq_overflow
,
1800 ctx
->cached_cq_overflow
);
1805 all_flushed
= list_empty(&ctx
->cq_overflow_list
);
1807 clear_bit(0, &ctx
->sq_check_overflow
);
1808 clear_bit(0, &ctx
->cq_check_overflow
);
1809 ctx
->rings
->sq_flags
&= ~IORING_SQ_CQ_OVERFLOW
;
1813 io_commit_cqring(ctx
);
1814 spin_unlock_irqrestore(&ctx
->completion_lock
, flags
);
1816 io_cqring_ev_posted(ctx
);
1818 while (!list_empty(&list
)) {
1819 req
= list_first_entry(&list
, struct io_kiocb
, compl.list
);
1820 list_del(&req
->compl.list
);
1827 static void io_cqring_overflow_flush(struct io_ring_ctx
*ctx
, bool force
,
1828 struct task_struct
*tsk
,
1829 struct files_struct
*files
)
1831 if (test_bit(0, &ctx
->cq_check_overflow
)) {
1832 /* iopoll syncs against uring_lock, not completion_lock */
1833 if (ctx
->flags
& IORING_SETUP_IOPOLL
)
1834 mutex_lock(&ctx
->uring_lock
);
1835 __io_cqring_overflow_flush(ctx
, force
, tsk
, files
);
1836 if (ctx
->flags
& IORING_SETUP_IOPOLL
)
1837 mutex_unlock(&ctx
->uring_lock
);
1841 static void __io_cqring_fill_event(struct io_kiocb
*req
, long res
, long cflags
)
1843 struct io_ring_ctx
*ctx
= req
->ctx
;
1844 struct io_uring_cqe
*cqe
;
1846 trace_io_uring_complete(ctx
, req
->user_data
, res
);
1849 * If we can't get a cq entry, userspace overflowed the
1850 * submission (by quite a lot). Increment the overflow count in
1853 cqe
= io_get_cqring(ctx
);
1855 WRITE_ONCE(cqe
->user_data
, req
->user_data
);
1856 WRITE_ONCE(cqe
->res
, res
);
1857 WRITE_ONCE(cqe
->flags
, cflags
);
1858 } else if (ctx
->cq_overflow_flushed
||
1859 atomic_read(&req
->task
->io_uring
->in_idle
)) {
1861 * If we're in ring overflow flush mode, or in task cancel mode,
1862 * then we cannot store the request for later flushing, we need
1863 * to drop it on the floor.
1865 ctx
->cached_cq_overflow
++;
1866 WRITE_ONCE(ctx
->rings
->cq_overflow
, ctx
->cached_cq_overflow
);
1868 if (list_empty(&ctx
->cq_overflow_list
)) {
1869 set_bit(0, &ctx
->sq_check_overflow
);
1870 set_bit(0, &ctx
->cq_check_overflow
);
1871 ctx
->rings
->sq_flags
|= IORING_SQ_CQ_OVERFLOW
;
1875 req
->compl.cflags
= cflags
;
1876 refcount_inc(&req
->refs
);
1877 list_add_tail(&req
->compl.list
, &ctx
->cq_overflow_list
);
1881 static void io_cqring_fill_event(struct io_kiocb
*req
, long res
)
1883 __io_cqring_fill_event(req
, res
, 0);
1886 static void io_cqring_add_event(struct io_kiocb
*req
, long res
, long cflags
)
1888 struct io_ring_ctx
*ctx
= req
->ctx
;
1889 unsigned long flags
;
1891 spin_lock_irqsave(&ctx
->completion_lock
, flags
);
1892 __io_cqring_fill_event(req
, res
, cflags
);
1893 io_commit_cqring(ctx
);
1894 spin_unlock_irqrestore(&ctx
->completion_lock
, flags
);
1896 io_cqring_ev_posted(ctx
);
1899 static void io_submit_flush_completions(struct io_comp_state
*cs
)
1901 struct io_ring_ctx
*ctx
= cs
->ctx
;
1903 spin_lock_irq(&ctx
->completion_lock
);
1904 while (!list_empty(&cs
->list
)) {
1905 struct io_kiocb
*req
;
1907 req
= list_first_entry(&cs
->list
, struct io_kiocb
, compl.list
);
1908 list_del(&req
->compl.list
);
1909 __io_cqring_fill_event(req
, req
->result
, req
->compl.cflags
);
1912 * io_free_req() doesn't care about completion_lock unless one
1913 * of these flags is set. REQ_F_WORK_INITIALIZED is in the list
1914 * because of a potential deadlock with req->work.fs->lock
1916 if (req
->flags
& (REQ_F_FAIL_LINK
|REQ_F_LINK_TIMEOUT
1917 |REQ_F_WORK_INITIALIZED
)) {
1918 spin_unlock_irq(&ctx
->completion_lock
);
1920 spin_lock_irq(&ctx
->completion_lock
);
1925 io_commit_cqring(ctx
);
1926 spin_unlock_irq(&ctx
->completion_lock
);
1928 io_cqring_ev_posted(ctx
);
1932 static void __io_req_complete(struct io_kiocb
*req
, long res
, unsigned cflags
,
1933 struct io_comp_state
*cs
)
1936 io_cqring_add_event(req
, res
, cflags
);
1941 req
->compl.cflags
= cflags
;
1942 list_add_tail(&req
->compl.list
, &cs
->list
);
1944 io_submit_flush_completions(cs
);
1948 static void io_req_complete(struct io_kiocb
*req
, long res
)
1950 __io_req_complete(req
, res
, 0, NULL
);
1953 static inline bool io_is_fallback_req(struct io_kiocb
*req
)
1955 return req
== (struct io_kiocb
*)
1956 ((unsigned long) req
->ctx
->fallback_req
& ~1UL);
1959 static struct io_kiocb
*io_get_fallback_req(struct io_ring_ctx
*ctx
)
1961 struct io_kiocb
*req
;
1963 req
= ctx
->fallback_req
;
1964 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx
->fallback_req
))
1970 static struct io_kiocb
*io_alloc_req(struct io_ring_ctx
*ctx
,
1971 struct io_submit_state
*state
)
1973 if (!state
->free_reqs
) {
1974 gfp_t gfp
= GFP_KERNEL
| __GFP_NOWARN
;
1978 sz
= min_t(size_t, state
->ios_left
, ARRAY_SIZE(state
->reqs
));
1979 ret
= kmem_cache_alloc_bulk(req_cachep
, gfp
, sz
, state
->reqs
);
1982 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1983 * retry single alloc to be on the safe side.
1985 if (unlikely(ret
<= 0)) {
1986 state
->reqs
[0] = kmem_cache_alloc(req_cachep
, gfp
);
1987 if (!state
->reqs
[0])
1991 state
->free_reqs
= ret
;
1995 return state
->reqs
[state
->free_reqs
];
1997 return io_get_fallback_req(ctx
);
2000 static inline void io_put_file(struct io_kiocb
*req
, struct file
*file
,
2007 static void io_dismantle_req(struct io_kiocb
*req
)
2011 if (req
->async_data
)
2012 kfree(req
->async_data
);
2014 io_put_file(req
, req
->file
, (req
->flags
& REQ_F_FIXED_FILE
));
2015 if (req
->fixed_file_refs
)
2016 percpu_ref_put(req
->fixed_file_refs
);
2017 io_req_clean_work(req
);
2020 static void __io_free_req(struct io_kiocb
*req
)
2022 struct io_uring_task
*tctx
= req
->task
->io_uring
;
2023 struct io_ring_ctx
*ctx
= req
->ctx
;
2025 io_dismantle_req(req
);
2027 percpu_counter_dec(&tctx
->inflight
);
2028 if (atomic_read(&tctx
->in_idle
))
2029 wake_up(&tctx
->wait
);
2030 put_task_struct(req
->task
);
2032 if (likely(!io_is_fallback_req(req
)))
2033 kmem_cache_free(req_cachep
, req
);
2035 clear_bit_unlock(0, (unsigned long *) &ctx
->fallback_req
);
2036 percpu_ref_put(&ctx
->refs
);
2039 static inline void io_remove_next_linked(struct io_kiocb
*req
)
2041 struct io_kiocb
*nxt
= req
->link
;
2043 req
->link
= nxt
->link
;
2047 static void io_kill_linked_timeout(struct io_kiocb
*req
)
2049 struct io_ring_ctx
*ctx
= req
->ctx
;
2050 struct io_kiocb
*link
;
2051 bool cancelled
= false;
2052 unsigned long flags
;
2054 spin_lock_irqsave(&ctx
->completion_lock
, flags
);
2058 * Can happen if a linked timeout fired and link had been like
2059 * req -> link t-out -> link t-out [-> ...]
2061 if (link
&& (link
->flags
& REQ_F_LTIMEOUT_ACTIVE
)) {
2062 struct io_timeout_data
*io
= link
->async_data
;
2065 io_remove_next_linked(req
);
2066 link
->timeout
.head
= NULL
;
2067 ret
= hrtimer_try_to_cancel(&io
->timer
);
2069 io_cqring_fill_event(link
, -ECANCELED
);
2070 io_commit_cqring(ctx
);
2074 req
->flags
&= ~REQ_F_LINK_TIMEOUT
;
2075 spin_unlock_irqrestore(&ctx
->completion_lock
, flags
);
2078 io_cqring_ev_posted(ctx
);
2084 static void io_fail_links(struct io_kiocb
*req
)
2086 struct io_kiocb
*link
, *nxt
;
2087 struct io_ring_ctx
*ctx
= req
->ctx
;
2088 unsigned long flags
;
2090 spin_lock_irqsave(&ctx
->completion_lock
, flags
);
2098 trace_io_uring_fail_link(req
, link
);
2099 io_cqring_fill_event(link
, -ECANCELED
);
2102 * It's ok to free under spinlock as they're not linked anymore,
2103 * but avoid REQ_F_WORK_INITIALIZED because it may deadlock on
2106 if (link
->flags
& REQ_F_WORK_INITIALIZED
)
2107 io_put_req_deferred(link
, 2);
2109 io_double_put_req(link
);
2112 io_commit_cqring(ctx
);
2113 spin_unlock_irqrestore(&ctx
->completion_lock
, flags
);
2115 io_cqring_ev_posted(ctx
);
2118 static struct io_kiocb
*__io_req_find_next(struct io_kiocb
*req
)
2120 if (req
->flags
& REQ_F_LINK_TIMEOUT
)
2121 io_kill_linked_timeout(req
);
2124 * If LINK is set, we have dependent requests in this chain. If we
2125 * didn't fail this request, queue the first one up, moving any other
2126 * dependencies to the next request. In case of failure, fail the rest
2129 if (likely(!(req
->flags
& REQ_F_FAIL_LINK
))) {
2130 struct io_kiocb
*nxt
= req
->link
;
2139 static inline struct io_kiocb
*io_req_find_next(struct io_kiocb
*req
)
2141 if (likely(!(req
->link
) && !(req
->flags
& REQ_F_LINK_TIMEOUT
)))
2143 return __io_req_find_next(req
);
2146 static int io_req_task_work_add(struct io_kiocb
*req
)
2148 struct task_struct
*tsk
= req
->task
;
2149 struct io_ring_ctx
*ctx
= req
->ctx
;
2150 enum task_work_notify_mode notify
;
2153 if (tsk
->flags
& PF_EXITING
)
2157 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
2158 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
2159 * processing task_work. There's no reliable way to tell if TWA_RESUME
2163 if (!(ctx
->flags
& IORING_SETUP_SQPOLL
))
2164 notify
= TWA_SIGNAL
;
2166 ret
= task_work_add(tsk
, &req
->task_work
, notify
);
2168 wake_up_process(tsk
);
2173 static void __io_req_task_cancel(struct io_kiocb
*req
, int error
)
2175 struct io_ring_ctx
*ctx
= req
->ctx
;
2177 spin_lock_irq(&ctx
->completion_lock
);
2178 io_cqring_fill_event(req
, error
);
2179 io_commit_cqring(ctx
);
2180 spin_unlock_irq(&ctx
->completion_lock
);
2182 io_cqring_ev_posted(ctx
);
2183 req_set_fail_links(req
);
2184 io_double_put_req(req
);
2187 static void io_req_task_cancel(struct callback_head
*cb
)
2189 struct io_kiocb
*req
= container_of(cb
, struct io_kiocb
, task_work
);
2190 struct io_ring_ctx
*ctx
= req
->ctx
;
2192 __io_req_task_cancel(req
, -ECANCELED
);
2193 percpu_ref_put(&ctx
->refs
);
2196 static void __io_req_task_submit(struct io_kiocb
*req
)
2198 struct io_ring_ctx
*ctx
= req
->ctx
;
2200 mutex_lock(&ctx
->uring_lock
);
2201 if (!ctx
->sqo_dead
&&
2202 !__io_sq_thread_acquire_mm(ctx
) &&
2203 !__io_sq_thread_acquire_files(ctx
))
2204 __io_queue_sqe(req
, NULL
);
2206 __io_req_task_cancel(req
, -EFAULT
);
2207 mutex_unlock(&ctx
->uring_lock
);
2210 static void io_req_task_submit(struct callback_head
*cb
)
2212 struct io_kiocb
*req
= container_of(cb
, struct io_kiocb
, task_work
);
2213 struct io_ring_ctx
*ctx
= req
->ctx
;
2215 __io_req_task_submit(req
);
2216 percpu_ref_put(&ctx
->refs
);
2219 static void io_req_task_queue(struct io_kiocb
*req
)
2223 init_task_work(&req
->task_work
, io_req_task_submit
);
2224 percpu_ref_get(&req
->ctx
->refs
);
2226 ret
= io_req_task_work_add(req
);
2227 if (unlikely(ret
)) {
2228 struct task_struct
*tsk
;
2230 init_task_work(&req
->task_work
, io_req_task_cancel
);
2231 tsk
= io_wq_get_task(req
->ctx
->io_wq
);
2232 task_work_add(tsk
, &req
->task_work
, TWA_NONE
);
2233 wake_up_process(tsk
);
2237 static inline void io_queue_next(struct io_kiocb
*req
)
2239 struct io_kiocb
*nxt
= io_req_find_next(req
);
2242 io_req_task_queue(nxt
);
2245 static void io_free_req(struct io_kiocb
*req
)
2252 void *reqs
[IO_IOPOLL_BATCH
];
2255 struct task_struct
*task
;
2259 static inline void io_init_req_batch(struct req_batch
*rb
)
2266 static void __io_req_free_batch_flush(struct io_ring_ctx
*ctx
,
2267 struct req_batch
*rb
)
2269 kmem_cache_free_bulk(req_cachep
, rb
->to_free
, rb
->reqs
);
2270 percpu_ref_put_many(&ctx
->refs
, rb
->to_free
);
2274 static void io_req_free_batch_finish(struct io_ring_ctx
*ctx
,
2275 struct req_batch
*rb
)
2278 __io_req_free_batch_flush(ctx
, rb
);
2280 struct io_uring_task
*tctx
= rb
->task
->io_uring
;
2282 percpu_counter_sub(&tctx
->inflight
, rb
->task_refs
);
2283 if (atomic_read(&tctx
->in_idle
))
2284 wake_up(&tctx
->wait
);
2285 put_task_struct_many(rb
->task
, rb
->task_refs
);
2290 static void io_req_free_batch(struct req_batch
*rb
, struct io_kiocb
*req
)
2292 if (unlikely(io_is_fallback_req(req
))) {
2298 if (req
->task
!= rb
->task
) {
2300 struct io_uring_task
*tctx
= rb
->task
->io_uring
;
2302 percpu_counter_sub(&tctx
->inflight
, rb
->task_refs
);
2303 if (atomic_read(&tctx
->in_idle
))
2304 wake_up(&tctx
->wait
);
2305 put_task_struct_many(rb
->task
, rb
->task_refs
);
2307 rb
->task
= req
->task
;
2312 io_dismantle_req(req
);
2313 rb
->reqs
[rb
->to_free
++] = req
;
2314 if (unlikely(rb
->to_free
== ARRAY_SIZE(rb
->reqs
)))
2315 __io_req_free_batch_flush(req
->ctx
, rb
);
2319 * Drop reference to request, return next in chain (if there is one) if this
2320 * was the last reference to this request.
2322 static struct io_kiocb
*io_put_req_find_next(struct io_kiocb
*req
)
2324 struct io_kiocb
*nxt
= NULL
;
2326 if (refcount_dec_and_test(&req
->refs
)) {
2327 nxt
= io_req_find_next(req
);
2333 static void io_put_req(struct io_kiocb
*req
)
2335 if (refcount_dec_and_test(&req
->refs
))
2339 static void io_put_req_deferred_cb(struct callback_head
*cb
)
2341 struct io_kiocb
*req
= container_of(cb
, struct io_kiocb
, task_work
);
2346 static void io_free_req_deferred(struct io_kiocb
*req
)
2350 init_task_work(&req
->task_work
, io_put_req_deferred_cb
);
2351 ret
= io_req_task_work_add(req
);
2352 if (unlikely(ret
)) {
2353 struct task_struct
*tsk
;
2355 tsk
= io_wq_get_task(req
->ctx
->io_wq
);
2356 task_work_add(tsk
, &req
->task_work
, TWA_NONE
);
2357 wake_up_process(tsk
);
2361 static inline void io_put_req_deferred(struct io_kiocb
*req
, int refs
)
2363 if (refcount_sub_and_test(refs
, &req
->refs
))
2364 io_free_req_deferred(req
);
2367 static struct io_wq_work
*io_steal_work(struct io_kiocb
*req
)
2369 struct io_kiocb
*nxt
;
2372 * A ref is owned by io-wq in which context we're. So, if that's the
2373 * last one, it's safe to steal next work. False negatives are Ok,
2374 * it just will be re-punted async in io_put_work()
2376 if (refcount_read(&req
->refs
) != 1)
2379 nxt
= io_req_find_next(req
);
2380 return nxt
? &nxt
->work
: NULL
;
2383 static void io_double_put_req(struct io_kiocb
*req
)
2385 /* drop both submit and complete references */
2386 if (refcount_sub_and_test(2, &req
->refs
))
2390 static unsigned io_cqring_events(struct io_ring_ctx
*ctx
)
2392 /* See comment at the top of this file */
2394 return __io_cqring_events(ctx
);
2397 static inline unsigned int io_sqring_entries(struct io_ring_ctx
*ctx
)
2399 struct io_rings
*rings
= ctx
->rings
;
2401 /* make sure SQ entry isn't read before tail */
2402 return smp_load_acquire(&rings
->sq
.tail
) - ctx
->cached_sq_head
;
2405 static unsigned int io_put_kbuf(struct io_kiocb
*req
, struct io_buffer
*kbuf
)
2407 unsigned int cflags
;
2409 cflags
= kbuf
->bid
<< IORING_CQE_BUFFER_SHIFT
;
2410 cflags
|= IORING_CQE_F_BUFFER
;
2411 req
->flags
&= ~REQ_F_BUFFER_SELECTED
;
2416 static inline unsigned int io_put_rw_kbuf(struct io_kiocb
*req
)
2418 struct io_buffer
*kbuf
;
2420 kbuf
= (struct io_buffer
*) (unsigned long) req
->rw
.addr
;
2421 return io_put_kbuf(req
, kbuf
);
2424 static inline bool io_run_task_work(void)
2427 * Not safe to run on exiting task, and the task_work handling will
2428 * not add work to such a task.
2430 if (unlikely(current
->flags
& PF_EXITING
))
2432 if (current
->task_works
) {
2433 __set_current_state(TASK_RUNNING
);
2441 static void io_iopoll_queue(struct list_head
*again
)
2443 struct io_kiocb
*req
;
2446 req
= list_first_entry(again
, struct io_kiocb
, inflight_entry
);
2447 list_del(&req
->inflight_entry
);
2448 __io_complete_rw(req
, -EAGAIN
, 0, NULL
);
2449 } while (!list_empty(again
));
2453 * Find and free completed poll iocbs
2455 static void io_iopoll_complete(struct io_ring_ctx
*ctx
, unsigned int *nr_events
,
2456 struct list_head
*done
)
2458 struct req_batch rb
;
2459 struct io_kiocb
*req
;
2462 /* order with ->result store in io_complete_rw_iopoll() */
2465 io_init_req_batch(&rb
);
2466 while (!list_empty(done
)) {
2469 req
= list_first_entry(done
, struct io_kiocb
, inflight_entry
);
2470 if (READ_ONCE(req
->result
) == -EAGAIN
) {
2472 req
->iopoll_completed
= 0;
2473 list_move_tail(&req
->inflight_entry
, &again
);
2476 list_del(&req
->inflight_entry
);
2478 if (req
->flags
& REQ_F_BUFFER_SELECTED
)
2479 cflags
= io_put_rw_kbuf(req
);
2481 __io_cqring_fill_event(req
, req
->result
, cflags
);
2484 if (refcount_dec_and_test(&req
->refs
))
2485 io_req_free_batch(&rb
, req
);
2488 io_commit_cqring(ctx
);
2489 io_cqring_ev_posted_iopoll(ctx
);
2490 io_req_free_batch_finish(ctx
, &rb
);
2492 if (!list_empty(&again
))
2493 io_iopoll_queue(&again
);
2496 static int io_do_iopoll(struct io_ring_ctx
*ctx
, unsigned int *nr_events
,
2499 struct io_kiocb
*req
, *tmp
;
2505 * Only spin for completions if we don't have multiple devices hanging
2506 * off our complete list, and we're under the requested amount.
2508 spin
= !ctx
->poll_multi_file
&& *nr_events
< min
;
2511 list_for_each_entry_safe(req
, tmp
, &ctx
->iopoll_list
, inflight_entry
) {
2512 struct kiocb
*kiocb
= &req
->rw
.kiocb
;
2515 * Move completed and retryable entries to our local lists.
2516 * If we find a request that requires polling, break out
2517 * and complete those lists first, if we have entries there.
2519 if (READ_ONCE(req
->iopoll_completed
)) {
2520 list_move_tail(&req
->inflight_entry
, &done
);
2523 if (!list_empty(&done
))
2526 ret
= kiocb
->ki_filp
->f_op
->iopoll(kiocb
, spin
);
2530 /* iopoll may have completed current req */
2531 if (READ_ONCE(req
->iopoll_completed
))
2532 list_move_tail(&req
->inflight_entry
, &done
);
2539 if (!list_empty(&done
))
2540 io_iopoll_complete(ctx
, nr_events
, &done
);
2546 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
2547 * non-spinning poll check - we'll still enter the driver poll loop, but only
2548 * as a non-spinning completion check.
2550 static int io_iopoll_getevents(struct io_ring_ctx
*ctx
, unsigned int *nr_events
,
2553 while (!list_empty(&ctx
->iopoll_list
) && !need_resched()) {
2556 ret
= io_do_iopoll(ctx
, nr_events
, min
);
2559 if (*nr_events
>= min
)
2567 * We can't just wait for polled events to come to us, we have to actively
2568 * find and complete them.
2570 static void io_iopoll_try_reap_events(struct io_ring_ctx
*ctx
)
2572 if (!(ctx
->flags
& IORING_SETUP_IOPOLL
))
2575 mutex_lock(&ctx
->uring_lock
);
2576 while (!list_empty(&ctx
->iopoll_list
)) {
2577 unsigned int nr_events
= 0;
2579 io_do_iopoll(ctx
, &nr_events
, 0);
2581 /* let it sleep and repeat later if can't complete a request */
2585 * Ensure we allow local-to-the-cpu processing to take place,
2586 * in this case we need to ensure that we reap all events.
2587 * Also let task_work, etc. to progress by releasing the mutex
2589 if (need_resched()) {
2590 mutex_unlock(&ctx
->uring_lock
);
2592 mutex_lock(&ctx
->uring_lock
);
2595 mutex_unlock(&ctx
->uring_lock
);
2598 static int io_iopoll_check(struct io_ring_ctx
*ctx
, long min
)
2600 unsigned int nr_events
= 0;
2601 int iters
= 0, ret
= 0;
2604 * We disallow the app entering submit/complete with polling, but we
2605 * still need to lock the ring to prevent racing with polled issue
2606 * that got punted to a workqueue.
2608 mutex_lock(&ctx
->uring_lock
);
2611 * Don't enter poll loop if we already have events pending.
2612 * If we do, we can potentially be spinning for commands that
2613 * already triggered a CQE (eg in error).
2615 if (test_bit(0, &ctx
->cq_check_overflow
))
2616 __io_cqring_overflow_flush(ctx
, false, NULL
, NULL
);
2617 if (io_cqring_events(ctx
))
2621 * If a submit got punted to a workqueue, we can have the
2622 * application entering polling for a command before it gets
2623 * issued. That app will hold the uring_lock for the duration
2624 * of the poll right here, so we need to take a breather every
2625 * now and then to ensure that the issue has a chance to add
2626 * the poll to the issued list. Otherwise we can spin here
2627 * forever, while the workqueue is stuck trying to acquire the
2630 if (!(++iters
& 7)) {
2631 mutex_unlock(&ctx
->uring_lock
);
2633 mutex_lock(&ctx
->uring_lock
);
2636 ret
= io_iopoll_getevents(ctx
, &nr_events
, min
);
2640 } while (min
&& !nr_events
&& !need_resched());
2642 mutex_unlock(&ctx
->uring_lock
);
2646 static void kiocb_end_write(struct io_kiocb
*req
)
2649 * Tell lockdep we inherited freeze protection from submission
2652 if (req
->flags
& REQ_F_ISREG
) {
2653 struct inode
*inode
= file_inode(req
->file
);
2655 __sb_writers_acquired(inode
->i_sb
, SB_FREEZE_WRITE
);
2657 file_end_write(req
->file
);
2660 static void io_complete_rw_common(struct kiocb
*kiocb
, long res
,
2661 struct io_comp_state
*cs
)
2663 struct io_kiocb
*req
= container_of(kiocb
, struct io_kiocb
, rw
.kiocb
);
2666 if (kiocb
->ki_flags
& IOCB_WRITE
)
2667 kiocb_end_write(req
);
2669 if (res
!= req
->result
)
2670 req_set_fail_links(req
);
2671 if (req
->flags
& REQ_F_BUFFER_SELECTED
)
2672 cflags
= io_put_rw_kbuf(req
);
2673 __io_req_complete(req
, res
, cflags
, cs
);
2677 static bool io_resubmit_prep(struct io_kiocb
*req
, int error
)
2679 struct iovec inline_vecs
[UIO_FASTIOV
], *iovec
= inline_vecs
;
2680 ssize_t ret
= -ECANCELED
;
2681 struct iov_iter iter
;
2689 switch (req
->opcode
) {
2690 case IORING_OP_READV
:
2691 case IORING_OP_READ_FIXED
:
2692 case IORING_OP_READ
:
2695 case IORING_OP_WRITEV
:
2696 case IORING_OP_WRITE_FIXED
:
2697 case IORING_OP_WRITE
:
2701 printk_once(KERN_WARNING
"io_uring: bad opcode in resubmit %d\n",
2706 if (!req
->async_data
) {
2707 ret
= io_import_iovec(rw
, req
, &iovec
, &iter
, false);
2710 ret
= io_setup_async_rw(req
, iovec
, inline_vecs
, &iter
, false);
2718 req_set_fail_links(req
);
2723 static bool io_rw_reissue(struct io_kiocb
*req
, long res
)
2726 umode_t mode
= file_inode(req
->file
)->i_mode
;
2729 if (!S_ISBLK(mode
) && !S_ISREG(mode
))
2731 if ((res
!= -EAGAIN
&& res
!= -EOPNOTSUPP
) || io_wq_current_is_worker())
2734 lockdep_assert_held(&req
->ctx
->uring_lock
);
2736 ret
= io_sq_thread_acquire_mm_files(req
->ctx
, req
);
2738 if (io_resubmit_prep(req
, ret
)) {
2739 refcount_inc(&req
->refs
);
2740 io_queue_async_work(req
);
2748 static void __io_complete_rw(struct io_kiocb
*req
, long res
, long res2
,
2749 struct io_comp_state
*cs
)
2751 if (!io_rw_reissue(req
, res
))
2752 io_complete_rw_common(&req
->rw
.kiocb
, res
, cs
);
2755 static void io_complete_rw(struct kiocb
*kiocb
, long res
, long res2
)
2757 struct io_kiocb
*req
= container_of(kiocb
, struct io_kiocb
, rw
.kiocb
);
2759 __io_complete_rw(req
, res
, res2
, NULL
);
2762 static void io_complete_rw_iopoll(struct kiocb
*kiocb
, long res
, long res2
)
2764 struct io_kiocb
*req
= container_of(kiocb
, struct io_kiocb
, rw
.kiocb
);
2766 if (kiocb
->ki_flags
& IOCB_WRITE
)
2767 kiocb_end_write(req
);
2769 if (res
!= -EAGAIN
&& res
!= req
->result
)
2770 req_set_fail_links(req
);
2772 WRITE_ONCE(req
->result
, res
);
2773 /* order with io_poll_complete() checking ->result */
2775 WRITE_ONCE(req
->iopoll_completed
, 1);
2779 * After the iocb has been issued, it's safe to be found on the poll list.
2780 * Adding the kiocb to the list AFTER submission ensures that we don't
2781 * find it from a io_iopoll_getevents() thread before the issuer is done
2782 * accessing the kiocb cookie.
2784 static void io_iopoll_req_issued(struct io_kiocb
*req
, bool in_async
)
2786 struct io_ring_ctx
*ctx
= req
->ctx
;
2789 * Track whether we have multiple files in our lists. This will impact
2790 * how we do polling eventually, not spinning if we're on potentially
2791 * different devices.
2793 if (list_empty(&ctx
->iopoll_list
)) {
2794 ctx
->poll_multi_file
= false;
2795 } else if (!ctx
->poll_multi_file
) {
2796 struct io_kiocb
*list_req
;
2798 list_req
= list_first_entry(&ctx
->iopoll_list
, struct io_kiocb
,
2800 if (list_req
->file
!= req
->file
)
2801 ctx
->poll_multi_file
= true;
2805 * For fast devices, IO may have already completed. If it has, add
2806 * it to the front so we find it first.
2808 if (READ_ONCE(req
->iopoll_completed
))
2809 list_add(&req
->inflight_entry
, &ctx
->iopoll_list
);
2811 list_add_tail(&req
->inflight_entry
, &ctx
->iopoll_list
);
2814 * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread
2815 * task context or in io worker task context. If current task context is
2816 * sq thread, we don't need to check whether should wake up sq thread.
2818 if (in_async
&& (ctx
->flags
& IORING_SETUP_SQPOLL
) &&
2819 wq_has_sleeper(&ctx
->sq_data
->wait
))
2820 wake_up(&ctx
->sq_data
->wait
);
2823 static inline void __io_state_file_put(struct io_submit_state
*state
)
2825 fput_many(state
->file
, state
->file_refs
);
2826 state
->file_refs
= 0;
2829 static inline void io_state_file_put(struct io_submit_state
*state
)
2831 if (state
->file_refs
)
2832 __io_state_file_put(state
);
2836 * Get as many references to a file as we have IOs left in this submission,
2837 * assuming most submissions are for one file, or at least that each file
2838 * has more than one submission.
2840 static struct file
*__io_file_get(struct io_submit_state
*state
, int fd
)
2845 if (state
->file_refs
) {
2846 if (state
->fd
== fd
) {
2850 __io_state_file_put(state
);
2852 state
->file
= fget_many(fd
, state
->ios_left
);
2853 if (unlikely(!state
->file
))
2857 state
->file_refs
= state
->ios_left
- 1;
2861 static bool io_bdev_nowait(struct block_device
*bdev
)
2863 return !bdev
|| blk_queue_nowait(bdev_get_queue(bdev
));
2867 * If we tracked the file through the SCM inflight mechanism, we could support
2868 * any file. For now, just ensure that anything potentially problematic is done
2871 static bool io_file_supports_async(struct file
*file
, int rw
)
2873 umode_t mode
= file_inode(file
)->i_mode
;
2875 if (S_ISBLK(mode
)) {
2876 if (IS_ENABLED(CONFIG_BLOCK
) &&
2877 io_bdev_nowait(I_BDEV(file
->f_mapping
->host
)))
2881 if (S_ISCHR(mode
) || S_ISSOCK(mode
))
2883 if (S_ISREG(mode
)) {
2884 if (IS_ENABLED(CONFIG_BLOCK
) &&
2885 io_bdev_nowait(file
->f_inode
->i_sb
->s_bdev
) &&
2886 file
->f_op
!= &io_uring_fops
)
2891 /* any ->read/write should understand O_NONBLOCK */
2892 if (file
->f_flags
& O_NONBLOCK
)
2895 if (!(file
->f_mode
& FMODE_NOWAIT
))
2899 return file
->f_op
->read_iter
!= NULL
;
2901 return file
->f_op
->write_iter
!= NULL
;
2904 static int io_prep_rw(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
2906 struct io_ring_ctx
*ctx
= req
->ctx
;
2907 struct kiocb
*kiocb
= &req
->rw
.kiocb
;
2911 if (S_ISREG(file_inode(req
->file
)->i_mode
))
2912 req
->flags
|= REQ_F_ISREG
;
2914 kiocb
->ki_pos
= READ_ONCE(sqe
->off
);
2915 if (kiocb
->ki_pos
== -1 && !(req
->file
->f_mode
& FMODE_STREAM
)) {
2916 req
->flags
|= REQ_F_CUR_POS
;
2917 kiocb
->ki_pos
= req
->file
->f_pos
;
2919 kiocb
->ki_hint
= ki_hint_validate(file_write_hint(kiocb
->ki_filp
));
2920 kiocb
->ki_flags
= iocb_flags(kiocb
->ki_filp
);
2921 ret
= kiocb_set_rw_flags(kiocb
, READ_ONCE(sqe
->rw_flags
));
2925 ioprio
= READ_ONCE(sqe
->ioprio
);
2927 ret
= ioprio_check_cap(ioprio
);
2931 kiocb
->ki_ioprio
= ioprio
;
2933 kiocb
->ki_ioprio
= get_current_ioprio();
2935 /* don't allow async punt if RWF_NOWAIT was requested */
2936 if (kiocb
->ki_flags
& IOCB_NOWAIT
)
2937 req
->flags
|= REQ_F_NOWAIT
;
2939 if (ctx
->flags
& IORING_SETUP_IOPOLL
) {
2940 if (!(kiocb
->ki_flags
& IOCB_DIRECT
) ||
2941 !kiocb
->ki_filp
->f_op
->iopoll
)
2944 kiocb
->ki_flags
|= IOCB_HIPRI
;
2945 kiocb
->ki_complete
= io_complete_rw_iopoll
;
2946 req
->iopoll_completed
= 0;
2948 if (kiocb
->ki_flags
& IOCB_HIPRI
)
2950 kiocb
->ki_complete
= io_complete_rw
;
2953 req
->rw
.addr
= READ_ONCE(sqe
->addr
);
2954 req
->rw
.len
= READ_ONCE(sqe
->len
);
2955 req
->buf_index
= READ_ONCE(sqe
->buf_index
);
2959 static inline void io_rw_done(struct kiocb
*kiocb
, ssize_t ret
)
2965 case -ERESTARTNOINTR
:
2966 case -ERESTARTNOHAND
:
2967 case -ERESTART_RESTARTBLOCK
:
2969 * We can't just restart the syscall, since previously
2970 * submitted sqes may already be in progress. Just fail this
2976 kiocb
->ki_complete(kiocb
, ret
, 0);
2980 static void kiocb_done(struct kiocb
*kiocb
, ssize_t ret
,
2981 struct io_comp_state
*cs
)
2983 struct io_kiocb
*req
= container_of(kiocb
, struct io_kiocb
, rw
.kiocb
);
2984 struct io_async_rw
*io
= req
->async_data
;
2986 /* add previously done IO, if any */
2987 if (io
&& io
->bytes_done
> 0) {
2989 ret
= io
->bytes_done
;
2991 ret
+= io
->bytes_done
;
2994 if (req
->flags
& REQ_F_CUR_POS
)
2995 req
->file
->f_pos
= kiocb
->ki_pos
;
2996 if (ret
>= 0 && kiocb
->ki_complete
== io_complete_rw
)
2997 __io_complete_rw(req
, ret
, 0, cs
);
2999 io_rw_done(kiocb
, ret
);
3002 static ssize_t
io_import_fixed(struct io_kiocb
*req
, int rw
,
3003 struct iov_iter
*iter
)
3005 struct io_ring_ctx
*ctx
= req
->ctx
;
3006 size_t len
= req
->rw
.len
;
3007 struct io_mapped_ubuf
*imu
;
3008 u16 index
, buf_index
= req
->buf_index
;
3012 if (unlikely(buf_index
>= ctx
->nr_user_bufs
))
3014 index
= array_index_nospec(buf_index
, ctx
->nr_user_bufs
);
3015 imu
= &ctx
->user_bufs
[index
];
3016 buf_addr
= req
->rw
.addr
;
3019 if (buf_addr
+ len
< buf_addr
)
3021 /* not inside the mapped region */
3022 if (buf_addr
< imu
->ubuf
|| buf_addr
+ len
> imu
->ubuf
+ imu
->len
)
3026 * May not be a start of buffer, set size appropriately
3027 * and advance us to the beginning.
3029 offset
= buf_addr
- imu
->ubuf
;
3030 iov_iter_bvec(iter
, rw
, imu
->bvec
, imu
->nr_bvecs
, offset
+ len
);
3034 * Don't use iov_iter_advance() here, as it's really slow for
3035 * using the latter parts of a big fixed buffer - it iterates
3036 * over each segment manually. We can cheat a bit here, because
3039 * 1) it's a BVEC iter, we set it up
3040 * 2) all bvecs are PAGE_SIZE in size, except potentially the
3041 * first and last bvec
3043 * So just find our index, and adjust the iterator afterwards.
3044 * If the offset is within the first bvec (or the whole first
3045 * bvec, just use iov_iter_advance(). This makes it easier
3046 * since we can just skip the first segment, which may not
3047 * be PAGE_SIZE aligned.
3049 const struct bio_vec
*bvec
= imu
->bvec
;
3051 if (offset
<= bvec
->bv_len
) {
3052 iov_iter_advance(iter
, offset
);
3054 unsigned long seg_skip
;
3056 /* skip first vec */
3057 offset
-= bvec
->bv_len
;
3058 seg_skip
= 1 + (offset
>> PAGE_SHIFT
);
3060 iter
->bvec
= bvec
+ seg_skip
;
3061 iter
->nr_segs
-= seg_skip
;
3062 iter
->count
-= bvec
->bv_len
+ offset
;
3063 iter
->iov_offset
= offset
& ~PAGE_MASK
;
3070 static void io_ring_submit_unlock(struct io_ring_ctx
*ctx
, bool needs_lock
)
3073 mutex_unlock(&ctx
->uring_lock
);
3076 static void io_ring_submit_lock(struct io_ring_ctx
*ctx
, bool needs_lock
)
3079 * "Normal" inline submissions always hold the uring_lock, since we
3080 * grab it from the system call. Same is true for the SQPOLL offload.
3081 * The only exception is when we've detached the request and issue it
3082 * from an async worker thread, grab the lock for that case.
3085 mutex_lock(&ctx
->uring_lock
);
3088 static struct io_buffer
*io_buffer_select(struct io_kiocb
*req
, size_t *len
,
3089 int bgid
, struct io_buffer
*kbuf
,
3092 struct io_buffer
*head
;
3094 if (req
->flags
& REQ_F_BUFFER_SELECTED
)
3097 io_ring_submit_lock(req
->ctx
, needs_lock
);
3099 lockdep_assert_held(&req
->ctx
->uring_lock
);
3101 head
= idr_find(&req
->ctx
->io_buffer_idr
, bgid
);
3103 if (!list_empty(&head
->list
)) {
3104 kbuf
= list_last_entry(&head
->list
, struct io_buffer
,
3106 list_del(&kbuf
->list
);
3109 idr_remove(&req
->ctx
->io_buffer_idr
, bgid
);
3111 if (*len
> kbuf
->len
)
3114 kbuf
= ERR_PTR(-ENOBUFS
);
3117 io_ring_submit_unlock(req
->ctx
, needs_lock
);
3122 static void __user
*io_rw_buffer_select(struct io_kiocb
*req
, size_t *len
,
3125 struct io_buffer
*kbuf
;
3128 kbuf
= (struct io_buffer
*) (unsigned long) req
->rw
.addr
;
3129 bgid
= req
->buf_index
;
3130 kbuf
= io_buffer_select(req
, len
, bgid
, kbuf
, needs_lock
);
3133 req
->rw
.addr
= (u64
) (unsigned long) kbuf
;
3134 req
->flags
|= REQ_F_BUFFER_SELECTED
;
3135 return u64_to_user_ptr(kbuf
->addr
);
3138 #ifdef CONFIG_COMPAT
3139 static ssize_t
io_compat_import(struct io_kiocb
*req
, struct iovec
*iov
,
3142 struct compat_iovec __user
*uiov
;
3143 compat_ssize_t clen
;
3147 uiov
= u64_to_user_ptr(req
->rw
.addr
);
3148 if (!access_ok(uiov
, sizeof(*uiov
)))
3150 if (__get_user(clen
, &uiov
->iov_len
))
3156 buf
= io_rw_buffer_select(req
, &len
, needs_lock
);
3158 return PTR_ERR(buf
);
3159 iov
[0].iov_base
= buf
;
3160 iov
[0].iov_len
= (compat_size_t
) len
;
3165 static ssize_t
__io_iov_buffer_select(struct io_kiocb
*req
, struct iovec
*iov
,
3168 struct iovec __user
*uiov
= u64_to_user_ptr(req
->rw
.addr
);
3172 if (copy_from_user(iov
, uiov
, sizeof(*uiov
)))
3175 len
= iov
[0].iov_len
;
3178 buf
= io_rw_buffer_select(req
, &len
, needs_lock
);
3180 return PTR_ERR(buf
);
3181 iov
[0].iov_base
= buf
;
3182 iov
[0].iov_len
= len
;
3186 static ssize_t
io_iov_buffer_select(struct io_kiocb
*req
, struct iovec
*iov
,
3189 if (req
->flags
& REQ_F_BUFFER_SELECTED
) {
3190 struct io_buffer
*kbuf
;
3192 kbuf
= (struct io_buffer
*) (unsigned long) req
->rw
.addr
;
3193 iov
[0].iov_base
= u64_to_user_ptr(kbuf
->addr
);
3194 iov
[0].iov_len
= kbuf
->len
;
3197 if (req
->rw
.len
!= 1)
3200 #ifdef CONFIG_COMPAT
3201 if (req
->ctx
->compat
)
3202 return io_compat_import(req
, iov
, needs_lock
);
3205 return __io_iov_buffer_select(req
, iov
, needs_lock
);
3208 static ssize_t
io_import_iovec(int rw
, struct io_kiocb
*req
,
3209 struct iovec
**iovec
, struct iov_iter
*iter
,
3212 void __user
*buf
= u64_to_user_ptr(req
->rw
.addr
);
3213 size_t sqe_len
= req
->rw
.len
;
3217 opcode
= req
->opcode
;
3218 if (opcode
== IORING_OP_READ_FIXED
|| opcode
== IORING_OP_WRITE_FIXED
) {
3220 return io_import_fixed(req
, rw
, iter
);
3223 /* buffer index only valid with fixed read/write, or buffer select */
3224 if (req
->buf_index
&& !(req
->flags
& REQ_F_BUFFER_SELECT
))
3227 if (opcode
== IORING_OP_READ
|| opcode
== IORING_OP_WRITE
) {
3228 if (req
->flags
& REQ_F_BUFFER_SELECT
) {
3229 buf
= io_rw_buffer_select(req
, &sqe_len
, needs_lock
);
3231 return PTR_ERR(buf
);
3232 req
->rw
.len
= sqe_len
;
3235 ret
= import_single_range(rw
, buf
, sqe_len
, *iovec
, iter
);
3240 if (req
->flags
& REQ_F_BUFFER_SELECT
) {
3241 ret
= io_iov_buffer_select(req
, *iovec
, needs_lock
);
3243 ret
= (*iovec
)->iov_len
;
3244 iov_iter_init(iter
, rw
, *iovec
, 1, ret
);
3250 return __import_iovec(rw
, buf
, sqe_len
, UIO_FASTIOV
, iovec
, iter
,
3254 static inline loff_t
*io_kiocb_ppos(struct kiocb
*kiocb
)
3256 return (kiocb
->ki_filp
->f_mode
& FMODE_STREAM
) ? NULL
: &kiocb
->ki_pos
;
3260 * For files that don't have ->read_iter() and ->write_iter(), handle them
3261 * by looping over ->read() or ->write() manually.
3263 static ssize_t
loop_rw_iter(int rw
, struct io_kiocb
*req
, struct iov_iter
*iter
)
3265 struct kiocb
*kiocb
= &req
->rw
.kiocb
;
3266 struct file
*file
= req
->file
;
3270 * Don't support polled IO through this interface, and we can't
3271 * support non-blocking either. For the latter, this just causes
3272 * the kiocb to be handled from an async context.
3274 if (kiocb
->ki_flags
& IOCB_HIPRI
)
3276 if (kiocb
->ki_flags
& IOCB_NOWAIT
)
3279 while (iov_iter_count(iter
)) {
3283 if (!iov_iter_is_bvec(iter
)) {
3284 iovec
= iov_iter_iovec(iter
);
3286 iovec
.iov_base
= u64_to_user_ptr(req
->rw
.addr
);
3287 iovec
.iov_len
= req
->rw
.len
;
3291 nr
= file
->f_op
->read(file
, iovec
.iov_base
,
3292 iovec
.iov_len
, io_kiocb_ppos(kiocb
));
3294 nr
= file
->f_op
->write(file
, iovec
.iov_base
,
3295 iovec
.iov_len
, io_kiocb_ppos(kiocb
));
3304 if (nr
!= iovec
.iov_len
)
3308 iov_iter_advance(iter
, nr
);
3314 static void io_req_map_rw(struct io_kiocb
*req
, const struct iovec
*iovec
,
3315 const struct iovec
*fast_iov
, struct iov_iter
*iter
)
3317 struct io_async_rw
*rw
= req
->async_data
;
3319 memcpy(&rw
->iter
, iter
, sizeof(*iter
));
3320 rw
->free_iovec
= iovec
;
3322 /* can only be fixed buffers, no need to do anything */
3323 if (iov_iter_is_bvec(iter
))
3326 unsigned iov_off
= 0;
3328 rw
->iter
.iov
= rw
->fast_iov
;
3329 if (iter
->iov
!= fast_iov
) {
3330 iov_off
= iter
->iov
- fast_iov
;
3331 rw
->iter
.iov
+= iov_off
;
3333 if (rw
->fast_iov
!= fast_iov
)
3334 memcpy(rw
->fast_iov
+ iov_off
, fast_iov
+ iov_off
,
3335 sizeof(struct iovec
) * iter
->nr_segs
);
3337 req
->flags
|= REQ_F_NEED_CLEANUP
;
3341 static inline int __io_alloc_async_data(struct io_kiocb
*req
)
3343 WARN_ON_ONCE(!io_op_defs
[req
->opcode
].async_size
);
3344 req
->async_data
= kmalloc(io_op_defs
[req
->opcode
].async_size
, GFP_KERNEL
);
3345 return req
->async_data
== NULL
;
3348 static int io_alloc_async_data(struct io_kiocb
*req
)
3350 if (!io_op_defs
[req
->opcode
].needs_async_data
)
3353 return __io_alloc_async_data(req
);
3356 static int io_setup_async_rw(struct io_kiocb
*req
, const struct iovec
*iovec
,
3357 const struct iovec
*fast_iov
,
3358 struct iov_iter
*iter
, bool force
)
3360 if (!force
&& !io_op_defs
[req
->opcode
].needs_async_data
)
3362 if (!req
->async_data
) {
3363 if (__io_alloc_async_data(req
))
3366 io_req_map_rw(req
, iovec
, fast_iov
, iter
);
3371 static inline int io_rw_prep_async(struct io_kiocb
*req
, int rw
)
3373 struct io_async_rw
*iorw
= req
->async_data
;
3374 struct iovec
*iov
= iorw
->fast_iov
;
3377 ret
= io_import_iovec(rw
, req
, &iov
, &iorw
->iter
, false);
3378 if (unlikely(ret
< 0))
3381 iorw
->bytes_done
= 0;
3382 iorw
->free_iovec
= iov
;
3384 req
->flags
|= REQ_F_NEED_CLEANUP
;
3388 static int io_read_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
3392 ret
= io_prep_rw(req
, sqe
);
3396 if (unlikely(!(req
->file
->f_mode
& FMODE_READ
)))
3399 /* either don't need iovec imported or already have it */
3400 if (!req
->async_data
)
3402 return io_rw_prep_async(req
, READ
);
3406 * This is our waitqueue callback handler, registered through lock_page_async()
3407 * when we initially tried to do the IO with the iocb armed our waitqueue.
3408 * This gets called when the page is unlocked, and we generally expect that to
3409 * happen when the page IO is completed and the page is now uptodate. This will
3410 * queue a task_work based retry of the operation, attempting to copy the data
3411 * again. If the latter fails because the page was NOT uptodate, then we will
3412 * do a thread based blocking retry of the operation. That's the unexpected
3415 static int io_async_buf_func(struct wait_queue_entry
*wait
, unsigned mode
,
3416 int sync
, void *arg
)
3418 struct wait_page_queue
*wpq
;
3419 struct io_kiocb
*req
= wait
->private;
3420 struct wait_page_key
*key
= arg
;
3423 wpq
= container_of(wait
, struct wait_page_queue
, wait
);
3425 if (!wake_page_match(wpq
, key
))
3428 req
->rw
.kiocb
.ki_flags
&= ~IOCB_WAITQ
;
3429 list_del_init(&wait
->entry
);
3431 init_task_work(&req
->task_work
, io_req_task_submit
);
3432 percpu_ref_get(&req
->ctx
->refs
);
3434 /* submit ref gets dropped, acquire a new one */
3435 refcount_inc(&req
->refs
);
3436 ret
= io_req_task_work_add(req
);
3437 if (unlikely(ret
)) {
3438 struct task_struct
*tsk
;
3440 /* queue just for cancelation */
3441 init_task_work(&req
->task_work
, io_req_task_cancel
);
3442 tsk
= io_wq_get_task(req
->ctx
->io_wq
);
3443 task_work_add(tsk
, &req
->task_work
, TWA_NONE
);
3444 wake_up_process(tsk
);
3450 * This controls whether a given IO request should be armed for async page
3451 * based retry. If we return false here, the request is handed to the async
3452 * worker threads for retry. If we're doing buffered reads on a regular file,
3453 * we prepare a private wait_page_queue entry and retry the operation. This
3454 * will either succeed because the page is now uptodate and unlocked, or it
3455 * will register a callback when the page is unlocked at IO completion. Through
3456 * that callback, io_uring uses task_work to setup a retry of the operation.
3457 * That retry will attempt the buffered read again. The retry will generally
3458 * succeed, or in rare cases where it fails, we then fall back to using the
3459 * async worker threads for a blocking retry.
3461 static bool io_rw_should_retry(struct io_kiocb
*req
)
3463 struct io_async_rw
*rw
= req
->async_data
;
3464 struct wait_page_queue
*wait
= &rw
->wpq
;
3465 struct kiocb
*kiocb
= &req
->rw
.kiocb
;
3467 /* never retry for NOWAIT, we just complete with -EAGAIN */
3468 if (req
->flags
& REQ_F_NOWAIT
)
3471 /* Only for buffered IO */
3472 if (kiocb
->ki_flags
& (IOCB_DIRECT
| IOCB_HIPRI
))
3476 * just use poll if we can, and don't attempt if the fs doesn't
3477 * support callback based unlocks
3479 if (file_can_poll(req
->file
) || !(req
->file
->f_mode
& FMODE_BUF_RASYNC
))
3482 wait
->wait
.func
= io_async_buf_func
;
3483 wait
->wait
.private = req
;
3484 wait
->wait
.flags
= 0;
3485 INIT_LIST_HEAD(&wait
->wait
.entry
);
3486 kiocb
->ki_flags
|= IOCB_WAITQ
;
3487 kiocb
->ki_flags
&= ~IOCB_NOWAIT
;
3488 kiocb
->ki_waitq
= wait
;
3492 static int io_iter_do_read(struct io_kiocb
*req
, struct iov_iter
*iter
)
3494 if (req
->file
->f_op
->read_iter
)
3495 return call_read_iter(req
->file
, &req
->rw
.kiocb
, iter
);
3496 else if (req
->file
->f_op
->read
)
3497 return loop_rw_iter(READ
, req
, iter
);
3502 static int io_read(struct io_kiocb
*req
, bool force_nonblock
,
3503 struct io_comp_state
*cs
)
3505 struct iovec inline_vecs
[UIO_FASTIOV
], *iovec
= inline_vecs
;
3506 struct kiocb
*kiocb
= &req
->rw
.kiocb
;
3507 struct iov_iter __iter
, *iter
= &__iter
;
3508 struct io_async_rw
*rw
= req
->async_data
;
3509 ssize_t io_size
, ret
, ret2
;
3516 ret
= io_import_iovec(READ
, req
, &iovec
, iter
, !force_nonblock
);
3520 io_size
= iov_iter_count(iter
);
3521 req
->result
= io_size
;
3524 /* Ensure we clear previously set non-block flag */
3525 if (!force_nonblock
)
3526 kiocb
->ki_flags
&= ~IOCB_NOWAIT
;
3528 kiocb
->ki_flags
|= IOCB_NOWAIT
;
3531 /* If the file doesn't support async, just async punt */
3532 no_async
= force_nonblock
&& !io_file_supports_async(req
->file
, READ
);
3536 ret
= rw_verify_area(READ
, req
->file
, io_kiocb_ppos(kiocb
), io_size
);
3540 ret
= io_iter_do_read(req
, iter
);
3544 } else if (ret
== -EIOCBQUEUED
) {
3547 } else if (ret
== -EAGAIN
) {
3548 /* IOPOLL retry should happen for io-wq threads */
3549 if (!force_nonblock
&& !(req
->ctx
->flags
& IORING_SETUP_IOPOLL
))
3551 /* no retry on NONBLOCK marked file */
3552 if (req
->file
->f_flags
& O_NONBLOCK
)
3554 /* some cases will consume bytes even on error returns */
3555 iov_iter_revert(iter
, io_size
- iov_iter_count(iter
));
3558 } else if (ret
< 0) {
3559 /* make sure -ERESTARTSYS -> -EINTR is done */
3563 /* read it all, or we did blocking attempt. no retry. */
3564 if (!iov_iter_count(iter
) || !force_nonblock
||
3565 (req
->file
->f_flags
& O_NONBLOCK
) || !(req
->flags
& REQ_F_ISREG
))
3570 ret2
= io_setup_async_rw(req
, iovec
, inline_vecs
, iter
, true);
3577 rw
= req
->async_data
;
3578 /* it's copied and will be cleaned with ->io */
3580 /* now use our persistent iterator, if we aren't already */
3583 rw
->bytes_done
+= ret
;
3584 /* if we can retry, do so with the callbacks armed */
3585 if (!io_rw_should_retry(req
)) {
3586 kiocb
->ki_flags
&= ~IOCB_WAITQ
;
3591 * Now retry read with the IOCB_WAITQ parts set in the iocb. If we
3592 * get -EIOCBQUEUED, then we'll get a notification when the desired
3593 * page gets unlocked. We can also get a partial read here, and if we
3594 * do, then just retry at the new offset.
3596 ret
= io_iter_do_read(req
, iter
);
3597 if (ret
== -EIOCBQUEUED
) {
3600 } else if (ret
> 0 && ret
< io_size
) {
3601 /* we got some bytes, but not all. retry. */
3605 kiocb_done(kiocb
, ret
, cs
);
3608 /* it's reportedly faster than delegating the null check to kfree() */
3614 static int io_write_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
3618 ret
= io_prep_rw(req
, sqe
);
3622 if (unlikely(!(req
->file
->f_mode
& FMODE_WRITE
)))
3625 /* either don't need iovec imported or already have it */
3626 if (!req
->async_data
)
3628 return io_rw_prep_async(req
, WRITE
);
3631 static int io_write(struct io_kiocb
*req
, bool force_nonblock
,
3632 struct io_comp_state
*cs
)
3634 struct iovec inline_vecs
[UIO_FASTIOV
], *iovec
= inline_vecs
;
3635 struct kiocb
*kiocb
= &req
->rw
.kiocb
;
3636 struct iov_iter __iter
, *iter
= &__iter
;
3637 struct io_async_rw
*rw
= req
->async_data
;
3638 ssize_t ret
, ret2
, io_size
;
3644 ret
= io_import_iovec(WRITE
, req
, &iovec
, iter
, !force_nonblock
);
3648 io_size
= iov_iter_count(iter
);
3649 req
->result
= io_size
;
3651 /* Ensure we clear previously set non-block flag */
3652 if (!force_nonblock
)
3653 kiocb
->ki_flags
&= ~IOCB_NOWAIT
;
3655 kiocb
->ki_flags
|= IOCB_NOWAIT
;
3657 /* If the file doesn't support async, just async punt */
3658 if (force_nonblock
&& !io_file_supports_async(req
->file
, WRITE
))
3661 /* file path doesn't support NOWAIT for non-direct_IO */
3662 if (force_nonblock
&& !(kiocb
->ki_flags
& IOCB_DIRECT
) &&
3663 (req
->flags
& REQ_F_ISREG
))
3666 ret
= rw_verify_area(WRITE
, req
->file
, io_kiocb_ppos(kiocb
), io_size
);
3671 * Open-code file_start_write here to grab freeze protection,
3672 * which will be released by another thread in
3673 * io_complete_rw(). Fool lockdep by telling it the lock got
3674 * released so that it doesn't complain about the held lock when
3675 * we return to userspace.
3677 if (req
->flags
& REQ_F_ISREG
) {
3678 sb_start_write(file_inode(req
->file
)->i_sb
);
3679 __sb_writers_release(file_inode(req
->file
)->i_sb
,
3682 kiocb
->ki_flags
|= IOCB_WRITE
;
3684 if (req
->file
->f_op
->write_iter
)
3685 ret2
= call_write_iter(req
->file
, kiocb
, iter
);
3686 else if (req
->file
->f_op
->write
)
3687 ret2
= loop_rw_iter(WRITE
, req
, iter
);
3692 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3693 * retry them without IOCB_NOWAIT.
3695 if (ret2
== -EOPNOTSUPP
&& (kiocb
->ki_flags
& IOCB_NOWAIT
))
3697 /* no retry on NONBLOCK marked file */
3698 if (ret2
== -EAGAIN
&& (req
->file
->f_flags
& O_NONBLOCK
))
3700 if (!force_nonblock
|| ret2
!= -EAGAIN
) {
3701 /* IOPOLL retry should happen for io-wq threads */
3702 if ((req
->ctx
->flags
& IORING_SETUP_IOPOLL
) && ret2
== -EAGAIN
)
3705 kiocb_done(kiocb
, ret2
, cs
);
3708 /* some cases will consume bytes even on error returns */
3709 iov_iter_revert(iter
, io_size
- iov_iter_count(iter
));
3710 ret
= io_setup_async_rw(req
, iovec
, inline_vecs
, iter
, false);
3715 /* it's reportedly faster than delegating the null check to kfree() */
3721 static int io_renameat_prep(struct io_kiocb
*req
,
3722 const struct io_uring_sqe
*sqe
)
3724 struct io_rename
*ren
= &req
->rename
;
3725 const char __user
*oldf
, *newf
;
3727 if (unlikely(req
->flags
& REQ_F_FIXED_FILE
))
3730 ren
->old_dfd
= READ_ONCE(sqe
->fd
);
3731 oldf
= u64_to_user_ptr(READ_ONCE(sqe
->addr
));
3732 newf
= u64_to_user_ptr(READ_ONCE(sqe
->addr2
));
3733 ren
->new_dfd
= READ_ONCE(sqe
->len
);
3734 ren
->flags
= READ_ONCE(sqe
->rename_flags
);
3736 ren
->oldpath
= getname(oldf
);
3737 if (IS_ERR(ren
->oldpath
))
3738 return PTR_ERR(ren
->oldpath
);
3740 ren
->newpath
= getname(newf
);
3741 if (IS_ERR(ren
->newpath
)) {
3742 putname(ren
->oldpath
);
3743 return PTR_ERR(ren
->newpath
);
3746 req
->flags
|= REQ_F_NEED_CLEANUP
;
3750 static int io_renameat(struct io_kiocb
*req
, bool force_nonblock
)
3752 struct io_rename
*ren
= &req
->rename
;
3758 ret
= do_renameat2(ren
->old_dfd
, ren
->oldpath
, ren
->new_dfd
,
3759 ren
->newpath
, ren
->flags
);
3761 req
->flags
&= ~REQ_F_NEED_CLEANUP
;
3763 req_set_fail_links(req
);
3764 io_req_complete(req
, ret
);
3768 static int io_unlinkat_prep(struct io_kiocb
*req
,
3769 const struct io_uring_sqe
*sqe
)
3771 struct io_unlink
*un
= &req
->unlink
;
3772 const char __user
*fname
;
3774 if (unlikely(req
->flags
& REQ_F_FIXED_FILE
))
3777 un
->dfd
= READ_ONCE(sqe
->fd
);
3779 un
->flags
= READ_ONCE(sqe
->unlink_flags
);
3780 if (un
->flags
& ~AT_REMOVEDIR
)
3783 fname
= u64_to_user_ptr(READ_ONCE(sqe
->addr
));
3784 un
->filename
= getname(fname
);
3785 if (IS_ERR(un
->filename
))
3786 return PTR_ERR(un
->filename
);
3788 req
->flags
|= REQ_F_NEED_CLEANUP
;
3792 static int io_unlinkat(struct io_kiocb
*req
, bool force_nonblock
)
3794 struct io_unlink
*un
= &req
->unlink
;
3800 if (un
->flags
& AT_REMOVEDIR
)
3801 ret
= do_rmdir(un
->dfd
, un
->filename
);
3803 ret
= do_unlinkat(un
->dfd
, un
->filename
);
3805 req
->flags
&= ~REQ_F_NEED_CLEANUP
;
3807 req_set_fail_links(req
);
3808 io_req_complete(req
, ret
);
3812 static int io_shutdown_prep(struct io_kiocb
*req
,
3813 const struct io_uring_sqe
*sqe
)
3815 #if defined(CONFIG_NET)
3816 if (unlikely(req
->ctx
->flags
& IORING_SETUP_IOPOLL
))
3818 if (sqe
->ioprio
|| sqe
->off
|| sqe
->addr
|| sqe
->rw_flags
||
3822 req
->shutdown
.how
= READ_ONCE(sqe
->len
);
3829 static int io_shutdown(struct io_kiocb
*req
, bool force_nonblock
)
3831 #if defined(CONFIG_NET)
3832 struct socket
*sock
;
3838 sock
= sock_from_file(req
->file
);
3839 if (unlikely(!sock
))
3842 ret
= __sys_shutdown_sock(sock
, req
->shutdown
.how
);
3844 req_set_fail_links(req
);
3845 io_req_complete(req
, ret
);
3852 static int __io_splice_prep(struct io_kiocb
*req
,
3853 const struct io_uring_sqe
*sqe
)
3855 struct io_splice
* sp
= &req
->splice
;
3856 unsigned int valid_flags
= SPLICE_F_FD_IN_FIXED
| SPLICE_F_ALL
;
3858 if (unlikely(req
->ctx
->flags
& IORING_SETUP_IOPOLL
))
3862 sp
->len
= READ_ONCE(sqe
->len
);
3863 sp
->flags
= READ_ONCE(sqe
->splice_flags
);
3865 if (unlikely(sp
->flags
& ~valid_flags
))
3868 sp
->file_in
= io_file_get(NULL
, req
, READ_ONCE(sqe
->splice_fd_in
),
3869 (sp
->flags
& SPLICE_F_FD_IN_FIXED
));
3872 req
->flags
|= REQ_F_NEED_CLEANUP
;
3874 if (!S_ISREG(file_inode(sp
->file_in
)->i_mode
)) {
3876 * Splice operation will be punted aync, and here need to
3877 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3879 io_req_init_async(req
);
3880 req
->work
.flags
|= IO_WQ_WORK_UNBOUND
;
3886 static int io_tee_prep(struct io_kiocb
*req
,
3887 const struct io_uring_sqe
*sqe
)
3889 if (READ_ONCE(sqe
->splice_off_in
) || READ_ONCE(sqe
->off
))
3891 return __io_splice_prep(req
, sqe
);
3894 static int io_tee(struct io_kiocb
*req
, bool force_nonblock
)
3896 struct io_splice
*sp
= &req
->splice
;
3897 struct file
*in
= sp
->file_in
;
3898 struct file
*out
= sp
->file_out
;
3899 unsigned int flags
= sp
->flags
& ~SPLICE_F_FD_IN_FIXED
;
3905 ret
= do_tee(in
, out
, sp
->len
, flags
);
3907 io_put_file(req
, in
, (sp
->flags
& SPLICE_F_FD_IN_FIXED
));
3908 req
->flags
&= ~REQ_F_NEED_CLEANUP
;
3911 req_set_fail_links(req
);
3912 io_req_complete(req
, ret
);
3916 static int io_splice_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
3918 struct io_splice
* sp
= &req
->splice
;
3920 sp
->off_in
= READ_ONCE(sqe
->splice_off_in
);
3921 sp
->off_out
= READ_ONCE(sqe
->off
);
3922 return __io_splice_prep(req
, sqe
);
3925 static int io_splice(struct io_kiocb
*req
, bool force_nonblock
)
3927 struct io_splice
*sp
= &req
->splice
;
3928 struct file
*in
= sp
->file_in
;
3929 struct file
*out
= sp
->file_out
;
3930 unsigned int flags
= sp
->flags
& ~SPLICE_F_FD_IN_FIXED
;
3931 loff_t
*poff_in
, *poff_out
;
3937 poff_in
= (sp
->off_in
== -1) ? NULL
: &sp
->off_in
;
3938 poff_out
= (sp
->off_out
== -1) ? NULL
: &sp
->off_out
;
3941 ret
= do_splice(in
, poff_in
, out
, poff_out
, sp
->len
, flags
);
3943 io_put_file(req
, in
, (sp
->flags
& SPLICE_F_FD_IN_FIXED
));
3944 req
->flags
&= ~REQ_F_NEED_CLEANUP
;
3947 req_set_fail_links(req
);
3948 io_req_complete(req
, ret
);
3953 * IORING_OP_NOP just posts a completion event, nothing else.
3955 static int io_nop(struct io_kiocb
*req
, struct io_comp_state
*cs
)
3957 struct io_ring_ctx
*ctx
= req
->ctx
;
3959 if (unlikely(ctx
->flags
& IORING_SETUP_IOPOLL
))
3962 __io_req_complete(req
, 0, 0, cs
);
3966 static int io_prep_fsync(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
3968 struct io_ring_ctx
*ctx
= req
->ctx
;
3973 if (unlikely(ctx
->flags
& IORING_SETUP_IOPOLL
))
3975 if (unlikely(sqe
->addr
|| sqe
->ioprio
|| sqe
->buf_index
))
3978 req
->sync
.flags
= READ_ONCE(sqe
->fsync_flags
);
3979 if (unlikely(req
->sync
.flags
& ~IORING_FSYNC_DATASYNC
))
3982 req
->sync
.off
= READ_ONCE(sqe
->off
);
3983 req
->sync
.len
= READ_ONCE(sqe
->len
);
3987 static int io_fsync(struct io_kiocb
*req
, bool force_nonblock
)
3989 loff_t end
= req
->sync
.off
+ req
->sync
.len
;
3992 /* fsync always requires a blocking context */
3996 ret
= vfs_fsync_range(req
->file
, req
->sync
.off
,
3997 end
> 0 ? end
: LLONG_MAX
,
3998 req
->sync
.flags
& IORING_FSYNC_DATASYNC
);
4000 req_set_fail_links(req
);
4001 io_req_complete(req
, ret
);
4005 static int io_fallocate_prep(struct io_kiocb
*req
,
4006 const struct io_uring_sqe
*sqe
)
4008 if (sqe
->ioprio
|| sqe
->buf_index
|| sqe
->rw_flags
)
4010 if (unlikely(req
->ctx
->flags
& IORING_SETUP_IOPOLL
))
4013 req
->sync
.off
= READ_ONCE(sqe
->off
);
4014 req
->sync
.len
= READ_ONCE(sqe
->addr
);
4015 req
->sync
.mode
= READ_ONCE(sqe
->len
);
4019 static int io_fallocate(struct io_kiocb
*req
, bool force_nonblock
)
4023 /* fallocate always requiring blocking context */
4026 ret
= vfs_fallocate(req
->file
, req
->sync
.mode
, req
->sync
.off
,
4029 req_set_fail_links(req
);
4030 io_req_complete(req
, ret
);
4034 static int __io_openat_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
4036 const char __user
*fname
;
4039 if (unlikely(sqe
->ioprio
|| sqe
->buf_index
))
4041 if (unlikely(req
->flags
& REQ_F_FIXED_FILE
))
4044 /* open.how should be already initialised */
4045 if (!(req
->open
.how
.flags
& O_PATH
) && force_o_largefile())
4046 req
->open
.how
.flags
|= O_LARGEFILE
;
4048 req
->open
.dfd
= READ_ONCE(sqe
->fd
);
4049 fname
= u64_to_user_ptr(READ_ONCE(sqe
->addr
));
4050 req
->open
.filename
= getname(fname
);
4051 if (IS_ERR(req
->open
.filename
)) {
4052 ret
= PTR_ERR(req
->open
.filename
);
4053 req
->open
.filename
= NULL
;
4056 req
->open
.nofile
= rlimit(RLIMIT_NOFILE
);
4057 req
->open
.ignore_nonblock
= false;
4058 req
->flags
|= REQ_F_NEED_CLEANUP
;
4062 static int io_openat_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
4066 if (unlikely(req
->ctx
->flags
& IORING_SETUP_IOPOLL
))
4068 mode
= READ_ONCE(sqe
->len
);
4069 flags
= READ_ONCE(sqe
->open_flags
);
4070 req
->open
.how
= build_open_how(flags
, mode
);
4071 return __io_openat_prep(req
, sqe
);
4074 static int io_openat2_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
4076 struct open_how __user
*how
;
4080 if (unlikely(req
->ctx
->flags
& IORING_SETUP_IOPOLL
))
4082 how
= u64_to_user_ptr(READ_ONCE(sqe
->addr2
));
4083 len
= READ_ONCE(sqe
->len
);
4084 if (len
< OPEN_HOW_SIZE_VER0
)
4087 ret
= copy_struct_from_user(&req
->open
.how
, sizeof(req
->open
.how
), how
,
4092 return __io_openat_prep(req
, sqe
);
4095 static int io_openat2(struct io_kiocb
*req
, bool force_nonblock
)
4097 struct open_flags op
;
4101 if (force_nonblock
&& !req
->open
.ignore_nonblock
)
4104 ret
= build_open_flags(&req
->open
.how
, &op
);
4108 ret
= __get_unused_fd_flags(req
->open
.how
.flags
, req
->open
.nofile
);
4112 file
= do_filp_open(req
->open
.dfd
, req
->open
.filename
, &op
);
4115 ret
= PTR_ERR(file
);
4117 * A work-around to ensure that /proc/self works that way
4118 * that it should - if we get -EOPNOTSUPP back, then assume
4119 * that proc_self_get_link() failed us because we're in async
4120 * context. We should be safe to retry this from the task
4121 * itself with force_nonblock == false set, as it should not
4122 * block on lookup. Would be nice to know this upfront and
4123 * avoid the async dance, but doesn't seem feasible.
4125 if (ret
== -EOPNOTSUPP
&& io_wq_current_is_worker()) {
4126 req
->open
.ignore_nonblock
= true;
4127 refcount_inc(&req
->refs
);
4128 io_req_task_queue(req
);
4132 fsnotify_open(file
);
4133 fd_install(ret
, file
);
4136 putname(req
->open
.filename
);
4137 req
->flags
&= ~REQ_F_NEED_CLEANUP
;
4139 req_set_fail_links(req
);
4140 io_req_complete(req
, ret
);
4144 static int io_openat(struct io_kiocb
*req
, bool force_nonblock
)
4146 return io_openat2(req
, force_nonblock
);
4149 static int io_remove_buffers_prep(struct io_kiocb
*req
,
4150 const struct io_uring_sqe
*sqe
)
4152 struct io_provide_buf
*p
= &req
->pbuf
;
4155 if (sqe
->ioprio
|| sqe
->rw_flags
|| sqe
->addr
|| sqe
->len
|| sqe
->off
)
4158 tmp
= READ_ONCE(sqe
->fd
);
4159 if (!tmp
|| tmp
> USHRT_MAX
)
4162 memset(p
, 0, sizeof(*p
));
4164 p
->bgid
= READ_ONCE(sqe
->buf_group
);
4168 static int __io_remove_buffers(struct io_ring_ctx
*ctx
, struct io_buffer
*buf
,
4169 int bgid
, unsigned nbufs
)
4173 /* shouldn't happen */
4177 /* the head kbuf is the list itself */
4178 while (!list_empty(&buf
->list
)) {
4179 struct io_buffer
*nxt
;
4181 nxt
= list_first_entry(&buf
->list
, struct io_buffer
, list
);
4182 list_del(&nxt
->list
);
4189 idr_remove(&ctx
->io_buffer_idr
, bgid
);
4194 static int io_remove_buffers(struct io_kiocb
*req
, bool force_nonblock
,
4195 struct io_comp_state
*cs
)
4197 struct io_provide_buf
*p
= &req
->pbuf
;
4198 struct io_ring_ctx
*ctx
= req
->ctx
;
4199 struct io_buffer
*head
;
4202 io_ring_submit_lock(ctx
, !force_nonblock
);
4204 lockdep_assert_held(&ctx
->uring_lock
);
4207 head
= idr_find(&ctx
->io_buffer_idr
, p
->bgid
);
4209 ret
= __io_remove_buffers(ctx
, head
, p
->bgid
, p
->nbufs
);
4211 req_set_fail_links(req
);
4213 /* need to hold the lock to complete IOPOLL requests */
4214 if (ctx
->flags
& IORING_SETUP_IOPOLL
) {
4215 __io_req_complete(req
, ret
, 0, cs
);
4216 io_ring_submit_unlock(ctx
, !force_nonblock
);
4218 io_ring_submit_unlock(ctx
, !force_nonblock
);
4219 __io_req_complete(req
, ret
, 0, cs
);
4224 static int io_provide_buffers_prep(struct io_kiocb
*req
,
4225 const struct io_uring_sqe
*sqe
)
4227 struct io_provide_buf
*p
= &req
->pbuf
;
4230 if (sqe
->ioprio
|| sqe
->rw_flags
)
4233 tmp
= READ_ONCE(sqe
->fd
);
4234 if (!tmp
|| tmp
> USHRT_MAX
)
4237 p
->addr
= READ_ONCE(sqe
->addr
);
4238 p
->len
= READ_ONCE(sqe
->len
);
4240 if (!access_ok(u64_to_user_ptr(p
->addr
), (p
->len
* p
->nbufs
)))
4243 p
->bgid
= READ_ONCE(sqe
->buf_group
);
4244 tmp
= READ_ONCE(sqe
->off
);
4245 if (tmp
> USHRT_MAX
)
4251 static int io_add_buffers(struct io_provide_buf
*pbuf
, struct io_buffer
**head
)
4253 struct io_buffer
*buf
;
4254 u64 addr
= pbuf
->addr
;
4255 int i
, bid
= pbuf
->bid
;
4257 for (i
= 0; i
< pbuf
->nbufs
; i
++) {
4258 buf
= kmalloc(sizeof(*buf
), GFP_KERNEL
);
4263 buf
->len
= pbuf
->len
;
4268 INIT_LIST_HEAD(&buf
->list
);
4271 list_add_tail(&buf
->list
, &(*head
)->list
);
4275 return i
? i
: -ENOMEM
;
4278 static int io_provide_buffers(struct io_kiocb
*req
, bool force_nonblock
,
4279 struct io_comp_state
*cs
)
4281 struct io_provide_buf
*p
= &req
->pbuf
;
4282 struct io_ring_ctx
*ctx
= req
->ctx
;
4283 struct io_buffer
*head
, *list
;
4286 io_ring_submit_lock(ctx
, !force_nonblock
);
4288 lockdep_assert_held(&ctx
->uring_lock
);
4290 list
= head
= idr_find(&ctx
->io_buffer_idr
, p
->bgid
);
4292 ret
= io_add_buffers(p
, &head
);
4297 ret
= idr_alloc(&ctx
->io_buffer_idr
, head
, p
->bgid
, p
->bgid
+ 1,
4300 __io_remove_buffers(ctx
, head
, p
->bgid
, -1U);
4306 req_set_fail_links(req
);
4308 /* need to hold the lock to complete IOPOLL requests */
4309 if (ctx
->flags
& IORING_SETUP_IOPOLL
) {
4310 __io_req_complete(req
, ret
, 0, cs
);
4311 io_ring_submit_unlock(ctx
, !force_nonblock
);
4313 io_ring_submit_unlock(ctx
, !force_nonblock
);
4314 __io_req_complete(req
, ret
, 0, cs
);
4319 static int io_epoll_ctl_prep(struct io_kiocb
*req
,
4320 const struct io_uring_sqe
*sqe
)
4322 #if defined(CONFIG_EPOLL)
4323 if (sqe
->ioprio
|| sqe
->buf_index
)
4325 if (unlikely(req
->ctx
->flags
& (IORING_SETUP_IOPOLL
| IORING_SETUP_SQPOLL
)))
4328 req
->epoll
.epfd
= READ_ONCE(sqe
->fd
);
4329 req
->epoll
.op
= READ_ONCE(sqe
->len
);
4330 req
->epoll
.fd
= READ_ONCE(sqe
->off
);
4332 if (ep_op_has_event(req
->epoll
.op
)) {
4333 struct epoll_event __user
*ev
;
4335 ev
= u64_to_user_ptr(READ_ONCE(sqe
->addr
));
4336 if (copy_from_user(&req
->epoll
.event
, ev
, sizeof(*ev
)))
4346 static int io_epoll_ctl(struct io_kiocb
*req
, bool force_nonblock
,
4347 struct io_comp_state
*cs
)
4349 #if defined(CONFIG_EPOLL)
4350 struct io_epoll
*ie
= &req
->epoll
;
4353 ret
= do_epoll_ctl(ie
->epfd
, ie
->op
, ie
->fd
, &ie
->event
, force_nonblock
);
4354 if (force_nonblock
&& ret
== -EAGAIN
)
4358 req_set_fail_links(req
);
4359 __io_req_complete(req
, ret
, 0, cs
);
4366 static int io_madvise_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
4368 #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4369 if (sqe
->ioprio
|| sqe
->buf_index
|| sqe
->off
)
4371 if (unlikely(req
->ctx
->flags
& IORING_SETUP_IOPOLL
))
4374 req
->madvise
.addr
= READ_ONCE(sqe
->addr
);
4375 req
->madvise
.len
= READ_ONCE(sqe
->len
);
4376 req
->madvise
.advice
= READ_ONCE(sqe
->fadvise_advice
);
4383 static int io_madvise(struct io_kiocb
*req
, bool force_nonblock
)
4385 #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4386 struct io_madvise
*ma
= &req
->madvise
;
4392 ret
= do_madvise(current
->mm
, ma
->addr
, ma
->len
, ma
->advice
);
4394 req_set_fail_links(req
);
4395 io_req_complete(req
, ret
);
4402 static int io_fadvise_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
4404 if (sqe
->ioprio
|| sqe
->buf_index
|| sqe
->addr
)
4406 if (unlikely(req
->ctx
->flags
& IORING_SETUP_IOPOLL
))
4409 req
->fadvise
.offset
= READ_ONCE(sqe
->off
);
4410 req
->fadvise
.len
= READ_ONCE(sqe
->len
);
4411 req
->fadvise
.advice
= READ_ONCE(sqe
->fadvise_advice
);
4415 static int io_fadvise(struct io_kiocb
*req
, bool force_nonblock
)
4417 struct io_fadvise
*fa
= &req
->fadvise
;
4420 if (force_nonblock
) {
4421 switch (fa
->advice
) {
4422 case POSIX_FADV_NORMAL
:
4423 case POSIX_FADV_RANDOM
:
4424 case POSIX_FADV_SEQUENTIAL
:
4431 ret
= vfs_fadvise(req
->file
, fa
->offset
, fa
->len
, fa
->advice
);
4433 req_set_fail_links(req
);
4434 io_req_complete(req
, ret
);
4438 static int io_statx_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
4440 if (unlikely(req
->ctx
->flags
& (IORING_SETUP_IOPOLL
| IORING_SETUP_SQPOLL
)))
4442 if (sqe
->ioprio
|| sqe
->buf_index
)
4444 if (req
->flags
& REQ_F_FIXED_FILE
)
4447 req
->statx
.dfd
= READ_ONCE(sqe
->fd
);
4448 req
->statx
.mask
= READ_ONCE(sqe
->len
);
4449 req
->statx
.filename
= u64_to_user_ptr(READ_ONCE(sqe
->addr
));
4450 req
->statx
.buffer
= u64_to_user_ptr(READ_ONCE(sqe
->addr2
));
4451 req
->statx
.flags
= READ_ONCE(sqe
->statx_flags
);
4456 static int io_statx(struct io_kiocb
*req
, bool force_nonblock
)
4458 struct io_statx
*ctx
= &req
->statx
;
4461 if (force_nonblock
) {
4462 /* only need file table for an actual valid fd */
4463 if (ctx
->dfd
== -1 || ctx
->dfd
== AT_FDCWD
)
4464 req
->flags
|= REQ_F_NO_FILE_TABLE
;
4468 ret
= do_statx(ctx
->dfd
, ctx
->filename
, ctx
->flags
, ctx
->mask
,
4472 req_set_fail_links(req
);
4473 io_req_complete(req
, ret
);
4477 static int io_close_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
4480 * If we queue this for async, it must not be cancellable. That would
4481 * leave the 'file' in an undeterminate state, and here need to modify
4482 * io_wq_work.flags, so initialize io_wq_work firstly.
4484 io_req_init_async(req
);
4486 if (unlikely(req
->ctx
->flags
& IORING_SETUP_IOPOLL
))
4488 if (sqe
->ioprio
|| sqe
->off
|| sqe
->addr
|| sqe
->len
||
4489 sqe
->rw_flags
|| sqe
->buf_index
)
4491 if (req
->flags
& REQ_F_FIXED_FILE
)
4494 req
->close
.fd
= READ_ONCE(sqe
->fd
);
4495 if ((req
->file
&& req
->file
->f_op
== &io_uring_fops
))
4498 req
->close
.put_file
= NULL
;
4502 static int io_close(struct io_kiocb
*req
, bool force_nonblock
,
4503 struct io_comp_state
*cs
)
4505 struct io_close
*close
= &req
->close
;
4508 /* might be already done during nonblock submission */
4509 if (!close
->put_file
) {
4510 ret
= close_fd_get_file(close
->fd
, &close
->put_file
);
4512 return (ret
== -ENOENT
) ? -EBADF
: ret
;
4515 /* if the file has a flush method, be safe and punt to async */
4516 if (close
->put_file
->f_op
->flush
&& force_nonblock
) {
4517 /* not safe to cancel at this point */
4518 req
->work
.flags
|= IO_WQ_WORK_NO_CANCEL
;
4519 /* was never set, but play safe */
4520 req
->flags
&= ~REQ_F_NOWAIT
;
4521 /* avoid grabbing files - we don't need the files */
4522 req
->flags
|= REQ_F_NO_FILE_TABLE
;
4526 /* No ->flush() or already async, safely close from here */
4527 ret
= filp_close(close
->put_file
, req
->work
.identity
->files
);
4529 req_set_fail_links(req
);
4530 fput(close
->put_file
);
4531 close
->put_file
= NULL
;
4532 __io_req_complete(req
, ret
, 0, cs
);
4536 static int io_prep_sfr(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
4538 struct io_ring_ctx
*ctx
= req
->ctx
;
4543 if (unlikely(ctx
->flags
& IORING_SETUP_IOPOLL
))
4545 if (unlikely(sqe
->addr
|| sqe
->ioprio
|| sqe
->buf_index
))
4548 req
->sync
.off
= READ_ONCE(sqe
->off
);
4549 req
->sync
.len
= READ_ONCE(sqe
->len
);
4550 req
->sync
.flags
= READ_ONCE(sqe
->sync_range_flags
);
4554 static int io_sync_file_range(struct io_kiocb
*req
, bool force_nonblock
)
4558 /* sync_file_range always requires a blocking context */
4562 ret
= sync_file_range(req
->file
, req
->sync
.off
, req
->sync
.len
,
4565 req_set_fail_links(req
);
4566 io_req_complete(req
, ret
);
4570 #if defined(CONFIG_NET)
4571 static int io_setup_async_msg(struct io_kiocb
*req
,
4572 struct io_async_msghdr
*kmsg
)
4574 struct io_async_msghdr
*async_msg
= req
->async_data
;
4578 if (io_alloc_async_data(req
)) {
4579 if (kmsg
->iov
!= kmsg
->fast_iov
)
4583 async_msg
= req
->async_data
;
4584 req
->flags
|= REQ_F_NEED_CLEANUP
;
4585 memcpy(async_msg
, kmsg
, sizeof(*kmsg
));
4589 static int io_sendmsg_copy_hdr(struct io_kiocb
*req
,
4590 struct io_async_msghdr
*iomsg
)
4592 iomsg
->iov
= iomsg
->fast_iov
;
4593 iomsg
->msg
.msg_name
= &iomsg
->addr
;
4594 return sendmsg_copy_msghdr(&iomsg
->msg
, req
->sr_msg
.umsg
,
4595 req
->sr_msg
.msg_flags
, &iomsg
->iov
);
4598 static int io_sendmsg_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
4600 struct io_async_msghdr
*async_msg
= req
->async_data
;
4601 struct io_sr_msg
*sr
= &req
->sr_msg
;
4604 if (unlikely(req
->ctx
->flags
& IORING_SETUP_IOPOLL
))
4607 sr
->msg_flags
= READ_ONCE(sqe
->msg_flags
);
4608 sr
->umsg
= u64_to_user_ptr(READ_ONCE(sqe
->addr
));
4609 sr
->len
= READ_ONCE(sqe
->len
);
4611 #ifdef CONFIG_COMPAT
4612 if (req
->ctx
->compat
)
4613 sr
->msg_flags
|= MSG_CMSG_COMPAT
;
4616 if (!async_msg
|| !io_op_defs
[req
->opcode
].needs_async_data
)
4618 ret
= io_sendmsg_copy_hdr(req
, async_msg
);
4620 req
->flags
|= REQ_F_NEED_CLEANUP
;
4624 static int io_sendmsg(struct io_kiocb
*req
, bool force_nonblock
,
4625 struct io_comp_state
*cs
)
4627 struct io_async_msghdr iomsg
, *kmsg
;
4628 struct socket
*sock
;
4632 sock
= sock_from_file(req
->file
);
4633 if (unlikely(!sock
))
4636 if (req
->async_data
) {
4637 kmsg
= req
->async_data
;
4638 kmsg
->msg
.msg_name
= &kmsg
->addr
;
4639 /* if iov is set, it's allocated already */
4641 kmsg
->iov
= kmsg
->fast_iov
;
4642 kmsg
->msg
.msg_iter
.iov
= kmsg
->iov
;
4644 ret
= io_sendmsg_copy_hdr(req
, &iomsg
);
4650 flags
= req
->sr_msg
.msg_flags
;
4651 if (flags
& MSG_DONTWAIT
)
4652 req
->flags
|= REQ_F_NOWAIT
;
4653 else if (force_nonblock
)
4654 flags
|= MSG_DONTWAIT
;
4656 ret
= __sys_sendmsg_sock(sock
, &kmsg
->msg
, flags
);
4657 if (force_nonblock
&& ret
== -EAGAIN
)
4658 return io_setup_async_msg(req
, kmsg
);
4659 if (ret
== -ERESTARTSYS
)
4662 if (kmsg
->iov
!= kmsg
->fast_iov
)
4664 req
->flags
&= ~REQ_F_NEED_CLEANUP
;
4666 req_set_fail_links(req
);
4667 __io_req_complete(req
, ret
, 0, cs
);
4671 static int io_send(struct io_kiocb
*req
, bool force_nonblock
,
4672 struct io_comp_state
*cs
)
4674 struct io_sr_msg
*sr
= &req
->sr_msg
;
4677 struct socket
*sock
;
4681 sock
= sock_from_file(req
->file
);
4682 if (unlikely(!sock
))
4685 ret
= import_single_range(WRITE
, sr
->buf
, sr
->len
, &iov
, &msg
.msg_iter
);
4689 msg
.msg_name
= NULL
;
4690 msg
.msg_control
= NULL
;
4691 msg
.msg_controllen
= 0;
4692 msg
.msg_namelen
= 0;
4694 flags
= req
->sr_msg
.msg_flags
;
4695 if (flags
& MSG_DONTWAIT
)
4696 req
->flags
|= REQ_F_NOWAIT
;
4697 else if (force_nonblock
)
4698 flags
|= MSG_DONTWAIT
;
4700 msg
.msg_flags
= flags
;
4701 ret
= sock_sendmsg(sock
, &msg
);
4702 if (force_nonblock
&& ret
== -EAGAIN
)
4704 if (ret
== -ERESTARTSYS
)
4708 req_set_fail_links(req
);
4709 __io_req_complete(req
, ret
, 0, cs
);
4713 static int __io_recvmsg_copy_hdr(struct io_kiocb
*req
,
4714 struct io_async_msghdr
*iomsg
)
4716 struct io_sr_msg
*sr
= &req
->sr_msg
;
4717 struct iovec __user
*uiov
;
4721 ret
= __copy_msghdr_from_user(&iomsg
->msg
, sr
->umsg
,
4722 &iomsg
->uaddr
, &uiov
, &iov_len
);
4726 if (req
->flags
& REQ_F_BUFFER_SELECT
) {
4729 if (copy_from_user(iomsg
->iov
, uiov
, sizeof(*uiov
)))
4731 sr
->len
= iomsg
->iov
[0].iov_len
;
4732 iov_iter_init(&iomsg
->msg
.msg_iter
, READ
, iomsg
->iov
, 1,
4736 ret
= __import_iovec(READ
, uiov
, iov_len
, UIO_FASTIOV
,
4737 &iomsg
->iov
, &iomsg
->msg
.msg_iter
,
4746 #ifdef CONFIG_COMPAT
4747 static int __io_compat_recvmsg_copy_hdr(struct io_kiocb
*req
,
4748 struct io_async_msghdr
*iomsg
)
4750 struct compat_msghdr __user
*msg_compat
;
4751 struct io_sr_msg
*sr
= &req
->sr_msg
;
4752 struct compat_iovec __user
*uiov
;
4757 msg_compat
= (struct compat_msghdr __user
*) sr
->umsg
;
4758 ret
= __get_compat_msghdr(&iomsg
->msg
, msg_compat
, &iomsg
->uaddr
,
4763 uiov
= compat_ptr(ptr
);
4764 if (req
->flags
& REQ_F_BUFFER_SELECT
) {
4765 compat_ssize_t clen
;
4769 if (!access_ok(uiov
, sizeof(*uiov
)))
4771 if (__get_user(clen
, &uiov
->iov_len
))
4776 iomsg
->iov
[0].iov_len
= clen
;
4779 ret
= __import_iovec(READ
, (struct iovec __user
*)uiov
, len
,
4780 UIO_FASTIOV
, &iomsg
->iov
,
4781 &iomsg
->msg
.msg_iter
, true);
4790 static int io_recvmsg_copy_hdr(struct io_kiocb
*req
,
4791 struct io_async_msghdr
*iomsg
)
4793 iomsg
->msg
.msg_name
= &iomsg
->addr
;
4794 iomsg
->iov
= iomsg
->fast_iov
;
4796 #ifdef CONFIG_COMPAT
4797 if (req
->ctx
->compat
)
4798 return __io_compat_recvmsg_copy_hdr(req
, iomsg
);
4801 return __io_recvmsg_copy_hdr(req
, iomsg
);
4804 static struct io_buffer
*io_recv_buffer_select(struct io_kiocb
*req
,
4807 struct io_sr_msg
*sr
= &req
->sr_msg
;
4808 struct io_buffer
*kbuf
;
4810 kbuf
= io_buffer_select(req
, &sr
->len
, sr
->bgid
, sr
->kbuf
, needs_lock
);
4815 req
->flags
|= REQ_F_BUFFER_SELECTED
;
4819 static inline unsigned int io_put_recv_kbuf(struct io_kiocb
*req
)
4821 return io_put_kbuf(req
, req
->sr_msg
.kbuf
);
4824 static int io_recvmsg_prep(struct io_kiocb
*req
,
4825 const struct io_uring_sqe
*sqe
)
4827 struct io_async_msghdr
*async_msg
= req
->async_data
;
4828 struct io_sr_msg
*sr
= &req
->sr_msg
;
4831 if (unlikely(req
->ctx
->flags
& IORING_SETUP_IOPOLL
))
4834 sr
->msg_flags
= READ_ONCE(sqe
->msg_flags
);
4835 sr
->umsg
= u64_to_user_ptr(READ_ONCE(sqe
->addr
));
4836 sr
->len
= READ_ONCE(sqe
->len
);
4837 sr
->bgid
= READ_ONCE(sqe
->buf_group
);
4839 #ifdef CONFIG_COMPAT
4840 if (req
->ctx
->compat
)
4841 sr
->msg_flags
|= MSG_CMSG_COMPAT
;
4844 if (!async_msg
|| !io_op_defs
[req
->opcode
].needs_async_data
)
4846 ret
= io_recvmsg_copy_hdr(req
, async_msg
);
4848 req
->flags
|= REQ_F_NEED_CLEANUP
;
4852 static int io_recvmsg(struct io_kiocb
*req
, bool force_nonblock
,
4853 struct io_comp_state
*cs
)
4855 struct io_async_msghdr iomsg
, *kmsg
;
4856 struct socket
*sock
;
4857 struct io_buffer
*kbuf
;
4859 int ret
, cflags
= 0;
4861 sock
= sock_from_file(req
->file
);
4862 if (unlikely(!sock
))
4865 if (req
->async_data
) {
4866 kmsg
= req
->async_data
;
4867 kmsg
->msg
.msg_name
= &kmsg
->addr
;
4868 /* if iov is set, it's allocated already */
4870 kmsg
->iov
= kmsg
->fast_iov
;
4871 kmsg
->msg
.msg_iter
.iov
= kmsg
->iov
;
4873 ret
= io_recvmsg_copy_hdr(req
, &iomsg
);
4879 if (req
->flags
& REQ_F_BUFFER_SELECT
) {
4880 kbuf
= io_recv_buffer_select(req
, !force_nonblock
);
4882 return PTR_ERR(kbuf
);
4883 kmsg
->fast_iov
[0].iov_base
= u64_to_user_ptr(kbuf
->addr
);
4884 iov_iter_init(&kmsg
->msg
.msg_iter
, READ
, kmsg
->iov
,
4885 1, req
->sr_msg
.len
);
4888 flags
= req
->sr_msg
.msg_flags
;
4889 if (flags
& MSG_DONTWAIT
)
4890 req
->flags
|= REQ_F_NOWAIT
;
4891 else if (force_nonblock
)
4892 flags
|= MSG_DONTWAIT
;
4894 ret
= __sys_recvmsg_sock(sock
, &kmsg
->msg
, req
->sr_msg
.umsg
,
4895 kmsg
->uaddr
, flags
);
4896 if (force_nonblock
&& ret
== -EAGAIN
)
4897 return io_setup_async_msg(req
, kmsg
);
4898 if (ret
== -ERESTARTSYS
)
4901 if (req
->flags
& REQ_F_BUFFER_SELECTED
)
4902 cflags
= io_put_recv_kbuf(req
);
4903 if (kmsg
->iov
!= kmsg
->fast_iov
)
4905 req
->flags
&= ~REQ_F_NEED_CLEANUP
;
4907 req_set_fail_links(req
);
4908 __io_req_complete(req
, ret
, cflags
, cs
);
4912 static int io_recv(struct io_kiocb
*req
, bool force_nonblock
,
4913 struct io_comp_state
*cs
)
4915 struct io_buffer
*kbuf
;
4916 struct io_sr_msg
*sr
= &req
->sr_msg
;
4918 void __user
*buf
= sr
->buf
;
4919 struct socket
*sock
;
4922 int ret
, cflags
= 0;
4924 sock
= sock_from_file(req
->file
);
4925 if (unlikely(!sock
))
4928 if (req
->flags
& REQ_F_BUFFER_SELECT
) {
4929 kbuf
= io_recv_buffer_select(req
, !force_nonblock
);
4931 return PTR_ERR(kbuf
);
4932 buf
= u64_to_user_ptr(kbuf
->addr
);
4935 ret
= import_single_range(READ
, buf
, sr
->len
, &iov
, &msg
.msg_iter
);
4939 msg
.msg_name
= NULL
;
4940 msg
.msg_control
= NULL
;
4941 msg
.msg_controllen
= 0;
4942 msg
.msg_namelen
= 0;
4943 msg
.msg_iocb
= NULL
;
4946 flags
= req
->sr_msg
.msg_flags
;
4947 if (flags
& MSG_DONTWAIT
)
4948 req
->flags
|= REQ_F_NOWAIT
;
4949 else if (force_nonblock
)
4950 flags
|= MSG_DONTWAIT
;
4952 ret
= sock_recvmsg(sock
, &msg
, flags
);
4953 if (force_nonblock
&& ret
== -EAGAIN
)
4955 if (ret
== -ERESTARTSYS
)
4958 if (req
->flags
& REQ_F_BUFFER_SELECTED
)
4959 cflags
= io_put_recv_kbuf(req
);
4961 req_set_fail_links(req
);
4962 __io_req_complete(req
, ret
, cflags
, cs
);
4966 static int io_accept_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
4968 struct io_accept
*accept
= &req
->accept
;
4970 if (unlikely(req
->ctx
->flags
& IORING_SETUP_IOPOLL
))
4972 if (sqe
->ioprio
|| sqe
->len
|| sqe
->buf_index
)
4975 accept
->addr
= u64_to_user_ptr(READ_ONCE(sqe
->addr
));
4976 accept
->addr_len
= u64_to_user_ptr(READ_ONCE(sqe
->addr2
));
4977 accept
->flags
= READ_ONCE(sqe
->accept_flags
);
4978 accept
->nofile
= rlimit(RLIMIT_NOFILE
);
4982 static int io_accept(struct io_kiocb
*req
, bool force_nonblock
,
4983 struct io_comp_state
*cs
)
4985 struct io_accept
*accept
= &req
->accept
;
4986 unsigned int file_flags
= force_nonblock
? O_NONBLOCK
: 0;
4989 if (req
->file
->f_flags
& O_NONBLOCK
)
4990 req
->flags
|= REQ_F_NOWAIT
;
4992 ret
= __sys_accept4_file(req
->file
, file_flags
, accept
->addr
,
4993 accept
->addr_len
, accept
->flags
,
4995 if (ret
== -EAGAIN
&& force_nonblock
)
4998 if (ret
== -ERESTARTSYS
)
5000 req_set_fail_links(req
);
5002 __io_req_complete(req
, ret
, 0, cs
);
5006 static int io_connect_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
5008 struct io_connect
*conn
= &req
->connect
;
5009 struct io_async_connect
*io
= req
->async_data
;
5011 if (unlikely(req
->ctx
->flags
& IORING_SETUP_IOPOLL
))
5013 if (sqe
->ioprio
|| sqe
->len
|| sqe
->buf_index
|| sqe
->rw_flags
)
5016 conn
->addr
= u64_to_user_ptr(READ_ONCE(sqe
->addr
));
5017 conn
->addr_len
= READ_ONCE(sqe
->addr2
);
5022 return move_addr_to_kernel(conn
->addr
, conn
->addr_len
,
5026 static int io_connect(struct io_kiocb
*req
, bool force_nonblock
,
5027 struct io_comp_state
*cs
)
5029 struct io_async_connect __io
, *io
;
5030 unsigned file_flags
;
5033 if (req
->async_data
) {
5034 io
= req
->async_data
;
5036 ret
= move_addr_to_kernel(req
->connect
.addr
,
5037 req
->connect
.addr_len
,
5044 file_flags
= force_nonblock
? O_NONBLOCK
: 0;
5046 ret
= __sys_connect_file(req
->file
, &io
->address
,
5047 req
->connect
.addr_len
, file_flags
);
5048 if ((ret
== -EAGAIN
|| ret
== -EINPROGRESS
) && force_nonblock
) {
5049 if (req
->async_data
)
5051 if (io_alloc_async_data(req
)) {
5055 io
= req
->async_data
;
5056 memcpy(req
->async_data
, &__io
, sizeof(__io
));
5059 if (ret
== -ERESTARTSYS
)
5063 req_set_fail_links(req
);
5064 __io_req_complete(req
, ret
, 0, cs
);
5067 #else /* !CONFIG_NET */
5068 static int io_sendmsg_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
5073 static int io_sendmsg(struct io_kiocb
*req
, bool force_nonblock
,
5074 struct io_comp_state
*cs
)
5079 static int io_send(struct io_kiocb
*req
, bool force_nonblock
,
5080 struct io_comp_state
*cs
)
5085 static int io_recvmsg_prep(struct io_kiocb
*req
,
5086 const struct io_uring_sqe
*sqe
)
5091 static int io_recvmsg(struct io_kiocb
*req
, bool force_nonblock
,
5092 struct io_comp_state
*cs
)
5097 static int io_recv(struct io_kiocb
*req
, bool force_nonblock
,
5098 struct io_comp_state
*cs
)
5103 static int io_accept_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
5108 static int io_accept(struct io_kiocb
*req
, bool force_nonblock
,
5109 struct io_comp_state
*cs
)
5114 static int io_connect_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
5119 static int io_connect(struct io_kiocb
*req
, bool force_nonblock
,
5120 struct io_comp_state
*cs
)
5124 #endif /* CONFIG_NET */
5126 struct io_poll_table
{
5127 struct poll_table_struct pt
;
5128 struct io_kiocb
*req
;
5132 static int __io_async_wake(struct io_kiocb
*req
, struct io_poll_iocb
*poll
,
5133 __poll_t mask
, task_work_func_t func
)
5137 /* for instances that support it check for an event match first: */
5138 if (mask
&& !(mask
& poll
->events
))
5141 trace_io_uring_task_add(req
->ctx
, req
->opcode
, req
->user_data
, mask
);
5143 list_del_init(&poll
->wait
.entry
);
5146 init_task_work(&req
->task_work
, func
);
5147 percpu_ref_get(&req
->ctx
->refs
);
5150 * If this fails, then the task is exiting. When a task exits, the
5151 * work gets canceled, so just cancel this request as well instead
5152 * of executing it. We can't safely execute it anyway, as we may not
5153 * have the needed state needed for it anyway.
5155 ret
= io_req_task_work_add(req
);
5156 if (unlikely(ret
)) {
5157 struct task_struct
*tsk
;
5159 WRITE_ONCE(poll
->canceled
, true);
5160 tsk
= io_wq_get_task(req
->ctx
->io_wq
);
5161 task_work_add(tsk
, &req
->task_work
, TWA_NONE
);
5162 wake_up_process(tsk
);
5167 static bool io_poll_rewait(struct io_kiocb
*req
, struct io_poll_iocb
*poll
)
5168 __acquires(&req
->ctx
->completion_lock
)
5170 struct io_ring_ctx
*ctx
= req
->ctx
;
5172 if (!req
->result
&& !READ_ONCE(poll
->canceled
)) {
5173 struct poll_table_struct pt
= { ._key
= poll
->events
};
5175 req
->result
= vfs_poll(req
->file
, &pt
) & poll
->events
;
5178 spin_lock_irq(&ctx
->completion_lock
);
5179 if (!req
->result
&& !READ_ONCE(poll
->canceled
)) {
5180 add_wait_queue(poll
->head
, &poll
->wait
);
5187 static struct io_poll_iocb
*io_poll_get_double(struct io_kiocb
*req
)
5189 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
5190 if (req
->opcode
== IORING_OP_POLL_ADD
)
5191 return req
->async_data
;
5192 return req
->apoll
->double_poll
;
5195 static struct io_poll_iocb
*io_poll_get_single(struct io_kiocb
*req
)
5197 if (req
->opcode
== IORING_OP_POLL_ADD
)
5199 return &req
->apoll
->poll
;
5202 static void io_poll_remove_double(struct io_kiocb
*req
)
5204 struct io_poll_iocb
*poll
= io_poll_get_double(req
);
5206 lockdep_assert_held(&req
->ctx
->completion_lock
);
5208 if (poll
&& poll
->head
) {
5209 struct wait_queue_head
*head
= poll
->head
;
5211 spin_lock(&head
->lock
);
5212 list_del_init(&poll
->wait
.entry
);
5213 if (poll
->wait
.private)
5214 refcount_dec(&req
->refs
);
5216 spin_unlock(&head
->lock
);
5220 static void io_poll_complete(struct io_kiocb
*req
, __poll_t mask
, int error
)
5222 struct io_ring_ctx
*ctx
= req
->ctx
;
5224 io_poll_remove_double(req
);
5225 req
->poll
.done
= true;
5226 io_cqring_fill_event(req
, error
? error
: mangle_poll(mask
));
5227 io_commit_cqring(ctx
);
5230 static void io_poll_task_func(struct callback_head
*cb
)
5232 struct io_kiocb
*req
= container_of(cb
, struct io_kiocb
, task_work
);
5233 struct io_ring_ctx
*ctx
= req
->ctx
;
5234 struct io_kiocb
*nxt
;
5236 if (io_poll_rewait(req
, &req
->poll
)) {
5237 spin_unlock_irq(&ctx
->completion_lock
);
5239 hash_del(&req
->hash_node
);
5240 io_poll_complete(req
, req
->result
, 0);
5241 spin_unlock_irq(&ctx
->completion_lock
);
5243 nxt
= io_put_req_find_next(req
);
5244 io_cqring_ev_posted(ctx
);
5246 __io_req_task_submit(nxt
);
5249 percpu_ref_put(&ctx
->refs
);
5252 static int io_poll_double_wake(struct wait_queue_entry
*wait
, unsigned mode
,
5253 int sync
, void *key
)
5255 struct io_kiocb
*req
= wait
->private;
5256 struct io_poll_iocb
*poll
= io_poll_get_single(req
);
5257 __poll_t mask
= key_to_poll(key
);
5259 /* for instances that support it check for an event match first: */
5260 if (mask
&& !(mask
& poll
->events
))
5263 list_del_init(&wait
->entry
);
5265 if (poll
&& poll
->head
) {
5268 spin_lock(&poll
->head
->lock
);
5269 done
= list_empty(&poll
->wait
.entry
);
5271 list_del_init(&poll
->wait
.entry
);
5272 /* make sure double remove sees this as being gone */
5273 wait
->private = NULL
;
5274 spin_unlock(&poll
->head
->lock
);
5276 /* use wait func handler, so it matches the rq type */
5277 poll
->wait
.func(&poll
->wait
, mode
, sync
, key
);
5280 refcount_dec(&req
->refs
);
5284 static void io_init_poll_iocb(struct io_poll_iocb
*poll
, __poll_t events
,
5285 wait_queue_func_t wake_func
)
5289 poll
->canceled
= false;
5290 poll
->events
= events
;
5291 INIT_LIST_HEAD(&poll
->wait
.entry
);
5292 init_waitqueue_func_entry(&poll
->wait
, wake_func
);
5295 static void __io_queue_proc(struct io_poll_iocb
*poll
, struct io_poll_table
*pt
,
5296 struct wait_queue_head
*head
,
5297 struct io_poll_iocb
**poll_ptr
)
5299 struct io_kiocb
*req
= pt
->req
;
5302 * If poll->head is already set, it's because the file being polled
5303 * uses multiple waitqueues for poll handling (eg one for read, one
5304 * for write). Setup a separate io_poll_iocb if this happens.
5306 if (unlikely(poll
->head
)) {
5307 struct io_poll_iocb
*poll_one
= poll
;
5309 /* already have a 2nd entry, fail a third attempt */
5311 pt
->error
= -EINVAL
;
5314 poll
= kmalloc(sizeof(*poll
), GFP_ATOMIC
);
5316 pt
->error
= -ENOMEM
;
5319 io_init_poll_iocb(poll
, poll_one
->events
, io_poll_double_wake
);
5320 refcount_inc(&req
->refs
);
5321 poll
->wait
.private = req
;
5328 if (poll
->events
& EPOLLEXCLUSIVE
)
5329 add_wait_queue_exclusive(head
, &poll
->wait
);
5331 add_wait_queue(head
, &poll
->wait
);
5334 static void io_async_queue_proc(struct file
*file
, struct wait_queue_head
*head
,
5335 struct poll_table_struct
*p
)
5337 struct io_poll_table
*pt
= container_of(p
, struct io_poll_table
, pt
);
5338 struct async_poll
*apoll
= pt
->req
->apoll
;
5340 __io_queue_proc(&apoll
->poll
, pt
, head
, &apoll
->double_poll
);
5343 static void io_async_task_func(struct callback_head
*cb
)
5345 struct io_kiocb
*req
= container_of(cb
, struct io_kiocb
, task_work
);
5346 struct async_poll
*apoll
= req
->apoll
;
5347 struct io_ring_ctx
*ctx
= req
->ctx
;
5349 trace_io_uring_task_run(req
->ctx
, req
->opcode
, req
->user_data
);
5351 if (io_poll_rewait(req
, &apoll
->poll
)) {
5352 spin_unlock_irq(&ctx
->completion_lock
);
5353 percpu_ref_put(&ctx
->refs
);
5357 /* If req is still hashed, it cannot have been canceled. Don't check. */
5358 if (hash_hashed(&req
->hash_node
))
5359 hash_del(&req
->hash_node
);
5361 io_poll_remove_double(req
);
5362 spin_unlock_irq(&ctx
->completion_lock
);
5364 if (!READ_ONCE(apoll
->poll
.canceled
))
5365 __io_req_task_submit(req
);
5367 __io_req_task_cancel(req
, -ECANCELED
);
5369 percpu_ref_put(&ctx
->refs
);
5370 kfree(apoll
->double_poll
);
5374 static int io_async_wake(struct wait_queue_entry
*wait
, unsigned mode
, int sync
,
5377 struct io_kiocb
*req
= wait
->private;
5378 struct io_poll_iocb
*poll
= &req
->apoll
->poll
;
5380 trace_io_uring_poll_wake(req
->ctx
, req
->opcode
, req
->user_data
,
5383 return __io_async_wake(req
, poll
, key_to_poll(key
), io_async_task_func
);
5386 static void io_poll_req_insert(struct io_kiocb
*req
)
5388 struct io_ring_ctx
*ctx
= req
->ctx
;
5389 struct hlist_head
*list
;
5391 list
= &ctx
->cancel_hash
[hash_long(req
->user_data
, ctx
->cancel_hash_bits
)];
5392 hlist_add_head(&req
->hash_node
, list
);
5395 static __poll_t
__io_arm_poll_handler(struct io_kiocb
*req
,
5396 struct io_poll_iocb
*poll
,
5397 struct io_poll_table
*ipt
, __poll_t mask
,
5398 wait_queue_func_t wake_func
)
5399 __acquires(&ctx
->completion_lock
)
5401 struct io_ring_ctx
*ctx
= req
->ctx
;
5402 bool cancel
= false;
5404 INIT_HLIST_NODE(&req
->hash_node
);
5405 io_init_poll_iocb(poll
, mask
, wake_func
);
5406 poll
->file
= req
->file
;
5407 poll
->wait
.private = req
;
5409 ipt
->pt
._key
= mask
;
5411 ipt
->error
= -EINVAL
;
5413 mask
= vfs_poll(req
->file
, &ipt
->pt
) & poll
->events
;
5415 spin_lock_irq(&ctx
->completion_lock
);
5416 if (likely(poll
->head
)) {
5417 spin_lock(&poll
->head
->lock
);
5418 if (unlikely(list_empty(&poll
->wait
.entry
))) {
5424 if (mask
|| ipt
->error
)
5425 list_del_init(&poll
->wait
.entry
);
5427 WRITE_ONCE(poll
->canceled
, true);
5428 else if (!poll
->done
) /* actually waiting for an event */
5429 io_poll_req_insert(req
);
5430 spin_unlock(&poll
->head
->lock
);
5436 static bool io_arm_poll_handler(struct io_kiocb
*req
)
5438 const struct io_op_def
*def
= &io_op_defs
[req
->opcode
];
5439 struct io_ring_ctx
*ctx
= req
->ctx
;
5440 struct async_poll
*apoll
;
5441 struct io_poll_table ipt
;
5445 if (!req
->file
|| !file_can_poll(req
->file
))
5447 if (req
->flags
& REQ_F_POLLED
)
5451 else if (def
->pollout
)
5455 /* if we can't nonblock try, then no point in arming a poll handler */
5456 if (!io_file_supports_async(req
->file
, rw
))
5459 apoll
= kmalloc(sizeof(*apoll
), GFP_ATOMIC
);
5460 if (unlikely(!apoll
))
5462 apoll
->double_poll
= NULL
;
5464 req
->flags
|= REQ_F_POLLED
;
5469 mask
|= POLLIN
| POLLRDNORM
;
5471 mask
|= POLLOUT
| POLLWRNORM
;
5473 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5474 if ((req
->opcode
== IORING_OP_RECVMSG
) &&
5475 (req
->sr_msg
.msg_flags
& MSG_ERRQUEUE
))
5478 mask
|= POLLERR
| POLLPRI
;
5480 ipt
.pt
._qproc
= io_async_queue_proc
;
5482 ret
= __io_arm_poll_handler(req
, &apoll
->poll
, &ipt
, mask
,
5484 if (ret
|| ipt
.error
) {
5485 io_poll_remove_double(req
);
5486 spin_unlock_irq(&ctx
->completion_lock
);
5487 kfree(apoll
->double_poll
);
5491 spin_unlock_irq(&ctx
->completion_lock
);
5492 trace_io_uring_poll_arm(ctx
, req
->opcode
, req
->user_data
, mask
,
5493 apoll
->poll
.events
);
5497 static bool __io_poll_remove_one(struct io_kiocb
*req
,
5498 struct io_poll_iocb
*poll
)
5500 bool do_complete
= false;
5502 spin_lock(&poll
->head
->lock
);
5503 WRITE_ONCE(poll
->canceled
, true);
5504 if (!list_empty(&poll
->wait
.entry
)) {
5505 list_del_init(&poll
->wait
.entry
);
5508 spin_unlock(&poll
->head
->lock
);
5509 hash_del(&req
->hash_node
);
5513 static bool io_poll_remove_one(struct io_kiocb
*req
)
5517 io_poll_remove_double(req
);
5519 if (req
->opcode
== IORING_OP_POLL_ADD
) {
5520 do_complete
= __io_poll_remove_one(req
, &req
->poll
);
5522 struct async_poll
*apoll
= req
->apoll
;
5524 /* non-poll requests have submit ref still */
5525 do_complete
= __io_poll_remove_one(req
, &apoll
->poll
);
5528 kfree(apoll
->double_poll
);
5534 io_cqring_fill_event(req
, -ECANCELED
);
5535 io_commit_cqring(req
->ctx
);
5536 req_set_fail_links(req
);
5537 io_put_req_deferred(req
, 1);
5544 * Returns true if we found and killed one or more poll requests
5546 static bool io_poll_remove_all(struct io_ring_ctx
*ctx
, struct task_struct
*tsk
,
5547 struct files_struct
*files
)
5549 struct hlist_node
*tmp
;
5550 struct io_kiocb
*req
;
5553 spin_lock_irq(&ctx
->completion_lock
);
5554 for (i
= 0; i
< (1U << ctx
->cancel_hash_bits
); i
++) {
5555 struct hlist_head
*list
;
5557 list
= &ctx
->cancel_hash
[i
];
5558 hlist_for_each_entry_safe(req
, tmp
, list
, hash_node
) {
5559 if (io_match_task(req
, tsk
, files
))
5560 posted
+= io_poll_remove_one(req
);
5563 spin_unlock_irq(&ctx
->completion_lock
);
5566 io_cqring_ev_posted(ctx
);
5571 static int io_poll_cancel(struct io_ring_ctx
*ctx
, __u64 sqe_addr
)
5573 struct hlist_head
*list
;
5574 struct io_kiocb
*req
;
5576 list
= &ctx
->cancel_hash
[hash_long(sqe_addr
, ctx
->cancel_hash_bits
)];
5577 hlist_for_each_entry(req
, list
, hash_node
) {
5578 if (sqe_addr
!= req
->user_data
)
5580 if (io_poll_remove_one(req
))
5588 static int io_poll_remove_prep(struct io_kiocb
*req
,
5589 const struct io_uring_sqe
*sqe
)
5591 if (unlikely(req
->ctx
->flags
& IORING_SETUP_IOPOLL
))
5593 if (sqe
->ioprio
|| sqe
->off
|| sqe
->len
|| sqe
->buf_index
||
5597 req
->poll_remove
.addr
= READ_ONCE(sqe
->addr
);
5602 * Find a running poll command that matches one specified in sqe->addr,
5603 * and remove it if found.
5605 static int io_poll_remove(struct io_kiocb
*req
)
5607 struct io_ring_ctx
*ctx
= req
->ctx
;
5610 spin_lock_irq(&ctx
->completion_lock
);
5611 ret
= io_poll_cancel(ctx
, req
->poll_remove
.addr
);
5612 spin_unlock_irq(&ctx
->completion_lock
);
5615 req_set_fail_links(req
);
5616 io_req_complete(req
, ret
);
5620 static int io_poll_wake(struct wait_queue_entry
*wait
, unsigned mode
, int sync
,
5623 struct io_kiocb
*req
= wait
->private;
5624 struct io_poll_iocb
*poll
= &req
->poll
;
5626 return __io_async_wake(req
, poll
, key_to_poll(key
), io_poll_task_func
);
5629 static void io_poll_queue_proc(struct file
*file
, struct wait_queue_head
*head
,
5630 struct poll_table_struct
*p
)
5632 struct io_poll_table
*pt
= container_of(p
, struct io_poll_table
, pt
);
5634 __io_queue_proc(&pt
->req
->poll
, pt
, head
, (struct io_poll_iocb
**) &pt
->req
->async_data
);
5637 static int io_poll_add_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
5639 struct io_poll_iocb
*poll
= &req
->poll
;
5642 if (unlikely(req
->ctx
->flags
& IORING_SETUP_IOPOLL
))
5644 if (sqe
->addr
|| sqe
->ioprio
|| sqe
->off
|| sqe
->len
|| sqe
->buf_index
)
5647 events
= READ_ONCE(sqe
->poll32_events
);
5649 events
= swahw32(events
);
5651 poll
->events
= demangle_poll(events
) | EPOLLERR
| EPOLLHUP
|
5652 (events
& EPOLLEXCLUSIVE
);
5656 static int io_poll_add(struct io_kiocb
*req
)
5658 struct io_poll_iocb
*poll
= &req
->poll
;
5659 struct io_ring_ctx
*ctx
= req
->ctx
;
5660 struct io_poll_table ipt
;
5663 ipt
.pt
._qproc
= io_poll_queue_proc
;
5665 mask
= __io_arm_poll_handler(req
, &req
->poll
, &ipt
, poll
->events
,
5668 if (mask
) { /* no async, we'd stolen it */
5670 io_poll_complete(req
, mask
, 0);
5672 spin_unlock_irq(&ctx
->completion_lock
);
5675 io_cqring_ev_posted(ctx
);
5681 static enum hrtimer_restart
io_timeout_fn(struct hrtimer
*timer
)
5683 struct io_timeout_data
*data
= container_of(timer
,
5684 struct io_timeout_data
, timer
);
5685 struct io_kiocb
*req
= data
->req
;
5686 struct io_ring_ctx
*ctx
= req
->ctx
;
5687 unsigned long flags
;
5689 spin_lock_irqsave(&ctx
->completion_lock
, flags
);
5690 list_del_init(&req
->timeout
.list
);
5691 atomic_set(&req
->ctx
->cq_timeouts
,
5692 atomic_read(&req
->ctx
->cq_timeouts
) + 1);
5694 io_cqring_fill_event(req
, -ETIME
);
5695 io_commit_cqring(ctx
);
5696 spin_unlock_irqrestore(&ctx
->completion_lock
, flags
);
5698 io_cqring_ev_posted(ctx
);
5699 req_set_fail_links(req
);
5701 return HRTIMER_NORESTART
;
5704 static struct io_kiocb
*io_timeout_extract(struct io_ring_ctx
*ctx
,
5707 struct io_timeout_data
*io
;
5708 struct io_kiocb
*req
;
5711 list_for_each_entry(req
, &ctx
->timeout_list
, timeout
.list
) {
5712 if (user_data
== req
->user_data
) {
5719 return ERR_PTR(ret
);
5721 io
= req
->async_data
;
5722 ret
= hrtimer_try_to_cancel(&io
->timer
);
5724 return ERR_PTR(-EALREADY
);
5725 list_del_init(&req
->timeout
.list
);
5729 static int io_timeout_cancel(struct io_ring_ctx
*ctx
, __u64 user_data
)
5731 struct io_kiocb
*req
= io_timeout_extract(ctx
, user_data
);
5734 return PTR_ERR(req
);
5736 req_set_fail_links(req
);
5737 io_cqring_fill_event(req
, -ECANCELED
);
5738 io_put_req_deferred(req
, 1);
5742 static int io_timeout_update(struct io_ring_ctx
*ctx
, __u64 user_data
,
5743 struct timespec64
*ts
, enum hrtimer_mode mode
)
5745 struct io_kiocb
*req
= io_timeout_extract(ctx
, user_data
);
5746 struct io_timeout_data
*data
;
5749 return PTR_ERR(req
);
5751 req
->timeout
.off
= 0; /* noseq */
5752 data
= req
->async_data
;
5753 list_add_tail(&req
->timeout
.list
, &ctx
->timeout_list
);
5754 hrtimer_init(&data
->timer
, CLOCK_MONOTONIC
, mode
);
5755 data
->timer
.function
= io_timeout_fn
;
5756 hrtimer_start(&data
->timer
, timespec64_to_ktime(*ts
), mode
);
5760 static int io_timeout_remove_prep(struct io_kiocb
*req
,
5761 const struct io_uring_sqe
*sqe
)
5763 struct io_timeout_rem
*tr
= &req
->timeout_rem
;
5765 if (unlikely(req
->ctx
->flags
& IORING_SETUP_IOPOLL
))
5767 if (unlikely(req
->flags
& (REQ_F_FIXED_FILE
| REQ_F_BUFFER_SELECT
)))
5769 if (sqe
->ioprio
|| sqe
->buf_index
|| sqe
->len
)
5772 tr
->addr
= READ_ONCE(sqe
->addr
);
5773 tr
->flags
= READ_ONCE(sqe
->timeout_flags
);
5774 if (tr
->flags
& IORING_TIMEOUT_UPDATE
) {
5775 if (tr
->flags
& ~(IORING_TIMEOUT_UPDATE
|IORING_TIMEOUT_ABS
))
5777 if (get_timespec64(&tr
->ts
, u64_to_user_ptr(sqe
->addr2
)))
5779 } else if (tr
->flags
) {
5780 /* timeout removal doesn't support flags */
5788 * Remove or update an existing timeout command
5790 static int io_timeout_remove(struct io_kiocb
*req
)
5792 struct io_timeout_rem
*tr
= &req
->timeout_rem
;
5793 struct io_ring_ctx
*ctx
= req
->ctx
;
5796 spin_lock_irq(&ctx
->completion_lock
);
5797 if (req
->timeout_rem
.flags
& IORING_TIMEOUT_UPDATE
) {
5798 enum hrtimer_mode mode
= (tr
->flags
& IORING_TIMEOUT_ABS
)
5799 ? HRTIMER_MODE_ABS
: HRTIMER_MODE_REL
;
5801 ret
= io_timeout_update(ctx
, tr
->addr
, &tr
->ts
, mode
);
5803 ret
= io_timeout_cancel(ctx
, tr
->addr
);
5806 io_cqring_fill_event(req
, ret
);
5807 io_commit_cqring(ctx
);
5808 spin_unlock_irq(&ctx
->completion_lock
);
5809 io_cqring_ev_posted(ctx
);
5811 req_set_fail_links(req
);
5816 static int io_timeout_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
,
5817 bool is_timeout_link
)
5819 struct io_timeout_data
*data
;
5821 u32 off
= READ_ONCE(sqe
->off
);
5823 if (unlikely(req
->ctx
->flags
& IORING_SETUP_IOPOLL
))
5825 if (sqe
->ioprio
|| sqe
->buf_index
|| sqe
->len
!= 1)
5827 if (off
&& is_timeout_link
)
5829 flags
= READ_ONCE(sqe
->timeout_flags
);
5830 if (flags
& ~IORING_TIMEOUT_ABS
)
5833 req
->timeout
.off
= off
;
5835 if (!req
->async_data
&& io_alloc_async_data(req
))
5838 data
= req
->async_data
;
5841 if (get_timespec64(&data
->ts
, u64_to_user_ptr(sqe
->addr
)))
5844 if (flags
& IORING_TIMEOUT_ABS
)
5845 data
->mode
= HRTIMER_MODE_ABS
;
5847 data
->mode
= HRTIMER_MODE_REL
;
5849 hrtimer_init(&data
->timer
, CLOCK_MONOTONIC
, data
->mode
);
5853 static int io_timeout(struct io_kiocb
*req
)
5855 struct io_ring_ctx
*ctx
= req
->ctx
;
5856 struct io_timeout_data
*data
= req
->async_data
;
5857 struct list_head
*entry
;
5858 u32 tail
, off
= req
->timeout
.off
;
5860 spin_lock_irq(&ctx
->completion_lock
);
5863 * sqe->off holds how many events that need to occur for this
5864 * timeout event to be satisfied. If it isn't set, then this is
5865 * a pure timeout request, sequence isn't used.
5867 if (io_is_timeout_noseq(req
)) {
5868 entry
= ctx
->timeout_list
.prev
;
5872 tail
= ctx
->cached_cq_tail
- atomic_read(&ctx
->cq_timeouts
);
5873 req
->timeout
.target_seq
= tail
+ off
;
5875 /* Update the last seq here in case io_flush_timeouts() hasn't.
5876 * This is safe because ->completion_lock is held, and submissions
5877 * and completions are never mixed in the same ->completion_lock section.
5879 ctx
->cq_last_tm_flush
= tail
;
5882 * Insertion sort, ensuring the first entry in the list is always
5883 * the one we need first.
5885 list_for_each_prev(entry
, &ctx
->timeout_list
) {
5886 struct io_kiocb
*nxt
= list_entry(entry
, struct io_kiocb
,
5889 if (io_is_timeout_noseq(nxt
))
5891 /* nxt.seq is behind @tail, otherwise would've been completed */
5892 if (off
>= nxt
->timeout
.target_seq
- tail
)
5896 list_add(&req
->timeout
.list
, entry
);
5897 data
->timer
.function
= io_timeout_fn
;
5898 hrtimer_start(&data
->timer
, timespec64_to_ktime(data
->ts
), data
->mode
);
5899 spin_unlock_irq(&ctx
->completion_lock
);
5903 static bool io_cancel_cb(struct io_wq_work
*work
, void *data
)
5905 struct io_kiocb
*req
= container_of(work
, struct io_kiocb
, work
);
5907 return req
->user_data
== (unsigned long) data
;
5910 static int io_async_cancel_one(struct io_ring_ctx
*ctx
, void *sqe_addr
)
5912 enum io_wq_cancel cancel_ret
;
5915 cancel_ret
= io_wq_cancel_cb(ctx
->io_wq
, io_cancel_cb
, sqe_addr
, false);
5916 switch (cancel_ret
) {
5917 case IO_WQ_CANCEL_OK
:
5920 case IO_WQ_CANCEL_RUNNING
:
5923 case IO_WQ_CANCEL_NOTFOUND
:
5931 static void io_async_find_and_cancel(struct io_ring_ctx
*ctx
,
5932 struct io_kiocb
*req
, __u64 sqe_addr
,
5935 unsigned long flags
;
5938 ret
= io_async_cancel_one(ctx
, (void *) (unsigned long) sqe_addr
);
5939 if (ret
!= -ENOENT
) {
5940 spin_lock_irqsave(&ctx
->completion_lock
, flags
);
5944 spin_lock_irqsave(&ctx
->completion_lock
, flags
);
5945 ret
= io_timeout_cancel(ctx
, sqe_addr
);
5948 ret
= io_poll_cancel(ctx
, sqe_addr
);
5952 io_cqring_fill_event(req
, ret
);
5953 io_commit_cqring(ctx
);
5954 spin_unlock_irqrestore(&ctx
->completion_lock
, flags
);
5955 io_cqring_ev_posted(ctx
);
5958 req_set_fail_links(req
);
5962 static int io_async_cancel_prep(struct io_kiocb
*req
,
5963 const struct io_uring_sqe
*sqe
)
5965 if (unlikely(req
->ctx
->flags
& IORING_SETUP_IOPOLL
))
5967 if (unlikely(req
->flags
& (REQ_F_FIXED_FILE
| REQ_F_BUFFER_SELECT
)))
5969 if (sqe
->ioprio
|| sqe
->off
|| sqe
->len
|| sqe
->cancel_flags
)
5972 req
->cancel
.addr
= READ_ONCE(sqe
->addr
);
5976 static int io_async_cancel(struct io_kiocb
*req
)
5978 struct io_ring_ctx
*ctx
= req
->ctx
;
5980 io_async_find_and_cancel(ctx
, req
, req
->cancel
.addr
, 0);
5984 static int io_files_update_prep(struct io_kiocb
*req
,
5985 const struct io_uring_sqe
*sqe
)
5987 if (unlikely(req
->ctx
->flags
& IORING_SETUP_SQPOLL
))
5989 if (unlikely(req
->flags
& (REQ_F_FIXED_FILE
| REQ_F_BUFFER_SELECT
)))
5991 if (sqe
->ioprio
|| sqe
->rw_flags
)
5994 req
->files_update
.offset
= READ_ONCE(sqe
->off
);
5995 req
->files_update
.nr_args
= READ_ONCE(sqe
->len
);
5996 if (!req
->files_update
.nr_args
)
5998 req
->files_update
.arg
= READ_ONCE(sqe
->addr
);
6002 static int io_files_update(struct io_kiocb
*req
, bool force_nonblock
,
6003 struct io_comp_state
*cs
)
6005 struct io_ring_ctx
*ctx
= req
->ctx
;
6006 struct io_uring_files_update up
;
6012 up
.offset
= req
->files_update
.offset
;
6013 up
.fds
= req
->files_update
.arg
;
6015 mutex_lock(&ctx
->uring_lock
);
6016 ret
= __io_sqe_files_update(ctx
, &up
, req
->files_update
.nr_args
);
6017 mutex_unlock(&ctx
->uring_lock
);
6020 req_set_fail_links(req
);
6021 __io_req_complete(req
, ret
, 0, cs
);
6025 static int io_req_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
6027 switch (req
->opcode
) {
6030 case IORING_OP_READV
:
6031 case IORING_OP_READ_FIXED
:
6032 case IORING_OP_READ
:
6033 return io_read_prep(req
, sqe
);
6034 case IORING_OP_WRITEV
:
6035 case IORING_OP_WRITE_FIXED
:
6036 case IORING_OP_WRITE
:
6037 return io_write_prep(req
, sqe
);
6038 case IORING_OP_POLL_ADD
:
6039 return io_poll_add_prep(req
, sqe
);
6040 case IORING_OP_POLL_REMOVE
:
6041 return io_poll_remove_prep(req
, sqe
);
6042 case IORING_OP_FSYNC
:
6043 return io_prep_fsync(req
, sqe
);
6044 case IORING_OP_SYNC_FILE_RANGE
:
6045 return io_prep_sfr(req
, sqe
);
6046 case IORING_OP_SENDMSG
:
6047 case IORING_OP_SEND
:
6048 return io_sendmsg_prep(req
, sqe
);
6049 case IORING_OP_RECVMSG
:
6050 case IORING_OP_RECV
:
6051 return io_recvmsg_prep(req
, sqe
);
6052 case IORING_OP_CONNECT
:
6053 return io_connect_prep(req
, sqe
);
6054 case IORING_OP_TIMEOUT
:
6055 return io_timeout_prep(req
, sqe
, false);
6056 case IORING_OP_TIMEOUT_REMOVE
:
6057 return io_timeout_remove_prep(req
, sqe
);
6058 case IORING_OP_ASYNC_CANCEL
:
6059 return io_async_cancel_prep(req
, sqe
);
6060 case IORING_OP_LINK_TIMEOUT
:
6061 return io_timeout_prep(req
, sqe
, true);
6062 case IORING_OP_ACCEPT
:
6063 return io_accept_prep(req
, sqe
);
6064 case IORING_OP_FALLOCATE
:
6065 return io_fallocate_prep(req
, sqe
);
6066 case IORING_OP_OPENAT
:
6067 return io_openat_prep(req
, sqe
);
6068 case IORING_OP_CLOSE
:
6069 return io_close_prep(req
, sqe
);
6070 case IORING_OP_FILES_UPDATE
:
6071 return io_files_update_prep(req
, sqe
);
6072 case IORING_OP_STATX
:
6073 return io_statx_prep(req
, sqe
);
6074 case IORING_OP_FADVISE
:
6075 return io_fadvise_prep(req
, sqe
);
6076 case IORING_OP_MADVISE
:
6077 return io_madvise_prep(req
, sqe
);
6078 case IORING_OP_OPENAT2
:
6079 return io_openat2_prep(req
, sqe
);
6080 case IORING_OP_EPOLL_CTL
:
6081 return io_epoll_ctl_prep(req
, sqe
);
6082 case IORING_OP_SPLICE
:
6083 return io_splice_prep(req
, sqe
);
6084 case IORING_OP_PROVIDE_BUFFERS
:
6085 return io_provide_buffers_prep(req
, sqe
);
6086 case IORING_OP_REMOVE_BUFFERS
:
6087 return io_remove_buffers_prep(req
, sqe
);
6089 return io_tee_prep(req
, sqe
);
6090 case IORING_OP_SHUTDOWN
:
6091 return io_shutdown_prep(req
, sqe
);
6092 case IORING_OP_RENAMEAT
:
6093 return io_renameat_prep(req
, sqe
);
6094 case IORING_OP_UNLINKAT
:
6095 return io_unlinkat_prep(req
, sqe
);
6098 printk_once(KERN_WARNING
"io_uring: unhandled opcode %d\n",
6103 static int io_req_defer_prep(struct io_kiocb
*req
,
6104 const struct io_uring_sqe
*sqe
)
6108 if (io_alloc_async_data(req
))
6110 return io_req_prep(req
, sqe
);
6113 static u32
io_get_sequence(struct io_kiocb
*req
)
6115 struct io_kiocb
*pos
;
6116 struct io_ring_ctx
*ctx
= req
->ctx
;
6117 u32 total_submitted
, nr_reqs
= 0;
6119 io_for_each_link(pos
, req
)
6122 total_submitted
= ctx
->cached_sq_head
- ctx
->cached_sq_dropped
;
6123 return total_submitted
- nr_reqs
;
6126 static int io_req_defer(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
6128 struct io_ring_ctx
*ctx
= req
->ctx
;
6129 struct io_defer_entry
*de
;
6133 /* Still need defer if there is pending req in defer list. */
6134 if (likely(list_empty_careful(&ctx
->defer_list
) &&
6135 !(req
->flags
& REQ_F_IO_DRAIN
)))
6138 seq
= io_get_sequence(req
);
6139 /* Still a chance to pass the sequence check */
6140 if (!req_need_defer(req
, seq
) && list_empty_careful(&ctx
->defer_list
))
6143 if (!req
->async_data
) {
6144 ret
= io_req_defer_prep(req
, sqe
);
6148 io_prep_async_link(req
);
6149 de
= kmalloc(sizeof(*de
), GFP_KERNEL
);
6153 spin_lock_irq(&ctx
->completion_lock
);
6154 if (!req_need_defer(req
, seq
) && list_empty(&ctx
->defer_list
)) {
6155 spin_unlock_irq(&ctx
->completion_lock
);
6157 io_queue_async_work(req
);
6158 return -EIOCBQUEUED
;
6161 trace_io_uring_defer(ctx
, req
, req
->user_data
);
6164 list_add_tail(&de
->list
, &ctx
->defer_list
);
6165 spin_unlock_irq(&ctx
->completion_lock
);
6166 return -EIOCBQUEUED
;
6169 static void io_req_drop_files(struct io_kiocb
*req
)
6171 struct io_ring_ctx
*ctx
= req
->ctx
;
6172 struct io_uring_task
*tctx
= req
->task
->io_uring
;
6173 unsigned long flags
;
6175 if (req
->work
.flags
& IO_WQ_WORK_FILES
) {
6176 put_files_struct(req
->work
.identity
->files
);
6177 put_nsproxy(req
->work
.identity
->nsproxy
);
6179 spin_lock_irqsave(&ctx
->inflight_lock
, flags
);
6180 list_del(&req
->inflight_entry
);
6181 spin_unlock_irqrestore(&ctx
->inflight_lock
, flags
);
6182 req
->flags
&= ~REQ_F_INFLIGHT
;
6183 req
->work
.flags
&= ~IO_WQ_WORK_FILES
;
6184 if (atomic_read(&tctx
->in_idle
))
6185 wake_up(&tctx
->wait
);
6188 static void __io_clean_op(struct io_kiocb
*req
)
6190 if (req
->flags
& REQ_F_BUFFER_SELECTED
) {
6191 switch (req
->opcode
) {
6192 case IORING_OP_READV
:
6193 case IORING_OP_READ_FIXED
:
6194 case IORING_OP_READ
:
6195 kfree((void *)(unsigned long)req
->rw
.addr
);
6197 case IORING_OP_RECVMSG
:
6198 case IORING_OP_RECV
:
6199 kfree(req
->sr_msg
.kbuf
);
6202 req
->flags
&= ~REQ_F_BUFFER_SELECTED
;
6205 if (req
->flags
& REQ_F_NEED_CLEANUP
) {
6206 switch (req
->opcode
) {
6207 case IORING_OP_READV
:
6208 case IORING_OP_READ_FIXED
:
6209 case IORING_OP_READ
:
6210 case IORING_OP_WRITEV
:
6211 case IORING_OP_WRITE_FIXED
:
6212 case IORING_OP_WRITE
: {
6213 struct io_async_rw
*io
= req
->async_data
;
6215 kfree(io
->free_iovec
);
6218 case IORING_OP_RECVMSG
:
6219 case IORING_OP_SENDMSG
: {
6220 struct io_async_msghdr
*io
= req
->async_data
;
6221 if (io
->iov
!= io
->fast_iov
)
6225 case IORING_OP_SPLICE
:
6227 io_put_file(req
, req
->splice
.file_in
,
6228 (req
->splice
.flags
& SPLICE_F_FD_IN_FIXED
));
6230 case IORING_OP_OPENAT
:
6231 case IORING_OP_OPENAT2
:
6232 if (req
->open
.filename
)
6233 putname(req
->open
.filename
);
6235 case IORING_OP_RENAMEAT
:
6236 putname(req
->rename
.oldpath
);
6237 putname(req
->rename
.newpath
);
6239 case IORING_OP_UNLINKAT
:
6240 putname(req
->unlink
.filename
);
6243 req
->flags
&= ~REQ_F_NEED_CLEANUP
;
6247 static int io_issue_sqe(struct io_kiocb
*req
, bool force_nonblock
,
6248 struct io_comp_state
*cs
)
6250 struct io_ring_ctx
*ctx
= req
->ctx
;
6253 switch (req
->opcode
) {
6255 ret
= io_nop(req
, cs
);
6257 case IORING_OP_READV
:
6258 case IORING_OP_READ_FIXED
:
6259 case IORING_OP_READ
:
6260 ret
= io_read(req
, force_nonblock
, cs
);
6262 case IORING_OP_WRITEV
:
6263 case IORING_OP_WRITE_FIXED
:
6264 case IORING_OP_WRITE
:
6265 ret
= io_write(req
, force_nonblock
, cs
);
6267 case IORING_OP_FSYNC
:
6268 ret
= io_fsync(req
, force_nonblock
);
6270 case IORING_OP_POLL_ADD
:
6271 ret
= io_poll_add(req
);
6273 case IORING_OP_POLL_REMOVE
:
6274 ret
= io_poll_remove(req
);
6276 case IORING_OP_SYNC_FILE_RANGE
:
6277 ret
= io_sync_file_range(req
, force_nonblock
);
6279 case IORING_OP_SENDMSG
:
6280 ret
= io_sendmsg(req
, force_nonblock
, cs
);
6282 case IORING_OP_SEND
:
6283 ret
= io_send(req
, force_nonblock
, cs
);
6285 case IORING_OP_RECVMSG
:
6286 ret
= io_recvmsg(req
, force_nonblock
, cs
);
6288 case IORING_OP_RECV
:
6289 ret
= io_recv(req
, force_nonblock
, cs
);
6291 case IORING_OP_TIMEOUT
:
6292 ret
= io_timeout(req
);
6294 case IORING_OP_TIMEOUT_REMOVE
:
6295 ret
= io_timeout_remove(req
);
6297 case IORING_OP_ACCEPT
:
6298 ret
= io_accept(req
, force_nonblock
, cs
);
6300 case IORING_OP_CONNECT
:
6301 ret
= io_connect(req
, force_nonblock
, cs
);
6303 case IORING_OP_ASYNC_CANCEL
:
6304 ret
= io_async_cancel(req
);
6306 case IORING_OP_FALLOCATE
:
6307 ret
= io_fallocate(req
, force_nonblock
);
6309 case IORING_OP_OPENAT
:
6310 ret
= io_openat(req
, force_nonblock
);
6312 case IORING_OP_CLOSE
:
6313 ret
= io_close(req
, force_nonblock
, cs
);
6315 case IORING_OP_FILES_UPDATE
:
6316 ret
= io_files_update(req
, force_nonblock
, cs
);
6318 case IORING_OP_STATX
:
6319 ret
= io_statx(req
, force_nonblock
);
6321 case IORING_OP_FADVISE
:
6322 ret
= io_fadvise(req
, force_nonblock
);
6324 case IORING_OP_MADVISE
:
6325 ret
= io_madvise(req
, force_nonblock
);
6327 case IORING_OP_OPENAT2
:
6328 ret
= io_openat2(req
, force_nonblock
);
6330 case IORING_OP_EPOLL_CTL
:
6331 ret
= io_epoll_ctl(req
, force_nonblock
, cs
);
6333 case IORING_OP_SPLICE
:
6334 ret
= io_splice(req
, force_nonblock
);
6336 case IORING_OP_PROVIDE_BUFFERS
:
6337 ret
= io_provide_buffers(req
, force_nonblock
, cs
);
6339 case IORING_OP_REMOVE_BUFFERS
:
6340 ret
= io_remove_buffers(req
, force_nonblock
, cs
);
6343 ret
= io_tee(req
, force_nonblock
);
6345 case IORING_OP_SHUTDOWN
:
6346 ret
= io_shutdown(req
, force_nonblock
);
6348 case IORING_OP_RENAMEAT
:
6349 ret
= io_renameat(req
, force_nonblock
);
6351 case IORING_OP_UNLINKAT
:
6352 ret
= io_unlinkat(req
, force_nonblock
);
6362 /* If the op doesn't have a file, we're not polling for it */
6363 if ((ctx
->flags
& IORING_SETUP_IOPOLL
) && req
->file
) {
6364 const bool in_async
= io_wq_current_is_worker();
6366 /* workqueue context doesn't hold uring_lock, grab it now */
6368 mutex_lock(&ctx
->uring_lock
);
6370 io_iopoll_req_issued(req
, in_async
);
6373 mutex_unlock(&ctx
->uring_lock
);
6379 static struct io_wq_work
*io_wq_submit_work(struct io_wq_work
*work
)
6381 struct io_kiocb
*req
= container_of(work
, struct io_kiocb
, work
);
6382 struct io_kiocb
*timeout
;
6385 timeout
= io_prep_linked_timeout(req
);
6387 io_queue_linked_timeout(timeout
);
6389 /* if NO_CANCEL is set, we must still run the work */
6390 if ((work
->flags
& (IO_WQ_WORK_CANCEL
|IO_WQ_WORK_NO_CANCEL
)) ==
6391 IO_WQ_WORK_CANCEL
) {
6397 ret
= io_issue_sqe(req
, false, NULL
);
6399 * We can get EAGAIN for polled IO even though we're
6400 * forcing a sync submission from here, since we can't
6401 * wait for request slots on the block side.
6410 struct io_ring_ctx
*lock_ctx
= NULL
;
6412 if (req
->ctx
->flags
& IORING_SETUP_IOPOLL
)
6413 lock_ctx
= req
->ctx
;
6416 * io_iopoll_complete() does not hold completion_lock to
6417 * complete polled io, so here for polled io, we can not call
6418 * io_req_complete() directly, otherwise there maybe concurrent
6419 * access to cqring, defer_list, etc, which is not safe. Given
6420 * that io_iopoll_complete() is always called under uring_lock,
6421 * so here for polled io, we also get uring_lock to complete
6425 mutex_lock(&lock_ctx
->uring_lock
);
6427 req_set_fail_links(req
);
6428 io_req_complete(req
, ret
);
6431 mutex_unlock(&lock_ctx
->uring_lock
);
6434 return io_steal_work(req
);
6437 static inline struct file
*io_file_from_index(struct io_ring_ctx
*ctx
,
6440 struct fixed_file_table
*table
;
6442 table
= &ctx
->file_data
->table
[index
>> IORING_FILE_TABLE_SHIFT
];
6443 return table
->files
[index
& IORING_FILE_TABLE_MASK
];
6446 static struct file
*io_file_get(struct io_submit_state
*state
,
6447 struct io_kiocb
*req
, int fd
, bool fixed
)
6449 struct io_ring_ctx
*ctx
= req
->ctx
;
6453 if (unlikely((unsigned int)fd
>= ctx
->nr_user_files
))
6455 fd
= array_index_nospec(fd
, ctx
->nr_user_files
);
6456 file
= io_file_from_index(ctx
, fd
);
6457 io_set_resource_node(req
);
6459 trace_io_uring_file_get(ctx
, fd
);
6460 file
= __io_file_get(state
, fd
);
6463 if (file
&& file
->f_op
== &io_uring_fops
&&
6464 !(req
->flags
& REQ_F_INFLIGHT
)) {
6465 io_req_init_async(req
);
6466 req
->flags
|= REQ_F_INFLIGHT
;
6468 spin_lock_irq(&ctx
->inflight_lock
);
6469 list_add(&req
->inflight_entry
, &ctx
->inflight_list
);
6470 spin_unlock_irq(&ctx
->inflight_lock
);
6476 static enum hrtimer_restart
io_link_timeout_fn(struct hrtimer
*timer
)
6478 struct io_timeout_data
*data
= container_of(timer
,
6479 struct io_timeout_data
, timer
);
6480 struct io_kiocb
*prev
, *req
= data
->req
;
6481 struct io_ring_ctx
*ctx
= req
->ctx
;
6482 unsigned long flags
;
6484 spin_lock_irqsave(&ctx
->completion_lock
, flags
);
6485 prev
= req
->timeout
.head
;
6486 req
->timeout
.head
= NULL
;
6489 * We don't expect the list to be empty, that will only happen if we
6490 * race with the completion of the linked work.
6492 if (prev
&& refcount_inc_not_zero(&prev
->refs
))
6493 io_remove_next_linked(prev
);
6496 spin_unlock_irqrestore(&ctx
->completion_lock
, flags
);
6499 req_set_fail_links(prev
);
6500 io_async_find_and_cancel(ctx
, req
, prev
->user_data
, -ETIME
);
6503 io_req_complete(req
, -ETIME
);
6505 return HRTIMER_NORESTART
;
6508 static void __io_queue_linked_timeout(struct io_kiocb
*req
)
6511 * If the back reference is NULL, then our linked request finished
6512 * before we got a chance to setup the timer
6514 if (req
->timeout
.head
) {
6515 struct io_timeout_data
*data
= req
->async_data
;
6517 data
->timer
.function
= io_link_timeout_fn
;
6518 hrtimer_start(&data
->timer
, timespec64_to_ktime(data
->ts
),
6523 static void io_queue_linked_timeout(struct io_kiocb
*req
)
6525 struct io_ring_ctx
*ctx
= req
->ctx
;
6527 spin_lock_irq(&ctx
->completion_lock
);
6528 __io_queue_linked_timeout(req
);
6529 spin_unlock_irq(&ctx
->completion_lock
);
6531 /* drop submission reference */
6535 static struct io_kiocb
*io_prep_linked_timeout(struct io_kiocb
*req
)
6537 struct io_kiocb
*nxt
= req
->link
;
6539 if (!nxt
|| (req
->flags
& REQ_F_LINK_TIMEOUT
) ||
6540 nxt
->opcode
!= IORING_OP_LINK_TIMEOUT
)
6543 nxt
->timeout
.head
= req
;
6544 nxt
->flags
|= REQ_F_LTIMEOUT_ACTIVE
;
6545 req
->flags
|= REQ_F_LINK_TIMEOUT
;
6549 static void __io_queue_sqe(struct io_kiocb
*req
, struct io_comp_state
*cs
)
6551 struct io_kiocb
*linked_timeout
;
6552 const struct cred
*old_creds
= NULL
;
6556 linked_timeout
= io_prep_linked_timeout(req
);
6558 if ((req
->flags
& REQ_F_WORK_INITIALIZED
) &&
6559 (req
->work
.flags
& IO_WQ_WORK_CREDS
) &&
6560 req
->work
.identity
->creds
!= current_cred()) {
6562 revert_creds(old_creds
);
6563 if (old_creds
== req
->work
.identity
->creds
)
6564 old_creds
= NULL
; /* restored original creds */
6566 old_creds
= override_creds(req
->work
.identity
->creds
);
6569 ret
= io_issue_sqe(req
, true, cs
);
6572 * We async punt it if the file wasn't marked NOWAIT, or if the file
6573 * doesn't support non-blocking read/write attempts
6575 if (ret
== -EAGAIN
&& !(req
->flags
& REQ_F_NOWAIT
)) {
6576 if (!io_arm_poll_handler(req
)) {
6578 * Queued up for async execution, worker will release
6579 * submit reference when the iocb is actually submitted.
6581 io_queue_async_work(req
);
6585 io_queue_linked_timeout(linked_timeout
);
6586 } else if (likely(!ret
)) {
6587 /* drop submission reference */
6588 req
= io_put_req_find_next(req
);
6590 io_queue_linked_timeout(linked_timeout
);
6593 if (!(req
->flags
& REQ_F_FORCE_ASYNC
))
6595 io_queue_async_work(req
);
6598 /* un-prep timeout, so it'll be killed as any other linked */
6599 req
->flags
&= ~REQ_F_LINK_TIMEOUT
;
6600 req_set_fail_links(req
);
6602 io_req_complete(req
, ret
);
6606 revert_creds(old_creds
);
6609 static void io_queue_sqe(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
,
6610 struct io_comp_state
*cs
)
6614 ret
= io_req_defer(req
, sqe
);
6616 if (ret
!= -EIOCBQUEUED
) {
6618 req_set_fail_links(req
);
6620 io_req_complete(req
, ret
);
6622 } else if (req
->flags
& REQ_F_FORCE_ASYNC
) {
6623 if (!req
->async_data
) {
6624 ret
= io_req_defer_prep(req
, sqe
);
6628 io_queue_async_work(req
);
6631 ret
= io_req_prep(req
, sqe
);
6635 __io_queue_sqe(req
, cs
);
6639 static inline void io_queue_link_head(struct io_kiocb
*req
,
6640 struct io_comp_state
*cs
)
6642 if (unlikely(req
->flags
& REQ_F_FAIL_LINK
)) {
6644 io_req_complete(req
, -ECANCELED
);
6646 io_queue_sqe(req
, NULL
, cs
);
6649 struct io_submit_link
{
6650 struct io_kiocb
*head
;
6651 struct io_kiocb
*last
;
6654 static int io_submit_sqe(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
,
6655 struct io_submit_link
*link
, struct io_comp_state
*cs
)
6657 struct io_ring_ctx
*ctx
= req
->ctx
;
6661 * If we already have a head request, queue this one for async
6662 * submittal once the head completes. If we don't have a head but
6663 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6664 * submitted sync once the chain is complete. If none of those
6665 * conditions are true (normal request), then just queue it.
6668 struct io_kiocb
*head
= link
->head
;
6671 * Taking sequential execution of a link, draining both sides
6672 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6673 * requests in the link. So, it drains the head and the
6674 * next after the link request. The last one is done via
6675 * drain_next flag to persist the effect across calls.
6677 if (req
->flags
& REQ_F_IO_DRAIN
) {
6678 head
->flags
|= REQ_F_IO_DRAIN
;
6679 ctx
->drain_next
= 1;
6681 ret
= io_req_defer_prep(req
, sqe
);
6682 if (unlikely(ret
)) {
6683 /* fail even hard links since we don't submit */
6684 head
->flags
|= REQ_F_FAIL_LINK
;
6687 trace_io_uring_link(ctx
, req
, head
);
6688 link
->last
->link
= req
;
6691 /* last request of a link, enqueue the link */
6692 if (!(req
->flags
& (REQ_F_LINK
| REQ_F_HARDLINK
))) {
6693 io_queue_link_head(head
, cs
);
6697 if (unlikely(ctx
->drain_next
)) {
6698 req
->flags
|= REQ_F_IO_DRAIN
;
6699 ctx
->drain_next
= 0;
6701 if (req
->flags
& (REQ_F_LINK
| REQ_F_HARDLINK
)) {
6702 ret
= io_req_defer_prep(req
, sqe
);
6704 req
->flags
|= REQ_F_FAIL_LINK
;
6708 io_queue_sqe(req
, sqe
, cs
);
6716 * Batched submission is done, ensure local IO is flushed out.
6718 static void io_submit_state_end(struct io_submit_state
*state
)
6720 if (!list_empty(&state
->comp
.list
))
6721 io_submit_flush_completions(&state
->comp
);
6722 if (state
->plug_started
)
6723 blk_finish_plug(&state
->plug
);
6724 io_state_file_put(state
);
6725 if (state
->free_reqs
)
6726 kmem_cache_free_bulk(req_cachep
, state
->free_reqs
, state
->reqs
);
6730 * Start submission side cache.
6732 static void io_submit_state_start(struct io_submit_state
*state
,
6733 struct io_ring_ctx
*ctx
, unsigned int max_ios
)
6735 state
->plug_started
= false;
6737 INIT_LIST_HEAD(&state
->comp
.list
);
6738 state
->comp
.ctx
= ctx
;
6739 state
->free_reqs
= 0;
6740 state
->file_refs
= 0;
6741 state
->ios_left
= max_ios
;
6744 static void io_commit_sqring(struct io_ring_ctx
*ctx
)
6746 struct io_rings
*rings
= ctx
->rings
;
6749 * Ensure any loads from the SQEs are done at this point,
6750 * since once we write the new head, the application could
6751 * write new data to them.
6753 smp_store_release(&rings
->sq
.head
, ctx
->cached_sq_head
);
6757 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
6758 * that is mapped by userspace. This means that care needs to be taken to
6759 * ensure that reads are stable, as we cannot rely on userspace always
6760 * being a good citizen. If members of the sqe are validated and then later
6761 * used, it's important that those reads are done through READ_ONCE() to
6762 * prevent a re-load down the line.
6764 static const struct io_uring_sqe
*io_get_sqe(struct io_ring_ctx
*ctx
)
6766 u32
*sq_array
= ctx
->sq_array
;
6770 * The cached sq head (or cq tail) serves two purposes:
6772 * 1) allows us to batch the cost of updating the user visible
6774 * 2) allows the kernel side to track the head on its own, even
6775 * though the application is the one updating it.
6777 head
= READ_ONCE(sq_array
[ctx
->cached_sq_head
& ctx
->sq_mask
]);
6778 if (likely(head
< ctx
->sq_entries
))
6779 return &ctx
->sq_sqes
[head
];
6781 /* drop invalid entries */
6782 ctx
->cached_sq_dropped
++;
6783 WRITE_ONCE(ctx
->rings
->sq_dropped
, ctx
->cached_sq_dropped
);
6787 static inline void io_consume_sqe(struct io_ring_ctx
*ctx
)
6789 ctx
->cached_sq_head
++;
6793 * Check SQE restrictions (opcode and flags).
6795 * Returns 'true' if SQE is allowed, 'false' otherwise.
6797 static inline bool io_check_restriction(struct io_ring_ctx
*ctx
,
6798 struct io_kiocb
*req
,
6799 unsigned int sqe_flags
)
6801 if (!ctx
->restricted
)
6804 if (!test_bit(req
->opcode
, ctx
->restrictions
.sqe_op
))
6807 if ((sqe_flags
& ctx
->restrictions
.sqe_flags_required
) !=
6808 ctx
->restrictions
.sqe_flags_required
)
6811 if (sqe_flags
& ~(ctx
->restrictions
.sqe_flags_allowed
|
6812 ctx
->restrictions
.sqe_flags_required
))
6818 #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6819 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6820 IOSQE_BUFFER_SELECT)
6822 static int io_init_req(struct io_ring_ctx
*ctx
, struct io_kiocb
*req
,
6823 const struct io_uring_sqe
*sqe
,
6824 struct io_submit_state
*state
)
6826 unsigned int sqe_flags
;
6829 req
->opcode
= READ_ONCE(sqe
->opcode
);
6830 req
->user_data
= READ_ONCE(sqe
->user_data
);
6831 req
->async_data
= NULL
;
6836 req
->fixed_file_refs
= NULL
;
6837 /* one is dropped after submission, the other at completion */
6838 refcount_set(&req
->refs
, 2);
6839 req
->task
= current
;
6842 if (unlikely(req
->opcode
>= IORING_OP_LAST
))
6845 if (unlikely(io_sq_thread_acquire_mm_files(ctx
, req
)))
6848 sqe_flags
= READ_ONCE(sqe
->flags
);
6849 /* enforce forwards compatibility on users */
6850 if (unlikely(sqe_flags
& ~SQE_VALID_FLAGS
))
6853 if (unlikely(!io_check_restriction(ctx
, req
, sqe_flags
)))
6856 if ((sqe_flags
& IOSQE_BUFFER_SELECT
) &&
6857 !io_op_defs
[req
->opcode
].buffer_select
)
6860 id
= READ_ONCE(sqe
->personality
);
6862 struct io_identity
*iod
;
6864 iod
= idr_find(&ctx
->personality_idr
, id
);
6867 refcount_inc(&iod
->count
);
6869 __io_req_init_async(req
);
6870 get_cred(iod
->creds
);
6871 req
->work
.identity
= iod
;
6872 req
->work
.flags
|= IO_WQ_WORK_CREDS
;
6875 /* same numerical values with corresponding REQ_F_*, safe to copy */
6876 req
->flags
|= sqe_flags
;
6879 * Plug now if we have more than 1 IO left after this, and the target
6880 * is potentially a read/write to block based storage.
6882 if (!state
->plug_started
&& state
->ios_left
> 1 &&
6883 io_op_defs
[req
->opcode
].plug
) {
6884 blk_start_plug(&state
->plug
);
6885 state
->plug_started
= true;
6889 if (io_op_defs
[req
->opcode
].needs_file
) {
6890 bool fixed
= req
->flags
& REQ_F_FIXED_FILE
;
6892 req
->file
= io_file_get(state
, req
, READ_ONCE(sqe
->fd
), fixed
);
6893 if (unlikely(!req
->file
&&
6894 !io_op_defs
[req
->opcode
].needs_file_no_error
))
6902 static int io_submit_sqes(struct io_ring_ctx
*ctx
, unsigned int nr
)
6904 struct io_submit_state state
;
6905 struct io_submit_link link
;
6906 int i
, submitted
= 0;
6908 /* if we have a backlog and couldn't flush it all, return BUSY */
6909 if (test_bit(0, &ctx
->sq_check_overflow
)) {
6910 if (!__io_cqring_overflow_flush(ctx
, false, NULL
, NULL
))
6914 /* make sure SQ entry isn't read before tail */
6915 nr
= min3(nr
, ctx
->sq_entries
, io_sqring_entries(ctx
));
6917 if (!percpu_ref_tryget_many(&ctx
->refs
, nr
))
6920 percpu_counter_add(¤t
->io_uring
->inflight
, nr
);
6921 refcount_add(nr
, ¤t
->usage
);
6923 io_submit_state_start(&state
, ctx
, nr
);
6926 for (i
= 0; i
< nr
; i
++) {
6927 const struct io_uring_sqe
*sqe
;
6928 struct io_kiocb
*req
;
6931 sqe
= io_get_sqe(ctx
);
6932 if (unlikely(!sqe
)) {
6933 io_consume_sqe(ctx
);
6936 req
= io_alloc_req(ctx
, &state
);
6937 if (unlikely(!req
)) {
6939 submitted
= -EAGAIN
;
6942 io_consume_sqe(ctx
);
6943 /* will complete beyond this point, count as submitted */
6946 err
= io_init_req(ctx
, req
, sqe
, &state
);
6947 if (unlikely(err
)) {
6950 io_req_complete(req
, err
);
6954 trace_io_uring_submit_sqe(ctx
, req
->opcode
, req
->user_data
,
6955 true, io_async_submit(ctx
));
6956 err
= io_submit_sqe(req
, sqe
, &link
, &state
.comp
);
6961 if (unlikely(submitted
!= nr
)) {
6962 int ref_used
= (submitted
== -EAGAIN
) ? 0 : submitted
;
6963 struct io_uring_task
*tctx
= current
->io_uring
;
6964 int unused
= nr
- ref_used
;
6966 percpu_ref_put_many(&ctx
->refs
, unused
);
6967 percpu_counter_sub(&tctx
->inflight
, unused
);
6968 put_task_struct_many(current
, unused
);
6971 io_queue_link_head(link
.head
, &state
.comp
);
6972 io_submit_state_end(&state
);
6974 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6975 io_commit_sqring(ctx
);
6980 static inline void io_ring_set_wakeup_flag(struct io_ring_ctx
*ctx
)
6982 /* Tell userspace we may need a wakeup call */
6983 spin_lock_irq(&ctx
->completion_lock
);
6984 ctx
->rings
->sq_flags
|= IORING_SQ_NEED_WAKEUP
;
6985 spin_unlock_irq(&ctx
->completion_lock
);
6988 static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx
*ctx
)
6990 spin_lock_irq(&ctx
->completion_lock
);
6991 ctx
->rings
->sq_flags
&= ~IORING_SQ_NEED_WAKEUP
;
6992 spin_unlock_irq(&ctx
->completion_lock
);
6995 static int __io_sq_thread(struct io_ring_ctx
*ctx
, bool cap_entries
)
6997 unsigned int to_submit
;
7000 to_submit
= io_sqring_entries(ctx
);
7001 /* if we're handling multiple rings, cap submit size for fairness */
7002 if (cap_entries
&& to_submit
> 8)
7005 if (!list_empty(&ctx
->iopoll_list
) || to_submit
) {
7006 unsigned nr_events
= 0;
7008 mutex_lock(&ctx
->uring_lock
);
7009 if (!list_empty(&ctx
->iopoll_list
))
7010 io_do_iopoll(ctx
, &nr_events
, 0);
7012 if (to_submit
&& !ctx
->sqo_dead
&&
7013 likely(!percpu_ref_is_dying(&ctx
->refs
)))
7014 ret
= io_submit_sqes(ctx
, to_submit
);
7015 mutex_unlock(&ctx
->uring_lock
);
7018 if (!io_sqring_full(ctx
) && wq_has_sleeper(&ctx
->sqo_sq_wait
))
7019 wake_up(&ctx
->sqo_sq_wait
);
7024 static void io_sqd_update_thread_idle(struct io_sq_data
*sqd
)
7026 struct io_ring_ctx
*ctx
;
7027 unsigned sq_thread_idle
= 0;
7029 list_for_each_entry(ctx
, &sqd
->ctx_list
, sqd_list
) {
7030 if (sq_thread_idle
< ctx
->sq_thread_idle
)
7031 sq_thread_idle
= ctx
->sq_thread_idle
;
7034 sqd
->sq_thread_idle
= sq_thread_idle
;
7037 static void io_sqd_init_new(struct io_sq_data
*sqd
)
7039 struct io_ring_ctx
*ctx
;
7041 while (!list_empty(&sqd
->ctx_new_list
)) {
7042 ctx
= list_first_entry(&sqd
->ctx_new_list
, struct io_ring_ctx
, sqd_list
);
7043 list_move_tail(&ctx
->sqd_list
, &sqd
->ctx_list
);
7044 complete(&ctx
->sq_thread_comp
);
7047 io_sqd_update_thread_idle(sqd
);
7050 static int io_sq_thread(void *data
)
7052 struct cgroup_subsys_state
*cur_css
= NULL
;
7053 struct files_struct
*old_files
= current
->files
;
7054 struct nsproxy
*old_nsproxy
= current
->nsproxy
;
7055 const struct cred
*old_cred
= NULL
;
7056 struct io_sq_data
*sqd
= data
;
7057 struct io_ring_ctx
*ctx
;
7058 unsigned long timeout
= 0;
7062 current
->files
= NULL
;
7063 current
->nsproxy
= NULL
;
7064 task_unlock(current
);
7066 while (!kthread_should_stop()) {
7068 bool cap_entries
, sqt_spin
, needs_sched
;
7071 * Any changes to the sqd lists are synchronized through the
7072 * kthread parking. This synchronizes the thread vs users,
7073 * the users are synchronized on the sqd->ctx_lock.
7075 if (kthread_should_park()) {
7078 * When sq thread is unparked, in case the previous park operation
7079 * comes from io_put_sq_data(), which means that sq thread is going
7080 * to be stopped, so here needs to have a check.
7082 if (kthread_should_stop())
7086 if (unlikely(!list_empty(&sqd
->ctx_new_list
))) {
7087 io_sqd_init_new(sqd
);
7088 timeout
= jiffies
+ sqd
->sq_thread_idle
;
7092 cap_entries
= !list_is_singular(&sqd
->ctx_list
);
7093 list_for_each_entry(ctx
, &sqd
->ctx_list
, sqd_list
) {
7094 if (current
->cred
!= ctx
->creds
) {
7096 revert_creds(old_cred
);
7097 old_cred
= override_creds(ctx
->creds
);
7099 io_sq_thread_associate_blkcg(ctx
, &cur_css
);
7101 current
->loginuid
= ctx
->loginuid
;
7102 current
->sessionid
= ctx
->sessionid
;
7105 ret
= __io_sq_thread(ctx
, cap_entries
);
7106 if (!sqt_spin
&& (ret
> 0 || !list_empty(&ctx
->iopoll_list
)))
7109 io_sq_thread_drop_mm_files();
7112 if (sqt_spin
|| !time_after(jiffies
, timeout
)) {
7114 io_sq_thread_drop_mm_files();
7117 timeout
= jiffies
+ sqd
->sq_thread_idle
;
7121 if (kthread_should_park())
7125 prepare_to_wait(&sqd
->wait
, &wait
, TASK_INTERRUPTIBLE
);
7126 list_for_each_entry(ctx
, &sqd
->ctx_list
, sqd_list
) {
7127 if ((ctx
->flags
& IORING_SETUP_IOPOLL
) &&
7128 !list_empty_careful(&ctx
->iopoll_list
)) {
7129 needs_sched
= false;
7132 if (io_sqring_entries(ctx
)) {
7133 needs_sched
= false;
7139 list_for_each_entry(ctx
, &sqd
->ctx_list
, sqd_list
)
7140 io_ring_set_wakeup_flag(ctx
);
7143 list_for_each_entry(ctx
, &sqd
->ctx_list
, sqd_list
)
7144 io_ring_clear_wakeup_flag(ctx
);
7147 finish_wait(&sqd
->wait
, &wait
);
7148 timeout
= jiffies
+ sqd
->sq_thread_idle
;
7152 io_sq_thread_drop_mm_files();
7155 io_sq_thread_unassociate_blkcg();
7157 revert_creds(old_cred
);
7160 current
->files
= old_files
;
7161 current
->nsproxy
= old_nsproxy
;
7162 task_unlock(current
);
7169 struct io_wait_queue
{
7170 struct wait_queue_entry wq
;
7171 struct io_ring_ctx
*ctx
;
7173 unsigned nr_timeouts
;
7176 static inline bool io_should_wake(struct io_wait_queue
*iowq
)
7178 struct io_ring_ctx
*ctx
= iowq
->ctx
;
7181 * Wake up if we have enough events, or if a timeout occurred since we
7182 * started waiting. For timeouts, we always want to return to userspace,
7183 * regardless of event count.
7185 return io_cqring_events(ctx
) >= iowq
->to_wait
||
7186 atomic_read(&ctx
->cq_timeouts
) != iowq
->nr_timeouts
;
7189 static int io_wake_function(struct wait_queue_entry
*curr
, unsigned int mode
,
7190 int wake_flags
, void *key
)
7192 struct io_wait_queue
*iowq
= container_of(curr
, struct io_wait_queue
,
7196 * Cannot safely flush overflowed CQEs from here, ensure we wake up
7197 * the task, and the next invocation will do it.
7199 if (io_should_wake(iowq
) || test_bit(0, &iowq
->ctx
->cq_check_overflow
))
7200 return autoremove_wake_function(curr
, mode
, wake_flags
, key
);
7204 static int io_run_task_work_sig(void)
7206 if (io_run_task_work())
7208 if (!signal_pending(current
))
7210 if (test_tsk_thread_flag(current
, TIF_NOTIFY_SIGNAL
))
7211 return -ERESTARTSYS
;
7216 * Wait until events become available, if we don't already have some. The
7217 * application must reap them itself, as they reside on the shared cq ring.
7219 static int io_cqring_wait(struct io_ring_ctx
*ctx
, int min_events
,
7220 const sigset_t __user
*sig
, size_t sigsz
,
7221 struct __kernel_timespec __user
*uts
)
7223 struct io_wait_queue iowq
= {
7226 .func
= io_wake_function
,
7227 .entry
= LIST_HEAD_INIT(iowq
.wq
.entry
),
7230 .to_wait
= min_events
,
7232 struct io_rings
*rings
= ctx
->rings
;
7233 struct timespec64 ts
;
7234 signed long timeout
= 0;
7238 io_cqring_overflow_flush(ctx
, false, NULL
, NULL
);
7239 if (io_cqring_events(ctx
) >= min_events
)
7241 if (!io_run_task_work())
7246 #ifdef CONFIG_COMPAT
7247 if (in_compat_syscall())
7248 ret
= set_compat_user_sigmask((const compat_sigset_t __user
*)sig
,
7252 ret
= set_user_sigmask(sig
, sigsz
);
7259 if (get_timespec64(&ts
, uts
))
7261 timeout
= timespec64_to_jiffies(&ts
);
7264 iowq
.nr_timeouts
= atomic_read(&ctx
->cq_timeouts
);
7265 trace_io_uring_cqring_wait(ctx
, min_events
);
7267 io_cqring_overflow_flush(ctx
, false, NULL
, NULL
);
7268 prepare_to_wait_exclusive(&ctx
->wait
, &iowq
.wq
,
7269 TASK_INTERRUPTIBLE
);
7270 /* make sure we run task_work before checking for signals */
7271 ret
= io_run_task_work_sig();
7273 finish_wait(&ctx
->wait
, &iowq
.wq
);
7278 if (io_should_wake(&iowq
))
7280 if (test_bit(0, &ctx
->cq_check_overflow
)) {
7281 finish_wait(&ctx
->wait
, &iowq
.wq
);
7285 timeout
= schedule_timeout(timeout
);
7294 finish_wait(&ctx
->wait
, &iowq
.wq
);
7296 restore_saved_sigmask_unless(ret
== -EINTR
);
7298 return READ_ONCE(rings
->cq
.head
) == READ_ONCE(rings
->cq
.tail
) ? ret
: 0;
7301 static void __io_sqe_files_unregister(struct io_ring_ctx
*ctx
)
7303 #if defined(CONFIG_UNIX)
7304 if (ctx
->ring_sock
) {
7305 struct sock
*sock
= ctx
->ring_sock
->sk
;
7306 struct sk_buff
*skb
;
7308 while ((skb
= skb_dequeue(&sock
->sk_receive_queue
)) != NULL
)
7314 for (i
= 0; i
< ctx
->nr_user_files
; i
++) {
7317 file
= io_file_from_index(ctx
, i
);
7324 static void io_file_ref_kill(struct percpu_ref
*ref
)
7326 struct fixed_file_data
*data
;
7328 data
= container_of(ref
, struct fixed_file_data
, refs
);
7329 complete(&data
->done
);
7332 static void io_sqe_files_set_node(struct fixed_file_data
*file_data
,
7333 struct fixed_file_ref_node
*ref_node
)
7335 spin_lock_bh(&file_data
->lock
);
7336 file_data
->node
= ref_node
;
7337 list_add_tail(&ref_node
->node
, &file_data
->ref_list
);
7338 spin_unlock_bh(&file_data
->lock
);
7339 percpu_ref_get(&file_data
->refs
);
7342 static int io_sqe_files_unregister(struct io_ring_ctx
*ctx
)
7344 struct fixed_file_data
*data
= ctx
->file_data
;
7345 struct fixed_file_ref_node
*backup_node
, *ref_node
= NULL
;
7346 unsigned nr_tables
, i
;
7351 backup_node
= alloc_fixed_file_ref_node(ctx
);
7355 spin_lock_bh(&data
->lock
);
7356 ref_node
= data
->node
;
7357 spin_unlock_bh(&data
->lock
);
7359 percpu_ref_kill(&ref_node
->refs
);
7361 percpu_ref_kill(&data
->refs
);
7363 /* wait for all refs nodes to complete */
7364 flush_delayed_work(&ctx
->file_put_work
);
7366 ret
= wait_for_completion_interruptible(&data
->done
);
7369 ret
= io_run_task_work_sig();
7371 percpu_ref_resurrect(&data
->refs
);
7372 reinit_completion(&data
->done
);
7373 io_sqe_files_set_node(data
, backup_node
);
7378 __io_sqe_files_unregister(ctx
);
7379 nr_tables
= DIV_ROUND_UP(ctx
->nr_user_files
, IORING_MAX_FILES_TABLE
);
7380 for (i
= 0; i
< nr_tables
; i
++)
7381 kfree(data
->table
[i
].files
);
7383 percpu_ref_exit(&data
->refs
);
7385 ctx
->file_data
= NULL
;
7386 ctx
->nr_user_files
= 0;
7387 destroy_fixed_file_ref_node(backup_node
);
7391 static void io_put_sq_data(struct io_sq_data
*sqd
)
7393 if (refcount_dec_and_test(&sqd
->refs
)) {
7395 * The park is a bit of a work-around, without it we get
7396 * warning spews on shutdown with SQPOLL set and affinity
7397 * set to a single CPU.
7400 kthread_park(sqd
->thread
);
7401 kthread_stop(sqd
->thread
);
7408 static struct io_sq_data
*io_attach_sq_data(struct io_uring_params
*p
)
7410 struct io_ring_ctx
*ctx_attach
;
7411 struct io_sq_data
*sqd
;
7414 f
= fdget(p
->wq_fd
);
7416 return ERR_PTR(-ENXIO
);
7417 if (f
.file
->f_op
!= &io_uring_fops
) {
7419 return ERR_PTR(-EINVAL
);
7422 ctx_attach
= f
.file
->private_data
;
7423 sqd
= ctx_attach
->sq_data
;
7426 return ERR_PTR(-EINVAL
);
7429 refcount_inc(&sqd
->refs
);
7434 static struct io_sq_data
*io_get_sq_data(struct io_uring_params
*p
)
7436 struct io_sq_data
*sqd
;
7438 if (p
->flags
& IORING_SETUP_ATTACH_WQ
)
7439 return io_attach_sq_data(p
);
7441 sqd
= kzalloc(sizeof(*sqd
), GFP_KERNEL
);
7443 return ERR_PTR(-ENOMEM
);
7445 refcount_set(&sqd
->refs
, 1);
7446 INIT_LIST_HEAD(&sqd
->ctx_list
);
7447 INIT_LIST_HEAD(&sqd
->ctx_new_list
);
7448 mutex_init(&sqd
->ctx_lock
);
7449 mutex_init(&sqd
->lock
);
7450 init_waitqueue_head(&sqd
->wait
);
7454 static void io_sq_thread_unpark(struct io_sq_data
*sqd
)
7455 __releases(&sqd
->lock
)
7459 kthread_unpark(sqd
->thread
);
7460 mutex_unlock(&sqd
->lock
);
7463 static void io_sq_thread_park(struct io_sq_data
*sqd
)
7464 __acquires(&sqd
->lock
)
7468 mutex_lock(&sqd
->lock
);
7469 kthread_park(sqd
->thread
);
7472 static void io_sq_thread_stop(struct io_ring_ctx
*ctx
)
7474 struct io_sq_data
*sqd
= ctx
->sq_data
;
7479 * We may arrive here from the error branch in
7480 * io_sq_offload_create() where the kthread is created
7481 * without being waked up, thus wake it up now to make
7482 * sure the wait will complete.
7484 wake_up_process(sqd
->thread
);
7485 wait_for_completion(&ctx
->sq_thread_comp
);
7487 io_sq_thread_park(sqd
);
7490 mutex_lock(&sqd
->ctx_lock
);
7491 list_del(&ctx
->sqd_list
);
7492 io_sqd_update_thread_idle(sqd
);
7493 mutex_unlock(&sqd
->ctx_lock
);
7496 io_sq_thread_unpark(sqd
);
7498 io_put_sq_data(sqd
);
7499 ctx
->sq_data
= NULL
;
7503 static void io_finish_async(struct io_ring_ctx
*ctx
)
7505 io_sq_thread_stop(ctx
);
7508 io_wq_destroy(ctx
->io_wq
);
7513 #if defined(CONFIG_UNIX)
7515 * Ensure the UNIX gc is aware of our file set, so we are certain that
7516 * the io_uring can be safely unregistered on process exit, even if we have
7517 * loops in the file referencing.
7519 static int __io_sqe_files_scm(struct io_ring_ctx
*ctx
, int nr
, int offset
)
7521 struct sock
*sk
= ctx
->ring_sock
->sk
;
7522 struct scm_fp_list
*fpl
;
7523 struct sk_buff
*skb
;
7526 fpl
= kzalloc(sizeof(*fpl
), GFP_KERNEL
);
7530 skb
= alloc_skb(0, GFP_KERNEL
);
7539 fpl
->user
= get_uid(ctx
->user
);
7540 for (i
= 0; i
< nr
; i
++) {
7541 struct file
*file
= io_file_from_index(ctx
, i
+ offset
);
7545 fpl
->fp
[nr_files
] = get_file(file
);
7546 unix_inflight(fpl
->user
, fpl
->fp
[nr_files
]);
7551 fpl
->max
= SCM_MAX_FD
;
7552 fpl
->count
= nr_files
;
7553 UNIXCB(skb
).fp
= fpl
;
7554 skb
->destructor
= unix_destruct_scm
;
7555 refcount_add(skb
->truesize
, &sk
->sk_wmem_alloc
);
7556 skb_queue_head(&sk
->sk_receive_queue
, skb
);
7558 for (i
= 0; i
< nr_files
; i
++)
7569 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7570 * causes regular reference counting to break down. We rely on the UNIX
7571 * garbage collection to take care of this problem for us.
7573 static int io_sqe_files_scm(struct io_ring_ctx
*ctx
)
7575 unsigned left
, total
;
7579 left
= ctx
->nr_user_files
;
7581 unsigned this_files
= min_t(unsigned, left
, SCM_MAX_FD
);
7583 ret
= __io_sqe_files_scm(ctx
, this_files
, total
);
7587 total
+= this_files
;
7593 while (total
< ctx
->nr_user_files
) {
7594 struct file
*file
= io_file_from_index(ctx
, total
);
7604 static int io_sqe_files_scm(struct io_ring_ctx
*ctx
)
7610 static int io_sqe_alloc_file_tables(struct fixed_file_data
*file_data
,
7611 unsigned nr_tables
, unsigned nr_files
)
7615 for (i
= 0; i
< nr_tables
; i
++) {
7616 struct fixed_file_table
*table
= &file_data
->table
[i
];
7617 unsigned this_files
;
7619 this_files
= min(nr_files
, IORING_MAX_FILES_TABLE
);
7620 table
->files
= kcalloc(this_files
, sizeof(struct file
*),
7624 nr_files
-= this_files
;
7630 for (i
= 0; i
< nr_tables
; i
++) {
7631 struct fixed_file_table
*table
= &file_data
->table
[i
];
7632 kfree(table
->files
);
7637 static void io_ring_file_put(struct io_ring_ctx
*ctx
, struct file
*file
)
7639 #if defined(CONFIG_UNIX)
7640 struct sock
*sock
= ctx
->ring_sock
->sk
;
7641 struct sk_buff_head list
, *head
= &sock
->sk_receive_queue
;
7642 struct sk_buff
*skb
;
7645 __skb_queue_head_init(&list
);
7648 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7649 * remove this entry and rearrange the file array.
7651 skb
= skb_dequeue(head
);
7653 struct scm_fp_list
*fp
;
7655 fp
= UNIXCB(skb
).fp
;
7656 for (i
= 0; i
< fp
->count
; i
++) {
7659 if (fp
->fp
[i
] != file
)
7662 unix_notinflight(fp
->user
, fp
->fp
[i
]);
7663 left
= fp
->count
- 1 - i
;
7665 memmove(&fp
->fp
[i
], &fp
->fp
[i
+ 1],
7666 left
* sizeof(struct file
*));
7673 __skb_queue_tail(&list
, skb
);
7683 __skb_queue_tail(&list
, skb
);
7685 skb
= skb_dequeue(head
);
7688 if (skb_peek(&list
)) {
7689 spin_lock_irq(&head
->lock
);
7690 while ((skb
= __skb_dequeue(&list
)) != NULL
)
7691 __skb_queue_tail(head
, skb
);
7692 spin_unlock_irq(&head
->lock
);
7699 struct io_file_put
{
7700 struct list_head list
;
7704 static void __io_file_put_work(struct fixed_file_ref_node
*ref_node
)
7706 struct fixed_file_data
*file_data
= ref_node
->file_data
;
7707 struct io_ring_ctx
*ctx
= file_data
->ctx
;
7708 struct io_file_put
*pfile
, *tmp
;
7710 list_for_each_entry_safe(pfile
, tmp
, &ref_node
->file_list
, list
) {
7711 list_del(&pfile
->list
);
7712 io_ring_file_put(ctx
, pfile
->file
);
7716 percpu_ref_exit(&ref_node
->refs
);
7718 percpu_ref_put(&file_data
->refs
);
7721 static void io_file_put_work(struct work_struct
*work
)
7723 struct io_ring_ctx
*ctx
;
7724 struct llist_node
*node
;
7726 ctx
= container_of(work
, struct io_ring_ctx
, file_put_work
.work
);
7727 node
= llist_del_all(&ctx
->file_put_llist
);
7730 struct fixed_file_ref_node
*ref_node
;
7731 struct llist_node
*next
= node
->next
;
7733 ref_node
= llist_entry(node
, struct fixed_file_ref_node
, llist
);
7734 __io_file_put_work(ref_node
);
7739 static void io_file_data_ref_zero(struct percpu_ref
*ref
)
7741 struct fixed_file_ref_node
*ref_node
;
7742 struct fixed_file_data
*data
;
7743 struct io_ring_ctx
*ctx
;
7744 bool first_add
= false;
7747 ref_node
= container_of(ref
, struct fixed_file_ref_node
, refs
);
7748 data
= ref_node
->file_data
;
7751 spin_lock_bh(&data
->lock
);
7752 ref_node
->done
= true;
7754 while (!list_empty(&data
->ref_list
)) {
7755 ref_node
= list_first_entry(&data
->ref_list
,
7756 struct fixed_file_ref_node
, node
);
7757 /* recycle ref nodes in order */
7758 if (!ref_node
->done
)
7760 list_del(&ref_node
->node
);
7761 first_add
|= llist_add(&ref_node
->llist
, &ctx
->file_put_llist
);
7763 spin_unlock_bh(&data
->lock
);
7765 if (percpu_ref_is_dying(&data
->refs
))
7769 mod_delayed_work(system_wq
, &ctx
->file_put_work
, 0);
7771 queue_delayed_work(system_wq
, &ctx
->file_put_work
, delay
);
7774 static struct fixed_file_ref_node
*alloc_fixed_file_ref_node(
7775 struct io_ring_ctx
*ctx
)
7777 struct fixed_file_ref_node
*ref_node
;
7779 ref_node
= kzalloc(sizeof(*ref_node
), GFP_KERNEL
);
7783 if (percpu_ref_init(&ref_node
->refs
, io_file_data_ref_zero
,
7788 INIT_LIST_HEAD(&ref_node
->node
);
7789 INIT_LIST_HEAD(&ref_node
->file_list
);
7790 ref_node
->file_data
= ctx
->file_data
;
7791 ref_node
->done
= false;
7795 static void destroy_fixed_file_ref_node(struct fixed_file_ref_node
*ref_node
)
7797 percpu_ref_exit(&ref_node
->refs
);
7801 static int io_sqe_files_register(struct io_ring_ctx
*ctx
, void __user
*arg
,
7804 __s32 __user
*fds
= (__s32 __user
*) arg
;
7805 unsigned nr_tables
, i
;
7807 int fd
, ret
= -ENOMEM
;
7808 struct fixed_file_ref_node
*ref_node
;
7809 struct fixed_file_data
*file_data
;
7815 if (nr_args
> IORING_MAX_FIXED_FILES
)
7818 file_data
= kzalloc(sizeof(*ctx
->file_data
), GFP_KERNEL
);
7821 file_data
->ctx
= ctx
;
7822 init_completion(&file_data
->done
);
7823 INIT_LIST_HEAD(&file_data
->ref_list
);
7824 spin_lock_init(&file_data
->lock
);
7826 nr_tables
= DIV_ROUND_UP(nr_args
, IORING_MAX_FILES_TABLE
);
7827 file_data
->table
= kcalloc(nr_tables
, sizeof(*file_data
->table
),
7829 if (!file_data
->table
)
7832 if (percpu_ref_init(&file_data
->refs
, io_file_ref_kill
,
7833 PERCPU_REF_ALLOW_REINIT
, GFP_KERNEL
))
7836 if (io_sqe_alloc_file_tables(file_data
, nr_tables
, nr_args
))
7838 ctx
->file_data
= file_data
;
7840 for (i
= 0; i
< nr_args
; i
++, ctx
->nr_user_files
++) {
7841 struct fixed_file_table
*table
;
7844 if (copy_from_user(&fd
, &fds
[i
], sizeof(fd
))) {
7848 /* allow sparse sets */
7858 * Don't allow io_uring instances to be registered. If UNIX
7859 * isn't enabled, then this causes a reference cycle and this
7860 * instance can never get freed. If UNIX is enabled we'll
7861 * handle it just fine, but there's still no point in allowing
7862 * a ring fd as it doesn't support regular read/write anyway.
7864 if (file
->f_op
== &io_uring_fops
) {
7868 table
= &file_data
->table
[i
>> IORING_FILE_TABLE_SHIFT
];
7869 index
= i
& IORING_FILE_TABLE_MASK
;
7870 table
->files
[index
] = file
;
7873 ret
= io_sqe_files_scm(ctx
);
7875 io_sqe_files_unregister(ctx
);
7879 ref_node
= alloc_fixed_file_ref_node(ctx
);
7881 io_sqe_files_unregister(ctx
);
7885 io_sqe_files_set_node(file_data
, ref_node
);
7888 for (i
= 0; i
< ctx
->nr_user_files
; i
++) {
7889 file
= io_file_from_index(ctx
, i
);
7893 for (i
= 0; i
< nr_tables
; i
++)
7894 kfree(file_data
->table
[i
].files
);
7895 ctx
->nr_user_files
= 0;
7897 percpu_ref_exit(&file_data
->refs
);
7899 kfree(file_data
->table
);
7901 ctx
->file_data
= NULL
;
7905 static int io_sqe_file_register(struct io_ring_ctx
*ctx
, struct file
*file
,
7908 #if defined(CONFIG_UNIX)
7909 struct sock
*sock
= ctx
->ring_sock
->sk
;
7910 struct sk_buff_head
*head
= &sock
->sk_receive_queue
;
7911 struct sk_buff
*skb
;
7914 * See if we can merge this file into an existing skb SCM_RIGHTS
7915 * file set. If there's no room, fall back to allocating a new skb
7916 * and filling it in.
7918 spin_lock_irq(&head
->lock
);
7919 skb
= skb_peek(head
);
7921 struct scm_fp_list
*fpl
= UNIXCB(skb
).fp
;
7923 if (fpl
->count
< SCM_MAX_FD
) {
7924 __skb_unlink(skb
, head
);
7925 spin_unlock_irq(&head
->lock
);
7926 fpl
->fp
[fpl
->count
] = get_file(file
);
7927 unix_inflight(fpl
->user
, fpl
->fp
[fpl
->count
]);
7929 spin_lock_irq(&head
->lock
);
7930 __skb_queue_head(head
, skb
);
7935 spin_unlock_irq(&head
->lock
);
7942 return __io_sqe_files_scm(ctx
, 1, index
);
7948 static int io_queue_file_removal(struct fixed_file_data
*data
,
7951 struct io_file_put
*pfile
;
7952 struct fixed_file_ref_node
*ref_node
= data
->node
;
7954 pfile
= kzalloc(sizeof(*pfile
), GFP_KERNEL
);
7959 list_add(&pfile
->list
, &ref_node
->file_list
);
7964 static int __io_sqe_files_update(struct io_ring_ctx
*ctx
,
7965 struct io_uring_files_update
*up
,
7968 struct fixed_file_data
*data
= ctx
->file_data
;
7969 struct fixed_file_ref_node
*ref_node
;
7974 bool needs_switch
= false;
7976 if (check_add_overflow(up
->offset
, nr_args
, &done
))
7978 if (done
> ctx
->nr_user_files
)
7981 ref_node
= alloc_fixed_file_ref_node(ctx
);
7986 fds
= u64_to_user_ptr(up
->fds
);
7988 struct fixed_file_table
*table
;
7992 if (copy_from_user(&fd
, &fds
[done
], sizeof(fd
))) {
7996 i
= array_index_nospec(up
->offset
, ctx
->nr_user_files
);
7997 table
= &ctx
->file_data
->table
[i
>> IORING_FILE_TABLE_SHIFT
];
7998 index
= i
& IORING_FILE_TABLE_MASK
;
7999 if (table
->files
[index
]) {
8000 file
= table
->files
[index
];
8001 err
= io_queue_file_removal(data
, file
);
8004 table
->files
[index
] = NULL
;
8005 needs_switch
= true;
8014 * Don't allow io_uring instances to be registered. If
8015 * UNIX isn't enabled, then this causes a reference
8016 * cycle and this instance can never get freed. If UNIX
8017 * is enabled we'll handle it just fine, but there's
8018 * still no point in allowing a ring fd as it doesn't
8019 * support regular read/write anyway.
8021 if (file
->f_op
== &io_uring_fops
) {
8026 table
->files
[index
] = file
;
8027 err
= io_sqe_file_register(ctx
, file
, i
);
8029 table
->files
[index
] = NULL
;
8040 percpu_ref_kill(&data
->node
->refs
);
8041 io_sqe_files_set_node(data
, ref_node
);
8043 destroy_fixed_file_ref_node(ref_node
);
8045 return done
? done
: err
;
8048 static int io_sqe_files_update(struct io_ring_ctx
*ctx
, void __user
*arg
,
8051 struct io_uring_files_update up
;
8053 if (!ctx
->file_data
)
8057 if (copy_from_user(&up
, arg
, sizeof(up
)))
8062 return __io_sqe_files_update(ctx
, &up
, nr_args
);
8065 static void io_free_work(struct io_wq_work
*work
)
8067 struct io_kiocb
*req
= container_of(work
, struct io_kiocb
, work
);
8069 /* Consider that io_steal_work() relies on this ref */
8073 static int io_init_wq_offload(struct io_ring_ctx
*ctx
,
8074 struct io_uring_params
*p
)
8076 struct io_wq_data data
;
8078 struct io_ring_ctx
*ctx_attach
;
8079 unsigned int concurrency
;
8082 data
.user
= ctx
->user
;
8083 data
.free_work
= io_free_work
;
8084 data
.do_work
= io_wq_submit_work
;
8086 if (!(p
->flags
& IORING_SETUP_ATTACH_WQ
)) {
8087 /* Do QD, or 4 * CPUS, whatever is smallest */
8088 concurrency
= min(ctx
->sq_entries
, 4 * num_online_cpus());
8090 ctx
->io_wq
= io_wq_create(concurrency
, &data
);
8091 if (IS_ERR(ctx
->io_wq
)) {
8092 ret
= PTR_ERR(ctx
->io_wq
);
8098 f
= fdget(p
->wq_fd
);
8102 if (f
.file
->f_op
!= &io_uring_fops
) {
8107 ctx_attach
= f
.file
->private_data
;
8108 /* @io_wq is protected by holding the fd */
8109 if (!io_wq_get(ctx_attach
->io_wq
, &data
)) {
8114 ctx
->io_wq
= ctx_attach
->io_wq
;
8120 static int io_uring_alloc_task_context(struct task_struct
*task
)
8122 struct io_uring_task
*tctx
;
8125 tctx
= kmalloc(sizeof(*tctx
), GFP_KERNEL
);
8126 if (unlikely(!tctx
))
8129 ret
= percpu_counter_init(&tctx
->inflight
, 0, GFP_KERNEL
);
8130 if (unlikely(ret
)) {
8136 init_waitqueue_head(&tctx
->wait
);
8138 atomic_set(&tctx
->in_idle
, 0);
8139 tctx
->sqpoll
= false;
8140 io_init_identity(&tctx
->__identity
);
8141 tctx
->identity
= &tctx
->__identity
;
8142 task
->io_uring
= tctx
;
8146 void __io_uring_free(struct task_struct
*tsk
)
8148 struct io_uring_task
*tctx
= tsk
->io_uring
;
8150 WARN_ON_ONCE(!xa_empty(&tctx
->xa
));
8151 WARN_ON_ONCE(refcount_read(&tctx
->identity
->count
) != 1);
8152 if (tctx
->identity
!= &tctx
->__identity
)
8153 kfree(tctx
->identity
);
8154 percpu_counter_destroy(&tctx
->inflight
);
8156 tsk
->io_uring
= NULL
;
8159 static int io_sq_offload_create(struct io_ring_ctx
*ctx
,
8160 struct io_uring_params
*p
)
8164 if (ctx
->flags
& IORING_SETUP_SQPOLL
) {
8165 struct io_sq_data
*sqd
;
8168 if (!capable(CAP_SYS_ADMIN
) && !capable(CAP_SYS_NICE
))
8171 sqd
= io_get_sq_data(p
);
8178 io_sq_thread_park(sqd
);
8179 mutex_lock(&sqd
->ctx_lock
);
8180 list_add(&ctx
->sqd_list
, &sqd
->ctx_new_list
);
8181 mutex_unlock(&sqd
->ctx_lock
);
8182 io_sq_thread_unpark(sqd
);
8184 ctx
->sq_thread_idle
= msecs_to_jiffies(p
->sq_thread_idle
);
8185 if (!ctx
->sq_thread_idle
)
8186 ctx
->sq_thread_idle
= HZ
;
8191 if (p
->flags
& IORING_SETUP_SQ_AFF
) {
8192 int cpu
= p
->sq_thread_cpu
;
8195 if (cpu
>= nr_cpu_ids
)
8197 if (!cpu_online(cpu
))
8200 sqd
->thread
= kthread_create_on_cpu(io_sq_thread
, sqd
,
8201 cpu
, "io_uring-sq");
8203 sqd
->thread
= kthread_create(io_sq_thread
, sqd
,
8206 if (IS_ERR(sqd
->thread
)) {
8207 ret
= PTR_ERR(sqd
->thread
);
8211 ret
= io_uring_alloc_task_context(sqd
->thread
);
8214 } else if (p
->flags
& IORING_SETUP_SQ_AFF
) {
8215 /* Can't have SQ_AFF without SQPOLL */
8221 ret
= io_init_wq_offload(ctx
, p
);
8227 io_finish_async(ctx
);
8231 static void io_sq_offload_start(struct io_ring_ctx
*ctx
)
8233 struct io_sq_data
*sqd
= ctx
->sq_data
;
8235 if ((ctx
->flags
& IORING_SETUP_SQPOLL
) && sqd
->thread
)
8236 wake_up_process(sqd
->thread
);
8239 static inline void __io_unaccount_mem(struct user_struct
*user
,
8240 unsigned long nr_pages
)
8242 atomic_long_sub(nr_pages
, &user
->locked_vm
);
8245 static inline int __io_account_mem(struct user_struct
*user
,
8246 unsigned long nr_pages
)
8248 unsigned long page_limit
, cur_pages
, new_pages
;
8250 /* Don't allow more pages than we can safely lock */
8251 page_limit
= rlimit(RLIMIT_MEMLOCK
) >> PAGE_SHIFT
;
8254 cur_pages
= atomic_long_read(&user
->locked_vm
);
8255 new_pages
= cur_pages
+ nr_pages
;
8256 if (new_pages
> page_limit
)
8258 } while (atomic_long_cmpxchg(&user
->locked_vm
, cur_pages
,
8259 new_pages
) != cur_pages
);
8264 static void io_unaccount_mem(struct io_ring_ctx
*ctx
, unsigned long nr_pages
,
8265 enum io_mem_account acct
)
8268 __io_unaccount_mem(ctx
->user
, nr_pages
);
8270 if (ctx
->mm_account
) {
8271 if (acct
== ACCT_LOCKED
) {
8272 mmap_write_lock(ctx
->mm_account
);
8273 ctx
->mm_account
->locked_vm
-= nr_pages
;
8274 mmap_write_unlock(ctx
->mm_account
);
8275 }else if (acct
== ACCT_PINNED
) {
8276 atomic64_sub(nr_pages
, &ctx
->mm_account
->pinned_vm
);
8281 static int io_account_mem(struct io_ring_ctx
*ctx
, unsigned long nr_pages
,
8282 enum io_mem_account acct
)
8286 if (ctx
->limit_mem
) {
8287 ret
= __io_account_mem(ctx
->user
, nr_pages
);
8292 if (ctx
->mm_account
) {
8293 if (acct
== ACCT_LOCKED
) {
8294 mmap_write_lock(ctx
->mm_account
);
8295 ctx
->mm_account
->locked_vm
+= nr_pages
;
8296 mmap_write_unlock(ctx
->mm_account
);
8297 } else if (acct
== ACCT_PINNED
) {
8298 atomic64_add(nr_pages
, &ctx
->mm_account
->pinned_vm
);
8305 static void io_mem_free(void *ptr
)
8312 page
= virt_to_head_page(ptr
);
8313 if (put_page_testzero(page
))
8314 free_compound_page(page
);
8317 static void *io_mem_alloc(size_t size
)
8319 gfp_t gfp_flags
= GFP_KERNEL
| __GFP_ZERO
| __GFP_NOWARN
| __GFP_COMP
|
8322 return (void *) __get_free_pages(gfp_flags
, get_order(size
));
8325 static unsigned long rings_size(unsigned sq_entries
, unsigned cq_entries
,
8328 struct io_rings
*rings
;
8329 size_t off
, sq_array_size
;
8331 off
= struct_size(rings
, cqes
, cq_entries
);
8332 if (off
== SIZE_MAX
)
8336 off
= ALIGN(off
, SMP_CACHE_BYTES
);
8344 sq_array_size
= array_size(sizeof(u32
), sq_entries
);
8345 if (sq_array_size
== SIZE_MAX
)
8348 if (check_add_overflow(off
, sq_array_size
, &off
))
8354 static unsigned long ring_pages(unsigned sq_entries
, unsigned cq_entries
)
8358 pages
= (size_t)1 << get_order(
8359 rings_size(sq_entries
, cq_entries
, NULL
));
8360 pages
+= (size_t)1 << get_order(
8361 array_size(sizeof(struct io_uring_sqe
), sq_entries
));
8366 static int io_sqe_buffer_unregister(struct io_ring_ctx
*ctx
)
8370 if (!ctx
->user_bufs
)
8373 for (i
= 0; i
< ctx
->nr_user_bufs
; i
++) {
8374 struct io_mapped_ubuf
*imu
= &ctx
->user_bufs
[i
];
8376 for (j
= 0; j
< imu
->nr_bvecs
; j
++)
8377 unpin_user_page(imu
->bvec
[j
].bv_page
);
8379 if (imu
->acct_pages
)
8380 io_unaccount_mem(ctx
, imu
->acct_pages
, ACCT_PINNED
);
8385 kfree(ctx
->user_bufs
);
8386 ctx
->user_bufs
= NULL
;
8387 ctx
->nr_user_bufs
= 0;
8391 static int io_copy_iov(struct io_ring_ctx
*ctx
, struct iovec
*dst
,
8392 void __user
*arg
, unsigned index
)
8394 struct iovec __user
*src
;
8396 #ifdef CONFIG_COMPAT
8398 struct compat_iovec __user
*ciovs
;
8399 struct compat_iovec ciov
;
8401 ciovs
= (struct compat_iovec __user
*) arg
;
8402 if (copy_from_user(&ciov
, &ciovs
[index
], sizeof(ciov
)))
8405 dst
->iov_base
= u64_to_user_ptr((u64
)ciov
.iov_base
);
8406 dst
->iov_len
= ciov
.iov_len
;
8410 src
= (struct iovec __user
*) arg
;
8411 if (copy_from_user(dst
, &src
[index
], sizeof(*dst
)))
8417 * Not super efficient, but this is just a registration time. And we do cache
8418 * the last compound head, so generally we'll only do a full search if we don't
8421 * We check if the given compound head page has already been accounted, to
8422 * avoid double accounting it. This allows us to account the full size of the
8423 * page, not just the constituent pages of a huge page.
8425 static bool headpage_already_acct(struct io_ring_ctx
*ctx
, struct page
**pages
,
8426 int nr_pages
, struct page
*hpage
)
8430 /* check current page array */
8431 for (i
= 0; i
< nr_pages
; i
++) {
8432 if (!PageCompound(pages
[i
]))
8434 if (compound_head(pages
[i
]) == hpage
)
8438 /* check previously registered pages */
8439 for (i
= 0; i
< ctx
->nr_user_bufs
; i
++) {
8440 struct io_mapped_ubuf
*imu
= &ctx
->user_bufs
[i
];
8442 for (j
= 0; j
< imu
->nr_bvecs
; j
++) {
8443 if (!PageCompound(imu
->bvec
[j
].bv_page
))
8445 if (compound_head(imu
->bvec
[j
].bv_page
) == hpage
)
8453 static int io_buffer_account_pin(struct io_ring_ctx
*ctx
, struct page
**pages
,
8454 int nr_pages
, struct io_mapped_ubuf
*imu
,
8455 struct page
**last_hpage
)
8459 for (i
= 0; i
< nr_pages
; i
++) {
8460 if (!PageCompound(pages
[i
])) {
8465 hpage
= compound_head(pages
[i
]);
8466 if (hpage
== *last_hpage
)
8468 *last_hpage
= hpage
;
8469 if (headpage_already_acct(ctx
, pages
, i
, hpage
))
8471 imu
->acct_pages
+= page_size(hpage
) >> PAGE_SHIFT
;
8475 if (!imu
->acct_pages
)
8478 ret
= io_account_mem(ctx
, imu
->acct_pages
, ACCT_PINNED
);
8480 imu
->acct_pages
= 0;
8484 static int io_sqe_buffer_register(struct io_ring_ctx
*ctx
, void __user
*arg
,
8487 struct vm_area_struct
**vmas
= NULL
;
8488 struct page
**pages
= NULL
;
8489 struct page
*last_hpage
= NULL
;
8490 int i
, j
, got_pages
= 0;
8495 if (!nr_args
|| nr_args
> UIO_MAXIOV
)
8498 ctx
->user_bufs
= kcalloc(nr_args
, sizeof(struct io_mapped_ubuf
),
8500 if (!ctx
->user_bufs
)
8503 for (i
= 0; i
< nr_args
; i
++) {
8504 struct io_mapped_ubuf
*imu
= &ctx
->user_bufs
[i
];
8505 unsigned long off
, start
, end
, ubuf
;
8510 ret
= io_copy_iov(ctx
, &iov
, arg
, i
);
8515 * Don't impose further limits on the size and buffer
8516 * constraints here, we'll -EINVAL later when IO is
8517 * submitted if they are wrong.
8520 if (!iov
.iov_base
|| !iov
.iov_len
)
8523 /* arbitrary limit, but we need something */
8524 if (iov
.iov_len
> SZ_1G
)
8527 ubuf
= (unsigned long) iov
.iov_base
;
8528 end
= (ubuf
+ iov
.iov_len
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
8529 start
= ubuf
>> PAGE_SHIFT
;
8530 nr_pages
= end
- start
;
8533 if (!pages
|| nr_pages
> got_pages
) {
8536 pages
= kvmalloc_array(nr_pages
, sizeof(struct page
*),
8538 vmas
= kvmalloc_array(nr_pages
,
8539 sizeof(struct vm_area_struct
*),
8541 if (!pages
|| !vmas
) {
8545 got_pages
= nr_pages
;
8548 imu
->bvec
= kvmalloc_array(nr_pages
, sizeof(struct bio_vec
),
8555 mmap_read_lock(current
->mm
);
8556 pret
= pin_user_pages(ubuf
, nr_pages
,
8557 FOLL_WRITE
| FOLL_LONGTERM
,
8559 if (pret
== nr_pages
) {
8560 /* don't support file backed memory */
8561 for (j
= 0; j
< nr_pages
; j
++) {
8562 struct vm_area_struct
*vma
= vmas
[j
];
8565 !is_file_hugepages(vma
->vm_file
)) {
8571 ret
= pret
< 0 ? pret
: -EFAULT
;
8573 mmap_read_unlock(current
->mm
);
8576 * if we did partial map, or found file backed vmas,
8577 * release any pages we did get
8580 unpin_user_pages(pages
, pret
);
8585 ret
= io_buffer_account_pin(ctx
, pages
, pret
, imu
, &last_hpage
);
8587 unpin_user_pages(pages
, pret
);
8592 off
= ubuf
& ~PAGE_MASK
;
8594 for (j
= 0; j
< nr_pages
; j
++) {
8597 vec_len
= min_t(size_t, size
, PAGE_SIZE
- off
);
8598 imu
->bvec
[j
].bv_page
= pages
[j
];
8599 imu
->bvec
[j
].bv_len
= vec_len
;
8600 imu
->bvec
[j
].bv_offset
= off
;
8604 /* store original address for later verification */
8606 imu
->len
= iov
.iov_len
;
8607 imu
->nr_bvecs
= nr_pages
;
8609 ctx
->nr_user_bufs
++;
8617 io_sqe_buffer_unregister(ctx
);
8621 static int io_eventfd_register(struct io_ring_ctx
*ctx
, void __user
*arg
)
8623 __s32 __user
*fds
= arg
;
8629 if (copy_from_user(&fd
, fds
, sizeof(*fds
)))
8632 ctx
->cq_ev_fd
= eventfd_ctx_fdget(fd
);
8633 if (IS_ERR(ctx
->cq_ev_fd
)) {
8634 int ret
= PTR_ERR(ctx
->cq_ev_fd
);
8635 ctx
->cq_ev_fd
= NULL
;
8642 static int io_eventfd_unregister(struct io_ring_ctx
*ctx
)
8644 if (ctx
->cq_ev_fd
) {
8645 eventfd_ctx_put(ctx
->cq_ev_fd
);
8646 ctx
->cq_ev_fd
= NULL
;
8653 static int __io_destroy_buffers(int id
, void *p
, void *data
)
8655 struct io_ring_ctx
*ctx
= data
;
8656 struct io_buffer
*buf
= p
;
8658 __io_remove_buffers(ctx
, buf
, id
, -1U);
8662 static void io_destroy_buffers(struct io_ring_ctx
*ctx
)
8664 idr_for_each(&ctx
->io_buffer_idr
, __io_destroy_buffers
, ctx
);
8665 idr_destroy(&ctx
->io_buffer_idr
);
8668 static void io_ring_ctx_free(struct io_ring_ctx
*ctx
)
8670 io_finish_async(ctx
);
8671 io_sqe_buffer_unregister(ctx
);
8673 if (ctx
->sqo_task
) {
8674 put_task_struct(ctx
->sqo_task
);
8675 ctx
->sqo_task
= NULL
;
8676 mmdrop(ctx
->mm_account
);
8677 ctx
->mm_account
= NULL
;
8680 #ifdef CONFIG_BLK_CGROUP
8681 if (ctx
->sqo_blkcg_css
)
8682 css_put(ctx
->sqo_blkcg_css
);
8685 io_sqe_files_unregister(ctx
);
8686 io_eventfd_unregister(ctx
);
8687 io_destroy_buffers(ctx
);
8688 idr_destroy(&ctx
->personality_idr
);
8690 #if defined(CONFIG_UNIX)
8691 if (ctx
->ring_sock
) {
8692 ctx
->ring_sock
->file
= NULL
; /* so that iput() is called */
8693 sock_release(ctx
->ring_sock
);
8697 io_mem_free(ctx
->rings
);
8698 io_mem_free(ctx
->sq_sqes
);
8700 percpu_ref_exit(&ctx
->refs
);
8701 free_uid(ctx
->user
);
8702 put_cred(ctx
->creds
);
8703 kfree(ctx
->cancel_hash
);
8704 kmem_cache_free(req_cachep
, ctx
->fallback_req
);
8708 static __poll_t
io_uring_poll(struct file
*file
, poll_table
*wait
)
8710 struct io_ring_ctx
*ctx
= file
->private_data
;
8713 poll_wait(file
, &ctx
->cq_wait
, wait
);
8715 * synchronizes with barrier from wq_has_sleeper call in
8719 if (!io_sqring_full(ctx
))
8720 mask
|= EPOLLOUT
| EPOLLWRNORM
;
8721 io_cqring_overflow_flush(ctx
, false, NULL
, NULL
);
8722 if (io_cqring_events(ctx
))
8723 mask
|= EPOLLIN
| EPOLLRDNORM
;
8728 static int io_uring_fasync(int fd
, struct file
*file
, int on
)
8730 struct io_ring_ctx
*ctx
= file
->private_data
;
8732 return fasync_helper(fd
, file
, on
, &ctx
->cq_fasync
);
8735 static int io_remove_personalities(int id
, void *p
, void *data
)
8737 struct io_ring_ctx
*ctx
= data
;
8738 struct io_identity
*iod
;
8740 iod
= idr_remove(&ctx
->personality_idr
, id
);
8742 put_cred(iod
->creds
);
8743 if (refcount_dec_and_test(&iod
->count
))
8749 static void io_ring_exit_work(struct work_struct
*work
)
8751 struct io_ring_ctx
*ctx
= container_of(work
, struct io_ring_ctx
,
8755 * If we're doing polled IO and end up having requests being
8756 * submitted async (out-of-line), then completions can come in while
8757 * we're waiting for refs to drop. We need to reap these manually,
8758 * as nobody else will be looking for them.
8761 __io_uring_cancel_task_requests(ctx
, NULL
);
8762 } while (!wait_for_completion_timeout(&ctx
->ref_comp
, HZ
/20));
8763 io_ring_ctx_free(ctx
);
8766 static bool io_cancel_ctx_cb(struct io_wq_work
*work
, void *data
)
8768 struct io_kiocb
*req
= container_of(work
, struct io_kiocb
, work
);
8770 return req
->ctx
== data
;
8773 static void io_ring_ctx_wait_and_kill(struct io_ring_ctx
*ctx
)
8775 mutex_lock(&ctx
->uring_lock
);
8776 percpu_ref_kill(&ctx
->refs
);
8778 if (WARN_ON_ONCE((ctx
->flags
& IORING_SETUP_SQPOLL
) && !ctx
->sqo_dead
))
8781 /* if force is set, the ring is going away. always drop after that */
8782 ctx
->cq_overflow_flushed
= 1;
8784 __io_cqring_overflow_flush(ctx
, true, NULL
, NULL
);
8785 mutex_unlock(&ctx
->uring_lock
);
8787 io_kill_timeouts(ctx
, NULL
, NULL
);
8788 io_poll_remove_all(ctx
, NULL
, NULL
);
8791 io_wq_cancel_cb(ctx
->io_wq
, io_cancel_ctx_cb
, ctx
, true);
8793 /* if we failed setting up the ctx, we might not have any rings */
8794 io_iopoll_try_reap_events(ctx
);
8795 idr_for_each(&ctx
->personality_idr
, io_remove_personalities
, ctx
);
8798 * Do this upfront, so we won't have a grace period where the ring
8799 * is closed but resources aren't reaped yet. This can cause
8800 * spurious failure in setting up a new ring.
8802 io_unaccount_mem(ctx
, ring_pages(ctx
->sq_entries
, ctx
->cq_entries
),
8805 INIT_WORK(&ctx
->exit_work
, io_ring_exit_work
);
8807 * Use system_unbound_wq to avoid spawning tons of event kworkers
8808 * if we're exiting a ton of rings at the same time. It just adds
8809 * noise and overhead, there's no discernable change in runtime
8810 * over using system_wq.
8812 queue_work(system_unbound_wq
, &ctx
->exit_work
);
8815 static int io_uring_release(struct inode
*inode
, struct file
*file
)
8817 struct io_ring_ctx
*ctx
= file
->private_data
;
8819 file
->private_data
= NULL
;
8820 io_ring_ctx_wait_and_kill(ctx
);
8824 struct io_task_cancel
{
8825 struct task_struct
*task
;
8826 struct files_struct
*files
;
8829 static bool io_cancel_task_cb(struct io_wq_work
*work
, void *data
)
8831 struct io_kiocb
*req
= container_of(work
, struct io_kiocb
, work
);
8832 struct io_task_cancel
*cancel
= data
;
8835 if (cancel
->files
&& (req
->flags
& REQ_F_LINK_TIMEOUT
)) {
8836 unsigned long flags
;
8837 struct io_ring_ctx
*ctx
= req
->ctx
;
8839 /* protect against races with linked timeouts */
8840 spin_lock_irqsave(&ctx
->completion_lock
, flags
);
8841 ret
= io_match_task(req
, cancel
->task
, cancel
->files
);
8842 spin_unlock_irqrestore(&ctx
->completion_lock
, flags
);
8844 ret
= io_match_task(req
, cancel
->task
, cancel
->files
);
8849 static void io_cancel_defer_files(struct io_ring_ctx
*ctx
,
8850 struct task_struct
*task
,
8851 struct files_struct
*files
)
8853 struct io_defer_entry
*de
= NULL
;
8856 spin_lock_irq(&ctx
->completion_lock
);
8857 list_for_each_entry_reverse(de
, &ctx
->defer_list
, list
) {
8858 if (io_match_task(de
->req
, task
, files
)) {
8859 list_cut_position(&list
, &ctx
->defer_list
, &de
->list
);
8863 spin_unlock_irq(&ctx
->completion_lock
);
8865 while (!list_empty(&list
)) {
8866 de
= list_first_entry(&list
, struct io_defer_entry
, list
);
8867 list_del_init(&de
->list
);
8868 req_set_fail_links(de
->req
);
8869 io_put_req(de
->req
);
8870 io_req_complete(de
->req
, -ECANCELED
);
8875 static int io_uring_count_inflight(struct io_ring_ctx
*ctx
,
8876 struct task_struct
*task
,
8877 struct files_struct
*files
)
8879 struct io_kiocb
*req
;
8882 spin_lock_irq(&ctx
->inflight_lock
);
8883 list_for_each_entry(req
, &ctx
->inflight_list
, inflight_entry
)
8884 cnt
+= io_match_task(req
, task
, files
);
8885 spin_unlock_irq(&ctx
->inflight_lock
);
8889 static void io_uring_cancel_files(struct io_ring_ctx
*ctx
,
8890 struct task_struct
*task
,
8891 struct files_struct
*files
)
8893 while (!list_empty_careful(&ctx
->inflight_list
)) {
8894 struct io_task_cancel cancel
= { .task
= task
, .files
= files
};
8898 inflight
= io_uring_count_inflight(ctx
, task
, files
);
8902 io_wq_cancel_cb(ctx
->io_wq
, io_cancel_task_cb
, &cancel
, true);
8903 io_poll_remove_all(ctx
, task
, files
);
8904 io_kill_timeouts(ctx
, task
, files
);
8905 io_cqring_overflow_flush(ctx
, true, task
, files
);
8906 /* cancellations _may_ trigger task work */
8909 prepare_to_wait(&task
->io_uring
->wait
, &wait
,
8910 TASK_UNINTERRUPTIBLE
);
8911 if (inflight
== io_uring_count_inflight(ctx
, task
, files
))
8913 finish_wait(&task
->io_uring
->wait
, &wait
);
8917 static void __io_uring_cancel_task_requests(struct io_ring_ctx
*ctx
,
8918 struct task_struct
*task
)
8921 struct io_task_cancel cancel
= { .task
= task
, .files
= NULL
, };
8922 enum io_wq_cancel cret
;
8926 cret
= io_wq_cancel_cb(ctx
->io_wq
, io_cancel_task_cb
,
8928 ret
|= (cret
!= IO_WQ_CANCEL_NOTFOUND
);
8931 /* SQPOLL thread does its own polling */
8932 if (!(ctx
->flags
& IORING_SETUP_SQPOLL
)) {
8933 while (!list_empty_careful(&ctx
->iopoll_list
)) {
8934 io_iopoll_try_reap_events(ctx
);
8939 ret
|= io_poll_remove_all(ctx
, task
, NULL
);
8940 ret
|= io_kill_timeouts(ctx
, task
, NULL
);
8941 ret
|= io_run_task_work();
8948 static void io_disable_sqo_submit(struct io_ring_ctx
*ctx
)
8950 mutex_lock(&ctx
->uring_lock
);
8952 mutex_unlock(&ctx
->uring_lock
);
8954 /* make sure callers enter the ring to get error */
8956 io_ring_set_wakeup_flag(ctx
);
8960 * We need to iteratively cancel requests, in case a request has dependent
8961 * hard links. These persist even for failure of cancelations, hence keep
8962 * looping until none are found.
8964 static void io_uring_cancel_task_requests(struct io_ring_ctx
*ctx
,
8965 struct files_struct
*files
)
8967 struct task_struct
*task
= current
;
8969 if ((ctx
->flags
& IORING_SETUP_SQPOLL
) && ctx
->sq_data
) {
8970 io_disable_sqo_submit(ctx
);
8971 task
= ctx
->sq_data
->thread
;
8972 atomic_inc(&task
->io_uring
->in_idle
);
8973 io_sq_thread_park(ctx
->sq_data
);
8976 io_cancel_defer_files(ctx
, task
, files
);
8977 io_cqring_overflow_flush(ctx
, true, task
, files
);
8979 io_uring_cancel_files(ctx
, task
, files
);
8981 __io_uring_cancel_task_requests(ctx
, task
);
8983 if ((ctx
->flags
& IORING_SETUP_SQPOLL
) && ctx
->sq_data
) {
8984 atomic_dec(&task
->io_uring
->in_idle
);
8986 * If the files that are going away are the ones in the thread
8987 * identity, clear them out.
8989 if (task
->io_uring
->identity
->files
== files
)
8990 task
->io_uring
->identity
->files
= NULL
;
8991 io_sq_thread_unpark(ctx
->sq_data
);
8996 * Note that this task has used io_uring. We use it for cancelation purposes.
8998 static int io_uring_add_task_file(struct io_ring_ctx
*ctx
, struct file
*file
)
9000 struct io_uring_task
*tctx
= current
->io_uring
;
9003 if (unlikely(!tctx
)) {
9004 ret
= io_uring_alloc_task_context(current
);
9007 tctx
= current
->io_uring
;
9009 if (tctx
->last
!= file
) {
9010 void *old
= xa_load(&tctx
->xa
, (unsigned long)file
);
9014 ret
= xa_err(xa_store(&tctx
->xa
, (unsigned long)file
,
9025 * This is race safe in that the task itself is doing this, hence it
9026 * cannot be going through the exit/cancel paths at the same time.
9027 * This cannot be modified while exit/cancel is running.
9029 if (!tctx
->sqpoll
&& (ctx
->flags
& IORING_SETUP_SQPOLL
))
9030 tctx
->sqpoll
= true;
9036 * Remove this io_uring_file -> task mapping.
9038 static void io_uring_del_task_file(struct file
*file
)
9040 struct io_uring_task
*tctx
= current
->io_uring
;
9042 if (tctx
->last
== file
)
9044 file
= xa_erase(&tctx
->xa
, (unsigned long)file
);
9049 static void io_uring_remove_task_files(struct io_uring_task
*tctx
)
9052 unsigned long index
;
9054 xa_for_each(&tctx
->xa
, index
, file
)
9055 io_uring_del_task_file(file
);
9058 void __io_uring_files_cancel(struct files_struct
*files
)
9060 struct io_uring_task
*tctx
= current
->io_uring
;
9062 unsigned long index
;
9064 /* make sure overflow events are dropped */
9065 atomic_inc(&tctx
->in_idle
);
9066 xa_for_each(&tctx
->xa
, index
, file
)
9067 io_uring_cancel_task_requests(file
->private_data
, files
);
9068 atomic_dec(&tctx
->in_idle
);
9071 io_uring_remove_task_files(tctx
);
9074 static s64
tctx_inflight(struct io_uring_task
*tctx
)
9076 unsigned long index
;
9080 inflight
= percpu_counter_sum(&tctx
->inflight
);
9085 * If we have SQPOLL rings, then we need to iterate and find them, and
9086 * add the pending count for those.
9088 xa_for_each(&tctx
->xa
, index
, file
) {
9089 struct io_ring_ctx
*ctx
= file
->private_data
;
9091 if (ctx
->flags
& IORING_SETUP_SQPOLL
) {
9092 struct io_uring_task
*__tctx
= ctx
->sqo_task
->io_uring
;
9094 inflight
+= percpu_counter_sum(&__tctx
->inflight
);
9102 * Find any io_uring fd that this task has registered or done IO on, and cancel
9105 void __io_uring_task_cancel(void)
9107 struct io_uring_task
*tctx
= current
->io_uring
;
9111 /* make sure overflow events are dropped */
9112 atomic_inc(&tctx
->in_idle
);
9114 /* trigger io_disable_sqo_submit() */
9116 __io_uring_files_cancel(NULL
);
9119 /* read completions before cancelations */
9120 inflight
= tctx_inflight(tctx
);
9123 __io_uring_files_cancel(NULL
);
9125 prepare_to_wait(&tctx
->wait
, &wait
, TASK_UNINTERRUPTIBLE
);
9128 * If we've seen completions, retry without waiting. This
9129 * avoids a race where a completion comes in before we did
9130 * prepare_to_wait().
9132 if (inflight
== tctx_inflight(tctx
))
9134 finish_wait(&tctx
->wait
, &wait
);
9137 atomic_dec(&tctx
->in_idle
);
9139 io_uring_remove_task_files(tctx
);
9142 static int io_uring_flush(struct file
*file
, void *data
)
9144 struct io_uring_task
*tctx
= current
->io_uring
;
9145 struct io_ring_ctx
*ctx
= file
->private_data
;
9147 if (fatal_signal_pending(current
) || (current
->flags
& PF_EXITING
))
9148 io_uring_cancel_task_requests(ctx
, NULL
);
9153 /* we should have cancelled and erased it before PF_EXITING */
9154 WARN_ON_ONCE((current
->flags
& PF_EXITING
) &&
9155 xa_load(&tctx
->xa
, (unsigned long)file
));
9158 * fput() is pending, will be 2 if the only other ref is our potential
9159 * task file note. If the task is exiting, drop regardless of count.
9161 if (atomic_long_read(&file
->f_count
) != 2)
9164 if (ctx
->flags
& IORING_SETUP_SQPOLL
) {
9165 /* there is only one file note, which is owned by sqo_task */
9166 WARN_ON_ONCE(ctx
->sqo_task
!= current
&&
9167 xa_load(&tctx
->xa
, (unsigned long)file
));
9168 /* sqo_dead check is for when this happens after cancellation */
9169 WARN_ON_ONCE(ctx
->sqo_task
== current
&& !ctx
->sqo_dead
&&
9170 !xa_load(&tctx
->xa
, (unsigned long)file
));
9172 io_disable_sqo_submit(ctx
);
9175 if (!(ctx
->flags
& IORING_SETUP_SQPOLL
) || ctx
->sqo_task
== current
)
9176 io_uring_del_task_file(file
);
9180 static void *io_uring_validate_mmap_request(struct file
*file
,
9181 loff_t pgoff
, size_t sz
)
9183 struct io_ring_ctx
*ctx
= file
->private_data
;
9184 loff_t offset
= pgoff
<< PAGE_SHIFT
;
9189 case IORING_OFF_SQ_RING
:
9190 case IORING_OFF_CQ_RING
:
9193 case IORING_OFF_SQES
:
9197 return ERR_PTR(-EINVAL
);
9200 page
= virt_to_head_page(ptr
);
9201 if (sz
> page_size(page
))
9202 return ERR_PTR(-EINVAL
);
9209 static int io_uring_mmap(struct file
*file
, struct vm_area_struct
*vma
)
9211 size_t sz
= vma
->vm_end
- vma
->vm_start
;
9215 ptr
= io_uring_validate_mmap_request(file
, vma
->vm_pgoff
, sz
);
9217 return PTR_ERR(ptr
);
9219 pfn
= virt_to_phys(ptr
) >> PAGE_SHIFT
;
9220 return remap_pfn_range(vma
, vma
->vm_start
, pfn
, sz
, vma
->vm_page_prot
);
9223 #else /* !CONFIG_MMU */
9225 static int io_uring_mmap(struct file
*file
, struct vm_area_struct
*vma
)
9227 return vma
->vm_flags
& (VM_SHARED
| VM_MAYSHARE
) ? 0 : -EINVAL
;
9230 static unsigned int io_uring_nommu_mmap_capabilities(struct file
*file
)
9232 return NOMMU_MAP_DIRECT
| NOMMU_MAP_READ
| NOMMU_MAP_WRITE
;
9235 static unsigned long io_uring_nommu_get_unmapped_area(struct file
*file
,
9236 unsigned long addr
, unsigned long len
,
9237 unsigned long pgoff
, unsigned long flags
)
9241 ptr
= io_uring_validate_mmap_request(file
, pgoff
, len
);
9243 return PTR_ERR(ptr
);
9245 return (unsigned long) ptr
;
9248 #endif /* !CONFIG_MMU */
9250 static int io_sqpoll_wait_sq(struct io_ring_ctx
*ctx
)
9256 if (!io_sqring_full(ctx
))
9259 prepare_to_wait(&ctx
->sqo_sq_wait
, &wait
, TASK_INTERRUPTIBLE
);
9261 if (unlikely(ctx
->sqo_dead
)) {
9266 if (!io_sqring_full(ctx
))
9270 } while (!signal_pending(current
));
9272 finish_wait(&ctx
->sqo_sq_wait
, &wait
);
9277 static int io_get_ext_arg(unsigned flags
, const void __user
*argp
, size_t *argsz
,
9278 struct __kernel_timespec __user
**ts
,
9279 const sigset_t __user
**sig
)
9281 struct io_uring_getevents_arg arg
;
9284 * If EXT_ARG isn't set, then we have no timespec and the argp pointer
9285 * is just a pointer to the sigset_t.
9287 if (!(flags
& IORING_ENTER_EXT_ARG
)) {
9288 *sig
= (const sigset_t __user
*) argp
;
9294 * EXT_ARG is set - ensure we agree on the size of it and copy in our
9295 * timespec and sigset_t pointers if good.
9297 if (*argsz
!= sizeof(arg
))
9299 if (copy_from_user(&arg
, argp
, sizeof(arg
)))
9301 *sig
= u64_to_user_ptr(arg
.sigmask
);
9302 *argsz
= arg
.sigmask_sz
;
9303 *ts
= u64_to_user_ptr(arg
.ts
);
9307 SYSCALL_DEFINE6(io_uring_enter
, unsigned int, fd
, u32
, to_submit
,
9308 u32
, min_complete
, u32
, flags
, const void __user
*, argp
,
9311 struct io_ring_ctx
*ctx
;
9318 if (flags
& ~(IORING_ENTER_GETEVENTS
| IORING_ENTER_SQ_WAKEUP
|
9319 IORING_ENTER_SQ_WAIT
| IORING_ENTER_EXT_ARG
))
9327 if (f
.file
->f_op
!= &io_uring_fops
)
9331 ctx
= f
.file
->private_data
;
9332 if (!percpu_ref_tryget(&ctx
->refs
))
9336 if (ctx
->flags
& IORING_SETUP_R_DISABLED
)
9340 * For SQ polling, the thread will do all submissions and completions.
9341 * Just return the requested submit count, and wake the thread if
9345 if (ctx
->flags
& IORING_SETUP_SQPOLL
) {
9346 io_cqring_overflow_flush(ctx
, false, NULL
, NULL
);
9349 if (unlikely(ctx
->sqo_dead
))
9351 if (flags
& IORING_ENTER_SQ_WAKEUP
)
9352 wake_up(&ctx
->sq_data
->wait
);
9353 if (flags
& IORING_ENTER_SQ_WAIT
) {
9354 ret
= io_sqpoll_wait_sq(ctx
);
9358 submitted
= to_submit
;
9359 } else if (to_submit
) {
9360 ret
= io_uring_add_task_file(ctx
, f
.file
);
9363 mutex_lock(&ctx
->uring_lock
);
9364 submitted
= io_submit_sqes(ctx
, to_submit
);
9365 mutex_unlock(&ctx
->uring_lock
);
9367 if (submitted
!= to_submit
)
9370 if (flags
& IORING_ENTER_GETEVENTS
) {
9371 const sigset_t __user
*sig
;
9372 struct __kernel_timespec __user
*ts
;
9374 ret
= io_get_ext_arg(flags
, argp
, &argsz
, &ts
, &sig
);
9378 min_complete
= min(min_complete
, ctx
->cq_entries
);
9381 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
9382 * space applications don't need to do io completion events
9383 * polling again, they can rely on io_sq_thread to do polling
9384 * work, which can reduce cpu usage and uring_lock contention.
9386 if (ctx
->flags
& IORING_SETUP_IOPOLL
&&
9387 !(ctx
->flags
& IORING_SETUP_SQPOLL
)) {
9388 ret
= io_iopoll_check(ctx
, min_complete
);
9390 ret
= io_cqring_wait(ctx
, min_complete
, sig
, argsz
, ts
);
9395 percpu_ref_put(&ctx
->refs
);
9398 return submitted
? submitted
: ret
;
9401 #ifdef CONFIG_PROC_FS
9402 static int io_uring_show_cred(int id
, void *p
, void *data
)
9404 struct io_identity
*iod
= p
;
9405 const struct cred
*cred
= iod
->creds
;
9406 struct seq_file
*m
= data
;
9407 struct user_namespace
*uns
= seq_user_ns(m
);
9408 struct group_info
*gi
;
9413 seq_printf(m
, "%5d\n", id
);
9414 seq_put_decimal_ull(m
, "\tUid:\t", from_kuid_munged(uns
, cred
->uid
));
9415 seq_put_decimal_ull(m
, "\t\t", from_kuid_munged(uns
, cred
->euid
));
9416 seq_put_decimal_ull(m
, "\t\t", from_kuid_munged(uns
, cred
->suid
));
9417 seq_put_decimal_ull(m
, "\t\t", from_kuid_munged(uns
, cred
->fsuid
));
9418 seq_put_decimal_ull(m
, "\n\tGid:\t", from_kgid_munged(uns
, cred
->gid
));
9419 seq_put_decimal_ull(m
, "\t\t", from_kgid_munged(uns
, cred
->egid
));
9420 seq_put_decimal_ull(m
, "\t\t", from_kgid_munged(uns
, cred
->sgid
));
9421 seq_put_decimal_ull(m
, "\t\t", from_kgid_munged(uns
, cred
->fsgid
));
9422 seq_puts(m
, "\n\tGroups:\t");
9423 gi
= cred
->group_info
;
9424 for (g
= 0; g
< gi
->ngroups
; g
++) {
9425 seq_put_decimal_ull(m
, g
? " " : "",
9426 from_kgid_munged(uns
, gi
->gid
[g
]));
9428 seq_puts(m
, "\n\tCapEff:\t");
9429 cap
= cred
->cap_effective
;
9430 CAP_FOR_EACH_U32(__capi
)
9431 seq_put_hex_ll(m
, NULL
, cap
.cap
[CAP_LAST_U32
- __capi
], 8);
9436 static void __io_uring_show_fdinfo(struct io_ring_ctx
*ctx
, struct seq_file
*m
)
9438 struct io_sq_data
*sq
= NULL
;
9443 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
9444 * since fdinfo case grabs it in the opposite direction of normal use
9445 * cases. If we fail to get the lock, we just don't iterate any
9446 * structures that could be going away outside the io_uring mutex.
9448 has_lock
= mutex_trylock(&ctx
->uring_lock
);
9450 if (has_lock
&& (ctx
->flags
& IORING_SETUP_SQPOLL
))
9453 seq_printf(m
, "SqThread:\t%d\n", sq
? task_pid_nr(sq
->thread
) : -1);
9454 seq_printf(m
, "SqThreadCpu:\t%d\n", sq
? task_cpu(sq
->thread
) : -1);
9455 seq_printf(m
, "UserFiles:\t%u\n", ctx
->nr_user_files
);
9456 for (i
= 0; has_lock
&& i
< ctx
->nr_user_files
; i
++) {
9457 struct fixed_file_table
*table
;
9460 table
= &ctx
->file_data
->table
[i
>> IORING_FILE_TABLE_SHIFT
];
9461 f
= table
->files
[i
& IORING_FILE_TABLE_MASK
];
9463 seq_printf(m
, "%5u: %s\n", i
, file_dentry(f
)->d_iname
);
9465 seq_printf(m
, "%5u: <none>\n", i
);
9467 seq_printf(m
, "UserBufs:\t%u\n", ctx
->nr_user_bufs
);
9468 for (i
= 0; has_lock
&& i
< ctx
->nr_user_bufs
; i
++) {
9469 struct io_mapped_ubuf
*buf
= &ctx
->user_bufs
[i
];
9471 seq_printf(m
, "%5u: 0x%llx/%u\n", i
, buf
->ubuf
,
9472 (unsigned int) buf
->len
);
9474 if (has_lock
&& !idr_is_empty(&ctx
->personality_idr
)) {
9475 seq_printf(m
, "Personalities:\n");
9476 idr_for_each(&ctx
->personality_idr
, io_uring_show_cred
, m
);
9478 seq_printf(m
, "PollList:\n");
9479 spin_lock_irq(&ctx
->completion_lock
);
9480 for (i
= 0; i
< (1U << ctx
->cancel_hash_bits
); i
++) {
9481 struct hlist_head
*list
= &ctx
->cancel_hash
[i
];
9482 struct io_kiocb
*req
;
9484 hlist_for_each_entry(req
, list
, hash_node
)
9485 seq_printf(m
, " op=%d, task_works=%d\n", req
->opcode
,
9486 req
->task
->task_works
!= NULL
);
9488 spin_unlock_irq(&ctx
->completion_lock
);
9490 mutex_unlock(&ctx
->uring_lock
);
9493 static void io_uring_show_fdinfo(struct seq_file
*m
, struct file
*f
)
9495 struct io_ring_ctx
*ctx
= f
->private_data
;
9497 if (percpu_ref_tryget(&ctx
->refs
)) {
9498 __io_uring_show_fdinfo(ctx
, m
);
9499 percpu_ref_put(&ctx
->refs
);
9504 static const struct file_operations io_uring_fops
= {
9505 .release
= io_uring_release
,
9506 .flush
= io_uring_flush
,
9507 .mmap
= io_uring_mmap
,
9509 .get_unmapped_area
= io_uring_nommu_get_unmapped_area
,
9510 .mmap_capabilities
= io_uring_nommu_mmap_capabilities
,
9512 .poll
= io_uring_poll
,
9513 .fasync
= io_uring_fasync
,
9514 #ifdef CONFIG_PROC_FS
9515 .show_fdinfo
= io_uring_show_fdinfo
,
9519 static int io_allocate_scq_urings(struct io_ring_ctx
*ctx
,
9520 struct io_uring_params
*p
)
9522 struct io_rings
*rings
;
9523 size_t size
, sq_array_offset
;
9525 /* make sure these are sane, as we already accounted them */
9526 ctx
->sq_entries
= p
->sq_entries
;
9527 ctx
->cq_entries
= p
->cq_entries
;
9529 size
= rings_size(p
->sq_entries
, p
->cq_entries
, &sq_array_offset
);
9530 if (size
== SIZE_MAX
)
9533 rings
= io_mem_alloc(size
);
9538 ctx
->sq_array
= (u32
*)((char *)rings
+ sq_array_offset
);
9539 rings
->sq_ring_mask
= p
->sq_entries
- 1;
9540 rings
->cq_ring_mask
= p
->cq_entries
- 1;
9541 rings
->sq_ring_entries
= p
->sq_entries
;
9542 rings
->cq_ring_entries
= p
->cq_entries
;
9543 ctx
->sq_mask
= rings
->sq_ring_mask
;
9544 ctx
->cq_mask
= rings
->cq_ring_mask
;
9546 size
= array_size(sizeof(struct io_uring_sqe
), p
->sq_entries
);
9547 if (size
== SIZE_MAX
) {
9548 io_mem_free(ctx
->rings
);
9553 ctx
->sq_sqes
= io_mem_alloc(size
);
9554 if (!ctx
->sq_sqes
) {
9555 io_mem_free(ctx
->rings
);
9563 static int io_uring_install_fd(struct io_ring_ctx
*ctx
, struct file
*file
)
9567 fd
= get_unused_fd_flags(O_RDWR
| O_CLOEXEC
);
9571 ret
= io_uring_add_task_file(ctx
, file
);
9576 fd_install(fd
, file
);
9581 * Allocate an anonymous fd, this is what constitutes the application
9582 * visible backing of an io_uring instance. The application mmaps this
9583 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
9584 * we have to tie this fd to a socket for file garbage collection purposes.
9586 static struct file
*io_uring_get_file(struct io_ring_ctx
*ctx
)
9589 #if defined(CONFIG_UNIX)
9592 ret
= sock_create_kern(&init_net
, PF_UNIX
, SOCK_RAW
, IPPROTO_IP
,
9595 return ERR_PTR(ret
);
9598 file
= anon_inode_getfile("[io_uring]", &io_uring_fops
, ctx
,
9599 O_RDWR
| O_CLOEXEC
);
9600 #if defined(CONFIG_UNIX)
9602 sock_release(ctx
->ring_sock
);
9603 ctx
->ring_sock
= NULL
;
9605 ctx
->ring_sock
->file
= file
;
9611 static int io_uring_create(unsigned entries
, struct io_uring_params
*p
,
9612 struct io_uring_params __user
*params
)
9614 struct user_struct
*user
= NULL
;
9615 struct io_ring_ctx
*ctx
;
9622 if (entries
> IORING_MAX_ENTRIES
) {
9623 if (!(p
->flags
& IORING_SETUP_CLAMP
))
9625 entries
= IORING_MAX_ENTRIES
;
9629 * Use twice as many entries for the CQ ring. It's possible for the
9630 * application to drive a higher depth than the size of the SQ ring,
9631 * since the sqes are only used at submission time. This allows for
9632 * some flexibility in overcommitting a bit. If the application has
9633 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
9634 * of CQ ring entries manually.
9636 p
->sq_entries
= roundup_pow_of_two(entries
);
9637 if (p
->flags
& IORING_SETUP_CQSIZE
) {
9639 * If IORING_SETUP_CQSIZE is set, we do the same roundup
9640 * to a power-of-two, if it isn't already. We do NOT impose
9641 * any cq vs sq ring sizing.
9645 if (p
->cq_entries
> IORING_MAX_CQ_ENTRIES
) {
9646 if (!(p
->flags
& IORING_SETUP_CLAMP
))
9648 p
->cq_entries
= IORING_MAX_CQ_ENTRIES
;
9650 p
->cq_entries
= roundup_pow_of_two(p
->cq_entries
);
9651 if (p
->cq_entries
< p
->sq_entries
)
9654 p
->cq_entries
= 2 * p
->sq_entries
;
9657 user
= get_uid(current_user());
9658 limit_mem
= !capable(CAP_IPC_LOCK
);
9661 ret
= __io_account_mem(user
,
9662 ring_pages(p
->sq_entries
, p
->cq_entries
));
9669 ctx
= io_ring_ctx_alloc(p
);
9672 __io_unaccount_mem(user
, ring_pages(p
->sq_entries
,
9677 ctx
->compat
= in_compat_syscall();
9679 ctx
->creds
= get_current_cred();
9681 ctx
->loginuid
= current
->loginuid
;
9682 ctx
->sessionid
= current
->sessionid
;
9684 ctx
->sqo_task
= get_task_struct(current
);
9687 * This is just grabbed for accounting purposes. When a process exits,
9688 * the mm is exited and dropped before the files, hence we need to hang
9689 * on to this mm purely for the purposes of being able to unaccount
9690 * memory (locked/pinned vm). It's not used for anything else.
9692 mmgrab(current
->mm
);
9693 ctx
->mm_account
= current
->mm
;
9695 #ifdef CONFIG_BLK_CGROUP
9697 * The sq thread will belong to the original cgroup it was inited in.
9698 * If the cgroup goes offline (e.g. disabling the io controller), then
9699 * issued bios will be associated with the closest cgroup later in the
9703 ctx
->sqo_blkcg_css
= blkcg_css();
9704 ret
= css_tryget_online(ctx
->sqo_blkcg_css
);
9707 /* don't init against a dying cgroup, have the user try again */
9708 ctx
->sqo_blkcg_css
= NULL
;
9715 * Account memory _before_ installing the file descriptor. Once
9716 * the descriptor is installed, it can get closed at any time. Also
9717 * do this before hitting the general error path, as ring freeing
9718 * will un-account as well.
9720 io_account_mem(ctx
, ring_pages(p
->sq_entries
, p
->cq_entries
),
9722 ctx
->limit_mem
= limit_mem
;
9724 ret
= io_allocate_scq_urings(ctx
, p
);
9728 ret
= io_sq_offload_create(ctx
, p
);
9732 if (!(p
->flags
& IORING_SETUP_R_DISABLED
))
9733 io_sq_offload_start(ctx
);
9735 memset(&p
->sq_off
, 0, sizeof(p
->sq_off
));
9736 p
->sq_off
.head
= offsetof(struct io_rings
, sq
.head
);
9737 p
->sq_off
.tail
= offsetof(struct io_rings
, sq
.tail
);
9738 p
->sq_off
.ring_mask
= offsetof(struct io_rings
, sq_ring_mask
);
9739 p
->sq_off
.ring_entries
= offsetof(struct io_rings
, sq_ring_entries
);
9740 p
->sq_off
.flags
= offsetof(struct io_rings
, sq_flags
);
9741 p
->sq_off
.dropped
= offsetof(struct io_rings
, sq_dropped
);
9742 p
->sq_off
.array
= (char *)ctx
->sq_array
- (char *)ctx
->rings
;
9744 memset(&p
->cq_off
, 0, sizeof(p
->cq_off
));
9745 p
->cq_off
.head
= offsetof(struct io_rings
, cq
.head
);
9746 p
->cq_off
.tail
= offsetof(struct io_rings
, cq
.tail
);
9747 p
->cq_off
.ring_mask
= offsetof(struct io_rings
, cq_ring_mask
);
9748 p
->cq_off
.ring_entries
= offsetof(struct io_rings
, cq_ring_entries
);
9749 p
->cq_off
.overflow
= offsetof(struct io_rings
, cq_overflow
);
9750 p
->cq_off
.cqes
= offsetof(struct io_rings
, cqes
);
9751 p
->cq_off
.flags
= offsetof(struct io_rings
, cq_flags
);
9753 p
->features
= IORING_FEAT_SINGLE_MMAP
| IORING_FEAT_NODROP
|
9754 IORING_FEAT_SUBMIT_STABLE
| IORING_FEAT_RW_CUR_POS
|
9755 IORING_FEAT_CUR_PERSONALITY
| IORING_FEAT_FAST_POLL
|
9756 IORING_FEAT_POLL_32BITS
| IORING_FEAT_SQPOLL_NONFIXED
|
9757 IORING_FEAT_EXT_ARG
;
9759 if (copy_to_user(params
, p
, sizeof(*p
))) {
9764 file
= io_uring_get_file(ctx
);
9766 ret
= PTR_ERR(file
);
9771 * Install ring fd as the very last thing, so we don't risk someone
9772 * having closed it before we finish setup
9774 ret
= io_uring_install_fd(ctx
, file
);
9776 io_disable_sqo_submit(ctx
);
9777 /* fput will clean it up */
9782 trace_io_uring_create(ret
, ctx
, p
->sq_entries
, p
->cq_entries
, p
->flags
);
9785 io_disable_sqo_submit(ctx
);
9786 io_ring_ctx_wait_and_kill(ctx
);
9791 * Sets up an aio uring context, and returns the fd. Applications asks for a
9792 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9793 * params structure passed in.
9795 static long io_uring_setup(u32 entries
, struct io_uring_params __user
*params
)
9797 struct io_uring_params p
;
9800 if (copy_from_user(&p
, params
, sizeof(p
)))
9802 for (i
= 0; i
< ARRAY_SIZE(p
.resv
); i
++) {
9807 if (p
.flags
& ~(IORING_SETUP_IOPOLL
| IORING_SETUP_SQPOLL
|
9808 IORING_SETUP_SQ_AFF
| IORING_SETUP_CQSIZE
|
9809 IORING_SETUP_CLAMP
| IORING_SETUP_ATTACH_WQ
|
9810 IORING_SETUP_R_DISABLED
))
9813 return io_uring_create(entries
, &p
, params
);
9816 SYSCALL_DEFINE2(io_uring_setup
, u32
, entries
,
9817 struct io_uring_params __user
*, params
)
9819 return io_uring_setup(entries
, params
);
9822 static int io_probe(struct io_ring_ctx
*ctx
, void __user
*arg
, unsigned nr_args
)
9824 struct io_uring_probe
*p
;
9828 size
= struct_size(p
, ops
, nr_args
);
9829 if (size
== SIZE_MAX
)
9831 p
= kzalloc(size
, GFP_KERNEL
);
9836 if (copy_from_user(p
, arg
, size
))
9839 if (memchr_inv(p
, 0, size
))
9842 p
->last_op
= IORING_OP_LAST
- 1;
9843 if (nr_args
> IORING_OP_LAST
)
9844 nr_args
= IORING_OP_LAST
;
9846 for (i
= 0; i
< nr_args
; i
++) {
9848 if (!io_op_defs
[i
].not_supported
)
9849 p
->ops
[i
].flags
= IO_URING_OP_SUPPORTED
;
9854 if (copy_to_user(arg
, p
, size
))
9861 static int io_register_personality(struct io_ring_ctx
*ctx
)
9863 struct io_identity
*id
;
9866 id
= kmalloc(sizeof(*id
), GFP_KERNEL
);
9870 io_init_identity(id
);
9871 id
->creds
= get_current_cred();
9873 ret
= idr_alloc_cyclic(&ctx
->personality_idr
, id
, 1, USHRT_MAX
, GFP_KERNEL
);
9875 put_cred(id
->creds
);
9881 static int io_unregister_personality(struct io_ring_ctx
*ctx
, unsigned id
)
9883 struct io_identity
*iod
;
9885 iod
= idr_remove(&ctx
->personality_idr
, id
);
9887 put_cred(iod
->creds
);
9888 if (refcount_dec_and_test(&iod
->count
))
9896 static int io_register_restrictions(struct io_ring_ctx
*ctx
, void __user
*arg
,
9897 unsigned int nr_args
)
9899 struct io_uring_restriction
*res
;
9903 /* Restrictions allowed only if rings started disabled */
9904 if (!(ctx
->flags
& IORING_SETUP_R_DISABLED
))
9907 /* We allow only a single restrictions registration */
9908 if (ctx
->restrictions
.registered
)
9911 if (!arg
|| nr_args
> IORING_MAX_RESTRICTIONS
)
9914 size
= array_size(nr_args
, sizeof(*res
));
9915 if (size
== SIZE_MAX
)
9918 res
= memdup_user(arg
, size
);
9920 return PTR_ERR(res
);
9924 for (i
= 0; i
< nr_args
; i
++) {
9925 switch (res
[i
].opcode
) {
9926 case IORING_RESTRICTION_REGISTER_OP
:
9927 if (res
[i
].register_op
>= IORING_REGISTER_LAST
) {
9932 __set_bit(res
[i
].register_op
,
9933 ctx
->restrictions
.register_op
);
9935 case IORING_RESTRICTION_SQE_OP
:
9936 if (res
[i
].sqe_op
>= IORING_OP_LAST
) {
9941 __set_bit(res
[i
].sqe_op
, ctx
->restrictions
.sqe_op
);
9943 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED
:
9944 ctx
->restrictions
.sqe_flags_allowed
= res
[i
].sqe_flags
;
9946 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED
:
9947 ctx
->restrictions
.sqe_flags_required
= res
[i
].sqe_flags
;
9956 /* Reset all restrictions if an error happened */
9958 memset(&ctx
->restrictions
, 0, sizeof(ctx
->restrictions
));
9960 ctx
->restrictions
.registered
= true;
9966 static int io_register_enable_rings(struct io_ring_ctx
*ctx
)
9968 if (!(ctx
->flags
& IORING_SETUP_R_DISABLED
))
9971 if (ctx
->restrictions
.registered
)
9972 ctx
->restricted
= 1;
9974 ctx
->flags
&= ~IORING_SETUP_R_DISABLED
;
9976 io_sq_offload_start(ctx
);
9981 static bool io_register_op_must_quiesce(int op
)
9984 case IORING_UNREGISTER_FILES
:
9985 case IORING_REGISTER_FILES_UPDATE
:
9986 case IORING_REGISTER_PROBE
:
9987 case IORING_REGISTER_PERSONALITY
:
9988 case IORING_UNREGISTER_PERSONALITY
:
9995 static int __io_uring_register(struct io_ring_ctx
*ctx
, unsigned opcode
,
9996 void __user
*arg
, unsigned nr_args
)
9997 __releases(ctx
->uring_lock
)
9998 __acquires(ctx
->uring_lock
)
10003 * We're inside the ring mutex, if the ref is already dying, then
10004 * someone else killed the ctx or is already going through
10005 * io_uring_register().
10007 if (percpu_ref_is_dying(&ctx
->refs
))
10010 if (io_register_op_must_quiesce(opcode
)) {
10011 percpu_ref_kill(&ctx
->refs
);
10014 * Drop uring mutex before waiting for references to exit. If
10015 * another thread is currently inside io_uring_enter() it might
10016 * need to grab the uring_lock to make progress. If we hold it
10017 * here across the drain wait, then we can deadlock. It's safe
10018 * to drop the mutex here, since no new references will come in
10019 * after we've killed the percpu ref.
10021 mutex_unlock(&ctx
->uring_lock
);
10023 ret
= wait_for_completion_interruptible(&ctx
->ref_comp
);
10026 ret
= io_run_task_work_sig();
10031 mutex_lock(&ctx
->uring_lock
);
10034 percpu_ref_resurrect(&ctx
->refs
);
10039 if (ctx
->restricted
) {
10040 if (opcode
>= IORING_REGISTER_LAST
) {
10045 if (!test_bit(opcode
, ctx
->restrictions
.register_op
)) {
10052 case IORING_REGISTER_BUFFERS
:
10053 ret
= io_sqe_buffer_register(ctx
, arg
, nr_args
);
10055 case IORING_UNREGISTER_BUFFERS
:
10057 if (arg
|| nr_args
)
10059 ret
= io_sqe_buffer_unregister(ctx
);
10061 case IORING_REGISTER_FILES
:
10062 ret
= io_sqe_files_register(ctx
, arg
, nr_args
);
10064 case IORING_UNREGISTER_FILES
:
10066 if (arg
|| nr_args
)
10068 ret
= io_sqe_files_unregister(ctx
);
10070 case IORING_REGISTER_FILES_UPDATE
:
10071 ret
= io_sqe_files_update(ctx
, arg
, nr_args
);
10073 case IORING_REGISTER_EVENTFD
:
10074 case IORING_REGISTER_EVENTFD_ASYNC
:
10078 ret
= io_eventfd_register(ctx
, arg
);
10081 if (opcode
== IORING_REGISTER_EVENTFD_ASYNC
)
10082 ctx
->eventfd_async
= 1;
10084 ctx
->eventfd_async
= 0;
10086 case IORING_UNREGISTER_EVENTFD
:
10088 if (arg
|| nr_args
)
10090 ret
= io_eventfd_unregister(ctx
);
10092 case IORING_REGISTER_PROBE
:
10094 if (!arg
|| nr_args
> 256)
10096 ret
= io_probe(ctx
, arg
, nr_args
);
10098 case IORING_REGISTER_PERSONALITY
:
10100 if (arg
|| nr_args
)
10102 ret
= io_register_personality(ctx
);
10104 case IORING_UNREGISTER_PERSONALITY
:
10108 ret
= io_unregister_personality(ctx
, nr_args
);
10110 case IORING_REGISTER_ENABLE_RINGS
:
10112 if (arg
|| nr_args
)
10114 ret
= io_register_enable_rings(ctx
);
10116 case IORING_REGISTER_RESTRICTIONS
:
10117 ret
= io_register_restrictions(ctx
, arg
, nr_args
);
10125 if (io_register_op_must_quiesce(opcode
)) {
10126 /* bring the ctx back to life */
10127 percpu_ref_reinit(&ctx
->refs
);
10129 reinit_completion(&ctx
->ref_comp
);
10134 SYSCALL_DEFINE4(io_uring_register
, unsigned int, fd
, unsigned int, opcode
,
10135 void __user
*, arg
, unsigned int, nr_args
)
10137 struct io_ring_ctx
*ctx
;
10146 if (f
.file
->f_op
!= &io_uring_fops
)
10149 ctx
= f
.file
->private_data
;
10151 mutex_lock(&ctx
->uring_lock
);
10152 ret
= __io_uring_register(ctx
, opcode
, arg
, nr_args
);
10153 mutex_unlock(&ctx
->uring_lock
);
10154 trace_io_uring_register(ctx
, opcode
, ctx
->nr_user_files
, ctx
->nr_user_bufs
,
10155 ctx
->cq_ev_fd
!= NULL
, ret
);
10161 static int __init
io_uring_init(void)
10163 #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
10164 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
10165 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
10168 #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
10169 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
10170 BUILD_BUG_ON(sizeof(struct io_uring_sqe
) != 64);
10171 BUILD_BUG_SQE_ELEM(0, __u8
, opcode
);
10172 BUILD_BUG_SQE_ELEM(1, __u8
, flags
);
10173 BUILD_BUG_SQE_ELEM(2, __u16
, ioprio
);
10174 BUILD_BUG_SQE_ELEM(4, __s32
, fd
);
10175 BUILD_BUG_SQE_ELEM(8, __u64
, off
);
10176 BUILD_BUG_SQE_ELEM(8, __u64
, addr2
);
10177 BUILD_BUG_SQE_ELEM(16, __u64
, addr
);
10178 BUILD_BUG_SQE_ELEM(16, __u64
, splice_off_in
);
10179 BUILD_BUG_SQE_ELEM(24, __u32
, len
);
10180 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t
, rw_flags
);
10181 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags
);
10182 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32
, rw_flags
);
10183 BUILD_BUG_SQE_ELEM(28, __u32
, fsync_flags
);
10184 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16
, poll_events
);
10185 BUILD_BUG_SQE_ELEM(28, __u32
, poll32_events
);
10186 BUILD_BUG_SQE_ELEM(28, __u32
, sync_range_flags
);
10187 BUILD_BUG_SQE_ELEM(28, __u32
, msg_flags
);
10188 BUILD_BUG_SQE_ELEM(28, __u32
, timeout_flags
);
10189 BUILD_BUG_SQE_ELEM(28, __u32
, accept_flags
);
10190 BUILD_BUG_SQE_ELEM(28, __u32
, cancel_flags
);
10191 BUILD_BUG_SQE_ELEM(28, __u32
, open_flags
);
10192 BUILD_BUG_SQE_ELEM(28, __u32
, statx_flags
);
10193 BUILD_BUG_SQE_ELEM(28, __u32
, fadvise_advice
);
10194 BUILD_BUG_SQE_ELEM(28, __u32
, splice_flags
);
10195 BUILD_BUG_SQE_ELEM(32, __u64
, user_data
);
10196 BUILD_BUG_SQE_ELEM(40, __u16
, buf_index
);
10197 BUILD_BUG_SQE_ELEM(42, __u16
, personality
);
10198 BUILD_BUG_SQE_ELEM(44, __s32
, splice_fd_in
);
10200 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs
) != IORING_OP_LAST
);
10201 BUILD_BUG_ON(__REQ_F_LAST_BIT
>= 8 * sizeof(int));
10202 req_cachep
= KMEM_CACHE(io_kiocb
, SLAB_HWCACHE_ALIGN
| SLAB_PANIC
);
10205 __initcall(io_uring_init
);