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/mmu_context.h>
59 #include <linux/percpu.h>
60 #include <linux/slab.h>
61 #include <linux/kthread.h>
62 #include <linux/blkdev.h>
63 #include <linux/bvec.h>
64 #include <linux/net.h>
66 #include <net/af_unix.h>
68 #include <linux/anon_inodes.h>
69 #include <linux/sched/mm.h>
70 #include <linux/uaccess.h>
71 #include <linux/nospec.h>
72 #include <linux/sizes.h>
73 #include <linux/hugetlb.h>
74 #include <linux/highmem.h>
75 #include <linux/namei.h>
76 #include <linux/fsnotify.h>
77 #include <linux/fadvise.h>
78 #include <linux/eventpoll.h>
79 #include <linux/fs_struct.h>
80 #include <linux/splice.h>
81 #include <linux/task_work.h>
83 #define CREATE_TRACE_POINTS
84 #include <trace/events/io_uring.h>
86 #include <uapi/linux/io_uring.h>
91 #define IORING_MAX_ENTRIES 32768
92 #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
95 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
97 #define IORING_FILE_TABLE_SHIFT 9
98 #define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
99 #define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
100 #define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
103 u32 head ____cacheline_aligned_in_smp
;
104 u32 tail ____cacheline_aligned_in_smp
;
108 * This data is shared with the application through the mmap at offsets
109 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
111 * The offsets to the member fields are published through struct
112 * io_sqring_offsets when calling io_uring_setup.
116 * Head and tail offsets into the ring; the offsets need to be
117 * masked to get valid indices.
119 * The kernel controls head of the sq ring and the tail of the cq ring,
120 * and the application controls tail of the sq ring and the head of the
123 struct io_uring sq
, cq
;
125 * Bitmasks to apply to head and tail offsets (constant, equals
128 u32 sq_ring_mask
, cq_ring_mask
;
129 /* Ring sizes (constant, power of 2) */
130 u32 sq_ring_entries
, cq_ring_entries
;
132 * Number of invalid entries dropped by the kernel due to
133 * invalid index stored in array
135 * Written by the kernel, shouldn't be modified by the
136 * application (i.e. get number of "new events" by comparing to
139 * After a new SQ head value was read by the application this
140 * counter includes all submissions that were dropped reaching
141 * the new SQ head (and possibly more).
147 * Written by the kernel, shouldn't be modified by the
150 * The application needs a full memory barrier before checking
151 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
155 * Number of completion events lost because the queue was full;
156 * this should be avoided by the application by making sure
157 * there are not more requests pending than there is space in
158 * the completion queue.
160 * Written by the kernel, shouldn't be modified by the
161 * application (i.e. get number of "new events" by comparing to
164 * As completion events come in out of order this counter is not
165 * ordered with any other data.
169 * Ring buffer of completion events.
171 * The kernel writes completion events fresh every time they are
172 * produced, so the application is allowed to modify pending
175 struct io_uring_cqe cqes
[] ____cacheline_aligned_in_smp
;
178 struct io_mapped_ubuf
{
181 struct bio_vec
*bvec
;
182 unsigned int nr_bvecs
;
185 struct fixed_file_table
{
189 struct fixed_file_ref_node
{
190 struct percpu_ref refs
;
191 struct list_head node
;
192 struct list_head file_list
;
193 struct fixed_file_data
*file_data
;
194 struct work_struct work
;
197 struct fixed_file_data
{
198 struct fixed_file_table
*table
;
199 struct io_ring_ctx
*ctx
;
201 struct percpu_ref
*cur_refs
;
202 struct percpu_ref refs
;
203 struct completion done
;
204 struct list_head ref_list
;
209 struct list_head list
;
217 struct percpu_ref refs
;
218 } ____cacheline_aligned_in_smp
;
222 unsigned int compat
: 1;
223 unsigned int account_mem
: 1;
224 unsigned int cq_overflow_flushed
: 1;
225 unsigned int drain_next
: 1;
226 unsigned int eventfd_async
: 1;
229 * Ring buffer of indices into array of io_uring_sqe, which is
230 * mmapped by the application using the IORING_OFF_SQES offset.
232 * This indirection could e.g. be used to assign fixed
233 * io_uring_sqe entries to operations and only submit them to
234 * the queue when needed.
236 * The kernel modifies neither the indices array nor the entries
240 unsigned cached_sq_head
;
243 unsigned sq_thread_idle
;
244 unsigned cached_sq_dropped
;
245 atomic_t cached_cq_overflow
;
246 unsigned long sq_check_overflow
;
248 struct list_head defer_list
;
249 struct list_head timeout_list
;
250 struct list_head cq_overflow_list
;
252 wait_queue_head_t inflight_wait
;
253 struct io_uring_sqe
*sq_sqes
;
254 } ____cacheline_aligned_in_smp
;
256 struct io_rings
*rings
;
260 struct task_struct
*sqo_thread
; /* if using sq thread polling */
261 struct mm_struct
*sqo_mm
;
262 wait_queue_head_t sqo_wait
;
265 * If used, fixed file set. Writers must ensure that ->refs is dead,
266 * readers must ensure that ->refs is alive as long as the file* is
267 * used. Only updated through io_uring_register(2).
269 struct fixed_file_data
*file_data
;
270 unsigned nr_user_files
;
272 struct file
*ring_file
;
274 /* if used, fixed mapped user buffers */
275 unsigned nr_user_bufs
;
276 struct io_mapped_ubuf
*user_bufs
;
278 struct user_struct
*user
;
280 const struct cred
*creds
;
282 /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */
283 struct completion
*completions
;
285 /* if all else fails... */
286 struct io_kiocb
*fallback_req
;
288 #if defined(CONFIG_UNIX)
289 struct socket
*ring_sock
;
292 struct idr io_buffer_idr
;
294 struct idr personality_idr
;
297 unsigned cached_cq_tail
;
300 atomic_t cq_timeouts
;
301 unsigned long cq_check_overflow
;
302 struct wait_queue_head cq_wait
;
303 struct fasync_struct
*cq_fasync
;
304 struct eventfd_ctx
*cq_ev_fd
;
305 } ____cacheline_aligned_in_smp
;
308 struct mutex uring_lock
;
309 wait_queue_head_t wait
;
310 } ____cacheline_aligned_in_smp
;
313 spinlock_t completion_lock
;
316 * ->poll_list is protected by the ctx->uring_lock for
317 * io_uring instances that don't use IORING_SETUP_SQPOLL.
318 * For SQPOLL, only the single threaded io_sq_thread() will
319 * manipulate the list, hence no extra locking is needed there.
321 struct list_head poll_list
;
322 struct hlist_head
*cancel_hash
;
323 unsigned cancel_hash_bits
;
324 bool poll_multi_file
;
326 spinlock_t inflight_lock
;
327 struct list_head inflight_list
;
328 } ____cacheline_aligned_in_smp
;
330 struct work_struct exit_work
;
334 * First field must be the file pointer in all the
335 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
337 struct io_poll_iocb
{
340 struct wait_queue_head
*head
;
346 struct wait_queue_entry wait
;
351 struct file
*put_file
;
355 struct io_timeout_data
{
356 struct io_kiocb
*req
;
357 struct hrtimer timer
;
358 struct timespec64 ts
;
359 enum hrtimer_mode mode
;
364 struct sockaddr __user
*addr
;
365 int __user
*addr_len
;
367 unsigned long nofile
;
391 /* NOTE: kiocb has the file as the first member, so don't do it here */
399 struct sockaddr __user
*addr
;
406 struct user_msghdr __user
*msg
;
412 struct io_buffer
*kbuf
;
421 struct filename
*filename
;
422 struct statx __user
*buffer
;
424 unsigned long nofile
;
427 struct io_files_update
{
453 struct epoll_event event
;
457 struct file
*file_out
;
458 struct file
*file_in
;
465 struct io_provide_buf
{
474 struct io_async_connect
{
475 struct sockaddr_storage address
;
478 struct io_async_msghdr
{
479 struct iovec fast_iov
[UIO_FASTIOV
];
481 struct sockaddr __user
*uaddr
;
483 struct sockaddr_storage addr
;
487 struct iovec fast_iov
[UIO_FASTIOV
];
493 struct io_async_ctx
{
495 struct io_async_rw rw
;
496 struct io_async_msghdr msg
;
497 struct io_async_connect connect
;
498 struct io_timeout_data timeout
;
503 REQ_F_FIXED_FILE_BIT
= IOSQE_FIXED_FILE_BIT
,
504 REQ_F_IO_DRAIN_BIT
= IOSQE_IO_DRAIN_BIT
,
505 REQ_F_LINK_BIT
= IOSQE_IO_LINK_BIT
,
506 REQ_F_HARDLINK_BIT
= IOSQE_IO_HARDLINK_BIT
,
507 REQ_F_FORCE_ASYNC_BIT
= IOSQE_ASYNC_BIT
,
508 REQ_F_BUFFER_SELECT_BIT
= IOSQE_BUFFER_SELECT_BIT
,
516 REQ_F_IOPOLL_COMPLETED_BIT
,
517 REQ_F_LINK_TIMEOUT_BIT
,
521 REQ_F_TIMEOUT_NOSEQ_BIT
,
522 REQ_F_COMP_LOCKED_BIT
,
523 REQ_F_NEED_CLEANUP_BIT
,
526 REQ_F_BUFFER_SELECTED_BIT
,
527 REQ_F_NO_FILE_TABLE_BIT
,
529 /* not a real bit, just to check we're not overflowing the space */
535 REQ_F_FIXED_FILE
= BIT(REQ_F_FIXED_FILE_BIT
),
536 /* drain existing IO first */
537 REQ_F_IO_DRAIN
= BIT(REQ_F_IO_DRAIN_BIT
),
539 REQ_F_LINK
= BIT(REQ_F_LINK_BIT
),
540 /* doesn't sever on completion < 0 */
541 REQ_F_HARDLINK
= BIT(REQ_F_HARDLINK_BIT
),
543 REQ_F_FORCE_ASYNC
= BIT(REQ_F_FORCE_ASYNC_BIT
),
544 /* IOSQE_BUFFER_SELECT */
545 REQ_F_BUFFER_SELECT
= BIT(REQ_F_BUFFER_SELECT_BIT
),
548 REQ_F_LINK_HEAD
= BIT(REQ_F_LINK_HEAD_BIT
),
549 /* already grabbed next link */
550 REQ_F_LINK_NEXT
= BIT(REQ_F_LINK_NEXT_BIT
),
551 /* fail rest of links */
552 REQ_F_FAIL_LINK
= BIT(REQ_F_FAIL_LINK_BIT
),
553 /* on inflight list */
554 REQ_F_INFLIGHT
= BIT(REQ_F_INFLIGHT_BIT
),
555 /* read/write uses file position */
556 REQ_F_CUR_POS
= BIT(REQ_F_CUR_POS_BIT
),
557 /* must not punt to workers */
558 REQ_F_NOWAIT
= BIT(REQ_F_NOWAIT_BIT
),
559 /* polled IO has completed */
560 REQ_F_IOPOLL_COMPLETED
= BIT(REQ_F_IOPOLL_COMPLETED_BIT
),
561 /* has linked timeout */
562 REQ_F_LINK_TIMEOUT
= BIT(REQ_F_LINK_TIMEOUT_BIT
),
563 /* timeout request */
564 REQ_F_TIMEOUT
= BIT(REQ_F_TIMEOUT_BIT
),
566 REQ_F_ISREG
= BIT(REQ_F_ISREG_BIT
),
567 /* must be punted even for NONBLOCK */
568 REQ_F_MUST_PUNT
= BIT(REQ_F_MUST_PUNT_BIT
),
569 /* no timeout sequence */
570 REQ_F_TIMEOUT_NOSEQ
= BIT(REQ_F_TIMEOUT_NOSEQ_BIT
),
571 /* completion under lock */
572 REQ_F_COMP_LOCKED
= BIT(REQ_F_COMP_LOCKED_BIT
),
574 REQ_F_NEED_CLEANUP
= BIT(REQ_F_NEED_CLEANUP_BIT
),
575 /* in overflow list */
576 REQ_F_OVERFLOW
= BIT(REQ_F_OVERFLOW_BIT
),
577 /* already went through poll handler */
578 REQ_F_POLLED
= BIT(REQ_F_POLLED_BIT
),
579 /* buffer already selected */
580 REQ_F_BUFFER_SELECTED
= BIT(REQ_F_BUFFER_SELECTED_BIT
),
581 /* doesn't need file table for this request */
582 REQ_F_NO_FILE_TABLE
= BIT(REQ_F_NO_FILE_TABLE_BIT
),
586 struct io_poll_iocb poll
;
587 struct io_wq_work work
;
591 * NOTE! Each of the iocb union members has the file pointer
592 * as the first entry in their struct definition. So you can
593 * access the file pointer through any of the sub-structs,
594 * or directly as just 'ki_filp' in this struct.
600 struct io_poll_iocb poll
;
601 struct io_accept accept
;
603 struct io_cancel cancel
;
604 struct io_timeout timeout
;
605 struct io_connect connect
;
606 struct io_sr_msg sr_msg
;
608 struct io_close close
;
609 struct io_files_update files_update
;
610 struct io_fadvise fadvise
;
611 struct io_madvise madvise
;
612 struct io_epoll epoll
;
613 struct io_splice splice
;
614 struct io_provide_buf pbuf
;
617 struct io_async_ctx
*io
;
619 bool needs_fixed_file
;
624 struct io_ring_ctx
*ctx
;
625 struct list_head list
;
628 struct task_struct
*task
;
634 struct list_head link_list
;
636 struct list_head inflight_entry
;
638 struct percpu_ref
*fixed_file_refs
;
642 * Only commands that never go async can use the below fields,
643 * obviously. Right now only IORING_OP_POLL_ADD uses them, and
644 * async armed poll handlers for regular commands. The latter
645 * restore the work, if needed.
648 struct callback_head task_work
;
649 struct hlist_node hash_node
;
650 struct async_poll
*apoll
;
652 struct io_wq_work work
;
656 #define IO_PLUG_THRESHOLD 2
657 #define IO_IOPOLL_BATCH 8
659 struct io_submit_state
{
660 struct blk_plug plug
;
663 * io_kiocb alloc cache
665 void *reqs
[IO_IOPOLL_BATCH
];
666 unsigned int free_reqs
;
669 * File reference cache
673 unsigned int has_refs
;
674 unsigned int used_refs
;
675 unsigned int ios_left
;
679 /* needs req->io allocated for deferral/async */
680 unsigned async_ctx
: 1;
681 /* needs current->mm setup, does mm access */
682 unsigned needs_mm
: 1;
683 /* needs req->file assigned */
684 unsigned needs_file
: 1;
685 /* hash wq insertion if file is a regular file */
686 unsigned hash_reg_file
: 1;
687 /* unbound wq insertion if file is a non-regular file */
688 unsigned unbound_nonreg_file
: 1;
689 /* opcode is not supported by this kernel */
690 unsigned not_supported
: 1;
691 /* needs file table */
692 unsigned file_table
: 1;
694 unsigned needs_fs
: 1;
695 /* set if opcode supports polled "wait" */
697 unsigned pollout
: 1;
698 /* op supports buffer selection */
699 unsigned buffer_select
: 1;
702 static const struct io_op_def io_op_defs
[] = {
703 [IORING_OP_NOP
] = {},
704 [IORING_OP_READV
] = {
708 .unbound_nonreg_file
= 1,
712 [IORING_OP_WRITEV
] = {
717 .unbound_nonreg_file
= 1,
720 [IORING_OP_FSYNC
] = {
723 [IORING_OP_READ_FIXED
] = {
725 .unbound_nonreg_file
= 1,
728 [IORING_OP_WRITE_FIXED
] = {
731 .unbound_nonreg_file
= 1,
734 [IORING_OP_POLL_ADD
] = {
736 .unbound_nonreg_file
= 1,
738 [IORING_OP_POLL_REMOVE
] = {},
739 [IORING_OP_SYNC_FILE_RANGE
] = {
742 [IORING_OP_SENDMSG
] = {
746 .unbound_nonreg_file
= 1,
750 [IORING_OP_RECVMSG
] = {
754 .unbound_nonreg_file
= 1,
759 [IORING_OP_TIMEOUT
] = {
763 [IORING_OP_TIMEOUT_REMOVE
] = {},
764 [IORING_OP_ACCEPT
] = {
767 .unbound_nonreg_file
= 1,
771 [IORING_OP_ASYNC_CANCEL
] = {},
772 [IORING_OP_LINK_TIMEOUT
] = {
776 [IORING_OP_CONNECT
] = {
780 .unbound_nonreg_file
= 1,
783 [IORING_OP_FALLOCATE
] = {
786 [IORING_OP_OPENAT
] = {
790 [IORING_OP_CLOSE
] = {
794 [IORING_OP_FILES_UPDATE
] = {
798 [IORING_OP_STATX
] = {
806 .unbound_nonreg_file
= 1,
810 [IORING_OP_WRITE
] = {
813 .unbound_nonreg_file
= 1,
816 [IORING_OP_FADVISE
] = {
819 [IORING_OP_MADVISE
] = {
825 .unbound_nonreg_file
= 1,
831 .unbound_nonreg_file
= 1,
835 [IORING_OP_OPENAT2
] = {
839 [IORING_OP_EPOLL_CTL
] = {
840 .unbound_nonreg_file
= 1,
843 [IORING_OP_SPLICE
] = {
846 .unbound_nonreg_file
= 1,
848 [IORING_OP_PROVIDE_BUFFERS
] = {},
849 [IORING_OP_REMOVE_BUFFERS
] = {},
852 static void io_wq_submit_work(struct io_wq_work
**workptr
);
853 static void io_cqring_fill_event(struct io_kiocb
*req
, long res
);
854 static void io_put_req(struct io_kiocb
*req
);
855 static void __io_double_put_req(struct io_kiocb
*req
);
856 static struct io_kiocb
*io_prep_linked_timeout(struct io_kiocb
*req
);
857 static void io_queue_linked_timeout(struct io_kiocb
*req
);
858 static int __io_sqe_files_update(struct io_ring_ctx
*ctx
,
859 struct io_uring_files_update
*ip
,
861 static int io_grab_files(struct io_kiocb
*req
);
862 static void io_cleanup_req(struct io_kiocb
*req
);
863 static int io_file_get(struct io_submit_state
*state
, struct io_kiocb
*req
,
864 int fd
, struct file
**out_file
, bool fixed
);
865 static void __io_queue_sqe(struct io_kiocb
*req
,
866 const struct io_uring_sqe
*sqe
);
868 static struct kmem_cache
*req_cachep
;
870 static const struct file_operations io_uring_fops
;
872 struct sock
*io_uring_get_socket(struct file
*file
)
874 #if defined(CONFIG_UNIX)
875 if (file
->f_op
== &io_uring_fops
) {
876 struct io_ring_ctx
*ctx
= file
->private_data
;
878 return ctx
->ring_sock
->sk
;
883 EXPORT_SYMBOL(io_uring_get_socket
);
885 static void io_ring_ctx_ref_free(struct percpu_ref
*ref
)
887 struct io_ring_ctx
*ctx
= container_of(ref
, struct io_ring_ctx
, refs
);
889 complete(&ctx
->completions
[0]);
892 static struct io_ring_ctx
*io_ring_ctx_alloc(struct io_uring_params
*p
)
894 struct io_ring_ctx
*ctx
;
897 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
901 ctx
->fallback_req
= kmem_cache_alloc(req_cachep
, GFP_KERNEL
);
902 if (!ctx
->fallback_req
)
905 ctx
->completions
= kmalloc(2 * sizeof(struct completion
), GFP_KERNEL
);
906 if (!ctx
->completions
)
910 * Use 5 bits less than the max cq entries, that should give us around
911 * 32 entries per hash list if totally full and uniformly spread.
913 hash_bits
= ilog2(p
->cq_entries
);
917 ctx
->cancel_hash_bits
= hash_bits
;
918 ctx
->cancel_hash
= kmalloc((1U << hash_bits
) * sizeof(struct hlist_head
),
920 if (!ctx
->cancel_hash
)
922 __hash_init(ctx
->cancel_hash
, 1U << hash_bits
);
924 if (percpu_ref_init(&ctx
->refs
, io_ring_ctx_ref_free
,
925 PERCPU_REF_ALLOW_REINIT
, GFP_KERNEL
))
928 ctx
->flags
= p
->flags
;
929 init_waitqueue_head(&ctx
->sqo_wait
);
930 init_waitqueue_head(&ctx
->cq_wait
);
931 INIT_LIST_HEAD(&ctx
->cq_overflow_list
);
932 init_completion(&ctx
->completions
[0]);
933 init_completion(&ctx
->completions
[1]);
934 idr_init(&ctx
->io_buffer_idr
);
935 idr_init(&ctx
->personality_idr
);
936 mutex_init(&ctx
->uring_lock
);
937 init_waitqueue_head(&ctx
->wait
);
938 spin_lock_init(&ctx
->completion_lock
);
939 INIT_LIST_HEAD(&ctx
->poll_list
);
940 INIT_LIST_HEAD(&ctx
->defer_list
);
941 INIT_LIST_HEAD(&ctx
->timeout_list
);
942 init_waitqueue_head(&ctx
->inflight_wait
);
943 spin_lock_init(&ctx
->inflight_lock
);
944 INIT_LIST_HEAD(&ctx
->inflight_list
);
947 if (ctx
->fallback_req
)
948 kmem_cache_free(req_cachep
, ctx
->fallback_req
);
949 kfree(ctx
->completions
);
950 kfree(ctx
->cancel_hash
);
955 static inline bool __req_need_defer(struct io_kiocb
*req
)
957 struct io_ring_ctx
*ctx
= req
->ctx
;
959 return req
->sequence
!= ctx
->cached_cq_tail
960 + atomic_read(&ctx
->cached_cq_overflow
);
963 static inline bool req_need_defer(struct io_kiocb
*req
)
965 if (unlikely(req
->flags
& REQ_F_IO_DRAIN
))
966 return __req_need_defer(req
);
971 static struct io_kiocb
*io_get_deferred_req(struct io_ring_ctx
*ctx
)
973 struct io_kiocb
*req
;
975 req
= list_first_entry_or_null(&ctx
->defer_list
, struct io_kiocb
, list
);
976 if (req
&& !req_need_defer(req
)) {
977 list_del_init(&req
->list
);
984 static struct io_kiocb
*io_get_timeout_req(struct io_ring_ctx
*ctx
)
986 struct io_kiocb
*req
;
988 req
= list_first_entry_or_null(&ctx
->timeout_list
, struct io_kiocb
, list
);
990 if (req
->flags
& REQ_F_TIMEOUT_NOSEQ
)
992 if (!__req_need_defer(req
)) {
993 list_del_init(&req
->list
);
1001 static void __io_commit_cqring(struct io_ring_ctx
*ctx
)
1003 struct io_rings
*rings
= ctx
->rings
;
1005 /* order cqe stores with ring update */
1006 smp_store_release(&rings
->cq
.tail
, ctx
->cached_cq_tail
);
1008 if (wq_has_sleeper(&ctx
->cq_wait
)) {
1009 wake_up_interruptible(&ctx
->cq_wait
);
1010 kill_fasync(&ctx
->cq_fasync
, SIGIO
, POLL_IN
);
1014 static inline void io_req_work_grab_env(struct io_kiocb
*req
,
1015 const struct io_op_def
*def
)
1017 if (!req
->work
.mm
&& def
->needs_mm
) {
1018 mmgrab(current
->mm
);
1019 req
->work
.mm
= current
->mm
;
1021 if (!req
->work
.creds
)
1022 req
->work
.creds
= get_current_cred();
1023 if (!req
->work
.fs
&& def
->needs_fs
) {
1024 spin_lock(¤t
->fs
->lock
);
1025 if (!current
->fs
->in_exec
) {
1026 req
->work
.fs
= current
->fs
;
1027 req
->work
.fs
->users
++;
1029 req
->work
.flags
|= IO_WQ_WORK_CANCEL
;
1031 spin_unlock(¤t
->fs
->lock
);
1033 if (!req
->work
.task_pid
)
1034 req
->work
.task_pid
= task_pid_vnr(current
);
1037 static inline void io_req_work_drop_env(struct io_kiocb
*req
)
1040 mmdrop(req
->work
.mm
);
1041 req
->work
.mm
= NULL
;
1043 if (req
->work
.creds
) {
1044 put_cred(req
->work
.creds
);
1045 req
->work
.creds
= NULL
;
1048 struct fs_struct
*fs
= req
->work
.fs
;
1050 spin_lock(&req
->work
.fs
->lock
);
1053 spin_unlock(&req
->work
.fs
->lock
);
1059 static inline void io_prep_async_work(struct io_kiocb
*req
,
1060 struct io_kiocb
**link
)
1062 const struct io_op_def
*def
= &io_op_defs
[req
->opcode
];
1064 if (req
->flags
& REQ_F_ISREG
) {
1065 if (def
->hash_reg_file
)
1066 io_wq_hash_work(&req
->work
, file_inode(req
->file
));
1068 if (def
->unbound_nonreg_file
)
1069 req
->work
.flags
|= IO_WQ_WORK_UNBOUND
;
1072 io_req_work_grab_env(req
, def
);
1074 *link
= io_prep_linked_timeout(req
);
1077 static inline void io_queue_async_work(struct io_kiocb
*req
)
1079 struct io_ring_ctx
*ctx
= req
->ctx
;
1080 struct io_kiocb
*link
;
1082 io_prep_async_work(req
, &link
);
1084 trace_io_uring_queue_async_work(ctx
, io_wq_is_hashed(&req
->work
), req
,
1085 &req
->work
, req
->flags
);
1086 io_wq_enqueue(ctx
->io_wq
, &req
->work
);
1089 io_queue_linked_timeout(link
);
1092 static void io_kill_timeout(struct io_kiocb
*req
)
1096 ret
= hrtimer_try_to_cancel(&req
->io
->timeout
.timer
);
1098 atomic_inc(&req
->ctx
->cq_timeouts
);
1099 list_del_init(&req
->list
);
1100 req
->flags
|= REQ_F_COMP_LOCKED
;
1101 io_cqring_fill_event(req
, 0);
1106 static void io_kill_timeouts(struct io_ring_ctx
*ctx
)
1108 struct io_kiocb
*req
, *tmp
;
1110 spin_lock_irq(&ctx
->completion_lock
);
1111 list_for_each_entry_safe(req
, tmp
, &ctx
->timeout_list
, list
)
1112 io_kill_timeout(req
);
1113 spin_unlock_irq(&ctx
->completion_lock
);
1116 static void io_commit_cqring(struct io_ring_ctx
*ctx
)
1118 struct io_kiocb
*req
;
1120 while ((req
= io_get_timeout_req(ctx
)) != NULL
)
1121 io_kill_timeout(req
);
1123 __io_commit_cqring(ctx
);
1125 while ((req
= io_get_deferred_req(ctx
)) != NULL
)
1126 io_queue_async_work(req
);
1129 static struct io_uring_cqe
*io_get_cqring(struct io_ring_ctx
*ctx
)
1131 struct io_rings
*rings
= ctx
->rings
;
1134 tail
= ctx
->cached_cq_tail
;
1136 * writes to the cq entry need to come after reading head; the
1137 * control dependency is enough as we're using WRITE_ONCE to
1140 if (tail
- READ_ONCE(rings
->cq
.head
) == rings
->cq_ring_entries
)
1143 ctx
->cached_cq_tail
++;
1144 return &rings
->cqes
[tail
& ctx
->cq_mask
];
1147 static inline bool io_should_trigger_evfd(struct io_ring_ctx
*ctx
)
1151 if (!ctx
->eventfd_async
)
1153 return io_wq_current_is_worker();
1156 static void io_cqring_ev_posted(struct io_ring_ctx
*ctx
)
1158 if (waitqueue_active(&ctx
->wait
))
1159 wake_up(&ctx
->wait
);
1160 if (waitqueue_active(&ctx
->sqo_wait
))
1161 wake_up(&ctx
->sqo_wait
);
1162 if (io_should_trigger_evfd(ctx
))
1163 eventfd_signal(ctx
->cq_ev_fd
, 1);
1166 /* Returns true if there are no backlogged entries after the flush */
1167 static bool io_cqring_overflow_flush(struct io_ring_ctx
*ctx
, bool force
)
1169 struct io_rings
*rings
= ctx
->rings
;
1170 struct io_uring_cqe
*cqe
;
1171 struct io_kiocb
*req
;
1172 unsigned long flags
;
1176 if (list_empty_careful(&ctx
->cq_overflow_list
))
1178 if ((ctx
->cached_cq_tail
- READ_ONCE(rings
->cq
.head
) ==
1179 rings
->cq_ring_entries
))
1183 spin_lock_irqsave(&ctx
->completion_lock
, flags
);
1185 /* if force is set, the ring is going away. always drop after that */
1187 ctx
->cq_overflow_flushed
= 1;
1190 while (!list_empty(&ctx
->cq_overflow_list
)) {
1191 cqe
= io_get_cqring(ctx
);
1195 req
= list_first_entry(&ctx
->cq_overflow_list
, struct io_kiocb
,
1197 list_move(&req
->list
, &list
);
1198 req
->flags
&= ~REQ_F_OVERFLOW
;
1200 WRITE_ONCE(cqe
->user_data
, req
->user_data
);
1201 WRITE_ONCE(cqe
->res
, req
->result
);
1202 WRITE_ONCE(cqe
->flags
, req
->cflags
);
1204 WRITE_ONCE(ctx
->rings
->cq_overflow
,
1205 atomic_inc_return(&ctx
->cached_cq_overflow
));
1209 io_commit_cqring(ctx
);
1211 clear_bit(0, &ctx
->sq_check_overflow
);
1212 clear_bit(0, &ctx
->cq_check_overflow
);
1214 spin_unlock_irqrestore(&ctx
->completion_lock
, flags
);
1215 io_cqring_ev_posted(ctx
);
1217 while (!list_empty(&list
)) {
1218 req
= list_first_entry(&list
, struct io_kiocb
, list
);
1219 list_del(&req
->list
);
1226 static void __io_cqring_fill_event(struct io_kiocb
*req
, long res
, long cflags
)
1228 struct io_ring_ctx
*ctx
= req
->ctx
;
1229 struct io_uring_cqe
*cqe
;
1231 trace_io_uring_complete(ctx
, req
->user_data
, res
);
1234 * If we can't get a cq entry, userspace overflowed the
1235 * submission (by quite a lot). Increment the overflow count in
1238 cqe
= io_get_cqring(ctx
);
1240 WRITE_ONCE(cqe
->user_data
, req
->user_data
);
1241 WRITE_ONCE(cqe
->res
, res
);
1242 WRITE_ONCE(cqe
->flags
, cflags
);
1243 } else if (ctx
->cq_overflow_flushed
) {
1244 WRITE_ONCE(ctx
->rings
->cq_overflow
,
1245 atomic_inc_return(&ctx
->cached_cq_overflow
));
1247 if (list_empty(&ctx
->cq_overflow_list
)) {
1248 set_bit(0, &ctx
->sq_check_overflow
);
1249 set_bit(0, &ctx
->cq_check_overflow
);
1251 req
->flags
|= REQ_F_OVERFLOW
;
1252 refcount_inc(&req
->refs
);
1254 req
->cflags
= cflags
;
1255 list_add_tail(&req
->list
, &ctx
->cq_overflow_list
);
1259 static void io_cqring_fill_event(struct io_kiocb
*req
, long res
)
1261 __io_cqring_fill_event(req
, res
, 0);
1264 static void __io_cqring_add_event(struct io_kiocb
*req
, long res
, long cflags
)
1266 struct io_ring_ctx
*ctx
= req
->ctx
;
1267 unsigned long flags
;
1269 spin_lock_irqsave(&ctx
->completion_lock
, flags
);
1270 __io_cqring_fill_event(req
, res
, cflags
);
1271 io_commit_cqring(ctx
);
1272 spin_unlock_irqrestore(&ctx
->completion_lock
, flags
);
1274 io_cqring_ev_posted(ctx
);
1277 static void io_cqring_add_event(struct io_kiocb
*req
, long res
)
1279 __io_cqring_add_event(req
, res
, 0);
1282 static inline bool io_is_fallback_req(struct io_kiocb
*req
)
1284 return req
== (struct io_kiocb
*)
1285 ((unsigned long) req
->ctx
->fallback_req
& ~1UL);
1288 static struct io_kiocb
*io_get_fallback_req(struct io_ring_ctx
*ctx
)
1290 struct io_kiocb
*req
;
1292 req
= ctx
->fallback_req
;
1293 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx
->fallback_req
))
1299 static struct io_kiocb
*io_alloc_req(struct io_ring_ctx
*ctx
,
1300 struct io_submit_state
*state
)
1302 gfp_t gfp
= GFP_KERNEL
| __GFP_NOWARN
;
1303 struct io_kiocb
*req
;
1306 req
= kmem_cache_alloc(req_cachep
, gfp
);
1309 } else if (!state
->free_reqs
) {
1313 sz
= min_t(size_t, state
->ios_left
, ARRAY_SIZE(state
->reqs
));
1314 ret
= kmem_cache_alloc_bulk(req_cachep
, gfp
, sz
, state
->reqs
);
1317 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1318 * retry single alloc to be on the safe side.
1320 if (unlikely(ret
<= 0)) {
1321 state
->reqs
[0] = kmem_cache_alloc(req_cachep
, gfp
);
1322 if (!state
->reqs
[0])
1326 state
->free_reqs
= ret
- 1;
1327 req
= state
->reqs
[ret
- 1];
1330 req
= state
->reqs
[state
->free_reqs
];
1335 return io_get_fallback_req(ctx
);
1338 static inline void io_put_file(struct io_kiocb
*req
, struct file
*file
,
1342 percpu_ref_put(req
->fixed_file_refs
);
1347 static void __io_req_aux_free(struct io_kiocb
*req
)
1349 if (req
->flags
& REQ_F_NEED_CLEANUP
)
1350 io_cleanup_req(req
);
1354 io_put_file(req
, req
->file
, (req
->flags
& REQ_F_FIXED_FILE
));
1356 put_task_struct(req
->task
);
1358 io_req_work_drop_env(req
);
1361 static void __io_free_req(struct io_kiocb
*req
)
1363 __io_req_aux_free(req
);
1365 if (req
->flags
& REQ_F_INFLIGHT
) {
1366 struct io_ring_ctx
*ctx
= req
->ctx
;
1367 unsigned long flags
;
1369 spin_lock_irqsave(&ctx
->inflight_lock
, flags
);
1370 list_del(&req
->inflight_entry
);
1371 if (waitqueue_active(&ctx
->inflight_wait
))
1372 wake_up(&ctx
->inflight_wait
);
1373 spin_unlock_irqrestore(&ctx
->inflight_lock
, flags
);
1376 percpu_ref_put(&req
->ctx
->refs
);
1377 if (likely(!io_is_fallback_req(req
)))
1378 kmem_cache_free(req_cachep
, req
);
1380 clear_bit_unlock(0, (unsigned long *) &req
->ctx
->fallback_req
);
1384 void *reqs
[IO_IOPOLL_BATCH
];
1389 static void io_free_req_many(struct io_ring_ctx
*ctx
, struct req_batch
*rb
)
1393 if (rb
->need_iter
) {
1394 int i
, inflight
= 0;
1395 unsigned long flags
;
1397 for (i
= 0; i
< rb
->to_free
; i
++) {
1398 struct io_kiocb
*req
= rb
->reqs
[i
];
1400 if (req
->flags
& REQ_F_INFLIGHT
)
1402 __io_req_aux_free(req
);
1407 spin_lock_irqsave(&ctx
->inflight_lock
, flags
);
1408 for (i
= 0; i
< rb
->to_free
; i
++) {
1409 struct io_kiocb
*req
= rb
->reqs
[i
];
1411 if (req
->flags
& REQ_F_INFLIGHT
) {
1412 list_del(&req
->inflight_entry
);
1417 spin_unlock_irqrestore(&ctx
->inflight_lock
, flags
);
1419 if (waitqueue_active(&ctx
->inflight_wait
))
1420 wake_up(&ctx
->inflight_wait
);
1423 kmem_cache_free_bulk(req_cachep
, rb
->to_free
, rb
->reqs
);
1424 percpu_ref_put_many(&ctx
->refs
, rb
->to_free
);
1425 rb
->to_free
= rb
->need_iter
= 0;
1428 static bool io_link_cancel_timeout(struct io_kiocb
*req
)
1430 struct io_ring_ctx
*ctx
= req
->ctx
;
1433 ret
= hrtimer_try_to_cancel(&req
->io
->timeout
.timer
);
1435 io_cqring_fill_event(req
, -ECANCELED
);
1436 io_commit_cqring(ctx
);
1437 req
->flags
&= ~REQ_F_LINK_HEAD
;
1445 static void io_req_link_next(struct io_kiocb
*req
, struct io_kiocb
**nxtptr
)
1447 struct io_ring_ctx
*ctx
= req
->ctx
;
1448 bool wake_ev
= false;
1450 /* Already got next link */
1451 if (req
->flags
& REQ_F_LINK_NEXT
)
1455 * The list should never be empty when we are called here. But could
1456 * potentially happen if the chain is messed up, check to be on the
1459 while (!list_empty(&req
->link_list
)) {
1460 struct io_kiocb
*nxt
= list_first_entry(&req
->link_list
,
1461 struct io_kiocb
, link_list
);
1463 if (unlikely((req
->flags
& REQ_F_LINK_TIMEOUT
) &&
1464 (nxt
->flags
& REQ_F_TIMEOUT
))) {
1465 list_del_init(&nxt
->link_list
);
1466 wake_ev
|= io_link_cancel_timeout(nxt
);
1467 req
->flags
&= ~REQ_F_LINK_TIMEOUT
;
1471 list_del_init(&req
->link_list
);
1472 if (!list_empty(&nxt
->link_list
))
1473 nxt
->flags
|= REQ_F_LINK_HEAD
;
1478 req
->flags
|= REQ_F_LINK_NEXT
;
1480 io_cqring_ev_posted(ctx
);
1484 * Called if REQ_F_LINK_HEAD is set, and we fail the head request
1486 static void io_fail_links(struct io_kiocb
*req
)
1488 struct io_ring_ctx
*ctx
= req
->ctx
;
1489 unsigned long flags
;
1491 spin_lock_irqsave(&ctx
->completion_lock
, flags
);
1493 while (!list_empty(&req
->link_list
)) {
1494 struct io_kiocb
*link
= list_first_entry(&req
->link_list
,
1495 struct io_kiocb
, link_list
);
1497 list_del_init(&link
->link_list
);
1498 trace_io_uring_fail_link(req
, link
);
1500 if ((req
->flags
& REQ_F_LINK_TIMEOUT
) &&
1501 link
->opcode
== IORING_OP_LINK_TIMEOUT
) {
1502 io_link_cancel_timeout(link
);
1504 io_cqring_fill_event(link
, -ECANCELED
);
1505 __io_double_put_req(link
);
1507 req
->flags
&= ~REQ_F_LINK_TIMEOUT
;
1510 io_commit_cqring(ctx
);
1511 spin_unlock_irqrestore(&ctx
->completion_lock
, flags
);
1512 io_cqring_ev_posted(ctx
);
1515 static void io_req_find_next(struct io_kiocb
*req
, struct io_kiocb
**nxt
)
1517 if (likely(!(req
->flags
& REQ_F_LINK_HEAD
)))
1521 * If LINK is set, we have dependent requests in this chain. If we
1522 * didn't fail this request, queue the first one up, moving any other
1523 * dependencies to the next request. In case of failure, fail the rest
1526 if (req
->flags
& REQ_F_FAIL_LINK
) {
1528 } else if ((req
->flags
& (REQ_F_LINK_TIMEOUT
| REQ_F_COMP_LOCKED
)) ==
1529 REQ_F_LINK_TIMEOUT
) {
1530 struct io_ring_ctx
*ctx
= req
->ctx
;
1531 unsigned long flags
;
1534 * If this is a timeout link, we could be racing with the
1535 * timeout timer. Grab the completion lock for this case to
1536 * protect against that.
1538 spin_lock_irqsave(&ctx
->completion_lock
, flags
);
1539 io_req_link_next(req
, nxt
);
1540 spin_unlock_irqrestore(&ctx
->completion_lock
, flags
);
1542 io_req_link_next(req
, nxt
);
1546 static void io_free_req(struct io_kiocb
*req
)
1548 struct io_kiocb
*nxt
= NULL
;
1550 io_req_find_next(req
, &nxt
);
1554 io_queue_async_work(nxt
);
1557 static void io_link_work_cb(struct io_wq_work
**workptr
)
1559 struct io_kiocb
*req
= container_of(*workptr
, struct io_kiocb
, work
);
1560 struct io_kiocb
*link
;
1562 link
= list_first_entry(&req
->link_list
, struct io_kiocb
, link_list
);
1563 io_queue_linked_timeout(link
);
1564 io_wq_submit_work(workptr
);
1567 static void io_wq_assign_next(struct io_wq_work
**workptr
, struct io_kiocb
*nxt
)
1569 struct io_kiocb
*link
;
1570 const struct io_op_def
*def
= &io_op_defs
[nxt
->opcode
];
1572 if ((nxt
->flags
& REQ_F_ISREG
) && def
->hash_reg_file
)
1573 io_wq_hash_work(&nxt
->work
, file_inode(nxt
->file
));
1575 *workptr
= &nxt
->work
;
1576 link
= io_prep_linked_timeout(nxt
);
1578 nxt
->work
.func
= io_link_work_cb
;
1582 * Drop reference to request, return next in chain (if there is one) if this
1583 * was the last reference to this request.
1585 __attribute__((nonnull
))
1586 static void io_put_req_find_next(struct io_kiocb
*req
, struct io_kiocb
**nxtptr
)
1588 if (refcount_dec_and_test(&req
->refs
)) {
1589 io_req_find_next(req
, nxtptr
);
1594 static void io_put_req(struct io_kiocb
*req
)
1596 if (refcount_dec_and_test(&req
->refs
))
1600 static void io_steal_work(struct io_kiocb
*req
,
1601 struct io_wq_work
**workptr
)
1604 * It's in an io-wq worker, so there always should be at least
1605 * one reference, which will be dropped in io_put_work() just
1606 * after the current handler returns.
1608 * It also means, that if the counter dropped to 1, then there is
1609 * no asynchronous users left, so it's safe to steal the next work.
1611 if (refcount_read(&req
->refs
) == 1) {
1612 struct io_kiocb
*nxt
= NULL
;
1614 io_req_find_next(req
, &nxt
);
1616 io_wq_assign_next(workptr
, nxt
);
1621 * Must only be used if we don't need to care about links, usually from
1622 * within the completion handling itself.
1624 static void __io_double_put_req(struct io_kiocb
*req
)
1626 /* drop both submit and complete references */
1627 if (refcount_sub_and_test(2, &req
->refs
))
1631 static void io_double_put_req(struct io_kiocb
*req
)
1633 /* drop both submit and complete references */
1634 if (refcount_sub_and_test(2, &req
->refs
))
1638 static unsigned io_cqring_events(struct io_ring_ctx
*ctx
, bool noflush
)
1640 struct io_rings
*rings
= ctx
->rings
;
1642 if (test_bit(0, &ctx
->cq_check_overflow
)) {
1644 * noflush == true is from the waitqueue handler, just ensure
1645 * we wake up the task, and the next invocation will flush the
1646 * entries. We cannot safely to it from here.
1648 if (noflush
&& !list_empty(&ctx
->cq_overflow_list
))
1651 io_cqring_overflow_flush(ctx
, false);
1654 /* See comment at the top of this file */
1656 return ctx
->cached_cq_tail
- READ_ONCE(rings
->cq
.head
);
1659 static inline unsigned int io_sqring_entries(struct io_ring_ctx
*ctx
)
1661 struct io_rings
*rings
= ctx
->rings
;
1663 /* make sure SQ entry isn't read before tail */
1664 return smp_load_acquire(&rings
->sq
.tail
) - ctx
->cached_sq_head
;
1667 static inline bool io_req_multi_free(struct req_batch
*rb
, struct io_kiocb
*req
)
1669 if ((req
->flags
& REQ_F_LINK_HEAD
) || io_is_fallback_req(req
))
1672 if (req
->file
|| req
->io
)
1675 rb
->reqs
[rb
->to_free
++] = req
;
1676 if (unlikely(rb
->to_free
== ARRAY_SIZE(rb
->reqs
)))
1677 io_free_req_many(req
->ctx
, rb
);
1681 static int io_put_kbuf(struct io_kiocb
*req
)
1683 struct io_buffer
*kbuf
;
1686 kbuf
= (struct io_buffer
*) (unsigned long) req
->rw
.addr
;
1687 cflags
= kbuf
->bid
<< IORING_CQE_BUFFER_SHIFT
;
1688 cflags
|= IORING_CQE_F_BUFFER
;
1695 * Find and free completed poll iocbs
1697 static void io_iopoll_complete(struct io_ring_ctx
*ctx
, unsigned int *nr_events
,
1698 struct list_head
*done
)
1700 struct req_batch rb
;
1701 struct io_kiocb
*req
;
1703 rb
.to_free
= rb
.need_iter
= 0;
1704 while (!list_empty(done
)) {
1707 req
= list_first_entry(done
, struct io_kiocb
, list
);
1708 list_del(&req
->list
);
1710 if (req
->flags
& REQ_F_BUFFER_SELECTED
)
1711 cflags
= io_put_kbuf(req
);
1713 __io_cqring_fill_event(req
, req
->result
, cflags
);
1716 if (refcount_dec_and_test(&req
->refs
) &&
1717 !io_req_multi_free(&rb
, req
))
1721 io_commit_cqring(ctx
);
1722 if (ctx
->flags
& IORING_SETUP_SQPOLL
)
1723 io_cqring_ev_posted(ctx
);
1724 io_free_req_many(ctx
, &rb
);
1727 static void io_iopoll_queue(struct list_head
*again
)
1729 struct io_kiocb
*req
;
1732 req
= list_first_entry(again
, struct io_kiocb
, list
);
1733 list_del(&req
->list
);
1734 refcount_inc(&req
->refs
);
1735 io_queue_async_work(req
);
1736 } while (!list_empty(again
));
1739 static int io_do_iopoll(struct io_ring_ctx
*ctx
, unsigned int *nr_events
,
1742 struct io_kiocb
*req
, *tmp
;
1749 * Only spin for completions if we don't have multiple devices hanging
1750 * off our complete list, and we're under the requested amount.
1752 spin
= !ctx
->poll_multi_file
&& *nr_events
< min
;
1755 list_for_each_entry_safe(req
, tmp
, &ctx
->poll_list
, list
) {
1756 struct kiocb
*kiocb
= &req
->rw
.kiocb
;
1759 * Move completed and retryable entries to our local lists.
1760 * If we find a request that requires polling, break out
1761 * and complete those lists first, if we have entries there.
1763 if (req
->flags
& REQ_F_IOPOLL_COMPLETED
) {
1764 list_move_tail(&req
->list
, &done
);
1767 if (!list_empty(&done
))
1770 if (req
->result
== -EAGAIN
) {
1771 list_move_tail(&req
->list
, &again
);
1774 if (!list_empty(&again
))
1777 ret
= kiocb
->ki_filp
->f_op
->iopoll(kiocb
, spin
);
1786 if (!list_empty(&done
))
1787 io_iopoll_complete(ctx
, nr_events
, &done
);
1789 if (!list_empty(&again
))
1790 io_iopoll_queue(&again
);
1796 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
1797 * non-spinning poll check - we'll still enter the driver poll loop, but only
1798 * as a non-spinning completion check.
1800 static int io_iopoll_getevents(struct io_ring_ctx
*ctx
, unsigned int *nr_events
,
1803 while (!list_empty(&ctx
->poll_list
) && !need_resched()) {
1806 ret
= io_do_iopoll(ctx
, nr_events
, min
);
1809 if (!min
|| *nr_events
>= min
)
1817 * We can't just wait for polled events to come to us, we have to actively
1818 * find and complete them.
1820 static void io_iopoll_reap_events(struct io_ring_ctx
*ctx
)
1822 if (!(ctx
->flags
& IORING_SETUP_IOPOLL
))
1825 mutex_lock(&ctx
->uring_lock
);
1826 while (!list_empty(&ctx
->poll_list
)) {
1827 unsigned int nr_events
= 0;
1829 io_iopoll_getevents(ctx
, &nr_events
, 1);
1832 * Ensure we allow local-to-the-cpu processing to take place,
1833 * in this case we need to ensure that we reap all events.
1837 mutex_unlock(&ctx
->uring_lock
);
1840 static int io_iopoll_check(struct io_ring_ctx
*ctx
, unsigned *nr_events
,
1843 int iters
= 0, ret
= 0;
1846 * We disallow the app entering submit/complete with polling, but we
1847 * still need to lock the ring to prevent racing with polled issue
1848 * that got punted to a workqueue.
1850 mutex_lock(&ctx
->uring_lock
);
1855 * Don't enter poll loop if we already have events pending.
1856 * If we do, we can potentially be spinning for commands that
1857 * already triggered a CQE (eg in error).
1859 if (io_cqring_events(ctx
, false))
1863 * If a submit got punted to a workqueue, we can have the
1864 * application entering polling for a command before it gets
1865 * issued. That app will hold the uring_lock for the duration
1866 * of the poll right here, so we need to take a breather every
1867 * now and then to ensure that the issue has a chance to add
1868 * the poll to the issued list. Otherwise we can spin here
1869 * forever, while the workqueue is stuck trying to acquire the
1872 if (!(++iters
& 7)) {
1873 mutex_unlock(&ctx
->uring_lock
);
1874 mutex_lock(&ctx
->uring_lock
);
1877 if (*nr_events
< min
)
1878 tmin
= min
- *nr_events
;
1880 ret
= io_iopoll_getevents(ctx
, nr_events
, tmin
);
1884 } while (min
&& !*nr_events
&& !need_resched());
1886 mutex_unlock(&ctx
->uring_lock
);
1890 static void kiocb_end_write(struct io_kiocb
*req
)
1893 * Tell lockdep we inherited freeze protection from submission
1896 if (req
->flags
& REQ_F_ISREG
) {
1897 struct inode
*inode
= file_inode(req
->file
);
1899 __sb_writers_acquired(inode
->i_sb
, SB_FREEZE_WRITE
);
1901 file_end_write(req
->file
);
1904 static inline void req_set_fail_links(struct io_kiocb
*req
)
1906 if ((req
->flags
& (REQ_F_LINK
| REQ_F_HARDLINK
)) == REQ_F_LINK
)
1907 req
->flags
|= REQ_F_FAIL_LINK
;
1910 static void io_complete_rw_common(struct kiocb
*kiocb
, long res
)
1912 struct io_kiocb
*req
= container_of(kiocb
, struct io_kiocb
, rw
.kiocb
);
1915 if (kiocb
->ki_flags
& IOCB_WRITE
)
1916 kiocb_end_write(req
);
1918 if (res
!= req
->result
)
1919 req_set_fail_links(req
);
1920 if (req
->flags
& REQ_F_BUFFER_SELECTED
)
1921 cflags
= io_put_kbuf(req
);
1922 __io_cqring_add_event(req
, res
, cflags
);
1925 static void io_complete_rw(struct kiocb
*kiocb
, long res
, long res2
)
1927 struct io_kiocb
*req
= container_of(kiocb
, struct io_kiocb
, rw
.kiocb
);
1929 io_complete_rw_common(kiocb
, res
);
1933 static void io_complete_rw_iopoll(struct kiocb
*kiocb
, long res
, long res2
)
1935 struct io_kiocb
*req
= container_of(kiocb
, struct io_kiocb
, rw
.kiocb
);
1937 if (kiocb
->ki_flags
& IOCB_WRITE
)
1938 kiocb_end_write(req
);
1940 if (res
!= req
->result
)
1941 req_set_fail_links(req
);
1944 req
->flags
|= REQ_F_IOPOLL_COMPLETED
;
1948 * After the iocb has been issued, it's safe to be found on the poll list.
1949 * Adding the kiocb to the list AFTER submission ensures that we don't
1950 * find it from a io_iopoll_getevents() thread before the issuer is done
1951 * accessing the kiocb cookie.
1953 static void io_iopoll_req_issued(struct io_kiocb
*req
)
1955 struct io_ring_ctx
*ctx
= req
->ctx
;
1958 * Track whether we have multiple files in our lists. This will impact
1959 * how we do polling eventually, not spinning if we're on potentially
1960 * different devices.
1962 if (list_empty(&ctx
->poll_list
)) {
1963 ctx
->poll_multi_file
= false;
1964 } else if (!ctx
->poll_multi_file
) {
1965 struct io_kiocb
*list_req
;
1967 list_req
= list_first_entry(&ctx
->poll_list
, struct io_kiocb
,
1969 if (list_req
->file
!= req
->file
)
1970 ctx
->poll_multi_file
= true;
1974 * For fast devices, IO may have already completed. If it has, add
1975 * it to the front so we find it first.
1977 if (req
->flags
& REQ_F_IOPOLL_COMPLETED
)
1978 list_add(&req
->list
, &ctx
->poll_list
);
1980 list_add_tail(&req
->list
, &ctx
->poll_list
);
1982 if ((ctx
->flags
& IORING_SETUP_SQPOLL
) &&
1983 wq_has_sleeper(&ctx
->sqo_wait
))
1984 wake_up(&ctx
->sqo_wait
);
1987 static void io_file_put(struct io_submit_state
*state
)
1990 int diff
= state
->has_refs
- state
->used_refs
;
1993 fput_many(state
->file
, diff
);
1999 * Get as many references to a file as we have IOs left in this submission,
2000 * assuming most submissions are for one file, or at least that each file
2001 * has more than one submission.
2003 static struct file
*__io_file_get(struct io_submit_state
*state
, int fd
)
2009 if (state
->fd
== fd
) {
2016 state
->file
= fget_many(fd
, state
->ios_left
);
2021 state
->has_refs
= state
->ios_left
;
2022 state
->used_refs
= 1;
2028 * If we tracked the file through the SCM inflight mechanism, we could support
2029 * any file. For now, just ensure that anything potentially problematic is done
2032 static bool io_file_supports_async(struct file
*file
, int rw
)
2034 umode_t mode
= file_inode(file
)->i_mode
;
2036 if (S_ISBLK(mode
) || S_ISCHR(mode
) || S_ISSOCK(mode
))
2038 if (S_ISREG(mode
) && file
->f_op
!= &io_uring_fops
)
2041 /* any ->read/write should understand O_NONBLOCK */
2042 if (file
->f_flags
& O_NONBLOCK
)
2045 if (!(file
->f_mode
& FMODE_NOWAIT
))
2049 return file
->f_op
->read_iter
!= NULL
;
2051 return file
->f_op
->write_iter
!= NULL
;
2054 static int io_prep_rw(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
,
2055 bool force_nonblock
)
2057 struct io_ring_ctx
*ctx
= req
->ctx
;
2058 struct kiocb
*kiocb
= &req
->rw
.kiocb
;
2062 if (S_ISREG(file_inode(req
->file
)->i_mode
))
2063 req
->flags
|= REQ_F_ISREG
;
2065 kiocb
->ki_pos
= READ_ONCE(sqe
->off
);
2066 if (kiocb
->ki_pos
== -1 && !(req
->file
->f_mode
& FMODE_STREAM
)) {
2067 req
->flags
|= REQ_F_CUR_POS
;
2068 kiocb
->ki_pos
= req
->file
->f_pos
;
2070 kiocb
->ki_hint
= ki_hint_validate(file_write_hint(kiocb
->ki_filp
));
2071 kiocb
->ki_flags
= iocb_flags(kiocb
->ki_filp
);
2072 ret
= kiocb_set_rw_flags(kiocb
, READ_ONCE(sqe
->rw_flags
));
2076 ioprio
= READ_ONCE(sqe
->ioprio
);
2078 ret
= ioprio_check_cap(ioprio
);
2082 kiocb
->ki_ioprio
= ioprio
;
2084 kiocb
->ki_ioprio
= get_current_ioprio();
2086 /* don't allow async punt if RWF_NOWAIT was requested */
2087 if (kiocb
->ki_flags
& IOCB_NOWAIT
)
2088 req
->flags
|= REQ_F_NOWAIT
;
2091 kiocb
->ki_flags
|= IOCB_NOWAIT
;
2093 if (ctx
->flags
& IORING_SETUP_IOPOLL
) {
2094 if (!(kiocb
->ki_flags
& IOCB_DIRECT
) ||
2095 !kiocb
->ki_filp
->f_op
->iopoll
)
2098 kiocb
->ki_flags
|= IOCB_HIPRI
;
2099 kiocb
->ki_complete
= io_complete_rw_iopoll
;
2102 if (kiocb
->ki_flags
& IOCB_HIPRI
)
2104 kiocb
->ki_complete
= io_complete_rw
;
2107 req
->rw
.addr
= READ_ONCE(sqe
->addr
);
2108 req
->rw
.len
= READ_ONCE(sqe
->len
);
2109 req
->buf_index
= READ_ONCE(sqe
->buf_index
);
2113 static inline void io_rw_done(struct kiocb
*kiocb
, ssize_t ret
)
2119 case -ERESTARTNOINTR
:
2120 case -ERESTARTNOHAND
:
2121 case -ERESTART_RESTARTBLOCK
:
2123 * We can't just restart the syscall, since previously
2124 * submitted sqes may already be in progress. Just fail this
2130 kiocb
->ki_complete(kiocb
, ret
, 0);
2134 static void kiocb_done(struct kiocb
*kiocb
, ssize_t ret
)
2136 struct io_kiocb
*req
= container_of(kiocb
, struct io_kiocb
, rw
.kiocb
);
2138 if (req
->flags
& REQ_F_CUR_POS
)
2139 req
->file
->f_pos
= kiocb
->ki_pos
;
2140 if (ret
>= 0 && kiocb
->ki_complete
== io_complete_rw
)
2141 io_complete_rw(kiocb
, ret
, 0);
2143 io_rw_done(kiocb
, ret
);
2146 static ssize_t
io_import_fixed(struct io_kiocb
*req
, int rw
,
2147 struct iov_iter
*iter
)
2149 struct io_ring_ctx
*ctx
= req
->ctx
;
2150 size_t len
= req
->rw
.len
;
2151 struct io_mapped_ubuf
*imu
;
2152 u16 index
, buf_index
;
2156 /* attempt to use fixed buffers without having provided iovecs */
2157 if (unlikely(!ctx
->user_bufs
))
2160 buf_index
= req
->buf_index
;
2161 if (unlikely(buf_index
>= ctx
->nr_user_bufs
))
2164 index
= array_index_nospec(buf_index
, ctx
->nr_user_bufs
);
2165 imu
= &ctx
->user_bufs
[index
];
2166 buf_addr
= req
->rw
.addr
;
2169 if (buf_addr
+ len
< buf_addr
)
2171 /* not inside the mapped region */
2172 if (buf_addr
< imu
->ubuf
|| buf_addr
+ len
> imu
->ubuf
+ imu
->len
)
2176 * May not be a start of buffer, set size appropriately
2177 * and advance us to the beginning.
2179 offset
= buf_addr
- imu
->ubuf
;
2180 iov_iter_bvec(iter
, rw
, imu
->bvec
, imu
->nr_bvecs
, offset
+ len
);
2184 * Don't use iov_iter_advance() here, as it's really slow for
2185 * using the latter parts of a big fixed buffer - it iterates
2186 * over each segment manually. We can cheat a bit here, because
2189 * 1) it's a BVEC iter, we set it up
2190 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2191 * first and last bvec
2193 * So just find our index, and adjust the iterator afterwards.
2194 * If the offset is within the first bvec (or the whole first
2195 * bvec, just use iov_iter_advance(). This makes it easier
2196 * since we can just skip the first segment, which may not
2197 * be PAGE_SIZE aligned.
2199 const struct bio_vec
*bvec
= imu
->bvec
;
2201 if (offset
<= bvec
->bv_len
) {
2202 iov_iter_advance(iter
, offset
);
2204 unsigned long seg_skip
;
2206 /* skip first vec */
2207 offset
-= bvec
->bv_len
;
2208 seg_skip
= 1 + (offset
>> PAGE_SHIFT
);
2210 iter
->bvec
= bvec
+ seg_skip
;
2211 iter
->nr_segs
-= seg_skip
;
2212 iter
->count
-= bvec
->bv_len
+ offset
;
2213 iter
->iov_offset
= offset
& ~PAGE_MASK
;
2220 static void io_ring_submit_unlock(struct io_ring_ctx
*ctx
, bool needs_lock
)
2223 mutex_unlock(&ctx
->uring_lock
);
2226 static void io_ring_submit_lock(struct io_ring_ctx
*ctx
, bool needs_lock
)
2229 * "Normal" inline submissions always hold the uring_lock, since we
2230 * grab it from the system call. Same is true for the SQPOLL offload.
2231 * The only exception is when we've detached the request and issue it
2232 * from an async worker thread, grab the lock for that case.
2235 mutex_lock(&ctx
->uring_lock
);
2238 static struct io_buffer
*io_buffer_select(struct io_kiocb
*req
, size_t *len
,
2239 int bgid
, struct io_buffer
*kbuf
,
2242 struct io_buffer
*head
;
2244 if (req
->flags
& REQ_F_BUFFER_SELECTED
)
2247 io_ring_submit_lock(req
->ctx
, needs_lock
);
2249 lockdep_assert_held(&req
->ctx
->uring_lock
);
2251 head
= idr_find(&req
->ctx
->io_buffer_idr
, bgid
);
2253 if (!list_empty(&head
->list
)) {
2254 kbuf
= list_last_entry(&head
->list
, struct io_buffer
,
2256 list_del(&kbuf
->list
);
2259 idr_remove(&req
->ctx
->io_buffer_idr
, bgid
);
2261 if (*len
> kbuf
->len
)
2264 kbuf
= ERR_PTR(-ENOBUFS
);
2267 io_ring_submit_unlock(req
->ctx
, needs_lock
);
2272 static void __user
*io_rw_buffer_select(struct io_kiocb
*req
, size_t *len
,
2275 struct io_buffer
*kbuf
;
2278 kbuf
= (struct io_buffer
*) (unsigned long) req
->rw
.addr
;
2279 bgid
= req
->buf_index
;
2280 kbuf
= io_buffer_select(req
, len
, bgid
, kbuf
, needs_lock
);
2283 req
->rw
.addr
= (u64
) (unsigned long) kbuf
;
2284 req
->flags
|= REQ_F_BUFFER_SELECTED
;
2285 return u64_to_user_ptr(kbuf
->addr
);
2288 #ifdef CONFIG_COMPAT
2289 static ssize_t
io_compat_import(struct io_kiocb
*req
, struct iovec
*iov
,
2292 struct compat_iovec __user
*uiov
;
2293 compat_ssize_t clen
;
2297 uiov
= u64_to_user_ptr(req
->rw
.addr
);
2298 if (!access_ok(uiov
, sizeof(*uiov
)))
2300 if (__get_user(clen
, &uiov
->iov_len
))
2306 buf
= io_rw_buffer_select(req
, &len
, needs_lock
);
2308 return PTR_ERR(buf
);
2309 iov
[0].iov_base
= buf
;
2310 iov
[0].iov_len
= (compat_size_t
) len
;
2315 static ssize_t
__io_iov_buffer_select(struct io_kiocb
*req
, struct iovec
*iov
,
2318 struct iovec __user
*uiov
= u64_to_user_ptr(req
->rw
.addr
);
2322 if (copy_from_user(iov
, uiov
, sizeof(*uiov
)))
2325 len
= iov
[0].iov_len
;
2328 buf
= io_rw_buffer_select(req
, &len
, needs_lock
);
2330 return PTR_ERR(buf
);
2331 iov
[0].iov_base
= buf
;
2332 iov
[0].iov_len
= len
;
2336 static ssize_t
io_iov_buffer_select(struct io_kiocb
*req
, struct iovec
*iov
,
2339 if (req
->flags
& REQ_F_BUFFER_SELECTED
) {
2340 struct io_buffer
*kbuf
;
2342 kbuf
= (struct io_buffer
*) (unsigned long) req
->rw
.addr
;
2343 iov
[0].iov_base
= u64_to_user_ptr(kbuf
->addr
);
2344 iov
[0].iov_len
= kbuf
->len
;
2349 else if (req
->rw
.len
> 1)
2352 #ifdef CONFIG_COMPAT
2353 if (req
->ctx
->compat
)
2354 return io_compat_import(req
, iov
, needs_lock
);
2357 return __io_iov_buffer_select(req
, iov
, needs_lock
);
2360 static ssize_t
io_import_iovec(int rw
, struct io_kiocb
*req
,
2361 struct iovec
**iovec
, struct iov_iter
*iter
,
2364 void __user
*buf
= u64_to_user_ptr(req
->rw
.addr
);
2365 size_t sqe_len
= req
->rw
.len
;
2369 opcode
= req
->opcode
;
2370 if (opcode
== IORING_OP_READ_FIXED
|| opcode
== IORING_OP_WRITE_FIXED
) {
2372 return io_import_fixed(req
, rw
, iter
);
2375 /* buffer index only valid with fixed read/write, or buffer select */
2376 if (req
->buf_index
&& !(req
->flags
& REQ_F_BUFFER_SELECT
))
2379 if (opcode
== IORING_OP_READ
|| opcode
== IORING_OP_WRITE
) {
2380 if (req
->flags
& REQ_F_BUFFER_SELECT
) {
2381 buf
= io_rw_buffer_select(req
, &sqe_len
, needs_lock
);
2384 return PTR_ERR(buf
);
2386 req
->rw
.len
= sqe_len
;
2389 ret
= import_single_range(rw
, buf
, sqe_len
, *iovec
, iter
);
2391 return ret
< 0 ? ret
: sqe_len
;
2395 struct io_async_rw
*iorw
= &req
->io
->rw
;
2398 iov_iter_init(iter
, rw
, *iovec
, iorw
->nr_segs
, iorw
->size
);
2399 if (iorw
->iov
== iorw
->fast_iov
)
2404 if (req
->flags
& REQ_F_BUFFER_SELECT
) {
2405 ret
= io_iov_buffer_select(req
, *iovec
, needs_lock
);
2407 ret
= (*iovec
)->iov_len
;
2408 iov_iter_init(iter
, rw
, *iovec
, 1, ret
);
2414 #ifdef CONFIG_COMPAT
2415 if (req
->ctx
->compat
)
2416 return compat_import_iovec(rw
, buf
, sqe_len
, UIO_FASTIOV
,
2420 return import_iovec(rw
, buf
, sqe_len
, UIO_FASTIOV
, iovec
, iter
);
2424 * For files that don't have ->read_iter() and ->write_iter(), handle them
2425 * by looping over ->read() or ->write() manually.
2427 static ssize_t
loop_rw_iter(int rw
, struct file
*file
, struct kiocb
*kiocb
,
2428 struct iov_iter
*iter
)
2433 * Don't support polled IO through this interface, and we can't
2434 * support non-blocking either. For the latter, this just causes
2435 * the kiocb to be handled from an async context.
2437 if (kiocb
->ki_flags
& IOCB_HIPRI
)
2439 if (kiocb
->ki_flags
& IOCB_NOWAIT
)
2442 while (iov_iter_count(iter
)) {
2446 if (!iov_iter_is_bvec(iter
)) {
2447 iovec
= iov_iter_iovec(iter
);
2449 /* fixed buffers import bvec */
2450 iovec
.iov_base
= kmap(iter
->bvec
->bv_page
)
2452 iovec
.iov_len
= min(iter
->count
,
2453 iter
->bvec
->bv_len
- iter
->iov_offset
);
2457 nr
= file
->f_op
->read(file
, iovec
.iov_base
,
2458 iovec
.iov_len
, &kiocb
->ki_pos
);
2460 nr
= file
->f_op
->write(file
, iovec
.iov_base
,
2461 iovec
.iov_len
, &kiocb
->ki_pos
);
2464 if (iov_iter_is_bvec(iter
))
2465 kunmap(iter
->bvec
->bv_page
);
2473 if (nr
!= iovec
.iov_len
)
2475 iov_iter_advance(iter
, nr
);
2481 static void io_req_map_rw(struct io_kiocb
*req
, ssize_t io_size
,
2482 struct iovec
*iovec
, struct iovec
*fast_iov
,
2483 struct iov_iter
*iter
)
2485 req
->io
->rw
.nr_segs
= iter
->nr_segs
;
2486 req
->io
->rw
.size
= io_size
;
2487 req
->io
->rw
.iov
= iovec
;
2488 if (!req
->io
->rw
.iov
) {
2489 req
->io
->rw
.iov
= req
->io
->rw
.fast_iov
;
2490 if (req
->io
->rw
.iov
!= fast_iov
)
2491 memcpy(req
->io
->rw
.iov
, fast_iov
,
2492 sizeof(struct iovec
) * iter
->nr_segs
);
2494 req
->flags
|= REQ_F_NEED_CLEANUP
;
2498 static inline int __io_alloc_async_ctx(struct io_kiocb
*req
)
2500 req
->io
= kmalloc(sizeof(*req
->io
), GFP_KERNEL
);
2501 return req
->io
== NULL
;
2504 static int io_alloc_async_ctx(struct io_kiocb
*req
)
2506 if (!io_op_defs
[req
->opcode
].async_ctx
)
2509 return __io_alloc_async_ctx(req
);
2512 static int io_setup_async_rw(struct io_kiocb
*req
, ssize_t io_size
,
2513 struct iovec
*iovec
, struct iovec
*fast_iov
,
2514 struct iov_iter
*iter
)
2516 if (!io_op_defs
[req
->opcode
].async_ctx
)
2519 if (__io_alloc_async_ctx(req
))
2522 io_req_map_rw(req
, io_size
, iovec
, fast_iov
, iter
);
2527 static int io_read_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
,
2528 bool force_nonblock
)
2530 struct io_async_ctx
*io
;
2531 struct iov_iter iter
;
2534 ret
= io_prep_rw(req
, sqe
, force_nonblock
);
2538 if (unlikely(!(req
->file
->f_mode
& FMODE_READ
)))
2541 /* either don't need iovec imported or already have it */
2542 if (!req
->io
|| req
->flags
& REQ_F_NEED_CLEANUP
)
2546 io
->rw
.iov
= io
->rw
.fast_iov
;
2548 ret
= io_import_iovec(READ
, req
, &io
->rw
.iov
, &iter
, !force_nonblock
);
2553 io_req_map_rw(req
, ret
, io
->rw
.iov
, io
->rw
.fast_iov
, &iter
);
2557 static int io_read(struct io_kiocb
*req
, bool force_nonblock
)
2559 struct iovec inline_vecs
[UIO_FASTIOV
], *iovec
= inline_vecs
;
2560 struct kiocb
*kiocb
= &req
->rw
.kiocb
;
2561 struct iov_iter iter
;
2563 ssize_t io_size
, ret
;
2565 ret
= io_import_iovec(READ
, req
, &iovec
, &iter
, !force_nonblock
);
2569 /* Ensure we clear previously set non-block flag */
2570 if (!force_nonblock
)
2571 kiocb
->ki_flags
&= ~IOCB_NOWAIT
;
2575 if (req
->flags
& REQ_F_LINK_HEAD
)
2576 req
->result
= io_size
;
2579 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2580 * we know to async punt it even if it was opened O_NONBLOCK
2582 if (force_nonblock
&& !io_file_supports_async(req
->file
, READ
))
2585 iov_count
= iov_iter_count(&iter
);
2586 ret
= rw_verify_area(READ
, req
->file
, &kiocb
->ki_pos
, iov_count
);
2590 if (req
->file
->f_op
->read_iter
)
2591 ret2
= call_read_iter(req
->file
, kiocb
, &iter
);
2593 ret2
= loop_rw_iter(READ
, req
->file
, kiocb
, &iter
);
2595 /* Catch -EAGAIN return for forced non-blocking submission */
2596 if (!force_nonblock
|| ret2
!= -EAGAIN
) {
2597 kiocb_done(kiocb
, ret2
);
2600 ret
= io_setup_async_rw(req
, io_size
, iovec
,
2601 inline_vecs
, &iter
);
2604 /* any defer here is final, must blocking retry */
2605 if (!(req
->flags
& REQ_F_NOWAIT
) &&
2606 !file_can_poll(req
->file
))
2607 req
->flags
|= REQ_F_MUST_PUNT
;
2613 req
->flags
&= ~REQ_F_NEED_CLEANUP
;
2617 static int io_write_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
,
2618 bool force_nonblock
)
2620 struct io_async_ctx
*io
;
2621 struct iov_iter iter
;
2624 ret
= io_prep_rw(req
, sqe
, force_nonblock
);
2628 if (unlikely(!(req
->file
->f_mode
& FMODE_WRITE
)))
2631 req
->fsize
= rlimit(RLIMIT_FSIZE
);
2633 /* either don't need iovec imported or already have it */
2634 if (!req
->io
|| req
->flags
& REQ_F_NEED_CLEANUP
)
2638 io
->rw
.iov
= io
->rw
.fast_iov
;
2640 ret
= io_import_iovec(WRITE
, req
, &io
->rw
.iov
, &iter
, !force_nonblock
);
2645 io_req_map_rw(req
, ret
, io
->rw
.iov
, io
->rw
.fast_iov
, &iter
);
2649 static int io_write(struct io_kiocb
*req
, bool force_nonblock
)
2651 struct iovec inline_vecs
[UIO_FASTIOV
], *iovec
= inline_vecs
;
2652 struct kiocb
*kiocb
= &req
->rw
.kiocb
;
2653 struct iov_iter iter
;
2655 ssize_t ret
, io_size
;
2657 ret
= io_import_iovec(WRITE
, req
, &iovec
, &iter
, !force_nonblock
);
2661 /* Ensure we clear previously set non-block flag */
2662 if (!force_nonblock
)
2663 req
->rw
.kiocb
.ki_flags
&= ~IOCB_NOWAIT
;
2667 if (req
->flags
& REQ_F_LINK_HEAD
)
2668 req
->result
= io_size
;
2671 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2672 * we know to async punt it even if it was opened O_NONBLOCK
2674 if (force_nonblock
&& !io_file_supports_async(req
->file
, WRITE
))
2677 /* file path doesn't support NOWAIT for non-direct_IO */
2678 if (force_nonblock
&& !(kiocb
->ki_flags
& IOCB_DIRECT
) &&
2679 (req
->flags
& REQ_F_ISREG
))
2682 iov_count
= iov_iter_count(&iter
);
2683 ret
= rw_verify_area(WRITE
, req
->file
, &kiocb
->ki_pos
, iov_count
);
2688 * Open-code file_start_write here to grab freeze protection,
2689 * which will be released by another thread in
2690 * io_complete_rw(). Fool lockdep by telling it the lock got
2691 * released so that it doesn't complain about the held lock when
2692 * we return to userspace.
2694 if (req
->flags
& REQ_F_ISREG
) {
2695 __sb_start_write(file_inode(req
->file
)->i_sb
,
2696 SB_FREEZE_WRITE
, true);
2697 __sb_writers_release(file_inode(req
->file
)->i_sb
,
2700 kiocb
->ki_flags
|= IOCB_WRITE
;
2702 if (!force_nonblock
)
2703 current
->signal
->rlim
[RLIMIT_FSIZE
].rlim_cur
= req
->fsize
;
2705 if (req
->file
->f_op
->write_iter
)
2706 ret2
= call_write_iter(req
->file
, kiocb
, &iter
);
2708 ret2
= loop_rw_iter(WRITE
, req
->file
, kiocb
, &iter
);
2710 if (!force_nonblock
)
2711 current
->signal
->rlim
[RLIMIT_FSIZE
].rlim_cur
= RLIM_INFINITY
;
2714 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
2715 * retry them without IOCB_NOWAIT.
2717 if (ret2
== -EOPNOTSUPP
&& (kiocb
->ki_flags
& IOCB_NOWAIT
))
2719 if (!force_nonblock
|| ret2
!= -EAGAIN
) {
2720 kiocb_done(kiocb
, ret2
);
2723 ret
= io_setup_async_rw(req
, io_size
, iovec
,
2724 inline_vecs
, &iter
);
2727 /* any defer here is final, must blocking retry */
2728 if (!(req
->flags
& REQ_F_NOWAIT
) &&
2729 !file_can_poll(req
->file
))
2730 req
->flags
|= REQ_F_MUST_PUNT
;
2735 req
->flags
&= ~REQ_F_NEED_CLEANUP
;
2740 static int io_splice_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
2742 struct io_splice
* sp
= &req
->splice
;
2743 unsigned int valid_flags
= SPLICE_F_FD_IN_FIXED
| SPLICE_F_ALL
;
2746 if (req
->flags
& REQ_F_NEED_CLEANUP
)
2750 sp
->off_in
= READ_ONCE(sqe
->splice_off_in
);
2751 sp
->off_out
= READ_ONCE(sqe
->off
);
2752 sp
->len
= READ_ONCE(sqe
->len
);
2753 sp
->flags
= READ_ONCE(sqe
->splice_flags
);
2755 if (unlikely(sp
->flags
& ~valid_flags
))
2758 ret
= io_file_get(NULL
, req
, READ_ONCE(sqe
->splice_fd_in
), &sp
->file_in
,
2759 (sp
->flags
& SPLICE_F_FD_IN_FIXED
));
2762 req
->flags
|= REQ_F_NEED_CLEANUP
;
2764 if (!S_ISREG(file_inode(sp
->file_in
)->i_mode
))
2765 req
->work
.flags
|= IO_WQ_WORK_UNBOUND
;
2770 static int io_splice(struct io_kiocb
*req
, bool force_nonblock
)
2772 struct io_splice
*sp
= &req
->splice
;
2773 struct file
*in
= sp
->file_in
;
2774 struct file
*out
= sp
->file_out
;
2775 unsigned int flags
= sp
->flags
& ~SPLICE_F_FD_IN_FIXED
;
2776 loff_t
*poff_in
, *poff_out
;
2782 poff_in
= (sp
->off_in
== -1) ? NULL
: &sp
->off_in
;
2783 poff_out
= (sp
->off_out
== -1) ? NULL
: &sp
->off_out
;
2786 ret
= do_splice(in
, poff_in
, out
, poff_out
, sp
->len
, flags
);
2788 io_put_file(req
, in
, (sp
->flags
& SPLICE_F_FD_IN_FIXED
));
2789 req
->flags
&= ~REQ_F_NEED_CLEANUP
;
2791 io_cqring_add_event(req
, ret
);
2793 req_set_fail_links(req
);
2799 * IORING_OP_NOP just posts a completion event, nothing else.
2801 static int io_nop(struct io_kiocb
*req
)
2803 struct io_ring_ctx
*ctx
= req
->ctx
;
2805 if (unlikely(ctx
->flags
& IORING_SETUP_IOPOLL
))
2808 io_cqring_add_event(req
, 0);
2813 static int io_prep_fsync(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
2815 struct io_ring_ctx
*ctx
= req
->ctx
;
2820 if (unlikely(ctx
->flags
& IORING_SETUP_IOPOLL
))
2822 if (unlikely(sqe
->addr
|| sqe
->ioprio
|| sqe
->buf_index
))
2825 req
->sync
.flags
= READ_ONCE(sqe
->fsync_flags
);
2826 if (unlikely(req
->sync
.flags
& ~IORING_FSYNC_DATASYNC
))
2829 req
->sync
.off
= READ_ONCE(sqe
->off
);
2830 req
->sync
.len
= READ_ONCE(sqe
->len
);
2834 static bool io_req_cancelled(struct io_kiocb
*req
)
2836 if (req
->work
.flags
& IO_WQ_WORK_CANCEL
) {
2837 req_set_fail_links(req
);
2838 io_cqring_add_event(req
, -ECANCELED
);
2846 static void __io_fsync(struct io_kiocb
*req
)
2848 loff_t end
= req
->sync
.off
+ req
->sync
.len
;
2851 ret
= vfs_fsync_range(req
->file
, req
->sync
.off
,
2852 end
> 0 ? end
: LLONG_MAX
,
2853 req
->sync
.flags
& IORING_FSYNC_DATASYNC
);
2855 req_set_fail_links(req
);
2856 io_cqring_add_event(req
, ret
);
2860 static void io_fsync_finish(struct io_wq_work
**workptr
)
2862 struct io_kiocb
*req
= container_of(*workptr
, struct io_kiocb
, work
);
2864 if (io_req_cancelled(req
))
2867 io_steal_work(req
, workptr
);
2870 static int io_fsync(struct io_kiocb
*req
, bool force_nonblock
)
2872 /* fsync always requires a blocking context */
2873 if (force_nonblock
) {
2874 req
->work
.func
= io_fsync_finish
;
2881 static void __io_fallocate(struct io_kiocb
*req
)
2885 current
->signal
->rlim
[RLIMIT_FSIZE
].rlim_cur
= req
->fsize
;
2886 ret
= vfs_fallocate(req
->file
, req
->sync
.mode
, req
->sync
.off
,
2888 current
->signal
->rlim
[RLIMIT_FSIZE
].rlim_cur
= RLIM_INFINITY
;
2890 req_set_fail_links(req
);
2891 io_cqring_add_event(req
, ret
);
2895 static void io_fallocate_finish(struct io_wq_work
**workptr
)
2897 struct io_kiocb
*req
= container_of(*workptr
, struct io_kiocb
, work
);
2899 if (io_req_cancelled(req
))
2901 __io_fallocate(req
);
2902 io_steal_work(req
, workptr
);
2905 static int io_fallocate_prep(struct io_kiocb
*req
,
2906 const struct io_uring_sqe
*sqe
)
2908 if (sqe
->ioprio
|| sqe
->buf_index
|| sqe
->rw_flags
)
2911 req
->sync
.off
= READ_ONCE(sqe
->off
);
2912 req
->sync
.len
= READ_ONCE(sqe
->addr
);
2913 req
->sync
.mode
= READ_ONCE(sqe
->len
);
2914 req
->fsize
= rlimit(RLIMIT_FSIZE
);
2918 static int io_fallocate(struct io_kiocb
*req
, bool force_nonblock
)
2920 /* fallocate always requiring blocking context */
2921 if (force_nonblock
) {
2922 req
->work
.func
= io_fallocate_finish
;
2926 __io_fallocate(req
);
2930 static int io_openat_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
2932 const char __user
*fname
;
2935 if (sqe
->ioprio
|| sqe
->buf_index
)
2937 if (req
->flags
& REQ_F_FIXED_FILE
)
2939 if (req
->flags
& REQ_F_NEED_CLEANUP
)
2942 req
->open
.dfd
= READ_ONCE(sqe
->fd
);
2943 req
->open
.how
.mode
= READ_ONCE(sqe
->len
);
2944 fname
= u64_to_user_ptr(READ_ONCE(sqe
->addr
));
2945 req
->open
.how
.flags
= READ_ONCE(sqe
->open_flags
);
2946 if (force_o_largefile())
2947 req
->open
.how
.flags
|= O_LARGEFILE
;
2949 req
->open
.filename
= getname(fname
);
2950 if (IS_ERR(req
->open
.filename
)) {
2951 ret
= PTR_ERR(req
->open
.filename
);
2952 req
->open
.filename
= NULL
;
2956 req
->open
.nofile
= rlimit(RLIMIT_NOFILE
);
2957 req
->flags
|= REQ_F_NEED_CLEANUP
;
2961 static int io_openat2_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
2963 struct open_how __user
*how
;
2964 const char __user
*fname
;
2968 if (sqe
->ioprio
|| sqe
->buf_index
)
2970 if (req
->flags
& REQ_F_FIXED_FILE
)
2972 if (req
->flags
& REQ_F_NEED_CLEANUP
)
2975 req
->open
.dfd
= READ_ONCE(sqe
->fd
);
2976 fname
= u64_to_user_ptr(READ_ONCE(sqe
->addr
));
2977 how
= u64_to_user_ptr(READ_ONCE(sqe
->addr2
));
2978 len
= READ_ONCE(sqe
->len
);
2980 if (len
< OPEN_HOW_SIZE_VER0
)
2983 ret
= copy_struct_from_user(&req
->open
.how
, sizeof(req
->open
.how
), how
,
2988 if (!(req
->open
.how
.flags
& O_PATH
) && force_o_largefile())
2989 req
->open
.how
.flags
|= O_LARGEFILE
;
2991 req
->open
.filename
= getname(fname
);
2992 if (IS_ERR(req
->open
.filename
)) {
2993 ret
= PTR_ERR(req
->open
.filename
);
2994 req
->open
.filename
= NULL
;
2998 req
->open
.nofile
= rlimit(RLIMIT_NOFILE
);
2999 req
->flags
|= REQ_F_NEED_CLEANUP
;
3003 static int io_openat2(struct io_kiocb
*req
, bool force_nonblock
)
3005 struct open_flags op
;
3012 ret
= build_open_flags(&req
->open
.how
, &op
);
3016 ret
= __get_unused_fd_flags(req
->open
.how
.flags
, req
->open
.nofile
);
3020 file
= do_filp_open(req
->open
.dfd
, req
->open
.filename
, &op
);
3023 ret
= PTR_ERR(file
);
3025 fsnotify_open(file
);
3026 fd_install(ret
, file
);
3029 putname(req
->open
.filename
);
3030 req
->flags
&= ~REQ_F_NEED_CLEANUP
;
3032 req_set_fail_links(req
);
3033 io_cqring_add_event(req
, ret
);
3038 static int io_openat(struct io_kiocb
*req
, bool force_nonblock
)
3040 req
->open
.how
= build_open_how(req
->open
.how
.flags
, req
->open
.how
.mode
);
3041 return io_openat2(req
, force_nonblock
);
3044 static int io_remove_buffers_prep(struct io_kiocb
*req
,
3045 const struct io_uring_sqe
*sqe
)
3047 struct io_provide_buf
*p
= &req
->pbuf
;
3050 if (sqe
->ioprio
|| sqe
->rw_flags
|| sqe
->addr
|| sqe
->len
|| sqe
->off
)
3053 tmp
= READ_ONCE(sqe
->fd
);
3054 if (!tmp
|| tmp
> USHRT_MAX
)
3057 memset(p
, 0, sizeof(*p
));
3059 p
->bgid
= READ_ONCE(sqe
->buf_group
);
3063 static int __io_remove_buffers(struct io_ring_ctx
*ctx
, struct io_buffer
*buf
,
3064 int bgid
, unsigned nbufs
)
3068 /* shouldn't happen */
3072 /* the head kbuf is the list itself */
3073 while (!list_empty(&buf
->list
)) {
3074 struct io_buffer
*nxt
;
3076 nxt
= list_first_entry(&buf
->list
, struct io_buffer
, list
);
3077 list_del(&nxt
->list
);
3084 idr_remove(&ctx
->io_buffer_idr
, bgid
);
3089 static int io_remove_buffers(struct io_kiocb
*req
, bool force_nonblock
)
3091 struct io_provide_buf
*p
= &req
->pbuf
;
3092 struct io_ring_ctx
*ctx
= req
->ctx
;
3093 struct io_buffer
*head
;
3096 io_ring_submit_lock(ctx
, !force_nonblock
);
3098 lockdep_assert_held(&ctx
->uring_lock
);
3101 head
= idr_find(&ctx
->io_buffer_idr
, p
->bgid
);
3103 ret
= __io_remove_buffers(ctx
, head
, p
->bgid
, p
->nbufs
);
3105 io_ring_submit_lock(ctx
, !force_nonblock
);
3107 req_set_fail_links(req
);
3108 io_cqring_add_event(req
, ret
);
3113 static int io_provide_buffers_prep(struct io_kiocb
*req
,
3114 const struct io_uring_sqe
*sqe
)
3116 struct io_provide_buf
*p
= &req
->pbuf
;
3119 if (sqe
->ioprio
|| sqe
->rw_flags
)
3122 tmp
= READ_ONCE(sqe
->fd
);
3123 if (!tmp
|| tmp
> USHRT_MAX
)
3126 p
->addr
= READ_ONCE(sqe
->addr
);
3127 p
->len
= READ_ONCE(sqe
->len
);
3129 if (!access_ok(u64_to_user_ptr(p
->addr
), p
->len
))
3132 p
->bgid
= READ_ONCE(sqe
->buf_group
);
3133 tmp
= READ_ONCE(sqe
->off
);
3134 if (tmp
> USHRT_MAX
)
3140 static int io_add_buffers(struct io_provide_buf
*pbuf
, struct io_buffer
**head
)
3142 struct io_buffer
*buf
;
3143 u64 addr
= pbuf
->addr
;
3144 int i
, bid
= pbuf
->bid
;
3146 for (i
= 0; i
< pbuf
->nbufs
; i
++) {
3147 buf
= kmalloc(sizeof(*buf
), GFP_KERNEL
);
3152 buf
->len
= pbuf
->len
;
3157 INIT_LIST_HEAD(&buf
->list
);
3160 list_add_tail(&buf
->list
, &(*head
)->list
);
3164 return i
? i
: -ENOMEM
;
3167 static int io_provide_buffers(struct io_kiocb
*req
, bool force_nonblock
)
3169 struct io_provide_buf
*p
= &req
->pbuf
;
3170 struct io_ring_ctx
*ctx
= req
->ctx
;
3171 struct io_buffer
*head
, *list
;
3174 io_ring_submit_lock(ctx
, !force_nonblock
);
3176 lockdep_assert_held(&ctx
->uring_lock
);
3178 list
= head
= idr_find(&ctx
->io_buffer_idr
, p
->bgid
);
3180 ret
= io_add_buffers(p
, &head
);
3185 ret
= idr_alloc(&ctx
->io_buffer_idr
, head
, p
->bgid
, p
->bgid
+ 1,
3188 __io_remove_buffers(ctx
, head
, p
->bgid
, -1U);
3193 io_ring_submit_unlock(ctx
, !force_nonblock
);
3195 req_set_fail_links(req
);
3196 io_cqring_add_event(req
, ret
);
3201 static int io_epoll_ctl_prep(struct io_kiocb
*req
,
3202 const struct io_uring_sqe
*sqe
)
3204 #if defined(CONFIG_EPOLL)
3205 if (sqe
->ioprio
|| sqe
->buf_index
)
3208 req
->epoll
.epfd
= READ_ONCE(sqe
->fd
);
3209 req
->epoll
.op
= READ_ONCE(sqe
->len
);
3210 req
->epoll
.fd
= READ_ONCE(sqe
->off
);
3212 if (ep_op_has_event(req
->epoll
.op
)) {
3213 struct epoll_event __user
*ev
;
3215 ev
= u64_to_user_ptr(READ_ONCE(sqe
->addr
));
3216 if (copy_from_user(&req
->epoll
.event
, ev
, sizeof(*ev
)))
3226 static int io_epoll_ctl(struct io_kiocb
*req
, bool force_nonblock
)
3228 #if defined(CONFIG_EPOLL)
3229 struct io_epoll
*ie
= &req
->epoll
;
3232 ret
= do_epoll_ctl(ie
->epfd
, ie
->op
, ie
->fd
, &ie
->event
, force_nonblock
);
3233 if (force_nonblock
&& ret
== -EAGAIN
)
3237 req_set_fail_links(req
);
3238 io_cqring_add_event(req
, ret
);
3246 static int io_madvise_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
3248 #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3249 if (sqe
->ioprio
|| sqe
->buf_index
|| sqe
->off
)
3252 req
->madvise
.addr
= READ_ONCE(sqe
->addr
);
3253 req
->madvise
.len
= READ_ONCE(sqe
->len
);
3254 req
->madvise
.advice
= READ_ONCE(sqe
->fadvise_advice
);
3261 static int io_madvise(struct io_kiocb
*req
, bool force_nonblock
)
3263 #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3264 struct io_madvise
*ma
= &req
->madvise
;
3270 ret
= do_madvise(ma
->addr
, ma
->len
, ma
->advice
);
3272 req_set_fail_links(req
);
3273 io_cqring_add_event(req
, ret
);
3281 static int io_fadvise_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
3283 if (sqe
->ioprio
|| sqe
->buf_index
|| sqe
->addr
)
3286 req
->fadvise
.offset
= READ_ONCE(sqe
->off
);
3287 req
->fadvise
.len
= READ_ONCE(sqe
->len
);
3288 req
->fadvise
.advice
= READ_ONCE(sqe
->fadvise_advice
);
3292 static int io_fadvise(struct io_kiocb
*req
, bool force_nonblock
)
3294 struct io_fadvise
*fa
= &req
->fadvise
;
3297 if (force_nonblock
) {
3298 switch (fa
->advice
) {
3299 case POSIX_FADV_NORMAL
:
3300 case POSIX_FADV_RANDOM
:
3301 case POSIX_FADV_SEQUENTIAL
:
3308 ret
= vfs_fadvise(req
->file
, fa
->offset
, fa
->len
, fa
->advice
);
3310 req_set_fail_links(req
);
3311 io_cqring_add_event(req
, ret
);
3316 static int io_statx_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
3318 const char __user
*fname
;
3319 unsigned lookup_flags
;
3322 if (sqe
->ioprio
|| sqe
->buf_index
)
3324 if (req
->flags
& REQ_F_FIXED_FILE
)
3326 if (req
->flags
& REQ_F_NEED_CLEANUP
)
3329 req
->open
.dfd
= READ_ONCE(sqe
->fd
);
3330 req
->open
.mask
= READ_ONCE(sqe
->len
);
3331 fname
= u64_to_user_ptr(READ_ONCE(sqe
->addr
));
3332 req
->open
.buffer
= u64_to_user_ptr(READ_ONCE(sqe
->addr2
));
3333 req
->open
.how
.flags
= READ_ONCE(sqe
->statx_flags
);
3335 if (vfs_stat_set_lookup_flags(&lookup_flags
, req
->open
.how
.flags
))
3338 req
->open
.filename
= getname_flags(fname
, lookup_flags
, NULL
);
3339 if (IS_ERR(req
->open
.filename
)) {
3340 ret
= PTR_ERR(req
->open
.filename
);
3341 req
->open
.filename
= NULL
;
3345 req
->flags
|= REQ_F_NEED_CLEANUP
;
3349 static int io_statx(struct io_kiocb
*req
, bool force_nonblock
)
3351 struct io_open
*ctx
= &req
->open
;
3352 unsigned lookup_flags
;
3357 if (force_nonblock
) {
3358 /* only need file table for an actual valid fd */
3359 if (ctx
->dfd
== -1 || ctx
->dfd
== AT_FDCWD
)
3360 req
->flags
|= REQ_F_NO_FILE_TABLE
;
3364 if (vfs_stat_set_lookup_flags(&lookup_flags
, ctx
->how
.flags
))
3368 /* filename_lookup() drops it, keep a reference */
3369 ctx
->filename
->refcnt
++;
3371 ret
= filename_lookup(ctx
->dfd
, ctx
->filename
, lookup_flags
, &path
,
3376 ret
= vfs_getattr(&path
, &stat
, ctx
->mask
, ctx
->how
.flags
);
3378 if (retry_estale(ret
, lookup_flags
)) {
3379 lookup_flags
|= LOOKUP_REVAL
;
3383 ret
= cp_statx(&stat
, ctx
->buffer
);
3385 putname(ctx
->filename
);
3386 req
->flags
&= ~REQ_F_NEED_CLEANUP
;
3388 req_set_fail_links(req
);
3389 io_cqring_add_event(req
, ret
);
3394 static int io_close_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
3397 * If we queue this for async, it must not be cancellable. That would
3398 * leave the 'file' in an undeterminate state.
3400 req
->work
.flags
|= IO_WQ_WORK_NO_CANCEL
;
3402 if (sqe
->ioprio
|| sqe
->off
|| sqe
->addr
|| sqe
->len
||
3403 sqe
->rw_flags
|| sqe
->buf_index
)
3405 if (req
->flags
& REQ_F_FIXED_FILE
)
3408 req
->close
.fd
= READ_ONCE(sqe
->fd
);
3409 if (req
->file
->f_op
== &io_uring_fops
||
3410 req
->close
.fd
== req
->ctx
->ring_fd
)
3416 /* only called when __close_fd_get_file() is done */
3417 static void __io_close_finish(struct io_kiocb
*req
)
3421 ret
= filp_close(req
->close
.put_file
, req
->work
.files
);
3423 req_set_fail_links(req
);
3424 io_cqring_add_event(req
, ret
);
3425 fput(req
->close
.put_file
);
3429 static void io_close_finish(struct io_wq_work
**workptr
)
3431 struct io_kiocb
*req
= container_of(*workptr
, struct io_kiocb
, work
);
3433 /* not cancellable, don't do io_req_cancelled() */
3434 __io_close_finish(req
);
3435 io_steal_work(req
, workptr
);
3438 static int io_close(struct io_kiocb
*req
, bool force_nonblock
)
3442 req
->close
.put_file
= NULL
;
3443 ret
= __close_fd_get_file(req
->close
.fd
, &req
->close
.put_file
);
3447 /* if the file has a flush method, be safe and punt to async */
3448 if (req
->close
.put_file
->f_op
->flush
&& force_nonblock
) {
3449 /* submission ref will be dropped, take it for async */
3450 refcount_inc(&req
->refs
);
3452 req
->work
.func
= io_close_finish
;
3454 * Do manual async queue here to avoid grabbing files - we don't
3455 * need the files, and it'll cause io_close_finish() to close
3456 * the file again and cause a double CQE entry for this request
3458 io_queue_async_work(req
);
3463 * No ->flush(), safely close from here and just punt the
3464 * fput() to async context.
3466 __io_close_finish(req
);
3470 static int io_prep_sfr(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
3472 struct io_ring_ctx
*ctx
= req
->ctx
;
3477 if (unlikely(ctx
->flags
& IORING_SETUP_IOPOLL
))
3479 if (unlikely(sqe
->addr
|| sqe
->ioprio
|| sqe
->buf_index
))
3482 req
->sync
.off
= READ_ONCE(sqe
->off
);
3483 req
->sync
.len
= READ_ONCE(sqe
->len
);
3484 req
->sync
.flags
= READ_ONCE(sqe
->sync_range_flags
);
3488 static void __io_sync_file_range(struct io_kiocb
*req
)
3492 ret
= sync_file_range(req
->file
, req
->sync
.off
, req
->sync
.len
,
3495 req_set_fail_links(req
);
3496 io_cqring_add_event(req
, ret
);
3501 static void io_sync_file_range_finish(struct io_wq_work
**workptr
)
3503 struct io_kiocb
*req
= container_of(*workptr
, struct io_kiocb
, work
);
3505 if (io_req_cancelled(req
))
3507 __io_sync_file_range(req
);
3508 io_steal_work(req
, workptr
);
3511 static int io_sync_file_range(struct io_kiocb
*req
, bool force_nonblock
)
3513 /* sync_file_range always requires a blocking context */
3514 if (force_nonblock
) {
3515 req
->work
.func
= io_sync_file_range_finish
;
3519 __io_sync_file_range(req
);
3523 #if defined(CONFIG_NET)
3524 static int io_setup_async_msg(struct io_kiocb
*req
,
3525 struct io_async_msghdr
*kmsg
)
3529 if (io_alloc_async_ctx(req
)) {
3530 if (kmsg
->iov
!= kmsg
->fast_iov
)
3534 req
->flags
|= REQ_F_NEED_CLEANUP
;
3535 memcpy(&req
->io
->msg
, kmsg
, sizeof(*kmsg
));
3539 static int io_sendmsg_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
3541 struct io_sr_msg
*sr
= &req
->sr_msg
;
3542 struct io_async_ctx
*io
= req
->io
;
3545 sr
->msg_flags
= READ_ONCE(sqe
->msg_flags
);
3546 sr
->msg
= u64_to_user_ptr(READ_ONCE(sqe
->addr
));
3547 sr
->len
= READ_ONCE(sqe
->len
);
3549 #ifdef CONFIG_COMPAT
3550 if (req
->ctx
->compat
)
3551 sr
->msg_flags
|= MSG_CMSG_COMPAT
;
3554 if (!io
|| req
->opcode
== IORING_OP_SEND
)
3556 /* iovec is already imported */
3557 if (req
->flags
& REQ_F_NEED_CLEANUP
)
3560 io
->msg
.iov
= io
->msg
.fast_iov
;
3561 ret
= sendmsg_copy_msghdr(&io
->msg
.msg
, sr
->msg
, sr
->msg_flags
,
3564 req
->flags
|= REQ_F_NEED_CLEANUP
;
3568 static int io_sendmsg(struct io_kiocb
*req
, bool force_nonblock
)
3570 struct io_async_msghdr
*kmsg
= NULL
;
3571 struct socket
*sock
;
3574 if (unlikely(req
->ctx
->flags
& IORING_SETUP_IOPOLL
))
3577 sock
= sock_from_file(req
->file
, &ret
);
3579 struct io_async_ctx io
;
3583 kmsg
= &req
->io
->msg
;
3584 kmsg
->msg
.msg_name
= &req
->io
->msg
.addr
;
3585 /* if iov is set, it's allocated already */
3587 kmsg
->iov
= kmsg
->fast_iov
;
3588 kmsg
->msg
.msg_iter
.iov
= kmsg
->iov
;
3590 struct io_sr_msg
*sr
= &req
->sr_msg
;
3593 kmsg
->msg
.msg_name
= &io
.msg
.addr
;
3595 io
.msg
.iov
= io
.msg
.fast_iov
;
3596 ret
= sendmsg_copy_msghdr(&io
.msg
.msg
, sr
->msg
,
3597 sr
->msg_flags
, &io
.msg
.iov
);
3602 flags
= req
->sr_msg
.msg_flags
;
3603 if (flags
& MSG_DONTWAIT
)
3604 req
->flags
|= REQ_F_NOWAIT
;
3605 else if (force_nonblock
)
3606 flags
|= MSG_DONTWAIT
;
3608 ret
= __sys_sendmsg_sock(sock
, &kmsg
->msg
, flags
);
3609 if (force_nonblock
&& ret
== -EAGAIN
)
3610 return io_setup_async_msg(req
, kmsg
);
3611 if (ret
== -ERESTARTSYS
)
3615 if (kmsg
&& kmsg
->iov
!= kmsg
->fast_iov
)
3617 req
->flags
&= ~REQ_F_NEED_CLEANUP
;
3618 io_cqring_add_event(req
, ret
);
3620 req_set_fail_links(req
);
3625 static int io_send(struct io_kiocb
*req
, bool force_nonblock
)
3627 struct socket
*sock
;
3630 if (unlikely(req
->ctx
->flags
& IORING_SETUP_IOPOLL
))
3633 sock
= sock_from_file(req
->file
, &ret
);
3635 struct io_sr_msg
*sr
= &req
->sr_msg
;
3640 ret
= import_single_range(WRITE
, sr
->buf
, sr
->len
, &iov
,
3645 msg
.msg_name
= NULL
;
3646 msg
.msg_control
= NULL
;
3647 msg
.msg_controllen
= 0;
3648 msg
.msg_namelen
= 0;
3650 flags
= req
->sr_msg
.msg_flags
;
3651 if (flags
& MSG_DONTWAIT
)
3652 req
->flags
|= REQ_F_NOWAIT
;
3653 else if (force_nonblock
)
3654 flags
|= MSG_DONTWAIT
;
3656 msg
.msg_flags
= flags
;
3657 ret
= sock_sendmsg(sock
, &msg
);
3658 if (force_nonblock
&& ret
== -EAGAIN
)
3660 if (ret
== -ERESTARTSYS
)
3664 io_cqring_add_event(req
, ret
);
3666 req_set_fail_links(req
);
3671 static int __io_recvmsg_copy_hdr(struct io_kiocb
*req
, struct io_async_ctx
*io
)
3673 struct io_sr_msg
*sr
= &req
->sr_msg
;
3674 struct iovec __user
*uiov
;
3678 ret
= __copy_msghdr_from_user(&io
->msg
.msg
, sr
->msg
, &io
->msg
.uaddr
,
3683 if (req
->flags
& REQ_F_BUFFER_SELECT
) {
3686 if (copy_from_user(io
->msg
.iov
, uiov
, sizeof(*uiov
)))
3688 sr
->len
= io
->msg
.iov
[0].iov_len
;
3689 iov_iter_init(&io
->msg
.msg
.msg_iter
, READ
, io
->msg
.iov
, 1,
3693 ret
= import_iovec(READ
, uiov
, iov_len
, UIO_FASTIOV
,
3694 &io
->msg
.iov
, &io
->msg
.msg
.msg_iter
);
3702 #ifdef CONFIG_COMPAT
3703 static int __io_compat_recvmsg_copy_hdr(struct io_kiocb
*req
,
3704 struct io_async_ctx
*io
)
3706 struct compat_msghdr __user
*msg_compat
;
3707 struct io_sr_msg
*sr
= &req
->sr_msg
;
3708 struct compat_iovec __user
*uiov
;
3713 msg_compat
= (struct compat_msghdr __user
*) sr
->msg
;
3714 ret
= __get_compat_msghdr(&io
->msg
.msg
, msg_compat
, &io
->msg
.uaddr
,
3719 uiov
= compat_ptr(ptr
);
3720 if (req
->flags
& REQ_F_BUFFER_SELECT
) {
3721 compat_ssize_t clen
;
3725 if (!access_ok(uiov
, sizeof(*uiov
)))
3727 if (__get_user(clen
, &uiov
->iov_len
))
3731 sr
->len
= io
->msg
.iov
[0].iov_len
;
3734 ret
= compat_import_iovec(READ
, uiov
, len
, UIO_FASTIOV
,
3736 &io
->msg
.msg
.msg_iter
);
3745 static int io_recvmsg_copy_hdr(struct io_kiocb
*req
, struct io_async_ctx
*io
)
3747 io
->msg
.iov
= io
->msg
.fast_iov
;
3749 #ifdef CONFIG_COMPAT
3750 if (req
->ctx
->compat
)
3751 return __io_compat_recvmsg_copy_hdr(req
, io
);
3754 return __io_recvmsg_copy_hdr(req
, io
);
3757 static struct io_buffer
*io_recv_buffer_select(struct io_kiocb
*req
,
3758 int *cflags
, bool needs_lock
)
3760 struct io_sr_msg
*sr
= &req
->sr_msg
;
3761 struct io_buffer
*kbuf
;
3763 if (!(req
->flags
& REQ_F_BUFFER_SELECT
))
3766 kbuf
= io_buffer_select(req
, &sr
->len
, sr
->bgid
, sr
->kbuf
, needs_lock
);
3771 req
->flags
|= REQ_F_BUFFER_SELECTED
;
3773 *cflags
= kbuf
->bid
<< IORING_CQE_BUFFER_SHIFT
;
3774 *cflags
|= IORING_CQE_F_BUFFER
;
3778 static int io_recvmsg_prep(struct io_kiocb
*req
,
3779 const struct io_uring_sqe
*sqe
)
3781 struct io_sr_msg
*sr
= &req
->sr_msg
;
3782 struct io_async_ctx
*io
= req
->io
;
3785 sr
->msg_flags
= READ_ONCE(sqe
->msg_flags
);
3786 sr
->msg
= u64_to_user_ptr(READ_ONCE(sqe
->addr
));
3787 sr
->len
= READ_ONCE(sqe
->len
);
3788 sr
->bgid
= READ_ONCE(sqe
->buf_group
);
3790 #ifdef CONFIG_COMPAT
3791 if (req
->ctx
->compat
)
3792 sr
->msg_flags
|= MSG_CMSG_COMPAT
;
3795 if (!io
|| req
->opcode
== IORING_OP_RECV
)
3797 /* iovec is already imported */
3798 if (req
->flags
& REQ_F_NEED_CLEANUP
)
3801 ret
= io_recvmsg_copy_hdr(req
, io
);
3803 req
->flags
|= REQ_F_NEED_CLEANUP
;
3807 static int io_recvmsg(struct io_kiocb
*req
, bool force_nonblock
)
3809 struct io_async_msghdr
*kmsg
= NULL
;
3810 struct socket
*sock
;
3811 int ret
, cflags
= 0;
3813 if (unlikely(req
->ctx
->flags
& IORING_SETUP_IOPOLL
))
3816 sock
= sock_from_file(req
->file
, &ret
);
3818 struct io_buffer
*kbuf
;
3819 struct io_async_ctx io
;
3823 kmsg
= &req
->io
->msg
;
3824 kmsg
->msg
.msg_name
= &req
->io
->msg
.addr
;
3825 /* if iov is set, it's allocated already */
3827 kmsg
->iov
= kmsg
->fast_iov
;
3828 kmsg
->msg
.msg_iter
.iov
= kmsg
->iov
;
3831 kmsg
->msg
.msg_name
= &io
.msg
.addr
;
3833 ret
= io_recvmsg_copy_hdr(req
, &io
);
3838 kbuf
= io_recv_buffer_select(req
, &cflags
, !force_nonblock
);
3840 return PTR_ERR(kbuf
);
3842 kmsg
->fast_iov
[0].iov_base
= u64_to_user_ptr(kbuf
->addr
);
3843 iov_iter_init(&kmsg
->msg
.msg_iter
, READ
, kmsg
->iov
,
3844 1, req
->sr_msg
.len
);
3847 flags
= req
->sr_msg
.msg_flags
;
3848 if (flags
& MSG_DONTWAIT
)
3849 req
->flags
|= REQ_F_NOWAIT
;
3850 else if (force_nonblock
)
3851 flags
|= MSG_DONTWAIT
;
3853 ret
= __sys_recvmsg_sock(sock
, &kmsg
->msg
, req
->sr_msg
.msg
,
3854 kmsg
->uaddr
, flags
);
3855 if (force_nonblock
&& ret
== -EAGAIN
)
3856 return io_setup_async_msg(req
, kmsg
);
3857 if (ret
== -ERESTARTSYS
)
3861 if (kmsg
&& kmsg
->iov
!= kmsg
->fast_iov
)
3863 req
->flags
&= ~REQ_F_NEED_CLEANUP
;
3864 __io_cqring_add_event(req
, ret
, cflags
);
3866 req_set_fail_links(req
);
3871 static int io_recv(struct io_kiocb
*req
, bool force_nonblock
)
3873 struct io_buffer
*kbuf
= NULL
;
3874 struct socket
*sock
;
3875 int ret
, cflags
= 0;
3877 if (unlikely(req
->ctx
->flags
& IORING_SETUP_IOPOLL
))
3880 sock
= sock_from_file(req
->file
, &ret
);
3882 struct io_sr_msg
*sr
= &req
->sr_msg
;
3883 void __user
*buf
= sr
->buf
;
3888 kbuf
= io_recv_buffer_select(req
, &cflags
, !force_nonblock
);
3890 return PTR_ERR(kbuf
);
3892 buf
= u64_to_user_ptr(kbuf
->addr
);
3894 ret
= import_single_range(READ
, buf
, sr
->len
, &iov
,
3901 req
->flags
|= REQ_F_NEED_CLEANUP
;
3902 msg
.msg_name
= NULL
;
3903 msg
.msg_control
= NULL
;
3904 msg
.msg_controllen
= 0;
3905 msg
.msg_namelen
= 0;
3906 msg
.msg_iocb
= NULL
;
3909 flags
= req
->sr_msg
.msg_flags
;
3910 if (flags
& MSG_DONTWAIT
)
3911 req
->flags
|= REQ_F_NOWAIT
;
3912 else if (force_nonblock
)
3913 flags
|= MSG_DONTWAIT
;
3915 ret
= sock_recvmsg(sock
, &msg
, flags
);
3916 if (force_nonblock
&& ret
== -EAGAIN
)
3918 if (ret
== -ERESTARTSYS
)
3923 req
->flags
&= ~REQ_F_NEED_CLEANUP
;
3924 __io_cqring_add_event(req
, ret
, cflags
);
3926 req_set_fail_links(req
);
3931 static int io_accept_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
3933 struct io_accept
*accept
= &req
->accept
;
3935 if (unlikely(req
->ctx
->flags
& (IORING_SETUP_IOPOLL
|IORING_SETUP_SQPOLL
)))
3937 if (sqe
->ioprio
|| sqe
->len
|| sqe
->buf_index
)
3940 accept
->addr
= u64_to_user_ptr(READ_ONCE(sqe
->addr
));
3941 accept
->addr_len
= u64_to_user_ptr(READ_ONCE(sqe
->addr2
));
3942 accept
->flags
= READ_ONCE(sqe
->accept_flags
);
3943 accept
->nofile
= rlimit(RLIMIT_NOFILE
);
3947 static int __io_accept(struct io_kiocb
*req
, bool force_nonblock
)
3949 struct io_accept
*accept
= &req
->accept
;
3950 unsigned file_flags
;
3953 file_flags
= force_nonblock
? O_NONBLOCK
: 0;
3954 ret
= __sys_accept4_file(req
->file
, file_flags
, accept
->addr
,
3955 accept
->addr_len
, accept
->flags
,
3957 if (ret
== -EAGAIN
&& force_nonblock
)
3959 if (ret
== -ERESTARTSYS
)
3962 req_set_fail_links(req
);
3963 io_cqring_add_event(req
, ret
);
3968 static void io_accept_finish(struct io_wq_work
**workptr
)
3970 struct io_kiocb
*req
= container_of(*workptr
, struct io_kiocb
, work
);
3972 if (io_req_cancelled(req
))
3974 __io_accept(req
, false);
3975 io_steal_work(req
, workptr
);
3978 static int io_accept(struct io_kiocb
*req
, bool force_nonblock
)
3982 ret
= __io_accept(req
, force_nonblock
);
3983 if (ret
== -EAGAIN
&& force_nonblock
) {
3984 req
->work
.func
= io_accept_finish
;
3990 static int io_connect_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
3992 struct io_connect
*conn
= &req
->connect
;
3993 struct io_async_ctx
*io
= req
->io
;
3995 if (unlikely(req
->ctx
->flags
& (IORING_SETUP_IOPOLL
|IORING_SETUP_SQPOLL
)))
3997 if (sqe
->ioprio
|| sqe
->len
|| sqe
->buf_index
|| sqe
->rw_flags
)
4000 conn
->addr
= u64_to_user_ptr(READ_ONCE(sqe
->addr
));
4001 conn
->addr_len
= READ_ONCE(sqe
->addr2
);
4006 return move_addr_to_kernel(conn
->addr
, conn
->addr_len
,
4007 &io
->connect
.address
);
4010 static int io_connect(struct io_kiocb
*req
, bool force_nonblock
)
4012 struct io_async_ctx __io
, *io
;
4013 unsigned file_flags
;
4019 ret
= move_addr_to_kernel(req
->connect
.addr
,
4020 req
->connect
.addr_len
,
4021 &__io
.connect
.address
);
4027 file_flags
= force_nonblock
? O_NONBLOCK
: 0;
4029 ret
= __sys_connect_file(req
->file
, &io
->connect
.address
,
4030 req
->connect
.addr_len
, file_flags
);
4031 if ((ret
== -EAGAIN
|| ret
== -EINPROGRESS
) && force_nonblock
) {
4034 if (io_alloc_async_ctx(req
)) {
4038 memcpy(&req
->io
->connect
, &__io
.connect
, sizeof(__io
.connect
));
4041 if (ret
== -ERESTARTSYS
)
4045 req_set_fail_links(req
);
4046 io_cqring_add_event(req
, ret
);
4050 #else /* !CONFIG_NET */
4051 static int io_sendmsg_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
4056 static int io_sendmsg(struct io_kiocb
*req
, bool force_nonblock
)
4061 static int io_send(struct io_kiocb
*req
, bool force_nonblock
)
4066 static int io_recvmsg_prep(struct io_kiocb
*req
,
4067 const struct io_uring_sqe
*sqe
)
4072 static int io_recvmsg(struct io_kiocb
*req
, bool force_nonblock
)
4077 static int io_recv(struct io_kiocb
*req
, bool force_nonblock
)
4082 static int io_accept_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
4087 static int io_accept(struct io_kiocb
*req
, bool force_nonblock
)
4092 static int io_connect_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
4097 static int io_connect(struct io_kiocb
*req
, bool force_nonblock
)
4101 #endif /* CONFIG_NET */
4103 struct io_poll_table
{
4104 struct poll_table_struct pt
;
4105 struct io_kiocb
*req
;
4109 static int __io_async_wake(struct io_kiocb
*req
, struct io_poll_iocb
*poll
,
4110 __poll_t mask
, task_work_func_t func
)
4112 struct task_struct
*tsk
;
4115 /* for instances that support it check for an event match first: */
4116 if (mask
&& !(mask
& poll
->events
))
4119 trace_io_uring_task_add(req
->ctx
, req
->opcode
, req
->user_data
, mask
);
4121 list_del_init(&poll
->wait
.entry
);
4125 init_task_work(&req
->task_work
, func
);
4127 * If this fails, then the task is exiting. When a task exits, the
4128 * work gets canceled, so just cancel this request as well instead
4129 * of executing it. We can't safely execute it anyway, as we may not
4130 * have the needed state needed for it anyway.
4132 ret
= task_work_add(tsk
, &req
->task_work
, true);
4133 if (unlikely(ret
)) {
4134 WRITE_ONCE(poll
->canceled
, true);
4135 tsk
= io_wq_get_task(req
->ctx
->io_wq
);
4136 task_work_add(tsk
, &req
->task_work
, true);
4138 wake_up_process(tsk
);
4142 static bool io_poll_rewait(struct io_kiocb
*req
, struct io_poll_iocb
*poll
)
4143 __acquires(&req
->ctx
->completion_lock
)
4145 struct io_ring_ctx
*ctx
= req
->ctx
;
4147 if (!req
->result
&& !READ_ONCE(poll
->canceled
)) {
4148 struct poll_table_struct pt
= { ._key
= poll
->events
};
4150 req
->result
= vfs_poll(req
->file
, &pt
) & poll
->events
;
4153 spin_lock_irq(&ctx
->completion_lock
);
4154 if (!req
->result
&& !READ_ONCE(poll
->canceled
)) {
4155 add_wait_queue(poll
->head
, &poll
->wait
);
4162 static void io_poll_remove_double(struct io_kiocb
*req
)
4164 struct io_poll_iocb
*poll
= (struct io_poll_iocb
*) req
->io
;
4166 lockdep_assert_held(&req
->ctx
->completion_lock
);
4168 if (poll
&& poll
->head
) {
4169 struct wait_queue_head
*head
= poll
->head
;
4171 spin_lock(&head
->lock
);
4172 list_del_init(&poll
->wait
.entry
);
4173 if (poll
->wait
.private)
4174 refcount_dec(&req
->refs
);
4176 spin_unlock(&head
->lock
);
4180 static void io_poll_complete(struct io_kiocb
*req
, __poll_t mask
, int error
)
4182 struct io_ring_ctx
*ctx
= req
->ctx
;
4184 io_poll_remove_double(req
);
4185 req
->poll
.done
= true;
4186 io_cqring_fill_event(req
, error
? error
: mangle_poll(mask
));
4187 io_commit_cqring(ctx
);
4190 static void io_poll_task_handler(struct io_kiocb
*req
, struct io_kiocb
**nxt
)
4192 struct io_ring_ctx
*ctx
= req
->ctx
;
4194 if (io_poll_rewait(req
, &req
->poll
)) {
4195 spin_unlock_irq(&ctx
->completion_lock
);
4199 hash_del(&req
->hash_node
);
4200 io_poll_complete(req
, req
->result
, 0);
4201 req
->flags
|= REQ_F_COMP_LOCKED
;
4202 io_put_req_find_next(req
, nxt
);
4203 spin_unlock_irq(&ctx
->completion_lock
);
4205 io_cqring_ev_posted(ctx
);
4208 static void io_poll_task_func(struct callback_head
*cb
)
4210 struct io_kiocb
*req
= container_of(cb
, struct io_kiocb
, task_work
);
4211 struct io_kiocb
*nxt
= NULL
;
4213 io_poll_task_handler(req
, &nxt
);
4215 struct io_ring_ctx
*ctx
= nxt
->ctx
;
4217 mutex_lock(&ctx
->uring_lock
);
4218 __io_queue_sqe(nxt
, NULL
);
4219 mutex_unlock(&ctx
->uring_lock
);
4223 static int io_poll_double_wake(struct wait_queue_entry
*wait
, unsigned mode
,
4224 int sync
, void *key
)
4226 struct io_kiocb
*req
= wait
->private;
4227 struct io_poll_iocb
*poll
= (struct io_poll_iocb
*) req
->io
;
4228 __poll_t mask
= key_to_poll(key
);
4230 /* for instances that support it check for an event match first: */
4231 if (mask
&& !(mask
& poll
->events
))
4234 if (req
->poll
.head
) {
4237 spin_lock(&req
->poll
.head
->lock
);
4238 done
= list_empty(&req
->poll
.wait
.entry
);
4240 list_del_init(&req
->poll
.wait
.entry
);
4241 spin_unlock(&req
->poll
.head
->lock
);
4243 __io_async_wake(req
, poll
, mask
, io_poll_task_func
);
4245 refcount_dec(&req
->refs
);
4249 static void io_init_poll_iocb(struct io_poll_iocb
*poll
, __poll_t events
,
4250 wait_queue_func_t wake_func
)
4254 poll
->canceled
= false;
4255 poll
->events
= events
;
4256 INIT_LIST_HEAD(&poll
->wait
.entry
);
4257 init_waitqueue_func_entry(&poll
->wait
, wake_func
);
4260 static void __io_queue_proc(struct io_poll_iocb
*poll
, struct io_poll_table
*pt
,
4261 struct wait_queue_head
*head
)
4263 struct io_kiocb
*req
= pt
->req
;
4266 * If poll->head is already set, it's because the file being polled
4267 * uses multiple waitqueues for poll handling (eg one for read, one
4268 * for write). Setup a separate io_poll_iocb if this happens.
4270 if (unlikely(poll
->head
)) {
4271 /* already have a 2nd entry, fail a third attempt */
4273 pt
->error
= -EINVAL
;
4276 poll
= kmalloc(sizeof(*poll
), GFP_ATOMIC
);
4278 pt
->error
= -ENOMEM
;
4281 io_init_poll_iocb(poll
, req
->poll
.events
, io_poll_double_wake
);
4282 refcount_inc(&req
->refs
);
4283 poll
->wait
.private = req
;
4284 req
->io
= (void *) poll
;
4289 add_wait_queue(head
, &poll
->wait
);
4292 static void io_async_queue_proc(struct file
*file
, struct wait_queue_head
*head
,
4293 struct poll_table_struct
*p
)
4295 struct io_poll_table
*pt
= container_of(p
, struct io_poll_table
, pt
);
4297 __io_queue_proc(&pt
->req
->apoll
->poll
, pt
, head
);
4300 static void io_async_task_func(struct callback_head
*cb
)
4302 struct io_kiocb
*req
= container_of(cb
, struct io_kiocb
, task_work
);
4303 struct async_poll
*apoll
= req
->apoll
;
4304 struct io_ring_ctx
*ctx
= req
->ctx
;
4307 trace_io_uring_task_run(req
->ctx
, req
->opcode
, req
->user_data
);
4309 if (io_poll_rewait(req
, &apoll
->poll
)) {
4310 spin_unlock_irq(&ctx
->completion_lock
);
4314 if (hash_hashed(&req
->hash_node
))
4315 hash_del(&req
->hash_node
);
4317 canceled
= READ_ONCE(apoll
->poll
.canceled
);
4319 io_cqring_fill_event(req
, -ECANCELED
);
4320 io_commit_cqring(ctx
);
4323 spin_unlock_irq(&ctx
->completion_lock
);
4325 /* restore ->work in case we need to retry again */
4326 memcpy(&req
->work
, &apoll
->work
, sizeof(req
->work
));
4330 io_cqring_ev_posted(ctx
);
4331 req_set_fail_links(req
);
4332 io_double_put_req(req
);
4336 __set_current_state(TASK_RUNNING
);
4337 mutex_lock(&ctx
->uring_lock
);
4338 __io_queue_sqe(req
, NULL
);
4339 mutex_unlock(&ctx
->uring_lock
);
4344 static int io_async_wake(struct wait_queue_entry
*wait
, unsigned mode
, int sync
,
4347 struct io_kiocb
*req
= wait
->private;
4348 struct io_poll_iocb
*poll
= &req
->apoll
->poll
;
4350 trace_io_uring_poll_wake(req
->ctx
, req
->opcode
, req
->user_data
,
4353 return __io_async_wake(req
, poll
, key_to_poll(key
), io_async_task_func
);
4356 static void io_poll_req_insert(struct io_kiocb
*req
)
4358 struct io_ring_ctx
*ctx
= req
->ctx
;
4359 struct hlist_head
*list
;
4361 list
= &ctx
->cancel_hash
[hash_long(req
->user_data
, ctx
->cancel_hash_bits
)];
4362 hlist_add_head(&req
->hash_node
, list
);
4365 static __poll_t
__io_arm_poll_handler(struct io_kiocb
*req
,
4366 struct io_poll_iocb
*poll
,
4367 struct io_poll_table
*ipt
, __poll_t mask
,
4368 wait_queue_func_t wake_func
)
4369 __acquires(&ctx
->completion_lock
)
4371 struct io_ring_ctx
*ctx
= req
->ctx
;
4372 bool cancel
= false;
4374 poll
->file
= req
->file
;
4375 io_init_poll_iocb(poll
, mask
, wake_func
);
4376 poll
->wait
.private = req
;
4378 ipt
->pt
._key
= mask
;
4380 ipt
->error
= -EINVAL
;
4382 mask
= vfs_poll(req
->file
, &ipt
->pt
) & poll
->events
;
4384 spin_lock_irq(&ctx
->completion_lock
);
4385 if (likely(poll
->head
)) {
4386 spin_lock(&poll
->head
->lock
);
4387 if (unlikely(list_empty(&poll
->wait
.entry
))) {
4393 if (mask
|| ipt
->error
)
4394 list_del_init(&poll
->wait
.entry
);
4396 WRITE_ONCE(poll
->canceled
, true);
4397 else if (!poll
->done
) /* actually waiting for an event */
4398 io_poll_req_insert(req
);
4399 spin_unlock(&poll
->head
->lock
);
4405 static bool io_arm_poll_handler(struct io_kiocb
*req
)
4407 const struct io_op_def
*def
= &io_op_defs
[req
->opcode
];
4408 struct io_ring_ctx
*ctx
= req
->ctx
;
4409 struct async_poll
*apoll
;
4410 struct io_poll_table ipt
;
4414 if (!req
->file
|| !file_can_poll(req
->file
))
4416 if (req
->flags
& (REQ_F_MUST_PUNT
| REQ_F_POLLED
))
4418 if (!def
->pollin
&& !def
->pollout
)
4421 apoll
= kmalloc(sizeof(*apoll
), GFP_ATOMIC
);
4422 if (unlikely(!apoll
))
4425 req
->flags
|= REQ_F_POLLED
;
4426 memcpy(&apoll
->work
, &req
->work
, sizeof(req
->work
));
4427 had_io
= req
->io
!= NULL
;
4429 get_task_struct(current
);
4430 req
->task
= current
;
4432 INIT_HLIST_NODE(&req
->hash_node
);
4436 mask
|= POLLIN
| POLLRDNORM
;
4438 mask
|= POLLOUT
| POLLWRNORM
;
4439 mask
|= POLLERR
| POLLPRI
;
4441 ipt
.pt
._qproc
= io_async_queue_proc
;
4443 ret
= __io_arm_poll_handler(req
, &apoll
->poll
, &ipt
, mask
,
4447 /* only remove double add if we did it here */
4449 io_poll_remove_double(req
);
4450 spin_unlock_irq(&ctx
->completion_lock
);
4451 memcpy(&req
->work
, &apoll
->work
, sizeof(req
->work
));
4455 spin_unlock_irq(&ctx
->completion_lock
);
4456 trace_io_uring_poll_arm(ctx
, req
->opcode
, req
->user_data
, mask
,
4457 apoll
->poll
.events
);
4461 static bool __io_poll_remove_one(struct io_kiocb
*req
,
4462 struct io_poll_iocb
*poll
)
4464 bool do_complete
= false;
4466 spin_lock(&poll
->head
->lock
);
4467 WRITE_ONCE(poll
->canceled
, true);
4468 if (!list_empty(&poll
->wait
.entry
)) {
4469 list_del_init(&poll
->wait
.entry
);
4472 spin_unlock(&poll
->head
->lock
);
4473 hash_del(&req
->hash_node
);
4477 static bool io_poll_remove_one(struct io_kiocb
*req
)
4481 if (req
->opcode
== IORING_OP_POLL_ADD
) {
4482 io_poll_remove_double(req
);
4483 do_complete
= __io_poll_remove_one(req
, &req
->poll
);
4485 struct async_poll
*apoll
= req
->apoll
;
4487 /* non-poll requests have submit ref still */
4488 do_complete
= __io_poll_remove_one(req
, &apoll
->poll
);
4492 * restore ->work because we will call
4493 * io_req_work_drop_env below when dropping the
4496 memcpy(&req
->work
, &apoll
->work
, sizeof(req
->work
));
4502 io_cqring_fill_event(req
, -ECANCELED
);
4503 io_commit_cqring(req
->ctx
);
4504 req
->flags
|= REQ_F_COMP_LOCKED
;
4511 static void io_poll_remove_all(struct io_ring_ctx
*ctx
)
4513 struct hlist_node
*tmp
;
4514 struct io_kiocb
*req
;
4517 spin_lock_irq(&ctx
->completion_lock
);
4518 for (i
= 0; i
< (1U << ctx
->cancel_hash_bits
); i
++) {
4519 struct hlist_head
*list
;
4521 list
= &ctx
->cancel_hash
[i
];
4522 hlist_for_each_entry_safe(req
, tmp
, list
, hash_node
)
4523 posted
+= io_poll_remove_one(req
);
4525 spin_unlock_irq(&ctx
->completion_lock
);
4528 io_cqring_ev_posted(ctx
);
4531 static int io_poll_cancel(struct io_ring_ctx
*ctx
, __u64 sqe_addr
)
4533 struct hlist_head
*list
;
4534 struct io_kiocb
*req
;
4536 list
= &ctx
->cancel_hash
[hash_long(sqe_addr
, ctx
->cancel_hash_bits
)];
4537 hlist_for_each_entry(req
, list
, hash_node
) {
4538 if (sqe_addr
!= req
->user_data
)
4540 if (io_poll_remove_one(req
))
4548 static int io_poll_remove_prep(struct io_kiocb
*req
,
4549 const struct io_uring_sqe
*sqe
)
4551 if (unlikely(req
->ctx
->flags
& IORING_SETUP_IOPOLL
))
4553 if (sqe
->ioprio
|| sqe
->off
|| sqe
->len
|| sqe
->buf_index
||
4557 req
->poll
.addr
= READ_ONCE(sqe
->addr
);
4562 * Find a running poll command that matches one specified in sqe->addr,
4563 * and remove it if found.
4565 static int io_poll_remove(struct io_kiocb
*req
)
4567 struct io_ring_ctx
*ctx
= req
->ctx
;
4571 addr
= req
->poll
.addr
;
4572 spin_lock_irq(&ctx
->completion_lock
);
4573 ret
= io_poll_cancel(ctx
, addr
);
4574 spin_unlock_irq(&ctx
->completion_lock
);
4576 io_cqring_add_event(req
, ret
);
4578 req_set_fail_links(req
);
4583 static int io_poll_wake(struct wait_queue_entry
*wait
, unsigned mode
, int sync
,
4586 struct io_kiocb
*req
= wait
->private;
4587 struct io_poll_iocb
*poll
= &req
->poll
;
4589 return __io_async_wake(req
, poll
, key_to_poll(key
), io_poll_task_func
);
4592 static void io_poll_queue_proc(struct file
*file
, struct wait_queue_head
*head
,
4593 struct poll_table_struct
*p
)
4595 struct io_poll_table
*pt
= container_of(p
, struct io_poll_table
, pt
);
4597 __io_queue_proc(&pt
->req
->poll
, pt
, head
);
4600 static int io_poll_add_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
4602 struct io_poll_iocb
*poll
= &req
->poll
;
4605 if (unlikely(req
->ctx
->flags
& IORING_SETUP_IOPOLL
))
4607 if (sqe
->addr
|| sqe
->ioprio
|| sqe
->off
|| sqe
->len
|| sqe
->buf_index
)
4612 events
= READ_ONCE(sqe
->poll_events
);
4613 poll
->events
= demangle_poll(events
) | EPOLLERR
| EPOLLHUP
;
4615 get_task_struct(current
);
4616 req
->task
= current
;
4620 static int io_poll_add(struct io_kiocb
*req
)
4622 struct io_poll_iocb
*poll
= &req
->poll
;
4623 struct io_ring_ctx
*ctx
= req
->ctx
;
4624 struct io_poll_table ipt
;
4627 INIT_HLIST_NODE(&req
->hash_node
);
4628 INIT_LIST_HEAD(&req
->list
);
4629 ipt
.pt
._qproc
= io_poll_queue_proc
;
4631 mask
= __io_arm_poll_handler(req
, &req
->poll
, &ipt
, poll
->events
,
4634 if (mask
) { /* no async, we'd stolen it */
4636 io_poll_complete(req
, mask
, 0);
4638 spin_unlock_irq(&ctx
->completion_lock
);
4641 io_cqring_ev_posted(ctx
);
4647 static enum hrtimer_restart
io_timeout_fn(struct hrtimer
*timer
)
4649 struct io_timeout_data
*data
= container_of(timer
,
4650 struct io_timeout_data
, timer
);
4651 struct io_kiocb
*req
= data
->req
;
4652 struct io_ring_ctx
*ctx
= req
->ctx
;
4653 unsigned long flags
;
4655 atomic_inc(&ctx
->cq_timeouts
);
4657 spin_lock_irqsave(&ctx
->completion_lock
, flags
);
4659 * We could be racing with timeout deletion. If the list is empty,
4660 * then timeout lookup already found it and will be handling it.
4662 if (!list_empty(&req
->list
)) {
4663 struct io_kiocb
*prev
;
4666 * Adjust the reqs sequence before the current one because it
4667 * will consume a slot in the cq_ring and the cq_tail
4668 * pointer will be increased, otherwise other timeout reqs may
4669 * return in advance without waiting for enough wait_nr.
4672 list_for_each_entry_continue_reverse(prev
, &ctx
->timeout_list
, list
)
4674 list_del_init(&req
->list
);
4677 io_cqring_fill_event(req
, -ETIME
);
4678 io_commit_cqring(ctx
);
4679 spin_unlock_irqrestore(&ctx
->completion_lock
, flags
);
4681 io_cqring_ev_posted(ctx
);
4682 req_set_fail_links(req
);
4684 return HRTIMER_NORESTART
;
4687 static int io_timeout_cancel(struct io_ring_ctx
*ctx
, __u64 user_data
)
4689 struct io_kiocb
*req
;
4692 list_for_each_entry(req
, &ctx
->timeout_list
, list
) {
4693 if (user_data
== req
->user_data
) {
4694 list_del_init(&req
->list
);
4703 ret
= hrtimer_try_to_cancel(&req
->io
->timeout
.timer
);
4707 req_set_fail_links(req
);
4708 io_cqring_fill_event(req
, -ECANCELED
);
4713 static int io_timeout_remove_prep(struct io_kiocb
*req
,
4714 const struct io_uring_sqe
*sqe
)
4716 if (unlikely(req
->ctx
->flags
& IORING_SETUP_IOPOLL
))
4718 if (sqe
->flags
|| sqe
->ioprio
|| sqe
->buf_index
|| sqe
->len
)
4721 req
->timeout
.addr
= READ_ONCE(sqe
->addr
);
4722 req
->timeout
.flags
= READ_ONCE(sqe
->timeout_flags
);
4723 if (req
->timeout
.flags
)
4730 * Remove or update an existing timeout command
4732 static int io_timeout_remove(struct io_kiocb
*req
)
4734 struct io_ring_ctx
*ctx
= req
->ctx
;
4737 spin_lock_irq(&ctx
->completion_lock
);
4738 ret
= io_timeout_cancel(ctx
, req
->timeout
.addr
);
4740 io_cqring_fill_event(req
, ret
);
4741 io_commit_cqring(ctx
);
4742 spin_unlock_irq(&ctx
->completion_lock
);
4743 io_cqring_ev_posted(ctx
);
4745 req_set_fail_links(req
);
4750 static int io_timeout_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
,
4751 bool is_timeout_link
)
4753 struct io_timeout_data
*data
;
4756 if (unlikely(req
->ctx
->flags
& IORING_SETUP_IOPOLL
))
4758 if (sqe
->ioprio
|| sqe
->buf_index
|| sqe
->len
!= 1)
4760 if (sqe
->off
&& is_timeout_link
)
4762 flags
= READ_ONCE(sqe
->timeout_flags
);
4763 if (flags
& ~IORING_TIMEOUT_ABS
)
4766 req
->timeout
.count
= READ_ONCE(sqe
->off
);
4768 if (!req
->io
&& io_alloc_async_ctx(req
))
4771 data
= &req
->io
->timeout
;
4773 req
->flags
|= REQ_F_TIMEOUT
;
4775 if (get_timespec64(&data
->ts
, u64_to_user_ptr(sqe
->addr
)))
4778 if (flags
& IORING_TIMEOUT_ABS
)
4779 data
->mode
= HRTIMER_MODE_ABS
;
4781 data
->mode
= HRTIMER_MODE_REL
;
4783 hrtimer_init(&data
->timer
, CLOCK_MONOTONIC
, data
->mode
);
4787 static int io_timeout(struct io_kiocb
*req
)
4789 struct io_ring_ctx
*ctx
= req
->ctx
;
4790 struct io_timeout_data
*data
;
4791 struct list_head
*entry
;
4793 u32 count
= req
->timeout
.count
;
4794 u32 seq
= req
->sequence
;
4796 data
= &req
->io
->timeout
;
4799 * sqe->off holds how many events that need to occur for this
4800 * timeout event to be satisfied. If it isn't set, then this is
4801 * a pure timeout request, sequence isn't used.
4804 req
->flags
|= REQ_F_TIMEOUT_NOSEQ
;
4805 spin_lock_irq(&ctx
->completion_lock
);
4806 entry
= ctx
->timeout_list
.prev
;
4810 req
->sequence
= seq
+ count
;
4813 * Insertion sort, ensuring the first entry in the list is always
4814 * the one we need first.
4816 spin_lock_irq(&ctx
->completion_lock
);
4817 list_for_each_prev(entry
, &ctx
->timeout_list
) {
4818 struct io_kiocb
*nxt
= list_entry(entry
, struct io_kiocb
, list
);
4820 long long tmp
, tmp_nxt
;
4821 u32 nxt_offset
= nxt
->timeout
.count
;
4823 if (nxt
->flags
& REQ_F_TIMEOUT_NOSEQ
)
4827 * Since seq + count can overflow, use type long
4830 tmp
= (long long)seq
+ count
;
4831 nxt_seq
= nxt
->sequence
- nxt_offset
;
4832 tmp_nxt
= (long long)nxt_seq
+ nxt_offset
;
4835 * cached_sq_head may overflow, and it will never overflow twice
4836 * once there is some timeout req still be valid.
4845 * Sequence of reqs after the insert one and itself should
4846 * be adjusted because each timeout req consumes a slot.
4851 req
->sequence
-= span
;
4853 list_add(&req
->list
, entry
);
4854 data
->timer
.function
= io_timeout_fn
;
4855 hrtimer_start(&data
->timer
, timespec64_to_ktime(data
->ts
), data
->mode
);
4856 spin_unlock_irq(&ctx
->completion_lock
);
4860 static bool io_cancel_cb(struct io_wq_work
*work
, void *data
)
4862 struct io_kiocb
*req
= container_of(work
, struct io_kiocb
, work
);
4864 return req
->user_data
== (unsigned long) data
;
4867 static int io_async_cancel_one(struct io_ring_ctx
*ctx
, void *sqe_addr
)
4869 enum io_wq_cancel cancel_ret
;
4872 cancel_ret
= io_wq_cancel_cb(ctx
->io_wq
, io_cancel_cb
, sqe_addr
);
4873 switch (cancel_ret
) {
4874 case IO_WQ_CANCEL_OK
:
4877 case IO_WQ_CANCEL_RUNNING
:
4880 case IO_WQ_CANCEL_NOTFOUND
:
4888 static void io_async_find_and_cancel(struct io_ring_ctx
*ctx
,
4889 struct io_kiocb
*req
, __u64 sqe_addr
,
4892 unsigned long flags
;
4895 ret
= io_async_cancel_one(ctx
, (void *) (unsigned long) sqe_addr
);
4896 if (ret
!= -ENOENT
) {
4897 spin_lock_irqsave(&ctx
->completion_lock
, flags
);
4901 spin_lock_irqsave(&ctx
->completion_lock
, flags
);
4902 ret
= io_timeout_cancel(ctx
, sqe_addr
);
4905 ret
= io_poll_cancel(ctx
, sqe_addr
);
4909 io_cqring_fill_event(req
, ret
);
4910 io_commit_cqring(ctx
);
4911 spin_unlock_irqrestore(&ctx
->completion_lock
, flags
);
4912 io_cqring_ev_posted(ctx
);
4915 req_set_fail_links(req
);
4919 static int io_async_cancel_prep(struct io_kiocb
*req
,
4920 const struct io_uring_sqe
*sqe
)
4922 if (unlikely(req
->ctx
->flags
& IORING_SETUP_IOPOLL
))
4924 if (sqe
->flags
|| sqe
->ioprio
|| sqe
->off
|| sqe
->len
||
4928 req
->cancel
.addr
= READ_ONCE(sqe
->addr
);
4932 static int io_async_cancel(struct io_kiocb
*req
)
4934 struct io_ring_ctx
*ctx
= req
->ctx
;
4936 io_async_find_and_cancel(ctx
, req
, req
->cancel
.addr
, 0);
4940 static int io_files_update_prep(struct io_kiocb
*req
,
4941 const struct io_uring_sqe
*sqe
)
4943 if (sqe
->flags
|| sqe
->ioprio
|| sqe
->rw_flags
)
4946 req
->files_update
.offset
= READ_ONCE(sqe
->off
);
4947 req
->files_update
.nr_args
= READ_ONCE(sqe
->len
);
4948 if (!req
->files_update
.nr_args
)
4950 req
->files_update
.arg
= READ_ONCE(sqe
->addr
);
4954 static int io_files_update(struct io_kiocb
*req
, bool force_nonblock
)
4956 struct io_ring_ctx
*ctx
= req
->ctx
;
4957 struct io_uring_files_update up
;
4963 up
.offset
= req
->files_update
.offset
;
4964 up
.fds
= req
->files_update
.arg
;
4966 mutex_lock(&ctx
->uring_lock
);
4967 ret
= __io_sqe_files_update(ctx
, &up
, req
->files_update
.nr_args
);
4968 mutex_unlock(&ctx
->uring_lock
);
4971 req_set_fail_links(req
);
4972 io_cqring_add_event(req
, ret
);
4977 static int io_req_defer_prep(struct io_kiocb
*req
,
4978 const struct io_uring_sqe
*sqe
)
4985 if (io_op_defs
[req
->opcode
].file_table
) {
4986 ret
= io_grab_files(req
);
4991 io_req_work_grab_env(req
, &io_op_defs
[req
->opcode
]);
4993 switch (req
->opcode
) {
4996 case IORING_OP_READV
:
4997 case IORING_OP_READ_FIXED
:
4998 case IORING_OP_READ
:
4999 ret
= io_read_prep(req
, sqe
, true);
5001 case IORING_OP_WRITEV
:
5002 case IORING_OP_WRITE_FIXED
:
5003 case IORING_OP_WRITE
:
5004 ret
= io_write_prep(req
, sqe
, true);
5006 case IORING_OP_POLL_ADD
:
5007 ret
= io_poll_add_prep(req
, sqe
);
5009 case IORING_OP_POLL_REMOVE
:
5010 ret
= io_poll_remove_prep(req
, sqe
);
5012 case IORING_OP_FSYNC
:
5013 ret
= io_prep_fsync(req
, sqe
);
5015 case IORING_OP_SYNC_FILE_RANGE
:
5016 ret
= io_prep_sfr(req
, sqe
);
5018 case IORING_OP_SENDMSG
:
5019 case IORING_OP_SEND
:
5020 ret
= io_sendmsg_prep(req
, sqe
);
5022 case IORING_OP_RECVMSG
:
5023 case IORING_OP_RECV
:
5024 ret
= io_recvmsg_prep(req
, sqe
);
5026 case IORING_OP_CONNECT
:
5027 ret
= io_connect_prep(req
, sqe
);
5029 case IORING_OP_TIMEOUT
:
5030 ret
= io_timeout_prep(req
, sqe
, false);
5032 case IORING_OP_TIMEOUT_REMOVE
:
5033 ret
= io_timeout_remove_prep(req
, sqe
);
5035 case IORING_OP_ASYNC_CANCEL
:
5036 ret
= io_async_cancel_prep(req
, sqe
);
5038 case IORING_OP_LINK_TIMEOUT
:
5039 ret
= io_timeout_prep(req
, sqe
, true);
5041 case IORING_OP_ACCEPT
:
5042 ret
= io_accept_prep(req
, sqe
);
5044 case IORING_OP_FALLOCATE
:
5045 ret
= io_fallocate_prep(req
, sqe
);
5047 case IORING_OP_OPENAT
:
5048 ret
= io_openat_prep(req
, sqe
);
5050 case IORING_OP_CLOSE
:
5051 ret
= io_close_prep(req
, sqe
);
5053 case IORING_OP_FILES_UPDATE
:
5054 ret
= io_files_update_prep(req
, sqe
);
5056 case IORING_OP_STATX
:
5057 ret
= io_statx_prep(req
, sqe
);
5059 case IORING_OP_FADVISE
:
5060 ret
= io_fadvise_prep(req
, sqe
);
5062 case IORING_OP_MADVISE
:
5063 ret
= io_madvise_prep(req
, sqe
);
5065 case IORING_OP_OPENAT2
:
5066 ret
= io_openat2_prep(req
, sqe
);
5068 case IORING_OP_EPOLL_CTL
:
5069 ret
= io_epoll_ctl_prep(req
, sqe
);
5071 case IORING_OP_SPLICE
:
5072 ret
= io_splice_prep(req
, sqe
);
5074 case IORING_OP_PROVIDE_BUFFERS
:
5075 ret
= io_provide_buffers_prep(req
, sqe
);
5077 case IORING_OP_REMOVE_BUFFERS
:
5078 ret
= io_remove_buffers_prep(req
, sqe
);
5081 printk_once(KERN_WARNING
"io_uring: unhandled opcode %d\n",
5090 static int io_req_defer(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
5092 struct io_ring_ctx
*ctx
= req
->ctx
;
5095 /* Still need defer if there is pending req in defer list. */
5096 if (!req_need_defer(req
) && list_empty_careful(&ctx
->defer_list
))
5100 if (io_alloc_async_ctx(req
))
5102 ret
= io_req_defer_prep(req
, sqe
);
5107 spin_lock_irq(&ctx
->completion_lock
);
5108 if (!req_need_defer(req
) && list_empty(&ctx
->defer_list
)) {
5109 spin_unlock_irq(&ctx
->completion_lock
);
5113 trace_io_uring_defer(ctx
, req
, req
->user_data
);
5114 list_add_tail(&req
->list
, &ctx
->defer_list
);
5115 spin_unlock_irq(&ctx
->completion_lock
);
5116 return -EIOCBQUEUED
;
5119 static void io_cleanup_req(struct io_kiocb
*req
)
5121 struct io_async_ctx
*io
= req
->io
;
5123 switch (req
->opcode
) {
5124 case IORING_OP_READV
:
5125 case IORING_OP_READ_FIXED
:
5126 case IORING_OP_READ
:
5127 if (req
->flags
& REQ_F_BUFFER_SELECTED
)
5128 kfree((void *)(unsigned long)req
->rw
.addr
);
5130 case IORING_OP_WRITEV
:
5131 case IORING_OP_WRITE_FIXED
:
5132 case IORING_OP_WRITE
:
5133 if (io
->rw
.iov
!= io
->rw
.fast_iov
)
5136 case IORING_OP_RECVMSG
:
5137 if (req
->flags
& REQ_F_BUFFER_SELECTED
)
5138 kfree(req
->sr_msg
.kbuf
);
5140 case IORING_OP_SENDMSG
:
5141 if (io
->msg
.iov
!= io
->msg
.fast_iov
)
5144 case IORING_OP_RECV
:
5145 if (req
->flags
& REQ_F_BUFFER_SELECTED
)
5146 kfree(req
->sr_msg
.kbuf
);
5148 case IORING_OP_OPENAT
:
5149 case IORING_OP_OPENAT2
:
5150 case IORING_OP_STATX
:
5151 putname(req
->open
.filename
);
5153 case IORING_OP_SPLICE
:
5154 io_put_file(req
, req
->splice
.file_in
,
5155 (req
->splice
.flags
& SPLICE_F_FD_IN_FIXED
));
5159 req
->flags
&= ~REQ_F_NEED_CLEANUP
;
5162 static int io_issue_sqe(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
,
5163 bool force_nonblock
)
5165 struct io_ring_ctx
*ctx
= req
->ctx
;
5168 switch (req
->opcode
) {
5172 case IORING_OP_READV
:
5173 case IORING_OP_READ_FIXED
:
5174 case IORING_OP_READ
:
5176 ret
= io_read_prep(req
, sqe
, force_nonblock
);
5180 ret
= io_read(req
, force_nonblock
);
5182 case IORING_OP_WRITEV
:
5183 case IORING_OP_WRITE_FIXED
:
5184 case IORING_OP_WRITE
:
5186 ret
= io_write_prep(req
, sqe
, force_nonblock
);
5190 ret
= io_write(req
, force_nonblock
);
5192 case IORING_OP_FSYNC
:
5194 ret
= io_prep_fsync(req
, sqe
);
5198 ret
= io_fsync(req
, force_nonblock
);
5200 case IORING_OP_POLL_ADD
:
5202 ret
= io_poll_add_prep(req
, sqe
);
5206 ret
= io_poll_add(req
);
5208 case IORING_OP_POLL_REMOVE
:
5210 ret
= io_poll_remove_prep(req
, sqe
);
5214 ret
= io_poll_remove(req
);
5216 case IORING_OP_SYNC_FILE_RANGE
:
5218 ret
= io_prep_sfr(req
, sqe
);
5222 ret
= io_sync_file_range(req
, force_nonblock
);
5224 case IORING_OP_SENDMSG
:
5225 case IORING_OP_SEND
:
5227 ret
= io_sendmsg_prep(req
, sqe
);
5231 if (req
->opcode
== IORING_OP_SENDMSG
)
5232 ret
= io_sendmsg(req
, force_nonblock
);
5234 ret
= io_send(req
, force_nonblock
);
5236 case IORING_OP_RECVMSG
:
5237 case IORING_OP_RECV
:
5239 ret
= io_recvmsg_prep(req
, sqe
);
5243 if (req
->opcode
== IORING_OP_RECVMSG
)
5244 ret
= io_recvmsg(req
, force_nonblock
);
5246 ret
= io_recv(req
, force_nonblock
);
5248 case IORING_OP_TIMEOUT
:
5250 ret
= io_timeout_prep(req
, sqe
, false);
5254 ret
= io_timeout(req
);
5256 case IORING_OP_TIMEOUT_REMOVE
:
5258 ret
= io_timeout_remove_prep(req
, sqe
);
5262 ret
= io_timeout_remove(req
);
5264 case IORING_OP_ACCEPT
:
5266 ret
= io_accept_prep(req
, sqe
);
5270 ret
= io_accept(req
, force_nonblock
);
5272 case IORING_OP_CONNECT
:
5274 ret
= io_connect_prep(req
, sqe
);
5278 ret
= io_connect(req
, force_nonblock
);
5280 case IORING_OP_ASYNC_CANCEL
:
5282 ret
= io_async_cancel_prep(req
, sqe
);
5286 ret
= io_async_cancel(req
);
5288 case IORING_OP_FALLOCATE
:
5290 ret
= io_fallocate_prep(req
, sqe
);
5294 ret
= io_fallocate(req
, force_nonblock
);
5296 case IORING_OP_OPENAT
:
5298 ret
= io_openat_prep(req
, sqe
);
5302 ret
= io_openat(req
, force_nonblock
);
5304 case IORING_OP_CLOSE
:
5306 ret
= io_close_prep(req
, sqe
);
5310 ret
= io_close(req
, force_nonblock
);
5312 case IORING_OP_FILES_UPDATE
:
5314 ret
= io_files_update_prep(req
, sqe
);
5318 ret
= io_files_update(req
, force_nonblock
);
5320 case IORING_OP_STATX
:
5322 ret
= io_statx_prep(req
, sqe
);
5326 ret
= io_statx(req
, force_nonblock
);
5328 case IORING_OP_FADVISE
:
5330 ret
= io_fadvise_prep(req
, sqe
);
5334 ret
= io_fadvise(req
, force_nonblock
);
5336 case IORING_OP_MADVISE
:
5338 ret
= io_madvise_prep(req
, sqe
);
5342 ret
= io_madvise(req
, force_nonblock
);
5344 case IORING_OP_OPENAT2
:
5346 ret
= io_openat2_prep(req
, sqe
);
5350 ret
= io_openat2(req
, force_nonblock
);
5352 case IORING_OP_EPOLL_CTL
:
5354 ret
= io_epoll_ctl_prep(req
, sqe
);
5358 ret
= io_epoll_ctl(req
, force_nonblock
);
5360 case IORING_OP_SPLICE
:
5362 ret
= io_splice_prep(req
, sqe
);
5366 ret
= io_splice(req
, force_nonblock
);
5368 case IORING_OP_PROVIDE_BUFFERS
:
5370 ret
= io_provide_buffers_prep(req
, sqe
);
5374 ret
= io_provide_buffers(req
, force_nonblock
);
5376 case IORING_OP_REMOVE_BUFFERS
:
5378 ret
= io_remove_buffers_prep(req
, sqe
);
5382 ret
= io_remove_buffers(req
, force_nonblock
);
5392 /* If the op doesn't have a file, we're not polling for it */
5393 if ((ctx
->flags
& IORING_SETUP_IOPOLL
) && req
->file
) {
5394 const bool in_async
= io_wq_current_is_worker();
5396 if (req
->result
== -EAGAIN
)
5399 /* workqueue context doesn't hold uring_lock, grab it now */
5401 mutex_lock(&ctx
->uring_lock
);
5403 io_iopoll_req_issued(req
);
5406 mutex_unlock(&ctx
->uring_lock
);
5412 static void io_wq_submit_work(struct io_wq_work
**workptr
)
5414 struct io_wq_work
*work
= *workptr
;
5415 struct io_kiocb
*req
= container_of(work
, struct io_kiocb
, work
);
5418 /* if NO_CANCEL is set, we must still run the work */
5419 if ((work
->flags
& (IO_WQ_WORK_CANCEL
|IO_WQ_WORK_NO_CANCEL
)) ==
5420 IO_WQ_WORK_CANCEL
) {
5426 ret
= io_issue_sqe(req
, NULL
, false);
5428 * We can get EAGAIN for polled IO even though we're
5429 * forcing a sync submission from here, since we can't
5430 * wait for request slots on the block side.
5439 req_set_fail_links(req
);
5440 io_cqring_add_event(req
, ret
);
5444 io_steal_work(req
, workptr
);
5447 static inline struct file
*io_file_from_index(struct io_ring_ctx
*ctx
,
5450 struct fixed_file_table
*table
;
5452 table
= &ctx
->file_data
->table
[index
>> IORING_FILE_TABLE_SHIFT
];
5453 return table
->files
[index
& IORING_FILE_TABLE_MASK
];;
5456 static int io_file_get(struct io_submit_state
*state
, struct io_kiocb
*req
,
5457 int fd
, struct file
**out_file
, bool fixed
)
5459 struct io_ring_ctx
*ctx
= req
->ctx
;
5463 if (unlikely(!ctx
->file_data
||
5464 (unsigned) fd
>= ctx
->nr_user_files
))
5466 fd
= array_index_nospec(fd
, ctx
->nr_user_files
);
5467 file
= io_file_from_index(ctx
, fd
);
5470 req
->fixed_file_refs
= ctx
->file_data
->cur_refs
;
5471 percpu_ref_get(req
->fixed_file_refs
);
5473 trace_io_uring_file_get(ctx
, fd
);
5474 file
= __io_file_get(state
, fd
);
5475 if (unlikely(!file
))
5483 static int io_req_set_file(struct io_submit_state
*state
, struct io_kiocb
*req
,
5488 fixed
= (req
->flags
& REQ_F_FIXED_FILE
) != 0;
5489 if (unlikely(!fixed
&& req
->needs_fixed_file
))
5492 return io_file_get(state
, req
, fd
, &req
->file
, fixed
);
5495 static int io_grab_files(struct io_kiocb
*req
)
5498 struct io_ring_ctx
*ctx
= req
->ctx
;
5500 if (req
->work
.files
|| (req
->flags
& REQ_F_NO_FILE_TABLE
))
5502 if (!ctx
->ring_file
)
5506 spin_lock_irq(&ctx
->inflight_lock
);
5508 * We use the f_ops->flush() handler to ensure that we can flush
5509 * out work accessing these files if the fd is closed. Check if
5510 * the fd has changed since we started down this path, and disallow
5511 * this operation if it has.
5513 if (fcheck(ctx
->ring_fd
) == ctx
->ring_file
) {
5514 list_add(&req
->inflight_entry
, &ctx
->inflight_list
);
5515 req
->flags
|= REQ_F_INFLIGHT
;
5516 req
->work
.files
= current
->files
;
5519 spin_unlock_irq(&ctx
->inflight_lock
);
5525 static enum hrtimer_restart
io_link_timeout_fn(struct hrtimer
*timer
)
5527 struct io_timeout_data
*data
= container_of(timer
,
5528 struct io_timeout_data
, timer
);
5529 struct io_kiocb
*req
= data
->req
;
5530 struct io_ring_ctx
*ctx
= req
->ctx
;
5531 struct io_kiocb
*prev
= NULL
;
5532 unsigned long flags
;
5534 spin_lock_irqsave(&ctx
->completion_lock
, flags
);
5537 * We don't expect the list to be empty, that will only happen if we
5538 * race with the completion of the linked work.
5540 if (!list_empty(&req
->link_list
)) {
5541 prev
= list_entry(req
->link_list
.prev
, struct io_kiocb
,
5543 if (refcount_inc_not_zero(&prev
->refs
)) {
5544 list_del_init(&req
->link_list
);
5545 prev
->flags
&= ~REQ_F_LINK_TIMEOUT
;
5550 spin_unlock_irqrestore(&ctx
->completion_lock
, flags
);
5553 req_set_fail_links(prev
);
5554 io_async_find_and_cancel(ctx
, req
, prev
->user_data
, -ETIME
);
5557 io_cqring_add_event(req
, -ETIME
);
5560 return HRTIMER_NORESTART
;
5563 static void io_queue_linked_timeout(struct io_kiocb
*req
)
5565 struct io_ring_ctx
*ctx
= req
->ctx
;
5568 * If the list is now empty, then our linked request finished before
5569 * we got a chance to setup the timer
5571 spin_lock_irq(&ctx
->completion_lock
);
5572 if (!list_empty(&req
->link_list
)) {
5573 struct io_timeout_data
*data
= &req
->io
->timeout
;
5575 data
->timer
.function
= io_link_timeout_fn
;
5576 hrtimer_start(&data
->timer
, timespec64_to_ktime(data
->ts
),
5579 spin_unlock_irq(&ctx
->completion_lock
);
5581 /* drop submission reference */
5585 static struct io_kiocb
*io_prep_linked_timeout(struct io_kiocb
*req
)
5587 struct io_kiocb
*nxt
;
5589 if (!(req
->flags
& REQ_F_LINK_HEAD
))
5591 /* for polled retry, if flag is set, we already went through here */
5592 if (req
->flags
& REQ_F_POLLED
)
5595 nxt
= list_first_entry_or_null(&req
->link_list
, struct io_kiocb
,
5597 if (!nxt
|| nxt
->opcode
!= IORING_OP_LINK_TIMEOUT
)
5600 req
->flags
|= REQ_F_LINK_TIMEOUT
;
5604 static void __io_queue_sqe(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
5606 struct io_kiocb
*linked_timeout
;
5607 struct io_kiocb
*nxt
;
5608 const struct cred
*old_creds
= NULL
;
5612 linked_timeout
= io_prep_linked_timeout(req
);
5614 if (req
->work
.creds
&& req
->work
.creds
!= current_cred()) {
5616 revert_creds(old_creds
);
5617 if (old_creds
== req
->work
.creds
)
5618 old_creds
= NULL
; /* restored original creds */
5620 old_creds
= override_creds(req
->work
.creds
);
5623 ret
= io_issue_sqe(req
, sqe
, true);
5626 * We async punt it if the file wasn't marked NOWAIT, or if the file
5627 * doesn't support non-blocking read/write attempts
5629 if (ret
== -EAGAIN
&& (!(req
->flags
& REQ_F_NOWAIT
) ||
5630 (req
->flags
& REQ_F_MUST_PUNT
))) {
5631 if (io_arm_poll_handler(req
)) {
5633 io_queue_linked_timeout(linked_timeout
);
5637 if (io_op_defs
[req
->opcode
].file_table
) {
5638 ret
= io_grab_files(req
);
5644 * Queued up for async execution, worker will release
5645 * submit reference when the iocb is actually submitted.
5647 io_queue_async_work(req
);
5653 /* drop submission reference */
5654 io_put_req_find_next(req
, &nxt
);
5656 if (linked_timeout
) {
5658 io_queue_linked_timeout(linked_timeout
);
5660 io_put_req(linked_timeout
);
5663 /* and drop final reference, if we failed */
5665 io_cqring_add_event(req
, ret
);
5666 req_set_fail_links(req
);
5672 if (req
->flags
& REQ_F_FORCE_ASYNC
)
5678 revert_creds(old_creds
);
5681 static void io_queue_sqe(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
5685 ret
= io_req_defer(req
, sqe
);
5687 if (ret
!= -EIOCBQUEUED
) {
5689 io_cqring_add_event(req
, ret
);
5690 req_set_fail_links(req
);
5691 io_double_put_req(req
);
5693 } else if (req
->flags
& REQ_F_FORCE_ASYNC
) {
5696 if (io_alloc_async_ctx(req
))
5698 ret
= io_req_defer_prep(req
, sqe
);
5699 if (unlikely(ret
< 0))
5704 * Never try inline submit of IOSQE_ASYNC is set, go straight
5705 * to async execution.
5707 req
->work
.flags
|= IO_WQ_WORK_CONCURRENT
;
5708 io_queue_async_work(req
);
5710 __io_queue_sqe(req
, sqe
);
5714 static inline void io_queue_link_head(struct io_kiocb
*req
)
5716 if (unlikely(req
->flags
& REQ_F_FAIL_LINK
)) {
5717 io_cqring_add_event(req
, -ECANCELED
);
5718 io_double_put_req(req
);
5720 io_queue_sqe(req
, NULL
);
5723 static int io_submit_sqe(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
,
5724 struct io_submit_state
*state
, struct io_kiocb
**link
)
5726 struct io_ring_ctx
*ctx
= req
->ctx
;
5730 * If we already have a head request, queue this one for async
5731 * submittal once the head completes. If we don't have a head but
5732 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
5733 * submitted sync once the chain is complete. If none of those
5734 * conditions are true (normal request), then just queue it.
5737 struct io_kiocb
*head
= *link
;
5740 * Taking sequential execution of a link, draining both sides
5741 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
5742 * requests in the link. So, it drains the head and the
5743 * next after the link request. The last one is done via
5744 * drain_next flag to persist the effect across calls.
5746 if (req
->flags
& REQ_F_IO_DRAIN
) {
5747 head
->flags
|= REQ_F_IO_DRAIN
;
5748 ctx
->drain_next
= 1;
5750 if (io_alloc_async_ctx(req
))
5753 ret
= io_req_defer_prep(req
, sqe
);
5755 /* fail even hard links since we don't submit */
5756 head
->flags
|= REQ_F_FAIL_LINK
;
5759 trace_io_uring_link(ctx
, req
, head
);
5760 list_add_tail(&req
->link_list
, &head
->link_list
);
5762 /* last request of a link, enqueue the link */
5763 if (!(req
->flags
& (REQ_F_LINK
| REQ_F_HARDLINK
))) {
5764 io_queue_link_head(head
);
5768 if (unlikely(ctx
->drain_next
)) {
5769 req
->flags
|= REQ_F_IO_DRAIN
;
5770 ctx
->drain_next
= 0;
5772 if (req
->flags
& (REQ_F_LINK
| REQ_F_HARDLINK
)) {
5773 req
->flags
|= REQ_F_LINK_HEAD
;
5774 INIT_LIST_HEAD(&req
->link_list
);
5776 if (io_alloc_async_ctx(req
))
5779 ret
= io_req_defer_prep(req
, sqe
);
5781 req
->flags
|= REQ_F_FAIL_LINK
;
5784 io_queue_sqe(req
, sqe
);
5792 * Batched submission is done, ensure local IO is flushed out.
5794 static void io_submit_state_end(struct io_submit_state
*state
)
5796 blk_finish_plug(&state
->plug
);
5798 if (state
->free_reqs
)
5799 kmem_cache_free_bulk(req_cachep
, state
->free_reqs
, state
->reqs
);
5803 * Start submission side cache.
5805 static void io_submit_state_start(struct io_submit_state
*state
,
5806 unsigned int max_ios
)
5808 blk_start_plug(&state
->plug
);
5809 state
->free_reqs
= 0;
5811 state
->ios_left
= max_ios
;
5814 static void io_commit_sqring(struct io_ring_ctx
*ctx
)
5816 struct io_rings
*rings
= ctx
->rings
;
5819 * Ensure any loads from the SQEs are done at this point,
5820 * since once we write the new head, the application could
5821 * write new data to them.
5823 smp_store_release(&rings
->sq
.head
, ctx
->cached_sq_head
);
5827 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
5828 * that is mapped by userspace. This means that care needs to be taken to
5829 * ensure that reads are stable, as we cannot rely on userspace always
5830 * being a good citizen. If members of the sqe are validated and then later
5831 * used, it's important that those reads are done through READ_ONCE() to
5832 * prevent a re-load down the line.
5834 static const struct io_uring_sqe
*io_get_sqe(struct io_ring_ctx
*ctx
)
5836 u32
*sq_array
= ctx
->sq_array
;
5840 * The cached sq head (or cq tail) serves two purposes:
5842 * 1) allows us to batch the cost of updating the user visible
5844 * 2) allows the kernel side to track the head on its own, even
5845 * though the application is the one updating it.
5847 head
= READ_ONCE(sq_array
[ctx
->cached_sq_head
& ctx
->sq_mask
]);
5848 if (likely(head
< ctx
->sq_entries
))
5849 return &ctx
->sq_sqes
[head
];
5851 /* drop invalid entries */
5852 ctx
->cached_sq_dropped
++;
5853 WRITE_ONCE(ctx
->rings
->sq_dropped
, ctx
->cached_sq_dropped
);
5857 static inline void io_consume_sqe(struct io_ring_ctx
*ctx
)
5859 ctx
->cached_sq_head
++;
5862 #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
5863 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
5864 IOSQE_BUFFER_SELECT)
5866 static int io_init_req(struct io_ring_ctx
*ctx
, struct io_kiocb
*req
,
5867 const struct io_uring_sqe
*sqe
,
5868 struct io_submit_state
*state
, bool async
)
5870 unsigned int sqe_flags
;
5874 * All io need record the previous position, if LINK vs DARIN,
5875 * it can be used to mark the position of the first IO in the
5878 req
->sequence
= ctx
->cached_sq_head
- ctx
->cached_sq_dropped
;
5879 req
->opcode
= READ_ONCE(sqe
->opcode
);
5880 req
->user_data
= READ_ONCE(sqe
->user_data
);
5885 /* one is dropped after submission, the other at completion */
5886 refcount_set(&req
->refs
, 2);
5889 req
->needs_fixed_file
= async
;
5890 INIT_IO_WORK(&req
->work
, io_wq_submit_work
);
5892 if (unlikely(req
->opcode
>= IORING_OP_LAST
))
5895 if (io_op_defs
[req
->opcode
].needs_mm
&& !current
->mm
) {
5896 if (unlikely(!mmget_not_zero(ctx
->sqo_mm
)))
5898 use_mm(ctx
->sqo_mm
);
5901 sqe_flags
= READ_ONCE(sqe
->flags
);
5902 /* enforce forwards compatibility on users */
5903 if (unlikely(sqe_flags
& ~SQE_VALID_FLAGS
))
5906 if ((sqe_flags
& IOSQE_BUFFER_SELECT
) &&
5907 !io_op_defs
[req
->opcode
].buffer_select
)
5910 id
= READ_ONCE(sqe
->personality
);
5912 req
->work
.creds
= idr_find(&ctx
->personality_idr
, id
);
5913 if (unlikely(!req
->work
.creds
))
5915 get_cred(req
->work
.creds
);
5918 /* same numerical values with corresponding REQ_F_*, safe to copy */
5919 req
->flags
|= sqe_flags
& (IOSQE_IO_DRAIN
| IOSQE_IO_HARDLINK
|
5920 IOSQE_ASYNC
| IOSQE_FIXED_FILE
|
5921 IOSQE_BUFFER_SELECT
| IOSQE_IO_LINK
);
5923 if (!io_op_defs
[req
->opcode
].needs_file
)
5926 return io_req_set_file(state
, req
, READ_ONCE(sqe
->fd
));
5929 static int io_submit_sqes(struct io_ring_ctx
*ctx
, unsigned int nr
,
5930 struct file
*ring_file
, int ring_fd
, bool async
)
5932 struct io_submit_state state
, *statep
= NULL
;
5933 struct io_kiocb
*link
= NULL
;
5934 int i
, submitted
= 0;
5936 /* if we have a backlog and couldn't flush it all, return BUSY */
5937 if (test_bit(0, &ctx
->sq_check_overflow
)) {
5938 if (!list_empty(&ctx
->cq_overflow_list
) &&
5939 !io_cqring_overflow_flush(ctx
, false))
5943 /* make sure SQ entry isn't read before tail */
5944 nr
= min3(nr
, ctx
->sq_entries
, io_sqring_entries(ctx
));
5946 if (!percpu_ref_tryget_many(&ctx
->refs
, nr
))
5949 if (nr
> IO_PLUG_THRESHOLD
) {
5950 io_submit_state_start(&state
, nr
);
5954 ctx
->ring_fd
= ring_fd
;
5955 ctx
->ring_file
= ring_file
;
5957 for (i
= 0; i
< nr
; i
++) {
5958 const struct io_uring_sqe
*sqe
;
5959 struct io_kiocb
*req
;
5962 sqe
= io_get_sqe(ctx
);
5963 if (unlikely(!sqe
)) {
5964 io_consume_sqe(ctx
);
5967 req
= io_alloc_req(ctx
, statep
);
5968 if (unlikely(!req
)) {
5970 submitted
= -EAGAIN
;
5974 err
= io_init_req(ctx
, req
, sqe
, statep
, async
);
5975 io_consume_sqe(ctx
);
5976 /* will complete beyond this point, count as submitted */
5979 if (unlikely(err
)) {
5981 io_cqring_add_event(req
, err
);
5982 io_double_put_req(req
);
5986 trace_io_uring_submit_sqe(ctx
, req
->opcode
, req
->user_data
,
5988 err
= io_submit_sqe(req
, sqe
, statep
, &link
);
5993 if (unlikely(submitted
!= nr
)) {
5994 int ref_used
= (submitted
== -EAGAIN
) ? 0 : submitted
;
5996 percpu_ref_put_many(&ctx
->refs
, nr
- ref_used
);
5999 io_queue_link_head(link
);
6001 io_submit_state_end(&state
);
6003 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6004 io_commit_sqring(ctx
);
6009 static inline void io_sq_thread_drop_mm(struct io_ring_ctx
*ctx
)
6011 struct mm_struct
*mm
= current
->mm
;
6019 static int io_sq_thread(void *data
)
6021 struct io_ring_ctx
*ctx
= data
;
6022 const struct cred
*old_cred
;
6023 mm_segment_t old_fs
;
6025 unsigned long timeout
;
6028 complete(&ctx
->completions
[1]);
6032 old_cred
= override_creds(ctx
->creds
);
6034 timeout
= jiffies
+ ctx
->sq_thread_idle
;
6035 while (!kthread_should_park()) {
6036 unsigned int to_submit
;
6038 if (!list_empty(&ctx
->poll_list
)) {
6039 unsigned nr_events
= 0;
6041 mutex_lock(&ctx
->uring_lock
);
6042 if (!list_empty(&ctx
->poll_list
))
6043 io_iopoll_getevents(ctx
, &nr_events
, 0);
6045 timeout
= jiffies
+ ctx
->sq_thread_idle
;
6046 mutex_unlock(&ctx
->uring_lock
);
6049 to_submit
= io_sqring_entries(ctx
);
6052 * If submit got -EBUSY, flag us as needing the application
6053 * to enter the kernel to reap and flush events.
6055 if (!to_submit
|| ret
== -EBUSY
) {
6057 * Drop cur_mm before scheduling, we can't hold it for
6058 * long periods (or over schedule()). Do this before
6059 * adding ourselves to the waitqueue, as the unuse/drop
6062 io_sq_thread_drop_mm(ctx
);
6065 * We're polling. If we're within the defined idle
6066 * period, then let us spin without work before going
6067 * to sleep. The exception is if we got EBUSY doing
6068 * more IO, we should wait for the application to
6069 * reap events and wake us up.
6071 if (!list_empty(&ctx
->poll_list
) ||
6072 (!time_after(jiffies
, timeout
) && ret
!= -EBUSY
&&
6073 !percpu_ref_is_dying(&ctx
->refs
))) {
6074 if (current
->task_works
)
6080 prepare_to_wait(&ctx
->sqo_wait
, &wait
,
6081 TASK_INTERRUPTIBLE
);
6084 * While doing polled IO, before going to sleep, we need
6085 * to check if there are new reqs added to poll_list, it
6086 * is because reqs may have been punted to io worker and
6087 * will be added to poll_list later, hence check the
6090 if ((ctx
->flags
& IORING_SETUP_IOPOLL
) &&
6091 !list_empty_careful(&ctx
->poll_list
)) {
6092 finish_wait(&ctx
->sqo_wait
, &wait
);
6096 /* Tell userspace we may need a wakeup call */
6097 ctx
->rings
->sq_flags
|= IORING_SQ_NEED_WAKEUP
;
6098 /* make sure to read SQ tail after writing flags */
6101 to_submit
= io_sqring_entries(ctx
);
6102 if (!to_submit
|| ret
== -EBUSY
) {
6103 if (kthread_should_park()) {
6104 finish_wait(&ctx
->sqo_wait
, &wait
);
6107 if (current
->task_works
) {
6109 finish_wait(&ctx
->sqo_wait
, &wait
);
6112 if (signal_pending(current
))
6113 flush_signals(current
);
6115 finish_wait(&ctx
->sqo_wait
, &wait
);
6117 ctx
->rings
->sq_flags
&= ~IORING_SQ_NEED_WAKEUP
;
6121 finish_wait(&ctx
->sqo_wait
, &wait
);
6123 ctx
->rings
->sq_flags
&= ~IORING_SQ_NEED_WAKEUP
;
6126 mutex_lock(&ctx
->uring_lock
);
6127 ret
= io_submit_sqes(ctx
, to_submit
, NULL
, -1, true);
6128 mutex_unlock(&ctx
->uring_lock
);
6129 timeout
= jiffies
+ ctx
->sq_thread_idle
;
6132 if (current
->task_works
)
6136 io_sq_thread_drop_mm(ctx
);
6137 revert_creds(old_cred
);
6144 struct io_wait_queue
{
6145 struct wait_queue_entry wq
;
6146 struct io_ring_ctx
*ctx
;
6148 unsigned nr_timeouts
;
6151 static inline bool io_should_wake(struct io_wait_queue
*iowq
, bool noflush
)
6153 struct io_ring_ctx
*ctx
= iowq
->ctx
;
6156 * Wake up if we have enough events, or if a timeout occurred since we
6157 * started waiting. For timeouts, we always want to return to userspace,
6158 * regardless of event count.
6160 return io_cqring_events(ctx
, noflush
) >= iowq
->to_wait
||
6161 atomic_read(&ctx
->cq_timeouts
) != iowq
->nr_timeouts
;
6164 static int io_wake_function(struct wait_queue_entry
*curr
, unsigned int mode
,
6165 int wake_flags
, void *key
)
6167 struct io_wait_queue
*iowq
= container_of(curr
, struct io_wait_queue
,
6170 /* use noflush == true, as we can't safely rely on locking context */
6171 if (!io_should_wake(iowq
, true))
6174 return autoremove_wake_function(curr
, mode
, wake_flags
, key
);
6178 * Wait until events become available, if we don't already have some. The
6179 * application must reap them itself, as they reside on the shared cq ring.
6181 static int io_cqring_wait(struct io_ring_ctx
*ctx
, int min_events
,
6182 const sigset_t __user
*sig
, size_t sigsz
)
6184 struct io_wait_queue iowq
= {
6187 .func
= io_wake_function
,
6188 .entry
= LIST_HEAD_INIT(iowq
.wq
.entry
),
6191 .to_wait
= min_events
,
6193 struct io_rings
*rings
= ctx
->rings
;
6197 if (io_cqring_events(ctx
, false) >= min_events
)
6199 if (!current
->task_works
)
6205 #ifdef CONFIG_COMPAT
6206 if (in_compat_syscall())
6207 ret
= set_compat_user_sigmask((const compat_sigset_t __user
*)sig
,
6211 ret
= set_user_sigmask(sig
, sigsz
);
6217 iowq
.nr_timeouts
= atomic_read(&ctx
->cq_timeouts
);
6218 trace_io_uring_cqring_wait(ctx
, min_events
);
6220 prepare_to_wait_exclusive(&ctx
->wait
, &iowq
.wq
,
6221 TASK_INTERRUPTIBLE
);
6222 if (current
->task_works
)
6224 if (io_should_wake(&iowq
, false))
6227 if (signal_pending(current
)) {
6232 finish_wait(&ctx
->wait
, &iowq
.wq
);
6234 restore_saved_sigmask_unless(ret
== -EINTR
);
6236 return READ_ONCE(rings
->cq
.head
) == READ_ONCE(rings
->cq
.tail
) ? ret
: 0;
6239 static void __io_sqe_files_unregister(struct io_ring_ctx
*ctx
)
6241 #if defined(CONFIG_UNIX)
6242 if (ctx
->ring_sock
) {
6243 struct sock
*sock
= ctx
->ring_sock
->sk
;
6244 struct sk_buff
*skb
;
6246 while ((skb
= skb_dequeue(&sock
->sk_receive_queue
)) != NULL
)
6252 for (i
= 0; i
< ctx
->nr_user_files
; i
++) {
6255 file
= io_file_from_index(ctx
, i
);
6262 static void io_file_ref_kill(struct percpu_ref
*ref
)
6264 struct fixed_file_data
*data
;
6266 data
= container_of(ref
, struct fixed_file_data
, refs
);
6267 complete(&data
->done
);
6270 static int io_sqe_files_unregister(struct io_ring_ctx
*ctx
)
6272 struct fixed_file_data
*data
= ctx
->file_data
;
6273 struct fixed_file_ref_node
*ref_node
= NULL
;
6274 unsigned nr_tables
, i
;
6275 unsigned long flags
;
6280 spin_lock_irqsave(&data
->lock
, flags
);
6281 if (!list_empty(&data
->ref_list
))
6282 ref_node
= list_first_entry(&data
->ref_list
,
6283 struct fixed_file_ref_node
, node
);
6284 spin_unlock_irqrestore(&data
->lock
, flags
);
6286 percpu_ref_kill(&ref_node
->refs
);
6288 percpu_ref_kill(&data
->refs
);
6290 /* wait for all refs nodes to complete */
6291 wait_for_completion(&data
->done
);
6293 __io_sqe_files_unregister(ctx
);
6294 nr_tables
= DIV_ROUND_UP(ctx
->nr_user_files
, IORING_MAX_FILES_TABLE
);
6295 for (i
= 0; i
< nr_tables
; i
++)
6296 kfree(data
->table
[i
].files
);
6298 percpu_ref_exit(&data
->refs
);
6300 ctx
->file_data
= NULL
;
6301 ctx
->nr_user_files
= 0;
6305 static void io_sq_thread_stop(struct io_ring_ctx
*ctx
)
6307 if (ctx
->sqo_thread
) {
6308 wait_for_completion(&ctx
->completions
[1]);
6310 * The park is a bit of a work-around, without it we get
6311 * warning spews on shutdown with SQPOLL set and affinity
6312 * set to a single CPU.
6314 kthread_park(ctx
->sqo_thread
);
6315 kthread_stop(ctx
->sqo_thread
);
6316 ctx
->sqo_thread
= NULL
;
6320 static void io_finish_async(struct io_ring_ctx
*ctx
)
6322 io_sq_thread_stop(ctx
);
6325 io_wq_destroy(ctx
->io_wq
);
6330 #if defined(CONFIG_UNIX)
6332 * Ensure the UNIX gc is aware of our file set, so we are certain that
6333 * the io_uring can be safely unregistered on process exit, even if we have
6334 * loops in the file referencing.
6336 static int __io_sqe_files_scm(struct io_ring_ctx
*ctx
, int nr
, int offset
)
6338 struct sock
*sk
= ctx
->ring_sock
->sk
;
6339 struct scm_fp_list
*fpl
;
6340 struct sk_buff
*skb
;
6343 fpl
= kzalloc(sizeof(*fpl
), GFP_KERNEL
);
6347 skb
= alloc_skb(0, GFP_KERNEL
);
6356 fpl
->user
= get_uid(ctx
->user
);
6357 for (i
= 0; i
< nr
; i
++) {
6358 struct file
*file
= io_file_from_index(ctx
, i
+ offset
);
6362 fpl
->fp
[nr_files
] = get_file(file
);
6363 unix_inflight(fpl
->user
, fpl
->fp
[nr_files
]);
6368 fpl
->max
= SCM_MAX_FD
;
6369 fpl
->count
= nr_files
;
6370 UNIXCB(skb
).fp
= fpl
;
6371 skb
->destructor
= unix_destruct_scm
;
6372 refcount_add(skb
->truesize
, &sk
->sk_wmem_alloc
);
6373 skb_queue_head(&sk
->sk_receive_queue
, skb
);
6375 for (i
= 0; i
< nr_files
; i
++)
6386 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
6387 * causes regular reference counting to break down. We rely on the UNIX
6388 * garbage collection to take care of this problem for us.
6390 static int io_sqe_files_scm(struct io_ring_ctx
*ctx
)
6392 unsigned left
, total
;
6396 left
= ctx
->nr_user_files
;
6398 unsigned this_files
= min_t(unsigned, left
, SCM_MAX_FD
);
6400 ret
= __io_sqe_files_scm(ctx
, this_files
, total
);
6404 total
+= this_files
;
6410 while (total
< ctx
->nr_user_files
) {
6411 struct file
*file
= io_file_from_index(ctx
, total
);
6421 static int io_sqe_files_scm(struct io_ring_ctx
*ctx
)
6427 static int io_sqe_alloc_file_tables(struct io_ring_ctx
*ctx
, unsigned nr_tables
,
6432 for (i
= 0; i
< nr_tables
; i
++) {
6433 struct fixed_file_table
*table
= &ctx
->file_data
->table
[i
];
6434 unsigned this_files
;
6436 this_files
= min(nr_files
, IORING_MAX_FILES_TABLE
);
6437 table
->files
= kcalloc(this_files
, sizeof(struct file
*),
6441 nr_files
-= this_files
;
6447 for (i
= 0; i
< nr_tables
; i
++) {
6448 struct fixed_file_table
*table
= &ctx
->file_data
->table
[i
];
6449 kfree(table
->files
);
6454 static void io_ring_file_put(struct io_ring_ctx
*ctx
, struct file
*file
)
6456 #if defined(CONFIG_UNIX)
6457 struct sock
*sock
= ctx
->ring_sock
->sk
;
6458 struct sk_buff_head list
, *head
= &sock
->sk_receive_queue
;
6459 struct sk_buff
*skb
;
6462 __skb_queue_head_init(&list
);
6465 * Find the skb that holds this file in its SCM_RIGHTS. When found,
6466 * remove this entry and rearrange the file array.
6468 skb
= skb_dequeue(head
);
6470 struct scm_fp_list
*fp
;
6472 fp
= UNIXCB(skb
).fp
;
6473 for (i
= 0; i
< fp
->count
; i
++) {
6476 if (fp
->fp
[i
] != file
)
6479 unix_notinflight(fp
->user
, fp
->fp
[i
]);
6480 left
= fp
->count
- 1 - i
;
6482 memmove(&fp
->fp
[i
], &fp
->fp
[i
+ 1],
6483 left
* sizeof(struct file
*));
6490 __skb_queue_tail(&list
, skb
);
6500 __skb_queue_tail(&list
, skb
);
6502 skb
= skb_dequeue(head
);
6505 if (skb_peek(&list
)) {
6506 spin_lock_irq(&head
->lock
);
6507 while ((skb
= __skb_dequeue(&list
)) != NULL
)
6508 __skb_queue_tail(head
, skb
);
6509 spin_unlock_irq(&head
->lock
);
6516 struct io_file_put
{
6517 struct list_head list
;
6521 static void io_file_put_work(struct work_struct
*work
)
6523 struct fixed_file_ref_node
*ref_node
;
6524 struct fixed_file_data
*file_data
;
6525 struct io_ring_ctx
*ctx
;
6526 struct io_file_put
*pfile
, *tmp
;
6527 unsigned long flags
;
6529 ref_node
= container_of(work
, struct fixed_file_ref_node
, work
);
6530 file_data
= ref_node
->file_data
;
6531 ctx
= file_data
->ctx
;
6533 list_for_each_entry_safe(pfile
, tmp
, &ref_node
->file_list
, list
) {
6534 list_del_init(&pfile
->list
);
6535 io_ring_file_put(ctx
, pfile
->file
);
6539 spin_lock_irqsave(&file_data
->lock
, flags
);
6540 list_del_init(&ref_node
->node
);
6541 spin_unlock_irqrestore(&file_data
->lock
, flags
);
6543 percpu_ref_exit(&ref_node
->refs
);
6545 percpu_ref_put(&file_data
->refs
);
6548 static void io_file_data_ref_zero(struct percpu_ref
*ref
)
6550 struct fixed_file_ref_node
*ref_node
;
6552 ref_node
= container_of(ref
, struct fixed_file_ref_node
, refs
);
6554 queue_work(system_wq
, &ref_node
->work
);
6557 static struct fixed_file_ref_node
*alloc_fixed_file_ref_node(
6558 struct io_ring_ctx
*ctx
)
6560 struct fixed_file_ref_node
*ref_node
;
6562 ref_node
= kzalloc(sizeof(*ref_node
), GFP_KERNEL
);
6564 return ERR_PTR(-ENOMEM
);
6566 if (percpu_ref_init(&ref_node
->refs
, io_file_data_ref_zero
,
6569 return ERR_PTR(-ENOMEM
);
6571 INIT_LIST_HEAD(&ref_node
->node
);
6572 INIT_LIST_HEAD(&ref_node
->file_list
);
6573 INIT_WORK(&ref_node
->work
, io_file_put_work
);
6574 ref_node
->file_data
= ctx
->file_data
;
6579 static void destroy_fixed_file_ref_node(struct fixed_file_ref_node
*ref_node
)
6581 percpu_ref_exit(&ref_node
->refs
);
6585 static int io_sqe_files_register(struct io_ring_ctx
*ctx
, void __user
*arg
,
6588 __s32 __user
*fds
= (__s32 __user
*) arg
;
6593 struct fixed_file_ref_node
*ref_node
;
6594 unsigned long flags
;
6600 if (nr_args
> IORING_MAX_FIXED_FILES
)
6603 ctx
->file_data
= kzalloc(sizeof(*ctx
->file_data
), GFP_KERNEL
);
6604 if (!ctx
->file_data
)
6606 ctx
->file_data
->ctx
= ctx
;
6607 init_completion(&ctx
->file_data
->done
);
6608 INIT_LIST_HEAD(&ctx
->file_data
->ref_list
);
6609 spin_lock_init(&ctx
->file_data
->lock
);
6611 nr_tables
= DIV_ROUND_UP(nr_args
, IORING_MAX_FILES_TABLE
);
6612 ctx
->file_data
->table
= kcalloc(nr_tables
,
6613 sizeof(struct fixed_file_table
),
6615 if (!ctx
->file_data
->table
) {
6616 kfree(ctx
->file_data
);
6617 ctx
->file_data
= NULL
;
6621 if (percpu_ref_init(&ctx
->file_data
->refs
, io_file_ref_kill
,
6622 PERCPU_REF_ALLOW_REINIT
, GFP_KERNEL
)) {
6623 kfree(ctx
->file_data
->table
);
6624 kfree(ctx
->file_data
);
6625 ctx
->file_data
= NULL
;
6629 if (io_sqe_alloc_file_tables(ctx
, nr_tables
, nr_args
)) {
6630 percpu_ref_exit(&ctx
->file_data
->refs
);
6631 kfree(ctx
->file_data
->table
);
6632 kfree(ctx
->file_data
);
6633 ctx
->file_data
= NULL
;
6637 for (i
= 0; i
< nr_args
; i
++, ctx
->nr_user_files
++) {
6638 struct fixed_file_table
*table
;
6642 if (copy_from_user(&fd
, &fds
[i
], sizeof(fd
)))
6644 /* allow sparse sets */
6650 table
= &ctx
->file_data
->table
[i
>> IORING_FILE_TABLE_SHIFT
];
6651 index
= i
& IORING_FILE_TABLE_MASK
;
6659 * Don't allow io_uring instances to be registered. If UNIX
6660 * isn't enabled, then this causes a reference cycle and this
6661 * instance can never get freed. If UNIX is enabled we'll
6662 * handle it just fine, but there's still no point in allowing
6663 * a ring fd as it doesn't support regular read/write anyway.
6665 if (file
->f_op
== &io_uring_fops
) {
6670 table
->files
[index
] = file
;
6674 for (i
= 0; i
< ctx
->nr_user_files
; i
++) {
6675 file
= io_file_from_index(ctx
, i
);
6679 for (i
= 0; i
< nr_tables
; i
++)
6680 kfree(ctx
->file_data
->table
[i
].files
);
6682 kfree(ctx
->file_data
->table
);
6683 kfree(ctx
->file_data
);
6684 ctx
->file_data
= NULL
;
6685 ctx
->nr_user_files
= 0;
6689 ret
= io_sqe_files_scm(ctx
);
6691 io_sqe_files_unregister(ctx
);
6695 ref_node
= alloc_fixed_file_ref_node(ctx
);
6696 if (IS_ERR(ref_node
)) {
6697 io_sqe_files_unregister(ctx
);
6698 return PTR_ERR(ref_node
);
6701 ctx
->file_data
->cur_refs
= &ref_node
->refs
;
6702 spin_lock_irqsave(&ctx
->file_data
->lock
, flags
);
6703 list_add(&ref_node
->node
, &ctx
->file_data
->ref_list
);
6704 spin_unlock_irqrestore(&ctx
->file_data
->lock
, flags
);
6705 percpu_ref_get(&ctx
->file_data
->refs
);
6709 static int io_sqe_file_register(struct io_ring_ctx
*ctx
, struct file
*file
,
6712 #if defined(CONFIG_UNIX)
6713 struct sock
*sock
= ctx
->ring_sock
->sk
;
6714 struct sk_buff_head
*head
= &sock
->sk_receive_queue
;
6715 struct sk_buff
*skb
;
6718 * See if we can merge this file into an existing skb SCM_RIGHTS
6719 * file set. If there's no room, fall back to allocating a new skb
6720 * and filling it in.
6722 spin_lock_irq(&head
->lock
);
6723 skb
= skb_peek(head
);
6725 struct scm_fp_list
*fpl
= UNIXCB(skb
).fp
;
6727 if (fpl
->count
< SCM_MAX_FD
) {
6728 __skb_unlink(skb
, head
);
6729 spin_unlock_irq(&head
->lock
);
6730 fpl
->fp
[fpl
->count
] = get_file(file
);
6731 unix_inflight(fpl
->user
, fpl
->fp
[fpl
->count
]);
6733 spin_lock_irq(&head
->lock
);
6734 __skb_queue_head(head
, skb
);
6739 spin_unlock_irq(&head
->lock
);
6746 return __io_sqe_files_scm(ctx
, 1, index
);
6752 static int io_queue_file_removal(struct fixed_file_data
*data
,
6755 struct io_file_put
*pfile
;
6756 struct percpu_ref
*refs
= data
->cur_refs
;
6757 struct fixed_file_ref_node
*ref_node
;
6759 pfile
= kzalloc(sizeof(*pfile
), GFP_KERNEL
);
6763 ref_node
= container_of(refs
, struct fixed_file_ref_node
, refs
);
6765 list_add(&pfile
->list
, &ref_node
->file_list
);
6770 static int __io_sqe_files_update(struct io_ring_ctx
*ctx
,
6771 struct io_uring_files_update
*up
,
6774 struct fixed_file_data
*data
= ctx
->file_data
;
6775 struct fixed_file_ref_node
*ref_node
;
6780 unsigned long flags
;
6781 bool needs_switch
= false;
6783 if (check_add_overflow(up
->offset
, nr_args
, &done
))
6785 if (done
> ctx
->nr_user_files
)
6788 ref_node
= alloc_fixed_file_ref_node(ctx
);
6789 if (IS_ERR(ref_node
))
6790 return PTR_ERR(ref_node
);
6793 fds
= u64_to_user_ptr(up
->fds
);
6795 struct fixed_file_table
*table
;
6799 if (copy_from_user(&fd
, &fds
[done
], sizeof(fd
))) {
6803 i
= array_index_nospec(up
->offset
, ctx
->nr_user_files
);
6804 table
= &ctx
->file_data
->table
[i
>> IORING_FILE_TABLE_SHIFT
];
6805 index
= i
& IORING_FILE_TABLE_MASK
;
6806 if (table
->files
[index
]) {
6807 file
= io_file_from_index(ctx
, index
);
6808 err
= io_queue_file_removal(data
, file
);
6811 table
->files
[index
] = NULL
;
6812 needs_switch
= true;
6821 * Don't allow io_uring instances to be registered. If
6822 * UNIX isn't enabled, then this causes a reference
6823 * cycle and this instance can never get freed. If UNIX
6824 * is enabled we'll handle it just fine, but there's
6825 * still no point in allowing a ring fd as it doesn't
6826 * support regular read/write anyway.
6828 if (file
->f_op
== &io_uring_fops
) {
6833 table
->files
[index
] = file
;
6834 err
= io_sqe_file_register(ctx
, file
, i
);
6844 percpu_ref_kill(data
->cur_refs
);
6845 spin_lock_irqsave(&data
->lock
, flags
);
6846 list_add(&ref_node
->node
, &data
->ref_list
);
6847 data
->cur_refs
= &ref_node
->refs
;
6848 spin_unlock_irqrestore(&data
->lock
, flags
);
6849 percpu_ref_get(&ctx
->file_data
->refs
);
6851 destroy_fixed_file_ref_node(ref_node
);
6853 return done
? done
: err
;
6856 static int io_sqe_files_update(struct io_ring_ctx
*ctx
, void __user
*arg
,
6859 struct io_uring_files_update up
;
6861 if (!ctx
->file_data
)
6865 if (copy_from_user(&up
, arg
, sizeof(up
)))
6870 return __io_sqe_files_update(ctx
, &up
, nr_args
);
6873 static void io_free_work(struct io_wq_work
*work
)
6875 struct io_kiocb
*req
= container_of(work
, struct io_kiocb
, work
);
6877 /* Consider that io_steal_work() relies on this ref */
6881 static int io_init_wq_offload(struct io_ring_ctx
*ctx
,
6882 struct io_uring_params
*p
)
6884 struct io_wq_data data
;
6886 struct io_ring_ctx
*ctx_attach
;
6887 unsigned int concurrency
;
6890 data
.user
= ctx
->user
;
6891 data
.free_work
= io_free_work
;
6893 if (!(p
->flags
& IORING_SETUP_ATTACH_WQ
)) {
6894 /* Do QD, or 4 * CPUS, whatever is smallest */
6895 concurrency
= min(ctx
->sq_entries
, 4 * num_online_cpus());
6897 ctx
->io_wq
= io_wq_create(concurrency
, &data
);
6898 if (IS_ERR(ctx
->io_wq
)) {
6899 ret
= PTR_ERR(ctx
->io_wq
);
6905 f
= fdget(p
->wq_fd
);
6909 if (f
.file
->f_op
!= &io_uring_fops
) {
6914 ctx_attach
= f
.file
->private_data
;
6915 /* @io_wq is protected by holding the fd */
6916 if (!io_wq_get(ctx_attach
->io_wq
, &data
)) {
6921 ctx
->io_wq
= ctx_attach
->io_wq
;
6927 static int io_sq_offload_start(struct io_ring_ctx
*ctx
,
6928 struct io_uring_params
*p
)
6932 mmgrab(current
->mm
);
6933 ctx
->sqo_mm
= current
->mm
;
6935 if (ctx
->flags
& IORING_SETUP_SQPOLL
) {
6937 if (!capable(CAP_SYS_ADMIN
))
6940 ctx
->sq_thread_idle
= msecs_to_jiffies(p
->sq_thread_idle
);
6941 if (!ctx
->sq_thread_idle
)
6942 ctx
->sq_thread_idle
= HZ
;
6944 if (p
->flags
& IORING_SETUP_SQ_AFF
) {
6945 int cpu
= p
->sq_thread_cpu
;
6948 if (cpu
>= nr_cpu_ids
)
6950 if (!cpu_online(cpu
))
6953 ctx
->sqo_thread
= kthread_create_on_cpu(io_sq_thread
,
6957 ctx
->sqo_thread
= kthread_create(io_sq_thread
, ctx
,
6960 if (IS_ERR(ctx
->sqo_thread
)) {
6961 ret
= PTR_ERR(ctx
->sqo_thread
);
6962 ctx
->sqo_thread
= NULL
;
6965 wake_up_process(ctx
->sqo_thread
);
6966 } else if (p
->flags
& IORING_SETUP_SQ_AFF
) {
6967 /* Can't have SQ_AFF without SQPOLL */
6972 ret
= io_init_wq_offload(ctx
, p
);
6978 io_finish_async(ctx
);
6979 mmdrop(ctx
->sqo_mm
);
6984 static void io_unaccount_mem(struct user_struct
*user
, unsigned long nr_pages
)
6986 atomic_long_sub(nr_pages
, &user
->locked_vm
);
6989 static int io_account_mem(struct user_struct
*user
, unsigned long nr_pages
)
6991 unsigned long page_limit
, cur_pages
, new_pages
;
6993 /* Don't allow more pages than we can safely lock */
6994 page_limit
= rlimit(RLIMIT_MEMLOCK
) >> PAGE_SHIFT
;
6997 cur_pages
= atomic_long_read(&user
->locked_vm
);
6998 new_pages
= cur_pages
+ nr_pages
;
6999 if (new_pages
> page_limit
)
7001 } while (atomic_long_cmpxchg(&user
->locked_vm
, cur_pages
,
7002 new_pages
) != cur_pages
);
7007 static void io_mem_free(void *ptr
)
7014 page
= virt_to_head_page(ptr
);
7015 if (put_page_testzero(page
))
7016 free_compound_page(page
);
7019 static void *io_mem_alloc(size_t size
)
7021 gfp_t gfp_flags
= GFP_KERNEL
| __GFP_ZERO
| __GFP_NOWARN
| __GFP_COMP
|
7024 return (void *) __get_free_pages(gfp_flags
, get_order(size
));
7027 static unsigned long rings_size(unsigned sq_entries
, unsigned cq_entries
,
7030 struct io_rings
*rings
;
7031 size_t off
, sq_array_size
;
7033 off
= struct_size(rings
, cqes
, cq_entries
);
7034 if (off
== SIZE_MAX
)
7038 off
= ALIGN(off
, SMP_CACHE_BYTES
);
7043 sq_array_size
= array_size(sizeof(u32
), sq_entries
);
7044 if (sq_array_size
== SIZE_MAX
)
7047 if (check_add_overflow(off
, sq_array_size
, &off
))
7056 static unsigned long ring_pages(unsigned sq_entries
, unsigned cq_entries
)
7060 pages
= (size_t)1 << get_order(
7061 rings_size(sq_entries
, cq_entries
, NULL
));
7062 pages
+= (size_t)1 << get_order(
7063 array_size(sizeof(struct io_uring_sqe
), sq_entries
));
7068 static int io_sqe_buffer_unregister(struct io_ring_ctx
*ctx
)
7072 if (!ctx
->user_bufs
)
7075 for (i
= 0; i
< ctx
->nr_user_bufs
; i
++) {
7076 struct io_mapped_ubuf
*imu
= &ctx
->user_bufs
[i
];
7078 for (j
= 0; j
< imu
->nr_bvecs
; j
++)
7079 unpin_user_page(imu
->bvec
[j
].bv_page
);
7081 if (ctx
->account_mem
)
7082 io_unaccount_mem(ctx
->user
, imu
->nr_bvecs
);
7087 kfree(ctx
->user_bufs
);
7088 ctx
->user_bufs
= NULL
;
7089 ctx
->nr_user_bufs
= 0;
7093 static int io_copy_iov(struct io_ring_ctx
*ctx
, struct iovec
*dst
,
7094 void __user
*arg
, unsigned index
)
7096 struct iovec __user
*src
;
7098 #ifdef CONFIG_COMPAT
7100 struct compat_iovec __user
*ciovs
;
7101 struct compat_iovec ciov
;
7103 ciovs
= (struct compat_iovec __user
*) arg
;
7104 if (copy_from_user(&ciov
, &ciovs
[index
], sizeof(ciov
)))
7107 dst
->iov_base
= u64_to_user_ptr((u64
)ciov
.iov_base
);
7108 dst
->iov_len
= ciov
.iov_len
;
7112 src
= (struct iovec __user
*) arg
;
7113 if (copy_from_user(dst
, &src
[index
], sizeof(*dst
)))
7118 static int io_sqe_buffer_register(struct io_ring_ctx
*ctx
, void __user
*arg
,
7121 struct vm_area_struct
**vmas
= NULL
;
7122 struct page
**pages
= NULL
;
7123 int i
, j
, got_pages
= 0;
7128 if (!nr_args
|| nr_args
> UIO_MAXIOV
)
7131 ctx
->user_bufs
= kcalloc(nr_args
, sizeof(struct io_mapped_ubuf
),
7133 if (!ctx
->user_bufs
)
7136 for (i
= 0; i
< nr_args
; i
++) {
7137 struct io_mapped_ubuf
*imu
= &ctx
->user_bufs
[i
];
7138 unsigned long off
, start
, end
, ubuf
;
7143 ret
= io_copy_iov(ctx
, &iov
, arg
, i
);
7148 * Don't impose further limits on the size and buffer
7149 * constraints here, we'll -EINVAL later when IO is
7150 * submitted if they are wrong.
7153 if (!iov
.iov_base
|| !iov
.iov_len
)
7156 /* arbitrary limit, but we need something */
7157 if (iov
.iov_len
> SZ_1G
)
7160 ubuf
= (unsigned long) iov
.iov_base
;
7161 end
= (ubuf
+ iov
.iov_len
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
7162 start
= ubuf
>> PAGE_SHIFT
;
7163 nr_pages
= end
- start
;
7165 if (ctx
->account_mem
) {
7166 ret
= io_account_mem(ctx
->user
, nr_pages
);
7172 if (!pages
|| nr_pages
> got_pages
) {
7175 pages
= kvmalloc_array(nr_pages
, sizeof(struct page
*),
7177 vmas
= kvmalloc_array(nr_pages
,
7178 sizeof(struct vm_area_struct
*),
7180 if (!pages
|| !vmas
) {
7182 if (ctx
->account_mem
)
7183 io_unaccount_mem(ctx
->user
, nr_pages
);
7186 got_pages
= nr_pages
;
7189 imu
->bvec
= kvmalloc_array(nr_pages
, sizeof(struct bio_vec
),
7193 if (ctx
->account_mem
)
7194 io_unaccount_mem(ctx
->user
, nr_pages
);
7199 down_read(¤t
->mm
->mmap_sem
);
7200 pret
= pin_user_pages(ubuf
, nr_pages
,
7201 FOLL_WRITE
| FOLL_LONGTERM
,
7203 if (pret
== nr_pages
) {
7204 /* don't support file backed memory */
7205 for (j
= 0; j
< nr_pages
; j
++) {
7206 struct vm_area_struct
*vma
= vmas
[j
];
7209 !is_file_hugepages(vma
->vm_file
)) {
7215 ret
= pret
< 0 ? pret
: -EFAULT
;
7217 up_read(¤t
->mm
->mmap_sem
);
7220 * if we did partial map, or found file backed vmas,
7221 * release any pages we did get
7224 unpin_user_pages(pages
, pret
);
7225 if (ctx
->account_mem
)
7226 io_unaccount_mem(ctx
->user
, nr_pages
);
7231 off
= ubuf
& ~PAGE_MASK
;
7233 for (j
= 0; j
< nr_pages
; j
++) {
7236 vec_len
= min_t(size_t, size
, PAGE_SIZE
- off
);
7237 imu
->bvec
[j
].bv_page
= pages
[j
];
7238 imu
->bvec
[j
].bv_len
= vec_len
;
7239 imu
->bvec
[j
].bv_offset
= off
;
7243 /* store original address for later verification */
7245 imu
->len
= iov
.iov_len
;
7246 imu
->nr_bvecs
= nr_pages
;
7248 ctx
->nr_user_bufs
++;
7256 io_sqe_buffer_unregister(ctx
);
7260 static int io_eventfd_register(struct io_ring_ctx
*ctx
, void __user
*arg
)
7262 __s32 __user
*fds
= arg
;
7268 if (copy_from_user(&fd
, fds
, sizeof(*fds
)))
7271 ctx
->cq_ev_fd
= eventfd_ctx_fdget(fd
);
7272 if (IS_ERR(ctx
->cq_ev_fd
)) {
7273 int ret
= PTR_ERR(ctx
->cq_ev_fd
);
7274 ctx
->cq_ev_fd
= NULL
;
7281 static int io_eventfd_unregister(struct io_ring_ctx
*ctx
)
7283 if (ctx
->cq_ev_fd
) {
7284 eventfd_ctx_put(ctx
->cq_ev_fd
);
7285 ctx
->cq_ev_fd
= NULL
;
7292 static int __io_destroy_buffers(int id
, void *p
, void *data
)
7294 struct io_ring_ctx
*ctx
= data
;
7295 struct io_buffer
*buf
= p
;
7297 __io_remove_buffers(ctx
, buf
, id
, -1U);
7301 static void io_destroy_buffers(struct io_ring_ctx
*ctx
)
7303 idr_for_each(&ctx
->io_buffer_idr
, __io_destroy_buffers
, ctx
);
7304 idr_destroy(&ctx
->io_buffer_idr
);
7307 static void io_ring_ctx_free(struct io_ring_ctx
*ctx
)
7309 io_finish_async(ctx
);
7311 mmdrop(ctx
->sqo_mm
);
7313 io_iopoll_reap_events(ctx
);
7314 io_sqe_buffer_unregister(ctx
);
7315 io_sqe_files_unregister(ctx
);
7316 io_eventfd_unregister(ctx
);
7317 io_destroy_buffers(ctx
);
7318 idr_destroy(&ctx
->personality_idr
);
7320 #if defined(CONFIG_UNIX)
7321 if (ctx
->ring_sock
) {
7322 ctx
->ring_sock
->file
= NULL
; /* so that iput() is called */
7323 sock_release(ctx
->ring_sock
);
7327 io_mem_free(ctx
->rings
);
7328 io_mem_free(ctx
->sq_sqes
);
7330 percpu_ref_exit(&ctx
->refs
);
7331 if (ctx
->account_mem
)
7332 io_unaccount_mem(ctx
->user
,
7333 ring_pages(ctx
->sq_entries
, ctx
->cq_entries
));
7334 free_uid(ctx
->user
);
7335 put_cred(ctx
->creds
);
7336 kfree(ctx
->completions
);
7337 kfree(ctx
->cancel_hash
);
7338 kmem_cache_free(req_cachep
, ctx
->fallback_req
);
7342 static __poll_t
io_uring_poll(struct file
*file
, poll_table
*wait
)
7344 struct io_ring_ctx
*ctx
= file
->private_data
;
7347 poll_wait(file
, &ctx
->cq_wait
, wait
);
7349 * synchronizes with barrier from wq_has_sleeper call in
7353 if (READ_ONCE(ctx
->rings
->sq
.tail
) - ctx
->cached_sq_head
!=
7354 ctx
->rings
->sq_ring_entries
)
7355 mask
|= EPOLLOUT
| EPOLLWRNORM
;
7356 if (io_cqring_events(ctx
, false))
7357 mask
|= EPOLLIN
| EPOLLRDNORM
;
7362 static int io_uring_fasync(int fd
, struct file
*file
, int on
)
7364 struct io_ring_ctx
*ctx
= file
->private_data
;
7366 return fasync_helper(fd
, file
, on
, &ctx
->cq_fasync
);
7369 static int io_remove_personalities(int id
, void *p
, void *data
)
7371 struct io_ring_ctx
*ctx
= data
;
7372 const struct cred
*cred
;
7374 cred
= idr_remove(&ctx
->personality_idr
, id
);
7380 static void io_ring_exit_work(struct work_struct
*work
)
7382 struct io_ring_ctx
*ctx
;
7384 ctx
= container_of(work
, struct io_ring_ctx
, exit_work
);
7386 io_cqring_overflow_flush(ctx
, true);
7388 wait_for_completion(&ctx
->completions
[0]);
7389 io_ring_ctx_free(ctx
);
7392 static void io_ring_ctx_wait_and_kill(struct io_ring_ctx
*ctx
)
7394 mutex_lock(&ctx
->uring_lock
);
7395 percpu_ref_kill(&ctx
->refs
);
7396 mutex_unlock(&ctx
->uring_lock
);
7399 * Wait for sq thread to idle, if we have one. It won't spin on new
7400 * work after we've killed the ctx ref above. This is important to do
7401 * before we cancel existing commands, as the thread could otherwise
7402 * be queueing new work post that. If that's work we need to cancel,
7403 * it could cause shutdown to hang.
7405 while (ctx
->sqo_thread
&& !wq_has_sleeper(&ctx
->sqo_wait
))
7408 io_kill_timeouts(ctx
);
7409 io_poll_remove_all(ctx
);
7412 io_wq_cancel_all(ctx
->io_wq
);
7414 io_iopoll_reap_events(ctx
);
7415 /* if we failed setting up the ctx, we might not have any rings */
7417 io_cqring_overflow_flush(ctx
, true);
7418 idr_for_each(&ctx
->personality_idr
, io_remove_personalities
, ctx
);
7419 INIT_WORK(&ctx
->exit_work
, io_ring_exit_work
);
7420 queue_work(system_wq
, &ctx
->exit_work
);
7423 static int io_uring_release(struct inode
*inode
, struct file
*file
)
7425 struct io_ring_ctx
*ctx
= file
->private_data
;
7427 file
->private_data
= NULL
;
7428 io_ring_ctx_wait_and_kill(ctx
);
7432 static void io_uring_cancel_files(struct io_ring_ctx
*ctx
,
7433 struct files_struct
*files
)
7435 while (!list_empty_careful(&ctx
->inflight_list
)) {
7436 struct io_kiocb
*cancel_req
= NULL
, *req
;
7439 spin_lock_irq(&ctx
->inflight_lock
);
7440 list_for_each_entry(req
, &ctx
->inflight_list
, inflight_entry
) {
7441 if (req
->work
.files
!= files
)
7443 /* req is being completed, ignore */
7444 if (!refcount_inc_not_zero(&req
->refs
))
7450 prepare_to_wait(&ctx
->inflight_wait
, &wait
,
7451 TASK_UNINTERRUPTIBLE
);
7452 spin_unlock_irq(&ctx
->inflight_lock
);
7454 /* We need to keep going until we don't find a matching req */
7458 if (cancel_req
->flags
& REQ_F_OVERFLOW
) {
7459 spin_lock_irq(&ctx
->completion_lock
);
7460 list_del(&cancel_req
->list
);
7461 cancel_req
->flags
&= ~REQ_F_OVERFLOW
;
7462 if (list_empty(&ctx
->cq_overflow_list
)) {
7463 clear_bit(0, &ctx
->sq_check_overflow
);
7464 clear_bit(0, &ctx
->cq_check_overflow
);
7466 spin_unlock_irq(&ctx
->completion_lock
);
7468 WRITE_ONCE(ctx
->rings
->cq_overflow
,
7469 atomic_inc_return(&ctx
->cached_cq_overflow
));
7472 * Put inflight ref and overflow ref. If that's
7473 * all we had, then we're done with this request.
7475 if (refcount_sub_and_test(2, &cancel_req
->refs
)) {
7476 io_free_req(cancel_req
);
7477 finish_wait(&ctx
->inflight_wait
, &wait
);
7481 io_wq_cancel_work(ctx
->io_wq
, &cancel_req
->work
);
7482 io_put_req(cancel_req
);
7486 finish_wait(&ctx
->inflight_wait
, &wait
);
7490 static int io_uring_flush(struct file
*file
, void *data
)
7492 struct io_ring_ctx
*ctx
= file
->private_data
;
7494 io_uring_cancel_files(ctx
, data
);
7497 * If the task is going away, cancel work it may have pending
7499 if (fatal_signal_pending(current
) || (current
->flags
& PF_EXITING
))
7500 io_wq_cancel_pid(ctx
->io_wq
, task_pid_vnr(current
));
7505 static void *io_uring_validate_mmap_request(struct file
*file
,
7506 loff_t pgoff
, size_t sz
)
7508 struct io_ring_ctx
*ctx
= file
->private_data
;
7509 loff_t offset
= pgoff
<< PAGE_SHIFT
;
7514 case IORING_OFF_SQ_RING
:
7515 case IORING_OFF_CQ_RING
:
7518 case IORING_OFF_SQES
:
7522 return ERR_PTR(-EINVAL
);
7525 page
= virt_to_head_page(ptr
);
7526 if (sz
> page_size(page
))
7527 return ERR_PTR(-EINVAL
);
7534 static int io_uring_mmap(struct file
*file
, struct vm_area_struct
*vma
)
7536 size_t sz
= vma
->vm_end
- vma
->vm_start
;
7540 ptr
= io_uring_validate_mmap_request(file
, vma
->vm_pgoff
, sz
);
7542 return PTR_ERR(ptr
);
7544 pfn
= virt_to_phys(ptr
) >> PAGE_SHIFT
;
7545 return remap_pfn_range(vma
, vma
->vm_start
, pfn
, sz
, vma
->vm_page_prot
);
7548 #else /* !CONFIG_MMU */
7550 static int io_uring_mmap(struct file
*file
, struct vm_area_struct
*vma
)
7552 return vma
->vm_flags
& (VM_SHARED
| VM_MAYSHARE
) ? 0 : -EINVAL
;
7555 static unsigned int io_uring_nommu_mmap_capabilities(struct file
*file
)
7557 return NOMMU_MAP_DIRECT
| NOMMU_MAP_READ
| NOMMU_MAP_WRITE
;
7560 static unsigned long io_uring_nommu_get_unmapped_area(struct file
*file
,
7561 unsigned long addr
, unsigned long len
,
7562 unsigned long pgoff
, unsigned long flags
)
7566 ptr
= io_uring_validate_mmap_request(file
, pgoff
, len
);
7568 return PTR_ERR(ptr
);
7570 return (unsigned long) ptr
;
7573 #endif /* !CONFIG_MMU */
7575 SYSCALL_DEFINE6(io_uring_enter
, unsigned int, fd
, u32
, to_submit
,
7576 u32
, min_complete
, u32
, flags
, const sigset_t __user
*, sig
,
7579 struct io_ring_ctx
*ctx
;
7584 if (current
->task_works
)
7587 if (flags
& ~(IORING_ENTER_GETEVENTS
| IORING_ENTER_SQ_WAKEUP
))
7595 if (f
.file
->f_op
!= &io_uring_fops
)
7599 ctx
= f
.file
->private_data
;
7600 if (!percpu_ref_tryget(&ctx
->refs
))
7604 * For SQ polling, the thread will do all submissions and completions.
7605 * Just return the requested submit count, and wake the thread if
7609 if (ctx
->flags
& IORING_SETUP_SQPOLL
) {
7610 if (!list_empty_careful(&ctx
->cq_overflow_list
))
7611 io_cqring_overflow_flush(ctx
, false);
7612 if (flags
& IORING_ENTER_SQ_WAKEUP
)
7613 wake_up(&ctx
->sqo_wait
);
7614 submitted
= to_submit
;
7615 } else if (to_submit
) {
7616 mutex_lock(&ctx
->uring_lock
);
7617 submitted
= io_submit_sqes(ctx
, to_submit
, f
.file
, fd
, false);
7618 mutex_unlock(&ctx
->uring_lock
);
7620 if (submitted
!= to_submit
)
7623 if (flags
& IORING_ENTER_GETEVENTS
) {
7624 unsigned nr_events
= 0;
7626 min_complete
= min(min_complete
, ctx
->cq_entries
);
7629 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
7630 * space applications don't need to do io completion events
7631 * polling again, they can rely on io_sq_thread to do polling
7632 * work, which can reduce cpu usage and uring_lock contention.
7634 if (ctx
->flags
& IORING_SETUP_IOPOLL
&&
7635 !(ctx
->flags
& IORING_SETUP_SQPOLL
)) {
7636 ret
= io_iopoll_check(ctx
, &nr_events
, min_complete
);
7638 ret
= io_cqring_wait(ctx
, min_complete
, sig
, sigsz
);
7643 percpu_ref_put(&ctx
->refs
);
7646 return submitted
? submitted
: ret
;
7649 #ifdef CONFIG_PROC_FS
7650 static int io_uring_show_cred(int id
, void *p
, void *data
)
7652 const struct cred
*cred
= p
;
7653 struct seq_file
*m
= data
;
7654 struct user_namespace
*uns
= seq_user_ns(m
);
7655 struct group_info
*gi
;
7660 seq_printf(m
, "%5d\n", id
);
7661 seq_put_decimal_ull(m
, "\tUid:\t", from_kuid_munged(uns
, cred
->uid
));
7662 seq_put_decimal_ull(m
, "\t\t", from_kuid_munged(uns
, cred
->euid
));
7663 seq_put_decimal_ull(m
, "\t\t", from_kuid_munged(uns
, cred
->suid
));
7664 seq_put_decimal_ull(m
, "\t\t", from_kuid_munged(uns
, cred
->fsuid
));
7665 seq_put_decimal_ull(m
, "\n\tGid:\t", from_kgid_munged(uns
, cred
->gid
));
7666 seq_put_decimal_ull(m
, "\t\t", from_kgid_munged(uns
, cred
->egid
));
7667 seq_put_decimal_ull(m
, "\t\t", from_kgid_munged(uns
, cred
->sgid
));
7668 seq_put_decimal_ull(m
, "\t\t", from_kgid_munged(uns
, cred
->fsgid
));
7669 seq_puts(m
, "\n\tGroups:\t");
7670 gi
= cred
->group_info
;
7671 for (g
= 0; g
< gi
->ngroups
; g
++) {
7672 seq_put_decimal_ull(m
, g
? " " : "",
7673 from_kgid_munged(uns
, gi
->gid
[g
]));
7675 seq_puts(m
, "\n\tCapEff:\t");
7676 cap
= cred
->cap_effective
;
7677 CAP_FOR_EACH_U32(__capi
)
7678 seq_put_hex_ll(m
, NULL
, cap
.cap
[CAP_LAST_U32
- __capi
], 8);
7683 static void __io_uring_show_fdinfo(struct io_ring_ctx
*ctx
, struct seq_file
*m
)
7687 mutex_lock(&ctx
->uring_lock
);
7688 seq_printf(m
, "UserFiles:\t%u\n", ctx
->nr_user_files
);
7689 for (i
= 0; i
< ctx
->nr_user_files
; i
++) {
7690 struct fixed_file_table
*table
;
7693 table
= &ctx
->file_data
->table
[i
>> IORING_FILE_TABLE_SHIFT
];
7694 f
= table
->files
[i
& IORING_FILE_TABLE_MASK
];
7696 seq_printf(m
, "%5u: %s\n", i
, file_dentry(f
)->d_iname
);
7698 seq_printf(m
, "%5u: <none>\n", i
);
7700 seq_printf(m
, "UserBufs:\t%u\n", ctx
->nr_user_bufs
);
7701 for (i
= 0; i
< ctx
->nr_user_bufs
; i
++) {
7702 struct io_mapped_ubuf
*buf
= &ctx
->user_bufs
[i
];
7704 seq_printf(m
, "%5u: 0x%llx/%u\n", i
, buf
->ubuf
,
7705 (unsigned int) buf
->len
);
7707 if (!idr_is_empty(&ctx
->personality_idr
)) {
7708 seq_printf(m
, "Personalities:\n");
7709 idr_for_each(&ctx
->personality_idr
, io_uring_show_cred
, m
);
7711 seq_printf(m
, "PollList:\n");
7712 spin_lock_irq(&ctx
->completion_lock
);
7713 for (i
= 0; i
< (1U << ctx
->cancel_hash_bits
); i
++) {
7714 struct hlist_head
*list
= &ctx
->cancel_hash
[i
];
7715 struct io_kiocb
*req
;
7717 hlist_for_each_entry(req
, list
, hash_node
)
7718 seq_printf(m
, " op=%d, task_works=%d\n", req
->opcode
,
7719 req
->task
->task_works
!= NULL
);
7721 spin_unlock_irq(&ctx
->completion_lock
);
7722 mutex_unlock(&ctx
->uring_lock
);
7725 static void io_uring_show_fdinfo(struct seq_file
*m
, struct file
*f
)
7727 struct io_ring_ctx
*ctx
= f
->private_data
;
7729 if (percpu_ref_tryget(&ctx
->refs
)) {
7730 __io_uring_show_fdinfo(ctx
, m
);
7731 percpu_ref_put(&ctx
->refs
);
7736 static const struct file_operations io_uring_fops
= {
7737 .release
= io_uring_release
,
7738 .flush
= io_uring_flush
,
7739 .mmap
= io_uring_mmap
,
7741 .get_unmapped_area
= io_uring_nommu_get_unmapped_area
,
7742 .mmap_capabilities
= io_uring_nommu_mmap_capabilities
,
7744 .poll
= io_uring_poll
,
7745 .fasync
= io_uring_fasync
,
7746 #ifdef CONFIG_PROC_FS
7747 .show_fdinfo
= io_uring_show_fdinfo
,
7751 static int io_allocate_scq_urings(struct io_ring_ctx
*ctx
,
7752 struct io_uring_params
*p
)
7754 struct io_rings
*rings
;
7755 size_t size
, sq_array_offset
;
7757 size
= rings_size(p
->sq_entries
, p
->cq_entries
, &sq_array_offset
);
7758 if (size
== SIZE_MAX
)
7761 rings
= io_mem_alloc(size
);
7766 ctx
->sq_array
= (u32
*)((char *)rings
+ sq_array_offset
);
7767 rings
->sq_ring_mask
= p
->sq_entries
- 1;
7768 rings
->cq_ring_mask
= p
->cq_entries
- 1;
7769 rings
->sq_ring_entries
= p
->sq_entries
;
7770 rings
->cq_ring_entries
= p
->cq_entries
;
7771 ctx
->sq_mask
= rings
->sq_ring_mask
;
7772 ctx
->cq_mask
= rings
->cq_ring_mask
;
7773 ctx
->sq_entries
= rings
->sq_ring_entries
;
7774 ctx
->cq_entries
= rings
->cq_ring_entries
;
7776 size
= array_size(sizeof(struct io_uring_sqe
), p
->sq_entries
);
7777 if (size
== SIZE_MAX
) {
7778 io_mem_free(ctx
->rings
);
7783 ctx
->sq_sqes
= io_mem_alloc(size
);
7784 if (!ctx
->sq_sqes
) {
7785 io_mem_free(ctx
->rings
);
7794 * Allocate an anonymous fd, this is what constitutes the application
7795 * visible backing of an io_uring instance. The application mmaps this
7796 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
7797 * we have to tie this fd to a socket for file garbage collection purposes.
7799 static int io_uring_get_fd(struct io_ring_ctx
*ctx
)
7804 #if defined(CONFIG_UNIX)
7805 ret
= sock_create_kern(&init_net
, PF_UNIX
, SOCK_RAW
, IPPROTO_IP
,
7811 ret
= get_unused_fd_flags(O_RDWR
| O_CLOEXEC
);
7815 file
= anon_inode_getfile("[io_uring]", &io_uring_fops
, ctx
,
7816 O_RDWR
| O_CLOEXEC
);
7819 ret
= PTR_ERR(file
);
7823 #if defined(CONFIG_UNIX)
7824 ctx
->ring_sock
->file
= file
;
7826 fd_install(ret
, file
);
7829 #if defined(CONFIG_UNIX)
7830 sock_release(ctx
->ring_sock
);
7831 ctx
->ring_sock
= NULL
;
7836 static int io_uring_create(unsigned entries
, struct io_uring_params
*p
,
7837 struct io_uring_params __user
*params
)
7839 struct user_struct
*user
= NULL
;
7840 struct io_ring_ctx
*ctx
;
7846 if (entries
> IORING_MAX_ENTRIES
) {
7847 if (!(p
->flags
& IORING_SETUP_CLAMP
))
7849 entries
= IORING_MAX_ENTRIES
;
7853 * Use twice as many entries for the CQ ring. It's possible for the
7854 * application to drive a higher depth than the size of the SQ ring,
7855 * since the sqes are only used at submission time. This allows for
7856 * some flexibility in overcommitting a bit. If the application has
7857 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
7858 * of CQ ring entries manually.
7860 p
->sq_entries
= roundup_pow_of_two(entries
);
7861 if (p
->flags
& IORING_SETUP_CQSIZE
) {
7863 * If IORING_SETUP_CQSIZE is set, we do the same roundup
7864 * to a power-of-two, if it isn't already. We do NOT impose
7865 * any cq vs sq ring sizing.
7867 if (p
->cq_entries
< p
->sq_entries
)
7869 if (p
->cq_entries
> IORING_MAX_CQ_ENTRIES
) {
7870 if (!(p
->flags
& IORING_SETUP_CLAMP
))
7872 p
->cq_entries
= IORING_MAX_CQ_ENTRIES
;
7874 p
->cq_entries
= roundup_pow_of_two(p
->cq_entries
);
7876 p
->cq_entries
= 2 * p
->sq_entries
;
7879 user
= get_uid(current_user());
7880 account_mem
= !capable(CAP_IPC_LOCK
);
7883 ret
= io_account_mem(user
,
7884 ring_pages(p
->sq_entries
, p
->cq_entries
));
7891 ctx
= io_ring_ctx_alloc(p
);
7894 io_unaccount_mem(user
, ring_pages(p
->sq_entries
,
7899 ctx
->compat
= in_compat_syscall();
7900 ctx
->account_mem
= account_mem
;
7902 ctx
->creds
= get_current_cred();
7904 ret
= io_allocate_scq_urings(ctx
, p
);
7908 ret
= io_sq_offload_start(ctx
, p
);
7912 memset(&p
->sq_off
, 0, sizeof(p
->sq_off
));
7913 p
->sq_off
.head
= offsetof(struct io_rings
, sq
.head
);
7914 p
->sq_off
.tail
= offsetof(struct io_rings
, sq
.tail
);
7915 p
->sq_off
.ring_mask
= offsetof(struct io_rings
, sq_ring_mask
);
7916 p
->sq_off
.ring_entries
= offsetof(struct io_rings
, sq_ring_entries
);
7917 p
->sq_off
.flags
= offsetof(struct io_rings
, sq_flags
);
7918 p
->sq_off
.dropped
= offsetof(struct io_rings
, sq_dropped
);
7919 p
->sq_off
.array
= (char *)ctx
->sq_array
- (char *)ctx
->rings
;
7921 memset(&p
->cq_off
, 0, sizeof(p
->cq_off
));
7922 p
->cq_off
.head
= offsetof(struct io_rings
, cq
.head
);
7923 p
->cq_off
.tail
= offsetof(struct io_rings
, cq
.tail
);
7924 p
->cq_off
.ring_mask
= offsetof(struct io_rings
, cq_ring_mask
);
7925 p
->cq_off
.ring_entries
= offsetof(struct io_rings
, cq_ring_entries
);
7926 p
->cq_off
.overflow
= offsetof(struct io_rings
, cq_overflow
);
7927 p
->cq_off
.cqes
= offsetof(struct io_rings
, cqes
);
7929 p
->features
= IORING_FEAT_SINGLE_MMAP
| IORING_FEAT_NODROP
|
7930 IORING_FEAT_SUBMIT_STABLE
| IORING_FEAT_RW_CUR_POS
|
7931 IORING_FEAT_CUR_PERSONALITY
| IORING_FEAT_FAST_POLL
;
7933 if (copy_to_user(params
, p
, sizeof(*p
))) {
7938 * Install ring fd as the very last thing, so we don't risk someone
7939 * having closed it before we finish setup
7941 ret
= io_uring_get_fd(ctx
);
7945 trace_io_uring_create(ret
, ctx
, p
->sq_entries
, p
->cq_entries
, p
->flags
);
7948 io_ring_ctx_wait_and_kill(ctx
);
7953 * Sets up an aio uring context, and returns the fd. Applications asks for a
7954 * ring size, we return the actual sq/cq ring sizes (among other things) in the
7955 * params structure passed in.
7957 static long io_uring_setup(u32 entries
, struct io_uring_params __user
*params
)
7959 struct io_uring_params p
;
7962 if (copy_from_user(&p
, params
, sizeof(p
)))
7964 for (i
= 0; i
< ARRAY_SIZE(p
.resv
); i
++) {
7969 if (p
.flags
& ~(IORING_SETUP_IOPOLL
| IORING_SETUP_SQPOLL
|
7970 IORING_SETUP_SQ_AFF
| IORING_SETUP_CQSIZE
|
7971 IORING_SETUP_CLAMP
| IORING_SETUP_ATTACH_WQ
))
7974 return io_uring_create(entries
, &p
, params
);
7977 SYSCALL_DEFINE2(io_uring_setup
, u32
, entries
,
7978 struct io_uring_params __user
*, params
)
7980 return io_uring_setup(entries
, params
);
7983 static int io_probe(struct io_ring_ctx
*ctx
, void __user
*arg
, unsigned nr_args
)
7985 struct io_uring_probe
*p
;
7989 size
= struct_size(p
, ops
, nr_args
);
7990 if (size
== SIZE_MAX
)
7992 p
= kzalloc(size
, GFP_KERNEL
);
7997 if (copy_from_user(p
, arg
, size
))
8000 if (memchr_inv(p
, 0, size
))
8003 p
->last_op
= IORING_OP_LAST
- 1;
8004 if (nr_args
> IORING_OP_LAST
)
8005 nr_args
= IORING_OP_LAST
;
8007 for (i
= 0; i
< nr_args
; i
++) {
8009 if (!io_op_defs
[i
].not_supported
)
8010 p
->ops
[i
].flags
= IO_URING_OP_SUPPORTED
;
8015 if (copy_to_user(arg
, p
, size
))
8022 static int io_register_personality(struct io_ring_ctx
*ctx
)
8024 const struct cred
*creds
= get_current_cred();
8027 id
= idr_alloc_cyclic(&ctx
->personality_idr
, (void *) creds
, 1,
8028 USHRT_MAX
, GFP_KERNEL
);
8034 static int io_unregister_personality(struct io_ring_ctx
*ctx
, unsigned id
)
8036 const struct cred
*old_creds
;
8038 old_creds
= idr_remove(&ctx
->personality_idr
, id
);
8040 put_cred(old_creds
);
8047 static bool io_register_op_must_quiesce(int op
)
8050 case IORING_UNREGISTER_FILES
:
8051 case IORING_REGISTER_FILES_UPDATE
:
8052 case IORING_REGISTER_PROBE
:
8053 case IORING_REGISTER_PERSONALITY
:
8054 case IORING_UNREGISTER_PERSONALITY
:
8061 static int __io_uring_register(struct io_ring_ctx
*ctx
, unsigned opcode
,
8062 void __user
*arg
, unsigned nr_args
)
8063 __releases(ctx
->uring_lock
)
8064 __acquires(ctx
->uring_lock
)
8069 * We're inside the ring mutex, if the ref is already dying, then
8070 * someone else killed the ctx or is already going through
8071 * io_uring_register().
8073 if (percpu_ref_is_dying(&ctx
->refs
))
8076 if (io_register_op_must_quiesce(opcode
)) {
8077 percpu_ref_kill(&ctx
->refs
);
8080 * Drop uring mutex before waiting for references to exit. If
8081 * another thread is currently inside io_uring_enter() it might
8082 * need to grab the uring_lock to make progress. If we hold it
8083 * here across the drain wait, then we can deadlock. It's safe
8084 * to drop the mutex here, since no new references will come in
8085 * after we've killed the percpu ref.
8087 mutex_unlock(&ctx
->uring_lock
);
8088 ret
= wait_for_completion_interruptible(&ctx
->completions
[0]);
8089 mutex_lock(&ctx
->uring_lock
);
8091 percpu_ref_resurrect(&ctx
->refs
);
8098 case IORING_REGISTER_BUFFERS
:
8099 ret
= io_sqe_buffer_register(ctx
, arg
, nr_args
);
8101 case IORING_UNREGISTER_BUFFERS
:
8105 ret
= io_sqe_buffer_unregister(ctx
);
8107 case IORING_REGISTER_FILES
:
8108 ret
= io_sqe_files_register(ctx
, arg
, nr_args
);
8110 case IORING_UNREGISTER_FILES
:
8114 ret
= io_sqe_files_unregister(ctx
);
8116 case IORING_REGISTER_FILES_UPDATE
:
8117 ret
= io_sqe_files_update(ctx
, arg
, nr_args
);
8119 case IORING_REGISTER_EVENTFD
:
8120 case IORING_REGISTER_EVENTFD_ASYNC
:
8124 ret
= io_eventfd_register(ctx
, arg
);
8127 if (opcode
== IORING_REGISTER_EVENTFD_ASYNC
)
8128 ctx
->eventfd_async
= 1;
8130 ctx
->eventfd_async
= 0;
8132 case IORING_UNREGISTER_EVENTFD
:
8136 ret
= io_eventfd_unregister(ctx
);
8138 case IORING_REGISTER_PROBE
:
8140 if (!arg
|| nr_args
> 256)
8142 ret
= io_probe(ctx
, arg
, nr_args
);
8144 case IORING_REGISTER_PERSONALITY
:
8148 ret
= io_register_personality(ctx
);
8150 case IORING_UNREGISTER_PERSONALITY
:
8154 ret
= io_unregister_personality(ctx
, nr_args
);
8161 if (io_register_op_must_quiesce(opcode
)) {
8162 /* bring the ctx back to life */
8163 percpu_ref_reinit(&ctx
->refs
);
8165 reinit_completion(&ctx
->completions
[0]);
8170 SYSCALL_DEFINE4(io_uring_register
, unsigned int, fd
, unsigned int, opcode
,
8171 void __user
*, arg
, unsigned int, nr_args
)
8173 struct io_ring_ctx
*ctx
;
8182 if (f
.file
->f_op
!= &io_uring_fops
)
8185 ctx
= f
.file
->private_data
;
8187 mutex_lock(&ctx
->uring_lock
);
8188 ret
= __io_uring_register(ctx
, opcode
, arg
, nr_args
);
8189 mutex_unlock(&ctx
->uring_lock
);
8190 trace_io_uring_register(ctx
, opcode
, ctx
->nr_user_files
, ctx
->nr_user_bufs
,
8191 ctx
->cq_ev_fd
!= NULL
, ret
);
8197 static int __init
io_uring_init(void)
8199 #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
8200 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
8201 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
8204 #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
8205 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
8206 BUILD_BUG_ON(sizeof(struct io_uring_sqe
) != 64);
8207 BUILD_BUG_SQE_ELEM(0, __u8
, opcode
);
8208 BUILD_BUG_SQE_ELEM(1, __u8
, flags
);
8209 BUILD_BUG_SQE_ELEM(2, __u16
, ioprio
);
8210 BUILD_BUG_SQE_ELEM(4, __s32
, fd
);
8211 BUILD_BUG_SQE_ELEM(8, __u64
, off
);
8212 BUILD_BUG_SQE_ELEM(8, __u64
, addr2
);
8213 BUILD_BUG_SQE_ELEM(16, __u64
, addr
);
8214 BUILD_BUG_SQE_ELEM(16, __u64
, splice_off_in
);
8215 BUILD_BUG_SQE_ELEM(24, __u32
, len
);
8216 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t
, rw_flags
);
8217 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags
);
8218 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32
, rw_flags
);
8219 BUILD_BUG_SQE_ELEM(28, __u32
, fsync_flags
);
8220 BUILD_BUG_SQE_ELEM(28, __u16
, poll_events
);
8221 BUILD_BUG_SQE_ELEM(28, __u32
, sync_range_flags
);
8222 BUILD_BUG_SQE_ELEM(28, __u32
, msg_flags
);
8223 BUILD_BUG_SQE_ELEM(28, __u32
, timeout_flags
);
8224 BUILD_BUG_SQE_ELEM(28, __u32
, accept_flags
);
8225 BUILD_BUG_SQE_ELEM(28, __u32
, cancel_flags
);
8226 BUILD_BUG_SQE_ELEM(28, __u32
, open_flags
);
8227 BUILD_BUG_SQE_ELEM(28, __u32
, statx_flags
);
8228 BUILD_BUG_SQE_ELEM(28, __u32
, fadvise_advice
);
8229 BUILD_BUG_SQE_ELEM(28, __u32
, splice_flags
);
8230 BUILD_BUG_SQE_ELEM(32, __u64
, user_data
);
8231 BUILD_BUG_SQE_ELEM(40, __u16
, buf_index
);
8232 BUILD_BUG_SQE_ELEM(42, __u16
, personality
);
8233 BUILD_BUG_SQE_ELEM(44, __s32
, splice_fd_in
);
8235 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs
) != IORING_OP_LAST
);
8236 BUILD_BUG_ON(__REQ_F_LAST_BIT
>= 8 * sizeof(int));
8237 req_cachep
= KMEM_CACHE(io_kiocb
, SLAB_HWCACHE_ALIGN
| SLAB_PANIC
);
8240 __initcall(io_uring_init
);