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 <linux/refcount.h>
48 #include <linux/uio.h>
50 #include <linux/sched/signal.h>
52 #include <linux/file.h>
53 #include <linux/fdtable.h>
55 #include <linux/mman.h>
56 #include <linux/mmu_context.h>
57 #include <linux/percpu.h>
58 #include <linux/slab.h>
59 #include <linux/kthread.h>
60 #include <linux/blkdev.h>
61 #include <linux/bvec.h>
62 #include <linux/net.h>
64 #include <net/af_unix.h>
66 #include <linux/anon_inodes.h>
67 #include <linux/sched/mm.h>
68 #include <linux/uaccess.h>
69 #include <linux/nospec.h>
70 #include <linux/sizes.h>
71 #include <linux/hugetlb.h>
72 #include <linux/highmem.h>
73 #include <linux/namei.h>
74 #include <linux/fsnotify.h>
75 #include <linux/fadvise.h>
77 #define CREATE_TRACE_POINTS
78 #include <trace/events/io_uring.h>
80 #include <uapi/linux/io_uring.h>
85 #define IORING_MAX_ENTRIES 32768
86 #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
89 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
91 #define IORING_FILE_TABLE_SHIFT 9
92 #define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
93 #define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
94 #define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
97 u32 head ____cacheline_aligned_in_smp;
98 u32 tail ____cacheline_aligned_in_smp;
102 * This data is shared with the application through the mmap at offsets
103 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
105 * The offsets to the member fields are published through struct
106 * io_sqring_offsets when calling io_uring_setup.
110 * Head and tail offsets into the ring; the offsets need to be
111 * masked to get valid indices.
113 * The kernel controls head of the sq ring and the tail of the cq ring,
114 * and the application controls tail of the sq ring and the head of the
117 struct io_uring sq, cq;
119 * Bitmasks to apply to head and tail offsets (constant, equals
122 u32 sq_ring_mask, cq_ring_mask;
123 /* Ring sizes (constant, power of 2) */
124 u32 sq_ring_entries, cq_ring_entries;
126 * Number of invalid entries dropped by the kernel due to
127 * invalid index stored in array
129 * Written by the kernel, shouldn't be modified by the
130 * application (i.e. get number of "new events" by comparing to
133 * After a new SQ head value was read by the application this
134 * counter includes all submissions that were dropped reaching
135 * the new SQ head (and possibly more).
141 * Written by the kernel, shouldn't be modified by the
144 * The application needs a full memory barrier before checking
145 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
149 * Number of completion events lost because the queue was full;
150 * this should be avoided by the application by making sure
151 * there are not more requests pending than there is space in
152 * the completion queue.
154 * Written by the kernel, shouldn't be modified by the
155 * application (i.e. get number of "new events" by comparing to
158 * As completion events come in out of order this counter is not
159 * ordered with any other data.
163 * Ring buffer of completion events.
165 * The kernel writes completion events fresh every time they are
166 * produced, so the application is allowed to modify pending
169 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
172 struct io_mapped_ubuf {
175 struct bio_vec *bvec;
176 unsigned int nr_bvecs;
179 struct fixed_file_table {
187 struct fixed_file_data {
188 struct fixed_file_table *table;
189 struct io_ring_ctx *ctx;
191 struct percpu_ref refs;
192 struct llist_head put_llist;
194 struct work_struct ref_work;
195 struct completion done;
200 struct percpu_ref refs;
201 } ____cacheline_aligned_in_smp;
207 bool cq_overflow_flushed;
211 * Ring buffer of indices into array of io_uring_sqe, which is
212 * mmapped by the application using the IORING_OFF_SQES offset.
214 * This indirection could e.g. be used to assign fixed
215 * io_uring_sqe entries to operations and only submit them to
216 * the queue when needed.
218 * The kernel modifies neither the indices array nor the entries
222 unsigned cached_sq_head;
225 unsigned sq_thread_idle;
226 unsigned cached_sq_dropped;
227 atomic_t cached_cq_overflow;
228 unsigned long sq_check_overflow;
230 struct list_head defer_list;
231 struct list_head timeout_list;
232 struct list_head cq_overflow_list;
234 wait_queue_head_t inflight_wait;
235 struct io_uring_sqe *sq_sqes;
236 } ____cacheline_aligned_in_smp;
238 struct io_rings *rings;
242 struct task_struct *sqo_thread; /* if using sq thread polling */
243 struct mm_struct *sqo_mm;
244 wait_queue_head_t sqo_wait;
247 * If used, fixed file set. Writers must ensure that ->refs is dead,
248 * readers must ensure that ->refs is alive as long as the file* is
249 * used. Only updated through io_uring_register(2).
251 struct fixed_file_data *file_data;
252 unsigned nr_user_files;
254 /* if used, fixed mapped user buffers */
255 unsigned nr_user_bufs;
256 struct io_mapped_ubuf *user_bufs;
258 struct user_struct *user;
260 const struct cred *creds;
262 /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */
263 struct completion *completions;
265 /* if all else fails... */
266 struct io_kiocb *fallback_req;
268 #if defined(CONFIG_UNIX)
269 struct socket *ring_sock;
273 unsigned cached_cq_tail;
276 atomic_t cq_timeouts;
277 unsigned long cq_check_overflow;
278 struct wait_queue_head cq_wait;
279 struct fasync_struct *cq_fasync;
280 struct eventfd_ctx *cq_ev_fd;
281 } ____cacheline_aligned_in_smp;
284 struct mutex uring_lock;
285 wait_queue_head_t wait;
286 } ____cacheline_aligned_in_smp;
289 spinlock_t completion_lock;
290 struct llist_head poll_llist;
293 * ->poll_list is protected by the ctx->uring_lock for
294 * io_uring instances that don't use IORING_SETUP_SQPOLL.
295 * For SQPOLL, only the single threaded io_sq_thread() will
296 * manipulate the list, hence no extra locking is needed there.
298 struct list_head poll_list;
299 struct hlist_head *cancel_hash;
300 unsigned cancel_hash_bits;
301 bool poll_multi_file;
303 spinlock_t inflight_lock;
304 struct list_head inflight_list;
305 } ____cacheline_aligned_in_smp;
309 * First field must be the file pointer in all the
310 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
312 struct io_poll_iocb {
315 struct wait_queue_head *head;
321 struct wait_queue_entry wait;
326 struct file *put_file;
330 struct io_timeout_data {
331 struct io_kiocb *req;
332 struct hrtimer timer;
333 struct timespec64 ts;
334 enum hrtimer_mode mode;
340 struct sockaddr __user *addr;
341 int __user *addr_len;
366 /* NOTE: kiocb has the file as the first member, so don't do it here */
374 struct sockaddr __user *addr;
381 struct user_msghdr __user *msg;
395 const char __user *fname;
396 struct filename *filename;
397 struct statx __user *buffer;
401 struct io_files_update {
422 struct io_async_connect {
423 struct sockaddr_storage address;
426 struct io_async_msghdr {
427 struct iovec fast_iov[UIO_FASTIOV];
429 struct sockaddr __user *uaddr;
434 struct iovec fast_iov[UIO_FASTIOV];
440 struct io_async_open {
441 struct filename *filename;
444 struct io_async_ctx {
446 struct io_async_rw rw;
447 struct io_async_msghdr msg;
448 struct io_async_connect connect;
449 struct io_timeout_data timeout;
450 struct io_async_open open;
455 * NOTE! Each of the iocb union members has the file pointer
456 * as the first entry in their struct definition. So you can
457 * access the file pointer through any of the sub-structs,
458 * or directly as just 'ki_filp' in this struct.
464 struct io_poll_iocb poll;
465 struct io_accept accept;
467 struct io_cancel cancel;
468 struct io_timeout timeout;
469 struct io_connect connect;
470 struct io_sr_msg sr_msg;
472 struct io_close close;
473 struct io_files_update files_update;
474 struct io_fadvise fadvise;
475 struct io_madvise madvise;
478 struct io_async_ctx *io;
481 * ring_file is only used in the submission path, and
482 * llist_node is only used for poll deferred completions
484 struct file *ring_file;
485 struct llist_node llist_node;
490 bool needs_fixed_file;
493 struct io_ring_ctx *ctx;
495 struct list_head list;
496 struct hlist_node hash_node;
498 struct list_head link_list;
501 #define REQ_F_NOWAIT 1 /* must not punt to workers */
502 #define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
503 #define REQ_F_FIXED_FILE 4 /* ctx owns file */
504 #define REQ_F_LINK_NEXT 8 /* already grabbed next link */
505 #define REQ_F_IO_DRAIN 16 /* drain existing IO first */
506 #define REQ_F_IO_DRAINED 32 /* drain done */
507 #define REQ_F_LINK 64 /* linked sqes */
508 #define REQ_F_LINK_TIMEOUT 128 /* has linked timeout */
509 #define REQ_F_FAIL_LINK 256 /* fail rest of links */
510 #define REQ_F_DRAIN_LINK 512 /* link should be fully drained */
511 #define REQ_F_TIMEOUT 1024 /* timeout request */
512 #define REQ_F_ISREG 2048 /* regular file */
513 #define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
514 #define REQ_F_TIMEOUT_NOSEQ 8192 /* no timeout sequence */
515 #define REQ_F_INFLIGHT 16384 /* on inflight list */
516 #define REQ_F_COMP_LOCKED 32768 /* completion under lock */
517 #define REQ_F_HARDLINK 65536 /* doesn't sever on completion < 0 */
518 #define REQ_F_FORCE_ASYNC 131072 /* IOSQE_ASYNC */
519 #define REQ_F_CUR_POS 262144 /* read/write uses file position */
524 struct list_head inflight_entry;
526 struct io_wq_work work;
529 #define IO_PLUG_THRESHOLD 2
530 #define IO_IOPOLL_BATCH 8
532 struct io_submit_state {
533 struct blk_plug plug;
536 * io_kiocb alloc cache
538 void *reqs[IO_IOPOLL_BATCH];
539 unsigned int free_reqs;
540 unsigned int cur_req;
543 * File reference cache
547 unsigned int has_refs;
548 unsigned int used_refs;
549 unsigned int ios_left;
553 /* needs req->io allocated for deferral/async */
554 unsigned async_ctx : 1;
555 /* needs current->mm setup, does mm access */
556 unsigned needs_mm : 1;
557 /* needs req->file assigned */
558 unsigned needs_file : 1;
559 /* needs req->file assigned IFF fd is >= 0 */
560 unsigned fd_non_neg : 1;
561 /* hash wq insertion if file is a regular file */
562 unsigned hash_reg_file : 1;
563 /* unbound wq insertion if file is a non-regular file */
564 unsigned unbound_nonreg_file : 1;
567 static const struct io_op_def io_op_defs[] = {
572 /* IORING_OP_READV */
576 .unbound_nonreg_file = 1,
579 /* IORING_OP_WRITEV */
584 .unbound_nonreg_file = 1,
587 /* IORING_OP_FSYNC */
591 /* IORING_OP_READ_FIXED */
593 .unbound_nonreg_file = 1,
596 /* IORING_OP_WRITE_FIXED */
599 .unbound_nonreg_file = 1,
602 /* IORING_OP_POLL_ADD */
604 .unbound_nonreg_file = 1,
607 /* IORING_OP_POLL_REMOVE */
610 /* IORING_OP_SYNC_FILE_RANGE */
614 /* IORING_OP_SENDMSG */
618 .unbound_nonreg_file = 1,
621 /* IORING_OP_RECVMSG */
625 .unbound_nonreg_file = 1,
628 /* IORING_OP_TIMEOUT */
633 /* IORING_OP_TIMEOUT_REMOVE */
636 /* IORING_OP_ACCEPT */
639 .unbound_nonreg_file = 1,
642 /* IORING_OP_ASYNC_CANCEL */
645 /* IORING_OP_LINK_TIMEOUT */
650 /* IORING_OP_CONNECT */
654 .unbound_nonreg_file = 1,
657 /* IORING_OP_FALLOCATE */
661 /* IORING_OP_OPENAT */
666 /* IORING_OP_CLOSE */
670 /* IORING_OP_FILES_UPDATE */
674 /* IORING_OP_STATX */
683 .unbound_nonreg_file = 1,
686 /* IORING_OP_WRITE */
689 .unbound_nonreg_file = 1,
692 /* IORING_OP_FADVISE */
696 /* IORING_OP_MADVISE */
703 .unbound_nonreg_file = 1,
709 .unbound_nonreg_file = 1,
713 static void io_wq_submit_work(struct io_wq_work **workptr);
714 static void io_cqring_fill_event(struct io_kiocb *req, long res);
715 static void io_put_req(struct io_kiocb *req);
716 static void __io_double_put_req(struct io_kiocb *req);
717 static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
718 static void io_queue_linked_timeout(struct io_kiocb *req);
719 static int __io_sqe_files_update(struct io_ring_ctx *ctx,
720 struct io_uring_files_update *ip,
723 static struct kmem_cache *req_cachep;
725 static const struct file_operations io_uring_fops;
727 struct sock *io_uring_get_socket(struct file *file)
729 #if defined(CONFIG_UNIX)
730 if (file->f_op == &io_uring_fops) {
731 struct io_ring_ctx *ctx = file->private_data;
733 return ctx->ring_sock->sk;
738 EXPORT_SYMBOL(io_uring_get_socket);
740 static void io_ring_ctx_ref_free(struct percpu_ref *ref)
742 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
744 complete(&ctx->completions[0]);
747 static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
749 struct io_ring_ctx *ctx;
752 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
756 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
757 if (!ctx->fallback_req)
760 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
761 if (!ctx->completions)
765 * Use 5 bits less than the max cq entries, that should give us around
766 * 32 entries per hash list if totally full and uniformly spread.
768 hash_bits = ilog2(p->cq_entries);
772 ctx->cancel_hash_bits = hash_bits;
773 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
775 if (!ctx->cancel_hash)
777 __hash_init(ctx->cancel_hash, 1U << hash_bits);
779 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
780 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
783 ctx->flags = p->flags;
784 init_waitqueue_head(&ctx->cq_wait);
785 INIT_LIST_HEAD(&ctx->cq_overflow_list);
786 init_completion(&ctx->completions[0]);
787 init_completion(&ctx->completions[1]);
788 mutex_init(&ctx->uring_lock);
789 init_waitqueue_head(&ctx->wait);
790 spin_lock_init(&ctx->completion_lock);
791 init_llist_head(&ctx->poll_llist);
792 INIT_LIST_HEAD(&ctx->poll_list);
793 INIT_LIST_HEAD(&ctx->defer_list);
794 INIT_LIST_HEAD(&ctx->timeout_list);
795 init_waitqueue_head(&ctx->inflight_wait);
796 spin_lock_init(&ctx->inflight_lock);
797 INIT_LIST_HEAD(&ctx->inflight_list);
800 if (ctx->fallback_req)
801 kmem_cache_free(req_cachep, ctx->fallback_req);
802 kfree(ctx->completions);
803 kfree(ctx->cancel_hash);
808 static inline bool __req_need_defer(struct io_kiocb *req)
810 struct io_ring_ctx *ctx = req->ctx;
812 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
813 + atomic_read(&ctx->cached_cq_overflow);
816 static inline bool req_need_defer(struct io_kiocb *req)
818 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) == REQ_F_IO_DRAIN)
819 return __req_need_defer(req);
824 static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
826 struct io_kiocb *req;
828 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
829 if (req && !req_need_defer(req)) {
830 list_del_init(&req->list);
837 static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
839 struct io_kiocb *req;
841 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
843 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
845 if (!__req_need_defer(req)) {
846 list_del_init(&req->list);
854 static void __io_commit_cqring(struct io_ring_ctx *ctx)
856 struct io_rings *rings = ctx->rings;
858 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
859 /* order cqe stores with ring update */
860 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
862 if (wq_has_sleeper(&ctx->cq_wait)) {
863 wake_up_interruptible(&ctx->cq_wait);
864 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
869 static inline bool io_prep_async_work(struct io_kiocb *req,
870 struct io_kiocb **link)
872 const struct io_op_def *def = &io_op_defs[req->opcode];
873 bool do_hashed = false;
875 if (req->flags & REQ_F_ISREG) {
876 if (def->hash_reg_file)
879 if (def->unbound_nonreg_file)
880 req->work.flags |= IO_WQ_WORK_UNBOUND;
883 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
885 *link = io_prep_linked_timeout(req);
889 static inline void io_queue_async_work(struct io_kiocb *req)
891 struct io_ring_ctx *ctx = req->ctx;
892 struct io_kiocb *link;
895 do_hashed = io_prep_async_work(req, &link);
897 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
900 io_wq_enqueue(ctx->io_wq, &req->work);
902 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
903 file_inode(req->file));
907 io_queue_linked_timeout(link);
910 static void io_kill_timeout(struct io_kiocb *req)
914 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
916 atomic_inc(&req->ctx->cq_timeouts);
917 list_del_init(&req->list);
918 io_cqring_fill_event(req, 0);
923 static void io_kill_timeouts(struct io_ring_ctx *ctx)
925 struct io_kiocb *req, *tmp;
927 spin_lock_irq(&ctx->completion_lock);
928 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
929 io_kill_timeout(req);
930 spin_unlock_irq(&ctx->completion_lock);
933 static void io_commit_cqring(struct io_ring_ctx *ctx)
935 struct io_kiocb *req;
937 while ((req = io_get_timeout_req(ctx)) != NULL)
938 io_kill_timeout(req);
940 __io_commit_cqring(ctx);
942 while ((req = io_get_deferred_req(ctx)) != NULL) {
943 req->flags |= REQ_F_IO_DRAINED;
944 io_queue_async_work(req);
948 static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
950 struct io_rings *rings = ctx->rings;
953 tail = ctx->cached_cq_tail;
955 * writes to the cq entry need to come after reading head; the
956 * control dependency is enough as we're using WRITE_ONCE to
959 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
962 ctx->cached_cq_tail++;
963 return &rings->cqes[tail & ctx->cq_mask];
966 static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
968 if (waitqueue_active(&ctx->wait))
970 if (waitqueue_active(&ctx->sqo_wait))
971 wake_up(&ctx->sqo_wait);
973 eventfd_signal(ctx->cq_ev_fd, 1);
976 /* Returns true if there are no backlogged entries after the flush */
977 static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
979 struct io_rings *rings = ctx->rings;
980 struct io_uring_cqe *cqe;
981 struct io_kiocb *req;
986 if (list_empty_careful(&ctx->cq_overflow_list))
988 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
989 rings->cq_ring_entries))
993 spin_lock_irqsave(&ctx->completion_lock, flags);
995 /* if force is set, the ring is going away. always drop after that */
997 ctx->cq_overflow_flushed = true;
1000 while (!list_empty(&ctx->cq_overflow_list)) {
1001 cqe = io_get_cqring(ctx);
1005 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1007 list_move(&req->list, &list);
1009 WRITE_ONCE(cqe->user_data, req->user_data);
1010 WRITE_ONCE(cqe->res, req->result);
1011 WRITE_ONCE(cqe->flags, 0);
1013 WRITE_ONCE(ctx->rings->cq_overflow,
1014 atomic_inc_return(&ctx->cached_cq_overflow));
1018 io_commit_cqring(ctx);
1020 clear_bit(0, &ctx->sq_check_overflow);
1021 clear_bit(0, &ctx->cq_check_overflow);
1023 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1024 io_cqring_ev_posted(ctx);
1026 while (!list_empty(&list)) {
1027 req = list_first_entry(&list, struct io_kiocb, list);
1028 list_del(&req->list);
1035 static void io_cqring_fill_event(struct io_kiocb *req, long res)
1037 struct io_ring_ctx *ctx = req->ctx;
1038 struct io_uring_cqe *cqe;
1040 trace_io_uring_complete(ctx, req->user_data, res);
1043 * If we can't get a cq entry, userspace overflowed the
1044 * submission (by quite a lot). Increment the overflow count in
1047 cqe = io_get_cqring(ctx);
1049 WRITE_ONCE(cqe->user_data, req->user_data);
1050 WRITE_ONCE(cqe->res, res);
1051 WRITE_ONCE(cqe->flags, 0);
1052 } else if (ctx->cq_overflow_flushed) {
1053 WRITE_ONCE(ctx->rings->cq_overflow,
1054 atomic_inc_return(&ctx->cached_cq_overflow));
1056 if (list_empty(&ctx->cq_overflow_list)) {
1057 set_bit(0, &ctx->sq_check_overflow);
1058 set_bit(0, &ctx->cq_check_overflow);
1060 refcount_inc(&req->refs);
1062 list_add_tail(&req->list, &ctx->cq_overflow_list);
1066 static void io_cqring_add_event(struct io_kiocb *req, long res)
1068 struct io_ring_ctx *ctx = req->ctx;
1069 unsigned long flags;
1071 spin_lock_irqsave(&ctx->completion_lock, flags);
1072 io_cqring_fill_event(req, res);
1073 io_commit_cqring(ctx);
1074 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1076 io_cqring_ev_posted(ctx);
1079 static inline bool io_is_fallback_req(struct io_kiocb *req)
1081 return req == (struct io_kiocb *)
1082 ((unsigned long) req->ctx->fallback_req & ~1UL);
1085 static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1087 struct io_kiocb *req;
1089 req = ctx->fallback_req;
1090 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
1096 static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
1097 struct io_submit_state *state)
1099 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
1100 struct io_kiocb *req;
1103 req = kmem_cache_alloc(req_cachep, gfp);
1106 } else if (!state->free_reqs) {
1110 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
1111 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1114 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1115 * retry single alloc to be on the safe side.
1117 if (unlikely(ret <= 0)) {
1118 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1119 if (!state->reqs[0])
1123 state->free_reqs = ret - 1;
1125 req = state->reqs[0];
1127 req = state->reqs[state->cur_req];
1134 req->ring_file = NULL;
1138 /* one is dropped after submission, the other at completion */
1139 refcount_set(&req->refs, 2);
1141 INIT_IO_WORK(&req->work, io_wq_submit_work);
1144 req = io_get_fallback_req(ctx);
1147 percpu_ref_put(&ctx->refs);
1151 static void __io_req_do_free(struct io_kiocb *req)
1153 if (likely(!io_is_fallback_req(req)))
1154 kmem_cache_free(req_cachep, req);
1156 clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req);
1159 static void __io_req_aux_free(struct io_kiocb *req)
1161 struct io_ring_ctx *ctx = req->ctx;
1166 if (req->flags & REQ_F_FIXED_FILE)
1167 percpu_ref_put(&ctx->file_data->refs);
1173 static void __io_free_req(struct io_kiocb *req)
1175 __io_req_aux_free(req);
1177 if (req->flags & REQ_F_INFLIGHT) {
1178 struct io_ring_ctx *ctx = req->ctx;
1179 unsigned long flags;
1181 spin_lock_irqsave(&ctx->inflight_lock, flags);
1182 list_del(&req->inflight_entry);
1183 if (waitqueue_active(&ctx->inflight_wait))
1184 wake_up(&ctx->inflight_wait);
1185 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1188 percpu_ref_put(&req->ctx->refs);
1189 __io_req_do_free(req);
1193 void *reqs[IO_IOPOLL_BATCH];
1198 static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1202 if (rb->need_iter) {
1203 int i, inflight = 0;
1204 unsigned long flags;
1206 for (i = 0; i < rb->to_free; i++) {
1207 struct io_kiocb *req = rb->reqs[i];
1209 if (req->flags & REQ_F_FIXED_FILE)
1211 if (req->flags & REQ_F_INFLIGHT)
1215 __io_req_aux_free(req);
1220 spin_lock_irqsave(&ctx->inflight_lock, flags);
1221 for (i = 0; i < rb->to_free; i++) {
1222 struct io_kiocb *req = rb->reqs[i];
1225 list_del(&req->inflight_entry);
1230 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1232 if (waitqueue_active(&ctx->inflight_wait))
1233 wake_up(&ctx->inflight_wait);
1236 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
1237 percpu_ref_put_many(&ctx->refs, rb->to_free);
1238 percpu_ref_put_many(&ctx->file_data->refs, rb->to_free);
1239 rb->to_free = rb->need_iter = 0;
1242 static bool io_link_cancel_timeout(struct io_kiocb *req)
1244 struct io_ring_ctx *ctx = req->ctx;
1247 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
1249 io_cqring_fill_event(req, -ECANCELED);
1250 io_commit_cqring(ctx);
1251 req->flags &= ~REQ_F_LINK;
1259 static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
1261 struct io_ring_ctx *ctx = req->ctx;
1262 bool wake_ev = false;
1264 /* Already got next link */
1265 if (req->flags & REQ_F_LINK_NEXT)
1269 * The list should never be empty when we are called here. But could
1270 * potentially happen if the chain is messed up, check to be on the
1273 while (!list_empty(&req->link_list)) {
1274 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1275 struct io_kiocb, link_list);
1277 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1278 (nxt->flags & REQ_F_TIMEOUT))) {
1279 list_del_init(&nxt->link_list);
1280 wake_ev |= io_link_cancel_timeout(nxt);
1281 req->flags &= ~REQ_F_LINK_TIMEOUT;
1285 list_del_init(&req->link_list);
1286 if (!list_empty(&nxt->link_list))
1287 nxt->flags |= REQ_F_LINK;
1292 req->flags |= REQ_F_LINK_NEXT;
1294 io_cqring_ev_posted(ctx);
1298 * Called if REQ_F_LINK is set, and we fail the head request
1300 static void io_fail_links(struct io_kiocb *req)
1302 struct io_ring_ctx *ctx = req->ctx;
1303 unsigned long flags;
1305 spin_lock_irqsave(&ctx->completion_lock, flags);
1307 while (!list_empty(&req->link_list)) {
1308 struct io_kiocb *link = list_first_entry(&req->link_list,
1309 struct io_kiocb, link_list);
1311 list_del_init(&link->link_list);
1312 trace_io_uring_fail_link(req, link);
1314 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
1315 link->opcode == IORING_OP_LINK_TIMEOUT) {
1316 io_link_cancel_timeout(link);
1318 io_cqring_fill_event(link, -ECANCELED);
1319 __io_double_put_req(link);
1321 req->flags &= ~REQ_F_LINK_TIMEOUT;
1324 io_commit_cqring(ctx);
1325 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1326 io_cqring_ev_posted(ctx);
1329 static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
1331 if (likely(!(req->flags & REQ_F_LINK)))
1335 * If LINK is set, we have dependent requests in this chain. If we
1336 * didn't fail this request, queue the first one up, moving any other
1337 * dependencies to the next request. In case of failure, fail the rest
1340 if (req->flags & REQ_F_FAIL_LINK) {
1342 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1343 REQ_F_LINK_TIMEOUT) {
1344 struct io_ring_ctx *ctx = req->ctx;
1345 unsigned long flags;
1348 * If this is a timeout link, we could be racing with the
1349 * timeout timer. Grab the completion lock for this case to
1350 * protect against that.
1352 spin_lock_irqsave(&ctx->completion_lock, flags);
1353 io_req_link_next(req, nxt);
1354 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1356 io_req_link_next(req, nxt);
1360 static void io_free_req(struct io_kiocb *req)
1362 struct io_kiocb *nxt = NULL;
1364 io_req_find_next(req, &nxt);
1368 io_queue_async_work(nxt);
1372 * Drop reference to request, return next in chain (if there is one) if this
1373 * was the last reference to this request.
1375 __attribute__((nonnull))
1376 static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
1378 io_req_find_next(req, nxtptr);
1380 if (refcount_dec_and_test(&req->refs))
1384 static void io_put_req(struct io_kiocb *req)
1386 if (refcount_dec_and_test(&req->refs))
1391 * Must only be used if we don't need to care about links, usually from
1392 * within the completion handling itself.
1394 static void __io_double_put_req(struct io_kiocb *req)
1396 /* drop both submit and complete references */
1397 if (refcount_sub_and_test(2, &req->refs))
1401 static void io_double_put_req(struct io_kiocb *req)
1403 /* drop both submit and complete references */
1404 if (refcount_sub_and_test(2, &req->refs))
1408 static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
1410 struct io_rings *rings = ctx->rings;
1412 if (test_bit(0, &ctx->cq_check_overflow)) {
1414 * noflush == true is from the waitqueue handler, just ensure
1415 * we wake up the task, and the next invocation will flush the
1416 * entries. We cannot safely to it from here.
1418 if (noflush && !list_empty(&ctx->cq_overflow_list))
1421 io_cqring_overflow_flush(ctx, false);
1424 /* See comment at the top of this file */
1426 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
1429 static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1431 struct io_rings *rings = ctx->rings;
1433 /* make sure SQ entry isn't read before tail */
1434 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1437 static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
1439 if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req))
1442 if (!(req->flags & REQ_F_FIXED_FILE) || req->io)
1445 rb->reqs[rb->to_free++] = req;
1446 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1447 io_free_req_many(req->ctx, rb);
1452 * Find and free completed poll iocbs
1454 static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1455 struct list_head *done)
1457 struct req_batch rb;
1458 struct io_kiocb *req;
1460 rb.to_free = rb.need_iter = 0;
1461 while (!list_empty(done)) {
1462 req = list_first_entry(done, struct io_kiocb, list);
1463 list_del(&req->list);
1465 io_cqring_fill_event(req, req->result);
1468 if (refcount_dec_and_test(&req->refs) &&
1469 !io_req_multi_free(&rb, req))
1473 io_commit_cqring(ctx);
1474 io_free_req_many(ctx, &rb);
1477 static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1480 struct io_kiocb *req, *tmp;
1486 * Only spin for completions if we don't have multiple devices hanging
1487 * off our complete list, and we're under the requested amount.
1489 spin = !ctx->poll_multi_file && *nr_events < min;
1492 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
1493 struct kiocb *kiocb = &req->rw.kiocb;
1496 * Move completed entries to our local list. If we find a
1497 * request that requires polling, break out and complete
1498 * the done list first, if we have entries there.
1500 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1501 list_move_tail(&req->list, &done);
1504 if (!list_empty(&done))
1507 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1516 if (!list_empty(&done))
1517 io_iopoll_complete(ctx, nr_events, &done);
1523 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
1524 * non-spinning poll check - we'll still enter the driver poll loop, but only
1525 * as a non-spinning completion check.
1527 static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1530 while (!list_empty(&ctx->poll_list) && !need_resched()) {
1533 ret = io_do_iopoll(ctx, nr_events, min);
1536 if (!min || *nr_events >= min)
1544 * We can't just wait for polled events to come to us, we have to actively
1545 * find and complete them.
1547 static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1549 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1552 mutex_lock(&ctx->uring_lock);
1553 while (!list_empty(&ctx->poll_list)) {
1554 unsigned int nr_events = 0;
1556 io_iopoll_getevents(ctx, &nr_events, 1);
1559 * Ensure we allow local-to-the-cpu processing to take place,
1560 * in this case we need to ensure that we reap all events.
1564 mutex_unlock(&ctx->uring_lock);
1567 static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1570 int iters = 0, ret = 0;
1576 * Don't enter poll loop if we already have events pending.
1577 * If we do, we can potentially be spinning for commands that
1578 * already triggered a CQE (eg in error).
1580 if (io_cqring_events(ctx, false))
1584 * If a submit got punted to a workqueue, we can have the
1585 * application entering polling for a command before it gets
1586 * issued. That app will hold the uring_lock for the duration
1587 * of the poll right here, so we need to take a breather every
1588 * now and then to ensure that the issue has a chance to add
1589 * the poll to the issued list. Otherwise we can spin here
1590 * forever, while the workqueue is stuck trying to acquire the
1593 if (!(++iters & 7)) {
1594 mutex_unlock(&ctx->uring_lock);
1595 mutex_lock(&ctx->uring_lock);
1598 if (*nr_events < min)
1599 tmin = min - *nr_events;
1601 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1605 } while (min && !*nr_events && !need_resched());
1610 static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1616 * We disallow the app entering submit/complete with polling, but we
1617 * still need to lock the ring to prevent racing with polled issue
1618 * that got punted to a workqueue.
1620 mutex_lock(&ctx->uring_lock);
1621 ret = __io_iopoll_check(ctx, nr_events, min);
1622 mutex_unlock(&ctx->uring_lock);
1626 static void kiocb_end_write(struct io_kiocb *req)
1629 * Tell lockdep we inherited freeze protection from submission
1632 if (req->flags & REQ_F_ISREG) {
1633 struct inode *inode = file_inode(req->file);
1635 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
1637 file_end_write(req->file);
1640 static inline void req_set_fail_links(struct io_kiocb *req)
1642 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1643 req->flags |= REQ_F_FAIL_LINK;
1646 static void io_complete_rw_common(struct kiocb *kiocb, long res)
1648 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
1650 if (kiocb->ki_flags & IOCB_WRITE)
1651 kiocb_end_write(req);
1653 if (res != req->result)
1654 req_set_fail_links(req);
1655 io_cqring_add_event(req, res);
1658 static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1660 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
1662 io_complete_rw_common(kiocb, res);
1666 static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1668 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
1669 struct io_kiocb *nxt = NULL;
1671 io_complete_rw_common(kiocb, res);
1672 io_put_req_find_next(req, &nxt);
1677 static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1679 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
1681 if (kiocb->ki_flags & IOCB_WRITE)
1682 kiocb_end_write(req);
1684 if (res != req->result)
1685 req_set_fail_links(req);
1688 req->flags |= REQ_F_IOPOLL_COMPLETED;
1692 * After the iocb has been issued, it's safe to be found on the poll list.
1693 * Adding the kiocb to the list AFTER submission ensures that we don't
1694 * find it from a io_iopoll_getevents() thread before the issuer is done
1695 * accessing the kiocb cookie.
1697 static void io_iopoll_req_issued(struct io_kiocb *req)
1699 struct io_ring_ctx *ctx = req->ctx;
1702 * Track whether we have multiple files in our lists. This will impact
1703 * how we do polling eventually, not spinning if we're on potentially
1704 * different devices.
1706 if (list_empty(&ctx->poll_list)) {
1707 ctx->poll_multi_file = false;
1708 } else if (!ctx->poll_multi_file) {
1709 struct io_kiocb *list_req;
1711 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1713 if (list_req->file != req->file)
1714 ctx->poll_multi_file = true;
1718 * For fast devices, IO may have already completed. If it has, add
1719 * it to the front so we find it first.
1721 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1722 list_add(&req->list, &ctx->poll_list);
1724 list_add_tail(&req->list, &ctx->poll_list);
1727 static void io_file_put(struct io_submit_state *state)
1730 int diff = state->has_refs - state->used_refs;
1733 fput_many(state->file, diff);
1739 * Get as many references to a file as we have IOs left in this submission,
1740 * assuming most submissions are for one file, or at least that each file
1741 * has more than one submission.
1743 static struct file *io_file_get(struct io_submit_state *state, int fd)
1749 if (state->fd == fd) {
1756 state->file = fget_many(fd, state->ios_left);
1761 state->has_refs = state->ios_left;
1762 state->used_refs = 1;
1768 * If we tracked the file through the SCM inflight mechanism, we could support
1769 * any file. For now, just ensure that anything potentially problematic is done
1772 static bool io_file_supports_async(struct file *file)
1774 umode_t mode = file_inode(file)->i_mode;
1776 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
1778 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1784 static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1785 bool force_nonblock)
1787 struct io_ring_ctx *ctx = req->ctx;
1788 struct kiocb *kiocb = &req->rw.kiocb;
1795 if (S_ISREG(file_inode(req->file)->i_mode))
1796 req->flags |= REQ_F_ISREG;
1798 kiocb->ki_pos = READ_ONCE(sqe->off);
1799 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
1800 req->flags |= REQ_F_CUR_POS;
1801 kiocb->ki_pos = req->file->f_pos;
1803 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1804 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1806 ioprio = READ_ONCE(sqe->ioprio);
1808 ret = ioprio_check_cap(ioprio);
1812 kiocb->ki_ioprio = ioprio;
1814 kiocb->ki_ioprio = get_current_ioprio();
1816 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1820 /* don't allow async punt if RWF_NOWAIT was requested */
1821 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1822 (req->file->f_flags & O_NONBLOCK))
1823 req->flags |= REQ_F_NOWAIT;
1826 kiocb->ki_flags |= IOCB_NOWAIT;
1828 if (ctx->flags & IORING_SETUP_IOPOLL) {
1829 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1830 !kiocb->ki_filp->f_op->iopoll)
1833 kiocb->ki_flags |= IOCB_HIPRI;
1834 kiocb->ki_complete = io_complete_rw_iopoll;
1837 if (kiocb->ki_flags & IOCB_HIPRI)
1839 kiocb->ki_complete = io_complete_rw;
1842 req->rw.addr = READ_ONCE(sqe->addr);
1843 req->rw.len = READ_ONCE(sqe->len);
1844 /* we own ->private, reuse it for the buffer index */
1845 req->rw.kiocb.private = (void *) (unsigned long)
1846 READ_ONCE(sqe->buf_index);
1850 static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1856 case -ERESTARTNOINTR:
1857 case -ERESTARTNOHAND:
1858 case -ERESTART_RESTARTBLOCK:
1860 * We can't just restart the syscall, since previously
1861 * submitted sqes may already be in progress. Just fail this
1867 kiocb->ki_complete(kiocb, ret, 0);
1871 static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1874 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
1876 if (req->flags & REQ_F_CUR_POS)
1877 req->file->f_pos = kiocb->ki_pos;
1878 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
1879 *nxt = __io_complete_rw(kiocb, ret);
1881 io_rw_done(kiocb, ret);
1884 static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
1885 struct iov_iter *iter)
1887 struct io_ring_ctx *ctx = req->ctx;
1888 size_t len = req->rw.len;
1889 struct io_mapped_ubuf *imu;
1890 unsigned index, buf_index;
1894 /* attempt to use fixed buffers without having provided iovecs */
1895 if (unlikely(!ctx->user_bufs))
1898 buf_index = (unsigned long) req->rw.kiocb.private;
1899 if (unlikely(buf_index >= ctx->nr_user_bufs))
1902 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1903 imu = &ctx->user_bufs[index];
1904 buf_addr = req->rw.addr;
1907 if (buf_addr + len < buf_addr)
1909 /* not inside the mapped region */
1910 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1914 * May not be a start of buffer, set size appropriately
1915 * and advance us to the beginning.
1917 offset = buf_addr - imu->ubuf;
1918 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
1922 * Don't use iov_iter_advance() here, as it's really slow for
1923 * using the latter parts of a big fixed buffer - it iterates
1924 * over each segment manually. We can cheat a bit here, because
1927 * 1) it's a BVEC iter, we set it up
1928 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1929 * first and last bvec
1931 * So just find our index, and adjust the iterator afterwards.
1932 * If the offset is within the first bvec (or the whole first
1933 * bvec, just use iov_iter_advance(). This makes it easier
1934 * since we can just skip the first segment, which may not
1935 * be PAGE_SIZE aligned.
1937 const struct bio_vec *bvec = imu->bvec;
1939 if (offset <= bvec->bv_len) {
1940 iov_iter_advance(iter, offset);
1942 unsigned long seg_skip;
1944 /* skip first vec */
1945 offset -= bvec->bv_len;
1946 seg_skip = 1 + (offset >> PAGE_SHIFT);
1948 iter->bvec = bvec + seg_skip;
1949 iter->nr_segs -= seg_skip;
1950 iter->count -= bvec->bv_len + offset;
1951 iter->iov_offset = offset & ~PAGE_MASK;
1958 static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
1959 struct iovec **iovec, struct iov_iter *iter)
1961 void __user *buf = u64_to_user_ptr(req->rw.addr);
1962 size_t sqe_len = req->rw.len;
1965 opcode = req->opcode;
1966 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
1968 return io_import_fixed(req, rw, iter);
1971 /* buffer index only valid with fixed read/write */
1972 if (req->rw.kiocb.private)
1975 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
1977 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
1983 struct io_async_rw *iorw = &req->io->rw;
1986 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
1987 if (iorw->iov == iorw->fast_iov)
1995 #ifdef CONFIG_COMPAT
1996 if (req->ctx->compat)
1997 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2001 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2005 * For files that don't have ->read_iter() and ->write_iter(), handle them
2006 * by looping over ->read() or ->write() manually.
2008 static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2009 struct iov_iter *iter)
2014 * Don't support polled IO through this interface, and we can't
2015 * support non-blocking either. For the latter, this just causes
2016 * the kiocb to be handled from an async context.
2018 if (kiocb->ki_flags & IOCB_HIPRI)
2020 if (kiocb->ki_flags & IOCB_NOWAIT)
2023 while (iov_iter_count(iter)) {
2027 if (!iov_iter_is_bvec(iter)) {
2028 iovec = iov_iter_iovec(iter);
2030 /* fixed buffers import bvec */
2031 iovec.iov_base = kmap(iter->bvec->bv_page)
2033 iovec.iov_len = min(iter->count,
2034 iter->bvec->bv_len - iter->iov_offset);
2038 nr = file->f_op->read(file, iovec.iov_base,
2039 iovec.iov_len, &kiocb->ki_pos);
2041 nr = file->f_op->write(file, iovec.iov_base,
2042 iovec.iov_len, &kiocb->ki_pos);
2045 if (iov_iter_is_bvec(iter))
2046 kunmap(iter->bvec->bv_page);
2054 if (nr != iovec.iov_len)
2056 iov_iter_advance(iter, nr);
2062 static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
2063 struct iovec *iovec, struct iovec *fast_iov,
2064 struct iov_iter *iter)
2066 req->io->rw.nr_segs = iter->nr_segs;
2067 req->io->rw.size = io_size;
2068 req->io->rw.iov = iovec;
2069 if (!req->io->rw.iov) {
2070 req->io->rw.iov = req->io->rw.fast_iov;
2071 memcpy(req->io->rw.iov, fast_iov,
2072 sizeof(struct iovec) * iter->nr_segs);
2076 static int io_alloc_async_ctx(struct io_kiocb *req)
2078 if (!io_op_defs[req->opcode].async_ctx)
2080 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
2081 return req->io == NULL;
2084 static void io_rw_async(struct io_wq_work **workptr)
2086 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2087 struct iovec *iov = NULL;
2089 if (req->io->rw.iov != req->io->rw.fast_iov)
2090 iov = req->io->rw.iov;
2091 io_wq_submit_work(workptr);
2095 static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2096 struct iovec *iovec, struct iovec *fast_iov,
2097 struct iov_iter *iter)
2099 if (req->opcode == IORING_OP_READ_FIXED ||
2100 req->opcode == IORING_OP_WRITE_FIXED)
2102 if (!req->io && io_alloc_async_ctx(req))
2105 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2106 req->work.func = io_rw_async;
2110 static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2111 bool force_nonblock)
2113 struct io_async_ctx *io;
2114 struct iov_iter iter;
2117 ret = io_prep_rw(req, sqe, force_nonblock);
2121 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2128 io->rw.iov = io->rw.fast_iov;
2130 ret = io_import_iovec(READ, req, &io->rw.iov, &iter);
2135 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2139 static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
2140 bool force_nonblock)
2142 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
2143 struct kiocb *kiocb = &req->rw.kiocb;
2144 struct iov_iter iter;
2146 ssize_t io_size, ret;
2148 ret = io_import_iovec(READ, req, &iovec, &iter);
2152 /* Ensure we clear previously set non-block flag */
2153 if (!force_nonblock)
2154 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
2158 if (req->flags & REQ_F_LINK)
2159 req->result = io_size;
2162 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2163 * we know to async punt it even if it was opened O_NONBLOCK
2165 if (force_nonblock && !io_file_supports_async(req->file)) {
2166 req->flags |= REQ_F_MUST_PUNT;
2170 iov_count = iov_iter_count(&iter);
2171 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
2175 if (req->file->f_op->read_iter)
2176 ret2 = call_read_iter(req->file, kiocb, &iter);
2178 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
2180 /* Catch -EAGAIN return for forced non-blocking submission */
2181 if (!force_nonblock || ret2 != -EAGAIN) {
2182 kiocb_done(kiocb, ret2, nxt, req->in_async);
2185 ret = io_setup_async_rw(req, io_size, iovec,
2186 inline_vecs, &iter);
2193 if (!io_wq_current_is_worker())
2198 static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2199 bool force_nonblock)
2201 struct io_async_ctx *io;
2202 struct iov_iter iter;
2205 ret = io_prep_rw(req, sqe, force_nonblock);
2209 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2216 io->rw.iov = io->rw.fast_iov;
2218 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter);
2223 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2227 static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
2228 bool force_nonblock)
2230 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
2231 struct kiocb *kiocb = &req->rw.kiocb;
2232 struct iov_iter iter;
2234 ssize_t ret, io_size;
2236 ret = io_import_iovec(WRITE, req, &iovec, &iter);
2240 /* Ensure we clear previously set non-block flag */
2241 if (!force_nonblock)
2242 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
2246 if (req->flags & REQ_F_LINK)
2247 req->result = io_size;
2250 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2251 * we know to async punt it even if it was opened O_NONBLOCK
2253 if (force_nonblock && !io_file_supports_async(req->file)) {
2254 req->flags |= REQ_F_MUST_PUNT;
2258 /* file path doesn't support NOWAIT for non-direct_IO */
2259 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2260 (req->flags & REQ_F_ISREG))
2263 iov_count = iov_iter_count(&iter);
2264 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
2269 * Open-code file_start_write here to grab freeze protection,
2270 * which will be released by another thread in
2271 * io_complete_rw(). Fool lockdep by telling it the lock got
2272 * released so that it doesn't complain about the held lock when
2273 * we return to userspace.
2275 if (req->flags & REQ_F_ISREG) {
2276 __sb_start_write(file_inode(req->file)->i_sb,
2277 SB_FREEZE_WRITE, true);
2278 __sb_writers_release(file_inode(req->file)->i_sb,
2281 kiocb->ki_flags |= IOCB_WRITE;
2283 if (req->file->f_op->write_iter)
2284 ret2 = call_write_iter(req->file, kiocb, &iter);
2286 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
2287 if (!force_nonblock || ret2 != -EAGAIN) {
2288 kiocb_done(kiocb, ret2, nxt, req->in_async);
2291 ret = io_setup_async_rw(req, io_size, iovec,
2292 inline_vecs, &iter);
2299 if (!io_wq_current_is_worker())
2305 * IORING_OP_NOP just posts a completion event, nothing else.
2307 static int io_nop(struct io_kiocb *req)
2309 struct io_ring_ctx *ctx = req->ctx;
2311 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2314 io_cqring_add_event(req, 0);
2319 static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2321 struct io_ring_ctx *ctx = req->ctx;
2326 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2328 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
2331 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2332 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2335 req->sync.off = READ_ONCE(sqe->off);
2336 req->sync.len = READ_ONCE(sqe->len);
2340 static bool io_req_cancelled(struct io_kiocb *req)
2342 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2343 req_set_fail_links(req);
2344 io_cqring_add_event(req, -ECANCELED);
2352 static void io_link_work_cb(struct io_wq_work **workptr)
2354 struct io_wq_work *work = *workptr;
2355 struct io_kiocb *link = work->data;
2357 io_queue_linked_timeout(link);
2358 work->func = io_wq_submit_work;
2361 static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
2363 struct io_kiocb *link;
2365 io_prep_async_work(nxt, &link);
2366 *workptr = &nxt->work;
2368 nxt->work.flags |= IO_WQ_WORK_CB;
2369 nxt->work.func = io_link_work_cb;
2370 nxt->work.data = link;
2374 static void io_fsync_finish(struct io_wq_work **workptr)
2376 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2377 loff_t end = req->sync.off + req->sync.len;
2378 struct io_kiocb *nxt = NULL;
2381 if (io_req_cancelled(req))
2384 ret = vfs_fsync_range(req->file, req->sync.off,
2385 end > 0 ? end : LLONG_MAX,
2386 req->sync.flags & IORING_FSYNC_DATASYNC);
2388 req_set_fail_links(req);
2389 io_cqring_add_event(req, ret);
2390 io_put_req_find_next(req, &nxt);
2392 io_wq_assign_next(workptr, nxt);
2395 static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt,
2396 bool force_nonblock)
2398 struct io_wq_work *work, *old_work;
2400 /* fsync always requires a blocking context */
2401 if (force_nonblock) {
2403 req->work.func = io_fsync_finish;
2407 work = old_work = &req->work;
2408 io_fsync_finish(&work);
2409 if (work && work != old_work)
2410 *nxt = container_of(work, struct io_kiocb, work);
2414 static void io_fallocate_finish(struct io_wq_work **workptr)
2416 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2417 struct io_kiocb *nxt = NULL;
2420 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2423 req_set_fail_links(req);
2424 io_cqring_add_event(req, ret);
2425 io_put_req_find_next(req, &nxt);
2427 io_wq_assign_next(workptr, nxt);
2430 static int io_fallocate_prep(struct io_kiocb *req,
2431 const struct io_uring_sqe *sqe)
2433 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2436 req->sync.off = READ_ONCE(sqe->off);
2437 req->sync.len = READ_ONCE(sqe->addr);
2438 req->sync.mode = READ_ONCE(sqe->len);
2442 static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt,
2443 bool force_nonblock)
2445 struct io_wq_work *work, *old_work;
2447 /* fallocate always requiring blocking context */
2448 if (force_nonblock) {
2450 req->work.func = io_fallocate_finish;
2454 work = old_work = &req->work;
2455 io_fallocate_finish(&work);
2456 if (work && work != old_work)
2457 *nxt = container_of(work, struct io_kiocb, work);
2462 static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2466 if (sqe->ioprio || sqe->buf_index)
2469 req->open.dfd = READ_ONCE(sqe->fd);
2470 req->open.mode = READ_ONCE(sqe->len);
2471 req->open.fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2472 req->open.flags = READ_ONCE(sqe->open_flags);
2474 req->open.filename = getname(req->open.fname);
2475 if (IS_ERR(req->open.filename)) {
2476 ret = PTR_ERR(req->open.filename);
2477 req->open.filename = NULL;
2484 static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt,
2485 bool force_nonblock)
2487 struct open_flags op;
2488 struct open_how how;
2492 if (force_nonblock) {
2493 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2497 how = build_open_how(req->open.flags, req->open.mode);
2498 ret = build_open_flags(&how, &op);
2502 ret = get_unused_fd_flags(how.flags);
2506 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2509 ret = PTR_ERR(file);
2511 fsnotify_open(file);
2512 fd_install(ret, file);
2515 putname(req->open.filename);
2517 req_set_fail_links(req);
2518 io_cqring_add_event(req, ret);
2519 io_put_req_find_next(req, nxt);
2523 static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2525 #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2526 if (sqe->ioprio || sqe->buf_index || sqe->off)
2529 req->madvise.addr = READ_ONCE(sqe->addr);
2530 req->madvise.len = READ_ONCE(sqe->len);
2531 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
2538 static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt,
2539 bool force_nonblock)
2541 #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2542 struct io_madvise *ma = &req->madvise;
2548 ret = do_madvise(ma->addr, ma->len, ma->advice);
2550 req_set_fail_links(req);
2551 io_cqring_add_event(req, ret);
2552 io_put_req_find_next(req, nxt);
2559 static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2561 if (sqe->ioprio || sqe->buf_index || sqe->addr)
2564 req->fadvise.offset = READ_ONCE(sqe->off);
2565 req->fadvise.len = READ_ONCE(sqe->len);
2566 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
2570 static int io_fadvise(struct io_kiocb *req, struct io_kiocb **nxt,
2571 bool force_nonblock)
2573 struct io_fadvise *fa = &req->fadvise;
2576 /* DONTNEED may block, others _should_ not */
2577 if (fa->advice == POSIX_FADV_DONTNEED && force_nonblock)
2580 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
2582 req_set_fail_links(req);
2583 io_cqring_add_event(req, ret);
2584 io_put_req_find_next(req, nxt);
2588 static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2590 unsigned lookup_flags;
2593 if (sqe->ioprio || sqe->buf_index)
2596 req->open.dfd = READ_ONCE(sqe->fd);
2597 req->open.mask = READ_ONCE(sqe->len);
2598 req->open.fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2599 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2600 req->open.flags = READ_ONCE(sqe->statx_flags);
2602 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.flags))
2605 req->open.filename = getname_flags(req->open.fname, lookup_flags, NULL);
2606 if (IS_ERR(req->open.filename)) {
2607 ret = PTR_ERR(req->open.filename);
2608 req->open.filename = NULL;
2615 static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt,
2616 bool force_nonblock)
2618 struct io_open *ctx = &req->open;
2619 unsigned lookup_flags;
2627 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->flags))
2631 /* filename_lookup() drops it, keep a reference */
2632 ctx->filename->refcnt++;
2634 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
2639 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->flags);
2641 if (retry_estale(ret, lookup_flags)) {
2642 lookup_flags |= LOOKUP_REVAL;
2646 ret = cp_statx(&stat, ctx->buffer);
2648 putname(ctx->filename);
2650 req_set_fail_links(req);
2651 io_cqring_add_event(req, ret);
2652 io_put_req_find_next(req, nxt);
2656 static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2659 * If we queue this for async, it must not be cancellable. That would
2660 * leave the 'file' in an undeterminate state.
2662 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
2664 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
2665 sqe->rw_flags || sqe->buf_index)
2667 if (sqe->flags & IOSQE_FIXED_FILE)
2670 req->close.fd = READ_ONCE(sqe->fd);
2671 if (req->file->f_op == &io_uring_fops ||
2672 req->close.fd == req->ring_fd)
2678 static void io_close_finish(struct io_wq_work **workptr)
2680 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2681 struct io_kiocb *nxt = NULL;
2683 /* Invoked with files, we need to do the close */
2684 if (req->work.files) {
2687 ret = filp_close(req->close.put_file, req->work.files);
2689 req_set_fail_links(req);
2691 io_cqring_add_event(req, ret);
2694 fput(req->close.put_file);
2696 /* we bypassed the re-issue, drop the submission reference */
2698 io_put_req_find_next(req, &nxt);
2700 io_wq_assign_next(workptr, nxt);
2703 static int io_close(struct io_kiocb *req, struct io_kiocb **nxt,
2704 bool force_nonblock)
2708 req->close.put_file = NULL;
2709 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
2713 /* if the file has a flush method, be safe and punt to async */
2714 if (req->close.put_file->f_op->flush && !io_wq_current_is_worker()) {
2715 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2720 * No ->flush(), safely close from here and just punt the
2721 * fput() to async context.
2723 ret = filp_close(req->close.put_file, current->files);
2726 req_set_fail_links(req);
2727 io_cqring_add_event(req, ret);
2729 if (io_wq_current_is_worker()) {
2730 struct io_wq_work *old_work, *work;
2732 old_work = work = &req->work;
2733 io_close_finish(&work);
2734 if (work && work != old_work)
2735 *nxt = container_of(work, struct io_kiocb, work);
2740 req->work.func = io_close_finish;
2744 static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2746 struct io_ring_ctx *ctx = req->ctx;
2751 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2753 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
2756 req->sync.off = READ_ONCE(sqe->off);
2757 req->sync.len = READ_ONCE(sqe->len);
2758 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
2762 static void io_sync_file_range_finish(struct io_wq_work **workptr)
2764 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2765 struct io_kiocb *nxt = NULL;
2768 if (io_req_cancelled(req))
2771 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
2774 req_set_fail_links(req);
2775 io_cqring_add_event(req, ret);
2776 io_put_req_find_next(req, &nxt);
2778 io_wq_assign_next(workptr, nxt);
2781 static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt,
2782 bool force_nonblock)
2784 struct io_wq_work *work, *old_work;
2786 /* sync_file_range always requires a blocking context */
2787 if (force_nonblock) {
2789 req->work.func = io_sync_file_range_finish;
2793 work = old_work = &req->work;
2794 io_sync_file_range_finish(&work);
2795 if (work && work != old_work)
2796 *nxt = container_of(work, struct io_kiocb, work);
2800 #if defined(CONFIG_NET)
2801 static void io_sendrecv_async(struct io_wq_work **workptr)
2803 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2804 struct iovec *iov = NULL;
2806 if (req->io->rw.iov != req->io->rw.fast_iov)
2807 iov = req->io->msg.iov;
2808 io_wq_submit_work(workptr);
2813 static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2815 #if defined(CONFIG_NET)
2816 struct io_sr_msg *sr = &req->sr_msg;
2817 struct io_async_ctx *io = req->io;
2819 sr->msg_flags = READ_ONCE(sqe->msg_flags);
2820 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
2821 sr->len = READ_ONCE(sqe->len);
2823 if (!io || req->opcode == IORING_OP_SEND)
2826 io->msg.iov = io->msg.fast_iov;
2827 return sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
2834 static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2835 bool force_nonblock)
2837 #if defined(CONFIG_NET)
2838 struct io_async_msghdr *kmsg = NULL;
2839 struct socket *sock;
2842 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2845 sock = sock_from_file(req->file, &ret);
2847 struct io_async_ctx io;
2848 struct sockaddr_storage addr;
2852 kmsg = &req->io->msg;
2853 kmsg->msg.msg_name = &addr;
2854 /* if iov is set, it's allocated already */
2856 kmsg->iov = kmsg->fast_iov;
2857 kmsg->msg.msg_iter.iov = kmsg->iov;
2859 struct io_sr_msg *sr = &req->sr_msg;
2862 kmsg->msg.msg_name = &addr;
2864 io.msg.iov = io.msg.fast_iov;
2865 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
2866 sr->msg_flags, &io.msg.iov);
2871 flags = req->sr_msg.msg_flags;
2872 if (flags & MSG_DONTWAIT)
2873 req->flags |= REQ_F_NOWAIT;
2874 else if (force_nonblock)
2875 flags |= MSG_DONTWAIT;
2877 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
2878 if (force_nonblock && ret == -EAGAIN) {
2881 if (io_alloc_async_ctx(req))
2883 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
2884 req->work.func = io_sendrecv_async;
2887 if (ret == -ERESTARTSYS)
2891 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
2893 io_cqring_add_event(req, ret);
2895 req_set_fail_links(req);
2896 io_put_req_find_next(req, nxt);
2903 static int io_send(struct io_kiocb *req, struct io_kiocb **nxt,
2904 bool force_nonblock)
2906 #if defined(CONFIG_NET)
2907 struct socket *sock;
2910 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2913 sock = sock_from_file(req->file, &ret);
2915 struct io_sr_msg *sr = &req->sr_msg;
2920 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
2925 msg.msg_name = NULL;
2926 msg.msg_control = NULL;
2927 msg.msg_controllen = 0;
2928 msg.msg_namelen = 0;
2930 flags = req->sr_msg.msg_flags;
2931 if (flags & MSG_DONTWAIT)
2932 req->flags |= REQ_F_NOWAIT;
2933 else if (force_nonblock)
2934 flags |= MSG_DONTWAIT;
2936 ret = __sys_sendmsg_sock(sock, &msg, flags);
2937 if (force_nonblock && ret == -EAGAIN)
2939 if (ret == -ERESTARTSYS)
2943 io_cqring_add_event(req, ret);
2945 req_set_fail_links(req);
2946 io_put_req_find_next(req, nxt);
2953 static int io_recvmsg_prep(struct io_kiocb *req,
2954 const struct io_uring_sqe *sqe)
2956 #if defined(CONFIG_NET)
2957 struct io_sr_msg *sr = &req->sr_msg;
2958 struct io_async_ctx *io = req->io;
2960 sr->msg_flags = READ_ONCE(sqe->msg_flags);
2961 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
2963 if (!io || req->opcode == IORING_OP_RECV)
2966 io->msg.iov = io->msg.fast_iov;
2967 return recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
2968 &io->msg.uaddr, &io->msg.iov);
2974 static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2975 bool force_nonblock)
2977 #if defined(CONFIG_NET)
2978 struct io_async_msghdr *kmsg = NULL;
2979 struct socket *sock;
2982 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2985 sock = sock_from_file(req->file, &ret);
2987 struct io_async_ctx io;
2988 struct sockaddr_storage addr;
2992 kmsg = &req->io->msg;
2993 kmsg->msg.msg_name = &addr;
2994 /* if iov is set, it's allocated already */
2996 kmsg->iov = kmsg->fast_iov;
2997 kmsg->msg.msg_iter.iov = kmsg->iov;
2999 struct io_sr_msg *sr = &req->sr_msg;
3002 kmsg->msg.msg_name = &addr;
3004 io.msg.iov = io.msg.fast_iov;
3005 ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg,
3006 sr->msg_flags, &io.msg.uaddr,
3012 flags = req->sr_msg.msg_flags;
3013 if (flags & MSG_DONTWAIT)
3014 req->flags |= REQ_F_NOWAIT;
3015 else if (force_nonblock)
3016 flags |= MSG_DONTWAIT;
3018 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3019 kmsg->uaddr, flags);
3020 if (force_nonblock && ret == -EAGAIN) {
3023 if (io_alloc_async_ctx(req))
3025 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
3026 req->work.func = io_sendrecv_async;
3029 if (ret == -ERESTARTSYS)
3033 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
3035 io_cqring_add_event(req, ret);
3037 req_set_fail_links(req);
3038 io_put_req_find_next(req, nxt);
3045 static int io_recv(struct io_kiocb *req, struct io_kiocb **nxt,
3046 bool force_nonblock)
3048 #if defined(CONFIG_NET)
3049 struct socket *sock;
3052 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3055 sock = sock_from_file(req->file, &ret);
3057 struct io_sr_msg *sr = &req->sr_msg;
3062 ret = import_single_range(READ, sr->buf, sr->len, &iov,
3067 msg.msg_name = NULL;
3068 msg.msg_control = NULL;
3069 msg.msg_controllen = 0;
3070 msg.msg_namelen = 0;
3071 msg.msg_iocb = NULL;
3074 flags = req->sr_msg.msg_flags;
3075 if (flags & MSG_DONTWAIT)
3076 req->flags |= REQ_F_NOWAIT;
3077 else if (force_nonblock)
3078 flags |= MSG_DONTWAIT;
3080 ret = __sys_recvmsg_sock(sock, &msg, NULL, NULL, flags);
3081 if (force_nonblock && ret == -EAGAIN)
3083 if (ret == -ERESTARTSYS)
3087 io_cqring_add_event(req, ret);
3089 req_set_fail_links(req);
3090 io_put_req_find_next(req, nxt);
3098 static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3100 #if defined(CONFIG_NET)
3101 struct io_accept *accept = &req->accept;
3103 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3105 if (sqe->ioprio || sqe->len || sqe->buf_index)
3108 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3109 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3110 accept->flags = READ_ONCE(sqe->accept_flags);
3117 #if defined(CONFIG_NET)
3118 static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3119 bool force_nonblock)
3121 struct io_accept *accept = &req->accept;
3122 unsigned file_flags;
3125 file_flags = force_nonblock ? O_NONBLOCK : 0;
3126 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
3127 accept->addr_len, accept->flags);
3128 if (ret == -EAGAIN && force_nonblock)
3130 if (ret == -ERESTARTSYS)
3133 req_set_fail_links(req);
3134 io_cqring_add_event(req, ret);
3135 io_put_req_find_next(req, nxt);
3139 static void io_accept_finish(struct io_wq_work **workptr)
3141 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3142 struct io_kiocb *nxt = NULL;
3144 if (io_req_cancelled(req))
3146 __io_accept(req, &nxt, false);
3148 io_wq_assign_next(workptr, nxt);
3152 static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3153 bool force_nonblock)
3155 #if defined(CONFIG_NET)
3158 ret = __io_accept(req, nxt, force_nonblock);
3159 if (ret == -EAGAIN && force_nonblock) {
3160 req->work.func = io_accept_finish;
3161 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
3171 static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3173 #if defined(CONFIG_NET)
3174 struct io_connect *conn = &req->connect;
3175 struct io_async_ctx *io = req->io;
3177 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3179 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
3182 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3183 conn->addr_len = READ_ONCE(sqe->addr2);
3188 return move_addr_to_kernel(conn->addr, conn->addr_len,
3189 &io->connect.address);
3195 static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt,
3196 bool force_nonblock)
3198 #if defined(CONFIG_NET)
3199 struct io_async_ctx __io, *io;
3200 unsigned file_flags;
3206 ret = move_addr_to_kernel(req->connect.addr,
3207 req->connect.addr_len,
3208 &__io.connect.address);
3214 file_flags = force_nonblock ? O_NONBLOCK : 0;
3216 ret = __sys_connect_file(req->file, &io->connect.address,
3217 req->connect.addr_len, file_flags);
3218 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
3221 if (io_alloc_async_ctx(req)) {
3225 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
3228 if (ret == -ERESTARTSYS)
3232 req_set_fail_links(req);
3233 io_cqring_add_event(req, ret);
3234 io_put_req_find_next(req, nxt);
3241 static void io_poll_remove_one(struct io_kiocb *req)
3243 struct io_poll_iocb *poll = &req->poll;
3245 spin_lock(&poll->head->lock);
3246 WRITE_ONCE(poll->canceled, true);
3247 if (!list_empty(&poll->wait.entry)) {
3248 list_del_init(&poll->wait.entry);
3249 io_queue_async_work(req);
3251 spin_unlock(&poll->head->lock);
3252 hash_del(&req->hash_node);
3255 static void io_poll_remove_all(struct io_ring_ctx *ctx)
3257 struct hlist_node *tmp;
3258 struct io_kiocb *req;
3261 spin_lock_irq(&ctx->completion_lock);
3262 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
3263 struct hlist_head *list;
3265 list = &ctx->cancel_hash[i];
3266 hlist_for_each_entry_safe(req, tmp, list, hash_node)
3267 io_poll_remove_one(req);
3269 spin_unlock_irq(&ctx->completion_lock);
3272 static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
3274 struct hlist_head *list;
3275 struct io_kiocb *req;
3277 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
3278 hlist_for_each_entry(req, list, hash_node) {
3279 if (sqe_addr == req->user_data) {
3280 io_poll_remove_one(req);
3288 static int io_poll_remove_prep(struct io_kiocb *req,
3289 const struct io_uring_sqe *sqe)
3291 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3293 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3297 req->poll.addr = READ_ONCE(sqe->addr);
3302 * Find a running poll command that matches one specified in sqe->addr,
3303 * and remove it if found.
3305 static int io_poll_remove(struct io_kiocb *req)
3307 struct io_ring_ctx *ctx = req->ctx;
3311 addr = req->poll.addr;
3312 spin_lock_irq(&ctx->completion_lock);
3313 ret = io_poll_cancel(ctx, addr);
3314 spin_unlock_irq(&ctx->completion_lock);
3316 io_cqring_add_event(req, ret);
3318 req_set_fail_links(req);
3323 static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
3325 struct io_ring_ctx *ctx = req->ctx;
3327 req->poll.done = true;
3329 io_cqring_fill_event(req, error);
3331 io_cqring_fill_event(req, mangle_poll(mask));
3332 io_commit_cqring(ctx);
3335 static void io_poll_complete_work(struct io_wq_work **workptr)
3337 struct io_wq_work *work = *workptr;
3338 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3339 struct io_poll_iocb *poll = &req->poll;
3340 struct poll_table_struct pt = { ._key = poll->events };
3341 struct io_ring_ctx *ctx = req->ctx;
3342 struct io_kiocb *nxt = NULL;
3346 if (work->flags & IO_WQ_WORK_CANCEL) {
3347 WRITE_ONCE(poll->canceled, true);
3349 } else if (READ_ONCE(poll->canceled)) {
3353 if (ret != -ECANCELED)
3354 mask = vfs_poll(poll->file, &pt) & poll->events;
3357 * Note that ->ki_cancel callers also delete iocb from active_reqs after
3358 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
3359 * synchronize with them. In the cancellation case the list_del_init
3360 * itself is not actually needed, but harmless so we keep it in to
3361 * avoid further branches in the fast path.
3363 spin_lock_irq(&ctx->completion_lock);
3364 if (!mask && ret != -ECANCELED) {
3365 add_wait_queue(poll->head, &poll->wait);
3366 spin_unlock_irq(&ctx->completion_lock);
3369 hash_del(&req->hash_node);
3370 io_poll_complete(req, mask, ret);
3371 spin_unlock_irq(&ctx->completion_lock);
3373 io_cqring_ev_posted(ctx);
3376 req_set_fail_links(req);
3377 io_put_req_find_next(req, &nxt);
3379 io_wq_assign_next(workptr, nxt);
3382 static void __io_poll_flush(struct io_ring_ctx *ctx, struct llist_node *nodes)
3384 struct io_kiocb *req, *tmp;
3385 struct req_batch rb;
3387 rb.to_free = rb.need_iter = 0;
3388 spin_lock_irq(&ctx->completion_lock);
3389 llist_for_each_entry_safe(req, tmp, nodes, llist_node) {
3390 hash_del(&req->hash_node);
3391 io_poll_complete(req, req->result, 0);
3393 if (refcount_dec_and_test(&req->refs) &&
3394 !io_req_multi_free(&rb, req)) {
3395 req->flags |= REQ_F_COMP_LOCKED;
3399 spin_unlock_irq(&ctx->completion_lock);
3401 io_cqring_ev_posted(ctx);
3402 io_free_req_many(ctx, &rb);
3405 static void io_poll_flush(struct io_wq_work **workptr)
3407 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3408 struct llist_node *nodes;
3410 nodes = llist_del_all(&req->ctx->poll_llist);
3412 __io_poll_flush(req->ctx, nodes);
3415 static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
3418 struct io_poll_iocb *poll = wait->private;
3419 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
3420 struct io_ring_ctx *ctx = req->ctx;
3421 __poll_t mask = key_to_poll(key);
3423 /* for instances that support it check for an event match first: */
3424 if (mask && !(mask & poll->events))
3427 list_del_init(&poll->wait.entry);
3430 * Run completion inline if we can. We're using trylock here because
3431 * we are violating the completion_lock -> poll wq lock ordering.
3432 * If we have a link timeout we're going to need the completion_lock
3433 * for finalizing the request, mark us as having grabbed that already.
3436 unsigned long flags;
3438 if (llist_empty(&ctx->poll_llist) &&
3439 spin_trylock_irqsave(&ctx->completion_lock, flags)) {
3440 hash_del(&req->hash_node);
3441 io_poll_complete(req, mask, 0);
3442 req->flags |= REQ_F_COMP_LOCKED;
3444 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3446 io_cqring_ev_posted(ctx);
3450 req->llist_node.next = NULL;
3451 /* if the list wasn't empty, we're done */
3452 if (!llist_add(&req->llist_node, &ctx->poll_llist))
3455 req->work.func = io_poll_flush;
3459 io_queue_async_work(req);
3464 struct io_poll_table {
3465 struct poll_table_struct pt;
3466 struct io_kiocb *req;
3470 static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
3471 struct poll_table_struct *p)
3473 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3475 if (unlikely(pt->req->poll.head)) {
3476 pt->error = -EINVAL;
3481 pt->req->poll.head = head;
3482 add_wait_queue(head, &pt->req->poll.wait);
3485 static void io_poll_req_insert(struct io_kiocb *req)
3487 struct io_ring_ctx *ctx = req->ctx;
3488 struct hlist_head *list;
3490 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
3491 hlist_add_head(&req->hash_node, list);
3494 static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3496 struct io_poll_iocb *poll = &req->poll;
3499 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3501 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
3506 events = READ_ONCE(sqe->poll_events);
3507 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
3511 static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt)
3513 struct io_poll_iocb *poll = &req->poll;
3514 struct io_ring_ctx *ctx = req->ctx;
3515 struct io_poll_table ipt;
3516 bool cancel = false;
3519 INIT_IO_WORK(&req->work, io_poll_complete_work);
3520 INIT_HLIST_NODE(&req->hash_node);
3524 poll->canceled = false;
3526 ipt.pt._qproc = io_poll_queue_proc;
3527 ipt.pt._key = poll->events;
3529 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
3531 /* initialized the list so that we can do list_empty checks */
3532 INIT_LIST_HEAD(&poll->wait.entry);
3533 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
3534 poll->wait.private = poll;
3536 INIT_LIST_HEAD(&req->list);
3538 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
3540 spin_lock_irq(&ctx->completion_lock);
3541 if (likely(poll->head)) {
3542 spin_lock(&poll->head->lock);
3543 if (unlikely(list_empty(&poll->wait.entry))) {
3549 if (mask || ipt.error)
3550 list_del_init(&poll->wait.entry);
3552 WRITE_ONCE(poll->canceled, true);
3553 else if (!poll->done) /* actually waiting for an event */
3554 io_poll_req_insert(req);
3555 spin_unlock(&poll->head->lock);
3557 if (mask) { /* no async, we'd stolen it */
3559 io_poll_complete(req, mask, 0);
3561 spin_unlock_irq(&ctx->completion_lock);
3564 io_cqring_ev_posted(ctx);
3565 io_put_req_find_next(req, nxt);
3570 static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
3572 struct io_timeout_data *data = container_of(timer,
3573 struct io_timeout_data, timer);
3574 struct io_kiocb *req = data->req;
3575 struct io_ring_ctx *ctx = req->ctx;
3576 unsigned long flags;
3578 atomic_inc(&ctx->cq_timeouts);
3580 spin_lock_irqsave(&ctx->completion_lock, flags);
3582 * We could be racing with timeout deletion. If the list is empty,
3583 * then timeout lookup already found it and will be handling it.
3585 if (!list_empty(&req->list)) {
3586 struct io_kiocb *prev;
3589 * Adjust the reqs sequence before the current one because it
3590 * will consume a slot in the cq_ring and the cq_tail
3591 * pointer will be increased, otherwise other timeout reqs may
3592 * return in advance without waiting for enough wait_nr.
3595 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
3597 list_del_init(&req->list);
3600 io_cqring_fill_event(req, -ETIME);
3601 io_commit_cqring(ctx);
3602 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3604 io_cqring_ev_posted(ctx);
3605 req_set_fail_links(req);
3607 return HRTIMER_NORESTART;
3610 static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
3612 struct io_kiocb *req;
3615 list_for_each_entry(req, &ctx->timeout_list, list) {
3616 if (user_data == req->user_data) {
3617 list_del_init(&req->list);
3626 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
3630 req_set_fail_links(req);
3631 io_cqring_fill_event(req, -ECANCELED);
3636 static int io_timeout_remove_prep(struct io_kiocb *req,
3637 const struct io_uring_sqe *sqe)
3639 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3641 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
3644 req->timeout.addr = READ_ONCE(sqe->addr);
3645 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
3646 if (req->timeout.flags)
3653 * Remove or update an existing timeout command
3655 static int io_timeout_remove(struct io_kiocb *req)
3657 struct io_ring_ctx *ctx = req->ctx;
3660 spin_lock_irq(&ctx->completion_lock);
3661 ret = io_timeout_cancel(ctx, req->timeout.addr);
3663 io_cqring_fill_event(req, ret);
3664 io_commit_cqring(ctx);
3665 spin_unlock_irq(&ctx->completion_lock);
3666 io_cqring_ev_posted(ctx);
3668 req_set_fail_links(req);
3673 static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3674 bool is_timeout_link)
3676 struct io_timeout_data *data;
3679 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3681 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
3683 if (sqe->off && is_timeout_link)
3685 flags = READ_ONCE(sqe->timeout_flags);
3686 if (flags & ~IORING_TIMEOUT_ABS)
3689 req->timeout.count = READ_ONCE(sqe->off);
3691 if (!req->io && io_alloc_async_ctx(req))
3694 data = &req->io->timeout;
3696 req->flags |= REQ_F_TIMEOUT;
3698 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
3701 if (flags & IORING_TIMEOUT_ABS)
3702 data->mode = HRTIMER_MODE_ABS;
3704 data->mode = HRTIMER_MODE_REL;
3706 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
3710 static int io_timeout(struct io_kiocb *req)
3713 struct io_ring_ctx *ctx = req->ctx;
3714 struct io_timeout_data *data;
3715 struct list_head *entry;
3718 data = &req->io->timeout;
3721 * sqe->off holds how many events that need to occur for this
3722 * timeout event to be satisfied. If it isn't set, then this is
3723 * a pure timeout request, sequence isn't used.
3725 count = req->timeout.count;
3727 req->flags |= REQ_F_TIMEOUT_NOSEQ;
3728 spin_lock_irq(&ctx->completion_lock);
3729 entry = ctx->timeout_list.prev;
3733 req->sequence = ctx->cached_sq_head + count - 1;
3734 data->seq_offset = count;
3737 * Insertion sort, ensuring the first entry in the list is always
3738 * the one we need first.
3740 spin_lock_irq(&ctx->completion_lock);
3741 list_for_each_prev(entry, &ctx->timeout_list) {
3742 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
3743 unsigned nxt_sq_head;
3744 long long tmp, tmp_nxt;
3745 u32 nxt_offset = nxt->io->timeout.seq_offset;
3747 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
3751 * Since cached_sq_head + count - 1 can overflow, use type long
3754 tmp = (long long)ctx->cached_sq_head + count - 1;
3755 nxt_sq_head = nxt->sequence - nxt_offset + 1;
3756 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
3759 * cached_sq_head may overflow, and it will never overflow twice
3760 * once there is some timeout req still be valid.
3762 if (ctx->cached_sq_head < nxt_sq_head)
3769 * Sequence of reqs after the insert one and itself should
3770 * be adjusted because each timeout req consumes a slot.
3775 req->sequence -= span;
3777 list_add(&req->list, entry);
3778 data->timer.function = io_timeout_fn;
3779 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
3780 spin_unlock_irq(&ctx->completion_lock);
3784 static bool io_cancel_cb(struct io_wq_work *work, void *data)
3786 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3788 return req->user_data == (unsigned long) data;
3791 static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
3793 enum io_wq_cancel cancel_ret;
3796 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
3797 switch (cancel_ret) {
3798 case IO_WQ_CANCEL_OK:
3801 case IO_WQ_CANCEL_RUNNING:
3804 case IO_WQ_CANCEL_NOTFOUND:
3812 static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
3813 struct io_kiocb *req, __u64 sqe_addr,
3814 struct io_kiocb **nxt, int success_ret)
3816 unsigned long flags;
3819 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
3820 if (ret != -ENOENT) {
3821 spin_lock_irqsave(&ctx->completion_lock, flags);
3825 spin_lock_irqsave(&ctx->completion_lock, flags);
3826 ret = io_timeout_cancel(ctx, sqe_addr);
3829 ret = io_poll_cancel(ctx, sqe_addr);
3833 io_cqring_fill_event(req, ret);
3834 io_commit_cqring(ctx);
3835 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3836 io_cqring_ev_posted(ctx);
3839 req_set_fail_links(req);
3840 io_put_req_find_next(req, nxt);
3843 static int io_async_cancel_prep(struct io_kiocb *req,
3844 const struct io_uring_sqe *sqe)
3846 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3848 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
3852 req->cancel.addr = READ_ONCE(sqe->addr);
3856 static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt)
3858 struct io_ring_ctx *ctx = req->ctx;
3860 io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0);
3864 static int io_files_update_prep(struct io_kiocb *req,
3865 const struct io_uring_sqe *sqe)
3867 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
3870 req->files_update.offset = READ_ONCE(sqe->off);
3871 req->files_update.nr_args = READ_ONCE(sqe->len);
3872 if (!req->files_update.nr_args)
3874 req->files_update.arg = READ_ONCE(sqe->addr);
3878 static int io_files_update(struct io_kiocb *req, bool force_nonblock)
3880 struct io_ring_ctx *ctx = req->ctx;
3881 struct io_uring_files_update up;
3884 if (force_nonblock) {
3885 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
3889 up.offset = req->files_update.offset;
3890 up.fds = req->files_update.arg;
3892 mutex_lock(&ctx->uring_lock);
3893 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
3894 mutex_unlock(&ctx->uring_lock);
3897 req_set_fail_links(req);
3898 io_cqring_add_event(req, ret);
3903 static int io_req_defer_prep(struct io_kiocb *req,
3904 const struct io_uring_sqe *sqe)
3908 switch (req->opcode) {
3911 case IORING_OP_READV:
3912 case IORING_OP_READ_FIXED:
3913 case IORING_OP_READ:
3914 ret = io_read_prep(req, sqe, true);
3916 case IORING_OP_WRITEV:
3917 case IORING_OP_WRITE_FIXED:
3918 case IORING_OP_WRITE:
3919 ret = io_write_prep(req, sqe, true);
3921 case IORING_OP_POLL_ADD:
3922 ret = io_poll_add_prep(req, sqe);
3924 case IORING_OP_POLL_REMOVE:
3925 ret = io_poll_remove_prep(req, sqe);
3927 case IORING_OP_FSYNC:
3928 ret = io_prep_fsync(req, sqe);
3930 case IORING_OP_SYNC_FILE_RANGE:
3931 ret = io_prep_sfr(req, sqe);
3933 case IORING_OP_SENDMSG:
3934 case IORING_OP_SEND:
3935 ret = io_sendmsg_prep(req, sqe);
3937 case IORING_OP_RECVMSG:
3938 case IORING_OP_RECV:
3939 ret = io_recvmsg_prep(req, sqe);
3941 case IORING_OP_CONNECT:
3942 ret = io_connect_prep(req, sqe);
3944 case IORING_OP_TIMEOUT:
3945 ret = io_timeout_prep(req, sqe, false);
3947 case IORING_OP_TIMEOUT_REMOVE:
3948 ret = io_timeout_remove_prep(req, sqe);
3950 case IORING_OP_ASYNC_CANCEL:
3951 ret = io_async_cancel_prep(req, sqe);
3953 case IORING_OP_LINK_TIMEOUT:
3954 ret = io_timeout_prep(req, sqe, true);
3956 case IORING_OP_ACCEPT:
3957 ret = io_accept_prep(req, sqe);
3959 case IORING_OP_FALLOCATE:
3960 ret = io_fallocate_prep(req, sqe);
3962 case IORING_OP_OPENAT:
3963 ret = io_openat_prep(req, sqe);
3965 case IORING_OP_CLOSE:
3966 ret = io_close_prep(req, sqe);
3968 case IORING_OP_FILES_UPDATE:
3969 ret = io_files_update_prep(req, sqe);
3971 case IORING_OP_STATX:
3972 ret = io_statx_prep(req, sqe);
3974 case IORING_OP_FADVISE:
3975 ret = io_fadvise_prep(req, sqe);
3977 case IORING_OP_MADVISE:
3978 ret = io_madvise_prep(req, sqe);
3981 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
3990 static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3992 struct io_ring_ctx *ctx = req->ctx;
3995 /* Still need defer if there is pending req in defer list. */
3996 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
3999 if (!req->io && io_alloc_async_ctx(req))
4002 ret = io_req_defer_prep(req, sqe);
4006 spin_lock_irq(&ctx->completion_lock);
4007 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
4008 spin_unlock_irq(&ctx->completion_lock);
4012 trace_io_uring_defer(ctx, req, req->user_data);
4013 list_add_tail(&req->list, &ctx->defer_list);
4014 spin_unlock_irq(&ctx->completion_lock);
4015 return -EIOCBQUEUED;
4018 static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4019 struct io_kiocb **nxt, bool force_nonblock)
4021 struct io_ring_ctx *ctx = req->ctx;
4024 switch (req->opcode) {
4028 case IORING_OP_READV:
4029 case IORING_OP_READ_FIXED:
4030 case IORING_OP_READ:
4032 ret = io_read_prep(req, sqe, force_nonblock);
4036 ret = io_read(req, nxt, force_nonblock);
4038 case IORING_OP_WRITEV:
4039 case IORING_OP_WRITE_FIXED:
4040 case IORING_OP_WRITE:
4042 ret = io_write_prep(req, sqe, force_nonblock);
4046 ret = io_write(req, nxt, force_nonblock);
4048 case IORING_OP_FSYNC:
4050 ret = io_prep_fsync(req, sqe);
4054 ret = io_fsync(req, nxt, force_nonblock);
4056 case IORING_OP_POLL_ADD:
4058 ret = io_poll_add_prep(req, sqe);
4062 ret = io_poll_add(req, nxt);
4064 case IORING_OP_POLL_REMOVE:
4066 ret = io_poll_remove_prep(req, sqe);
4070 ret = io_poll_remove(req);
4072 case IORING_OP_SYNC_FILE_RANGE:
4074 ret = io_prep_sfr(req, sqe);
4078 ret = io_sync_file_range(req, nxt, force_nonblock);
4080 case IORING_OP_SENDMSG:
4081 case IORING_OP_SEND:
4083 ret = io_sendmsg_prep(req, sqe);
4087 if (req->opcode == IORING_OP_SENDMSG)
4088 ret = io_sendmsg(req, nxt, force_nonblock);
4090 ret = io_send(req, nxt, force_nonblock);
4092 case IORING_OP_RECVMSG:
4093 case IORING_OP_RECV:
4095 ret = io_recvmsg_prep(req, sqe);
4099 if (req->opcode == IORING_OP_RECVMSG)
4100 ret = io_recvmsg(req, nxt, force_nonblock);
4102 ret = io_recv(req, nxt, force_nonblock);
4104 case IORING_OP_TIMEOUT:
4106 ret = io_timeout_prep(req, sqe, false);
4110 ret = io_timeout(req);
4112 case IORING_OP_TIMEOUT_REMOVE:
4114 ret = io_timeout_remove_prep(req, sqe);
4118 ret = io_timeout_remove(req);
4120 case IORING_OP_ACCEPT:
4122 ret = io_accept_prep(req, sqe);
4126 ret = io_accept(req, nxt, force_nonblock);
4128 case IORING_OP_CONNECT:
4130 ret = io_connect_prep(req, sqe);
4134 ret = io_connect(req, nxt, force_nonblock);
4136 case IORING_OP_ASYNC_CANCEL:
4138 ret = io_async_cancel_prep(req, sqe);
4142 ret = io_async_cancel(req, nxt);
4144 case IORING_OP_FALLOCATE:
4146 ret = io_fallocate_prep(req, sqe);
4150 ret = io_fallocate(req, nxt, force_nonblock);
4152 case IORING_OP_OPENAT:
4154 ret = io_openat_prep(req, sqe);
4158 ret = io_openat(req, nxt, force_nonblock);
4160 case IORING_OP_CLOSE:
4162 ret = io_close_prep(req, sqe);
4166 ret = io_close(req, nxt, force_nonblock);
4168 case IORING_OP_FILES_UPDATE:
4170 ret = io_files_update_prep(req, sqe);
4174 ret = io_files_update(req, force_nonblock);
4176 case IORING_OP_STATX:
4178 ret = io_statx_prep(req, sqe);
4182 ret = io_statx(req, nxt, force_nonblock);
4184 case IORING_OP_FADVISE:
4186 ret = io_fadvise_prep(req, sqe);
4190 ret = io_fadvise(req, nxt, force_nonblock);
4192 case IORING_OP_MADVISE:
4194 ret = io_madvise_prep(req, sqe);
4198 ret = io_madvise(req, nxt, force_nonblock);
4208 if (ctx->flags & IORING_SETUP_IOPOLL) {
4209 const bool in_async = io_wq_current_is_worker();
4211 if (req->result == -EAGAIN)
4214 /* workqueue context doesn't hold uring_lock, grab it now */
4216 mutex_lock(&ctx->uring_lock);
4218 io_iopoll_req_issued(req);
4221 mutex_unlock(&ctx->uring_lock);
4227 static void io_wq_submit_work(struct io_wq_work **workptr)
4229 struct io_wq_work *work = *workptr;
4230 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
4231 struct io_kiocb *nxt = NULL;
4234 /* if NO_CANCEL is set, we must still run the work */
4235 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
4236 IO_WQ_WORK_CANCEL) {
4241 req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
4242 req->in_async = true;
4244 ret = io_issue_sqe(req, NULL, &nxt, false);
4246 * We can get EAGAIN for polled IO even though we're
4247 * forcing a sync submission from here, since we can't
4248 * wait for request slots on the block side.
4256 /* drop submission reference */
4260 req_set_fail_links(req);
4261 io_cqring_add_event(req, ret);
4265 /* if a dependent link is ready, pass it back */
4267 io_wq_assign_next(workptr, nxt);
4270 static int io_req_needs_file(struct io_kiocb *req, int fd)
4272 if (!io_op_defs[req->opcode].needs_file)
4274 if (fd == -1 && io_op_defs[req->opcode].fd_non_neg)
4279 static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
4282 struct fixed_file_table *table;
4284 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
4285 return table->files[index & IORING_FILE_TABLE_MASK];;
4288 static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
4289 const struct io_uring_sqe *sqe)
4291 struct io_ring_ctx *ctx = req->ctx;
4295 flags = READ_ONCE(sqe->flags);
4296 fd = READ_ONCE(sqe->fd);
4298 if (flags & IOSQE_IO_DRAIN)
4299 req->flags |= REQ_F_IO_DRAIN;
4301 if (!io_req_needs_file(req, fd))
4304 if (flags & IOSQE_FIXED_FILE) {
4305 if (unlikely(!ctx->file_data ||
4306 (unsigned) fd >= ctx->nr_user_files))
4308 fd = array_index_nospec(fd, ctx->nr_user_files);
4309 req->file = io_file_from_index(ctx, fd);
4312 req->flags |= REQ_F_FIXED_FILE;
4313 percpu_ref_get(&ctx->file_data->refs);
4315 if (req->needs_fixed_file)
4317 trace_io_uring_file_get(ctx, fd);
4318 req->file = io_file_get(state, fd);
4319 if (unlikely(!req->file))
4326 static int io_grab_files(struct io_kiocb *req)
4329 struct io_ring_ctx *ctx = req->ctx;
4331 if (!req->ring_file)
4335 spin_lock_irq(&ctx->inflight_lock);
4337 * We use the f_ops->flush() handler to ensure that we can flush
4338 * out work accessing these files if the fd is closed. Check if
4339 * the fd has changed since we started down this path, and disallow
4340 * this operation if it has.
4342 if (fcheck(req->ring_fd) == req->ring_file) {
4343 list_add(&req->inflight_entry, &ctx->inflight_list);
4344 req->flags |= REQ_F_INFLIGHT;
4345 req->work.files = current->files;
4348 spin_unlock_irq(&ctx->inflight_lock);
4354 static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
4356 struct io_timeout_data *data = container_of(timer,
4357 struct io_timeout_data, timer);
4358 struct io_kiocb *req = data->req;
4359 struct io_ring_ctx *ctx = req->ctx;
4360 struct io_kiocb *prev = NULL;
4361 unsigned long flags;
4363 spin_lock_irqsave(&ctx->completion_lock, flags);
4366 * We don't expect the list to be empty, that will only happen if we
4367 * race with the completion of the linked work.
4369 if (!list_empty(&req->link_list)) {
4370 prev = list_entry(req->link_list.prev, struct io_kiocb,
4372 if (refcount_inc_not_zero(&prev->refs)) {
4373 list_del_init(&req->link_list);
4374 prev->flags &= ~REQ_F_LINK_TIMEOUT;
4379 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4382 req_set_fail_links(prev);
4383 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
4387 io_cqring_add_event(req, -ETIME);
4390 return HRTIMER_NORESTART;
4393 static void io_queue_linked_timeout(struct io_kiocb *req)
4395 struct io_ring_ctx *ctx = req->ctx;
4398 * If the list is now empty, then our linked request finished before
4399 * we got a chance to setup the timer
4401 spin_lock_irq(&ctx->completion_lock);
4402 if (!list_empty(&req->link_list)) {
4403 struct io_timeout_data *data = &req->io->timeout;
4405 data->timer.function = io_link_timeout_fn;
4406 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
4409 spin_unlock_irq(&ctx->completion_lock);
4411 /* drop submission reference */
4415 static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
4417 struct io_kiocb *nxt;
4419 if (!(req->flags & REQ_F_LINK))
4422 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
4424 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
4427 req->flags |= REQ_F_LINK_TIMEOUT;
4431 static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4433 struct io_kiocb *linked_timeout;
4434 struct io_kiocb *nxt = NULL;
4438 linked_timeout = io_prep_linked_timeout(req);
4440 ret = io_issue_sqe(req, sqe, &nxt, true);
4443 * We async punt it if the file wasn't marked NOWAIT, or if the file
4444 * doesn't support non-blocking read/write attempts
4446 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
4447 (req->flags & REQ_F_MUST_PUNT))) {
4448 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
4449 ret = io_grab_files(req);
4455 * Queued up for async execution, worker will release
4456 * submit reference when the iocb is actually submitted.
4458 io_queue_async_work(req);
4463 /* drop submission reference */
4466 if (linked_timeout) {
4468 io_queue_linked_timeout(linked_timeout);
4470 io_put_req(linked_timeout);
4473 /* and drop final reference, if we failed */
4475 io_cqring_add_event(req, ret);
4476 req_set_fail_links(req);
4487 static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4491 if (unlikely(req->ctx->drain_next)) {
4492 req->flags |= REQ_F_IO_DRAIN;
4493 req->ctx->drain_next = false;
4495 req->ctx->drain_next = (req->flags & REQ_F_DRAIN_LINK);
4497 ret = io_req_defer(req, sqe);
4499 if (ret != -EIOCBQUEUED) {
4500 io_cqring_add_event(req, ret);
4501 req_set_fail_links(req);
4502 io_double_put_req(req);
4504 } else if (req->flags & REQ_F_FORCE_ASYNC) {
4506 * Never try inline submit of IOSQE_ASYNC is set, go straight
4507 * to async execution.
4509 req->work.flags |= IO_WQ_WORK_CONCURRENT;
4510 io_queue_async_work(req);
4512 __io_queue_sqe(req, sqe);
4516 static inline void io_queue_link_head(struct io_kiocb *req)
4518 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
4519 io_cqring_add_event(req, -ECANCELED);
4520 io_double_put_req(req);
4522 io_queue_sqe(req, NULL);
4525 #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
4526 IOSQE_IO_HARDLINK | IOSQE_ASYNC)
4528 static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4529 struct io_submit_state *state, struct io_kiocb **link)
4531 struct io_ring_ctx *ctx = req->ctx;
4532 unsigned int sqe_flags;
4535 sqe_flags = READ_ONCE(sqe->flags);
4537 /* enforce forwards compatibility on users */
4538 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
4542 if (sqe_flags & IOSQE_ASYNC)
4543 req->flags |= REQ_F_FORCE_ASYNC;
4545 ret = io_req_set_file(state, req, sqe);
4546 if (unlikely(ret)) {
4548 io_cqring_add_event(req, ret);
4549 io_double_put_req(req);
4554 * If we already have a head request, queue this one for async
4555 * submittal once the head completes. If we don't have a head but
4556 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
4557 * submitted sync once the chain is complete. If none of those
4558 * conditions are true (normal request), then just queue it.
4561 struct io_kiocb *head = *link;
4563 if (sqe_flags & IOSQE_IO_DRAIN)
4564 head->flags |= REQ_F_DRAIN_LINK | REQ_F_IO_DRAIN;
4566 if (sqe_flags & IOSQE_IO_HARDLINK)
4567 req->flags |= REQ_F_HARDLINK;
4569 if (io_alloc_async_ctx(req)) {
4574 ret = io_req_defer_prep(req, sqe);
4576 /* fail even hard links since we don't submit */
4577 head->flags |= REQ_F_FAIL_LINK;
4580 trace_io_uring_link(ctx, req, head);
4581 list_add_tail(&req->link_list, &head->link_list);
4583 /* last request of a link, enqueue the link */
4584 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) {
4585 io_queue_link_head(head);
4588 } else if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
4589 req->flags |= REQ_F_LINK;
4590 if (sqe_flags & IOSQE_IO_HARDLINK)
4591 req->flags |= REQ_F_HARDLINK;
4593 INIT_LIST_HEAD(&req->link_list);
4594 ret = io_req_defer_prep(req, sqe);
4596 req->flags |= REQ_F_FAIL_LINK;
4599 io_queue_sqe(req, sqe);
4606 * Batched submission is done, ensure local IO is flushed out.
4608 static void io_submit_state_end(struct io_submit_state *state)
4610 blk_finish_plug(&state->plug);
4612 if (state->free_reqs)
4613 kmem_cache_free_bulk(req_cachep, state->free_reqs,
4614 &state->reqs[state->cur_req]);
4618 * Start submission side cache.
4620 static void io_submit_state_start(struct io_submit_state *state,
4621 unsigned int max_ios)
4623 blk_start_plug(&state->plug);
4624 state->free_reqs = 0;
4626 state->ios_left = max_ios;
4629 static void io_commit_sqring(struct io_ring_ctx *ctx)
4631 struct io_rings *rings = ctx->rings;
4634 * Ensure any loads from the SQEs are done at this point,
4635 * since once we write the new head, the application could
4636 * write new data to them.
4638 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
4642 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
4643 * that is mapped by userspace. This means that care needs to be taken to
4644 * ensure that reads are stable, as we cannot rely on userspace always
4645 * being a good citizen. If members of the sqe are validated and then later
4646 * used, it's important that those reads are done through READ_ONCE() to
4647 * prevent a re-load down the line.
4649 static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
4650 const struct io_uring_sqe **sqe_ptr)
4652 u32 *sq_array = ctx->sq_array;
4656 * The cached sq head (or cq tail) serves two purposes:
4658 * 1) allows us to batch the cost of updating the user visible
4660 * 2) allows the kernel side to track the head on its own, even
4661 * though the application is the one updating it.
4663 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
4664 if (likely(head < ctx->sq_entries)) {
4666 * All io need record the previous position, if LINK vs DARIN,
4667 * it can be used to mark the position of the first IO in the
4670 req->sequence = ctx->cached_sq_head;
4671 *sqe_ptr = &ctx->sq_sqes[head];
4672 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
4673 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
4674 ctx->cached_sq_head++;
4678 /* drop invalid entries */
4679 ctx->cached_sq_head++;
4680 ctx->cached_sq_dropped++;
4681 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
4685 static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
4686 struct file *ring_file, int ring_fd,
4687 struct mm_struct **mm, bool async)
4689 struct io_submit_state state, *statep = NULL;
4690 struct io_kiocb *link = NULL;
4691 int i, submitted = 0;
4692 bool mm_fault = false;
4694 /* if we have a backlog and couldn't flush it all, return BUSY */
4695 if (test_bit(0, &ctx->sq_check_overflow)) {
4696 if (!list_empty(&ctx->cq_overflow_list) &&
4697 !io_cqring_overflow_flush(ctx, false))
4701 /* make sure SQ entry isn't read before tail */
4702 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
4704 if (!percpu_ref_tryget_many(&ctx->refs, nr))
4707 if (nr > IO_PLUG_THRESHOLD) {
4708 io_submit_state_start(&state, nr);
4712 for (i = 0; i < nr; i++) {
4713 const struct io_uring_sqe *sqe;
4714 struct io_kiocb *req;
4716 req = io_get_req(ctx, statep);
4717 if (unlikely(!req)) {
4719 submitted = -EAGAIN;
4722 if (!io_get_sqring(ctx, req, &sqe)) {
4723 __io_req_do_free(req);
4727 /* will complete beyond this point, count as submitted */
4730 if (unlikely(req->opcode >= IORING_OP_LAST)) {
4731 io_cqring_add_event(req, -EINVAL);
4732 io_double_put_req(req);
4736 if (io_op_defs[req->opcode].needs_mm && !*mm) {
4737 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
4739 use_mm(ctx->sqo_mm);
4744 req->ring_file = ring_file;
4745 req->ring_fd = ring_fd;
4746 req->has_user = *mm != NULL;
4747 req->in_async = async;
4748 req->needs_fixed_file = async;
4749 trace_io_uring_submit_sqe(ctx, req->user_data, true, async);
4750 if (!io_submit_sqe(req, sqe, statep, &link))
4754 if (submitted != nr)
4755 percpu_ref_put_many(&ctx->refs, nr - submitted);
4757 io_queue_link_head(link);
4759 io_submit_state_end(&state);
4761 /* Commit SQ ring head once we've consumed and submitted all SQEs */
4762 io_commit_sqring(ctx);
4767 static int io_sq_thread(void *data)
4769 struct io_ring_ctx *ctx = data;
4770 struct mm_struct *cur_mm = NULL;
4771 const struct cred *old_cred;
4772 mm_segment_t old_fs;
4775 unsigned long timeout;
4778 complete(&ctx->completions[1]);
4782 old_cred = override_creds(ctx->creds);
4784 ret = timeout = inflight = 0;
4785 while (!kthread_should_park()) {
4786 unsigned int to_submit;
4789 unsigned nr_events = 0;
4791 if (ctx->flags & IORING_SETUP_IOPOLL) {
4793 * inflight is the count of the maximum possible
4794 * entries we submitted, but it can be smaller
4795 * if we dropped some of them. If we don't have
4796 * poll entries available, then we know that we
4797 * have nothing left to poll for. Reset the
4798 * inflight count to zero in that case.
4800 mutex_lock(&ctx->uring_lock);
4801 if (!list_empty(&ctx->poll_list))
4802 __io_iopoll_check(ctx, &nr_events, 0);
4805 mutex_unlock(&ctx->uring_lock);
4808 * Normal IO, just pretend everything completed.
4809 * We don't have to poll completions for that.
4811 nr_events = inflight;
4814 inflight -= nr_events;
4816 timeout = jiffies + ctx->sq_thread_idle;
4819 to_submit = io_sqring_entries(ctx);
4822 * If submit got -EBUSY, flag us as needing the application
4823 * to enter the kernel to reap and flush events.
4825 if (!to_submit || ret == -EBUSY) {
4827 * We're polling. If we're within the defined idle
4828 * period, then let us spin without work before going
4829 * to sleep. The exception is if we got EBUSY doing
4830 * more IO, we should wait for the application to
4831 * reap events and wake us up.
4834 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
4840 * Drop cur_mm before scheduling, we can't hold it for
4841 * long periods (or over schedule()). Do this before
4842 * adding ourselves to the waitqueue, as the unuse/drop
4851 prepare_to_wait(&ctx->sqo_wait, &wait,
4852 TASK_INTERRUPTIBLE);
4854 /* Tell userspace we may need a wakeup call */
4855 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
4856 /* make sure to read SQ tail after writing flags */
4859 to_submit = io_sqring_entries(ctx);
4860 if (!to_submit || ret == -EBUSY) {
4861 if (kthread_should_park()) {
4862 finish_wait(&ctx->sqo_wait, &wait);
4865 if (signal_pending(current))
4866 flush_signals(current);
4868 finish_wait(&ctx->sqo_wait, &wait);
4870 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
4873 finish_wait(&ctx->sqo_wait, &wait);
4875 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
4878 mutex_lock(&ctx->uring_lock);
4879 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
4880 mutex_unlock(&ctx->uring_lock);
4890 revert_creds(old_cred);
4897 struct io_wait_queue {
4898 struct wait_queue_entry wq;
4899 struct io_ring_ctx *ctx;
4901 unsigned nr_timeouts;
4904 static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
4906 struct io_ring_ctx *ctx = iowq->ctx;
4909 * Wake up if we have enough events, or if a timeout occurred since we
4910 * started waiting. For timeouts, we always want to return to userspace,
4911 * regardless of event count.
4913 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
4914 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
4917 static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
4918 int wake_flags, void *key)
4920 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
4923 /* use noflush == true, as we can't safely rely on locking context */
4924 if (!io_should_wake(iowq, true))
4927 return autoremove_wake_function(curr, mode, wake_flags, key);
4931 * Wait until events become available, if we don't already have some. The
4932 * application must reap them itself, as they reside on the shared cq ring.
4934 static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
4935 const sigset_t __user *sig, size_t sigsz)
4937 struct io_wait_queue iowq = {
4940 .func = io_wake_function,
4941 .entry = LIST_HEAD_INIT(iowq.wq.entry),
4944 .to_wait = min_events,
4946 struct io_rings *rings = ctx->rings;
4949 if (io_cqring_events(ctx, false) >= min_events)
4953 #ifdef CONFIG_COMPAT
4954 if (in_compat_syscall())
4955 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
4959 ret = set_user_sigmask(sig, sigsz);
4965 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
4966 trace_io_uring_cqring_wait(ctx, min_events);
4968 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
4969 TASK_INTERRUPTIBLE);
4970 if (io_should_wake(&iowq, false))
4973 if (signal_pending(current)) {
4978 finish_wait(&ctx->wait, &iowq.wq);
4980 restore_saved_sigmask_unless(ret == -EINTR);
4982 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
4985 static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
4987 #if defined(CONFIG_UNIX)
4988 if (ctx->ring_sock) {
4989 struct sock *sock = ctx->ring_sock->sk;
4990 struct sk_buff *skb;
4992 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
4998 for (i = 0; i < ctx->nr_user_files; i++) {
5001 file = io_file_from_index(ctx, i);
5008 static void io_file_ref_kill(struct percpu_ref *ref)
5010 struct fixed_file_data *data;
5012 data = container_of(ref, struct fixed_file_data, refs);
5013 complete(&data->done);
5016 static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
5018 struct fixed_file_data *data = ctx->file_data;
5019 unsigned nr_tables, i;
5024 /* protect against inflight atomic switch, which drops the ref */
5025 flush_work(&data->ref_work);
5026 percpu_ref_get(&data->refs);
5027 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
5028 wait_for_completion(&data->done);
5029 percpu_ref_put(&data->refs);
5030 percpu_ref_exit(&data->refs);
5032 __io_sqe_files_unregister(ctx);
5033 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
5034 for (i = 0; i < nr_tables; i++)
5035 kfree(data->table[i].files);
5038 ctx->file_data = NULL;
5039 ctx->nr_user_files = 0;
5043 static void io_sq_thread_stop(struct io_ring_ctx *ctx)
5045 if (ctx->sqo_thread) {
5046 wait_for_completion(&ctx->completions[1]);
5048 * The park is a bit of a work-around, without it we get
5049 * warning spews on shutdown with SQPOLL set and affinity
5050 * set to a single CPU.
5052 kthread_park(ctx->sqo_thread);
5053 kthread_stop(ctx->sqo_thread);
5054 ctx->sqo_thread = NULL;
5058 static void io_finish_async(struct io_ring_ctx *ctx)
5060 io_sq_thread_stop(ctx);
5063 io_wq_destroy(ctx->io_wq);
5068 #if defined(CONFIG_UNIX)
5070 * Ensure the UNIX gc is aware of our file set, so we are certain that
5071 * the io_uring can be safely unregistered on process exit, even if we have
5072 * loops in the file referencing.
5074 static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
5076 struct sock *sk = ctx->ring_sock->sk;
5077 struct scm_fp_list *fpl;
5078 struct sk_buff *skb;
5081 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
5082 unsigned long inflight = ctx->user->unix_inflight + nr;
5084 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
5088 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
5092 skb = alloc_skb(0, GFP_KERNEL);
5101 fpl->user = get_uid(ctx->user);
5102 for (i = 0; i < nr; i++) {
5103 struct file *file = io_file_from_index(ctx, i + offset);
5107 fpl->fp[nr_files] = get_file(file);
5108 unix_inflight(fpl->user, fpl->fp[nr_files]);
5113 fpl->max = SCM_MAX_FD;
5114 fpl->count = nr_files;
5115 UNIXCB(skb).fp = fpl;
5116 skb->destructor = unix_destruct_scm;
5117 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
5118 skb_queue_head(&sk->sk_receive_queue, skb);
5120 for (i = 0; i < nr_files; i++)
5131 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
5132 * causes regular reference counting to break down. We rely on the UNIX
5133 * garbage collection to take care of this problem for us.
5135 static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5137 unsigned left, total;
5141 left = ctx->nr_user_files;
5143 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
5145 ret = __io_sqe_files_scm(ctx, this_files, total);
5149 total += this_files;
5155 while (total < ctx->nr_user_files) {
5156 struct file *file = io_file_from_index(ctx, total);
5166 static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5172 static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
5177 for (i = 0; i < nr_tables; i++) {
5178 struct fixed_file_table *table = &ctx->file_data->table[i];
5179 unsigned this_files;
5181 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
5182 table->files = kcalloc(this_files, sizeof(struct file *),
5186 nr_files -= this_files;
5192 for (i = 0; i < nr_tables; i++) {
5193 struct fixed_file_table *table = &ctx->file_data->table[i];
5194 kfree(table->files);
5199 static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
5201 #if defined(CONFIG_UNIX)
5202 struct sock *sock = ctx->ring_sock->sk;
5203 struct sk_buff_head list, *head = &sock->sk_receive_queue;
5204 struct sk_buff *skb;
5207 __skb_queue_head_init(&list);
5210 * Find the skb that holds this file in its SCM_RIGHTS. When found,
5211 * remove this entry and rearrange the file array.
5213 skb = skb_dequeue(head);
5215 struct scm_fp_list *fp;
5217 fp = UNIXCB(skb).fp;
5218 for (i = 0; i < fp->count; i++) {
5221 if (fp->fp[i] != file)
5224 unix_notinflight(fp->user, fp->fp[i]);
5225 left = fp->count - 1 - i;
5227 memmove(&fp->fp[i], &fp->fp[i + 1],
5228 left * sizeof(struct file *));
5235 __skb_queue_tail(&list, skb);
5245 __skb_queue_tail(&list, skb);
5247 skb = skb_dequeue(head);
5250 if (skb_peek(&list)) {
5251 spin_lock_irq(&head->lock);
5252 while ((skb = __skb_dequeue(&list)) != NULL)
5253 __skb_queue_tail(head, skb);
5254 spin_unlock_irq(&head->lock);
5261 struct io_file_put {
5262 struct llist_node llist;
5264 struct completion *done;
5267 static void io_ring_file_ref_switch(struct work_struct *work)
5269 struct io_file_put *pfile, *tmp;
5270 struct fixed_file_data *data;
5271 struct llist_node *node;
5273 data = container_of(work, struct fixed_file_data, ref_work);
5275 while ((node = llist_del_all(&data->put_llist)) != NULL) {
5276 llist_for_each_entry_safe(pfile, tmp, node, llist) {
5277 io_ring_file_put(data->ctx, pfile->file);
5279 complete(pfile->done);
5285 percpu_ref_get(&data->refs);
5286 percpu_ref_switch_to_percpu(&data->refs);
5289 static void io_file_data_ref_zero(struct percpu_ref *ref)
5291 struct fixed_file_data *data;
5293 data = container_of(ref, struct fixed_file_data, refs);
5295 /* we can't safely switch from inside this context, punt to wq */
5296 queue_work(system_wq, &data->ref_work);
5299 static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
5302 __s32 __user *fds = (__s32 __user *) arg;
5312 if (nr_args > IORING_MAX_FIXED_FILES)
5315 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
5316 if (!ctx->file_data)
5318 ctx->file_data->ctx = ctx;
5319 init_completion(&ctx->file_data->done);
5321 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
5322 ctx->file_data->table = kcalloc(nr_tables,
5323 sizeof(struct fixed_file_table),
5325 if (!ctx->file_data->table) {
5326 kfree(ctx->file_data);
5327 ctx->file_data = NULL;
5331 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
5332 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
5333 kfree(ctx->file_data->table);
5334 kfree(ctx->file_data);
5335 ctx->file_data = NULL;
5338 ctx->file_data->put_llist.first = NULL;
5339 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
5341 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
5342 percpu_ref_exit(&ctx->file_data->refs);
5343 kfree(ctx->file_data->table);
5344 kfree(ctx->file_data);
5345 ctx->file_data = NULL;
5349 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
5350 struct fixed_file_table *table;
5354 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
5356 /* allow sparse sets */
5362 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
5363 index = i & IORING_FILE_TABLE_MASK;
5371 * Don't allow io_uring instances to be registered. If UNIX
5372 * isn't enabled, then this causes a reference cycle and this
5373 * instance can never get freed. If UNIX is enabled we'll
5374 * handle it just fine, but there's still no point in allowing
5375 * a ring fd as it doesn't support regular read/write anyway.
5377 if (file->f_op == &io_uring_fops) {
5382 table->files[index] = file;
5386 for (i = 0; i < ctx->nr_user_files; i++) {
5387 file = io_file_from_index(ctx, i);
5391 for (i = 0; i < nr_tables; i++)
5392 kfree(ctx->file_data->table[i].files);
5394 kfree(ctx->file_data->table);
5395 kfree(ctx->file_data);
5396 ctx->file_data = NULL;
5397 ctx->nr_user_files = 0;
5401 ret = io_sqe_files_scm(ctx);
5403 io_sqe_files_unregister(ctx);
5408 static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
5411 #if defined(CONFIG_UNIX)
5412 struct sock *sock = ctx->ring_sock->sk;
5413 struct sk_buff_head *head = &sock->sk_receive_queue;
5414 struct sk_buff *skb;
5417 * See if we can merge this file into an existing skb SCM_RIGHTS
5418 * file set. If there's no room, fall back to allocating a new skb
5419 * and filling it in.
5421 spin_lock_irq(&head->lock);
5422 skb = skb_peek(head);
5424 struct scm_fp_list *fpl = UNIXCB(skb).fp;
5426 if (fpl->count < SCM_MAX_FD) {
5427 __skb_unlink(skb, head);
5428 spin_unlock_irq(&head->lock);
5429 fpl->fp[fpl->count] = get_file(file);
5430 unix_inflight(fpl->user, fpl->fp[fpl->count]);
5432 spin_lock_irq(&head->lock);
5433 __skb_queue_head(head, skb);
5438 spin_unlock_irq(&head->lock);
5445 return __io_sqe_files_scm(ctx, 1, index);
5451 static void io_atomic_switch(struct percpu_ref *ref)
5453 struct fixed_file_data *data;
5455 data = container_of(ref, struct fixed_file_data, refs);
5456 clear_bit(FFD_F_ATOMIC, &data->state);
5459 static bool io_queue_file_removal(struct fixed_file_data *data,
5462 struct io_file_put *pfile, pfile_stack;
5463 DECLARE_COMPLETION_ONSTACK(done);
5466 * If we fail allocating the struct we need for doing async reomval
5467 * of this file, just punt to sync and wait for it.
5469 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
5471 pfile = &pfile_stack;
5472 pfile->done = &done;
5476 llist_add(&pfile->llist, &data->put_llist);
5478 if (pfile == &pfile_stack) {
5479 if (!test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5480 percpu_ref_put(&data->refs);
5481 percpu_ref_switch_to_atomic(&data->refs,
5484 wait_for_completion(&done);
5485 flush_work(&data->ref_work);
5492 static int __io_sqe_files_update(struct io_ring_ctx *ctx,
5493 struct io_uring_files_update *up,
5496 struct fixed_file_data *data = ctx->file_data;
5497 bool ref_switch = false;
5503 if (check_add_overflow(up->offset, nr_args, &done))
5505 if (done > ctx->nr_user_files)
5509 fds = u64_to_user_ptr(up->fds);
5511 struct fixed_file_table *table;
5515 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
5519 i = array_index_nospec(up->offset, ctx->nr_user_files);
5520 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
5521 index = i & IORING_FILE_TABLE_MASK;
5522 if (table->files[index]) {
5523 file = io_file_from_index(ctx, index);
5524 table->files[index] = NULL;
5525 if (io_queue_file_removal(data, file))
5535 * Don't allow io_uring instances to be registered. If
5536 * UNIX isn't enabled, then this causes a reference
5537 * cycle and this instance can never get freed. If UNIX
5538 * is enabled we'll handle it just fine, but there's
5539 * still no point in allowing a ring fd as it doesn't
5540 * support regular read/write anyway.
5542 if (file->f_op == &io_uring_fops) {
5547 table->files[index] = file;
5548 err = io_sqe_file_register(ctx, file, i);
5557 if (ref_switch && !test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5558 percpu_ref_put(&data->refs);
5559 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
5562 return done ? done : err;
5564 static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
5567 struct io_uring_files_update up;
5569 if (!ctx->file_data)
5573 if (copy_from_user(&up, arg, sizeof(up)))
5578 return __io_sqe_files_update(ctx, &up, nr_args);
5581 static void io_put_work(struct io_wq_work *work)
5583 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5588 static void io_get_work(struct io_wq_work *work)
5590 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5592 refcount_inc(&req->refs);
5595 static int io_sq_offload_start(struct io_ring_ctx *ctx,
5596 struct io_uring_params *p)
5598 struct io_wq_data data;
5599 unsigned concurrency;
5602 init_waitqueue_head(&ctx->sqo_wait);
5603 mmgrab(current->mm);
5604 ctx->sqo_mm = current->mm;
5606 if (ctx->flags & IORING_SETUP_SQPOLL) {
5608 if (!capable(CAP_SYS_ADMIN))
5611 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
5612 if (!ctx->sq_thread_idle)
5613 ctx->sq_thread_idle = HZ;
5615 if (p->flags & IORING_SETUP_SQ_AFF) {
5616 int cpu = p->sq_thread_cpu;
5619 if (cpu >= nr_cpu_ids)
5621 if (!cpu_online(cpu))
5624 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
5628 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
5631 if (IS_ERR(ctx->sqo_thread)) {
5632 ret = PTR_ERR(ctx->sqo_thread);
5633 ctx->sqo_thread = NULL;
5636 wake_up_process(ctx->sqo_thread);
5637 } else if (p->flags & IORING_SETUP_SQ_AFF) {
5638 /* Can't have SQ_AFF without SQPOLL */
5643 data.mm = ctx->sqo_mm;
5644 data.user = ctx->user;
5645 data.creds = ctx->creds;
5646 data.get_work = io_get_work;
5647 data.put_work = io_put_work;
5649 /* Do QD, or 4 * CPUS, whatever is smallest */
5650 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
5651 ctx->io_wq = io_wq_create(concurrency, &data);
5652 if (IS_ERR(ctx->io_wq)) {
5653 ret = PTR_ERR(ctx->io_wq);
5660 io_finish_async(ctx);
5661 mmdrop(ctx->sqo_mm);
5666 static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
5668 atomic_long_sub(nr_pages, &user->locked_vm);
5671 static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
5673 unsigned long page_limit, cur_pages, new_pages;
5675 /* Don't allow more pages than we can safely lock */
5676 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
5679 cur_pages = atomic_long_read(&user->locked_vm);
5680 new_pages = cur_pages + nr_pages;
5681 if (new_pages > page_limit)
5683 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
5684 new_pages) != cur_pages);
5689 static void io_mem_free(void *ptr)
5696 page = virt_to_head_page(ptr);
5697 if (put_page_testzero(page))
5698 free_compound_page(page);
5701 static void *io_mem_alloc(size_t size)
5703 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
5706 return (void *) __get_free_pages(gfp_flags, get_order(size));
5709 static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
5712 struct io_rings *rings;
5713 size_t off, sq_array_size;
5715 off = struct_size(rings, cqes, cq_entries);
5716 if (off == SIZE_MAX)
5720 off = ALIGN(off, SMP_CACHE_BYTES);
5725 sq_array_size = array_size(sizeof(u32), sq_entries);
5726 if (sq_array_size == SIZE_MAX)
5729 if (check_add_overflow(off, sq_array_size, &off))
5738 static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
5742 pages = (size_t)1 << get_order(
5743 rings_size(sq_entries, cq_entries, NULL));
5744 pages += (size_t)1 << get_order(
5745 array_size(sizeof(struct io_uring_sqe), sq_entries));
5750 static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
5754 if (!ctx->user_bufs)
5757 for (i = 0; i < ctx->nr_user_bufs; i++) {
5758 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
5760 for (j = 0; j < imu->nr_bvecs; j++)
5761 put_user_page(imu->bvec[j].bv_page);
5763 if (ctx->account_mem)
5764 io_unaccount_mem(ctx->user, imu->nr_bvecs);
5769 kfree(ctx->user_bufs);
5770 ctx->user_bufs = NULL;
5771 ctx->nr_user_bufs = 0;
5775 static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
5776 void __user *arg, unsigned index)
5778 struct iovec __user *src;
5780 #ifdef CONFIG_COMPAT
5782 struct compat_iovec __user *ciovs;
5783 struct compat_iovec ciov;
5785 ciovs = (struct compat_iovec __user *) arg;
5786 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
5789 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
5790 dst->iov_len = ciov.iov_len;
5794 src = (struct iovec __user *) arg;
5795 if (copy_from_user(dst, &src[index], sizeof(*dst)))
5800 static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
5803 struct vm_area_struct **vmas = NULL;
5804 struct page **pages = NULL;
5805 int i, j, got_pages = 0;
5810 if (!nr_args || nr_args > UIO_MAXIOV)
5813 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
5815 if (!ctx->user_bufs)
5818 for (i = 0; i < nr_args; i++) {
5819 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
5820 unsigned long off, start, end, ubuf;
5825 ret = io_copy_iov(ctx, &iov, arg, i);
5830 * Don't impose further limits on the size and buffer
5831 * constraints here, we'll -EINVAL later when IO is
5832 * submitted if they are wrong.
5835 if (!iov.iov_base || !iov.iov_len)
5838 /* arbitrary limit, but we need something */
5839 if (iov.iov_len > SZ_1G)
5842 ubuf = (unsigned long) iov.iov_base;
5843 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
5844 start = ubuf >> PAGE_SHIFT;
5845 nr_pages = end - start;
5847 if (ctx->account_mem) {
5848 ret = io_account_mem(ctx->user, nr_pages);
5854 if (!pages || nr_pages > got_pages) {
5857 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
5859 vmas = kvmalloc_array(nr_pages,
5860 sizeof(struct vm_area_struct *),
5862 if (!pages || !vmas) {
5864 if (ctx->account_mem)
5865 io_unaccount_mem(ctx->user, nr_pages);
5868 got_pages = nr_pages;
5871 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
5875 if (ctx->account_mem)
5876 io_unaccount_mem(ctx->user, nr_pages);
5881 down_read(¤t->mm->mmap_sem);
5882 pret = get_user_pages(ubuf, nr_pages,
5883 FOLL_WRITE | FOLL_LONGTERM,
5885 if (pret == nr_pages) {
5886 /* don't support file backed memory */
5887 for (j = 0; j < nr_pages; j++) {
5888 struct vm_area_struct *vma = vmas[j];
5891 !is_file_hugepages(vma->vm_file)) {
5897 ret = pret < 0 ? pret : -EFAULT;
5899 up_read(¤t->mm->mmap_sem);
5902 * if we did partial map, or found file backed vmas,
5903 * release any pages we did get
5906 put_user_pages(pages, pret);
5907 if (ctx->account_mem)
5908 io_unaccount_mem(ctx->user, nr_pages);
5913 off = ubuf & ~PAGE_MASK;
5915 for (j = 0; j < nr_pages; j++) {
5918 vec_len = min_t(size_t, size, PAGE_SIZE - off);
5919 imu->bvec[j].bv_page = pages[j];
5920 imu->bvec[j].bv_len = vec_len;
5921 imu->bvec[j].bv_offset = off;
5925 /* store original address for later verification */
5927 imu->len = iov.iov_len;
5928 imu->nr_bvecs = nr_pages;
5930 ctx->nr_user_bufs++;
5938 io_sqe_buffer_unregister(ctx);
5942 static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
5944 __s32 __user *fds = arg;
5950 if (copy_from_user(&fd, fds, sizeof(*fds)))
5953 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
5954 if (IS_ERR(ctx->cq_ev_fd)) {
5955 int ret = PTR_ERR(ctx->cq_ev_fd);
5956 ctx->cq_ev_fd = NULL;
5963 static int io_eventfd_unregister(struct io_ring_ctx *ctx)
5965 if (ctx->cq_ev_fd) {
5966 eventfd_ctx_put(ctx->cq_ev_fd);
5967 ctx->cq_ev_fd = NULL;
5974 static void io_ring_ctx_free(struct io_ring_ctx *ctx)
5976 io_finish_async(ctx);
5978 mmdrop(ctx->sqo_mm);
5980 io_iopoll_reap_events(ctx);
5981 io_sqe_buffer_unregister(ctx);
5982 io_sqe_files_unregister(ctx);
5983 io_eventfd_unregister(ctx);
5985 #if defined(CONFIG_UNIX)
5986 if (ctx->ring_sock) {
5987 ctx->ring_sock->file = NULL; /* so that iput() is called */
5988 sock_release(ctx->ring_sock);
5992 io_mem_free(ctx->rings);
5993 io_mem_free(ctx->sq_sqes);
5995 percpu_ref_exit(&ctx->refs);
5996 if (ctx->account_mem)
5997 io_unaccount_mem(ctx->user,
5998 ring_pages(ctx->sq_entries, ctx->cq_entries));
5999 free_uid(ctx->user);
6000 put_cred(ctx->creds);
6001 kfree(ctx->completions);
6002 kfree(ctx->cancel_hash);
6003 kmem_cache_free(req_cachep, ctx->fallback_req);
6007 static __poll_t io_uring_poll(struct file *file, poll_table *wait)
6009 struct io_ring_ctx *ctx = file->private_data;
6012 poll_wait(file, &ctx->cq_wait, wait);
6014 * synchronizes with barrier from wq_has_sleeper call in
6018 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
6019 ctx->rings->sq_ring_entries)
6020 mask |= EPOLLOUT | EPOLLWRNORM;
6021 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
6022 mask |= EPOLLIN | EPOLLRDNORM;
6027 static int io_uring_fasync(int fd, struct file *file, int on)
6029 struct io_ring_ctx *ctx = file->private_data;
6031 return fasync_helper(fd, file, on, &ctx->cq_fasync);
6034 static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
6036 mutex_lock(&ctx->uring_lock);
6037 percpu_ref_kill(&ctx->refs);
6038 mutex_unlock(&ctx->uring_lock);
6040 io_kill_timeouts(ctx);
6041 io_poll_remove_all(ctx);
6044 io_wq_cancel_all(ctx->io_wq);
6046 io_iopoll_reap_events(ctx);
6047 /* if we failed setting up the ctx, we might not have any rings */
6049 io_cqring_overflow_flush(ctx, true);
6050 wait_for_completion(&ctx->completions[0]);
6051 io_ring_ctx_free(ctx);
6054 static int io_uring_release(struct inode *inode, struct file *file)
6056 struct io_ring_ctx *ctx = file->private_data;
6058 file->private_data = NULL;
6059 io_ring_ctx_wait_and_kill(ctx);
6063 static void io_uring_cancel_files(struct io_ring_ctx *ctx,
6064 struct files_struct *files)
6066 struct io_kiocb *req;
6069 while (!list_empty_careful(&ctx->inflight_list)) {
6070 struct io_kiocb *cancel_req = NULL;
6072 spin_lock_irq(&ctx->inflight_lock);
6073 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
6074 if (req->work.files != files)
6076 /* req is being completed, ignore */
6077 if (!refcount_inc_not_zero(&req->refs))
6083 prepare_to_wait(&ctx->inflight_wait, &wait,
6084 TASK_UNINTERRUPTIBLE);
6085 spin_unlock_irq(&ctx->inflight_lock);
6087 /* We need to keep going until we don't find a matching req */
6091 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
6092 io_put_req(cancel_req);
6095 finish_wait(&ctx->inflight_wait, &wait);
6098 static int io_uring_flush(struct file *file, void *data)
6100 struct io_ring_ctx *ctx = file->private_data;
6102 io_uring_cancel_files(ctx, data);
6103 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
6104 io_cqring_overflow_flush(ctx, true);
6105 io_wq_cancel_all(ctx->io_wq);
6110 static void *io_uring_validate_mmap_request(struct file *file,
6111 loff_t pgoff, size_t sz)
6113 struct io_ring_ctx *ctx = file->private_data;
6114 loff_t offset = pgoff << PAGE_SHIFT;
6119 case IORING_OFF_SQ_RING:
6120 case IORING_OFF_CQ_RING:
6123 case IORING_OFF_SQES:
6127 return ERR_PTR(-EINVAL);
6130 page = virt_to_head_page(ptr);
6131 if (sz > page_size(page))
6132 return ERR_PTR(-EINVAL);
6139 static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6141 size_t sz = vma->vm_end - vma->vm_start;
6145 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
6147 return PTR_ERR(ptr);
6149 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
6150 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
6153 #else /* !CONFIG_MMU */
6155 static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6157 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
6160 static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
6162 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
6165 static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
6166 unsigned long addr, unsigned long len,
6167 unsigned long pgoff, unsigned long flags)
6171 ptr = io_uring_validate_mmap_request(file, pgoff, len);
6173 return PTR_ERR(ptr);
6175 return (unsigned long) ptr;
6178 #endif /* !CONFIG_MMU */
6180 SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
6181 u32, min_complete, u32, flags, const sigset_t __user *, sig,
6184 struct io_ring_ctx *ctx;
6189 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
6197 if (f.file->f_op != &io_uring_fops)
6201 ctx = f.file->private_data;
6202 if (!percpu_ref_tryget(&ctx->refs))
6206 * For SQ polling, the thread will do all submissions and completions.
6207 * Just return the requested submit count, and wake the thread if
6211 if (ctx->flags & IORING_SETUP_SQPOLL) {
6212 if (!list_empty_careful(&ctx->cq_overflow_list))
6213 io_cqring_overflow_flush(ctx, false);
6214 if (flags & IORING_ENTER_SQ_WAKEUP)
6215 wake_up(&ctx->sqo_wait);
6216 submitted = to_submit;
6217 } else if (to_submit) {
6218 struct mm_struct *cur_mm;
6220 if (current->mm != ctx->sqo_mm ||
6221 current_cred() != ctx->creds) {
6226 mutex_lock(&ctx->uring_lock);
6227 /* already have mm, so io_submit_sqes() won't try to grab it */
6228 cur_mm = ctx->sqo_mm;
6229 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
6231 mutex_unlock(&ctx->uring_lock);
6233 if (submitted != to_submit)
6236 if (flags & IORING_ENTER_GETEVENTS) {
6237 unsigned nr_events = 0;
6239 min_complete = min(min_complete, ctx->cq_entries);
6241 if (ctx->flags & IORING_SETUP_IOPOLL) {
6242 ret = io_iopoll_check(ctx, &nr_events, min_complete);
6244 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
6249 percpu_ref_put(&ctx->refs);
6252 return submitted ? submitted : ret;
6255 static const struct file_operations io_uring_fops = {
6256 .release = io_uring_release,
6257 .flush = io_uring_flush,
6258 .mmap = io_uring_mmap,
6260 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
6261 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
6263 .poll = io_uring_poll,
6264 .fasync = io_uring_fasync,
6267 static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
6268 struct io_uring_params *p)
6270 struct io_rings *rings;
6271 size_t size, sq_array_offset;
6273 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
6274 if (size == SIZE_MAX)
6277 rings = io_mem_alloc(size);
6282 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
6283 rings->sq_ring_mask = p->sq_entries - 1;
6284 rings->cq_ring_mask = p->cq_entries - 1;
6285 rings->sq_ring_entries = p->sq_entries;
6286 rings->cq_ring_entries = p->cq_entries;
6287 ctx->sq_mask = rings->sq_ring_mask;
6288 ctx->cq_mask = rings->cq_ring_mask;
6289 ctx->sq_entries = rings->sq_ring_entries;
6290 ctx->cq_entries = rings->cq_ring_entries;
6292 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
6293 if (size == SIZE_MAX) {
6294 io_mem_free(ctx->rings);
6299 ctx->sq_sqes = io_mem_alloc(size);
6300 if (!ctx->sq_sqes) {
6301 io_mem_free(ctx->rings);
6310 * Allocate an anonymous fd, this is what constitutes the application
6311 * visible backing of an io_uring instance. The application mmaps this
6312 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
6313 * we have to tie this fd to a socket for file garbage collection purposes.
6315 static int io_uring_get_fd(struct io_ring_ctx *ctx)
6320 #if defined(CONFIG_UNIX)
6321 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
6327 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
6331 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
6332 O_RDWR | O_CLOEXEC);
6335 ret = PTR_ERR(file);
6339 #if defined(CONFIG_UNIX)
6340 ctx->ring_sock->file = file;
6342 fd_install(ret, file);
6345 #if defined(CONFIG_UNIX)
6346 sock_release(ctx->ring_sock);
6347 ctx->ring_sock = NULL;
6352 static int io_uring_create(unsigned entries, struct io_uring_params *p)
6354 struct user_struct *user = NULL;
6355 struct io_ring_ctx *ctx;
6361 if (entries > IORING_MAX_ENTRIES) {
6362 if (!(p->flags & IORING_SETUP_CLAMP))
6364 entries = IORING_MAX_ENTRIES;
6368 * Use twice as many entries for the CQ ring. It's possible for the
6369 * application to drive a higher depth than the size of the SQ ring,
6370 * since the sqes are only used at submission time. This allows for
6371 * some flexibility in overcommitting a bit. If the application has
6372 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
6373 * of CQ ring entries manually.
6375 p->sq_entries = roundup_pow_of_two(entries);
6376 if (p->flags & IORING_SETUP_CQSIZE) {
6378 * If IORING_SETUP_CQSIZE is set, we do the same roundup
6379 * to a power-of-two, if it isn't already. We do NOT impose
6380 * any cq vs sq ring sizing.
6382 if (p->cq_entries < p->sq_entries)
6384 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
6385 if (!(p->flags & IORING_SETUP_CLAMP))
6387 p->cq_entries = IORING_MAX_CQ_ENTRIES;
6389 p->cq_entries = roundup_pow_of_two(p->cq_entries);
6391 p->cq_entries = 2 * p->sq_entries;
6394 user = get_uid(current_user());
6395 account_mem = !capable(CAP_IPC_LOCK);
6398 ret = io_account_mem(user,
6399 ring_pages(p->sq_entries, p->cq_entries));
6406 ctx = io_ring_ctx_alloc(p);
6409 io_unaccount_mem(user, ring_pages(p->sq_entries,
6414 ctx->compat = in_compat_syscall();
6415 ctx->account_mem = account_mem;
6417 ctx->creds = get_current_cred();
6419 ret = io_allocate_scq_urings(ctx, p);
6423 ret = io_sq_offload_start(ctx, p);
6427 memset(&p->sq_off, 0, sizeof(p->sq_off));
6428 p->sq_off.head = offsetof(struct io_rings, sq.head);
6429 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
6430 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
6431 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
6432 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
6433 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
6434 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
6436 memset(&p->cq_off, 0, sizeof(p->cq_off));
6437 p->cq_off.head = offsetof(struct io_rings, cq.head);
6438 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
6439 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
6440 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
6441 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
6442 p->cq_off.cqes = offsetof(struct io_rings, cqes);
6445 * Install ring fd as the very last thing, so we don't risk someone
6446 * having closed it before we finish setup
6448 ret = io_uring_get_fd(ctx);
6452 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
6453 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS;
6454 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
6457 io_ring_ctx_wait_and_kill(ctx);
6462 * Sets up an aio uring context, and returns the fd. Applications asks for a
6463 * ring size, we return the actual sq/cq ring sizes (among other things) in the
6464 * params structure passed in.
6466 static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
6468 struct io_uring_params p;
6472 if (copy_from_user(&p, params, sizeof(p)))
6474 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
6479 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
6480 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
6481 IORING_SETUP_CLAMP))
6484 ret = io_uring_create(entries, &p);
6488 if (copy_to_user(params, &p, sizeof(p)))
6494 SYSCALL_DEFINE2(io_uring_setup, u32, entries,
6495 struct io_uring_params __user *, params)
6497 return io_uring_setup(entries, params);
6500 static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
6501 void __user *arg, unsigned nr_args)
6502 __releases(ctx->uring_lock)
6503 __acquires(ctx->uring_lock)
6508 * We're inside the ring mutex, if the ref is already dying, then
6509 * someone else killed the ctx or is already going through
6510 * io_uring_register().
6512 if (percpu_ref_is_dying(&ctx->refs))
6515 if (opcode != IORING_UNREGISTER_FILES &&
6516 opcode != IORING_REGISTER_FILES_UPDATE) {
6517 percpu_ref_kill(&ctx->refs);
6520 * Drop uring mutex before waiting for references to exit. If
6521 * another thread is currently inside io_uring_enter() it might
6522 * need to grab the uring_lock to make progress. If we hold it
6523 * here across the drain wait, then we can deadlock. It's safe
6524 * to drop the mutex here, since no new references will come in
6525 * after we've killed the percpu ref.
6527 mutex_unlock(&ctx->uring_lock);
6528 wait_for_completion(&ctx->completions[0]);
6529 mutex_lock(&ctx->uring_lock);
6533 case IORING_REGISTER_BUFFERS:
6534 ret = io_sqe_buffer_register(ctx, arg, nr_args);
6536 case IORING_UNREGISTER_BUFFERS:
6540 ret = io_sqe_buffer_unregister(ctx);
6542 case IORING_REGISTER_FILES:
6543 ret = io_sqe_files_register(ctx, arg, nr_args);
6545 case IORING_UNREGISTER_FILES:
6549 ret = io_sqe_files_unregister(ctx);
6551 case IORING_REGISTER_FILES_UPDATE:
6552 ret = io_sqe_files_update(ctx, arg, nr_args);
6554 case IORING_REGISTER_EVENTFD:
6558 ret = io_eventfd_register(ctx, arg);
6560 case IORING_UNREGISTER_EVENTFD:
6564 ret = io_eventfd_unregister(ctx);
6572 if (opcode != IORING_UNREGISTER_FILES &&
6573 opcode != IORING_REGISTER_FILES_UPDATE) {
6574 /* bring the ctx back to life */
6575 reinit_completion(&ctx->completions[0]);
6576 percpu_ref_reinit(&ctx->refs);
6581 SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
6582 void __user *, arg, unsigned int, nr_args)
6584 struct io_ring_ctx *ctx;
6593 if (f.file->f_op != &io_uring_fops)
6596 ctx = f.file->private_data;
6598 mutex_lock(&ctx->uring_lock);
6599 ret = __io_uring_register(ctx, opcode, arg, nr_args);
6600 mutex_unlock(&ctx->uring_lock);
6601 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
6602 ctx->cq_ev_fd != NULL, ret);
6608 static int __init io_uring_init(void)
6610 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
6611 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
6614 __initcall(io_uring_init);