1 // SPDX-License-Identifier: GPL-2.0
3 * Shared application/kernel submission and completion ring pairs, for
4 * supporting fast/efficient IO.
6 * A note on the read/write ordering memory barriers that are matched between
7 * the application and kernel side.
9 * After the application reads the CQ ring tail, it must use an
10 * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses
11 * before writing the tail (using smp_load_acquire to read the tail will
12 * do). It also needs a smp_mb() before updating CQ head (ordering the
13 * entry load(s) with the head store), pairing with an implicit barrier
14 * through a control-dependency in io_get_cqring (smp_store_release to
15 * store head will do). Failure to do so could lead to reading invalid
18 * Likewise, the application must use an appropriate smp_wmb() before
19 * writing the SQ tail (ordering SQ entry stores with the tail store),
20 * which pairs with smp_load_acquire in io_get_sqring (smp_store_release
21 * to store the tail will do). And it needs a barrier ordering the SQ
22 * head load before writing new SQ entries (smp_load_acquire to read
25 * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application
26 * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after*
27 * updating the SQ tail; a full memory barrier smp_mb() is needed
30 * Also see the examples in the liburing library:
32 * git://git.kernel.dk/liburing
34 * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens
35 * from data shared between the kernel and application. This is done both
36 * for ordering purposes, but also to ensure that once a value is loaded from
37 * data that the application could potentially modify, it remains stable.
39 * Copyright (C) 2018-2019 Jens Axboe
40 * Copyright (c) 2018-2019 Christoph Hellwig
42 #include <linux/kernel.h>
43 #include <linux/init.h>
44 #include <linux/errno.h>
45 #include <linux/syscalls.h>
46 #include <linux/compat.h>
47 #include <net/compat.h>
48 #include <linux/refcount.h>
49 #include <linux/uio.h>
50 #include <linux/bits.h>
52 #include <linux/sched/signal.h>
54 #include <linux/file.h>
55 #include <linux/fdtable.h>
57 #include <linux/mman.h>
58 #include <linux/percpu.h>
59 #include <linux/slab.h>
60 #include <linux/kthread.h>
61 #include <linux/blkdev.h>
62 #include <linux/bvec.h>
63 #include <linux/net.h>
65 #include <net/af_unix.h>
67 #include <linux/anon_inodes.h>
68 #include <linux/sched/mm.h>
69 #include <linux/uaccess.h>
70 #include <linux/nospec.h>
71 #include <linux/sizes.h>
72 #include <linux/hugetlb.h>
73 #include <linux/highmem.h>
74 #include <linux/namei.h>
75 #include <linux/fsnotify.h>
76 #include <linux/fadvise.h>
77 #include <linux/eventpoll.h>
78 #include <linux/fs_struct.h>
79 #include <linux/splice.h>
80 #include <linux/task_work.h>
81 #include <linux/pagemap.h>
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.
157 * Written by the application, shouldn't be modified by the
162 * Number of completion events lost because the queue was full;
163 * this should be avoided by the application by making sure
164 * there are not more requests pending than there is space in
165 * the completion queue.
167 * Written by the kernel, shouldn't be modified by the
168 * application (i.e. get number of "new events" by comparing to
171 * As completion events come in out of order this counter is not
172 * ordered with any other data.
176 * Ring buffer of completion events.
178 * The kernel writes completion events fresh every time they are
179 * produced, so the application is allowed to modify pending
182 struct io_uring_cqe cqes
[] ____cacheline_aligned_in_smp
;
185 struct io_mapped_ubuf
{
188 struct bio_vec
*bvec
;
189 unsigned int nr_bvecs
;
192 struct fixed_file_table
{
196 struct fixed_file_ref_node
{
197 struct percpu_ref refs
;
198 struct list_head node
;
199 struct list_head file_list
;
200 struct fixed_file_data
*file_data
;
201 struct llist_node llist
;
204 struct fixed_file_data
{
205 struct fixed_file_table
*table
;
206 struct io_ring_ctx
*ctx
;
208 struct percpu_ref
*cur_refs
;
209 struct percpu_ref refs
;
210 struct completion done
;
211 struct list_head ref_list
;
216 struct list_head list
;
224 struct percpu_ref refs
;
225 } ____cacheline_aligned_in_smp
;
229 unsigned int compat
: 1;
230 unsigned int limit_mem
: 1;
231 unsigned int cq_overflow_flushed
: 1;
232 unsigned int drain_next
: 1;
233 unsigned int eventfd_async
: 1;
236 * Ring buffer of indices into array of io_uring_sqe, which is
237 * mmapped by the application using the IORING_OFF_SQES offset.
239 * This indirection could e.g. be used to assign fixed
240 * io_uring_sqe entries to operations and only submit them to
241 * the queue when needed.
243 * The kernel modifies neither the indices array nor the entries
247 unsigned cached_sq_head
;
250 unsigned sq_thread_idle
;
251 unsigned cached_sq_dropped
;
252 atomic_t cached_cq_overflow
;
253 unsigned long sq_check_overflow
;
255 struct list_head defer_list
;
256 struct list_head timeout_list
;
257 struct list_head cq_overflow_list
;
259 wait_queue_head_t inflight_wait
;
260 struct io_uring_sqe
*sq_sqes
;
261 } ____cacheline_aligned_in_smp
;
263 struct io_rings
*rings
;
267 struct task_struct
*sqo_thread
; /* if using sq thread polling */
268 struct mm_struct
*sqo_mm
;
269 wait_queue_head_t sqo_wait
;
272 * If used, fixed file set. Writers must ensure that ->refs is dead,
273 * readers must ensure that ->refs is alive as long as the file* is
274 * used. Only updated through io_uring_register(2).
276 struct fixed_file_data
*file_data
;
277 unsigned nr_user_files
;
279 struct file
*ring_file
;
281 /* if used, fixed mapped user buffers */
282 unsigned nr_user_bufs
;
283 struct io_mapped_ubuf
*user_bufs
;
285 struct user_struct
*user
;
287 const struct cred
*creds
;
289 struct completion ref_comp
;
290 struct completion sq_thread_comp
;
292 /* if all else fails... */
293 struct io_kiocb
*fallback_req
;
295 #if defined(CONFIG_UNIX)
296 struct socket
*ring_sock
;
299 struct idr io_buffer_idr
;
301 struct idr personality_idr
;
304 unsigned cached_cq_tail
;
307 atomic_t cq_timeouts
;
308 unsigned long cq_check_overflow
;
309 struct wait_queue_head cq_wait
;
310 struct fasync_struct
*cq_fasync
;
311 struct eventfd_ctx
*cq_ev_fd
;
312 } ____cacheline_aligned_in_smp
;
315 struct mutex uring_lock
;
316 wait_queue_head_t wait
;
317 } ____cacheline_aligned_in_smp
;
320 spinlock_t completion_lock
;
323 * ->poll_list is protected by the ctx->uring_lock for
324 * io_uring instances that don't use IORING_SETUP_SQPOLL.
325 * For SQPOLL, only the single threaded io_sq_thread() will
326 * manipulate the list, hence no extra locking is needed there.
328 struct list_head poll_list
;
329 struct hlist_head
*cancel_hash
;
330 unsigned cancel_hash_bits
;
331 bool poll_multi_file
;
333 spinlock_t inflight_lock
;
334 struct list_head inflight_list
;
335 } ____cacheline_aligned_in_smp
;
337 struct delayed_work file_put_work
;
338 struct llist_head file_put_llist
;
340 struct work_struct exit_work
;
344 * First field must be the file pointer in all the
345 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
347 struct io_poll_iocb
{
350 struct wait_queue_head
*head
;
356 struct wait_queue_entry wait
;
361 struct file
*put_file
;
365 struct io_timeout_data
{
366 struct io_kiocb
*req
;
367 struct hrtimer timer
;
368 struct timespec64 ts
;
369 enum hrtimer_mode mode
;
374 struct sockaddr __user
*addr
;
375 int __user
*addr_len
;
377 unsigned long nofile
;
402 /* NOTE: kiocb has the file as the first member, so don't do it here */
410 struct sockaddr __user
*addr
;
417 struct user_msghdr __user
*msg
;
423 struct io_buffer
*kbuf
;
429 struct filename
*filename
;
431 unsigned long nofile
;
434 struct io_files_update
{
460 struct epoll_event event
;
464 struct file
*file_out
;
465 struct file
*file_in
;
472 struct io_provide_buf
{
486 const char __user
*filename
;
487 struct statx __user
*buffer
;
490 struct io_async_connect
{
491 struct sockaddr_storage address
;
494 struct io_async_msghdr
{
495 struct iovec fast_iov
[UIO_FASTIOV
];
497 struct sockaddr __user
*uaddr
;
499 struct sockaddr_storage addr
;
503 struct iovec fast_iov
[UIO_FASTIOV
];
507 struct wait_page_queue wpq
;
508 struct callback_head task_work
;
511 struct io_async_ctx
{
513 struct io_async_rw rw
;
514 struct io_async_msghdr msg
;
515 struct io_async_connect connect
;
516 struct io_timeout_data timeout
;
521 REQ_F_FIXED_FILE_BIT
= IOSQE_FIXED_FILE_BIT
,
522 REQ_F_IO_DRAIN_BIT
= IOSQE_IO_DRAIN_BIT
,
523 REQ_F_LINK_BIT
= IOSQE_IO_LINK_BIT
,
524 REQ_F_HARDLINK_BIT
= IOSQE_IO_HARDLINK_BIT
,
525 REQ_F_FORCE_ASYNC_BIT
= IOSQE_ASYNC_BIT
,
526 REQ_F_BUFFER_SELECT_BIT
= IOSQE_BUFFER_SELECT_BIT
,
534 REQ_F_LINK_TIMEOUT_BIT
,
537 REQ_F_TIMEOUT_NOSEQ_BIT
,
538 REQ_F_COMP_LOCKED_BIT
,
539 REQ_F_NEED_CLEANUP_BIT
,
542 REQ_F_BUFFER_SELECTED_BIT
,
543 REQ_F_NO_FILE_TABLE_BIT
,
544 REQ_F_QUEUE_TIMEOUT_BIT
,
545 REQ_F_WORK_INITIALIZED_BIT
,
546 REQ_F_TASK_PINNED_BIT
,
548 /* not a real bit, just to check we're not overflowing the space */
554 REQ_F_FIXED_FILE
= BIT(REQ_F_FIXED_FILE_BIT
),
555 /* drain existing IO first */
556 REQ_F_IO_DRAIN
= BIT(REQ_F_IO_DRAIN_BIT
),
558 REQ_F_LINK
= BIT(REQ_F_LINK_BIT
),
559 /* doesn't sever on completion < 0 */
560 REQ_F_HARDLINK
= BIT(REQ_F_HARDLINK_BIT
),
562 REQ_F_FORCE_ASYNC
= BIT(REQ_F_FORCE_ASYNC_BIT
),
563 /* IOSQE_BUFFER_SELECT */
564 REQ_F_BUFFER_SELECT
= BIT(REQ_F_BUFFER_SELECT_BIT
),
567 REQ_F_LINK_HEAD
= BIT(REQ_F_LINK_HEAD_BIT
),
568 /* already grabbed next link */
569 REQ_F_LINK_NEXT
= BIT(REQ_F_LINK_NEXT_BIT
),
570 /* fail rest of links */
571 REQ_F_FAIL_LINK
= BIT(REQ_F_FAIL_LINK_BIT
),
572 /* on inflight list */
573 REQ_F_INFLIGHT
= BIT(REQ_F_INFLIGHT_BIT
),
574 /* read/write uses file position */
575 REQ_F_CUR_POS
= BIT(REQ_F_CUR_POS_BIT
),
576 /* must not punt to workers */
577 REQ_F_NOWAIT
= BIT(REQ_F_NOWAIT_BIT
),
578 /* has linked timeout */
579 REQ_F_LINK_TIMEOUT
= BIT(REQ_F_LINK_TIMEOUT_BIT
),
580 /* timeout request */
581 REQ_F_TIMEOUT
= BIT(REQ_F_TIMEOUT_BIT
),
583 REQ_F_ISREG
= BIT(REQ_F_ISREG_BIT
),
584 /* no timeout sequence */
585 REQ_F_TIMEOUT_NOSEQ
= BIT(REQ_F_TIMEOUT_NOSEQ_BIT
),
586 /* completion under lock */
587 REQ_F_COMP_LOCKED
= BIT(REQ_F_COMP_LOCKED_BIT
),
589 REQ_F_NEED_CLEANUP
= BIT(REQ_F_NEED_CLEANUP_BIT
),
590 /* in overflow list */
591 REQ_F_OVERFLOW
= BIT(REQ_F_OVERFLOW_BIT
),
592 /* already went through poll handler */
593 REQ_F_POLLED
= BIT(REQ_F_POLLED_BIT
),
594 /* buffer already selected */
595 REQ_F_BUFFER_SELECTED
= BIT(REQ_F_BUFFER_SELECTED_BIT
),
596 /* doesn't need file table for this request */
597 REQ_F_NO_FILE_TABLE
= BIT(REQ_F_NO_FILE_TABLE_BIT
),
598 /* needs to queue linked timeout */
599 REQ_F_QUEUE_TIMEOUT
= BIT(REQ_F_QUEUE_TIMEOUT_BIT
),
600 /* io_wq_work is initialized */
601 REQ_F_WORK_INITIALIZED
= BIT(REQ_F_WORK_INITIALIZED_BIT
),
602 /* req->task is refcounted */
603 REQ_F_TASK_PINNED
= BIT(REQ_F_TASK_PINNED_BIT
),
607 struct io_poll_iocb poll
;
608 struct io_wq_work work
;
612 * NOTE! Each of the iocb union members has the file pointer
613 * as the first entry in their struct definition. So you can
614 * access the file pointer through any of the sub-structs,
615 * or directly as just 'ki_filp' in this struct.
621 struct io_poll_iocb poll
;
622 struct io_accept accept
;
624 struct io_cancel cancel
;
625 struct io_timeout timeout
;
626 struct io_connect connect
;
627 struct io_sr_msg sr_msg
;
629 struct io_close close
;
630 struct io_files_update files_update
;
631 struct io_fadvise fadvise
;
632 struct io_madvise madvise
;
633 struct io_epoll epoll
;
634 struct io_splice splice
;
635 struct io_provide_buf pbuf
;
636 struct io_statx statx
;
639 struct io_async_ctx
*io
;
642 /* polled IO has completed */
647 struct io_ring_ctx
*ctx
;
648 struct list_head list
;
651 struct task_struct
*task
;
657 struct list_head link_list
;
659 struct list_head inflight_entry
;
661 struct percpu_ref
*fixed_file_refs
;
665 * Only commands that never go async can use the below fields,
666 * obviously. Right now only IORING_OP_POLL_ADD uses them, and
667 * async armed poll handlers for regular commands. The latter
668 * restore the work, if needed.
671 struct callback_head task_work
;
672 struct hlist_node hash_node
;
673 struct async_poll
*apoll
;
675 struct io_wq_work work
;
679 #define IO_IOPOLL_BATCH 8
681 struct io_comp_state
{
683 struct list_head list
;
684 struct io_ring_ctx
*ctx
;
687 struct io_submit_state
{
688 struct blk_plug plug
;
691 * io_kiocb alloc cache
693 void *reqs
[IO_IOPOLL_BATCH
];
694 unsigned int free_reqs
;
697 * Batch completion logic
699 struct io_comp_state comp
;
702 * File reference cache
706 unsigned int has_refs
;
707 unsigned int used_refs
;
708 unsigned int ios_left
;
712 /* needs req->io allocated for deferral/async */
713 unsigned async_ctx
: 1;
714 /* needs current->mm setup, does mm access */
715 unsigned needs_mm
: 1;
716 /* needs req->file assigned */
717 unsigned needs_file
: 1;
718 /* don't fail if file grab fails */
719 unsigned needs_file_no_error
: 1;
720 /* hash wq insertion if file is a regular file */
721 unsigned hash_reg_file
: 1;
722 /* unbound wq insertion if file is a non-regular file */
723 unsigned unbound_nonreg_file
: 1;
724 /* opcode is not supported by this kernel */
725 unsigned not_supported
: 1;
726 /* needs file table */
727 unsigned file_table
: 1;
729 unsigned needs_fs
: 1;
730 /* set if opcode supports polled "wait" */
732 unsigned pollout
: 1;
733 /* op supports buffer selection */
734 unsigned buffer_select
: 1;
737 static const struct io_op_def io_op_defs
[] = {
738 [IORING_OP_NOP
] = {},
739 [IORING_OP_READV
] = {
743 .unbound_nonreg_file
= 1,
747 [IORING_OP_WRITEV
] = {
752 .unbound_nonreg_file
= 1,
755 [IORING_OP_FSYNC
] = {
758 [IORING_OP_READ_FIXED
] = {
760 .unbound_nonreg_file
= 1,
763 [IORING_OP_WRITE_FIXED
] = {
766 .unbound_nonreg_file
= 1,
769 [IORING_OP_POLL_ADD
] = {
771 .unbound_nonreg_file
= 1,
773 [IORING_OP_POLL_REMOVE
] = {},
774 [IORING_OP_SYNC_FILE_RANGE
] = {
777 [IORING_OP_SENDMSG
] = {
781 .unbound_nonreg_file
= 1,
785 [IORING_OP_RECVMSG
] = {
789 .unbound_nonreg_file
= 1,
794 [IORING_OP_TIMEOUT
] = {
798 [IORING_OP_TIMEOUT_REMOVE
] = {},
799 [IORING_OP_ACCEPT
] = {
802 .unbound_nonreg_file
= 1,
806 [IORING_OP_ASYNC_CANCEL
] = {},
807 [IORING_OP_LINK_TIMEOUT
] = {
811 [IORING_OP_CONNECT
] = {
815 .unbound_nonreg_file
= 1,
818 [IORING_OP_FALLOCATE
] = {
821 [IORING_OP_OPENAT
] = {
825 [IORING_OP_CLOSE
] = {
827 .needs_file_no_error
= 1,
830 [IORING_OP_FILES_UPDATE
] = {
834 [IORING_OP_STATX
] = {
842 .unbound_nonreg_file
= 1,
846 [IORING_OP_WRITE
] = {
849 .unbound_nonreg_file
= 1,
852 [IORING_OP_FADVISE
] = {
855 [IORING_OP_MADVISE
] = {
861 .unbound_nonreg_file
= 1,
867 .unbound_nonreg_file
= 1,
871 [IORING_OP_OPENAT2
] = {
875 [IORING_OP_EPOLL_CTL
] = {
876 .unbound_nonreg_file
= 1,
879 [IORING_OP_SPLICE
] = {
882 .unbound_nonreg_file
= 1,
884 [IORING_OP_PROVIDE_BUFFERS
] = {},
885 [IORING_OP_REMOVE_BUFFERS
] = {},
889 .unbound_nonreg_file
= 1,
893 enum io_mem_account
{
898 static void io_wq_submit_work(struct io_wq_work
**workptr
);
899 static void io_cqring_fill_event(struct io_kiocb
*req
, long res
);
900 static void io_put_req(struct io_kiocb
*req
);
901 static void __io_double_put_req(struct io_kiocb
*req
);
902 static struct io_kiocb
*io_prep_linked_timeout(struct io_kiocb
*req
);
903 static void io_queue_linked_timeout(struct io_kiocb
*req
);
904 static int __io_sqe_files_update(struct io_ring_ctx
*ctx
,
905 struct io_uring_files_update
*ip
,
907 static int io_grab_files(struct io_kiocb
*req
);
908 static void io_cleanup_req(struct io_kiocb
*req
);
909 static int io_file_get(struct io_submit_state
*state
, struct io_kiocb
*req
,
910 int fd
, struct file
**out_file
, bool fixed
);
911 static void __io_queue_sqe(struct io_kiocb
*req
,
912 const struct io_uring_sqe
*sqe
,
913 struct io_comp_state
*cs
);
915 static ssize_t
io_import_iovec(int rw
, struct io_kiocb
*req
,
916 struct iovec
**iovec
, struct iov_iter
*iter
,
918 static int io_setup_async_rw(struct io_kiocb
*req
, ssize_t io_size
,
919 struct iovec
*iovec
, struct iovec
*fast_iov
,
920 struct iov_iter
*iter
);
922 static struct kmem_cache
*req_cachep
;
924 static const struct file_operations io_uring_fops
;
926 struct sock
*io_uring_get_socket(struct file
*file
)
928 #if defined(CONFIG_UNIX)
929 if (file
->f_op
== &io_uring_fops
) {
930 struct io_ring_ctx
*ctx
= file
->private_data
;
932 return ctx
->ring_sock
->sk
;
937 EXPORT_SYMBOL(io_uring_get_socket
);
939 static void io_get_req_task(struct io_kiocb
*req
)
941 if (req
->flags
& REQ_F_TASK_PINNED
)
943 get_task_struct(req
->task
);
944 req
->flags
|= REQ_F_TASK_PINNED
;
947 /* not idempotent -- it doesn't clear REQ_F_TASK_PINNED */
948 static void __io_put_req_task(struct io_kiocb
*req
)
950 if (req
->flags
& REQ_F_TASK_PINNED
)
951 put_task_struct(req
->task
);
954 static void io_file_put_work(struct work_struct
*work
);
957 * Note: must call io_req_init_async() for the first time you
958 * touch any members of io_wq_work.
960 static inline void io_req_init_async(struct io_kiocb
*req
)
962 if (req
->flags
& REQ_F_WORK_INITIALIZED
)
965 memset(&req
->work
, 0, sizeof(req
->work
));
966 req
->flags
|= REQ_F_WORK_INITIALIZED
;
969 static inline bool io_async_submit(struct io_ring_ctx
*ctx
)
971 return ctx
->flags
& IORING_SETUP_SQPOLL
;
974 static void io_ring_ctx_ref_free(struct percpu_ref
*ref
)
976 struct io_ring_ctx
*ctx
= container_of(ref
, struct io_ring_ctx
, refs
);
978 complete(&ctx
->ref_comp
);
981 static struct io_ring_ctx
*io_ring_ctx_alloc(struct io_uring_params
*p
)
983 struct io_ring_ctx
*ctx
;
986 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
990 ctx
->fallback_req
= kmem_cache_alloc(req_cachep
, GFP_KERNEL
);
991 if (!ctx
->fallback_req
)
995 * Use 5 bits less than the max cq entries, that should give us around
996 * 32 entries per hash list if totally full and uniformly spread.
998 hash_bits
= ilog2(p
->cq_entries
);
1002 ctx
->cancel_hash_bits
= hash_bits
;
1003 ctx
->cancel_hash
= kmalloc((1U << hash_bits
) * sizeof(struct hlist_head
),
1005 if (!ctx
->cancel_hash
)
1007 __hash_init(ctx
->cancel_hash
, 1U << hash_bits
);
1009 if (percpu_ref_init(&ctx
->refs
, io_ring_ctx_ref_free
,
1010 PERCPU_REF_ALLOW_REINIT
, GFP_KERNEL
))
1013 ctx
->flags
= p
->flags
;
1014 init_waitqueue_head(&ctx
->sqo_wait
);
1015 init_waitqueue_head(&ctx
->cq_wait
);
1016 INIT_LIST_HEAD(&ctx
->cq_overflow_list
);
1017 init_completion(&ctx
->ref_comp
);
1018 init_completion(&ctx
->sq_thread_comp
);
1019 idr_init(&ctx
->io_buffer_idr
);
1020 idr_init(&ctx
->personality_idr
);
1021 mutex_init(&ctx
->uring_lock
);
1022 init_waitqueue_head(&ctx
->wait
);
1023 spin_lock_init(&ctx
->completion_lock
);
1024 INIT_LIST_HEAD(&ctx
->poll_list
);
1025 INIT_LIST_HEAD(&ctx
->defer_list
);
1026 INIT_LIST_HEAD(&ctx
->timeout_list
);
1027 init_waitqueue_head(&ctx
->inflight_wait
);
1028 spin_lock_init(&ctx
->inflight_lock
);
1029 INIT_LIST_HEAD(&ctx
->inflight_list
);
1030 INIT_DELAYED_WORK(&ctx
->file_put_work
, io_file_put_work
);
1031 init_llist_head(&ctx
->file_put_llist
);
1034 if (ctx
->fallback_req
)
1035 kmem_cache_free(req_cachep
, ctx
->fallback_req
);
1036 kfree(ctx
->cancel_hash
);
1041 static inline bool __req_need_defer(struct io_kiocb
*req
)
1043 struct io_ring_ctx
*ctx
= req
->ctx
;
1045 return req
->sequence
!= ctx
->cached_cq_tail
1046 + atomic_read(&ctx
->cached_cq_overflow
);
1049 static inline bool req_need_defer(struct io_kiocb
*req
)
1051 if (unlikely(req
->flags
& REQ_F_IO_DRAIN
))
1052 return __req_need_defer(req
);
1057 static void __io_commit_cqring(struct io_ring_ctx
*ctx
)
1059 struct io_rings
*rings
= ctx
->rings
;
1061 /* order cqe stores with ring update */
1062 smp_store_release(&rings
->cq
.tail
, ctx
->cached_cq_tail
);
1064 if (wq_has_sleeper(&ctx
->cq_wait
)) {
1065 wake_up_interruptible(&ctx
->cq_wait
);
1066 kill_fasync(&ctx
->cq_fasync
, SIGIO
, POLL_IN
);
1070 static inline void io_req_work_grab_env(struct io_kiocb
*req
,
1071 const struct io_op_def
*def
)
1073 if (!req
->work
.mm
&& def
->needs_mm
) {
1074 mmgrab(current
->mm
);
1075 req
->work
.mm
= current
->mm
;
1077 if (!req
->work
.creds
)
1078 req
->work
.creds
= get_current_cred();
1079 if (!req
->work
.fs
&& def
->needs_fs
) {
1080 spin_lock(¤t
->fs
->lock
);
1081 if (!current
->fs
->in_exec
) {
1082 req
->work
.fs
= current
->fs
;
1083 req
->work
.fs
->users
++;
1085 req
->work
.flags
|= IO_WQ_WORK_CANCEL
;
1087 spin_unlock(¤t
->fs
->lock
);
1091 static inline void io_req_work_drop_env(struct io_kiocb
*req
)
1093 if (!(req
->flags
& REQ_F_WORK_INITIALIZED
))
1097 mmdrop(req
->work
.mm
);
1098 req
->work
.mm
= NULL
;
1100 if (req
->work
.creds
) {
1101 put_cred(req
->work
.creds
);
1102 req
->work
.creds
= NULL
;
1105 struct fs_struct
*fs
= req
->work
.fs
;
1107 spin_lock(&req
->work
.fs
->lock
);
1110 spin_unlock(&req
->work
.fs
->lock
);
1116 static inline void io_prep_async_work(struct io_kiocb
*req
,
1117 struct io_kiocb
**link
)
1119 const struct io_op_def
*def
= &io_op_defs
[req
->opcode
];
1121 if (req
->flags
& REQ_F_ISREG
) {
1122 if (def
->hash_reg_file
)
1123 io_wq_hash_work(&req
->work
, file_inode(req
->file
));
1125 if (def
->unbound_nonreg_file
)
1126 req
->work
.flags
|= IO_WQ_WORK_UNBOUND
;
1129 io_req_init_async(req
);
1130 io_req_work_grab_env(req
, def
);
1132 *link
= io_prep_linked_timeout(req
);
1135 static inline void io_queue_async_work(struct io_kiocb
*req
)
1137 struct io_ring_ctx
*ctx
= req
->ctx
;
1138 struct io_kiocb
*link
;
1140 io_prep_async_work(req
, &link
);
1142 trace_io_uring_queue_async_work(ctx
, io_wq_is_hashed(&req
->work
), req
,
1143 &req
->work
, req
->flags
);
1144 io_wq_enqueue(ctx
->io_wq
, &req
->work
);
1147 io_queue_linked_timeout(link
);
1150 static void io_kill_timeout(struct io_kiocb
*req
)
1154 ret
= hrtimer_try_to_cancel(&req
->io
->timeout
.timer
);
1156 atomic_inc(&req
->ctx
->cq_timeouts
);
1157 list_del_init(&req
->list
);
1158 req
->flags
|= REQ_F_COMP_LOCKED
;
1159 io_cqring_fill_event(req
, 0);
1164 static void io_kill_timeouts(struct io_ring_ctx
*ctx
)
1166 struct io_kiocb
*req
, *tmp
;
1168 spin_lock_irq(&ctx
->completion_lock
);
1169 list_for_each_entry_safe(req
, tmp
, &ctx
->timeout_list
, list
)
1170 io_kill_timeout(req
);
1171 spin_unlock_irq(&ctx
->completion_lock
);
1174 static void __io_queue_deferred(struct io_ring_ctx
*ctx
)
1177 struct io_kiocb
*req
= list_first_entry(&ctx
->defer_list
,
1178 struct io_kiocb
, list
);
1180 if (req_need_defer(req
))
1182 list_del_init(&req
->list
);
1183 io_queue_async_work(req
);
1184 } while (!list_empty(&ctx
->defer_list
));
1187 static void io_flush_timeouts(struct io_ring_ctx
*ctx
)
1189 while (!list_empty(&ctx
->timeout_list
)) {
1190 struct io_kiocb
*req
= list_first_entry(&ctx
->timeout_list
,
1191 struct io_kiocb
, list
);
1193 if (req
->flags
& REQ_F_TIMEOUT_NOSEQ
)
1195 if (req
->timeout
.target_seq
!= ctx
->cached_cq_tail
1196 - atomic_read(&ctx
->cq_timeouts
))
1199 list_del_init(&req
->list
);
1200 io_kill_timeout(req
);
1204 static void io_commit_cqring(struct io_ring_ctx
*ctx
)
1206 io_flush_timeouts(ctx
);
1207 __io_commit_cqring(ctx
);
1209 if (unlikely(!list_empty(&ctx
->defer_list
)))
1210 __io_queue_deferred(ctx
);
1213 static struct io_uring_cqe
*io_get_cqring(struct io_ring_ctx
*ctx
)
1215 struct io_rings
*rings
= ctx
->rings
;
1218 tail
= ctx
->cached_cq_tail
;
1220 * writes to the cq entry need to come after reading head; the
1221 * control dependency is enough as we're using WRITE_ONCE to
1224 if (tail
- READ_ONCE(rings
->cq
.head
) == rings
->cq_ring_entries
)
1227 ctx
->cached_cq_tail
++;
1228 return &rings
->cqes
[tail
& ctx
->cq_mask
];
1231 static inline bool io_should_trigger_evfd(struct io_ring_ctx
*ctx
)
1235 if (READ_ONCE(ctx
->rings
->cq_flags
) & IORING_CQ_EVENTFD_DISABLED
)
1237 if (!ctx
->eventfd_async
)
1239 return io_wq_current_is_worker();
1242 static void io_cqring_ev_posted(struct io_ring_ctx
*ctx
)
1244 if (waitqueue_active(&ctx
->wait
))
1245 wake_up(&ctx
->wait
);
1246 if (waitqueue_active(&ctx
->sqo_wait
))
1247 wake_up(&ctx
->sqo_wait
);
1248 if (io_should_trigger_evfd(ctx
))
1249 eventfd_signal(ctx
->cq_ev_fd
, 1);
1252 /* Returns true if there are no backlogged entries after the flush */
1253 static bool io_cqring_overflow_flush(struct io_ring_ctx
*ctx
, bool force
)
1255 struct io_rings
*rings
= ctx
->rings
;
1256 struct io_uring_cqe
*cqe
;
1257 struct io_kiocb
*req
;
1258 unsigned long flags
;
1262 if (list_empty_careful(&ctx
->cq_overflow_list
))
1264 if ((ctx
->cached_cq_tail
- READ_ONCE(rings
->cq
.head
) ==
1265 rings
->cq_ring_entries
))
1269 spin_lock_irqsave(&ctx
->completion_lock
, flags
);
1271 /* if force is set, the ring is going away. always drop after that */
1273 ctx
->cq_overflow_flushed
= 1;
1276 while (!list_empty(&ctx
->cq_overflow_list
)) {
1277 cqe
= io_get_cqring(ctx
);
1281 req
= list_first_entry(&ctx
->cq_overflow_list
, struct io_kiocb
,
1283 list_move(&req
->list
, &list
);
1284 req
->flags
&= ~REQ_F_OVERFLOW
;
1286 WRITE_ONCE(cqe
->user_data
, req
->user_data
);
1287 WRITE_ONCE(cqe
->res
, req
->result
);
1288 WRITE_ONCE(cqe
->flags
, req
->cflags
);
1290 WRITE_ONCE(ctx
->rings
->cq_overflow
,
1291 atomic_inc_return(&ctx
->cached_cq_overflow
));
1295 io_commit_cqring(ctx
);
1297 clear_bit(0, &ctx
->sq_check_overflow
);
1298 clear_bit(0, &ctx
->cq_check_overflow
);
1300 spin_unlock_irqrestore(&ctx
->completion_lock
, flags
);
1301 io_cqring_ev_posted(ctx
);
1303 while (!list_empty(&list
)) {
1304 req
= list_first_entry(&list
, struct io_kiocb
, list
);
1305 list_del(&req
->list
);
1312 static void __io_cqring_fill_event(struct io_kiocb
*req
, long res
, long cflags
)
1314 struct io_ring_ctx
*ctx
= req
->ctx
;
1315 struct io_uring_cqe
*cqe
;
1317 trace_io_uring_complete(ctx
, req
->user_data
, res
);
1320 * If we can't get a cq entry, userspace overflowed the
1321 * submission (by quite a lot). Increment the overflow count in
1324 cqe
= io_get_cqring(ctx
);
1326 WRITE_ONCE(cqe
->user_data
, req
->user_data
);
1327 WRITE_ONCE(cqe
->res
, res
);
1328 WRITE_ONCE(cqe
->flags
, cflags
);
1329 } else if (ctx
->cq_overflow_flushed
) {
1330 WRITE_ONCE(ctx
->rings
->cq_overflow
,
1331 atomic_inc_return(&ctx
->cached_cq_overflow
));
1333 if (list_empty(&ctx
->cq_overflow_list
)) {
1334 set_bit(0, &ctx
->sq_check_overflow
);
1335 set_bit(0, &ctx
->cq_check_overflow
);
1337 req
->flags
|= REQ_F_OVERFLOW
;
1338 refcount_inc(&req
->refs
);
1340 req
->cflags
= cflags
;
1341 list_add_tail(&req
->list
, &ctx
->cq_overflow_list
);
1345 static void io_cqring_fill_event(struct io_kiocb
*req
, long res
)
1347 __io_cqring_fill_event(req
, res
, 0);
1350 static void io_cqring_add_event(struct io_kiocb
*req
, long res
, long cflags
)
1352 struct io_ring_ctx
*ctx
= req
->ctx
;
1353 unsigned long flags
;
1355 spin_lock_irqsave(&ctx
->completion_lock
, flags
);
1356 __io_cqring_fill_event(req
, res
, cflags
);
1357 io_commit_cqring(ctx
);
1358 spin_unlock_irqrestore(&ctx
->completion_lock
, flags
);
1360 io_cqring_ev_posted(ctx
);
1363 static void io_submit_flush_completions(struct io_comp_state
*cs
)
1365 struct io_ring_ctx
*ctx
= cs
->ctx
;
1367 spin_lock_irq(&ctx
->completion_lock
);
1368 while (!list_empty(&cs
->list
)) {
1369 struct io_kiocb
*req
;
1371 req
= list_first_entry(&cs
->list
, struct io_kiocb
, list
);
1372 list_del(&req
->list
);
1373 io_cqring_fill_event(req
, req
->result
);
1374 if (!(req
->flags
& REQ_F_LINK_HEAD
)) {
1375 req
->flags
|= REQ_F_COMP_LOCKED
;
1378 spin_unlock_irq(&ctx
->completion_lock
);
1380 spin_lock_irq(&ctx
->completion_lock
);
1383 io_commit_cqring(ctx
);
1384 spin_unlock_irq(&ctx
->completion_lock
);
1386 io_cqring_ev_posted(ctx
);
1390 static void __io_req_complete(struct io_kiocb
*req
, long res
, unsigned cflags
,
1391 struct io_comp_state
*cs
)
1394 io_cqring_add_event(req
, res
, cflags
);
1398 list_add_tail(&req
->list
, &cs
->list
);
1400 io_submit_flush_completions(cs
);
1404 static void io_req_complete(struct io_kiocb
*req
, long res
)
1406 __io_req_complete(req
, res
, 0, NULL
);
1409 static inline bool io_is_fallback_req(struct io_kiocb
*req
)
1411 return req
== (struct io_kiocb
*)
1412 ((unsigned long) req
->ctx
->fallback_req
& ~1UL);
1415 static struct io_kiocb
*io_get_fallback_req(struct io_ring_ctx
*ctx
)
1417 struct io_kiocb
*req
;
1419 req
= ctx
->fallback_req
;
1420 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx
->fallback_req
))
1426 static struct io_kiocb
*io_alloc_req(struct io_ring_ctx
*ctx
,
1427 struct io_submit_state
*state
)
1429 gfp_t gfp
= GFP_KERNEL
| __GFP_NOWARN
;
1430 struct io_kiocb
*req
;
1432 if (!state
->free_reqs
) {
1436 sz
= min_t(size_t, state
->ios_left
, ARRAY_SIZE(state
->reqs
));
1437 ret
= kmem_cache_alloc_bulk(req_cachep
, gfp
, sz
, state
->reqs
);
1440 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1441 * retry single alloc to be on the safe side.
1443 if (unlikely(ret
<= 0)) {
1444 state
->reqs
[0] = kmem_cache_alloc(req_cachep
, gfp
);
1445 if (!state
->reqs
[0])
1449 state
->free_reqs
= ret
- 1;
1450 req
= state
->reqs
[ret
- 1];
1453 req
= state
->reqs
[state
->free_reqs
];
1458 return io_get_fallback_req(ctx
);
1461 static inline void io_put_file(struct io_kiocb
*req
, struct file
*file
,
1465 percpu_ref_put(req
->fixed_file_refs
);
1470 static void __io_req_aux_free(struct io_kiocb
*req
)
1472 if (req
->flags
& REQ_F_NEED_CLEANUP
)
1473 io_cleanup_req(req
);
1477 io_put_file(req
, req
->file
, (req
->flags
& REQ_F_FIXED_FILE
));
1478 __io_put_req_task(req
);
1479 io_req_work_drop_env(req
);
1482 static void __io_free_req(struct io_kiocb
*req
)
1484 __io_req_aux_free(req
);
1486 if (req
->flags
& REQ_F_INFLIGHT
) {
1487 struct io_ring_ctx
*ctx
= req
->ctx
;
1488 unsigned long flags
;
1490 spin_lock_irqsave(&ctx
->inflight_lock
, flags
);
1491 list_del(&req
->inflight_entry
);
1492 if (waitqueue_active(&ctx
->inflight_wait
))
1493 wake_up(&ctx
->inflight_wait
);
1494 spin_unlock_irqrestore(&ctx
->inflight_lock
, flags
);
1497 percpu_ref_put(&req
->ctx
->refs
);
1498 if (likely(!io_is_fallback_req(req
)))
1499 kmem_cache_free(req_cachep
, req
);
1501 clear_bit_unlock(0, (unsigned long *) &req
->ctx
->fallback_req
);
1505 void *reqs
[IO_IOPOLL_BATCH
];
1510 static void io_free_req_many(struct io_ring_ctx
*ctx
, struct req_batch
*rb
)
1514 if (rb
->need_iter
) {
1515 int i
, inflight
= 0;
1516 unsigned long flags
;
1518 for (i
= 0; i
< rb
->to_free
; i
++) {
1519 struct io_kiocb
*req
= rb
->reqs
[i
];
1521 if (req
->flags
& REQ_F_INFLIGHT
)
1523 __io_req_aux_free(req
);
1528 spin_lock_irqsave(&ctx
->inflight_lock
, flags
);
1529 for (i
= 0; i
< rb
->to_free
; i
++) {
1530 struct io_kiocb
*req
= rb
->reqs
[i
];
1532 if (req
->flags
& REQ_F_INFLIGHT
) {
1533 list_del(&req
->inflight_entry
);
1538 spin_unlock_irqrestore(&ctx
->inflight_lock
, flags
);
1540 if (waitqueue_active(&ctx
->inflight_wait
))
1541 wake_up(&ctx
->inflight_wait
);
1544 kmem_cache_free_bulk(req_cachep
, rb
->to_free
, rb
->reqs
);
1545 percpu_ref_put_many(&ctx
->refs
, rb
->to_free
);
1546 rb
->to_free
= rb
->need_iter
= 0;
1549 static bool io_link_cancel_timeout(struct io_kiocb
*req
)
1551 struct io_ring_ctx
*ctx
= req
->ctx
;
1554 ret
= hrtimer_try_to_cancel(&req
->io
->timeout
.timer
);
1556 io_cqring_fill_event(req
, -ECANCELED
);
1557 io_commit_cqring(ctx
);
1558 req
->flags
&= ~REQ_F_LINK_HEAD
;
1566 static void io_req_link_next(struct io_kiocb
*req
, struct io_kiocb
**nxtptr
)
1568 struct io_ring_ctx
*ctx
= req
->ctx
;
1569 bool wake_ev
= false;
1571 /* Already got next link */
1572 if (req
->flags
& REQ_F_LINK_NEXT
)
1576 * The list should never be empty when we are called here. But could
1577 * potentially happen if the chain is messed up, check to be on the
1580 while (!list_empty(&req
->link_list
)) {
1581 struct io_kiocb
*nxt
= list_first_entry(&req
->link_list
,
1582 struct io_kiocb
, link_list
);
1584 if (unlikely((req
->flags
& REQ_F_LINK_TIMEOUT
) &&
1585 (nxt
->flags
& REQ_F_TIMEOUT
))) {
1586 list_del_init(&nxt
->link_list
);
1587 wake_ev
|= io_link_cancel_timeout(nxt
);
1588 req
->flags
&= ~REQ_F_LINK_TIMEOUT
;
1592 list_del_init(&req
->link_list
);
1593 if (!list_empty(&nxt
->link_list
))
1594 nxt
->flags
|= REQ_F_LINK_HEAD
;
1599 req
->flags
|= REQ_F_LINK_NEXT
;
1601 io_cqring_ev_posted(ctx
);
1605 * Called if REQ_F_LINK_HEAD is set, and we fail the head request
1607 static void io_fail_links(struct io_kiocb
*req
)
1609 struct io_ring_ctx
*ctx
= req
->ctx
;
1610 unsigned long flags
;
1612 spin_lock_irqsave(&ctx
->completion_lock
, flags
);
1614 while (!list_empty(&req
->link_list
)) {
1615 struct io_kiocb
*link
= list_first_entry(&req
->link_list
,
1616 struct io_kiocb
, link_list
);
1618 list_del_init(&link
->link_list
);
1619 trace_io_uring_fail_link(req
, link
);
1621 if ((req
->flags
& REQ_F_LINK_TIMEOUT
) &&
1622 link
->opcode
== IORING_OP_LINK_TIMEOUT
) {
1623 io_link_cancel_timeout(link
);
1625 io_cqring_fill_event(link
, -ECANCELED
);
1626 __io_double_put_req(link
);
1628 req
->flags
&= ~REQ_F_LINK_TIMEOUT
;
1631 io_commit_cqring(ctx
);
1632 spin_unlock_irqrestore(&ctx
->completion_lock
, flags
);
1633 io_cqring_ev_posted(ctx
);
1636 static void io_req_find_next(struct io_kiocb
*req
, struct io_kiocb
**nxt
)
1638 if (likely(!(req
->flags
& REQ_F_LINK_HEAD
)))
1642 * If LINK is set, we have dependent requests in this chain. If we
1643 * didn't fail this request, queue the first one up, moving any other
1644 * dependencies to the next request. In case of failure, fail the rest
1647 if (req
->flags
& REQ_F_FAIL_LINK
) {
1649 } else if ((req
->flags
& (REQ_F_LINK_TIMEOUT
| REQ_F_COMP_LOCKED
)) ==
1650 REQ_F_LINK_TIMEOUT
) {
1651 struct io_ring_ctx
*ctx
= req
->ctx
;
1652 unsigned long flags
;
1655 * If this is a timeout link, we could be racing with the
1656 * timeout timer. Grab the completion lock for this case to
1657 * protect against that.
1659 spin_lock_irqsave(&ctx
->completion_lock
, flags
);
1660 io_req_link_next(req
, nxt
);
1661 spin_unlock_irqrestore(&ctx
->completion_lock
, flags
);
1663 io_req_link_next(req
, nxt
);
1667 static void io_free_req(struct io_kiocb
*req
)
1669 struct io_kiocb
*nxt
= NULL
;
1671 io_req_find_next(req
, &nxt
);
1675 io_queue_async_work(nxt
);
1678 static void io_wq_assign_next(struct io_wq_work
**workptr
, struct io_kiocb
*nxt
)
1680 struct io_kiocb
*link
;
1681 const struct io_op_def
*def
= &io_op_defs
[nxt
->opcode
];
1683 if ((nxt
->flags
& REQ_F_ISREG
) && def
->hash_reg_file
)
1684 io_wq_hash_work(&nxt
->work
, file_inode(nxt
->file
));
1686 *workptr
= &nxt
->work
;
1687 link
= io_prep_linked_timeout(nxt
);
1689 nxt
->flags
|= REQ_F_QUEUE_TIMEOUT
;
1693 * Drop reference to request, return next in chain (if there is one) if this
1694 * was the last reference to this request.
1696 __attribute__((nonnull
))
1697 static void io_put_req_find_next(struct io_kiocb
*req
, struct io_kiocb
**nxtptr
)
1699 if (refcount_dec_and_test(&req
->refs
)) {
1700 io_req_find_next(req
, nxtptr
);
1705 static void io_put_req(struct io_kiocb
*req
)
1707 if (refcount_dec_and_test(&req
->refs
))
1711 static void io_steal_work(struct io_kiocb
*req
,
1712 struct io_wq_work
**workptr
)
1715 * It's in an io-wq worker, so there always should be at least
1716 * one reference, which will be dropped in io_put_work() just
1717 * after the current handler returns.
1719 * It also means, that if the counter dropped to 1, then there is
1720 * no asynchronous users left, so it's safe to steal the next work.
1722 if (refcount_read(&req
->refs
) == 1) {
1723 struct io_kiocb
*nxt
= NULL
;
1725 io_req_find_next(req
, &nxt
);
1727 io_wq_assign_next(workptr
, nxt
);
1732 * Must only be used if we don't need to care about links, usually from
1733 * within the completion handling itself.
1735 static void __io_double_put_req(struct io_kiocb
*req
)
1737 /* drop both submit and complete references */
1738 if (refcount_sub_and_test(2, &req
->refs
))
1742 static void io_double_put_req(struct io_kiocb
*req
)
1744 /* drop both submit and complete references */
1745 if (refcount_sub_and_test(2, &req
->refs
))
1749 static unsigned io_cqring_events(struct io_ring_ctx
*ctx
, bool noflush
)
1751 struct io_rings
*rings
= ctx
->rings
;
1753 if (test_bit(0, &ctx
->cq_check_overflow
)) {
1755 * noflush == true is from the waitqueue handler, just ensure
1756 * we wake up the task, and the next invocation will flush the
1757 * entries. We cannot safely to it from here.
1759 if (noflush
&& !list_empty(&ctx
->cq_overflow_list
))
1762 io_cqring_overflow_flush(ctx
, false);
1765 /* See comment at the top of this file */
1767 return ctx
->cached_cq_tail
- READ_ONCE(rings
->cq
.head
);
1770 static inline unsigned int io_sqring_entries(struct io_ring_ctx
*ctx
)
1772 struct io_rings
*rings
= ctx
->rings
;
1774 /* make sure SQ entry isn't read before tail */
1775 return smp_load_acquire(&rings
->sq
.tail
) - ctx
->cached_sq_head
;
1778 static inline bool io_req_multi_free(struct req_batch
*rb
, struct io_kiocb
*req
)
1780 if ((req
->flags
& REQ_F_LINK_HEAD
) || io_is_fallback_req(req
))
1783 if (req
->file
|| req
->io
)
1786 rb
->reqs
[rb
->to_free
++] = req
;
1787 if (unlikely(rb
->to_free
== ARRAY_SIZE(rb
->reqs
)))
1788 io_free_req_many(req
->ctx
, rb
);
1792 static int io_put_kbuf(struct io_kiocb
*req
)
1794 struct io_buffer
*kbuf
;
1797 kbuf
= (struct io_buffer
*) (unsigned long) req
->rw
.addr
;
1798 cflags
= kbuf
->bid
<< IORING_CQE_BUFFER_SHIFT
;
1799 cflags
|= IORING_CQE_F_BUFFER
;
1805 static void io_iopoll_queue(struct list_head
*again
)
1807 struct io_kiocb
*req
;
1810 req
= list_first_entry(again
, struct io_kiocb
, list
);
1811 list_del(&req
->list
);
1812 refcount_inc(&req
->refs
);
1813 io_queue_async_work(req
);
1814 } while (!list_empty(again
));
1818 * Find and free completed poll iocbs
1820 static void io_iopoll_complete(struct io_ring_ctx
*ctx
, unsigned int *nr_events
,
1821 struct list_head
*done
)
1823 struct req_batch rb
;
1824 struct io_kiocb
*req
;
1827 /* order with ->result store in io_complete_rw_iopoll() */
1830 rb
.to_free
= rb
.need_iter
= 0;
1831 while (!list_empty(done
)) {
1834 req
= list_first_entry(done
, struct io_kiocb
, list
);
1835 if (READ_ONCE(req
->result
) == -EAGAIN
) {
1836 req
->iopoll_completed
= 0;
1837 list_move_tail(&req
->list
, &again
);
1840 list_del(&req
->list
);
1842 if (req
->flags
& REQ_F_BUFFER_SELECTED
)
1843 cflags
= io_put_kbuf(req
);
1845 __io_cqring_fill_event(req
, req
->result
, cflags
);
1848 if (refcount_dec_and_test(&req
->refs
) &&
1849 !io_req_multi_free(&rb
, req
))
1853 io_commit_cqring(ctx
);
1854 if (ctx
->flags
& IORING_SETUP_SQPOLL
)
1855 io_cqring_ev_posted(ctx
);
1856 io_free_req_many(ctx
, &rb
);
1858 if (!list_empty(&again
))
1859 io_iopoll_queue(&again
);
1862 static int io_do_iopoll(struct io_ring_ctx
*ctx
, unsigned int *nr_events
,
1865 struct io_kiocb
*req
, *tmp
;
1871 * Only spin for completions if we don't have multiple devices hanging
1872 * off our complete list, and we're under the requested amount.
1874 spin
= !ctx
->poll_multi_file
&& *nr_events
< min
;
1877 list_for_each_entry_safe(req
, tmp
, &ctx
->poll_list
, list
) {
1878 struct kiocb
*kiocb
= &req
->rw
.kiocb
;
1881 * Move completed and retryable entries to our local lists.
1882 * If we find a request that requires polling, break out
1883 * and complete those lists first, if we have entries there.
1885 if (READ_ONCE(req
->iopoll_completed
)) {
1886 list_move_tail(&req
->list
, &done
);
1889 if (!list_empty(&done
))
1892 ret
= kiocb
->ki_filp
->f_op
->iopoll(kiocb
, spin
);
1901 if (!list_empty(&done
))
1902 io_iopoll_complete(ctx
, nr_events
, &done
);
1908 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
1909 * non-spinning poll check - we'll still enter the driver poll loop, but only
1910 * as a non-spinning completion check.
1912 static int io_iopoll_getevents(struct io_ring_ctx
*ctx
, unsigned int *nr_events
,
1915 while (!list_empty(&ctx
->poll_list
) && !need_resched()) {
1918 ret
= io_do_iopoll(ctx
, nr_events
, min
);
1921 if (!min
|| *nr_events
>= min
)
1929 * We can't just wait for polled events to come to us, we have to actively
1930 * find and complete them.
1932 static void io_iopoll_reap_events(struct io_ring_ctx
*ctx
)
1934 if (!(ctx
->flags
& IORING_SETUP_IOPOLL
))
1937 mutex_lock(&ctx
->uring_lock
);
1938 while (!list_empty(&ctx
->poll_list
)) {
1939 unsigned int nr_events
= 0;
1941 io_iopoll_getevents(ctx
, &nr_events
, 1);
1944 * Ensure we allow local-to-the-cpu processing to take place,
1945 * in this case we need to ensure that we reap all events.
1949 mutex_unlock(&ctx
->uring_lock
);
1952 static int io_iopoll_check(struct io_ring_ctx
*ctx
, unsigned *nr_events
,
1955 int iters
= 0, ret
= 0;
1958 * We disallow the app entering submit/complete with polling, but we
1959 * still need to lock the ring to prevent racing with polled issue
1960 * that got punted to a workqueue.
1962 mutex_lock(&ctx
->uring_lock
);
1967 * Don't enter poll loop if we already have events pending.
1968 * If we do, we can potentially be spinning for commands that
1969 * already triggered a CQE (eg in error).
1971 if (io_cqring_events(ctx
, false))
1975 * If a submit got punted to a workqueue, we can have the
1976 * application entering polling for a command before it gets
1977 * issued. That app will hold the uring_lock for the duration
1978 * of the poll right here, so we need to take a breather every
1979 * now and then to ensure that the issue has a chance to add
1980 * the poll to the issued list. Otherwise we can spin here
1981 * forever, while the workqueue is stuck trying to acquire the
1984 if (!(++iters
& 7)) {
1985 mutex_unlock(&ctx
->uring_lock
);
1986 mutex_lock(&ctx
->uring_lock
);
1989 if (*nr_events
< min
)
1990 tmin
= min
- *nr_events
;
1992 ret
= io_iopoll_getevents(ctx
, nr_events
, tmin
);
1996 } while (min
&& !*nr_events
&& !need_resched());
1998 mutex_unlock(&ctx
->uring_lock
);
2002 static void kiocb_end_write(struct io_kiocb
*req
)
2005 * Tell lockdep we inherited freeze protection from submission
2008 if (req
->flags
& REQ_F_ISREG
) {
2009 struct inode
*inode
= file_inode(req
->file
);
2011 __sb_writers_acquired(inode
->i_sb
, SB_FREEZE_WRITE
);
2013 file_end_write(req
->file
);
2016 static inline void req_set_fail_links(struct io_kiocb
*req
)
2018 if ((req
->flags
& (REQ_F_LINK
| REQ_F_HARDLINK
)) == REQ_F_LINK
)
2019 req
->flags
|= REQ_F_FAIL_LINK
;
2022 static void io_complete_rw_common(struct kiocb
*kiocb
, long res
)
2024 struct io_kiocb
*req
= container_of(kiocb
, struct io_kiocb
, rw
.kiocb
);
2027 if (kiocb
->ki_flags
& IOCB_WRITE
)
2028 kiocb_end_write(req
);
2030 if (res
!= req
->result
)
2031 req_set_fail_links(req
);
2032 if (req
->flags
& REQ_F_BUFFER_SELECTED
)
2033 cflags
= io_put_kbuf(req
);
2034 io_cqring_add_event(req
, res
, cflags
);
2037 static void io_sq_thread_drop_mm(struct io_ring_ctx
*ctx
)
2039 struct mm_struct
*mm
= current
->mm
;
2042 kthread_unuse_mm(mm
);
2047 static int __io_sq_thread_acquire_mm(struct io_ring_ctx
*ctx
)
2050 if (unlikely(!mmget_not_zero(ctx
->sqo_mm
)))
2052 kthread_use_mm(ctx
->sqo_mm
);
2058 static int io_sq_thread_acquire_mm(struct io_ring_ctx
*ctx
,
2059 struct io_kiocb
*req
)
2061 if (!io_op_defs
[req
->opcode
].needs_mm
)
2063 return __io_sq_thread_acquire_mm(ctx
);
2067 static bool io_resubmit_prep(struct io_kiocb
*req
, int error
)
2069 struct iovec inline_vecs
[UIO_FASTIOV
], *iovec
= inline_vecs
;
2070 ssize_t ret
= -ECANCELED
;
2071 struct iov_iter iter
;
2079 switch (req
->opcode
) {
2080 case IORING_OP_READV
:
2081 case IORING_OP_READ_FIXED
:
2082 case IORING_OP_READ
:
2085 case IORING_OP_WRITEV
:
2086 case IORING_OP_WRITE_FIXED
:
2087 case IORING_OP_WRITE
:
2091 printk_once(KERN_WARNING
"io_uring: bad opcode in resubmit %d\n",
2096 ret
= io_import_iovec(rw
, req
, &iovec
, &iter
, false);
2099 ret
= io_setup_async_rw(req
, ret
, iovec
, inline_vecs
, &iter
);
2104 req_set_fail_links(req
);
2105 io_req_complete(req
, ret
);
2109 static void io_rw_resubmit(struct callback_head
*cb
)
2111 struct io_kiocb
*req
= container_of(cb
, struct io_kiocb
, task_work
);
2112 struct io_ring_ctx
*ctx
= req
->ctx
;
2115 __set_current_state(TASK_RUNNING
);
2117 err
= io_sq_thread_acquire_mm(ctx
, req
);
2119 if (io_resubmit_prep(req
, err
)) {
2120 refcount_inc(&req
->refs
);
2121 io_queue_async_work(req
);
2126 static bool io_rw_reissue(struct io_kiocb
*req
, long res
)
2129 struct task_struct
*tsk
;
2132 if ((res
!= -EAGAIN
&& res
!= -EOPNOTSUPP
) || io_wq_current_is_worker())
2136 init_task_work(&req
->task_work
, io_rw_resubmit
);
2137 ret
= task_work_add(tsk
, &req
->task_work
, true);
2144 static void io_complete_rw(struct kiocb
*kiocb
, long res
, long res2
)
2146 struct io_kiocb
*req
= container_of(kiocb
, struct io_kiocb
, rw
.kiocb
);
2148 if (!io_rw_reissue(req
, res
)) {
2149 io_complete_rw_common(kiocb
, res
);
2154 static void io_complete_rw_iopoll(struct kiocb
*kiocb
, long res
, long res2
)
2156 struct io_kiocb
*req
= container_of(kiocb
, struct io_kiocb
, rw
.kiocb
);
2158 if (kiocb
->ki_flags
& IOCB_WRITE
)
2159 kiocb_end_write(req
);
2161 if (res
!= -EAGAIN
&& res
!= req
->result
)
2162 req_set_fail_links(req
);
2164 WRITE_ONCE(req
->result
, res
);
2165 /* order with io_poll_complete() checking ->result */
2166 if (res
!= -EAGAIN
) {
2168 WRITE_ONCE(req
->iopoll_completed
, 1);
2173 * After the iocb has been issued, it's safe to be found on the poll list.
2174 * Adding the kiocb to the list AFTER submission ensures that we don't
2175 * find it from a io_iopoll_getevents() thread before the issuer is done
2176 * accessing the kiocb cookie.
2178 static void io_iopoll_req_issued(struct io_kiocb
*req
)
2180 struct io_ring_ctx
*ctx
= req
->ctx
;
2183 * Track whether we have multiple files in our lists. This will impact
2184 * how we do polling eventually, not spinning if we're on potentially
2185 * different devices.
2187 if (list_empty(&ctx
->poll_list
)) {
2188 ctx
->poll_multi_file
= false;
2189 } else if (!ctx
->poll_multi_file
) {
2190 struct io_kiocb
*list_req
;
2192 list_req
= list_first_entry(&ctx
->poll_list
, struct io_kiocb
,
2194 if (list_req
->file
!= req
->file
)
2195 ctx
->poll_multi_file
= true;
2199 * For fast devices, IO may have already completed. If it has, add
2200 * it to the front so we find it first.
2202 if (READ_ONCE(req
->iopoll_completed
))
2203 list_add(&req
->list
, &ctx
->poll_list
);
2205 list_add_tail(&req
->list
, &ctx
->poll_list
);
2207 if ((ctx
->flags
& IORING_SETUP_SQPOLL
) &&
2208 wq_has_sleeper(&ctx
->sqo_wait
))
2209 wake_up(&ctx
->sqo_wait
);
2212 static void __io_state_file_put(struct io_submit_state
*state
)
2214 int diff
= state
->has_refs
- state
->used_refs
;
2217 fput_many(state
->file
, diff
);
2221 static inline void io_state_file_put(struct io_submit_state
*state
)
2224 __io_state_file_put(state
);
2228 * Get as many references to a file as we have IOs left in this submission,
2229 * assuming most submissions are for one file, or at least that each file
2230 * has more than one submission.
2232 static struct file
*__io_file_get(struct io_submit_state
*state
, int fd
)
2238 if (state
->fd
== fd
) {
2243 __io_state_file_put(state
);
2245 state
->file
= fget_many(fd
, state
->ios_left
);
2250 state
->has_refs
= state
->ios_left
;
2251 state
->used_refs
= 1;
2256 static bool io_bdev_nowait(struct block_device
*bdev
)
2259 return !bdev
|| queue_is_mq(bdev_get_queue(bdev
));
2266 * If we tracked the file through the SCM inflight mechanism, we could support
2267 * any file. For now, just ensure that anything potentially problematic is done
2270 static bool io_file_supports_async(struct file
*file
, int rw
)
2272 umode_t mode
= file_inode(file
)->i_mode
;
2274 if (S_ISBLK(mode
)) {
2275 if (io_bdev_nowait(file
->f_inode
->i_bdev
))
2279 if (S_ISCHR(mode
) || S_ISSOCK(mode
))
2281 if (S_ISREG(mode
)) {
2282 if (io_bdev_nowait(file
->f_inode
->i_sb
->s_bdev
) &&
2283 file
->f_op
!= &io_uring_fops
)
2288 /* any ->read/write should understand O_NONBLOCK */
2289 if (file
->f_flags
& O_NONBLOCK
)
2292 if (!(file
->f_mode
& FMODE_NOWAIT
))
2296 return file
->f_op
->read_iter
!= NULL
;
2298 return file
->f_op
->write_iter
!= NULL
;
2301 static int io_prep_rw(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
,
2302 bool force_nonblock
)
2304 struct io_ring_ctx
*ctx
= req
->ctx
;
2305 struct kiocb
*kiocb
= &req
->rw
.kiocb
;
2309 if (S_ISREG(file_inode(req
->file
)->i_mode
))
2310 req
->flags
|= REQ_F_ISREG
;
2312 kiocb
->ki_pos
= READ_ONCE(sqe
->off
);
2313 if (kiocb
->ki_pos
== -1 && !(req
->file
->f_mode
& FMODE_STREAM
)) {
2314 req
->flags
|= REQ_F_CUR_POS
;
2315 kiocb
->ki_pos
= req
->file
->f_pos
;
2317 kiocb
->ki_hint
= ki_hint_validate(file_write_hint(kiocb
->ki_filp
));
2318 kiocb
->ki_flags
= iocb_flags(kiocb
->ki_filp
);
2319 ret
= kiocb_set_rw_flags(kiocb
, READ_ONCE(sqe
->rw_flags
));
2323 ioprio
= READ_ONCE(sqe
->ioprio
);
2325 ret
= ioprio_check_cap(ioprio
);
2329 kiocb
->ki_ioprio
= ioprio
;
2331 kiocb
->ki_ioprio
= get_current_ioprio();
2333 /* don't allow async punt if RWF_NOWAIT was requested */
2334 if (kiocb
->ki_flags
& IOCB_NOWAIT
)
2335 req
->flags
|= REQ_F_NOWAIT
;
2337 if (kiocb
->ki_flags
& IOCB_DIRECT
)
2338 io_get_req_task(req
);
2341 kiocb
->ki_flags
|= IOCB_NOWAIT
;
2343 if (ctx
->flags
& IORING_SETUP_IOPOLL
) {
2344 if (!(kiocb
->ki_flags
& IOCB_DIRECT
) ||
2345 !kiocb
->ki_filp
->f_op
->iopoll
)
2348 kiocb
->ki_flags
|= IOCB_HIPRI
;
2349 kiocb
->ki_complete
= io_complete_rw_iopoll
;
2351 req
->iopoll_completed
= 0;
2353 if (kiocb
->ki_flags
& IOCB_HIPRI
)
2355 kiocb
->ki_complete
= io_complete_rw
;
2358 req
->rw
.addr
= READ_ONCE(sqe
->addr
);
2359 req
->rw
.len
= READ_ONCE(sqe
->len
);
2360 req
->buf_index
= READ_ONCE(sqe
->buf_index
);
2364 static inline void io_rw_done(struct kiocb
*kiocb
, ssize_t ret
)
2370 case -ERESTARTNOINTR
:
2371 case -ERESTARTNOHAND
:
2372 case -ERESTART_RESTARTBLOCK
:
2374 * We can't just restart the syscall, since previously
2375 * submitted sqes may already be in progress. Just fail this
2381 kiocb
->ki_complete(kiocb
, ret
, 0);
2385 static void kiocb_done(struct kiocb
*kiocb
, ssize_t ret
)
2387 struct io_kiocb
*req
= container_of(kiocb
, struct io_kiocb
, rw
.kiocb
);
2389 if (req
->flags
& REQ_F_CUR_POS
)
2390 req
->file
->f_pos
= kiocb
->ki_pos
;
2391 if (ret
>= 0 && kiocb
->ki_complete
== io_complete_rw
)
2392 io_complete_rw(kiocb
, ret
, 0);
2394 io_rw_done(kiocb
, ret
);
2397 static ssize_t
io_import_fixed(struct io_kiocb
*req
, int rw
,
2398 struct iov_iter
*iter
)
2400 struct io_ring_ctx
*ctx
= req
->ctx
;
2401 size_t len
= req
->rw
.len
;
2402 struct io_mapped_ubuf
*imu
;
2403 u16 index
, buf_index
;
2407 /* attempt to use fixed buffers without having provided iovecs */
2408 if (unlikely(!ctx
->user_bufs
))
2411 buf_index
= req
->buf_index
;
2412 if (unlikely(buf_index
>= ctx
->nr_user_bufs
))
2415 index
= array_index_nospec(buf_index
, ctx
->nr_user_bufs
);
2416 imu
= &ctx
->user_bufs
[index
];
2417 buf_addr
= req
->rw
.addr
;
2420 if (buf_addr
+ len
< buf_addr
)
2422 /* not inside the mapped region */
2423 if (buf_addr
< imu
->ubuf
|| buf_addr
+ len
> imu
->ubuf
+ imu
->len
)
2427 * May not be a start of buffer, set size appropriately
2428 * and advance us to the beginning.
2430 offset
= buf_addr
- imu
->ubuf
;
2431 iov_iter_bvec(iter
, rw
, imu
->bvec
, imu
->nr_bvecs
, offset
+ len
);
2435 * Don't use iov_iter_advance() here, as it's really slow for
2436 * using the latter parts of a big fixed buffer - it iterates
2437 * over each segment manually. We can cheat a bit here, because
2440 * 1) it's a BVEC iter, we set it up
2441 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2442 * first and last bvec
2444 * So just find our index, and adjust the iterator afterwards.
2445 * If the offset is within the first bvec (or the whole first
2446 * bvec, just use iov_iter_advance(). This makes it easier
2447 * since we can just skip the first segment, which may not
2448 * be PAGE_SIZE aligned.
2450 const struct bio_vec
*bvec
= imu
->bvec
;
2452 if (offset
<= bvec
->bv_len
) {
2453 iov_iter_advance(iter
, offset
);
2455 unsigned long seg_skip
;
2457 /* skip first vec */
2458 offset
-= bvec
->bv_len
;
2459 seg_skip
= 1 + (offset
>> PAGE_SHIFT
);
2461 iter
->bvec
= bvec
+ seg_skip
;
2462 iter
->nr_segs
-= seg_skip
;
2463 iter
->count
-= bvec
->bv_len
+ offset
;
2464 iter
->iov_offset
= offset
& ~PAGE_MASK
;
2471 static void io_ring_submit_unlock(struct io_ring_ctx
*ctx
, bool needs_lock
)
2474 mutex_unlock(&ctx
->uring_lock
);
2477 static void io_ring_submit_lock(struct io_ring_ctx
*ctx
, bool needs_lock
)
2480 * "Normal" inline submissions always hold the uring_lock, since we
2481 * grab it from the system call. Same is true for the SQPOLL offload.
2482 * The only exception is when we've detached the request and issue it
2483 * from an async worker thread, grab the lock for that case.
2486 mutex_lock(&ctx
->uring_lock
);
2489 static struct io_buffer
*io_buffer_select(struct io_kiocb
*req
, size_t *len
,
2490 int bgid
, struct io_buffer
*kbuf
,
2493 struct io_buffer
*head
;
2495 if (req
->flags
& REQ_F_BUFFER_SELECTED
)
2498 io_ring_submit_lock(req
->ctx
, needs_lock
);
2500 lockdep_assert_held(&req
->ctx
->uring_lock
);
2502 head
= idr_find(&req
->ctx
->io_buffer_idr
, bgid
);
2504 if (!list_empty(&head
->list
)) {
2505 kbuf
= list_last_entry(&head
->list
, struct io_buffer
,
2507 list_del(&kbuf
->list
);
2510 idr_remove(&req
->ctx
->io_buffer_idr
, bgid
);
2512 if (*len
> kbuf
->len
)
2515 kbuf
= ERR_PTR(-ENOBUFS
);
2518 io_ring_submit_unlock(req
->ctx
, needs_lock
);
2523 static void __user
*io_rw_buffer_select(struct io_kiocb
*req
, size_t *len
,
2526 struct io_buffer
*kbuf
;
2529 kbuf
= (struct io_buffer
*) (unsigned long) req
->rw
.addr
;
2530 bgid
= req
->buf_index
;
2531 kbuf
= io_buffer_select(req
, len
, bgid
, kbuf
, needs_lock
);
2534 req
->rw
.addr
= (u64
) (unsigned long) kbuf
;
2535 req
->flags
|= REQ_F_BUFFER_SELECTED
;
2536 return u64_to_user_ptr(kbuf
->addr
);
2539 #ifdef CONFIG_COMPAT
2540 static ssize_t
io_compat_import(struct io_kiocb
*req
, struct iovec
*iov
,
2543 struct compat_iovec __user
*uiov
;
2544 compat_ssize_t clen
;
2548 uiov
= u64_to_user_ptr(req
->rw
.addr
);
2549 if (!access_ok(uiov
, sizeof(*uiov
)))
2551 if (__get_user(clen
, &uiov
->iov_len
))
2557 buf
= io_rw_buffer_select(req
, &len
, needs_lock
);
2559 return PTR_ERR(buf
);
2560 iov
[0].iov_base
= buf
;
2561 iov
[0].iov_len
= (compat_size_t
) len
;
2566 static ssize_t
__io_iov_buffer_select(struct io_kiocb
*req
, struct iovec
*iov
,
2569 struct iovec __user
*uiov
= u64_to_user_ptr(req
->rw
.addr
);
2573 if (copy_from_user(iov
, uiov
, sizeof(*uiov
)))
2576 len
= iov
[0].iov_len
;
2579 buf
= io_rw_buffer_select(req
, &len
, needs_lock
);
2581 return PTR_ERR(buf
);
2582 iov
[0].iov_base
= buf
;
2583 iov
[0].iov_len
= len
;
2587 static ssize_t
io_iov_buffer_select(struct io_kiocb
*req
, struct iovec
*iov
,
2590 if (req
->flags
& REQ_F_BUFFER_SELECTED
) {
2591 struct io_buffer
*kbuf
;
2593 kbuf
= (struct io_buffer
*) (unsigned long) req
->rw
.addr
;
2594 iov
[0].iov_base
= u64_to_user_ptr(kbuf
->addr
);
2595 iov
[0].iov_len
= kbuf
->len
;
2600 else if (req
->rw
.len
> 1)
2603 #ifdef CONFIG_COMPAT
2604 if (req
->ctx
->compat
)
2605 return io_compat_import(req
, iov
, needs_lock
);
2608 return __io_iov_buffer_select(req
, iov
, needs_lock
);
2611 static ssize_t
io_import_iovec(int rw
, struct io_kiocb
*req
,
2612 struct iovec
**iovec
, struct iov_iter
*iter
,
2615 void __user
*buf
= u64_to_user_ptr(req
->rw
.addr
);
2616 size_t sqe_len
= req
->rw
.len
;
2620 opcode
= req
->opcode
;
2621 if (opcode
== IORING_OP_READ_FIXED
|| opcode
== IORING_OP_WRITE_FIXED
) {
2623 return io_import_fixed(req
, rw
, iter
);
2626 /* buffer index only valid with fixed read/write, or buffer select */
2627 if (req
->buf_index
&& !(req
->flags
& REQ_F_BUFFER_SELECT
))
2630 if (opcode
== IORING_OP_READ
|| opcode
== IORING_OP_WRITE
) {
2631 if (req
->flags
& REQ_F_BUFFER_SELECT
) {
2632 buf
= io_rw_buffer_select(req
, &sqe_len
, needs_lock
);
2635 return PTR_ERR(buf
);
2637 req
->rw
.len
= sqe_len
;
2640 ret
= import_single_range(rw
, buf
, sqe_len
, *iovec
, iter
);
2642 return ret
< 0 ? ret
: sqe_len
;
2646 struct io_async_rw
*iorw
= &req
->io
->rw
;
2649 iov_iter_init(iter
, rw
, *iovec
, iorw
->nr_segs
, iorw
->size
);
2650 if (iorw
->iov
== iorw
->fast_iov
)
2655 if (req
->flags
& REQ_F_BUFFER_SELECT
) {
2656 ret
= io_iov_buffer_select(req
, *iovec
, needs_lock
);
2658 ret
= (*iovec
)->iov_len
;
2659 iov_iter_init(iter
, rw
, *iovec
, 1, ret
);
2665 #ifdef CONFIG_COMPAT
2666 if (req
->ctx
->compat
)
2667 return compat_import_iovec(rw
, buf
, sqe_len
, UIO_FASTIOV
,
2671 return import_iovec(rw
, buf
, sqe_len
, UIO_FASTIOV
, iovec
, iter
);
2675 * For files that don't have ->read_iter() and ->write_iter(), handle them
2676 * by looping over ->read() or ->write() manually.
2678 static ssize_t
loop_rw_iter(int rw
, struct file
*file
, struct kiocb
*kiocb
,
2679 struct iov_iter
*iter
)
2684 * Don't support polled IO through this interface, and we can't
2685 * support non-blocking either. For the latter, this just causes
2686 * the kiocb to be handled from an async context.
2688 if (kiocb
->ki_flags
& IOCB_HIPRI
)
2690 if (kiocb
->ki_flags
& IOCB_NOWAIT
)
2693 while (iov_iter_count(iter
)) {
2697 if (!iov_iter_is_bvec(iter
)) {
2698 iovec
= iov_iter_iovec(iter
);
2700 /* fixed buffers import bvec */
2701 iovec
.iov_base
= kmap(iter
->bvec
->bv_page
)
2703 iovec
.iov_len
= min(iter
->count
,
2704 iter
->bvec
->bv_len
- iter
->iov_offset
);
2708 nr
= file
->f_op
->read(file
, iovec
.iov_base
,
2709 iovec
.iov_len
, &kiocb
->ki_pos
);
2711 nr
= file
->f_op
->write(file
, iovec
.iov_base
,
2712 iovec
.iov_len
, &kiocb
->ki_pos
);
2715 if (iov_iter_is_bvec(iter
))
2716 kunmap(iter
->bvec
->bv_page
);
2724 if (nr
!= iovec
.iov_len
)
2726 iov_iter_advance(iter
, nr
);
2732 static void io_req_map_rw(struct io_kiocb
*req
, ssize_t io_size
,
2733 struct iovec
*iovec
, struct iovec
*fast_iov
,
2734 struct iov_iter
*iter
)
2736 req
->io
->rw
.nr_segs
= iter
->nr_segs
;
2737 req
->io
->rw
.size
= io_size
;
2738 req
->io
->rw
.iov
= iovec
;
2739 if (!req
->io
->rw
.iov
) {
2740 req
->io
->rw
.iov
= req
->io
->rw
.fast_iov
;
2741 if (req
->io
->rw
.iov
!= fast_iov
)
2742 memcpy(req
->io
->rw
.iov
, fast_iov
,
2743 sizeof(struct iovec
) * iter
->nr_segs
);
2745 req
->flags
|= REQ_F_NEED_CLEANUP
;
2749 static inline int __io_alloc_async_ctx(struct io_kiocb
*req
)
2751 req
->io
= kmalloc(sizeof(*req
->io
), GFP_KERNEL
);
2752 return req
->io
== NULL
;
2755 static int io_alloc_async_ctx(struct io_kiocb
*req
)
2757 if (!io_op_defs
[req
->opcode
].async_ctx
)
2760 return __io_alloc_async_ctx(req
);
2763 static int io_setup_async_rw(struct io_kiocb
*req
, ssize_t io_size
,
2764 struct iovec
*iovec
, struct iovec
*fast_iov
,
2765 struct iov_iter
*iter
)
2767 if (!io_op_defs
[req
->opcode
].async_ctx
)
2770 if (__io_alloc_async_ctx(req
))
2773 io_req_map_rw(req
, io_size
, iovec
, fast_iov
, iter
);
2778 static int io_read_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
,
2779 bool force_nonblock
)
2781 struct io_async_ctx
*io
;
2782 struct iov_iter iter
;
2785 ret
= io_prep_rw(req
, sqe
, force_nonblock
);
2789 if (unlikely(!(req
->file
->f_mode
& FMODE_READ
)))
2792 /* either don't need iovec imported or already have it */
2793 if (!req
->io
|| req
->flags
& REQ_F_NEED_CLEANUP
)
2797 io
->rw
.iov
= io
->rw
.fast_iov
;
2799 ret
= io_import_iovec(READ
, req
, &io
->rw
.iov
, &iter
, !force_nonblock
);
2804 io_req_map_rw(req
, ret
, io
->rw
.iov
, io
->rw
.fast_iov
, &iter
);
2808 static void __io_async_buf_error(struct io_kiocb
*req
, int error
)
2810 struct io_ring_ctx
*ctx
= req
->ctx
;
2812 spin_lock_irq(&ctx
->completion_lock
);
2813 io_cqring_fill_event(req
, error
);
2814 io_commit_cqring(ctx
);
2815 spin_unlock_irq(&ctx
->completion_lock
);
2817 io_cqring_ev_posted(ctx
);
2818 req_set_fail_links(req
);
2819 io_double_put_req(req
);
2822 static void io_async_buf_cancel(struct callback_head
*cb
)
2824 struct io_async_rw
*rw
;
2825 struct io_kiocb
*req
;
2827 rw
= container_of(cb
, struct io_async_rw
, task_work
);
2828 req
= rw
->wpq
.wait
.private;
2829 __io_async_buf_error(req
, -ECANCELED
);
2832 static void io_async_buf_retry(struct callback_head
*cb
)
2834 struct io_async_rw
*rw
;
2835 struct io_ring_ctx
*ctx
;
2836 struct io_kiocb
*req
;
2838 rw
= container_of(cb
, struct io_async_rw
, task_work
);
2839 req
= rw
->wpq
.wait
.private;
2842 __set_current_state(TASK_RUNNING
);
2843 if (!__io_sq_thread_acquire_mm(ctx
)) {
2844 mutex_lock(&ctx
->uring_lock
);
2845 __io_queue_sqe(req
, NULL
, NULL
);
2846 mutex_unlock(&ctx
->uring_lock
);
2848 __io_async_buf_error(req
, -EFAULT
);
2852 static int io_async_buf_func(struct wait_queue_entry
*wait
, unsigned mode
,
2853 int sync
, void *arg
)
2855 struct wait_page_queue
*wpq
;
2856 struct io_kiocb
*req
= wait
->private;
2857 struct io_async_rw
*rw
= &req
->io
->rw
;
2858 struct wait_page_key
*key
= arg
;
2859 struct task_struct
*tsk
;
2862 wpq
= container_of(wait
, struct wait_page_queue
, wait
);
2864 ret
= wake_page_match(wpq
, key
);
2868 list_del_init(&wait
->entry
);
2870 init_task_work(&rw
->task_work
, io_async_buf_retry
);
2871 /* submit ref gets dropped, acquire a new one */
2872 refcount_inc(&req
->refs
);
2874 ret
= task_work_add(tsk
, &rw
->task_work
, true);
2875 if (unlikely(ret
)) {
2876 /* queue just for cancelation */
2877 init_task_work(&rw
->task_work
, io_async_buf_cancel
);
2878 tsk
= io_wq_get_task(req
->ctx
->io_wq
);
2879 task_work_add(tsk
, &rw
->task_work
, true);
2881 wake_up_process(tsk
);
2885 static bool io_rw_should_retry(struct io_kiocb
*req
)
2887 struct kiocb
*kiocb
= &req
->rw
.kiocb
;
2890 /* never retry for NOWAIT, we just complete with -EAGAIN */
2891 if (req
->flags
& REQ_F_NOWAIT
)
2894 /* already tried, or we're doing O_DIRECT */
2895 if (kiocb
->ki_flags
& (IOCB_DIRECT
| IOCB_WAITQ
))
2898 * just use poll if we can, and don't attempt if the fs doesn't
2899 * support callback based unlocks
2901 if (file_can_poll(req
->file
) || !(req
->file
->f_mode
& FMODE_BUF_RASYNC
))
2905 * If request type doesn't require req->io to defer in general,
2906 * we need to allocate it here
2908 if (!req
->io
&& __io_alloc_async_ctx(req
))
2911 ret
= kiocb_wait_page_queue_init(kiocb
, &req
->io
->rw
.wpq
,
2912 io_async_buf_func
, req
);
2914 io_get_req_task(req
);
2921 static int io_iter_do_read(struct io_kiocb
*req
, struct iov_iter
*iter
)
2923 if (req
->file
->f_op
->read_iter
)
2924 return call_read_iter(req
->file
, &req
->rw
.kiocb
, iter
);
2925 return loop_rw_iter(READ
, req
->file
, &req
->rw
.kiocb
, iter
);
2928 static int io_read(struct io_kiocb
*req
, bool force_nonblock
)
2930 struct iovec inline_vecs
[UIO_FASTIOV
], *iovec
= inline_vecs
;
2931 struct kiocb
*kiocb
= &req
->rw
.kiocb
;
2932 struct iov_iter iter
;
2934 ssize_t io_size
, ret
;
2936 ret
= io_import_iovec(READ
, req
, &iovec
, &iter
, !force_nonblock
);
2940 /* Ensure we clear previously set non-block flag */
2941 if (!force_nonblock
)
2942 kiocb
->ki_flags
&= ~IOCB_NOWAIT
;
2946 if (req
->flags
& REQ_F_LINK_HEAD
)
2947 req
->result
= io_size
;
2949 /* If the file doesn't support async, just async punt */
2950 if (force_nonblock
&& !io_file_supports_async(req
->file
, READ
))
2953 iov_count
= iov_iter_count(&iter
);
2954 ret
= rw_verify_area(READ
, req
->file
, &kiocb
->ki_pos
, iov_count
);
2956 unsigned long nr_segs
= iter
.nr_segs
;
2959 ret2
= io_iter_do_read(req
, &iter
);
2961 /* Catch -EAGAIN return for forced non-blocking submission */
2962 if (!force_nonblock
|| (ret2
!= -EAGAIN
&& ret2
!= -EIO
)) {
2963 kiocb_done(kiocb
, ret2
);
2965 iter
.count
= iov_count
;
2966 iter
.nr_segs
= nr_segs
;
2968 ret
= io_setup_async_rw(req
, io_size
, iovec
,
2969 inline_vecs
, &iter
);
2972 /* if we can retry, do so with the callbacks armed */
2973 if (io_rw_should_retry(req
)) {
2974 ret2
= io_iter_do_read(req
, &iter
);
2975 if (ret2
== -EIOCBQUEUED
) {
2977 } else if (ret2
!= -EAGAIN
) {
2978 kiocb_done(kiocb
, ret2
);
2982 kiocb
->ki_flags
&= ~IOCB_WAITQ
;
2987 if (!(req
->flags
& REQ_F_NEED_CLEANUP
))
2992 static int io_write_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
,
2993 bool force_nonblock
)
2995 struct io_async_ctx
*io
;
2996 struct iov_iter iter
;
2999 ret
= io_prep_rw(req
, sqe
, force_nonblock
);
3003 if (unlikely(!(req
->file
->f_mode
& FMODE_WRITE
)))
3006 req
->fsize
= rlimit(RLIMIT_FSIZE
);
3008 /* either don't need iovec imported or already have it */
3009 if (!req
->io
|| req
->flags
& REQ_F_NEED_CLEANUP
)
3013 io
->rw
.iov
= io
->rw
.fast_iov
;
3015 ret
= io_import_iovec(WRITE
, req
, &io
->rw
.iov
, &iter
, !force_nonblock
);
3020 io_req_map_rw(req
, ret
, io
->rw
.iov
, io
->rw
.fast_iov
, &iter
);
3024 static int io_write(struct io_kiocb
*req
, bool force_nonblock
)
3026 struct iovec inline_vecs
[UIO_FASTIOV
], *iovec
= inline_vecs
;
3027 struct kiocb
*kiocb
= &req
->rw
.kiocb
;
3028 struct iov_iter iter
;
3030 ssize_t ret
, io_size
;
3032 ret
= io_import_iovec(WRITE
, req
, &iovec
, &iter
, !force_nonblock
);
3036 /* Ensure we clear previously set non-block flag */
3037 if (!force_nonblock
)
3038 req
->rw
.kiocb
.ki_flags
&= ~IOCB_NOWAIT
;
3042 if (req
->flags
& REQ_F_LINK_HEAD
)
3043 req
->result
= io_size
;
3045 /* If the file doesn't support async, just async punt */
3046 if (force_nonblock
&& !io_file_supports_async(req
->file
, WRITE
))
3049 /* file path doesn't support NOWAIT for non-direct_IO */
3050 if (force_nonblock
&& !(kiocb
->ki_flags
& IOCB_DIRECT
) &&
3051 (req
->flags
& REQ_F_ISREG
))
3054 iov_count
= iov_iter_count(&iter
);
3055 ret
= rw_verify_area(WRITE
, req
->file
, &kiocb
->ki_pos
, iov_count
);
3057 unsigned long nr_segs
= iter
.nr_segs
;
3061 * Open-code file_start_write here to grab freeze protection,
3062 * which will be released by another thread in
3063 * io_complete_rw(). Fool lockdep by telling it the lock got
3064 * released so that it doesn't complain about the held lock when
3065 * we return to userspace.
3067 if (req
->flags
& REQ_F_ISREG
) {
3068 __sb_start_write(file_inode(req
->file
)->i_sb
,
3069 SB_FREEZE_WRITE
, true);
3070 __sb_writers_release(file_inode(req
->file
)->i_sb
,
3073 kiocb
->ki_flags
|= IOCB_WRITE
;
3075 if (!force_nonblock
)
3076 current
->signal
->rlim
[RLIMIT_FSIZE
].rlim_cur
= req
->fsize
;
3078 if (req
->file
->f_op
->write_iter
)
3079 ret2
= call_write_iter(req
->file
, kiocb
, &iter
);
3081 ret2
= loop_rw_iter(WRITE
, req
->file
, kiocb
, &iter
);
3083 if (!force_nonblock
)
3084 current
->signal
->rlim
[RLIMIT_FSIZE
].rlim_cur
= RLIM_INFINITY
;
3087 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3088 * retry them without IOCB_NOWAIT.
3090 if (ret2
== -EOPNOTSUPP
&& (kiocb
->ki_flags
& IOCB_NOWAIT
))
3092 if (!force_nonblock
|| ret2
!= -EAGAIN
) {
3093 kiocb_done(kiocb
, ret2
);
3095 iter
.count
= iov_count
;
3096 iter
.nr_segs
= nr_segs
;
3098 ret
= io_setup_async_rw(req
, io_size
, iovec
,
3099 inline_vecs
, &iter
);
3106 if (!(req
->flags
& REQ_F_NEED_CLEANUP
))
3111 static int __io_splice_prep(struct io_kiocb
*req
,
3112 const struct io_uring_sqe
*sqe
)
3114 struct io_splice
* sp
= &req
->splice
;
3115 unsigned int valid_flags
= SPLICE_F_FD_IN_FIXED
| SPLICE_F_ALL
;
3118 if (req
->flags
& REQ_F_NEED_CLEANUP
)
3120 if (unlikely(req
->ctx
->flags
& IORING_SETUP_IOPOLL
))
3124 sp
->len
= READ_ONCE(sqe
->len
);
3125 sp
->flags
= READ_ONCE(sqe
->splice_flags
);
3127 if (unlikely(sp
->flags
& ~valid_flags
))
3130 ret
= io_file_get(NULL
, req
, READ_ONCE(sqe
->splice_fd_in
), &sp
->file_in
,
3131 (sp
->flags
& SPLICE_F_FD_IN_FIXED
));
3134 req
->flags
|= REQ_F_NEED_CLEANUP
;
3136 if (!S_ISREG(file_inode(sp
->file_in
)->i_mode
)) {
3138 * Splice operation will be punted aync, and here need to
3139 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3141 io_req_init_async(req
);
3142 req
->work
.flags
|= IO_WQ_WORK_UNBOUND
;
3148 static int io_tee_prep(struct io_kiocb
*req
,
3149 const struct io_uring_sqe
*sqe
)
3151 if (READ_ONCE(sqe
->splice_off_in
) || READ_ONCE(sqe
->off
))
3153 return __io_splice_prep(req
, sqe
);
3156 static int io_tee(struct io_kiocb
*req
, bool force_nonblock
)
3158 struct io_splice
*sp
= &req
->splice
;
3159 struct file
*in
= sp
->file_in
;
3160 struct file
*out
= sp
->file_out
;
3161 unsigned int flags
= sp
->flags
& ~SPLICE_F_FD_IN_FIXED
;
3167 ret
= do_tee(in
, out
, sp
->len
, flags
);
3169 io_put_file(req
, in
, (sp
->flags
& SPLICE_F_FD_IN_FIXED
));
3170 req
->flags
&= ~REQ_F_NEED_CLEANUP
;
3173 req_set_fail_links(req
);
3174 io_req_complete(req
, ret
);
3178 static int io_splice_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
3180 struct io_splice
* sp
= &req
->splice
;
3182 sp
->off_in
= READ_ONCE(sqe
->splice_off_in
);
3183 sp
->off_out
= READ_ONCE(sqe
->off
);
3184 return __io_splice_prep(req
, sqe
);
3187 static int io_splice(struct io_kiocb
*req
, bool force_nonblock
)
3189 struct io_splice
*sp
= &req
->splice
;
3190 struct file
*in
= sp
->file_in
;
3191 struct file
*out
= sp
->file_out
;
3192 unsigned int flags
= sp
->flags
& ~SPLICE_F_FD_IN_FIXED
;
3193 loff_t
*poff_in
, *poff_out
;
3199 poff_in
= (sp
->off_in
== -1) ? NULL
: &sp
->off_in
;
3200 poff_out
= (sp
->off_out
== -1) ? NULL
: &sp
->off_out
;
3203 ret
= do_splice(in
, poff_in
, out
, poff_out
, sp
->len
, flags
);
3205 io_put_file(req
, in
, (sp
->flags
& SPLICE_F_FD_IN_FIXED
));
3206 req
->flags
&= ~REQ_F_NEED_CLEANUP
;
3209 req_set_fail_links(req
);
3210 io_req_complete(req
, ret
);
3215 * IORING_OP_NOP just posts a completion event, nothing else.
3217 static int io_nop(struct io_kiocb
*req
, struct io_comp_state
*cs
)
3219 struct io_ring_ctx
*ctx
= req
->ctx
;
3221 if (unlikely(ctx
->flags
& IORING_SETUP_IOPOLL
))
3224 __io_req_complete(req
, 0, 0, cs
);
3228 static int io_prep_fsync(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
3230 struct io_ring_ctx
*ctx
= req
->ctx
;
3235 if (unlikely(ctx
->flags
& IORING_SETUP_IOPOLL
))
3237 if (unlikely(sqe
->addr
|| sqe
->ioprio
|| sqe
->buf_index
))
3240 req
->sync
.flags
= READ_ONCE(sqe
->fsync_flags
);
3241 if (unlikely(req
->sync
.flags
& ~IORING_FSYNC_DATASYNC
))
3244 req
->sync
.off
= READ_ONCE(sqe
->off
);
3245 req
->sync
.len
= READ_ONCE(sqe
->len
);
3249 static int io_fsync(struct io_kiocb
*req
, bool force_nonblock
)
3251 loff_t end
= req
->sync
.off
+ req
->sync
.len
;
3254 /* fsync always requires a blocking context */
3258 ret
= vfs_fsync_range(req
->file
, req
->sync
.off
,
3259 end
> 0 ? end
: LLONG_MAX
,
3260 req
->sync
.flags
& IORING_FSYNC_DATASYNC
);
3262 req_set_fail_links(req
);
3263 io_req_complete(req
, ret
);
3267 static int io_fallocate_prep(struct io_kiocb
*req
,
3268 const struct io_uring_sqe
*sqe
)
3270 if (sqe
->ioprio
|| sqe
->buf_index
|| sqe
->rw_flags
)
3272 if (unlikely(req
->ctx
->flags
& IORING_SETUP_IOPOLL
))
3275 req
->sync
.off
= READ_ONCE(sqe
->off
);
3276 req
->sync
.len
= READ_ONCE(sqe
->addr
);
3277 req
->sync
.mode
= READ_ONCE(sqe
->len
);
3278 req
->fsize
= rlimit(RLIMIT_FSIZE
);
3282 static int io_fallocate(struct io_kiocb
*req
, bool force_nonblock
)
3286 /* fallocate always requiring blocking context */
3290 current
->signal
->rlim
[RLIMIT_FSIZE
].rlim_cur
= req
->fsize
;
3291 ret
= vfs_fallocate(req
->file
, req
->sync
.mode
, req
->sync
.off
,
3293 current
->signal
->rlim
[RLIMIT_FSIZE
].rlim_cur
= RLIM_INFINITY
;
3295 req_set_fail_links(req
);
3296 io_req_complete(req
, ret
);
3300 static int __io_openat_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
3302 const char __user
*fname
;
3305 if (unlikely(req
->ctx
->flags
& (IORING_SETUP_IOPOLL
|IORING_SETUP_SQPOLL
)))
3307 if (unlikely(sqe
->ioprio
|| sqe
->buf_index
))
3309 if (unlikely(req
->flags
& REQ_F_FIXED_FILE
))
3312 /* open.how should be already initialised */
3313 if (!(req
->open
.how
.flags
& O_PATH
) && force_o_largefile())
3314 req
->open
.how
.flags
|= O_LARGEFILE
;
3316 req
->open
.dfd
= READ_ONCE(sqe
->fd
);
3317 fname
= u64_to_user_ptr(READ_ONCE(sqe
->addr
));
3318 req
->open
.filename
= getname(fname
);
3319 if (IS_ERR(req
->open
.filename
)) {
3320 ret
= PTR_ERR(req
->open
.filename
);
3321 req
->open
.filename
= NULL
;
3324 req
->open
.nofile
= rlimit(RLIMIT_NOFILE
);
3325 req
->flags
|= REQ_F_NEED_CLEANUP
;
3329 static int io_openat_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
3333 if (req
->flags
& REQ_F_NEED_CLEANUP
)
3335 mode
= READ_ONCE(sqe
->len
);
3336 flags
= READ_ONCE(sqe
->open_flags
);
3337 req
->open
.how
= build_open_how(flags
, mode
);
3338 return __io_openat_prep(req
, sqe
);
3341 static int io_openat2_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
3343 struct open_how __user
*how
;
3347 if (req
->flags
& REQ_F_NEED_CLEANUP
)
3349 how
= u64_to_user_ptr(READ_ONCE(sqe
->addr2
));
3350 len
= READ_ONCE(sqe
->len
);
3351 if (len
< OPEN_HOW_SIZE_VER0
)
3354 ret
= copy_struct_from_user(&req
->open
.how
, sizeof(req
->open
.how
), how
,
3359 return __io_openat_prep(req
, sqe
);
3362 static int io_openat2(struct io_kiocb
*req
, bool force_nonblock
)
3364 struct open_flags op
;
3371 ret
= build_open_flags(&req
->open
.how
, &op
);
3375 ret
= __get_unused_fd_flags(req
->open
.how
.flags
, req
->open
.nofile
);
3379 file
= do_filp_open(req
->open
.dfd
, req
->open
.filename
, &op
);
3382 ret
= PTR_ERR(file
);
3384 fsnotify_open(file
);
3385 fd_install(ret
, file
);
3388 putname(req
->open
.filename
);
3389 req
->flags
&= ~REQ_F_NEED_CLEANUP
;
3391 req_set_fail_links(req
);
3392 io_req_complete(req
, ret
);
3396 static int io_openat(struct io_kiocb
*req
, bool force_nonblock
)
3398 return io_openat2(req
, force_nonblock
);
3401 static int io_remove_buffers_prep(struct io_kiocb
*req
,
3402 const struct io_uring_sqe
*sqe
)
3404 struct io_provide_buf
*p
= &req
->pbuf
;
3407 if (sqe
->ioprio
|| sqe
->rw_flags
|| sqe
->addr
|| sqe
->len
|| sqe
->off
)
3410 tmp
= READ_ONCE(sqe
->fd
);
3411 if (!tmp
|| tmp
> USHRT_MAX
)
3414 memset(p
, 0, sizeof(*p
));
3416 p
->bgid
= READ_ONCE(sqe
->buf_group
);
3420 static int __io_remove_buffers(struct io_ring_ctx
*ctx
, struct io_buffer
*buf
,
3421 int bgid
, unsigned nbufs
)
3425 /* shouldn't happen */
3429 /* the head kbuf is the list itself */
3430 while (!list_empty(&buf
->list
)) {
3431 struct io_buffer
*nxt
;
3433 nxt
= list_first_entry(&buf
->list
, struct io_buffer
, list
);
3434 list_del(&nxt
->list
);
3441 idr_remove(&ctx
->io_buffer_idr
, bgid
);
3446 static int io_remove_buffers(struct io_kiocb
*req
, bool force_nonblock
,
3447 struct io_comp_state
*cs
)
3449 struct io_provide_buf
*p
= &req
->pbuf
;
3450 struct io_ring_ctx
*ctx
= req
->ctx
;
3451 struct io_buffer
*head
;
3454 io_ring_submit_lock(ctx
, !force_nonblock
);
3456 lockdep_assert_held(&ctx
->uring_lock
);
3459 head
= idr_find(&ctx
->io_buffer_idr
, p
->bgid
);
3461 ret
= __io_remove_buffers(ctx
, head
, p
->bgid
, p
->nbufs
);
3463 io_ring_submit_lock(ctx
, !force_nonblock
);
3465 req_set_fail_links(req
);
3466 __io_req_complete(req
, ret
, 0, cs
);
3470 static int io_provide_buffers_prep(struct io_kiocb
*req
,
3471 const struct io_uring_sqe
*sqe
)
3473 struct io_provide_buf
*p
= &req
->pbuf
;
3476 if (sqe
->ioprio
|| sqe
->rw_flags
)
3479 tmp
= READ_ONCE(sqe
->fd
);
3480 if (!tmp
|| tmp
> USHRT_MAX
)
3483 p
->addr
= READ_ONCE(sqe
->addr
);
3484 p
->len
= READ_ONCE(sqe
->len
);
3486 if (!access_ok(u64_to_user_ptr(p
->addr
), (p
->len
* p
->nbufs
)))
3489 p
->bgid
= READ_ONCE(sqe
->buf_group
);
3490 tmp
= READ_ONCE(sqe
->off
);
3491 if (tmp
> USHRT_MAX
)
3497 static int io_add_buffers(struct io_provide_buf
*pbuf
, struct io_buffer
**head
)
3499 struct io_buffer
*buf
;
3500 u64 addr
= pbuf
->addr
;
3501 int i
, bid
= pbuf
->bid
;
3503 for (i
= 0; i
< pbuf
->nbufs
; i
++) {
3504 buf
= kmalloc(sizeof(*buf
), GFP_KERNEL
);
3509 buf
->len
= pbuf
->len
;
3514 INIT_LIST_HEAD(&buf
->list
);
3517 list_add_tail(&buf
->list
, &(*head
)->list
);
3521 return i
? i
: -ENOMEM
;
3524 static int io_provide_buffers(struct io_kiocb
*req
, bool force_nonblock
,
3525 struct io_comp_state
*cs
)
3527 struct io_provide_buf
*p
= &req
->pbuf
;
3528 struct io_ring_ctx
*ctx
= req
->ctx
;
3529 struct io_buffer
*head
, *list
;
3532 io_ring_submit_lock(ctx
, !force_nonblock
);
3534 lockdep_assert_held(&ctx
->uring_lock
);
3536 list
= head
= idr_find(&ctx
->io_buffer_idr
, p
->bgid
);
3538 ret
= io_add_buffers(p
, &head
);
3543 ret
= idr_alloc(&ctx
->io_buffer_idr
, head
, p
->bgid
, p
->bgid
+ 1,
3546 __io_remove_buffers(ctx
, head
, p
->bgid
, -1U);
3551 io_ring_submit_unlock(ctx
, !force_nonblock
);
3553 req_set_fail_links(req
);
3554 __io_req_complete(req
, ret
, 0, cs
);
3558 static int io_epoll_ctl_prep(struct io_kiocb
*req
,
3559 const struct io_uring_sqe
*sqe
)
3561 #if defined(CONFIG_EPOLL)
3562 if (sqe
->ioprio
|| sqe
->buf_index
)
3564 if (unlikely(req
->ctx
->flags
& IORING_SETUP_IOPOLL
))
3567 req
->epoll
.epfd
= READ_ONCE(sqe
->fd
);
3568 req
->epoll
.op
= READ_ONCE(sqe
->len
);
3569 req
->epoll
.fd
= READ_ONCE(sqe
->off
);
3571 if (ep_op_has_event(req
->epoll
.op
)) {
3572 struct epoll_event __user
*ev
;
3574 ev
= u64_to_user_ptr(READ_ONCE(sqe
->addr
));
3575 if (copy_from_user(&req
->epoll
.event
, ev
, sizeof(*ev
)))
3585 static int io_epoll_ctl(struct io_kiocb
*req
, bool force_nonblock
,
3586 struct io_comp_state
*cs
)
3588 #if defined(CONFIG_EPOLL)
3589 struct io_epoll
*ie
= &req
->epoll
;
3592 ret
= do_epoll_ctl(ie
->epfd
, ie
->op
, ie
->fd
, &ie
->event
, force_nonblock
);
3593 if (force_nonblock
&& ret
== -EAGAIN
)
3597 req_set_fail_links(req
);
3598 __io_req_complete(req
, ret
, 0, cs
);
3605 static int io_madvise_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
3607 #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3608 if (sqe
->ioprio
|| sqe
->buf_index
|| sqe
->off
)
3610 if (unlikely(req
->ctx
->flags
& IORING_SETUP_IOPOLL
))
3613 req
->madvise
.addr
= READ_ONCE(sqe
->addr
);
3614 req
->madvise
.len
= READ_ONCE(sqe
->len
);
3615 req
->madvise
.advice
= READ_ONCE(sqe
->fadvise_advice
);
3622 static int io_madvise(struct io_kiocb
*req
, bool force_nonblock
)
3624 #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3625 struct io_madvise
*ma
= &req
->madvise
;
3631 ret
= do_madvise(ma
->addr
, ma
->len
, ma
->advice
);
3633 req_set_fail_links(req
);
3634 io_req_complete(req
, ret
);
3641 static int io_fadvise_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
3643 if (sqe
->ioprio
|| sqe
->buf_index
|| sqe
->addr
)
3645 if (unlikely(req
->ctx
->flags
& IORING_SETUP_IOPOLL
))
3648 req
->fadvise
.offset
= READ_ONCE(sqe
->off
);
3649 req
->fadvise
.len
= READ_ONCE(sqe
->len
);
3650 req
->fadvise
.advice
= READ_ONCE(sqe
->fadvise_advice
);
3654 static int io_fadvise(struct io_kiocb
*req
, bool force_nonblock
)
3656 struct io_fadvise
*fa
= &req
->fadvise
;
3659 if (force_nonblock
) {
3660 switch (fa
->advice
) {
3661 case POSIX_FADV_NORMAL
:
3662 case POSIX_FADV_RANDOM
:
3663 case POSIX_FADV_SEQUENTIAL
:
3670 ret
= vfs_fadvise(req
->file
, fa
->offset
, fa
->len
, fa
->advice
);
3672 req_set_fail_links(req
);
3673 io_req_complete(req
, ret
);
3677 static int io_statx_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
3679 if (unlikely(req
->ctx
->flags
& IORING_SETUP_IOPOLL
))
3681 if (sqe
->ioprio
|| sqe
->buf_index
)
3683 if (req
->flags
& REQ_F_FIXED_FILE
)
3686 req
->statx
.dfd
= READ_ONCE(sqe
->fd
);
3687 req
->statx
.mask
= READ_ONCE(sqe
->len
);
3688 req
->statx
.filename
= u64_to_user_ptr(READ_ONCE(sqe
->addr
));
3689 req
->statx
.buffer
= u64_to_user_ptr(READ_ONCE(sqe
->addr2
));
3690 req
->statx
.flags
= READ_ONCE(sqe
->statx_flags
);
3695 static int io_statx(struct io_kiocb
*req
, bool force_nonblock
)
3697 struct io_statx
*ctx
= &req
->statx
;
3700 if (force_nonblock
) {
3701 /* only need file table for an actual valid fd */
3702 if (ctx
->dfd
== -1 || ctx
->dfd
== AT_FDCWD
)
3703 req
->flags
|= REQ_F_NO_FILE_TABLE
;
3707 ret
= do_statx(ctx
->dfd
, ctx
->filename
, ctx
->flags
, ctx
->mask
,
3711 req_set_fail_links(req
);
3712 io_req_complete(req
, ret
);
3716 static int io_close_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
3719 * If we queue this for async, it must not be cancellable. That would
3720 * leave the 'file' in an undeterminate state, and here need to modify
3721 * io_wq_work.flags, so initialize io_wq_work firstly.
3723 io_req_init_async(req
);
3724 req
->work
.flags
|= IO_WQ_WORK_NO_CANCEL
;
3726 if (unlikely(req
->ctx
->flags
& (IORING_SETUP_IOPOLL
|IORING_SETUP_SQPOLL
)))
3728 if (sqe
->ioprio
|| sqe
->off
|| sqe
->addr
|| sqe
->len
||
3729 sqe
->rw_flags
|| sqe
->buf_index
)
3731 if (req
->flags
& REQ_F_FIXED_FILE
)
3734 req
->close
.fd
= READ_ONCE(sqe
->fd
);
3735 if ((req
->file
&& req
->file
->f_op
== &io_uring_fops
) ||
3736 req
->close
.fd
== req
->ctx
->ring_fd
)
3739 req
->close
.put_file
= NULL
;
3743 static int io_close(struct io_kiocb
*req
, bool force_nonblock
,
3744 struct io_comp_state
*cs
)
3746 struct io_close
*close
= &req
->close
;
3749 /* might be already done during nonblock submission */
3750 if (!close
->put_file
) {
3751 ret
= __close_fd_get_file(close
->fd
, &close
->put_file
);
3753 return (ret
== -ENOENT
) ? -EBADF
: ret
;
3756 /* if the file has a flush method, be safe and punt to async */
3757 if (close
->put_file
->f_op
->flush
&& force_nonblock
) {
3758 /* was never set, but play safe */
3759 req
->flags
&= ~REQ_F_NOWAIT
;
3760 /* avoid grabbing files - we don't need the files */
3761 req
->flags
|= REQ_F_NO_FILE_TABLE
;
3765 /* No ->flush() or already async, safely close from here */
3766 ret
= filp_close(close
->put_file
, req
->work
.files
);
3768 req_set_fail_links(req
);
3769 fput(close
->put_file
);
3770 close
->put_file
= NULL
;
3771 __io_req_complete(req
, ret
, 0, cs
);
3775 static int io_prep_sfr(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
3777 struct io_ring_ctx
*ctx
= req
->ctx
;
3782 if (unlikely(ctx
->flags
& IORING_SETUP_IOPOLL
))
3784 if (unlikely(sqe
->addr
|| sqe
->ioprio
|| sqe
->buf_index
))
3787 req
->sync
.off
= READ_ONCE(sqe
->off
);
3788 req
->sync
.len
= READ_ONCE(sqe
->len
);
3789 req
->sync
.flags
= READ_ONCE(sqe
->sync_range_flags
);
3793 static int io_sync_file_range(struct io_kiocb
*req
, bool force_nonblock
)
3797 /* sync_file_range always requires a blocking context */
3801 ret
= sync_file_range(req
->file
, req
->sync
.off
, req
->sync
.len
,
3804 req_set_fail_links(req
);
3805 io_req_complete(req
, ret
);
3809 #if defined(CONFIG_NET)
3810 static int io_setup_async_msg(struct io_kiocb
*req
,
3811 struct io_async_msghdr
*kmsg
)
3815 if (io_alloc_async_ctx(req
)) {
3816 if (kmsg
->iov
!= kmsg
->fast_iov
)
3820 req
->flags
|= REQ_F_NEED_CLEANUP
;
3821 memcpy(&req
->io
->msg
, kmsg
, sizeof(*kmsg
));
3825 static int io_sendmsg_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
3827 struct io_sr_msg
*sr
= &req
->sr_msg
;
3828 struct io_async_ctx
*io
= req
->io
;
3831 if (unlikely(req
->ctx
->flags
& IORING_SETUP_IOPOLL
))
3834 sr
->msg_flags
= READ_ONCE(sqe
->msg_flags
);
3835 sr
->msg
= u64_to_user_ptr(READ_ONCE(sqe
->addr
));
3836 sr
->len
= READ_ONCE(sqe
->len
);
3838 #ifdef CONFIG_COMPAT
3839 if (req
->ctx
->compat
)
3840 sr
->msg_flags
|= MSG_CMSG_COMPAT
;
3843 if (!io
|| req
->opcode
== IORING_OP_SEND
)
3845 /* iovec is already imported */
3846 if (req
->flags
& REQ_F_NEED_CLEANUP
)
3849 io
->msg
.iov
= io
->msg
.fast_iov
;
3850 ret
= sendmsg_copy_msghdr(&io
->msg
.msg
, sr
->msg
, sr
->msg_flags
,
3853 req
->flags
|= REQ_F_NEED_CLEANUP
;
3857 static int io_sendmsg(struct io_kiocb
*req
, bool force_nonblock
,
3858 struct io_comp_state
*cs
)
3860 struct io_async_msghdr
*kmsg
= NULL
;
3861 struct socket
*sock
;
3864 sock
= sock_from_file(req
->file
, &ret
);
3866 struct io_async_ctx io
;
3870 kmsg
= &req
->io
->msg
;
3871 kmsg
->msg
.msg_name
= &req
->io
->msg
.addr
;
3872 /* if iov is set, it's allocated already */
3874 kmsg
->iov
= kmsg
->fast_iov
;
3875 kmsg
->msg
.msg_iter
.iov
= kmsg
->iov
;
3877 struct io_sr_msg
*sr
= &req
->sr_msg
;
3880 kmsg
->msg
.msg_name
= &io
.msg
.addr
;
3882 io
.msg
.iov
= io
.msg
.fast_iov
;
3883 ret
= sendmsg_copy_msghdr(&io
.msg
.msg
, sr
->msg
,
3884 sr
->msg_flags
, &io
.msg
.iov
);
3889 flags
= req
->sr_msg
.msg_flags
;
3890 if (flags
& MSG_DONTWAIT
)
3891 req
->flags
|= REQ_F_NOWAIT
;
3892 else if (force_nonblock
)
3893 flags
|= MSG_DONTWAIT
;
3895 ret
= __sys_sendmsg_sock(sock
, &kmsg
->msg
, flags
);
3896 if (force_nonblock
&& ret
== -EAGAIN
)
3897 return io_setup_async_msg(req
, kmsg
);
3898 if (ret
== -ERESTARTSYS
)
3902 if (kmsg
&& kmsg
->iov
!= kmsg
->fast_iov
)
3904 req
->flags
&= ~REQ_F_NEED_CLEANUP
;
3906 req_set_fail_links(req
);
3907 __io_req_complete(req
, ret
, 0, cs
);
3911 static int io_send(struct io_kiocb
*req
, bool force_nonblock
,
3912 struct io_comp_state
*cs
)
3914 struct socket
*sock
;
3917 sock
= sock_from_file(req
->file
, &ret
);
3919 struct io_sr_msg
*sr
= &req
->sr_msg
;
3924 ret
= import_single_range(WRITE
, sr
->buf
, sr
->len
, &iov
,
3929 msg
.msg_name
= NULL
;
3930 msg
.msg_control
= NULL
;
3931 msg
.msg_controllen
= 0;
3932 msg
.msg_namelen
= 0;
3934 flags
= req
->sr_msg
.msg_flags
;
3935 if (flags
& MSG_DONTWAIT
)
3936 req
->flags
|= REQ_F_NOWAIT
;
3937 else if (force_nonblock
)
3938 flags
|= MSG_DONTWAIT
;
3940 msg
.msg_flags
= flags
;
3941 ret
= sock_sendmsg(sock
, &msg
);
3942 if (force_nonblock
&& ret
== -EAGAIN
)
3944 if (ret
== -ERESTARTSYS
)
3949 req_set_fail_links(req
);
3950 __io_req_complete(req
, ret
, 0, cs
);
3954 static int __io_recvmsg_copy_hdr(struct io_kiocb
*req
, struct io_async_ctx
*io
)
3956 struct io_sr_msg
*sr
= &req
->sr_msg
;
3957 struct iovec __user
*uiov
;
3961 ret
= __copy_msghdr_from_user(&io
->msg
.msg
, sr
->msg
, &io
->msg
.uaddr
,
3966 if (req
->flags
& REQ_F_BUFFER_SELECT
) {
3969 if (copy_from_user(io
->msg
.iov
, uiov
, sizeof(*uiov
)))
3971 sr
->len
= io
->msg
.iov
[0].iov_len
;
3972 iov_iter_init(&io
->msg
.msg
.msg_iter
, READ
, io
->msg
.iov
, 1,
3976 ret
= import_iovec(READ
, uiov
, iov_len
, UIO_FASTIOV
,
3977 &io
->msg
.iov
, &io
->msg
.msg
.msg_iter
);
3985 #ifdef CONFIG_COMPAT
3986 static int __io_compat_recvmsg_copy_hdr(struct io_kiocb
*req
,
3987 struct io_async_ctx
*io
)
3989 struct compat_msghdr __user
*msg_compat
;
3990 struct io_sr_msg
*sr
= &req
->sr_msg
;
3991 struct compat_iovec __user
*uiov
;
3996 msg_compat
= (struct compat_msghdr __user
*) sr
->msg
;
3997 ret
= __get_compat_msghdr(&io
->msg
.msg
, msg_compat
, &io
->msg
.uaddr
,
4002 uiov
= compat_ptr(ptr
);
4003 if (req
->flags
& REQ_F_BUFFER_SELECT
) {
4004 compat_ssize_t clen
;
4008 if (!access_ok(uiov
, sizeof(*uiov
)))
4010 if (__get_user(clen
, &uiov
->iov_len
))
4014 sr
->len
= io
->msg
.iov
[0].iov_len
;
4017 ret
= compat_import_iovec(READ
, uiov
, len
, UIO_FASTIOV
,
4019 &io
->msg
.msg
.msg_iter
);
4028 static int io_recvmsg_copy_hdr(struct io_kiocb
*req
, struct io_async_ctx
*io
)
4030 io
->msg
.iov
= io
->msg
.fast_iov
;
4032 #ifdef CONFIG_COMPAT
4033 if (req
->ctx
->compat
)
4034 return __io_compat_recvmsg_copy_hdr(req
, io
);
4037 return __io_recvmsg_copy_hdr(req
, io
);
4040 static struct io_buffer
*io_recv_buffer_select(struct io_kiocb
*req
,
4041 int *cflags
, bool needs_lock
)
4043 struct io_sr_msg
*sr
= &req
->sr_msg
;
4044 struct io_buffer
*kbuf
;
4046 if (!(req
->flags
& REQ_F_BUFFER_SELECT
))
4049 kbuf
= io_buffer_select(req
, &sr
->len
, sr
->bgid
, sr
->kbuf
, needs_lock
);
4054 req
->flags
|= REQ_F_BUFFER_SELECTED
;
4056 *cflags
= kbuf
->bid
<< IORING_CQE_BUFFER_SHIFT
;
4057 *cflags
|= IORING_CQE_F_BUFFER
;
4061 static int io_recvmsg_prep(struct io_kiocb
*req
,
4062 const struct io_uring_sqe
*sqe
)
4064 struct io_sr_msg
*sr
= &req
->sr_msg
;
4065 struct io_async_ctx
*io
= req
->io
;
4068 if (unlikely(req
->ctx
->flags
& IORING_SETUP_IOPOLL
))
4071 sr
->msg_flags
= READ_ONCE(sqe
->msg_flags
);
4072 sr
->msg
= u64_to_user_ptr(READ_ONCE(sqe
->addr
));
4073 sr
->len
= READ_ONCE(sqe
->len
);
4074 sr
->bgid
= READ_ONCE(sqe
->buf_group
);
4076 #ifdef CONFIG_COMPAT
4077 if (req
->ctx
->compat
)
4078 sr
->msg_flags
|= MSG_CMSG_COMPAT
;
4081 if (!io
|| req
->opcode
== IORING_OP_RECV
)
4083 /* iovec is already imported */
4084 if (req
->flags
& REQ_F_NEED_CLEANUP
)
4087 ret
= io_recvmsg_copy_hdr(req
, io
);
4089 req
->flags
|= REQ_F_NEED_CLEANUP
;
4093 static int io_recvmsg(struct io_kiocb
*req
, bool force_nonblock
,
4094 struct io_comp_state
*cs
)
4096 struct io_async_msghdr
*kmsg
= NULL
;
4097 struct socket
*sock
;
4098 int ret
, cflags
= 0;
4100 sock
= sock_from_file(req
->file
, &ret
);
4102 struct io_buffer
*kbuf
;
4103 struct io_async_ctx io
;
4107 kmsg
= &req
->io
->msg
;
4108 kmsg
->msg
.msg_name
= &req
->io
->msg
.addr
;
4109 /* if iov is set, it's allocated already */
4111 kmsg
->iov
= kmsg
->fast_iov
;
4112 kmsg
->msg
.msg_iter
.iov
= kmsg
->iov
;
4115 kmsg
->msg
.msg_name
= &io
.msg
.addr
;
4117 ret
= io_recvmsg_copy_hdr(req
, &io
);
4122 kbuf
= io_recv_buffer_select(req
, &cflags
, !force_nonblock
);
4124 return PTR_ERR(kbuf
);
4126 kmsg
->fast_iov
[0].iov_base
= u64_to_user_ptr(kbuf
->addr
);
4127 iov_iter_init(&kmsg
->msg
.msg_iter
, READ
, kmsg
->iov
,
4128 1, req
->sr_msg
.len
);
4131 flags
= req
->sr_msg
.msg_flags
;
4132 if (flags
& MSG_DONTWAIT
)
4133 req
->flags
|= REQ_F_NOWAIT
;
4134 else if (force_nonblock
)
4135 flags
|= MSG_DONTWAIT
;
4137 ret
= __sys_recvmsg_sock(sock
, &kmsg
->msg
, req
->sr_msg
.msg
,
4138 kmsg
->uaddr
, flags
);
4139 if (force_nonblock
&& ret
== -EAGAIN
)
4140 return io_setup_async_msg(req
, kmsg
);
4141 if (ret
== -ERESTARTSYS
)
4145 if (kmsg
&& kmsg
->iov
!= kmsg
->fast_iov
)
4147 req
->flags
&= ~REQ_F_NEED_CLEANUP
;
4149 req_set_fail_links(req
);
4150 __io_req_complete(req
, ret
, cflags
, cs
);
4154 static int io_recv(struct io_kiocb
*req
, bool force_nonblock
,
4155 struct io_comp_state
*cs
)
4157 struct io_buffer
*kbuf
= NULL
;
4158 struct socket
*sock
;
4159 int ret
, cflags
= 0;
4161 sock
= sock_from_file(req
->file
, &ret
);
4163 struct io_sr_msg
*sr
= &req
->sr_msg
;
4164 void __user
*buf
= sr
->buf
;
4169 kbuf
= io_recv_buffer_select(req
, &cflags
, !force_nonblock
);
4171 return PTR_ERR(kbuf
);
4173 buf
= u64_to_user_ptr(kbuf
->addr
);
4175 ret
= import_single_range(READ
, buf
, sr
->len
, &iov
,
4182 req
->flags
|= REQ_F_NEED_CLEANUP
;
4183 msg
.msg_name
= NULL
;
4184 msg
.msg_control
= NULL
;
4185 msg
.msg_controllen
= 0;
4186 msg
.msg_namelen
= 0;
4187 msg
.msg_iocb
= NULL
;
4190 flags
= req
->sr_msg
.msg_flags
;
4191 if (flags
& MSG_DONTWAIT
)
4192 req
->flags
|= REQ_F_NOWAIT
;
4193 else if (force_nonblock
)
4194 flags
|= MSG_DONTWAIT
;
4196 ret
= sock_recvmsg(sock
, &msg
, flags
);
4197 if (force_nonblock
&& ret
== -EAGAIN
)
4199 if (ret
== -ERESTARTSYS
)
4204 req
->flags
&= ~REQ_F_NEED_CLEANUP
;
4206 req_set_fail_links(req
);
4207 __io_req_complete(req
, ret
, cflags
, cs
);
4211 static int io_accept_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
4213 struct io_accept
*accept
= &req
->accept
;
4215 if (unlikely(req
->ctx
->flags
& (IORING_SETUP_IOPOLL
|IORING_SETUP_SQPOLL
)))
4217 if (sqe
->ioprio
|| sqe
->len
|| sqe
->buf_index
)
4220 accept
->addr
= u64_to_user_ptr(READ_ONCE(sqe
->addr
));
4221 accept
->addr_len
= u64_to_user_ptr(READ_ONCE(sqe
->addr2
));
4222 accept
->flags
= READ_ONCE(sqe
->accept_flags
);
4223 accept
->nofile
= rlimit(RLIMIT_NOFILE
);
4227 static int io_accept(struct io_kiocb
*req
, bool force_nonblock
,
4228 struct io_comp_state
*cs
)
4230 struct io_accept
*accept
= &req
->accept
;
4231 unsigned int file_flags
= force_nonblock
? O_NONBLOCK
: 0;
4234 if (req
->file
->f_flags
& O_NONBLOCK
)
4235 req
->flags
|= REQ_F_NOWAIT
;
4237 ret
= __sys_accept4_file(req
->file
, file_flags
, accept
->addr
,
4238 accept
->addr_len
, accept
->flags
,
4240 if (ret
== -EAGAIN
&& force_nonblock
)
4243 if (ret
== -ERESTARTSYS
)
4245 req_set_fail_links(req
);
4247 __io_req_complete(req
, ret
, 0, cs
);
4251 static int io_connect_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
4253 struct io_connect
*conn
= &req
->connect
;
4254 struct io_async_ctx
*io
= req
->io
;
4256 if (unlikely(req
->ctx
->flags
& (IORING_SETUP_IOPOLL
|IORING_SETUP_SQPOLL
)))
4258 if (sqe
->ioprio
|| sqe
->len
|| sqe
->buf_index
|| sqe
->rw_flags
)
4261 conn
->addr
= u64_to_user_ptr(READ_ONCE(sqe
->addr
));
4262 conn
->addr_len
= READ_ONCE(sqe
->addr2
);
4267 return move_addr_to_kernel(conn
->addr
, conn
->addr_len
,
4268 &io
->connect
.address
);
4271 static int io_connect(struct io_kiocb
*req
, bool force_nonblock
,
4272 struct io_comp_state
*cs
)
4274 struct io_async_ctx __io
, *io
;
4275 unsigned file_flags
;
4281 ret
= move_addr_to_kernel(req
->connect
.addr
,
4282 req
->connect
.addr_len
,
4283 &__io
.connect
.address
);
4289 file_flags
= force_nonblock
? O_NONBLOCK
: 0;
4291 ret
= __sys_connect_file(req
->file
, &io
->connect
.address
,
4292 req
->connect
.addr_len
, file_flags
);
4293 if ((ret
== -EAGAIN
|| ret
== -EINPROGRESS
) && force_nonblock
) {
4296 if (io_alloc_async_ctx(req
)) {
4300 memcpy(&req
->io
->connect
, &__io
.connect
, sizeof(__io
.connect
));
4303 if (ret
== -ERESTARTSYS
)
4307 req_set_fail_links(req
);
4308 __io_req_complete(req
, ret
, 0, cs
);
4311 #else /* !CONFIG_NET */
4312 static int io_sendmsg_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
4317 static int io_sendmsg(struct io_kiocb
*req
, bool force_nonblock
)
4322 static int io_send(struct io_kiocb
*req
, bool force_nonblock
)
4327 static int io_recvmsg_prep(struct io_kiocb
*req
,
4328 const struct io_uring_sqe
*sqe
)
4333 static int io_recvmsg(struct io_kiocb
*req
, bool force_nonblock
)
4338 static int io_recv(struct io_kiocb
*req
, bool force_nonblock
)
4343 static int io_accept_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
4348 static int io_accept(struct io_kiocb
*req
, bool force_nonblock
)
4353 static int io_connect_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
4358 static int io_connect(struct io_kiocb
*req
, bool force_nonblock
)
4362 #endif /* CONFIG_NET */
4364 struct io_poll_table
{
4365 struct poll_table_struct pt
;
4366 struct io_kiocb
*req
;
4370 static int __io_async_wake(struct io_kiocb
*req
, struct io_poll_iocb
*poll
,
4371 __poll_t mask
, task_work_func_t func
)
4373 struct task_struct
*tsk
;
4376 /* for instances that support it check for an event match first: */
4377 if (mask
&& !(mask
& poll
->events
))
4380 trace_io_uring_task_add(req
->ctx
, req
->opcode
, req
->user_data
, mask
);
4382 list_del_init(&poll
->wait
.entry
);
4386 init_task_work(&req
->task_work
, func
);
4388 * If this fails, then the task is exiting. When a task exits, the
4389 * work gets canceled, so just cancel this request as well instead
4390 * of executing it. We can't safely execute it anyway, as we may not
4391 * have the needed state needed for it anyway.
4393 ret
= task_work_add(tsk
, &req
->task_work
, true);
4394 if (unlikely(ret
)) {
4395 WRITE_ONCE(poll
->canceled
, true);
4396 tsk
= io_wq_get_task(req
->ctx
->io_wq
);
4397 task_work_add(tsk
, &req
->task_work
, true);
4399 wake_up_process(tsk
);
4403 static bool io_poll_rewait(struct io_kiocb
*req
, struct io_poll_iocb
*poll
)
4404 __acquires(&req
->ctx
->completion_lock
)
4406 struct io_ring_ctx
*ctx
= req
->ctx
;
4408 if (!req
->result
&& !READ_ONCE(poll
->canceled
)) {
4409 struct poll_table_struct pt
= { ._key
= poll
->events
};
4411 req
->result
= vfs_poll(req
->file
, &pt
) & poll
->events
;
4414 spin_lock_irq(&ctx
->completion_lock
);
4415 if (!req
->result
&& !READ_ONCE(poll
->canceled
)) {
4416 add_wait_queue(poll
->head
, &poll
->wait
);
4423 static void io_poll_remove_double(struct io_kiocb
*req
)
4425 struct io_poll_iocb
*poll
= (struct io_poll_iocb
*) req
->io
;
4427 lockdep_assert_held(&req
->ctx
->completion_lock
);
4429 if (poll
&& poll
->head
) {
4430 struct wait_queue_head
*head
= poll
->head
;
4432 spin_lock(&head
->lock
);
4433 list_del_init(&poll
->wait
.entry
);
4434 if (poll
->wait
.private)
4435 refcount_dec(&req
->refs
);
4437 spin_unlock(&head
->lock
);
4441 static void io_poll_complete(struct io_kiocb
*req
, __poll_t mask
, int error
)
4443 struct io_ring_ctx
*ctx
= req
->ctx
;
4445 io_poll_remove_double(req
);
4446 req
->poll
.done
= true;
4447 io_cqring_fill_event(req
, error
? error
: mangle_poll(mask
));
4448 io_commit_cqring(ctx
);
4451 static void io_poll_task_handler(struct io_kiocb
*req
, struct io_kiocb
**nxt
)
4453 struct io_ring_ctx
*ctx
= req
->ctx
;
4455 if (io_poll_rewait(req
, &req
->poll
)) {
4456 spin_unlock_irq(&ctx
->completion_lock
);
4460 hash_del(&req
->hash_node
);
4461 io_poll_complete(req
, req
->result
, 0);
4462 req
->flags
|= REQ_F_COMP_LOCKED
;
4463 io_put_req_find_next(req
, nxt
);
4464 spin_unlock_irq(&ctx
->completion_lock
);
4466 io_cqring_ev_posted(ctx
);
4469 static void io_poll_task_func(struct callback_head
*cb
)
4471 struct io_kiocb
*req
= container_of(cb
, struct io_kiocb
, task_work
);
4472 struct io_kiocb
*nxt
= NULL
;
4474 io_poll_task_handler(req
, &nxt
);
4476 struct io_ring_ctx
*ctx
= nxt
->ctx
;
4478 mutex_lock(&ctx
->uring_lock
);
4479 __io_queue_sqe(nxt
, NULL
, NULL
);
4480 mutex_unlock(&ctx
->uring_lock
);
4484 static int io_poll_double_wake(struct wait_queue_entry
*wait
, unsigned mode
,
4485 int sync
, void *key
)
4487 struct io_kiocb
*req
= wait
->private;
4488 struct io_poll_iocb
*poll
= (struct io_poll_iocb
*) req
->io
;
4489 __poll_t mask
= key_to_poll(key
);
4491 /* for instances that support it check for an event match first: */
4492 if (mask
&& !(mask
& poll
->events
))
4495 if (req
->poll
.head
) {
4498 spin_lock(&req
->poll
.head
->lock
);
4499 done
= list_empty(&req
->poll
.wait
.entry
);
4501 list_del_init(&req
->poll
.wait
.entry
);
4502 spin_unlock(&req
->poll
.head
->lock
);
4504 __io_async_wake(req
, poll
, mask
, io_poll_task_func
);
4506 refcount_dec(&req
->refs
);
4510 static void io_init_poll_iocb(struct io_poll_iocb
*poll
, __poll_t events
,
4511 wait_queue_func_t wake_func
)
4515 poll
->canceled
= false;
4516 poll
->events
= events
;
4517 INIT_LIST_HEAD(&poll
->wait
.entry
);
4518 init_waitqueue_func_entry(&poll
->wait
, wake_func
);
4521 static void __io_queue_proc(struct io_poll_iocb
*poll
, struct io_poll_table
*pt
,
4522 struct wait_queue_head
*head
)
4524 struct io_kiocb
*req
= pt
->req
;
4527 * If poll->head is already set, it's because the file being polled
4528 * uses multiple waitqueues for poll handling (eg one for read, one
4529 * for write). Setup a separate io_poll_iocb if this happens.
4531 if (unlikely(poll
->head
)) {
4532 /* already have a 2nd entry, fail a third attempt */
4534 pt
->error
= -EINVAL
;
4537 poll
= kmalloc(sizeof(*poll
), GFP_ATOMIC
);
4539 pt
->error
= -ENOMEM
;
4542 io_init_poll_iocb(poll
, req
->poll
.events
, io_poll_double_wake
);
4543 refcount_inc(&req
->refs
);
4544 poll
->wait
.private = req
;
4545 req
->io
= (void *) poll
;
4551 if (poll
->events
& EPOLLEXCLUSIVE
)
4552 add_wait_queue_exclusive(head
, &poll
->wait
);
4554 add_wait_queue(head
, &poll
->wait
);
4557 static void io_async_queue_proc(struct file
*file
, struct wait_queue_head
*head
,
4558 struct poll_table_struct
*p
)
4560 struct io_poll_table
*pt
= container_of(p
, struct io_poll_table
, pt
);
4562 __io_queue_proc(&pt
->req
->apoll
->poll
, pt
, head
);
4565 static void io_async_task_func(struct callback_head
*cb
)
4567 struct io_kiocb
*req
= container_of(cb
, struct io_kiocb
, task_work
);
4568 struct async_poll
*apoll
= req
->apoll
;
4569 struct io_ring_ctx
*ctx
= req
->ctx
;
4570 bool canceled
= false;
4572 trace_io_uring_task_run(req
->ctx
, req
->opcode
, req
->user_data
);
4574 if (io_poll_rewait(req
, &apoll
->poll
)) {
4575 spin_unlock_irq(&ctx
->completion_lock
);
4579 /* If req is still hashed, it cannot have been canceled. Don't check. */
4580 if (hash_hashed(&req
->hash_node
)) {
4581 hash_del(&req
->hash_node
);
4583 canceled
= READ_ONCE(apoll
->poll
.canceled
);
4585 io_cqring_fill_event(req
, -ECANCELED
);
4586 io_commit_cqring(ctx
);
4590 spin_unlock_irq(&ctx
->completion_lock
);
4592 /* restore ->work in case we need to retry again */
4593 if (req
->flags
& REQ_F_WORK_INITIALIZED
)
4594 memcpy(&req
->work
, &apoll
->work
, sizeof(req
->work
));
4598 __set_current_state(TASK_RUNNING
);
4599 if (io_sq_thread_acquire_mm(ctx
, req
)) {
4600 io_cqring_add_event(req
, -EFAULT
, 0);
4603 mutex_lock(&ctx
->uring_lock
);
4604 __io_queue_sqe(req
, NULL
, NULL
);
4605 mutex_unlock(&ctx
->uring_lock
);
4607 io_cqring_ev_posted(ctx
);
4609 req_set_fail_links(req
);
4610 io_double_put_req(req
);
4614 static int io_async_wake(struct wait_queue_entry
*wait
, unsigned mode
, int sync
,
4617 struct io_kiocb
*req
= wait
->private;
4618 struct io_poll_iocb
*poll
= &req
->apoll
->poll
;
4620 trace_io_uring_poll_wake(req
->ctx
, req
->opcode
, req
->user_data
,
4623 return __io_async_wake(req
, poll
, key_to_poll(key
), io_async_task_func
);
4626 static void io_poll_req_insert(struct io_kiocb
*req
)
4628 struct io_ring_ctx
*ctx
= req
->ctx
;
4629 struct hlist_head
*list
;
4631 list
= &ctx
->cancel_hash
[hash_long(req
->user_data
, ctx
->cancel_hash_bits
)];
4632 hlist_add_head(&req
->hash_node
, list
);
4635 static __poll_t
__io_arm_poll_handler(struct io_kiocb
*req
,
4636 struct io_poll_iocb
*poll
,
4637 struct io_poll_table
*ipt
, __poll_t mask
,
4638 wait_queue_func_t wake_func
)
4639 __acquires(&ctx
->completion_lock
)
4641 struct io_ring_ctx
*ctx
= req
->ctx
;
4642 bool cancel
= false;
4644 io_init_poll_iocb(poll
, mask
, wake_func
);
4645 poll
->file
= req
->file
;
4646 poll
->wait
.private = req
;
4648 ipt
->pt
._key
= mask
;
4650 ipt
->error
= -EINVAL
;
4652 mask
= vfs_poll(req
->file
, &ipt
->pt
) & poll
->events
;
4654 spin_lock_irq(&ctx
->completion_lock
);
4655 if (likely(poll
->head
)) {
4656 spin_lock(&poll
->head
->lock
);
4657 if (unlikely(list_empty(&poll
->wait
.entry
))) {
4663 if (mask
|| ipt
->error
)
4664 list_del_init(&poll
->wait
.entry
);
4666 WRITE_ONCE(poll
->canceled
, true);
4667 else if (!poll
->done
) /* actually waiting for an event */
4668 io_poll_req_insert(req
);
4669 spin_unlock(&poll
->head
->lock
);
4675 static bool io_arm_poll_handler(struct io_kiocb
*req
)
4677 const struct io_op_def
*def
= &io_op_defs
[req
->opcode
];
4678 struct io_ring_ctx
*ctx
= req
->ctx
;
4679 struct async_poll
*apoll
;
4680 struct io_poll_table ipt
;
4684 if (!req
->file
|| !file_can_poll(req
->file
))
4686 if (req
->flags
& REQ_F_POLLED
)
4688 if (!def
->pollin
&& !def
->pollout
)
4691 apoll
= kmalloc(sizeof(*apoll
), GFP_ATOMIC
);
4692 if (unlikely(!apoll
))
4695 req
->flags
|= REQ_F_POLLED
;
4696 if (req
->flags
& REQ_F_WORK_INITIALIZED
)
4697 memcpy(&apoll
->work
, &req
->work
, sizeof(req
->work
));
4698 had_io
= req
->io
!= NULL
;
4700 io_get_req_task(req
);
4702 INIT_HLIST_NODE(&req
->hash_node
);
4706 mask
|= POLLIN
| POLLRDNORM
;
4708 mask
|= POLLOUT
| POLLWRNORM
;
4709 mask
|= POLLERR
| POLLPRI
;
4711 ipt
.pt
._qproc
= io_async_queue_proc
;
4713 ret
= __io_arm_poll_handler(req
, &apoll
->poll
, &ipt
, mask
,
4717 /* only remove double add if we did it here */
4719 io_poll_remove_double(req
);
4720 spin_unlock_irq(&ctx
->completion_lock
);
4721 if (req
->flags
& REQ_F_WORK_INITIALIZED
)
4722 memcpy(&req
->work
, &apoll
->work
, sizeof(req
->work
));
4726 spin_unlock_irq(&ctx
->completion_lock
);
4727 trace_io_uring_poll_arm(ctx
, req
->opcode
, req
->user_data
, mask
,
4728 apoll
->poll
.events
);
4732 static bool __io_poll_remove_one(struct io_kiocb
*req
,
4733 struct io_poll_iocb
*poll
)
4735 bool do_complete
= false;
4737 spin_lock(&poll
->head
->lock
);
4738 WRITE_ONCE(poll
->canceled
, true);
4739 if (!list_empty(&poll
->wait
.entry
)) {
4740 list_del_init(&poll
->wait
.entry
);
4743 spin_unlock(&poll
->head
->lock
);
4744 hash_del(&req
->hash_node
);
4748 static bool io_poll_remove_one(struct io_kiocb
*req
)
4752 if (req
->opcode
== IORING_OP_POLL_ADD
) {
4753 io_poll_remove_double(req
);
4754 do_complete
= __io_poll_remove_one(req
, &req
->poll
);
4756 struct async_poll
*apoll
= req
->apoll
;
4758 /* non-poll requests have submit ref still */
4759 do_complete
= __io_poll_remove_one(req
, &apoll
->poll
);
4763 * restore ->work because we will call
4764 * io_req_work_drop_env below when dropping the
4767 if (req
->flags
& REQ_F_WORK_INITIALIZED
)
4768 memcpy(&req
->work
, &apoll
->work
,
4775 io_cqring_fill_event(req
, -ECANCELED
);
4776 io_commit_cqring(req
->ctx
);
4777 req
->flags
|= REQ_F_COMP_LOCKED
;
4784 static void io_poll_remove_all(struct io_ring_ctx
*ctx
)
4786 struct hlist_node
*tmp
;
4787 struct io_kiocb
*req
;
4790 spin_lock_irq(&ctx
->completion_lock
);
4791 for (i
= 0; i
< (1U << ctx
->cancel_hash_bits
); i
++) {
4792 struct hlist_head
*list
;
4794 list
= &ctx
->cancel_hash
[i
];
4795 hlist_for_each_entry_safe(req
, tmp
, list
, hash_node
)
4796 posted
+= io_poll_remove_one(req
);
4798 spin_unlock_irq(&ctx
->completion_lock
);
4801 io_cqring_ev_posted(ctx
);
4804 static int io_poll_cancel(struct io_ring_ctx
*ctx
, __u64 sqe_addr
)
4806 struct hlist_head
*list
;
4807 struct io_kiocb
*req
;
4809 list
= &ctx
->cancel_hash
[hash_long(sqe_addr
, ctx
->cancel_hash_bits
)];
4810 hlist_for_each_entry(req
, list
, hash_node
) {
4811 if (sqe_addr
!= req
->user_data
)
4813 if (io_poll_remove_one(req
))
4821 static int io_poll_remove_prep(struct io_kiocb
*req
,
4822 const struct io_uring_sqe
*sqe
)
4824 if (unlikely(req
->ctx
->flags
& IORING_SETUP_IOPOLL
))
4826 if (sqe
->ioprio
|| sqe
->off
|| sqe
->len
|| sqe
->buf_index
||
4830 req
->poll
.addr
= READ_ONCE(sqe
->addr
);
4835 * Find a running poll command that matches one specified in sqe->addr,
4836 * and remove it if found.
4838 static int io_poll_remove(struct io_kiocb
*req
)
4840 struct io_ring_ctx
*ctx
= req
->ctx
;
4844 addr
= req
->poll
.addr
;
4845 spin_lock_irq(&ctx
->completion_lock
);
4846 ret
= io_poll_cancel(ctx
, addr
);
4847 spin_unlock_irq(&ctx
->completion_lock
);
4850 req_set_fail_links(req
);
4851 io_req_complete(req
, ret
);
4855 static int io_poll_wake(struct wait_queue_entry
*wait
, unsigned mode
, int sync
,
4858 struct io_kiocb
*req
= wait
->private;
4859 struct io_poll_iocb
*poll
= &req
->poll
;
4861 return __io_async_wake(req
, poll
, key_to_poll(key
), io_poll_task_func
);
4864 static void io_poll_queue_proc(struct file
*file
, struct wait_queue_head
*head
,
4865 struct poll_table_struct
*p
)
4867 struct io_poll_table
*pt
= container_of(p
, struct io_poll_table
, pt
);
4869 __io_queue_proc(&pt
->req
->poll
, pt
, head
);
4872 static int io_poll_add_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
4874 struct io_poll_iocb
*poll
= &req
->poll
;
4877 if (unlikely(req
->ctx
->flags
& IORING_SETUP_IOPOLL
))
4879 if (sqe
->addr
|| sqe
->ioprio
|| sqe
->off
|| sqe
->len
|| sqe
->buf_index
)
4884 events
= READ_ONCE(sqe
->poll32_events
);
4886 events
= swahw32(events
);
4888 poll
->events
= demangle_poll(events
) | EPOLLERR
| EPOLLHUP
|
4889 (events
& EPOLLEXCLUSIVE
);
4891 io_get_req_task(req
);
4895 static int io_poll_add(struct io_kiocb
*req
)
4897 struct io_poll_iocb
*poll
= &req
->poll
;
4898 struct io_ring_ctx
*ctx
= req
->ctx
;
4899 struct io_poll_table ipt
;
4902 INIT_HLIST_NODE(&req
->hash_node
);
4903 INIT_LIST_HEAD(&req
->list
);
4904 ipt
.pt
._qproc
= io_poll_queue_proc
;
4906 mask
= __io_arm_poll_handler(req
, &req
->poll
, &ipt
, poll
->events
,
4909 if (mask
) { /* no async, we'd stolen it */
4911 io_poll_complete(req
, mask
, 0);
4913 spin_unlock_irq(&ctx
->completion_lock
);
4916 io_cqring_ev_posted(ctx
);
4922 static enum hrtimer_restart
io_timeout_fn(struct hrtimer
*timer
)
4924 struct io_timeout_data
*data
= container_of(timer
,
4925 struct io_timeout_data
, timer
);
4926 struct io_kiocb
*req
= data
->req
;
4927 struct io_ring_ctx
*ctx
= req
->ctx
;
4928 unsigned long flags
;
4930 atomic_inc(&ctx
->cq_timeouts
);
4932 spin_lock_irqsave(&ctx
->completion_lock
, flags
);
4934 * We could be racing with timeout deletion. If the list is empty,
4935 * then timeout lookup already found it and will be handling it.
4937 if (!list_empty(&req
->list
))
4938 list_del_init(&req
->list
);
4940 io_cqring_fill_event(req
, -ETIME
);
4941 io_commit_cqring(ctx
);
4942 spin_unlock_irqrestore(&ctx
->completion_lock
, flags
);
4944 io_cqring_ev_posted(ctx
);
4945 req_set_fail_links(req
);
4947 return HRTIMER_NORESTART
;
4950 static int io_timeout_cancel(struct io_ring_ctx
*ctx
, __u64 user_data
)
4952 struct io_kiocb
*req
;
4955 list_for_each_entry(req
, &ctx
->timeout_list
, list
) {
4956 if (user_data
== req
->user_data
) {
4957 list_del_init(&req
->list
);
4966 ret
= hrtimer_try_to_cancel(&req
->io
->timeout
.timer
);
4970 req_set_fail_links(req
);
4971 io_cqring_fill_event(req
, -ECANCELED
);
4976 static int io_timeout_remove_prep(struct io_kiocb
*req
,
4977 const struct io_uring_sqe
*sqe
)
4979 if (unlikely(req
->ctx
->flags
& IORING_SETUP_IOPOLL
))
4981 if (sqe
->flags
|| sqe
->ioprio
|| sqe
->buf_index
|| sqe
->len
)
4984 req
->timeout
.addr
= READ_ONCE(sqe
->addr
);
4985 req
->timeout
.flags
= READ_ONCE(sqe
->timeout_flags
);
4986 if (req
->timeout
.flags
)
4993 * Remove or update an existing timeout command
4995 static int io_timeout_remove(struct io_kiocb
*req
)
4997 struct io_ring_ctx
*ctx
= req
->ctx
;
5000 spin_lock_irq(&ctx
->completion_lock
);
5001 ret
= io_timeout_cancel(ctx
, req
->timeout
.addr
);
5003 io_cqring_fill_event(req
, ret
);
5004 io_commit_cqring(ctx
);
5005 spin_unlock_irq(&ctx
->completion_lock
);
5006 io_cqring_ev_posted(ctx
);
5008 req_set_fail_links(req
);
5013 static int io_timeout_prep(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
,
5014 bool is_timeout_link
)
5016 struct io_timeout_data
*data
;
5018 u32 off
= READ_ONCE(sqe
->off
);
5020 if (unlikely(req
->ctx
->flags
& IORING_SETUP_IOPOLL
))
5022 if (sqe
->ioprio
|| sqe
->buf_index
|| sqe
->len
!= 1)
5024 if (off
&& is_timeout_link
)
5026 flags
= READ_ONCE(sqe
->timeout_flags
);
5027 if (flags
& ~IORING_TIMEOUT_ABS
)
5030 req
->timeout
.off
= off
;
5032 if (!req
->io
&& io_alloc_async_ctx(req
))
5035 data
= &req
->io
->timeout
;
5037 req
->flags
|= REQ_F_TIMEOUT
;
5039 if (get_timespec64(&data
->ts
, u64_to_user_ptr(sqe
->addr
)))
5042 if (flags
& IORING_TIMEOUT_ABS
)
5043 data
->mode
= HRTIMER_MODE_ABS
;
5045 data
->mode
= HRTIMER_MODE_REL
;
5047 hrtimer_init(&data
->timer
, CLOCK_MONOTONIC
, data
->mode
);
5051 static int io_timeout(struct io_kiocb
*req
)
5053 struct io_ring_ctx
*ctx
= req
->ctx
;
5054 struct io_timeout_data
*data
= &req
->io
->timeout
;
5055 struct list_head
*entry
;
5056 u32 tail
, off
= req
->timeout
.off
;
5058 spin_lock_irq(&ctx
->completion_lock
);
5061 * sqe->off holds how many events that need to occur for this
5062 * timeout event to be satisfied. If it isn't set, then this is
5063 * a pure timeout request, sequence isn't used.
5066 req
->flags
|= REQ_F_TIMEOUT_NOSEQ
;
5067 entry
= ctx
->timeout_list
.prev
;
5071 tail
= ctx
->cached_cq_tail
- atomic_read(&ctx
->cq_timeouts
);
5072 req
->timeout
.target_seq
= tail
+ off
;
5075 * Insertion sort, ensuring the first entry in the list is always
5076 * the one we need first.
5078 list_for_each_prev(entry
, &ctx
->timeout_list
) {
5079 struct io_kiocb
*nxt
= list_entry(entry
, struct io_kiocb
, list
);
5081 if (nxt
->flags
& REQ_F_TIMEOUT_NOSEQ
)
5083 /* nxt.seq is behind @tail, otherwise would've been completed */
5084 if (off
>= nxt
->timeout
.target_seq
- tail
)
5088 list_add(&req
->list
, entry
);
5089 data
->timer
.function
= io_timeout_fn
;
5090 hrtimer_start(&data
->timer
, timespec64_to_ktime(data
->ts
), data
->mode
);
5091 spin_unlock_irq(&ctx
->completion_lock
);
5095 static bool io_cancel_cb(struct io_wq_work
*work
, void *data
)
5097 struct io_kiocb
*req
= container_of(work
, struct io_kiocb
, work
);
5099 return req
->user_data
== (unsigned long) data
;
5102 static int io_async_cancel_one(struct io_ring_ctx
*ctx
, void *sqe_addr
)
5104 enum io_wq_cancel cancel_ret
;
5107 cancel_ret
= io_wq_cancel_cb(ctx
->io_wq
, io_cancel_cb
, sqe_addr
, false);
5108 switch (cancel_ret
) {
5109 case IO_WQ_CANCEL_OK
:
5112 case IO_WQ_CANCEL_RUNNING
:
5115 case IO_WQ_CANCEL_NOTFOUND
:
5123 static void io_async_find_and_cancel(struct io_ring_ctx
*ctx
,
5124 struct io_kiocb
*req
, __u64 sqe_addr
,
5127 unsigned long flags
;
5130 ret
= io_async_cancel_one(ctx
, (void *) (unsigned long) sqe_addr
);
5131 if (ret
!= -ENOENT
) {
5132 spin_lock_irqsave(&ctx
->completion_lock
, flags
);
5136 spin_lock_irqsave(&ctx
->completion_lock
, flags
);
5137 ret
= io_timeout_cancel(ctx
, sqe_addr
);
5140 ret
= io_poll_cancel(ctx
, sqe_addr
);
5144 io_cqring_fill_event(req
, ret
);
5145 io_commit_cqring(ctx
);
5146 spin_unlock_irqrestore(&ctx
->completion_lock
, flags
);
5147 io_cqring_ev_posted(ctx
);
5150 req_set_fail_links(req
);
5154 static int io_async_cancel_prep(struct io_kiocb
*req
,
5155 const struct io_uring_sqe
*sqe
)
5157 if (unlikely(req
->ctx
->flags
& IORING_SETUP_IOPOLL
))
5159 if (sqe
->flags
|| sqe
->ioprio
|| sqe
->off
|| sqe
->len
||
5163 req
->cancel
.addr
= READ_ONCE(sqe
->addr
);
5167 static int io_async_cancel(struct io_kiocb
*req
)
5169 struct io_ring_ctx
*ctx
= req
->ctx
;
5171 io_async_find_and_cancel(ctx
, req
, req
->cancel
.addr
, 0);
5175 static int io_files_update_prep(struct io_kiocb
*req
,
5176 const struct io_uring_sqe
*sqe
)
5178 if (sqe
->flags
|| sqe
->ioprio
|| sqe
->rw_flags
)
5181 req
->files_update
.offset
= READ_ONCE(sqe
->off
);
5182 req
->files_update
.nr_args
= READ_ONCE(sqe
->len
);
5183 if (!req
->files_update
.nr_args
)
5185 req
->files_update
.arg
= READ_ONCE(sqe
->addr
);
5189 static int io_files_update(struct io_kiocb
*req
, bool force_nonblock
,
5190 struct io_comp_state
*cs
)
5192 struct io_ring_ctx
*ctx
= req
->ctx
;
5193 struct io_uring_files_update up
;
5199 up
.offset
= req
->files_update
.offset
;
5200 up
.fds
= req
->files_update
.arg
;
5202 mutex_lock(&ctx
->uring_lock
);
5203 ret
= __io_sqe_files_update(ctx
, &up
, req
->files_update
.nr_args
);
5204 mutex_unlock(&ctx
->uring_lock
);
5207 req_set_fail_links(req
);
5208 __io_req_complete(req
, ret
, 0, cs
);
5212 static int io_req_defer_prep(struct io_kiocb
*req
,
5213 const struct io_uring_sqe
*sqe
)
5220 io_req_init_async(req
);
5222 if (io_op_defs
[req
->opcode
].file_table
) {
5223 ret
= io_grab_files(req
);
5228 io_req_work_grab_env(req
, &io_op_defs
[req
->opcode
]);
5230 switch (req
->opcode
) {
5233 case IORING_OP_READV
:
5234 case IORING_OP_READ_FIXED
:
5235 case IORING_OP_READ
:
5236 ret
= io_read_prep(req
, sqe
, true);
5238 case IORING_OP_WRITEV
:
5239 case IORING_OP_WRITE_FIXED
:
5240 case IORING_OP_WRITE
:
5241 ret
= io_write_prep(req
, sqe
, true);
5243 case IORING_OP_POLL_ADD
:
5244 ret
= io_poll_add_prep(req
, sqe
);
5246 case IORING_OP_POLL_REMOVE
:
5247 ret
= io_poll_remove_prep(req
, sqe
);
5249 case IORING_OP_FSYNC
:
5250 ret
= io_prep_fsync(req
, sqe
);
5252 case IORING_OP_SYNC_FILE_RANGE
:
5253 ret
= io_prep_sfr(req
, sqe
);
5255 case IORING_OP_SENDMSG
:
5256 case IORING_OP_SEND
:
5257 ret
= io_sendmsg_prep(req
, sqe
);
5259 case IORING_OP_RECVMSG
:
5260 case IORING_OP_RECV
:
5261 ret
= io_recvmsg_prep(req
, sqe
);
5263 case IORING_OP_CONNECT
:
5264 ret
= io_connect_prep(req
, sqe
);
5266 case IORING_OP_TIMEOUT
:
5267 ret
= io_timeout_prep(req
, sqe
, false);
5269 case IORING_OP_TIMEOUT_REMOVE
:
5270 ret
= io_timeout_remove_prep(req
, sqe
);
5272 case IORING_OP_ASYNC_CANCEL
:
5273 ret
= io_async_cancel_prep(req
, sqe
);
5275 case IORING_OP_LINK_TIMEOUT
:
5276 ret
= io_timeout_prep(req
, sqe
, true);
5278 case IORING_OP_ACCEPT
:
5279 ret
= io_accept_prep(req
, sqe
);
5281 case IORING_OP_FALLOCATE
:
5282 ret
= io_fallocate_prep(req
, sqe
);
5284 case IORING_OP_OPENAT
:
5285 ret
= io_openat_prep(req
, sqe
);
5287 case IORING_OP_CLOSE
:
5288 ret
= io_close_prep(req
, sqe
);
5290 case IORING_OP_FILES_UPDATE
:
5291 ret
= io_files_update_prep(req
, sqe
);
5293 case IORING_OP_STATX
:
5294 ret
= io_statx_prep(req
, sqe
);
5296 case IORING_OP_FADVISE
:
5297 ret
= io_fadvise_prep(req
, sqe
);
5299 case IORING_OP_MADVISE
:
5300 ret
= io_madvise_prep(req
, sqe
);
5302 case IORING_OP_OPENAT2
:
5303 ret
= io_openat2_prep(req
, sqe
);
5305 case IORING_OP_EPOLL_CTL
:
5306 ret
= io_epoll_ctl_prep(req
, sqe
);
5308 case IORING_OP_SPLICE
:
5309 ret
= io_splice_prep(req
, sqe
);
5311 case IORING_OP_PROVIDE_BUFFERS
:
5312 ret
= io_provide_buffers_prep(req
, sqe
);
5314 case IORING_OP_REMOVE_BUFFERS
:
5315 ret
= io_remove_buffers_prep(req
, sqe
);
5318 ret
= io_tee_prep(req
, sqe
);
5321 printk_once(KERN_WARNING
"io_uring: unhandled opcode %d\n",
5330 static int io_req_defer(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
5332 struct io_ring_ctx
*ctx
= req
->ctx
;
5335 /* Still need defer if there is pending req in defer list. */
5336 if (!req_need_defer(req
) && list_empty_careful(&ctx
->defer_list
))
5340 if (io_alloc_async_ctx(req
))
5342 ret
= io_req_defer_prep(req
, sqe
);
5347 spin_lock_irq(&ctx
->completion_lock
);
5348 if (!req_need_defer(req
) && list_empty(&ctx
->defer_list
)) {
5349 spin_unlock_irq(&ctx
->completion_lock
);
5353 trace_io_uring_defer(ctx
, req
, req
->user_data
);
5354 list_add_tail(&req
->list
, &ctx
->defer_list
);
5355 spin_unlock_irq(&ctx
->completion_lock
);
5356 return -EIOCBQUEUED
;
5359 static void io_cleanup_req(struct io_kiocb
*req
)
5361 struct io_async_ctx
*io
= req
->io
;
5363 switch (req
->opcode
) {
5364 case IORING_OP_READV
:
5365 case IORING_OP_READ_FIXED
:
5366 case IORING_OP_READ
:
5367 if (req
->flags
& REQ_F_BUFFER_SELECTED
)
5368 kfree((void *)(unsigned long)req
->rw
.addr
);
5370 case IORING_OP_WRITEV
:
5371 case IORING_OP_WRITE_FIXED
:
5372 case IORING_OP_WRITE
:
5373 if (io
->rw
.iov
!= io
->rw
.fast_iov
)
5376 case IORING_OP_RECVMSG
:
5377 if (req
->flags
& REQ_F_BUFFER_SELECTED
)
5378 kfree(req
->sr_msg
.kbuf
);
5380 case IORING_OP_SENDMSG
:
5381 if (io
->msg
.iov
!= io
->msg
.fast_iov
)
5384 case IORING_OP_RECV
:
5385 if (req
->flags
& REQ_F_BUFFER_SELECTED
)
5386 kfree(req
->sr_msg
.kbuf
);
5388 case IORING_OP_OPENAT
:
5389 case IORING_OP_OPENAT2
:
5391 case IORING_OP_SPLICE
:
5393 io_put_file(req
, req
->splice
.file_in
,
5394 (req
->splice
.flags
& SPLICE_F_FD_IN_FIXED
));
5398 req
->flags
&= ~REQ_F_NEED_CLEANUP
;
5401 static int io_issue_sqe(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
,
5402 bool force_nonblock
, struct io_comp_state
*cs
)
5404 struct io_ring_ctx
*ctx
= req
->ctx
;
5407 switch (req
->opcode
) {
5409 ret
= io_nop(req
, cs
);
5411 case IORING_OP_READV
:
5412 case IORING_OP_READ_FIXED
:
5413 case IORING_OP_READ
:
5415 ret
= io_read_prep(req
, sqe
, force_nonblock
);
5419 ret
= io_read(req
, force_nonblock
);
5421 case IORING_OP_WRITEV
:
5422 case IORING_OP_WRITE_FIXED
:
5423 case IORING_OP_WRITE
:
5425 ret
= io_write_prep(req
, sqe
, force_nonblock
);
5429 ret
= io_write(req
, force_nonblock
);
5431 case IORING_OP_FSYNC
:
5433 ret
= io_prep_fsync(req
, sqe
);
5437 ret
= io_fsync(req
, force_nonblock
);
5439 case IORING_OP_POLL_ADD
:
5441 ret
= io_poll_add_prep(req
, sqe
);
5445 ret
= io_poll_add(req
);
5447 case IORING_OP_POLL_REMOVE
:
5449 ret
= io_poll_remove_prep(req
, sqe
);
5453 ret
= io_poll_remove(req
);
5455 case IORING_OP_SYNC_FILE_RANGE
:
5457 ret
= io_prep_sfr(req
, sqe
);
5461 ret
= io_sync_file_range(req
, force_nonblock
);
5463 case IORING_OP_SENDMSG
:
5464 case IORING_OP_SEND
:
5466 ret
= io_sendmsg_prep(req
, sqe
);
5470 if (req
->opcode
== IORING_OP_SENDMSG
)
5471 ret
= io_sendmsg(req
, force_nonblock
, cs
);
5473 ret
= io_send(req
, force_nonblock
, cs
);
5475 case IORING_OP_RECVMSG
:
5476 case IORING_OP_RECV
:
5478 ret
= io_recvmsg_prep(req
, sqe
);
5482 if (req
->opcode
== IORING_OP_RECVMSG
)
5483 ret
= io_recvmsg(req
, force_nonblock
, cs
);
5485 ret
= io_recv(req
, force_nonblock
, cs
);
5487 case IORING_OP_TIMEOUT
:
5489 ret
= io_timeout_prep(req
, sqe
, false);
5493 ret
= io_timeout(req
);
5495 case IORING_OP_TIMEOUT_REMOVE
:
5497 ret
= io_timeout_remove_prep(req
, sqe
);
5501 ret
= io_timeout_remove(req
);
5503 case IORING_OP_ACCEPT
:
5505 ret
= io_accept_prep(req
, sqe
);
5509 ret
= io_accept(req
, force_nonblock
, cs
);
5511 case IORING_OP_CONNECT
:
5513 ret
= io_connect_prep(req
, sqe
);
5517 ret
= io_connect(req
, force_nonblock
, cs
);
5519 case IORING_OP_ASYNC_CANCEL
:
5521 ret
= io_async_cancel_prep(req
, sqe
);
5525 ret
= io_async_cancel(req
);
5527 case IORING_OP_FALLOCATE
:
5529 ret
= io_fallocate_prep(req
, sqe
);
5533 ret
= io_fallocate(req
, force_nonblock
);
5535 case IORING_OP_OPENAT
:
5537 ret
= io_openat_prep(req
, sqe
);
5541 ret
= io_openat(req
, force_nonblock
);
5543 case IORING_OP_CLOSE
:
5545 ret
= io_close_prep(req
, sqe
);
5549 ret
= io_close(req
, force_nonblock
, cs
);
5551 case IORING_OP_FILES_UPDATE
:
5553 ret
= io_files_update_prep(req
, sqe
);
5557 ret
= io_files_update(req
, force_nonblock
, cs
);
5559 case IORING_OP_STATX
:
5561 ret
= io_statx_prep(req
, sqe
);
5565 ret
= io_statx(req
, force_nonblock
);
5567 case IORING_OP_FADVISE
:
5569 ret
= io_fadvise_prep(req
, sqe
);
5573 ret
= io_fadvise(req
, force_nonblock
);
5575 case IORING_OP_MADVISE
:
5577 ret
= io_madvise_prep(req
, sqe
);
5581 ret
= io_madvise(req
, force_nonblock
);
5583 case IORING_OP_OPENAT2
:
5585 ret
= io_openat2_prep(req
, sqe
);
5589 ret
= io_openat2(req
, force_nonblock
);
5591 case IORING_OP_EPOLL_CTL
:
5593 ret
= io_epoll_ctl_prep(req
, sqe
);
5597 ret
= io_epoll_ctl(req
, force_nonblock
, cs
);
5599 case IORING_OP_SPLICE
:
5601 ret
= io_splice_prep(req
, sqe
);
5605 ret
= io_splice(req
, force_nonblock
);
5607 case IORING_OP_PROVIDE_BUFFERS
:
5609 ret
= io_provide_buffers_prep(req
, sqe
);
5613 ret
= io_provide_buffers(req
, force_nonblock
, cs
);
5615 case IORING_OP_REMOVE_BUFFERS
:
5617 ret
= io_remove_buffers_prep(req
, sqe
);
5621 ret
= io_remove_buffers(req
, force_nonblock
, cs
);
5625 ret
= io_tee_prep(req
, sqe
);
5629 ret
= io_tee(req
, force_nonblock
);
5639 /* If the op doesn't have a file, we're not polling for it */
5640 if ((ctx
->flags
& IORING_SETUP_IOPOLL
) && req
->file
) {
5641 const bool in_async
= io_wq_current_is_worker();
5643 if (req
->result
== -EAGAIN
)
5646 /* workqueue context doesn't hold uring_lock, grab it now */
5648 mutex_lock(&ctx
->uring_lock
);
5650 io_iopoll_req_issued(req
);
5653 mutex_unlock(&ctx
->uring_lock
);
5659 static void io_arm_async_linked_timeout(struct io_kiocb
*req
)
5661 struct io_kiocb
*link
;
5663 /* link head's timeout is queued in io_queue_async_work() */
5664 if (!(req
->flags
& REQ_F_QUEUE_TIMEOUT
))
5667 link
= list_first_entry(&req
->link_list
, struct io_kiocb
, link_list
);
5668 io_queue_linked_timeout(link
);
5671 static void io_wq_submit_work(struct io_wq_work
**workptr
)
5673 struct io_wq_work
*work
= *workptr
;
5674 struct io_kiocb
*req
= container_of(work
, struct io_kiocb
, work
);
5677 io_arm_async_linked_timeout(req
);
5679 /* if NO_CANCEL is set, we must still run the work */
5680 if ((work
->flags
& (IO_WQ_WORK_CANCEL
|IO_WQ_WORK_NO_CANCEL
)) ==
5681 IO_WQ_WORK_CANCEL
) {
5687 ret
= io_issue_sqe(req
, NULL
, false, NULL
);
5689 * We can get EAGAIN for polled IO even though we're
5690 * forcing a sync submission from here, since we can't
5691 * wait for request slots on the block side.
5700 req_set_fail_links(req
);
5701 io_req_complete(req
, ret
);
5704 io_steal_work(req
, workptr
);
5707 static inline struct file
*io_file_from_index(struct io_ring_ctx
*ctx
,
5710 struct fixed_file_table
*table
;
5712 table
= &ctx
->file_data
->table
[index
>> IORING_FILE_TABLE_SHIFT
];
5713 return table
->files
[index
& IORING_FILE_TABLE_MASK
];
5716 static int io_file_get(struct io_submit_state
*state
, struct io_kiocb
*req
,
5717 int fd
, struct file
**out_file
, bool fixed
)
5719 struct io_ring_ctx
*ctx
= req
->ctx
;
5723 if (unlikely(!ctx
->file_data
||
5724 (unsigned) fd
>= ctx
->nr_user_files
))
5726 fd
= array_index_nospec(fd
, ctx
->nr_user_files
);
5727 file
= io_file_from_index(ctx
, fd
);
5729 req
->fixed_file_refs
= ctx
->file_data
->cur_refs
;
5730 percpu_ref_get(req
->fixed_file_refs
);
5733 trace_io_uring_file_get(ctx
, fd
);
5734 file
= __io_file_get(state
, fd
);
5737 if (file
|| io_op_defs
[req
->opcode
].needs_file_no_error
) {
5744 static int io_req_set_file(struct io_submit_state
*state
, struct io_kiocb
*req
,
5749 fixed
= (req
->flags
& REQ_F_FIXED_FILE
) != 0;
5750 if (unlikely(!fixed
&& io_async_submit(req
->ctx
)))
5753 return io_file_get(state
, req
, fd
, &req
->file
, fixed
);
5756 static int io_grab_files(struct io_kiocb
*req
)
5759 struct io_ring_ctx
*ctx
= req
->ctx
;
5761 if (req
->work
.files
|| (req
->flags
& REQ_F_NO_FILE_TABLE
))
5763 if (!ctx
->ring_file
)
5767 spin_lock_irq(&ctx
->inflight_lock
);
5769 * We use the f_ops->flush() handler to ensure that we can flush
5770 * out work accessing these files if the fd is closed. Check if
5771 * the fd has changed since we started down this path, and disallow
5772 * this operation if it has.
5774 if (fcheck(ctx
->ring_fd
) == ctx
->ring_file
) {
5775 list_add(&req
->inflight_entry
, &ctx
->inflight_list
);
5776 req
->flags
|= REQ_F_INFLIGHT
;
5777 req
->work
.files
= current
->files
;
5780 spin_unlock_irq(&ctx
->inflight_lock
);
5786 static enum hrtimer_restart
io_link_timeout_fn(struct hrtimer
*timer
)
5788 struct io_timeout_data
*data
= container_of(timer
,
5789 struct io_timeout_data
, timer
);
5790 struct io_kiocb
*req
= data
->req
;
5791 struct io_ring_ctx
*ctx
= req
->ctx
;
5792 struct io_kiocb
*prev
= NULL
;
5793 unsigned long flags
;
5795 spin_lock_irqsave(&ctx
->completion_lock
, flags
);
5798 * We don't expect the list to be empty, that will only happen if we
5799 * race with the completion of the linked work.
5801 if (!list_empty(&req
->link_list
)) {
5802 prev
= list_entry(req
->link_list
.prev
, struct io_kiocb
,
5804 if (refcount_inc_not_zero(&prev
->refs
)) {
5805 list_del_init(&req
->link_list
);
5806 prev
->flags
&= ~REQ_F_LINK_TIMEOUT
;
5811 spin_unlock_irqrestore(&ctx
->completion_lock
, flags
);
5814 req_set_fail_links(prev
);
5815 io_async_find_and_cancel(ctx
, req
, prev
->user_data
, -ETIME
);
5818 io_req_complete(req
, -ETIME
);
5820 return HRTIMER_NORESTART
;
5823 static void io_queue_linked_timeout(struct io_kiocb
*req
)
5825 struct io_ring_ctx
*ctx
= req
->ctx
;
5828 * If the list is now empty, then our linked request finished before
5829 * we got a chance to setup the timer
5831 spin_lock_irq(&ctx
->completion_lock
);
5832 if (!list_empty(&req
->link_list
)) {
5833 struct io_timeout_data
*data
= &req
->io
->timeout
;
5835 data
->timer
.function
= io_link_timeout_fn
;
5836 hrtimer_start(&data
->timer
, timespec64_to_ktime(data
->ts
),
5839 spin_unlock_irq(&ctx
->completion_lock
);
5841 /* drop submission reference */
5845 static struct io_kiocb
*io_prep_linked_timeout(struct io_kiocb
*req
)
5847 struct io_kiocb
*nxt
;
5849 if (!(req
->flags
& REQ_F_LINK_HEAD
))
5851 /* for polled retry, if flag is set, we already went through here */
5852 if (req
->flags
& REQ_F_POLLED
)
5855 nxt
= list_first_entry_or_null(&req
->link_list
, struct io_kiocb
,
5857 if (!nxt
|| nxt
->opcode
!= IORING_OP_LINK_TIMEOUT
)
5860 req
->flags
|= REQ_F_LINK_TIMEOUT
;
5864 static void __io_queue_sqe(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
,
5865 struct io_comp_state
*cs
)
5867 struct io_kiocb
*linked_timeout
;
5868 struct io_kiocb
*nxt
;
5869 const struct cred
*old_creds
= NULL
;
5873 linked_timeout
= io_prep_linked_timeout(req
);
5875 if ((req
->flags
& REQ_F_WORK_INITIALIZED
) && req
->work
.creds
&&
5876 req
->work
.creds
!= current_cred()) {
5878 revert_creds(old_creds
);
5879 if (old_creds
== req
->work
.creds
)
5880 old_creds
= NULL
; /* restored original creds */
5882 old_creds
= override_creds(req
->work
.creds
);
5885 ret
= io_issue_sqe(req
, sqe
, true, cs
);
5888 * We async punt it if the file wasn't marked NOWAIT, or if the file
5889 * doesn't support non-blocking read/write attempts
5891 if (ret
== -EAGAIN
&& !(req
->flags
& REQ_F_NOWAIT
)) {
5892 if (io_arm_poll_handler(req
)) {
5894 io_queue_linked_timeout(linked_timeout
);
5898 io_req_init_async(req
);
5900 if (io_op_defs
[req
->opcode
].file_table
) {
5901 ret
= io_grab_files(req
);
5907 * Queued up for async execution, worker will release
5908 * submit reference when the iocb is actually submitted.
5910 io_queue_async_work(req
);
5916 /* drop submission reference */
5917 io_put_req_find_next(req
, &nxt
);
5919 if (linked_timeout
) {
5921 io_queue_linked_timeout(linked_timeout
);
5923 io_put_req(linked_timeout
);
5926 /* and drop final reference, if we failed */
5928 req_set_fail_links(req
);
5929 io_req_complete(req
, ret
);
5934 if (req
->flags
& REQ_F_FORCE_ASYNC
)
5940 revert_creds(old_creds
);
5943 static void io_queue_sqe(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
,
5944 struct io_comp_state
*cs
)
5948 ret
= io_req_defer(req
, sqe
);
5950 if (ret
!= -EIOCBQUEUED
) {
5952 req_set_fail_links(req
);
5954 io_req_complete(req
, ret
);
5956 } else if (req
->flags
& REQ_F_FORCE_ASYNC
) {
5959 if (io_alloc_async_ctx(req
))
5961 ret
= io_req_defer_prep(req
, sqe
);
5962 if (unlikely(ret
< 0))
5967 * Never try inline submit of IOSQE_ASYNC is set, go straight
5968 * to async execution.
5970 req
->work
.flags
|= IO_WQ_WORK_CONCURRENT
;
5971 io_queue_async_work(req
);
5973 __io_queue_sqe(req
, sqe
, cs
);
5977 static inline void io_queue_link_head(struct io_kiocb
*req
,
5978 struct io_comp_state
*cs
)
5980 if (unlikely(req
->flags
& REQ_F_FAIL_LINK
)) {
5982 io_req_complete(req
, -ECANCELED
);
5984 io_queue_sqe(req
, NULL
, cs
);
5987 static int io_submit_sqe(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
,
5988 struct io_kiocb
**link
, struct io_comp_state
*cs
)
5990 struct io_ring_ctx
*ctx
= req
->ctx
;
5994 * If we already have a head request, queue this one for async
5995 * submittal once the head completes. If we don't have a head but
5996 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
5997 * submitted sync once the chain is complete. If none of those
5998 * conditions are true (normal request), then just queue it.
6001 struct io_kiocb
*head
= *link
;
6004 * Taking sequential execution of a link, draining both sides
6005 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6006 * requests in the link. So, it drains the head and the
6007 * next after the link request. The last one is done via
6008 * drain_next flag to persist the effect across calls.
6010 if (req
->flags
& REQ_F_IO_DRAIN
) {
6011 head
->flags
|= REQ_F_IO_DRAIN
;
6012 ctx
->drain_next
= 1;
6014 if (io_alloc_async_ctx(req
))
6017 ret
= io_req_defer_prep(req
, sqe
);
6019 /* fail even hard links since we don't submit */
6020 head
->flags
|= REQ_F_FAIL_LINK
;
6023 trace_io_uring_link(ctx
, req
, head
);
6024 list_add_tail(&req
->link_list
, &head
->link_list
);
6026 /* last request of a link, enqueue the link */
6027 if (!(req
->flags
& (REQ_F_LINK
| REQ_F_HARDLINK
))) {
6028 io_queue_link_head(head
, cs
);
6032 if (unlikely(ctx
->drain_next
)) {
6033 req
->flags
|= REQ_F_IO_DRAIN
;
6034 ctx
->drain_next
= 0;
6036 if (req
->flags
& (REQ_F_LINK
| REQ_F_HARDLINK
)) {
6037 req
->flags
|= REQ_F_LINK_HEAD
;
6038 INIT_LIST_HEAD(&req
->link_list
);
6040 if (io_alloc_async_ctx(req
))
6043 ret
= io_req_defer_prep(req
, sqe
);
6045 req
->flags
|= REQ_F_FAIL_LINK
;
6048 io_queue_sqe(req
, sqe
, cs
);
6056 * Batched submission is done, ensure local IO is flushed out.
6058 static void io_submit_state_end(struct io_submit_state
*state
)
6060 if (!list_empty(&state
->comp
.list
))
6061 io_submit_flush_completions(&state
->comp
);
6062 blk_finish_plug(&state
->plug
);
6063 io_state_file_put(state
);
6064 if (state
->free_reqs
)
6065 kmem_cache_free_bulk(req_cachep
, state
->free_reqs
, state
->reqs
);
6069 * Start submission side cache.
6071 static void io_submit_state_start(struct io_submit_state
*state
,
6072 struct io_ring_ctx
*ctx
, unsigned int max_ios
)
6074 blk_start_plug(&state
->plug
);
6076 state
->plug
.nowait
= true;
6079 INIT_LIST_HEAD(&state
->comp
.list
);
6080 state
->comp
.ctx
= ctx
;
6081 state
->free_reqs
= 0;
6083 state
->ios_left
= max_ios
;
6086 static void io_commit_sqring(struct io_ring_ctx
*ctx
)
6088 struct io_rings
*rings
= ctx
->rings
;
6091 * Ensure any loads from the SQEs are done at this point,
6092 * since once we write the new head, the application could
6093 * write new data to them.
6095 smp_store_release(&rings
->sq
.head
, ctx
->cached_sq_head
);
6099 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
6100 * that is mapped by userspace. This means that care needs to be taken to
6101 * ensure that reads are stable, as we cannot rely on userspace always
6102 * being a good citizen. If members of the sqe are validated and then later
6103 * used, it's important that those reads are done through READ_ONCE() to
6104 * prevent a re-load down the line.
6106 static const struct io_uring_sqe
*io_get_sqe(struct io_ring_ctx
*ctx
)
6108 u32
*sq_array
= ctx
->sq_array
;
6112 * The cached sq head (or cq tail) serves two purposes:
6114 * 1) allows us to batch the cost of updating the user visible
6116 * 2) allows the kernel side to track the head on its own, even
6117 * though the application is the one updating it.
6119 head
= READ_ONCE(sq_array
[ctx
->cached_sq_head
& ctx
->sq_mask
]);
6120 if (likely(head
< ctx
->sq_entries
))
6121 return &ctx
->sq_sqes
[head
];
6123 /* drop invalid entries */
6124 ctx
->cached_sq_dropped
++;
6125 WRITE_ONCE(ctx
->rings
->sq_dropped
, ctx
->cached_sq_dropped
);
6129 static inline void io_consume_sqe(struct io_ring_ctx
*ctx
)
6131 ctx
->cached_sq_head
++;
6134 #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6135 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6136 IOSQE_BUFFER_SELECT)
6138 static int io_init_req(struct io_ring_ctx
*ctx
, struct io_kiocb
*req
,
6139 const struct io_uring_sqe
*sqe
,
6140 struct io_submit_state
*state
)
6142 unsigned int sqe_flags
;
6146 * All io need record the previous position, if LINK vs DARIN,
6147 * it can be used to mark the position of the first IO in the
6150 req
->sequence
= ctx
->cached_sq_head
- ctx
->cached_sq_dropped
;
6151 req
->opcode
= READ_ONCE(sqe
->opcode
);
6152 req
->user_data
= READ_ONCE(sqe
->user_data
);
6157 /* one is dropped after submission, the other at completion */
6158 refcount_set(&req
->refs
, 2);
6159 req
->task
= current
;
6162 if (unlikely(req
->opcode
>= IORING_OP_LAST
))
6165 if (unlikely(io_sq_thread_acquire_mm(ctx
, req
)))
6168 sqe_flags
= READ_ONCE(sqe
->flags
);
6169 /* enforce forwards compatibility on users */
6170 if (unlikely(sqe_flags
& ~SQE_VALID_FLAGS
))
6173 if ((sqe_flags
& IOSQE_BUFFER_SELECT
) &&
6174 !io_op_defs
[req
->opcode
].buffer_select
)
6177 id
= READ_ONCE(sqe
->personality
);
6179 io_req_init_async(req
);
6180 req
->work
.creds
= idr_find(&ctx
->personality_idr
, id
);
6181 if (unlikely(!req
->work
.creds
))
6183 get_cred(req
->work
.creds
);
6186 /* same numerical values with corresponding REQ_F_*, safe to copy */
6187 req
->flags
|= sqe_flags
;
6189 if (!io_op_defs
[req
->opcode
].needs_file
)
6192 return io_req_set_file(state
, req
, READ_ONCE(sqe
->fd
));
6195 static int io_submit_sqes(struct io_ring_ctx
*ctx
, unsigned int nr
,
6196 struct file
*ring_file
, int ring_fd
)
6198 struct io_submit_state state
;
6199 struct io_kiocb
*link
= NULL
;
6200 int i
, submitted
= 0;
6202 /* if we have a backlog and couldn't flush it all, return BUSY */
6203 if (test_bit(0, &ctx
->sq_check_overflow
)) {
6204 if (!list_empty(&ctx
->cq_overflow_list
) &&
6205 !io_cqring_overflow_flush(ctx
, false))
6209 /* make sure SQ entry isn't read before tail */
6210 nr
= min3(nr
, ctx
->sq_entries
, io_sqring_entries(ctx
));
6212 if (!percpu_ref_tryget_many(&ctx
->refs
, nr
))
6215 io_submit_state_start(&state
, ctx
, nr
);
6217 ctx
->ring_fd
= ring_fd
;
6218 ctx
->ring_file
= ring_file
;
6220 for (i
= 0; i
< nr
; i
++) {
6221 const struct io_uring_sqe
*sqe
;
6222 struct io_kiocb
*req
;
6225 sqe
= io_get_sqe(ctx
);
6226 if (unlikely(!sqe
)) {
6227 io_consume_sqe(ctx
);
6230 req
= io_alloc_req(ctx
, &state
);
6231 if (unlikely(!req
)) {
6233 submitted
= -EAGAIN
;
6237 err
= io_init_req(ctx
, req
, sqe
, &state
);
6238 io_consume_sqe(ctx
);
6239 /* will complete beyond this point, count as submitted */
6242 if (unlikely(err
)) {
6245 io_req_complete(req
, err
);
6249 trace_io_uring_submit_sqe(ctx
, req
->opcode
, req
->user_data
,
6250 true, io_async_submit(ctx
));
6251 err
= io_submit_sqe(req
, sqe
, &link
, &state
.comp
);
6256 if (unlikely(submitted
!= nr
)) {
6257 int ref_used
= (submitted
== -EAGAIN
) ? 0 : submitted
;
6259 percpu_ref_put_many(&ctx
->refs
, nr
- ref_used
);
6262 io_queue_link_head(link
, &state
.comp
);
6263 io_submit_state_end(&state
);
6265 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6266 io_commit_sqring(ctx
);
6271 static int io_sq_thread(void *data
)
6273 struct io_ring_ctx
*ctx
= data
;
6274 const struct cred
*old_cred
;
6276 unsigned long timeout
;
6279 complete(&ctx
->sq_thread_comp
);
6281 old_cred
= override_creds(ctx
->creds
);
6283 timeout
= jiffies
+ ctx
->sq_thread_idle
;
6284 while (!kthread_should_park()) {
6285 unsigned int to_submit
;
6287 if (!list_empty(&ctx
->poll_list
)) {
6288 unsigned nr_events
= 0;
6290 mutex_lock(&ctx
->uring_lock
);
6291 if (!list_empty(&ctx
->poll_list
))
6292 io_iopoll_getevents(ctx
, &nr_events
, 0);
6294 timeout
= jiffies
+ ctx
->sq_thread_idle
;
6295 mutex_unlock(&ctx
->uring_lock
);
6298 to_submit
= io_sqring_entries(ctx
);
6301 * If submit got -EBUSY, flag us as needing the application
6302 * to enter the kernel to reap and flush events.
6304 if (!to_submit
|| ret
== -EBUSY
) {
6306 * Drop cur_mm before scheduling, we can't hold it for
6307 * long periods (or over schedule()). Do this before
6308 * adding ourselves to the waitqueue, as the unuse/drop
6311 io_sq_thread_drop_mm(ctx
);
6314 * We're polling. If we're within the defined idle
6315 * period, then let us spin without work before going
6316 * to sleep. The exception is if we got EBUSY doing
6317 * more IO, we should wait for the application to
6318 * reap events and wake us up.
6320 if (!list_empty(&ctx
->poll_list
) ||
6321 (!time_after(jiffies
, timeout
) && ret
!= -EBUSY
&&
6322 !percpu_ref_is_dying(&ctx
->refs
))) {
6323 if (current
->task_works
)
6329 prepare_to_wait(&ctx
->sqo_wait
, &wait
,
6330 TASK_INTERRUPTIBLE
);
6333 * While doing polled IO, before going to sleep, we need
6334 * to check if there are new reqs added to poll_list, it
6335 * is because reqs may have been punted to io worker and
6336 * will be added to poll_list later, hence check the
6339 if ((ctx
->flags
& IORING_SETUP_IOPOLL
) &&
6340 !list_empty_careful(&ctx
->poll_list
)) {
6341 finish_wait(&ctx
->sqo_wait
, &wait
);
6345 /* Tell userspace we may need a wakeup call */
6346 ctx
->rings
->sq_flags
|= IORING_SQ_NEED_WAKEUP
;
6347 /* make sure to read SQ tail after writing flags */
6350 to_submit
= io_sqring_entries(ctx
);
6351 if (!to_submit
|| ret
== -EBUSY
) {
6352 if (kthread_should_park()) {
6353 finish_wait(&ctx
->sqo_wait
, &wait
);
6356 if (current
->task_works
) {
6358 finish_wait(&ctx
->sqo_wait
, &wait
);
6361 if (signal_pending(current
))
6362 flush_signals(current
);
6364 finish_wait(&ctx
->sqo_wait
, &wait
);
6366 ctx
->rings
->sq_flags
&= ~IORING_SQ_NEED_WAKEUP
;
6370 finish_wait(&ctx
->sqo_wait
, &wait
);
6372 ctx
->rings
->sq_flags
&= ~IORING_SQ_NEED_WAKEUP
;
6375 mutex_lock(&ctx
->uring_lock
);
6376 if (likely(!percpu_ref_is_dying(&ctx
->refs
)))
6377 ret
= io_submit_sqes(ctx
, to_submit
, NULL
, -1);
6378 mutex_unlock(&ctx
->uring_lock
);
6379 timeout
= jiffies
+ ctx
->sq_thread_idle
;
6382 if (current
->task_works
)
6385 io_sq_thread_drop_mm(ctx
);
6386 revert_creds(old_cred
);
6393 struct io_wait_queue
{
6394 struct wait_queue_entry wq
;
6395 struct io_ring_ctx
*ctx
;
6397 unsigned nr_timeouts
;
6400 static inline bool io_should_wake(struct io_wait_queue
*iowq
, bool noflush
)
6402 struct io_ring_ctx
*ctx
= iowq
->ctx
;
6405 * Wake up if we have enough events, or if a timeout occurred since we
6406 * started waiting. For timeouts, we always want to return to userspace,
6407 * regardless of event count.
6409 return io_cqring_events(ctx
, noflush
) >= iowq
->to_wait
||
6410 atomic_read(&ctx
->cq_timeouts
) != iowq
->nr_timeouts
;
6413 static int io_wake_function(struct wait_queue_entry
*curr
, unsigned int mode
,
6414 int wake_flags
, void *key
)
6416 struct io_wait_queue
*iowq
= container_of(curr
, struct io_wait_queue
,
6419 /* use noflush == true, as we can't safely rely on locking context */
6420 if (!io_should_wake(iowq
, true))
6423 return autoremove_wake_function(curr
, mode
, wake_flags
, key
);
6427 * Wait until events become available, if we don't already have some. The
6428 * application must reap them itself, as they reside on the shared cq ring.
6430 static int io_cqring_wait(struct io_ring_ctx
*ctx
, int min_events
,
6431 const sigset_t __user
*sig
, size_t sigsz
)
6433 struct io_wait_queue iowq
= {
6436 .func
= io_wake_function
,
6437 .entry
= LIST_HEAD_INIT(iowq
.wq
.entry
),
6440 .to_wait
= min_events
,
6442 struct io_rings
*rings
= ctx
->rings
;
6446 if (io_cqring_events(ctx
, false) >= min_events
)
6448 if (!current
->task_works
)
6454 #ifdef CONFIG_COMPAT
6455 if (in_compat_syscall())
6456 ret
= set_compat_user_sigmask((const compat_sigset_t __user
*)sig
,
6460 ret
= set_user_sigmask(sig
, sigsz
);
6466 iowq
.nr_timeouts
= atomic_read(&ctx
->cq_timeouts
);
6467 trace_io_uring_cqring_wait(ctx
, min_events
);
6469 prepare_to_wait_exclusive(&ctx
->wait
, &iowq
.wq
,
6470 TASK_INTERRUPTIBLE
);
6471 if (current
->task_works
)
6473 if (io_should_wake(&iowq
, false))
6476 if (signal_pending(current
)) {
6481 finish_wait(&ctx
->wait
, &iowq
.wq
);
6483 restore_saved_sigmask_unless(ret
== -EINTR
);
6485 return READ_ONCE(rings
->cq
.head
) == READ_ONCE(rings
->cq
.tail
) ? ret
: 0;
6488 static void __io_sqe_files_unregister(struct io_ring_ctx
*ctx
)
6490 #if defined(CONFIG_UNIX)
6491 if (ctx
->ring_sock
) {
6492 struct sock
*sock
= ctx
->ring_sock
->sk
;
6493 struct sk_buff
*skb
;
6495 while ((skb
= skb_dequeue(&sock
->sk_receive_queue
)) != NULL
)
6501 for (i
= 0; i
< ctx
->nr_user_files
; i
++) {
6504 file
= io_file_from_index(ctx
, i
);
6511 static void io_file_ref_kill(struct percpu_ref
*ref
)
6513 struct fixed_file_data
*data
;
6515 data
= container_of(ref
, struct fixed_file_data
, refs
);
6516 complete(&data
->done
);
6519 static int io_sqe_files_unregister(struct io_ring_ctx
*ctx
)
6521 struct fixed_file_data
*data
= ctx
->file_data
;
6522 struct fixed_file_ref_node
*ref_node
= NULL
;
6523 unsigned nr_tables
, i
;
6528 spin_lock(&data
->lock
);
6529 if (!list_empty(&data
->ref_list
))
6530 ref_node
= list_first_entry(&data
->ref_list
,
6531 struct fixed_file_ref_node
, node
);
6532 spin_unlock(&data
->lock
);
6534 percpu_ref_kill(&ref_node
->refs
);
6536 percpu_ref_kill(&data
->refs
);
6538 /* wait for all refs nodes to complete */
6539 flush_delayed_work(&ctx
->file_put_work
);
6540 wait_for_completion(&data
->done
);
6542 __io_sqe_files_unregister(ctx
);
6543 nr_tables
= DIV_ROUND_UP(ctx
->nr_user_files
, IORING_MAX_FILES_TABLE
);
6544 for (i
= 0; i
< nr_tables
; i
++)
6545 kfree(data
->table
[i
].files
);
6547 percpu_ref_exit(&data
->refs
);
6549 ctx
->file_data
= NULL
;
6550 ctx
->nr_user_files
= 0;
6554 static void io_sq_thread_stop(struct io_ring_ctx
*ctx
)
6556 if (ctx
->sqo_thread
) {
6557 wait_for_completion(&ctx
->sq_thread_comp
);
6559 * The park is a bit of a work-around, without it we get
6560 * warning spews on shutdown with SQPOLL set and affinity
6561 * set to a single CPU.
6563 kthread_park(ctx
->sqo_thread
);
6564 kthread_stop(ctx
->sqo_thread
);
6565 ctx
->sqo_thread
= NULL
;
6569 static void io_finish_async(struct io_ring_ctx
*ctx
)
6571 io_sq_thread_stop(ctx
);
6574 io_wq_destroy(ctx
->io_wq
);
6579 #if defined(CONFIG_UNIX)
6581 * Ensure the UNIX gc is aware of our file set, so we are certain that
6582 * the io_uring can be safely unregistered on process exit, even if we have
6583 * loops in the file referencing.
6585 static int __io_sqe_files_scm(struct io_ring_ctx
*ctx
, int nr
, int offset
)
6587 struct sock
*sk
= ctx
->ring_sock
->sk
;
6588 struct scm_fp_list
*fpl
;
6589 struct sk_buff
*skb
;
6592 fpl
= kzalloc(sizeof(*fpl
), GFP_KERNEL
);
6596 skb
= alloc_skb(0, GFP_KERNEL
);
6605 fpl
->user
= get_uid(ctx
->user
);
6606 for (i
= 0; i
< nr
; i
++) {
6607 struct file
*file
= io_file_from_index(ctx
, i
+ offset
);
6611 fpl
->fp
[nr_files
] = get_file(file
);
6612 unix_inflight(fpl
->user
, fpl
->fp
[nr_files
]);
6617 fpl
->max
= SCM_MAX_FD
;
6618 fpl
->count
= nr_files
;
6619 UNIXCB(skb
).fp
= fpl
;
6620 skb
->destructor
= unix_destruct_scm
;
6621 refcount_add(skb
->truesize
, &sk
->sk_wmem_alloc
);
6622 skb_queue_head(&sk
->sk_receive_queue
, skb
);
6624 for (i
= 0; i
< nr_files
; i
++)
6635 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
6636 * causes regular reference counting to break down. We rely on the UNIX
6637 * garbage collection to take care of this problem for us.
6639 static int io_sqe_files_scm(struct io_ring_ctx
*ctx
)
6641 unsigned left
, total
;
6645 left
= ctx
->nr_user_files
;
6647 unsigned this_files
= min_t(unsigned, left
, SCM_MAX_FD
);
6649 ret
= __io_sqe_files_scm(ctx
, this_files
, total
);
6653 total
+= this_files
;
6659 while (total
< ctx
->nr_user_files
) {
6660 struct file
*file
= io_file_from_index(ctx
, total
);
6670 static int io_sqe_files_scm(struct io_ring_ctx
*ctx
)
6676 static int io_sqe_alloc_file_tables(struct io_ring_ctx
*ctx
, unsigned nr_tables
,
6681 for (i
= 0; i
< nr_tables
; i
++) {
6682 struct fixed_file_table
*table
= &ctx
->file_data
->table
[i
];
6683 unsigned this_files
;
6685 this_files
= min(nr_files
, IORING_MAX_FILES_TABLE
);
6686 table
->files
= kcalloc(this_files
, sizeof(struct file
*),
6690 nr_files
-= this_files
;
6696 for (i
= 0; i
< nr_tables
; i
++) {
6697 struct fixed_file_table
*table
= &ctx
->file_data
->table
[i
];
6698 kfree(table
->files
);
6703 static void io_ring_file_put(struct io_ring_ctx
*ctx
, struct file
*file
)
6705 #if defined(CONFIG_UNIX)
6706 struct sock
*sock
= ctx
->ring_sock
->sk
;
6707 struct sk_buff_head list
, *head
= &sock
->sk_receive_queue
;
6708 struct sk_buff
*skb
;
6711 __skb_queue_head_init(&list
);
6714 * Find the skb that holds this file in its SCM_RIGHTS. When found,
6715 * remove this entry and rearrange the file array.
6717 skb
= skb_dequeue(head
);
6719 struct scm_fp_list
*fp
;
6721 fp
= UNIXCB(skb
).fp
;
6722 for (i
= 0; i
< fp
->count
; i
++) {
6725 if (fp
->fp
[i
] != file
)
6728 unix_notinflight(fp
->user
, fp
->fp
[i
]);
6729 left
= fp
->count
- 1 - i
;
6731 memmove(&fp
->fp
[i
], &fp
->fp
[i
+ 1],
6732 left
* sizeof(struct file
*));
6739 __skb_queue_tail(&list
, skb
);
6749 __skb_queue_tail(&list
, skb
);
6751 skb
= skb_dequeue(head
);
6754 if (skb_peek(&list
)) {
6755 spin_lock_irq(&head
->lock
);
6756 while ((skb
= __skb_dequeue(&list
)) != NULL
)
6757 __skb_queue_tail(head
, skb
);
6758 spin_unlock_irq(&head
->lock
);
6765 struct io_file_put
{
6766 struct list_head list
;
6770 static void __io_file_put_work(struct fixed_file_ref_node
*ref_node
)
6772 struct fixed_file_data
*file_data
= ref_node
->file_data
;
6773 struct io_ring_ctx
*ctx
= file_data
->ctx
;
6774 struct io_file_put
*pfile
, *tmp
;
6776 list_for_each_entry_safe(pfile
, tmp
, &ref_node
->file_list
, list
) {
6777 list_del(&pfile
->list
);
6778 io_ring_file_put(ctx
, pfile
->file
);
6782 spin_lock(&file_data
->lock
);
6783 list_del(&ref_node
->node
);
6784 spin_unlock(&file_data
->lock
);
6786 percpu_ref_exit(&ref_node
->refs
);
6788 percpu_ref_put(&file_data
->refs
);
6791 static void io_file_put_work(struct work_struct
*work
)
6793 struct io_ring_ctx
*ctx
;
6794 struct llist_node
*node
;
6796 ctx
= container_of(work
, struct io_ring_ctx
, file_put_work
.work
);
6797 node
= llist_del_all(&ctx
->file_put_llist
);
6800 struct fixed_file_ref_node
*ref_node
;
6801 struct llist_node
*next
= node
->next
;
6803 ref_node
= llist_entry(node
, struct fixed_file_ref_node
, llist
);
6804 __io_file_put_work(ref_node
);
6809 static void io_file_data_ref_zero(struct percpu_ref
*ref
)
6811 struct fixed_file_ref_node
*ref_node
;
6812 struct io_ring_ctx
*ctx
;
6816 ref_node
= container_of(ref
, struct fixed_file_ref_node
, refs
);
6817 ctx
= ref_node
->file_data
->ctx
;
6819 if (percpu_ref_is_dying(&ctx
->file_data
->refs
))
6822 first_add
= llist_add(&ref_node
->llist
, &ctx
->file_put_llist
);
6824 mod_delayed_work(system_wq
, &ctx
->file_put_work
, 0);
6826 queue_delayed_work(system_wq
, &ctx
->file_put_work
, delay
);
6829 static struct fixed_file_ref_node
*alloc_fixed_file_ref_node(
6830 struct io_ring_ctx
*ctx
)
6832 struct fixed_file_ref_node
*ref_node
;
6834 ref_node
= kzalloc(sizeof(*ref_node
), GFP_KERNEL
);
6836 return ERR_PTR(-ENOMEM
);
6838 if (percpu_ref_init(&ref_node
->refs
, io_file_data_ref_zero
,
6841 return ERR_PTR(-ENOMEM
);
6843 INIT_LIST_HEAD(&ref_node
->node
);
6844 INIT_LIST_HEAD(&ref_node
->file_list
);
6845 ref_node
->file_data
= ctx
->file_data
;
6849 static void destroy_fixed_file_ref_node(struct fixed_file_ref_node
*ref_node
)
6851 percpu_ref_exit(&ref_node
->refs
);
6855 static int io_sqe_files_register(struct io_ring_ctx
*ctx
, void __user
*arg
,
6858 __s32 __user
*fds
= (__s32 __user
*) arg
;
6863 struct fixed_file_ref_node
*ref_node
;
6869 if (nr_args
> IORING_MAX_FIXED_FILES
)
6872 ctx
->file_data
= kzalloc(sizeof(*ctx
->file_data
), GFP_KERNEL
);
6873 if (!ctx
->file_data
)
6875 ctx
->file_data
->ctx
= ctx
;
6876 init_completion(&ctx
->file_data
->done
);
6877 INIT_LIST_HEAD(&ctx
->file_data
->ref_list
);
6878 spin_lock_init(&ctx
->file_data
->lock
);
6880 nr_tables
= DIV_ROUND_UP(nr_args
, IORING_MAX_FILES_TABLE
);
6881 ctx
->file_data
->table
= kcalloc(nr_tables
,
6882 sizeof(struct fixed_file_table
),
6884 if (!ctx
->file_data
->table
) {
6885 kfree(ctx
->file_data
);
6886 ctx
->file_data
= NULL
;
6890 if (percpu_ref_init(&ctx
->file_data
->refs
, io_file_ref_kill
,
6891 PERCPU_REF_ALLOW_REINIT
, GFP_KERNEL
)) {
6892 kfree(ctx
->file_data
->table
);
6893 kfree(ctx
->file_data
);
6894 ctx
->file_data
= NULL
;
6898 if (io_sqe_alloc_file_tables(ctx
, nr_tables
, nr_args
)) {
6899 percpu_ref_exit(&ctx
->file_data
->refs
);
6900 kfree(ctx
->file_data
->table
);
6901 kfree(ctx
->file_data
);
6902 ctx
->file_data
= NULL
;
6906 for (i
= 0; i
< nr_args
; i
++, ctx
->nr_user_files
++) {
6907 struct fixed_file_table
*table
;
6911 if (copy_from_user(&fd
, &fds
[i
], sizeof(fd
)))
6913 /* allow sparse sets */
6919 table
= &ctx
->file_data
->table
[i
>> IORING_FILE_TABLE_SHIFT
];
6920 index
= i
& IORING_FILE_TABLE_MASK
;
6928 * Don't allow io_uring instances to be registered. If UNIX
6929 * isn't enabled, then this causes a reference cycle and this
6930 * instance can never get freed. If UNIX is enabled we'll
6931 * handle it just fine, but there's still no point in allowing
6932 * a ring fd as it doesn't support regular read/write anyway.
6934 if (file
->f_op
== &io_uring_fops
) {
6939 table
->files
[index
] = file
;
6943 for (i
= 0; i
< ctx
->nr_user_files
; i
++) {
6944 file
= io_file_from_index(ctx
, i
);
6948 for (i
= 0; i
< nr_tables
; i
++)
6949 kfree(ctx
->file_data
->table
[i
].files
);
6951 kfree(ctx
->file_data
->table
);
6952 kfree(ctx
->file_data
);
6953 ctx
->file_data
= NULL
;
6954 ctx
->nr_user_files
= 0;
6958 ret
= io_sqe_files_scm(ctx
);
6960 io_sqe_files_unregister(ctx
);
6964 ref_node
= alloc_fixed_file_ref_node(ctx
);
6965 if (IS_ERR(ref_node
)) {
6966 io_sqe_files_unregister(ctx
);
6967 return PTR_ERR(ref_node
);
6970 ctx
->file_data
->cur_refs
= &ref_node
->refs
;
6971 spin_lock(&ctx
->file_data
->lock
);
6972 list_add(&ref_node
->node
, &ctx
->file_data
->ref_list
);
6973 spin_unlock(&ctx
->file_data
->lock
);
6974 percpu_ref_get(&ctx
->file_data
->refs
);
6978 static int io_sqe_file_register(struct io_ring_ctx
*ctx
, struct file
*file
,
6981 #if defined(CONFIG_UNIX)
6982 struct sock
*sock
= ctx
->ring_sock
->sk
;
6983 struct sk_buff_head
*head
= &sock
->sk_receive_queue
;
6984 struct sk_buff
*skb
;
6987 * See if we can merge this file into an existing skb SCM_RIGHTS
6988 * file set. If there's no room, fall back to allocating a new skb
6989 * and filling it in.
6991 spin_lock_irq(&head
->lock
);
6992 skb
= skb_peek(head
);
6994 struct scm_fp_list
*fpl
= UNIXCB(skb
).fp
;
6996 if (fpl
->count
< SCM_MAX_FD
) {
6997 __skb_unlink(skb
, head
);
6998 spin_unlock_irq(&head
->lock
);
6999 fpl
->fp
[fpl
->count
] = get_file(file
);
7000 unix_inflight(fpl
->user
, fpl
->fp
[fpl
->count
]);
7002 spin_lock_irq(&head
->lock
);
7003 __skb_queue_head(head
, skb
);
7008 spin_unlock_irq(&head
->lock
);
7015 return __io_sqe_files_scm(ctx
, 1, index
);
7021 static int io_queue_file_removal(struct fixed_file_data
*data
,
7024 struct io_file_put
*pfile
;
7025 struct percpu_ref
*refs
= data
->cur_refs
;
7026 struct fixed_file_ref_node
*ref_node
;
7028 pfile
= kzalloc(sizeof(*pfile
), GFP_KERNEL
);
7032 ref_node
= container_of(refs
, struct fixed_file_ref_node
, refs
);
7034 list_add(&pfile
->list
, &ref_node
->file_list
);
7039 static int __io_sqe_files_update(struct io_ring_ctx
*ctx
,
7040 struct io_uring_files_update
*up
,
7043 struct fixed_file_data
*data
= ctx
->file_data
;
7044 struct fixed_file_ref_node
*ref_node
;
7049 bool needs_switch
= false;
7051 if (check_add_overflow(up
->offset
, nr_args
, &done
))
7053 if (done
> ctx
->nr_user_files
)
7056 ref_node
= alloc_fixed_file_ref_node(ctx
);
7057 if (IS_ERR(ref_node
))
7058 return PTR_ERR(ref_node
);
7061 fds
= u64_to_user_ptr(up
->fds
);
7063 struct fixed_file_table
*table
;
7067 if (copy_from_user(&fd
, &fds
[done
], sizeof(fd
))) {
7071 i
= array_index_nospec(up
->offset
, ctx
->nr_user_files
);
7072 table
= &ctx
->file_data
->table
[i
>> IORING_FILE_TABLE_SHIFT
];
7073 index
= i
& IORING_FILE_TABLE_MASK
;
7074 if (table
->files
[index
]) {
7075 file
= io_file_from_index(ctx
, index
);
7076 err
= io_queue_file_removal(data
, file
);
7079 table
->files
[index
] = NULL
;
7080 needs_switch
= true;
7089 * Don't allow io_uring instances to be registered. If
7090 * UNIX isn't enabled, then this causes a reference
7091 * cycle and this instance can never get freed. If UNIX
7092 * is enabled we'll handle it just fine, but there's
7093 * still no point in allowing a ring fd as it doesn't
7094 * support regular read/write anyway.
7096 if (file
->f_op
== &io_uring_fops
) {
7101 table
->files
[index
] = file
;
7102 err
= io_sqe_file_register(ctx
, file
, i
);
7112 percpu_ref_kill(data
->cur_refs
);
7113 spin_lock(&data
->lock
);
7114 list_add(&ref_node
->node
, &data
->ref_list
);
7115 data
->cur_refs
= &ref_node
->refs
;
7116 spin_unlock(&data
->lock
);
7117 percpu_ref_get(&ctx
->file_data
->refs
);
7119 destroy_fixed_file_ref_node(ref_node
);
7121 return done
? done
: err
;
7124 static int io_sqe_files_update(struct io_ring_ctx
*ctx
, void __user
*arg
,
7127 struct io_uring_files_update up
;
7129 if (!ctx
->file_data
)
7133 if (copy_from_user(&up
, arg
, sizeof(up
)))
7138 return __io_sqe_files_update(ctx
, &up
, nr_args
);
7141 static void io_free_work(struct io_wq_work
*work
)
7143 struct io_kiocb
*req
= container_of(work
, struct io_kiocb
, work
);
7145 /* Consider that io_steal_work() relies on this ref */
7149 static int io_init_wq_offload(struct io_ring_ctx
*ctx
,
7150 struct io_uring_params
*p
)
7152 struct io_wq_data data
;
7154 struct io_ring_ctx
*ctx_attach
;
7155 unsigned int concurrency
;
7158 data
.user
= ctx
->user
;
7159 data
.free_work
= io_free_work
;
7160 data
.do_work
= io_wq_submit_work
;
7162 if (!(p
->flags
& IORING_SETUP_ATTACH_WQ
)) {
7163 /* Do QD, or 4 * CPUS, whatever is smallest */
7164 concurrency
= min(ctx
->sq_entries
, 4 * num_online_cpus());
7166 ctx
->io_wq
= io_wq_create(concurrency
, &data
);
7167 if (IS_ERR(ctx
->io_wq
)) {
7168 ret
= PTR_ERR(ctx
->io_wq
);
7174 f
= fdget(p
->wq_fd
);
7178 if (f
.file
->f_op
!= &io_uring_fops
) {
7183 ctx_attach
= f
.file
->private_data
;
7184 /* @io_wq is protected by holding the fd */
7185 if (!io_wq_get(ctx_attach
->io_wq
, &data
)) {
7190 ctx
->io_wq
= ctx_attach
->io_wq
;
7196 static int io_sq_offload_start(struct io_ring_ctx
*ctx
,
7197 struct io_uring_params
*p
)
7201 mmgrab(current
->mm
);
7202 ctx
->sqo_mm
= current
->mm
;
7204 if (ctx
->flags
& IORING_SETUP_SQPOLL
) {
7206 if (!capable(CAP_SYS_ADMIN
))
7209 ctx
->sq_thread_idle
= msecs_to_jiffies(p
->sq_thread_idle
);
7210 if (!ctx
->sq_thread_idle
)
7211 ctx
->sq_thread_idle
= HZ
;
7213 if (p
->flags
& IORING_SETUP_SQ_AFF
) {
7214 int cpu
= p
->sq_thread_cpu
;
7217 if (cpu
>= nr_cpu_ids
)
7219 if (!cpu_online(cpu
))
7222 ctx
->sqo_thread
= kthread_create_on_cpu(io_sq_thread
,
7226 ctx
->sqo_thread
= kthread_create(io_sq_thread
, ctx
,
7229 if (IS_ERR(ctx
->sqo_thread
)) {
7230 ret
= PTR_ERR(ctx
->sqo_thread
);
7231 ctx
->sqo_thread
= NULL
;
7234 wake_up_process(ctx
->sqo_thread
);
7235 } else if (p
->flags
& IORING_SETUP_SQ_AFF
) {
7236 /* Can't have SQ_AFF without SQPOLL */
7241 ret
= io_init_wq_offload(ctx
, p
);
7247 io_finish_async(ctx
);
7248 mmdrop(ctx
->sqo_mm
);
7253 static inline void __io_unaccount_mem(struct user_struct
*user
,
7254 unsigned long nr_pages
)
7256 atomic_long_sub(nr_pages
, &user
->locked_vm
);
7259 static inline int __io_account_mem(struct user_struct
*user
,
7260 unsigned long nr_pages
)
7262 unsigned long page_limit
, cur_pages
, new_pages
;
7264 /* Don't allow more pages than we can safely lock */
7265 page_limit
= rlimit(RLIMIT_MEMLOCK
) >> PAGE_SHIFT
;
7268 cur_pages
= atomic_long_read(&user
->locked_vm
);
7269 new_pages
= cur_pages
+ nr_pages
;
7270 if (new_pages
> page_limit
)
7272 } while (atomic_long_cmpxchg(&user
->locked_vm
, cur_pages
,
7273 new_pages
) != cur_pages
);
7278 static void io_unaccount_mem(struct io_ring_ctx
*ctx
, unsigned long nr_pages
,
7279 enum io_mem_account acct
)
7282 __io_unaccount_mem(ctx
->user
, nr_pages
);
7285 if (acct
== ACCT_LOCKED
)
7286 ctx
->sqo_mm
->locked_vm
-= nr_pages
;
7287 else if (acct
== ACCT_PINNED
)
7288 atomic64_sub(nr_pages
, &ctx
->sqo_mm
->pinned_vm
);
7292 static int io_account_mem(struct io_ring_ctx
*ctx
, unsigned long nr_pages
,
7293 enum io_mem_account acct
)
7297 if (ctx
->limit_mem
) {
7298 ret
= __io_account_mem(ctx
->user
, nr_pages
);
7304 if (acct
== ACCT_LOCKED
)
7305 ctx
->sqo_mm
->locked_vm
+= nr_pages
;
7306 else if (acct
== ACCT_PINNED
)
7307 atomic64_add(nr_pages
, &ctx
->sqo_mm
->pinned_vm
);
7313 static void io_mem_free(void *ptr
)
7320 page
= virt_to_head_page(ptr
);
7321 if (put_page_testzero(page
))
7322 free_compound_page(page
);
7325 static void *io_mem_alloc(size_t size
)
7327 gfp_t gfp_flags
= GFP_KERNEL
| __GFP_ZERO
| __GFP_NOWARN
| __GFP_COMP
|
7330 return (void *) __get_free_pages(gfp_flags
, get_order(size
));
7333 static unsigned long rings_size(unsigned sq_entries
, unsigned cq_entries
,
7336 struct io_rings
*rings
;
7337 size_t off
, sq_array_size
;
7339 off
= struct_size(rings
, cqes
, cq_entries
);
7340 if (off
== SIZE_MAX
)
7344 off
= ALIGN(off
, SMP_CACHE_BYTES
);
7349 sq_array_size
= array_size(sizeof(u32
), sq_entries
);
7350 if (sq_array_size
== SIZE_MAX
)
7353 if (check_add_overflow(off
, sq_array_size
, &off
))
7362 static unsigned long ring_pages(unsigned sq_entries
, unsigned cq_entries
)
7366 pages
= (size_t)1 << get_order(
7367 rings_size(sq_entries
, cq_entries
, NULL
));
7368 pages
+= (size_t)1 << get_order(
7369 array_size(sizeof(struct io_uring_sqe
), sq_entries
));
7374 static int io_sqe_buffer_unregister(struct io_ring_ctx
*ctx
)
7378 if (!ctx
->user_bufs
)
7381 for (i
= 0; i
< ctx
->nr_user_bufs
; i
++) {
7382 struct io_mapped_ubuf
*imu
= &ctx
->user_bufs
[i
];
7384 for (j
= 0; j
< imu
->nr_bvecs
; j
++)
7385 unpin_user_page(imu
->bvec
[j
].bv_page
);
7387 io_unaccount_mem(ctx
, imu
->nr_bvecs
, ACCT_PINNED
);
7392 kfree(ctx
->user_bufs
);
7393 ctx
->user_bufs
= NULL
;
7394 ctx
->nr_user_bufs
= 0;
7398 static int io_copy_iov(struct io_ring_ctx
*ctx
, struct iovec
*dst
,
7399 void __user
*arg
, unsigned index
)
7401 struct iovec __user
*src
;
7403 #ifdef CONFIG_COMPAT
7405 struct compat_iovec __user
*ciovs
;
7406 struct compat_iovec ciov
;
7408 ciovs
= (struct compat_iovec __user
*) arg
;
7409 if (copy_from_user(&ciov
, &ciovs
[index
], sizeof(ciov
)))
7412 dst
->iov_base
= u64_to_user_ptr((u64
)ciov
.iov_base
);
7413 dst
->iov_len
= ciov
.iov_len
;
7417 src
= (struct iovec __user
*) arg
;
7418 if (copy_from_user(dst
, &src
[index
], sizeof(*dst
)))
7423 static int io_sqe_buffer_register(struct io_ring_ctx
*ctx
, void __user
*arg
,
7426 struct vm_area_struct
**vmas
= NULL
;
7427 struct page
**pages
= NULL
;
7428 int i
, j
, got_pages
= 0;
7433 if (!nr_args
|| nr_args
> UIO_MAXIOV
)
7436 ctx
->user_bufs
= kcalloc(nr_args
, sizeof(struct io_mapped_ubuf
),
7438 if (!ctx
->user_bufs
)
7441 for (i
= 0; i
< nr_args
; i
++) {
7442 struct io_mapped_ubuf
*imu
= &ctx
->user_bufs
[i
];
7443 unsigned long off
, start
, end
, ubuf
;
7448 ret
= io_copy_iov(ctx
, &iov
, arg
, i
);
7453 * Don't impose further limits on the size and buffer
7454 * constraints here, we'll -EINVAL later when IO is
7455 * submitted if they are wrong.
7458 if (!iov
.iov_base
|| !iov
.iov_len
)
7461 /* arbitrary limit, but we need something */
7462 if (iov
.iov_len
> SZ_1G
)
7465 ubuf
= (unsigned long) iov
.iov_base
;
7466 end
= (ubuf
+ iov
.iov_len
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
7467 start
= ubuf
>> PAGE_SHIFT
;
7468 nr_pages
= end
- start
;
7470 ret
= io_account_mem(ctx
, nr_pages
, ACCT_PINNED
);
7475 if (!pages
|| nr_pages
> got_pages
) {
7478 pages
= kvmalloc_array(nr_pages
, sizeof(struct page
*),
7480 vmas
= kvmalloc_array(nr_pages
,
7481 sizeof(struct vm_area_struct
*),
7483 if (!pages
|| !vmas
) {
7485 io_unaccount_mem(ctx
, nr_pages
, ACCT_PINNED
);
7488 got_pages
= nr_pages
;
7491 imu
->bvec
= kvmalloc_array(nr_pages
, sizeof(struct bio_vec
),
7495 io_unaccount_mem(ctx
, nr_pages
, ACCT_PINNED
);
7500 mmap_read_lock(current
->mm
);
7501 pret
= pin_user_pages(ubuf
, nr_pages
,
7502 FOLL_WRITE
| FOLL_LONGTERM
,
7504 if (pret
== nr_pages
) {
7505 /* don't support file backed memory */
7506 for (j
= 0; j
< nr_pages
; j
++) {
7507 struct vm_area_struct
*vma
= vmas
[j
];
7510 !is_file_hugepages(vma
->vm_file
)) {
7516 ret
= pret
< 0 ? pret
: -EFAULT
;
7518 mmap_read_unlock(current
->mm
);
7521 * if we did partial map, or found file backed vmas,
7522 * release any pages we did get
7525 unpin_user_pages(pages
, pret
);
7526 io_unaccount_mem(ctx
, nr_pages
, ACCT_PINNED
);
7531 off
= ubuf
& ~PAGE_MASK
;
7533 for (j
= 0; j
< nr_pages
; j
++) {
7536 vec_len
= min_t(size_t, size
, PAGE_SIZE
- off
);
7537 imu
->bvec
[j
].bv_page
= pages
[j
];
7538 imu
->bvec
[j
].bv_len
= vec_len
;
7539 imu
->bvec
[j
].bv_offset
= off
;
7543 /* store original address for later verification */
7545 imu
->len
= iov
.iov_len
;
7546 imu
->nr_bvecs
= nr_pages
;
7548 ctx
->nr_user_bufs
++;
7556 io_sqe_buffer_unregister(ctx
);
7560 static int io_eventfd_register(struct io_ring_ctx
*ctx
, void __user
*arg
)
7562 __s32 __user
*fds
= arg
;
7568 if (copy_from_user(&fd
, fds
, sizeof(*fds
)))
7571 ctx
->cq_ev_fd
= eventfd_ctx_fdget(fd
);
7572 if (IS_ERR(ctx
->cq_ev_fd
)) {
7573 int ret
= PTR_ERR(ctx
->cq_ev_fd
);
7574 ctx
->cq_ev_fd
= NULL
;
7581 static int io_eventfd_unregister(struct io_ring_ctx
*ctx
)
7583 if (ctx
->cq_ev_fd
) {
7584 eventfd_ctx_put(ctx
->cq_ev_fd
);
7585 ctx
->cq_ev_fd
= NULL
;
7592 static int __io_destroy_buffers(int id
, void *p
, void *data
)
7594 struct io_ring_ctx
*ctx
= data
;
7595 struct io_buffer
*buf
= p
;
7597 __io_remove_buffers(ctx
, buf
, id
, -1U);
7601 static void io_destroy_buffers(struct io_ring_ctx
*ctx
)
7603 idr_for_each(&ctx
->io_buffer_idr
, __io_destroy_buffers
, ctx
);
7604 idr_destroy(&ctx
->io_buffer_idr
);
7607 static void io_ring_ctx_free(struct io_ring_ctx
*ctx
)
7609 io_finish_async(ctx
);
7611 mmdrop(ctx
->sqo_mm
);
7615 io_iopoll_reap_events(ctx
);
7616 io_sqe_buffer_unregister(ctx
);
7617 io_sqe_files_unregister(ctx
);
7618 io_eventfd_unregister(ctx
);
7619 io_destroy_buffers(ctx
);
7620 idr_destroy(&ctx
->personality_idr
);
7622 #if defined(CONFIG_UNIX)
7623 if (ctx
->ring_sock
) {
7624 ctx
->ring_sock
->file
= NULL
; /* so that iput() is called */
7625 sock_release(ctx
->ring_sock
);
7629 io_mem_free(ctx
->rings
);
7630 io_mem_free(ctx
->sq_sqes
);
7632 percpu_ref_exit(&ctx
->refs
);
7633 io_unaccount_mem(ctx
, ring_pages(ctx
->sq_entries
, ctx
->cq_entries
),
7635 free_uid(ctx
->user
);
7636 put_cred(ctx
->creds
);
7637 kfree(ctx
->cancel_hash
);
7638 kmem_cache_free(req_cachep
, ctx
->fallback_req
);
7642 static __poll_t
io_uring_poll(struct file
*file
, poll_table
*wait
)
7644 struct io_ring_ctx
*ctx
= file
->private_data
;
7647 poll_wait(file
, &ctx
->cq_wait
, wait
);
7649 * synchronizes with barrier from wq_has_sleeper call in
7653 if (READ_ONCE(ctx
->rings
->sq
.tail
) - ctx
->cached_sq_head
!=
7654 ctx
->rings
->sq_ring_entries
)
7655 mask
|= EPOLLOUT
| EPOLLWRNORM
;
7656 if (io_cqring_events(ctx
, false))
7657 mask
|= EPOLLIN
| EPOLLRDNORM
;
7662 static int io_uring_fasync(int fd
, struct file
*file
, int on
)
7664 struct io_ring_ctx
*ctx
= file
->private_data
;
7666 return fasync_helper(fd
, file
, on
, &ctx
->cq_fasync
);
7669 static int io_remove_personalities(int id
, void *p
, void *data
)
7671 struct io_ring_ctx
*ctx
= data
;
7672 const struct cred
*cred
;
7674 cred
= idr_remove(&ctx
->personality_idr
, id
);
7680 static void io_ring_exit_work(struct work_struct
*work
)
7682 struct io_ring_ctx
*ctx
;
7684 ctx
= container_of(work
, struct io_ring_ctx
, exit_work
);
7686 io_cqring_overflow_flush(ctx
, true);
7689 * If we're doing polled IO and end up having requests being
7690 * submitted async (out-of-line), then completions can come in while
7691 * we're waiting for refs to drop. We need to reap these manually,
7692 * as nobody else will be looking for them.
7694 while (!wait_for_completion_timeout(&ctx
->ref_comp
, HZ
/20)) {
7695 io_iopoll_reap_events(ctx
);
7697 io_cqring_overflow_flush(ctx
, true);
7699 io_ring_ctx_free(ctx
);
7702 static void io_ring_ctx_wait_and_kill(struct io_ring_ctx
*ctx
)
7704 mutex_lock(&ctx
->uring_lock
);
7705 percpu_ref_kill(&ctx
->refs
);
7706 mutex_unlock(&ctx
->uring_lock
);
7708 io_kill_timeouts(ctx
);
7709 io_poll_remove_all(ctx
);
7712 io_wq_cancel_all(ctx
->io_wq
);
7714 io_iopoll_reap_events(ctx
);
7715 /* if we failed setting up the ctx, we might not have any rings */
7717 io_cqring_overflow_flush(ctx
, true);
7718 idr_for_each(&ctx
->personality_idr
, io_remove_personalities
, ctx
);
7719 INIT_WORK(&ctx
->exit_work
, io_ring_exit_work
);
7720 queue_work(system_wq
, &ctx
->exit_work
);
7723 static int io_uring_release(struct inode
*inode
, struct file
*file
)
7725 struct io_ring_ctx
*ctx
= file
->private_data
;
7727 file
->private_data
= NULL
;
7728 io_ring_ctx_wait_and_kill(ctx
);
7732 static bool io_wq_files_match(struct io_wq_work
*work
, void *data
)
7734 struct files_struct
*files
= data
;
7736 return work
->files
== files
;
7739 static void io_uring_cancel_files(struct io_ring_ctx
*ctx
,
7740 struct files_struct
*files
)
7742 if (list_empty_careful(&ctx
->inflight_list
))
7745 /* cancel all at once, should be faster than doing it one by one*/
7746 io_wq_cancel_cb(ctx
->io_wq
, io_wq_files_match
, files
, true);
7748 while (!list_empty_careful(&ctx
->inflight_list
)) {
7749 struct io_kiocb
*cancel_req
= NULL
, *req
;
7752 spin_lock_irq(&ctx
->inflight_lock
);
7753 list_for_each_entry(req
, &ctx
->inflight_list
, inflight_entry
) {
7754 if (req
->work
.files
!= files
)
7756 /* req is being completed, ignore */
7757 if (!refcount_inc_not_zero(&req
->refs
))
7763 prepare_to_wait(&ctx
->inflight_wait
, &wait
,
7764 TASK_UNINTERRUPTIBLE
);
7765 spin_unlock_irq(&ctx
->inflight_lock
);
7767 /* We need to keep going until we don't find a matching req */
7771 if (cancel_req
->flags
& REQ_F_OVERFLOW
) {
7772 spin_lock_irq(&ctx
->completion_lock
);
7773 list_del(&cancel_req
->list
);
7774 cancel_req
->flags
&= ~REQ_F_OVERFLOW
;
7775 if (list_empty(&ctx
->cq_overflow_list
)) {
7776 clear_bit(0, &ctx
->sq_check_overflow
);
7777 clear_bit(0, &ctx
->cq_check_overflow
);
7779 spin_unlock_irq(&ctx
->completion_lock
);
7781 WRITE_ONCE(ctx
->rings
->cq_overflow
,
7782 atomic_inc_return(&ctx
->cached_cq_overflow
));
7785 * Put inflight ref and overflow ref. If that's
7786 * all we had, then we're done with this request.
7788 if (refcount_sub_and_test(2, &cancel_req
->refs
)) {
7789 io_free_req(cancel_req
);
7790 finish_wait(&ctx
->inflight_wait
, &wait
);
7794 io_wq_cancel_work(ctx
->io_wq
, &cancel_req
->work
);
7795 io_put_req(cancel_req
);
7799 finish_wait(&ctx
->inflight_wait
, &wait
);
7803 static bool io_cancel_task_cb(struct io_wq_work
*work
, void *data
)
7805 struct io_kiocb
*req
= container_of(work
, struct io_kiocb
, work
);
7806 struct task_struct
*task
= data
;
7808 return req
->task
== task
;
7811 static int io_uring_flush(struct file
*file
, void *data
)
7813 struct io_ring_ctx
*ctx
= file
->private_data
;
7815 io_uring_cancel_files(ctx
, data
);
7818 * If the task is going away, cancel work it may have pending
7820 if (fatal_signal_pending(current
) || (current
->flags
& PF_EXITING
))
7821 io_wq_cancel_cb(ctx
->io_wq
, io_cancel_task_cb
, current
, true);
7826 static void *io_uring_validate_mmap_request(struct file
*file
,
7827 loff_t pgoff
, size_t sz
)
7829 struct io_ring_ctx
*ctx
= file
->private_data
;
7830 loff_t offset
= pgoff
<< PAGE_SHIFT
;
7835 case IORING_OFF_SQ_RING
:
7836 case IORING_OFF_CQ_RING
:
7839 case IORING_OFF_SQES
:
7843 return ERR_PTR(-EINVAL
);
7846 page
= virt_to_head_page(ptr
);
7847 if (sz
> page_size(page
))
7848 return ERR_PTR(-EINVAL
);
7855 static int io_uring_mmap(struct file
*file
, struct vm_area_struct
*vma
)
7857 size_t sz
= vma
->vm_end
- vma
->vm_start
;
7861 ptr
= io_uring_validate_mmap_request(file
, vma
->vm_pgoff
, sz
);
7863 return PTR_ERR(ptr
);
7865 pfn
= virt_to_phys(ptr
) >> PAGE_SHIFT
;
7866 return remap_pfn_range(vma
, vma
->vm_start
, pfn
, sz
, vma
->vm_page_prot
);
7869 #else /* !CONFIG_MMU */
7871 static int io_uring_mmap(struct file
*file
, struct vm_area_struct
*vma
)
7873 return vma
->vm_flags
& (VM_SHARED
| VM_MAYSHARE
) ? 0 : -EINVAL
;
7876 static unsigned int io_uring_nommu_mmap_capabilities(struct file
*file
)
7878 return NOMMU_MAP_DIRECT
| NOMMU_MAP_READ
| NOMMU_MAP_WRITE
;
7881 static unsigned long io_uring_nommu_get_unmapped_area(struct file
*file
,
7882 unsigned long addr
, unsigned long len
,
7883 unsigned long pgoff
, unsigned long flags
)
7887 ptr
= io_uring_validate_mmap_request(file
, pgoff
, len
);
7889 return PTR_ERR(ptr
);
7891 return (unsigned long) ptr
;
7894 #endif /* !CONFIG_MMU */
7896 SYSCALL_DEFINE6(io_uring_enter
, unsigned int, fd
, u32
, to_submit
,
7897 u32
, min_complete
, u32
, flags
, const sigset_t __user
*, sig
,
7900 struct io_ring_ctx
*ctx
;
7905 if (current
->task_works
)
7908 if (flags
& ~(IORING_ENTER_GETEVENTS
| IORING_ENTER_SQ_WAKEUP
))
7916 if (f
.file
->f_op
!= &io_uring_fops
)
7920 ctx
= f
.file
->private_data
;
7921 if (!percpu_ref_tryget(&ctx
->refs
))
7925 * For SQ polling, the thread will do all submissions and completions.
7926 * Just return the requested submit count, and wake the thread if
7930 if (ctx
->flags
& IORING_SETUP_SQPOLL
) {
7931 if (!list_empty_careful(&ctx
->cq_overflow_list
))
7932 io_cqring_overflow_flush(ctx
, false);
7933 if (flags
& IORING_ENTER_SQ_WAKEUP
)
7934 wake_up(&ctx
->sqo_wait
);
7935 submitted
= to_submit
;
7936 } else if (to_submit
) {
7937 mutex_lock(&ctx
->uring_lock
);
7938 submitted
= io_submit_sqes(ctx
, to_submit
, f
.file
, fd
);
7939 mutex_unlock(&ctx
->uring_lock
);
7941 if (submitted
!= to_submit
)
7944 if (flags
& IORING_ENTER_GETEVENTS
) {
7945 unsigned nr_events
= 0;
7947 min_complete
= min(min_complete
, ctx
->cq_entries
);
7950 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
7951 * space applications don't need to do io completion events
7952 * polling again, they can rely on io_sq_thread to do polling
7953 * work, which can reduce cpu usage and uring_lock contention.
7955 if (ctx
->flags
& IORING_SETUP_IOPOLL
&&
7956 !(ctx
->flags
& IORING_SETUP_SQPOLL
)) {
7957 ret
= io_iopoll_check(ctx
, &nr_events
, min_complete
);
7959 ret
= io_cqring_wait(ctx
, min_complete
, sig
, sigsz
);
7964 percpu_ref_put(&ctx
->refs
);
7967 return submitted
? submitted
: ret
;
7970 #ifdef CONFIG_PROC_FS
7971 static int io_uring_show_cred(int id
, void *p
, void *data
)
7973 const struct cred
*cred
= p
;
7974 struct seq_file
*m
= data
;
7975 struct user_namespace
*uns
= seq_user_ns(m
);
7976 struct group_info
*gi
;
7981 seq_printf(m
, "%5d\n", id
);
7982 seq_put_decimal_ull(m
, "\tUid:\t", from_kuid_munged(uns
, cred
->uid
));
7983 seq_put_decimal_ull(m
, "\t\t", from_kuid_munged(uns
, cred
->euid
));
7984 seq_put_decimal_ull(m
, "\t\t", from_kuid_munged(uns
, cred
->suid
));
7985 seq_put_decimal_ull(m
, "\t\t", from_kuid_munged(uns
, cred
->fsuid
));
7986 seq_put_decimal_ull(m
, "\n\tGid:\t", from_kgid_munged(uns
, cred
->gid
));
7987 seq_put_decimal_ull(m
, "\t\t", from_kgid_munged(uns
, cred
->egid
));
7988 seq_put_decimal_ull(m
, "\t\t", from_kgid_munged(uns
, cred
->sgid
));
7989 seq_put_decimal_ull(m
, "\t\t", from_kgid_munged(uns
, cred
->fsgid
));
7990 seq_puts(m
, "\n\tGroups:\t");
7991 gi
= cred
->group_info
;
7992 for (g
= 0; g
< gi
->ngroups
; g
++) {
7993 seq_put_decimal_ull(m
, g
? " " : "",
7994 from_kgid_munged(uns
, gi
->gid
[g
]));
7996 seq_puts(m
, "\n\tCapEff:\t");
7997 cap
= cred
->cap_effective
;
7998 CAP_FOR_EACH_U32(__capi
)
7999 seq_put_hex_ll(m
, NULL
, cap
.cap
[CAP_LAST_U32
- __capi
], 8);
8004 static void __io_uring_show_fdinfo(struct io_ring_ctx
*ctx
, struct seq_file
*m
)
8008 mutex_lock(&ctx
->uring_lock
);
8009 seq_printf(m
, "UserFiles:\t%u\n", ctx
->nr_user_files
);
8010 for (i
= 0; i
< ctx
->nr_user_files
; i
++) {
8011 struct fixed_file_table
*table
;
8014 table
= &ctx
->file_data
->table
[i
>> IORING_FILE_TABLE_SHIFT
];
8015 f
= table
->files
[i
& IORING_FILE_TABLE_MASK
];
8017 seq_printf(m
, "%5u: %s\n", i
, file_dentry(f
)->d_iname
);
8019 seq_printf(m
, "%5u: <none>\n", i
);
8021 seq_printf(m
, "UserBufs:\t%u\n", ctx
->nr_user_bufs
);
8022 for (i
= 0; i
< ctx
->nr_user_bufs
; i
++) {
8023 struct io_mapped_ubuf
*buf
= &ctx
->user_bufs
[i
];
8025 seq_printf(m
, "%5u: 0x%llx/%u\n", i
, buf
->ubuf
,
8026 (unsigned int) buf
->len
);
8028 if (!idr_is_empty(&ctx
->personality_idr
)) {
8029 seq_printf(m
, "Personalities:\n");
8030 idr_for_each(&ctx
->personality_idr
, io_uring_show_cred
, m
);
8032 seq_printf(m
, "PollList:\n");
8033 spin_lock_irq(&ctx
->completion_lock
);
8034 for (i
= 0; i
< (1U << ctx
->cancel_hash_bits
); i
++) {
8035 struct hlist_head
*list
= &ctx
->cancel_hash
[i
];
8036 struct io_kiocb
*req
;
8038 hlist_for_each_entry(req
, list
, hash_node
)
8039 seq_printf(m
, " op=%d, task_works=%d\n", req
->opcode
,
8040 req
->task
->task_works
!= NULL
);
8042 spin_unlock_irq(&ctx
->completion_lock
);
8043 mutex_unlock(&ctx
->uring_lock
);
8046 static void io_uring_show_fdinfo(struct seq_file
*m
, struct file
*f
)
8048 struct io_ring_ctx
*ctx
= f
->private_data
;
8050 if (percpu_ref_tryget(&ctx
->refs
)) {
8051 __io_uring_show_fdinfo(ctx
, m
);
8052 percpu_ref_put(&ctx
->refs
);
8057 static const struct file_operations io_uring_fops
= {
8058 .release
= io_uring_release
,
8059 .flush
= io_uring_flush
,
8060 .mmap
= io_uring_mmap
,
8062 .get_unmapped_area
= io_uring_nommu_get_unmapped_area
,
8063 .mmap_capabilities
= io_uring_nommu_mmap_capabilities
,
8065 .poll
= io_uring_poll
,
8066 .fasync
= io_uring_fasync
,
8067 #ifdef CONFIG_PROC_FS
8068 .show_fdinfo
= io_uring_show_fdinfo
,
8072 static int io_allocate_scq_urings(struct io_ring_ctx
*ctx
,
8073 struct io_uring_params
*p
)
8075 struct io_rings
*rings
;
8076 size_t size
, sq_array_offset
;
8078 size
= rings_size(p
->sq_entries
, p
->cq_entries
, &sq_array_offset
);
8079 if (size
== SIZE_MAX
)
8082 rings
= io_mem_alloc(size
);
8087 ctx
->sq_array
= (u32
*)((char *)rings
+ sq_array_offset
);
8088 rings
->sq_ring_mask
= p
->sq_entries
- 1;
8089 rings
->cq_ring_mask
= p
->cq_entries
- 1;
8090 rings
->sq_ring_entries
= p
->sq_entries
;
8091 rings
->cq_ring_entries
= p
->cq_entries
;
8092 ctx
->sq_mask
= rings
->sq_ring_mask
;
8093 ctx
->cq_mask
= rings
->cq_ring_mask
;
8094 ctx
->sq_entries
= rings
->sq_ring_entries
;
8095 ctx
->cq_entries
= rings
->cq_ring_entries
;
8097 size
= array_size(sizeof(struct io_uring_sqe
), p
->sq_entries
);
8098 if (size
== SIZE_MAX
) {
8099 io_mem_free(ctx
->rings
);
8104 ctx
->sq_sqes
= io_mem_alloc(size
);
8105 if (!ctx
->sq_sqes
) {
8106 io_mem_free(ctx
->rings
);
8115 * Allocate an anonymous fd, this is what constitutes the application
8116 * visible backing of an io_uring instance. The application mmaps this
8117 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
8118 * we have to tie this fd to a socket for file garbage collection purposes.
8120 static int io_uring_get_fd(struct io_ring_ctx
*ctx
)
8125 #if defined(CONFIG_UNIX)
8126 ret
= sock_create_kern(&init_net
, PF_UNIX
, SOCK_RAW
, IPPROTO_IP
,
8132 ret
= get_unused_fd_flags(O_RDWR
| O_CLOEXEC
);
8136 file
= anon_inode_getfile("[io_uring]", &io_uring_fops
, ctx
,
8137 O_RDWR
| O_CLOEXEC
);
8140 ret
= PTR_ERR(file
);
8144 #if defined(CONFIG_UNIX)
8145 ctx
->ring_sock
->file
= file
;
8147 fd_install(ret
, file
);
8150 #if defined(CONFIG_UNIX)
8151 sock_release(ctx
->ring_sock
);
8152 ctx
->ring_sock
= NULL
;
8157 static int io_uring_create(unsigned entries
, struct io_uring_params
*p
,
8158 struct io_uring_params __user
*params
)
8160 struct user_struct
*user
= NULL
;
8161 struct io_ring_ctx
*ctx
;
8167 if (entries
> IORING_MAX_ENTRIES
) {
8168 if (!(p
->flags
& IORING_SETUP_CLAMP
))
8170 entries
= IORING_MAX_ENTRIES
;
8174 * Use twice as many entries for the CQ ring. It's possible for the
8175 * application to drive a higher depth than the size of the SQ ring,
8176 * since the sqes are only used at submission time. This allows for
8177 * some flexibility in overcommitting a bit. If the application has
8178 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
8179 * of CQ ring entries manually.
8181 p
->sq_entries
= roundup_pow_of_two(entries
);
8182 if (p
->flags
& IORING_SETUP_CQSIZE
) {
8184 * If IORING_SETUP_CQSIZE is set, we do the same roundup
8185 * to a power-of-two, if it isn't already. We do NOT impose
8186 * any cq vs sq ring sizing.
8188 if (p
->cq_entries
< p
->sq_entries
)
8190 if (p
->cq_entries
> IORING_MAX_CQ_ENTRIES
) {
8191 if (!(p
->flags
& IORING_SETUP_CLAMP
))
8193 p
->cq_entries
= IORING_MAX_CQ_ENTRIES
;
8195 p
->cq_entries
= roundup_pow_of_two(p
->cq_entries
);
8197 p
->cq_entries
= 2 * p
->sq_entries
;
8200 user
= get_uid(current_user());
8201 limit_mem
= !capable(CAP_IPC_LOCK
);
8204 ret
= __io_account_mem(user
,
8205 ring_pages(p
->sq_entries
, p
->cq_entries
));
8212 ctx
= io_ring_ctx_alloc(p
);
8215 __io_unaccount_mem(user
, ring_pages(p
->sq_entries
,
8220 ctx
->compat
= in_compat_syscall();
8222 ctx
->creds
= get_current_cred();
8224 ret
= io_allocate_scq_urings(ctx
, p
);
8228 ret
= io_sq_offload_start(ctx
, p
);
8232 memset(&p
->sq_off
, 0, sizeof(p
->sq_off
));
8233 p
->sq_off
.head
= offsetof(struct io_rings
, sq
.head
);
8234 p
->sq_off
.tail
= offsetof(struct io_rings
, sq
.tail
);
8235 p
->sq_off
.ring_mask
= offsetof(struct io_rings
, sq_ring_mask
);
8236 p
->sq_off
.ring_entries
= offsetof(struct io_rings
, sq_ring_entries
);
8237 p
->sq_off
.flags
= offsetof(struct io_rings
, sq_flags
);
8238 p
->sq_off
.dropped
= offsetof(struct io_rings
, sq_dropped
);
8239 p
->sq_off
.array
= (char *)ctx
->sq_array
- (char *)ctx
->rings
;
8241 memset(&p
->cq_off
, 0, sizeof(p
->cq_off
));
8242 p
->cq_off
.head
= offsetof(struct io_rings
, cq
.head
);
8243 p
->cq_off
.tail
= offsetof(struct io_rings
, cq
.tail
);
8244 p
->cq_off
.ring_mask
= offsetof(struct io_rings
, cq_ring_mask
);
8245 p
->cq_off
.ring_entries
= offsetof(struct io_rings
, cq_ring_entries
);
8246 p
->cq_off
.overflow
= offsetof(struct io_rings
, cq_overflow
);
8247 p
->cq_off
.cqes
= offsetof(struct io_rings
, cqes
);
8248 p
->cq_off
.flags
= offsetof(struct io_rings
, cq_flags
);
8250 p
->features
= IORING_FEAT_SINGLE_MMAP
| IORING_FEAT_NODROP
|
8251 IORING_FEAT_SUBMIT_STABLE
| IORING_FEAT_RW_CUR_POS
|
8252 IORING_FEAT_CUR_PERSONALITY
| IORING_FEAT_FAST_POLL
|
8253 IORING_FEAT_POLL_32BITS
;
8255 if (copy_to_user(params
, p
, sizeof(*p
))) {
8260 * Install ring fd as the very last thing, so we don't risk someone
8261 * having closed it before we finish setup
8263 ret
= io_uring_get_fd(ctx
);
8267 trace_io_uring_create(ret
, ctx
, p
->sq_entries
, p
->cq_entries
, p
->flags
);
8268 io_account_mem(ctx
, ring_pages(p
->sq_entries
, p
->cq_entries
),
8270 ctx
->limit_mem
= limit_mem
;
8273 io_ring_ctx_wait_and_kill(ctx
);
8278 * Sets up an aio uring context, and returns the fd. Applications asks for a
8279 * ring size, we return the actual sq/cq ring sizes (among other things) in the
8280 * params structure passed in.
8282 static long io_uring_setup(u32 entries
, struct io_uring_params __user
*params
)
8284 struct io_uring_params p
;
8287 if (copy_from_user(&p
, params
, sizeof(p
)))
8289 for (i
= 0; i
< ARRAY_SIZE(p
.resv
); i
++) {
8294 if (p
.flags
& ~(IORING_SETUP_IOPOLL
| IORING_SETUP_SQPOLL
|
8295 IORING_SETUP_SQ_AFF
| IORING_SETUP_CQSIZE
|
8296 IORING_SETUP_CLAMP
| IORING_SETUP_ATTACH_WQ
))
8299 return io_uring_create(entries
, &p
, params
);
8302 SYSCALL_DEFINE2(io_uring_setup
, u32
, entries
,
8303 struct io_uring_params __user
*, params
)
8305 return io_uring_setup(entries
, params
);
8308 static int io_probe(struct io_ring_ctx
*ctx
, void __user
*arg
, unsigned nr_args
)
8310 struct io_uring_probe
*p
;
8314 size
= struct_size(p
, ops
, nr_args
);
8315 if (size
== SIZE_MAX
)
8317 p
= kzalloc(size
, GFP_KERNEL
);
8322 if (copy_from_user(p
, arg
, size
))
8325 if (memchr_inv(p
, 0, size
))
8328 p
->last_op
= IORING_OP_LAST
- 1;
8329 if (nr_args
> IORING_OP_LAST
)
8330 nr_args
= IORING_OP_LAST
;
8332 for (i
= 0; i
< nr_args
; i
++) {
8334 if (!io_op_defs
[i
].not_supported
)
8335 p
->ops
[i
].flags
= IO_URING_OP_SUPPORTED
;
8340 if (copy_to_user(arg
, p
, size
))
8347 static int io_register_personality(struct io_ring_ctx
*ctx
)
8349 const struct cred
*creds
= get_current_cred();
8352 id
= idr_alloc_cyclic(&ctx
->personality_idr
, (void *) creds
, 1,
8353 USHRT_MAX
, GFP_KERNEL
);
8359 static int io_unregister_personality(struct io_ring_ctx
*ctx
, unsigned id
)
8361 const struct cred
*old_creds
;
8363 old_creds
= idr_remove(&ctx
->personality_idr
, id
);
8365 put_cred(old_creds
);
8372 static bool io_register_op_must_quiesce(int op
)
8375 case IORING_UNREGISTER_FILES
:
8376 case IORING_REGISTER_FILES_UPDATE
:
8377 case IORING_REGISTER_PROBE
:
8378 case IORING_REGISTER_PERSONALITY
:
8379 case IORING_UNREGISTER_PERSONALITY
:
8386 static int __io_uring_register(struct io_ring_ctx
*ctx
, unsigned opcode
,
8387 void __user
*arg
, unsigned nr_args
)
8388 __releases(ctx
->uring_lock
)
8389 __acquires(ctx
->uring_lock
)
8394 * We're inside the ring mutex, if the ref is already dying, then
8395 * someone else killed the ctx or is already going through
8396 * io_uring_register().
8398 if (percpu_ref_is_dying(&ctx
->refs
))
8401 if (io_register_op_must_quiesce(opcode
)) {
8402 percpu_ref_kill(&ctx
->refs
);
8405 * Drop uring mutex before waiting for references to exit. If
8406 * another thread is currently inside io_uring_enter() it might
8407 * need to grab the uring_lock to make progress. If we hold it
8408 * here across the drain wait, then we can deadlock. It's safe
8409 * to drop the mutex here, since no new references will come in
8410 * after we've killed the percpu ref.
8412 mutex_unlock(&ctx
->uring_lock
);
8413 ret
= wait_for_completion_interruptible(&ctx
->ref_comp
);
8414 mutex_lock(&ctx
->uring_lock
);
8416 percpu_ref_resurrect(&ctx
->refs
);
8423 case IORING_REGISTER_BUFFERS
:
8424 ret
= io_sqe_buffer_register(ctx
, arg
, nr_args
);
8426 case IORING_UNREGISTER_BUFFERS
:
8430 ret
= io_sqe_buffer_unregister(ctx
);
8432 case IORING_REGISTER_FILES
:
8433 ret
= io_sqe_files_register(ctx
, arg
, nr_args
);
8435 case IORING_UNREGISTER_FILES
:
8439 ret
= io_sqe_files_unregister(ctx
);
8441 case IORING_REGISTER_FILES_UPDATE
:
8442 ret
= io_sqe_files_update(ctx
, arg
, nr_args
);
8444 case IORING_REGISTER_EVENTFD
:
8445 case IORING_REGISTER_EVENTFD_ASYNC
:
8449 ret
= io_eventfd_register(ctx
, arg
);
8452 if (opcode
== IORING_REGISTER_EVENTFD_ASYNC
)
8453 ctx
->eventfd_async
= 1;
8455 ctx
->eventfd_async
= 0;
8457 case IORING_UNREGISTER_EVENTFD
:
8461 ret
= io_eventfd_unregister(ctx
);
8463 case IORING_REGISTER_PROBE
:
8465 if (!arg
|| nr_args
> 256)
8467 ret
= io_probe(ctx
, arg
, nr_args
);
8469 case IORING_REGISTER_PERSONALITY
:
8473 ret
= io_register_personality(ctx
);
8475 case IORING_UNREGISTER_PERSONALITY
:
8479 ret
= io_unregister_personality(ctx
, nr_args
);
8486 if (io_register_op_must_quiesce(opcode
)) {
8487 /* bring the ctx back to life */
8488 percpu_ref_reinit(&ctx
->refs
);
8490 reinit_completion(&ctx
->ref_comp
);
8495 SYSCALL_DEFINE4(io_uring_register
, unsigned int, fd
, unsigned int, opcode
,
8496 void __user
*, arg
, unsigned int, nr_args
)
8498 struct io_ring_ctx
*ctx
;
8507 if (f
.file
->f_op
!= &io_uring_fops
)
8510 ctx
= f
.file
->private_data
;
8512 mutex_lock(&ctx
->uring_lock
);
8513 ret
= __io_uring_register(ctx
, opcode
, arg
, nr_args
);
8514 mutex_unlock(&ctx
->uring_lock
);
8515 trace_io_uring_register(ctx
, opcode
, ctx
->nr_user_files
, ctx
->nr_user_bufs
,
8516 ctx
->cq_ev_fd
!= NULL
, ret
);
8522 static int __init
io_uring_init(void)
8524 #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
8525 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
8526 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
8529 #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
8530 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
8531 BUILD_BUG_ON(sizeof(struct io_uring_sqe
) != 64);
8532 BUILD_BUG_SQE_ELEM(0, __u8
, opcode
);
8533 BUILD_BUG_SQE_ELEM(1, __u8
, flags
);
8534 BUILD_BUG_SQE_ELEM(2, __u16
, ioprio
);
8535 BUILD_BUG_SQE_ELEM(4, __s32
, fd
);
8536 BUILD_BUG_SQE_ELEM(8, __u64
, off
);
8537 BUILD_BUG_SQE_ELEM(8, __u64
, addr2
);
8538 BUILD_BUG_SQE_ELEM(16, __u64
, addr
);
8539 BUILD_BUG_SQE_ELEM(16, __u64
, splice_off_in
);
8540 BUILD_BUG_SQE_ELEM(24, __u32
, len
);
8541 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t
, rw_flags
);
8542 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags
);
8543 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32
, rw_flags
);
8544 BUILD_BUG_SQE_ELEM(28, __u32
, fsync_flags
);
8545 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16
, poll_events
);
8546 BUILD_BUG_SQE_ELEM(28, __u32
, poll32_events
);
8547 BUILD_BUG_SQE_ELEM(28, __u32
, sync_range_flags
);
8548 BUILD_BUG_SQE_ELEM(28, __u32
, msg_flags
);
8549 BUILD_BUG_SQE_ELEM(28, __u32
, timeout_flags
);
8550 BUILD_BUG_SQE_ELEM(28, __u32
, accept_flags
);
8551 BUILD_BUG_SQE_ELEM(28, __u32
, cancel_flags
);
8552 BUILD_BUG_SQE_ELEM(28, __u32
, open_flags
);
8553 BUILD_BUG_SQE_ELEM(28, __u32
, statx_flags
);
8554 BUILD_BUG_SQE_ELEM(28, __u32
, fadvise_advice
);
8555 BUILD_BUG_SQE_ELEM(28, __u32
, splice_flags
);
8556 BUILD_BUG_SQE_ELEM(32, __u64
, user_data
);
8557 BUILD_BUG_SQE_ELEM(40, __u16
, buf_index
);
8558 BUILD_BUG_SQE_ELEM(42, __u16
, personality
);
8559 BUILD_BUG_SQE_ELEM(44, __s32
, splice_fd_in
);
8561 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs
) != IORING_OP_LAST
);
8562 BUILD_BUG_ON(__REQ_F_LAST_BIT
>= 8 * sizeof(int));
8563 req_cachep
= KMEM_CACHE(io_kiocb
, SLAB_HWCACHE_ALIGN
| SLAB_PANIC
);
8566 __initcall(io_uring_init
);