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>
74 #define CREATE_TRACE_POINTS
75 #include <trace/events/io_uring.h>
77 #include <uapi/linux/io_uring.h>
82 #define IORING_MAX_ENTRIES 32768
83 #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
86 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
88 #define IORING_FILE_TABLE_SHIFT 9
89 #define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
90 #define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
91 #define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
94 u32 head ____cacheline_aligned_in_smp;
95 u32 tail ____cacheline_aligned_in_smp;
99 * This data is shared with the application through the mmap at offsets
100 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
102 * The offsets to the member fields are published through struct
103 * io_sqring_offsets when calling io_uring_setup.
107 * Head and tail offsets into the ring; the offsets need to be
108 * masked to get valid indices.
110 * The kernel controls head of the sq ring and the tail of the cq ring,
111 * and the application controls tail of the sq ring and the head of the
114 struct io_uring sq, cq;
116 * Bitmasks to apply to head and tail offsets (constant, equals
119 u32 sq_ring_mask, cq_ring_mask;
120 /* Ring sizes (constant, power of 2) */
121 u32 sq_ring_entries, cq_ring_entries;
123 * Number of invalid entries dropped by the kernel due to
124 * invalid index stored in array
126 * Written by the kernel, shouldn't be modified by the
127 * application (i.e. get number of "new events" by comparing to
130 * After a new SQ head value was read by the application this
131 * counter includes all submissions that were dropped reaching
132 * the new SQ head (and possibly more).
138 * Written by the kernel, shouldn't be modified by the
141 * The application needs a full memory barrier before checking
142 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
146 * Number of completion events lost because the queue was full;
147 * this should be avoided by the application by making sure
148 * there are not more requests pending thatn there is space in
149 * the completion queue.
151 * Written by the kernel, shouldn't be modified by the
152 * application (i.e. get number of "new events" by comparing to
155 * As completion events come in out of order this counter is not
156 * ordered with any other data.
160 * Ring buffer of completion events.
162 * The kernel writes completion events fresh every time they are
163 * produced, so the application is allowed to modify pending
166 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
169 struct io_mapped_ubuf {
172 struct bio_vec *bvec;
173 unsigned int nr_bvecs;
176 struct fixed_file_table {
182 struct percpu_ref refs;
183 } ____cacheline_aligned_in_smp;
189 bool cq_overflow_flushed;
193 * Ring buffer of indices into array of io_uring_sqe, which is
194 * mmapped by the application using the IORING_OFF_SQES offset.
196 * This indirection could e.g. be used to assign fixed
197 * io_uring_sqe entries to operations and only submit them to
198 * the queue when needed.
200 * The kernel modifies neither the indices array nor the entries
204 unsigned cached_sq_head;
207 unsigned sq_thread_idle;
208 unsigned cached_sq_dropped;
209 atomic_t cached_cq_overflow;
210 struct io_uring_sqe *sq_sqes;
212 struct list_head defer_list;
213 struct list_head timeout_list;
214 struct list_head cq_overflow_list;
216 wait_queue_head_t inflight_wait;
217 } ____cacheline_aligned_in_smp;
219 struct io_rings *rings;
223 struct task_struct *sqo_thread; /* if using sq thread polling */
224 struct mm_struct *sqo_mm;
225 wait_queue_head_t sqo_wait;
228 * If used, fixed file set. Writers must ensure that ->refs is dead,
229 * readers must ensure that ->refs is alive as long as the file* is
230 * used. Only updated through io_uring_register(2).
232 struct fixed_file_table *file_table;
233 unsigned nr_user_files;
235 /* if used, fixed mapped user buffers */
236 unsigned nr_user_bufs;
237 struct io_mapped_ubuf *user_bufs;
239 struct user_struct *user;
243 /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */
244 struct completion *completions;
246 /* if all else fails... */
247 struct io_kiocb *fallback_req;
249 #if defined(CONFIG_UNIX)
250 struct socket *ring_sock;
254 unsigned cached_cq_tail;
257 atomic_t cq_timeouts;
258 struct wait_queue_head cq_wait;
259 struct fasync_struct *cq_fasync;
260 struct eventfd_ctx *cq_ev_fd;
261 } ____cacheline_aligned_in_smp;
264 struct mutex uring_lock;
265 wait_queue_head_t wait;
266 } ____cacheline_aligned_in_smp;
269 spinlock_t completion_lock;
270 bool poll_multi_file;
272 * ->poll_list is protected by the ctx->uring_lock for
273 * io_uring instances that don't use IORING_SETUP_SQPOLL.
274 * For SQPOLL, only the single threaded io_sq_thread() will
275 * manipulate the list, hence no extra locking is needed there.
277 struct list_head poll_list;
278 struct rb_root cancel_tree;
280 spinlock_t inflight_lock;
281 struct list_head inflight_list;
282 } ____cacheline_aligned_in_smp;
286 * First field must be the file pointer in all the
287 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
289 struct io_poll_iocb {
291 struct wait_queue_head *head;
295 struct wait_queue_entry *wait;
298 struct io_timeout_data {
299 struct io_kiocb *req;
300 struct hrtimer timer;
301 struct timespec64 ts;
302 enum hrtimer_mode mode;
308 struct io_timeout_data *data;
312 * NOTE! Each of the iocb union members has the file pointer
313 * as the first entry in their struct definition. So you can
314 * access the file pointer through any of the sub-structs,
315 * or directly as just 'ki_filp' in this struct.
321 struct io_poll_iocb poll;
322 struct io_timeout timeout;
325 const struct io_uring_sqe *sqe;
326 struct file *ring_file;
330 bool needs_fixed_file;
332 struct io_ring_ctx *ctx;
334 struct list_head list;
335 struct rb_node rb_node;
337 struct list_head link_list;
340 #define REQ_F_NOWAIT 1 /* must not punt to workers */
341 #define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
342 #define REQ_F_FIXED_FILE 4 /* ctx owns file */
343 #define REQ_F_LINK_NEXT 8 /* already grabbed next link */
344 #define REQ_F_IO_DRAIN 16 /* drain existing IO first */
345 #define REQ_F_IO_DRAINED 32 /* drain done */
346 #define REQ_F_LINK 64 /* linked sqes */
347 #define REQ_F_LINK_TIMEOUT 128 /* has linked timeout */
348 #define REQ_F_FAIL_LINK 256 /* fail rest of links */
349 #define REQ_F_DRAIN_LINK 512 /* link should be fully drained */
350 #define REQ_F_TIMEOUT 1024 /* timeout request */
351 #define REQ_F_ISREG 2048 /* regular file */
352 #define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
353 #define REQ_F_TIMEOUT_NOSEQ 8192 /* no timeout sequence */
354 #define REQ_F_INFLIGHT 16384 /* on inflight list */
355 #define REQ_F_COMP_LOCKED 32768 /* completion under lock */
356 #define REQ_F_FREE_SQE 65536 /* free sqe if not async queued */
361 struct list_head inflight_entry;
363 struct io_wq_work work;
366 #define IO_PLUG_THRESHOLD 2
367 #define IO_IOPOLL_BATCH 8
369 struct io_submit_state {
370 struct blk_plug plug;
373 * io_kiocb alloc cache
375 void *reqs[IO_IOPOLL_BATCH];
376 unsigned int free_reqs;
377 unsigned int cur_req;
380 * File reference cache
384 unsigned int has_refs;
385 unsigned int used_refs;
386 unsigned int ios_left;
389 static void io_wq_submit_work(struct io_wq_work **workptr);
390 static void io_cqring_fill_event(struct io_kiocb *req, long res);
391 static void __io_free_req(struct io_kiocb *req);
392 static void io_put_req(struct io_kiocb *req);
393 static void io_double_put_req(struct io_kiocb *req);
394 static void __io_double_put_req(struct io_kiocb *req);
395 static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
396 static void io_queue_linked_timeout(struct io_kiocb *req);
398 static struct kmem_cache *req_cachep;
400 static const struct file_operations io_uring_fops;
402 struct sock *io_uring_get_socket(struct file *file)
404 #if defined(CONFIG_UNIX)
405 if (file->f_op == &io_uring_fops) {
406 struct io_ring_ctx *ctx = file->private_data;
408 return ctx->ring_sock->sk;
413 EXPORT_SYMBOL(io_uring_get_socket);
415 static void io_ring_ctx_ref_free(struct percpu_ref *ref)
417 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
419 complete(&ctx->completions[0]);
422 static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
424 struct io_ring_ctx *ctx;
426 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
430 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
431 if (!ctx->fallback_req)
434 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
435 if (!ctx->completions)
438 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
439 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
442 ctx->flags = p->flags;
443 init_waitqueue_head(&ctx->cq_wait);
444 INIT_LIST_HEAD(&ctx->cq_overflow_list);
445 init_completion(&ctx->completions[0]);
446 init_completion(&ctx->completions[1]);
447 mutex_init(&ctx->uring_lock);
448 init_waitqueue_head(&ctx->wait);
449 spin_lock_init(&ctx->completion_lock);
450 INIT_LIST_HEAD(&ctx->poll_list);
451 ctx->cancel_tree = RB_ROOT;
452 INIT_LIST_HEAD(&ctx->defer_list);
453 INIT_LIST_HEAD(&ctx->timeout_list);
454 init_waitqueue_head(&ctx->inflight_wait);
455 spin_lock_init(&ctx->inflight_lock);
456 INIT_LIST_HEAD(&ctx->inflight_list);
459 if (ctx->fallback_req)
460 kmem_cache_free(req_cachep, ctx->fallback_req);
461 kfree(ctx->completions);
466 static inline bool __req_need_defer(struct io_kiocb *req)
468 struct io_ring_ctx *ctx = req->ctx;
470 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
471 + atomic_read(&ctx->cached_cq_overflow);
474 static inline bool req_need_defer(struct io_kiocb *req)
476 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) == REQ_F_IO_DRAIN)
477 return __req_need_defer(req);
482 static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
484 struct io_kiocb *req;
486 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
487 if (req && !req_need_defer(req)) {
488 list_del_init(&req->list);
495 static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
497 struct io_kiocb *req;
499 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
501 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
503 if (!__req_need_defer(req)) {
504 list_del_init(&req->list);
512 static void __io_commit_cqring(struct io_ring_ctx *ctx)
514 struct io_rings *rings = ctx->rings;
516 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
517 /* order cqe stores with ring update */
518 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
520 if (wq_has_sleeper(&ctx->cq_wait)) {
521 wake_up_interruptible(&ctx->cq_wait);
522 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
527 static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
529 u8 opcode = READ_ONCE(sqe->opcode);
531 return !(opcode == IORING_OP_READ_FIXED ||
532 opcode == IORING_OP_WRITE_FIXED);
535 static inline bool io_prep_async_work(struct io_kiocb *req,
536 struct io_kiocb **link)
538 bool do_hashed = false;
541 switch (req->sqe->opcode) {
542 case IORING_OP_WRITEV:
543 case IORING_OP_WRITE_FIXED:
546 case IORING_OP_READV:
547 case IORING_OP_READ_FIXED:
548 case IORING_OP_SENDMSG:
549 case IORING_OP_RECVMSG:
550 case IORING_OP_ACCEPT:
551 case IORING_OP_POLL_ADD:
552 case IORING_OP_CONNECT:
554 * We know REQ_F_ISREG is not set on some of these
555 * opcodes, but this enables us to keep the check in
558 if (!(req->flags & REQ_F_ISREG))
559 req->work.flags |= IO_WQ_WORK_UNBOUND;
562 if (io_sqe_needs_user(req->sqe))
563 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
566 *link = io_prep_linked_timeout(req);
570 static inline void io_queue_async_work(struct io_kiocb *req)
572 struct io_ring_ctx *ctx = req->ctx;
573 struct io_kiocb *link;
576 do_hashed = io_prep_async_work(req, &link);
578 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
581 io_wq_enqueue(ctx->io_wq, &req->work);
583 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
584 file_inode(req->file));
588 io_queue_linked_timeout(link);
591 static void io_kill_timeout(struct io_kiocb *req)
595 ret = hrtimer_try_to_cancel(&req->timeout.data->timer);
597 atomic_inc(&req->ctx->cq_timeouts);
598 list_del_init(&req->list);
599 io_cqring_fill_event(req, 0);
604 static void io_kill_timeouts(struct io_ring_ctx *ctx)
606 struct io_kiocb *req, *tmp;
608 spin_lock_irq(&ctx->completion_lock);
609 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
610 io_kill_timeout(req);
611 spin_unlock_irq(&ctx->completion_lock);
614 static void io_commit_cqring(struct io_ring_ctx *ctx)
616 struct io_kiocb *req;
618 while ((req = io_get_timeout_req(ctx)) != NULL)
619 io_kill_timeout(req);
621 __io_commit_cqring(ctx);
623 while ((req = io_get_deferred_req(ctx)) != NULL) {
624 req->flags |= REQ_F_IO_DRAINED;
625 io_queue_async_work(req);
629 static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
631 struct io_rings *rings = ctx->rings;
634 tail = ctx->cached_cq_tail;
636 * writes to the cq entry need to come after reading head; the
637 * control dependency is enough as we're using WRITE_ONCE to
640 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
643 ctx->cached_cq_tail++;
644 return &rings->cqes[tail & ctx->cq_mask];
647 static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
649 if (waitqueue_active(&ctx->wait))
651 if (waitqueue_active(&ctx->sqo_wait))
652 wake_up(&ctx->sqo_wait);
654 eventfd_signal(ctx->cq_ev_fd, 1);
657 /* Returns true if there are no backlogged entries after the flush */
658 static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
660 struct io_rings *rings = ctx->rings;
661 struct io_uring_cqe *cqe;
662 struct io_kiocb *req;
667 if (list_empty_careful(&ctx->cq_overflow_list))
669 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
670 rings->cq_ring_entries))
674 spin_lock_irqsave(&ctx->completion_lock, flags);
676 /* if force is set, the ring is going away. always drop after that */
678 ctx->cq_overflow_flushed = true;
681 while (!list_empty(&ctx->cq_overflow_list)) {
682 cqe = io_get_cqring(ctx);
686 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
688 list_move(&req->list, &list);
690 WRITE_ONCE(cqe->user_data, req->user_data);
691 WRITE_ONCE(cqe->res, req->result);
692 WRITE_ONCE(cqe->flags, 0);
694 WRITE_ONCE(ctx->rings->cq_overflow,
695 atomic_inc_return(&ctx->cached_cq_overflow));
699 io_commit_cqring(ctx);
700 spin_unlock_irqrestore(&ctx->completion_lock, flags);
701 io_cqring_ev_posted(ctx);
703 while (!list_empty(&list)) {
704 req = list_first_entry(&list, struct io_kiocb, list);
705 list_del(&req->list);
712 static void io_cqring_fill_event(struct io_kiocb *req, long res)
714 struct io_ring_ctx *ctx = req->ctx;
715 struct io_uring_cqe *cqe;
717 trace_io_uring_complete(ctx, req->user_data, res);
720 * If we can't get a cq entry, userspace overflowed the
721 * submission (by quite a lot). Increment the overflow count in
724 cqe = io_get_cqring(ctx);
726 WRITE_ONCE(cqe->user_data, req->user_data);
727 WRITE_ONCE(cqe->res, res);
728 WRITE_ONCE(cqe->flags, 0);
729 } else if (ctx->cq_overflow_flushed) {
730 WRITE_ONCE(ctx->rings->cq_overflow,
731 atomic_inc_return(&ctx->cached_cq_overflow));
733 refcount_inc(&req->refs);
735 list_add_tail(&req->list, &ctx->cq_overflow_list);
739 static void io_cqring_add_event(struct io_kiocb *req, long res)
741 struct io_ring_ctx *ctx = req->ctx;
744 spin_lock_irqsave(&ctx->completion_lock, flags);
745 io_cqring_fill_event(req, res);
746 io_commit_cqring(ctx);
747 spin_unlock_irqrestore(&ctx->completion_lock, flags);
749 io_cqring_ev_posted(ctx);
752 static inline bool io_is_fallback_req(struct io_kiocb *req)
754 return req == (struct io_kiocb *)
755 ((unsigned long) req->ctx->fallback_req & ~1UL);
758 static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
760 struct io_kiocb *req;
762 req = ctx->fallback_req;
763 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
769 static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
770 struct io_submit_state *state)
772 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
773 struct io_kiocb *req;
775 if (!percpu_ref_tryget(&ctx->refs))
779 req = kmem_cache_alloc(req_cachep, gfp);
782 } else if (!state->free_reqs) {
786 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
787 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
790 * Bulk alloc is all-or-nothing. If we fail to get a batch,
791 * retry single alloc to be on the safe side.
793 if (unlikely(ret <= 0)) {
794 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
799 state->free_reqs = ret - 1;
801 req = state->reqs[0];
803 req = state->reqs[state->cur_req];
809 req->ring_file = NULL;
813 /* one is dropped after submission, the other at completion */
814 refcount_set(&req->refs, 2);
816 INIT_IO_WORK(&req->work, io_wq_submit_work);
819 req = io_get_fallback_req(ctx);
822 percpu_ref_put(&ctx->refs);
826 static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
829 kmem_cache_free_bulk(req_cachep, *nr, reqs);
830 percpu_ref_put_many(&ctx->refs, *nr);
835 static void __io_free_req(struct io_kiocb *req)
837 struct io_ring_ctx *ctx = req->ctx;
839 if (req->flags & REQ_F_FREE_SQE)
841 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
843 if (req->flags & REQ_F_INFLIGHT) {
846 spin_lock_irqsave(&ctx->inflight_lock, flags);
847 list_del(&req->inflight_entry);
848 if (waitqueue_active(&ctx->inflight_wait))
849 wake_up(&ctx->inflight_wait);
850 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
852 if (req->flags & REQ_F_TIMEOUT)
853 kfree(req->timeout.data);
854 percpu_ref_put(&ctx->refs);
855 if (likely(!io_is_fallback_req(req)))
856 kmem_cache_free(req_cachep, req);
858 clear_bit_unlock(0, (unsigned long *) ctx->fallback_req);
861 static bool io_link_cancel_timeout(struct io_kiocb *req)
863 struct io_ring_ctx *ctx = req->ctx;
866 ret = hrtimer_try_to_cancel(&req->timeout.data->timer);
868 io_cqring_fill_event(req, -ECANCELED);
869 io_commit_cqring(ctx);
870 req->flags &= ~REQ_F_LINK;
878 static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
880 struct io_ring_ctx *ctx = req->ctx;
881 struct io_kiocb *nxt;
882 bool wake_ev = false;
884 /* Already got next link */
885 if (req->flags & REQ_F_LINK_NEXT)
889 * The list should never be empty when we are called here. But could
890 * potentially happen if the chain is messed up, check to be on the
893 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
895 list_del_init(&nxt->list);
897 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
898 (nxt->flags & REQ_F_TIMEOUT)) {
899 wake_ev |= io_link_cancel_timeout(nxt);
900 nxt = list_first_entry_or_null(&req->link_list,
901 struct io_kiocb, list);
902 req->flags &= ~REQ_F_LINK_TIMEOUT;
905 if (!list_empty(&req->link_list)) {
906 INIT_LIST_HEAD(&nxt->link_list);
907 list_splice(&req->link_list, &nxt->link_list);
908 nxt->flags |= REQ_F_LINK;
915 req->flags |= REQ_F_LINK_NEXT;
917 io_cqring_ev_posted(ctx);
921 * Called if REQ_F_LINK is set, and we fail the head request
923 static void io_fail_links(struct io_kiocb *req)
925 struct io_ring_ctx *ctx = req->ctx;
926 struct io_kiocb *link;
929 spin_lock_irqsave(&ctx->completion_lock, flags);
931 while (!list_empty(&req->link_list)) {
932 link = list_first_entry(&req->link_list, struct io_kiocb, list);
933 list_del_init(&link->list);
935 trace_io_uring_fail_link(req, link);
937 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
938 link->sqe->opcode == IORING_OP_LINK_TIMEOUT) {
939 io_link_cancel_timeout(link);
941 io_cqring_fill_event(link, -ECANCELED);
942 __io_double_put_req(link);
944 req->flags &= ~REQ_F_LINK_TIMEOUT;
947 io_commit_cqring(ctx);
948 spin_unlock_irqrestore(&ctx->completion_lock, flags);
949 io_cqring_ev_posted(ctx);
952 static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
954 if (likely(!(req->flags & REQ_F_LINK)))
958 * If LINK is set, we have dependent requests in this chain. If we
959 * didn't fail this request, queue the first one up, moving any other
960 * dependencies to the next request. In case of failure, fail the rest
963 if (req->flags & REQ_F_FAIL_LINK) {
965 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
966 REQ_F_LINK_TIMEOUT) {
967 struct io_ring_ctx *ctx = req->ctx;
971 * If this is a timeout link, we could be racing with the
972 * timeout timer. Grab the completion lock for this case to
973 * protect against that.
975 spin_lock_irqsave(&ctx->completion_lock, flags);
976 io_req_link_next(req, nxt);
977 spin_unlock_irqrestore(&ctx->completion_lock, flags);
979 io_req_link_next(req, nxt);
983 static void io_free_req(struct io_kiocb *req)
985 struct io_kiocb *nxt = NULL;
987 io_req_find_next(req, &nxt);
991 io_queue_async_work(nxt);
995 * Drop reference to request, return next in chain (if there is one) if this
996 * was the last reference to this request.
998 __attribute__((nonnull))
999 static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
1001 io_req_find_next(req, nxtptr);
1003 if (refcount_dec_and_test(&req->refs))
1007 static void io_put_req(struct io_kiocb *req)
1009 if (refcount_dec_and_test(&req->refs))
1014 * Must only be used if we don't need to care about links, usually from
1015 * within the completion handling itself.
1017 static void __io_double_put_req(struct io_kiocb *req)
1019 /* drop both submit and complete references */
1020 if (refcount_sub_and_test(2, &req->refs))
1024 static void io_double_put_req(struct io_kiocb *req)
1026 /* drop both submit and complete references */
1027 if (refcount_sub_and_test(2, &req->refs))
1031 static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
1033 struct io_rings *rings = ctx->rings;
1036 * noflush == true is from the waitqueue handler, just ensure we wake
1037 * up the task, and the next invocation will flush the entries. We
1038 * cannot safely to it from here.
1040 if (noflush && !list_empty(&ctx->cq_overflow_list))
1043 io_cqring_overflow_flush(ctx, false);
1045 /* See comment at the top of this file */
1047 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
1050 static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1052 struct io_rings *rings = ctx->rings;
1054 /* make sure SQ entry isn't read before tail */
1055 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1059 * Find and free completed poll iocbs
1061 static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1062 struct list_head *done)
1064 void *reqs[IO_IOPOLL_BATCH];
1065 struct io_kiocb *req;
1069 while (!list_empty(done)) {
1070 req = list_first_entry(done, struct io_kiocb, list);
1071 list_del(&req->list);
1073 io_cqring_fill_event(req, req->result);
1076 if (refcount_dec_and_test(&req->refs)) {
1077 /* If we're not using fixed files, we have to pair the
1078 * completion part with the file put. Use regular
1079 * completions for those, only batch free for fixed
1080 * file and non-linked commands.
1083 (REQ_F_FIXED_FILE|REQ_F_LINK|REQ_F_FREE_SQE)) ==
1084 REQ_F_FIXED_FILE) && !io_is_fallback_req(req)) {
1085 reqs[to_free++] = req;
1086 if (to_free == ARRAY_SIZE(reqs))
1087 io_free_req_many(ctx, reqs, &to_free);
1094 io_commit_cqring(ctx);
1095 io_free_req_many(ctx, reqs, &to_free);
1098 static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1101 struct io_kiocb *req, *tmp;
1107 * Only spin for completions if we don't have multiple devices hanging
1108 * off our complete list, and we're under the requested amount.
1110 spin = !ctx->poll_multi_file && *nr_events < min;
1113 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
1114 struct kiocb *kiocb = &req->rw;
1117 * Move completed entries to our local list. If we find a
1118 * request that requires polling, break out and complete
1119 * the done list first, if we have entries there.
1121 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1122 list_move_tail(&req->list, &done);
1125 if (!list_empty(&done))
1128 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1137 if (!list_empty(&done))
1138 io_iopoll_complete(ctx, nr_events, &done);
1144 * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a
1145 * non-spinning poll check - we'll still enter the driver poll loop, but only
1146 * as a non-spinning completion check.
1148 static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1151 while (!list_empty(&ctx->poll_list) && !need_resched()) {
1154 ret = io_do_iopoll(ctx, nr_events, min);
1157 if (!min || *nr_events >= min)
1165 * We can't just wait for polled events to come to us, we have to actively
1166 * find and complete them.
1168 static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1170 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1173 mutex_lock(&ctx->uring_lock);
1174 while (!list_empty(&ctx->poll_list)) {
1175 unsigned int nr_events = 0;
1177 io_iopoll_getevents(ctx, &nr_events, 1);
1180 * Ensure we allow local-to-the-cpu processing to take place,
1181 * in this case we need to ensure that we reap all events.
1185 mutex_unlock(&ctx->uring_lock);
1188 static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1191 int iters = 0, ret = 0;
1197 * Don't enter poll loop if we already have events pending.
1198 * If we do, we can potentially be spinning for commands that
1199 * already triggered a CQE (eg in error).
1201 if (io_cqring_events(ctx, false))
1205 * If a submit got punted to a workqueue, we can have the
1206 * application entering polling for a command before it gets
1207 * issued. That app will hold the uring_lock for the duration
1208 * of the poll right here, so we need to take a breather every
1209 * now and then to ensure that the issue has a chance to add
1210 * the poll to the issued list. Otherwise we can spin here
1211 * forever, while the workqueue is stuck trying to acquire the
1214 if (!(++iters & 7)) {
1215 mutex_unlock(&ctx->uring_lock);
1216 mutex_lock(&ctx->uring_lock);
1219 if (*nr_events < min)
1220 tmin = min - *nr_events;
1222 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1226 } while (min && !*nr_events && !need_resched());
1231 static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1237 * We disallow the app entering submit/complete with polling, but we
1238 * still need to lock the ring to prevent racing with polled issue
1239 * that got punted to a workqueue.
1241 mutex_lock(&ctx->uring_lock);
1242 ret = __io_iopoll_check(ctx, nr_events, min);
1243 mutex_unlock(&ctx->uring_lock);
1247 static void kiocb_end_write(struct io_kiocb *req)
1250 * Tell lockdep we inherited freeze protection from submission
1253 if (req->flags & REQ_F_ISREG) {
1254 struct inode *inode = file_inode(req->file);
1256 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
1258 file_end_write(req->file);
1261 static void io_complete_rw_common(struct kiocb *kiocb, long res)
1263 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1265 if (kiocb->ki_flags & IOCB_WRITE)
1266 kiocb_end_write(req);
1268 if ((req->flags & REQ_F_LINK) && res != req->result)
1269 req->flags |= REQ_F_FAIL_LINK;
1270 io_cqring_add_event(req, res);
1273 static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1275 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1277 io_complete_rw_common(kiocb, res);
1281 static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1283 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1284 struct io_kiocb *nxt = NULL;
1286 io_complete_rw_common(kiocb, res);
1287 io_put_req_find_next(req, &nxt);
1292 static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1294 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1296 if (kiocb->ki_flags & IOCB_WRITE)
1297 kiocb_end_write(req);
1299 if ((req->flags & REQ_F_LINK) && res != req->result)
1300 req->flags |= REQ_F_FAIL_LINK;
1303 req->flags |= REQ_F_IOPOLL_COMPLETED;
1307 * After the iocb has been issued, it's safe to be found on the poll list.
1308 * Adding the kiocb to the list AFTER submission ensures that we don't
1309 * find it from a io_iopoll_getevents() thread before the issuer is done
1310 * accessing the kiocb cookie.
1312 static void io_iopoll_req_issued(struct io_kiocb *req)
1314 struct io_ring_ctx *ctx = req->ctx;
1317 * Track whether we have multiple files in our lists. This will impact
1318 * how we do polling eventually, not spinning if we're on potentially
1319 * different devices.
1321 if (list_empty(&ctx->poll_list)) {
1322 ctx->poll_multi_file = false;
1323 } else if (!ctx->poll_multi_file) {
1324 struct io_kiocb *list_req;
1326 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1328 if (list_req->rw.ki_filp != req->rw.ki_filp)
1329 ctx->poll_multi_file = true;
1333 * For fast devices, IO may have already completed. If it has, add
1334 * it to the front so we find it first.
1336 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1337 list_add(&req->list, &ctx->poll_list);
1339 list_add_tail(&req->list, &ctx->poll_list);
1342 static void io_file_put(struct io_submit_state *state)
1345 int diff = state->has_refs - state->used_refs;
1348 fput_many(state->file, diff);
1354 * Get as many references to a file as we have IOs left in this submission,
1355 * assuming most submissions are for one file, or at least that each file
1356 * has more than one submission.
1358 static struct file *io_file_get(struct io_submit_state *state, int fd)
1364 if (state->fd == fd) {
1371 state->file = fget_many(fd, state->ios_left);
1376 state->has_refs = state->ios_left;
1377 state->used_refs = 1;
1383 * If we tracked the file through the SCM inflight mechanism, we could support
1384 * any file. For now, just ensure that anything potentially problematic is done
1387 static bool io_file_supports_async(struct file *file)
1389 umode_t mode = file_inode(file)->i_mode;
1391 if (S_ISBLK(mode) || S_ISCHR(mode))
1393 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1399 static int io_prep_rw(struct io_kiocb *req, bool force_nonblock)
1401 const struct io_uring_sqe *sqe = req->sqe;
1402 struct io_ring_ctx *ctx = req->ctx;
1403 struct kiocb *kiocb = &req->rw;
1410 if (S_ISREG(file_inode(req->file)->i_mode))
1411 req->flags |= REQ_F_ISREG;
1414 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1415 * we know to async punt it even if it was opened O_NONBLOCK
1417 if (force_nonblock && !io_file_supports_async(req->file)) {
1418 req->flags |= REQ_F_MUST_PUNT;
1422 kiocb->ki_pos = READ_ONCE(sqe->off);
1423 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1424 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1426 ioprio = READ_ONCE(sqe->ioprio);
1428 ret = ioprio_check_cap(ioprio);
1432 kiocb->ki_ioprio = ioprio;
1434 kiocb->ki_ioprio = get_current_ioprio();
1436 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1440 /* don't allow async punt if RWF_NOWAIT was requested */
1441 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1442 (req->file->f_flags & O_NONBLOCK))
1443 req->flags |= REQ_F_NOWAIT;
1446 kiocb->ki_flags |= IOCB_NOWAIT;
1448 if (ctx->flags & IORING_SETUP_IOPOLL) {
1449 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1450 !kiocb->ki_filp->f_op->iopoll)
1453 kiocb->ki_flags |= IOCB_HIPRI;
1454 kiocb->ki_complete = io_complete_rw_iopoll;
1457 if (kiocb->ki_flags & IOCB_HIPRI)
1459 kiocb->ki_complete = io_complete_rw;
1464 static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1470 case -ERESTARTNOINTR:
1471 case -ERESTARTNOHAND:
1472 case -ERESTART_RESTARTBLOCK:
1474 * We can't just restart the syscall, since previously
1475 * submitted sqes may already be in progress. Just fail this
1481 kiocb->ki_complete(kiocb, ret, 0);
1485 static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1488 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
1489 *nxt = __io_complete_rw(kiocb, ret);
1491 io_rw_done(kiocb, ret);
1494 static ssize_t io_import_fixed(struct io_ring_ctx *ctx, int rw,
1495 const struct io_uring_sqe *sqe,
1496 struct iov_iter *iter)
1498 size_t len = READ_ONCE(sqe->len);
1499 struct io_mapped_ubuf *imu;
1500 unsigned index, buf_index;
1504 /* attempt to use fixed buffers without having provided iovecs */
1505 if (unlikely(!ctx->user_bufs))
1508 buf_index = READ_ONCE(sqe->buf_index);
1509 if (unlikely(buf_index >= ctx->nr_user_bufs))
1512 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1513 imu = &ctx->user_bufs[index];
1514 buf_addr = READ_ONCE(sqe->addr);
1517 if (buf_addr + len < buf_addr)
1519 /* not inside the mapped region */
1520 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1524 * May not be a start of buffer, set size appropriately
1525 * and advance us to the beginning.
1527 offset = buf_addr - imu->ubuf;
1528 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
1532 * Don't use iov_iter_advance() here, as it's really slow for
1533 * using the latter parts of a big fixed buffer - it iterates
1534 * over each segment manually. We can cheat a bit here, because
1537 * 1) it's a BVEC iter, we set it up
1538 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1539 * first and last bvec
1541 * So just find our index, and adjust the iterator afterwards.
1542 * If the offset is within the first bvec (or the whole first
1543 * bvec, just use iov_iter_advance(). This makes it easier
1544 * since we can just skip the first segment, which may not
1545 * be PAGE_SIZE aligned.
1547 const struct bio_vec *bvec = imu->bvec;
1549 if (offset <= bvec->bv_len) {
1550 iov_iter_advance(iter, offset);
1552 unsigned long seg_skip;
1554 /* skip first vec */
1555 offset -= bvec->bv_len;
1556 seg_skip = 1 + (offset >> PAGE_SHIFT);
1558 iter->bvec = bvec + seg_skip;
1559 iter->nr_segs -= seg_skip;
1560 iter->count -= bvec->bv_len + offset;
1561 iter->iov_offset = offset & ~PAGE_MASK;
1568 static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
1569 struct iovec **iovec, struct iov_iter *iter)
1571 const struct io_uring_sqe *sqe = req->sqe;
1572 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1573 size_t sqe_len = READ_ONCE(sqe->len);
1577 * We're reading ->opcode for the second time, but the first read
1578 * doesn't care whether it's _FIXED or not, so it doesn't matter
1579 * whether ->opcode changes concurrently. The first read does care
1580 * about whether it is a READ or a WRITE, so we don't trust this read
1581 * for that purpose and instead let the caller pass in the read/write
1584 opcode = READ_ONCE(sqe->opcode);
1585 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
1587 return io_import_fixed(req->ctx, rw, sqe, iter);
1593 #ifdef CONFIG_COMPAT
1594 if (req->ctx->compat)
1595 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1599 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1603 * For files that don't have ->read_iter() and ->write_iter(), handle them
1604 * by looping over ->read() or ->write() manually.
1606 static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1607 struct iov_iter *iter)
1612 * Don't support polled IO through this interface, and we can't
1613 * support non-blocking either. For the latter, this just causes
1614 * the kiocb to be handled from an async context.
1616 if (kiocb->ki_flags & IOCB_HIPRI)
1618 if (kiocb->ki_flags & IOCB_NOWAIT)
1621 while (iov_iter_count(iter)) {
1625 if (!iov_iter_is_bvec(iter)) {
1626 iovec = iov_iter_iovec(iter);
1628 /* fixed buffers import bvec */
1629 iovec.iov_base = kmap(iter->bvec->bv_page)
1631 iovec.iov_len = min(iter->count,
1632 iter->bvec->bv_len - iter->iov_offset);
1636 nr = file->f_op->read(file, iovec.iov_base,
1637 iovec.iov_len, &kiocb->ki_pos);
1639 nr = file->f_op->write(file, iovec.iov_base,
1640 iovec.iov_len, &kiocb->ki_pos);
1643 if (iov_iter_is_bvec(iter))
1644 kunmap(iter->bvec->bv_page);
1652 if (nr != iovec.iov_len)
1654 iov_iter_advance(iter, nr);
1660 static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
1661 bool force_nonblock)
1663 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1664 struct kiocb *kiocb = &req->rw;
1665 struct iov_iter iter;
1668 ssize_t read_size, ret;
1670 ret = io_prep_rw(req, force_nonblock);
1673 file = kiocb->ki_filp;
1675 if (unlikely(!(file->f_mode & FMODE_READ)))
1678 ret = io_import_iovec(READ, req, &iovec, &iter);
1683 if (req->flags & REQ_F_LINK)
1684 req->result = read_size;
1686 iov_count = iov_iter_count(&iter);
1687 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
1691 if (file->f_op->read_iter)
1692 ret2 = call_read_iter(file, kiocb, &iter);
1694 ret2 = loop_rw_iter(READ, file, kiocb, &iter);
1697 * In case of a short read, punt to async. This can happen
1698 * if we have data partially cached. Alternatively we can
1699 * return the short read, in which case the application will
1700 * need to issue another SQE and wait for it. That SQE will
1701 * need async punt anyway, so it's more efficient to do it
1704 if (force_nonblock && !(req->flags & REQ_F_NOWAIT) &&
1705 (req->flags & REQ_F_ISREG) &&
1706 ret2 > 0 && ret2 < read_size)
1708 /* Catch -EAGAIN return for forced non-blocking submission */
1709 if (!force_nonblock || ret2 != -EAGAIN)
1710 kiocb_done(kiocb, ret2, nxt, req->in_async);
1718 static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
1719 bool force_nonblock)
1721 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1722 struct kiocb *kiocb = &req->rw;
1723 struct iov_iter iter;
1728 ret = io_prep_rw(req, force_nonblock);
1732 file = kiocb->ki_filp;
1733 if (unlikely(!(file->f_mode & FMODE_WRITE)))
1736 ret = io_import_iovec(WRITE, req, &iovec, &iter);
1740 if (req->flags & REQ_F_LINK)
1743 iov_count = iov_iter_count(&iter);
1746 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT))
1749 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
1754 * Open-code file_start_write here to grab freeze protection,
1755 * which will be released by another thread in
1756 * io_complete_rw(). Fool lockdep by telling it the lock got
1757 * released so that it doesn't complain about the held lock when
1758 * we return to userspace.
1760 if (req->flags & REQ_F_ISREG) {
1761 __sb_start_write(file_inode(file)->i_sb,
1762 SB_FREEZE_WRITE, true);
1763 __sb_writers_release(file_inode(file)->i_sb,
1766 kiocb->ki_flags |= IOCB_WRITE;
1768 if (file->f_op->write_iter)
1769 ret2 = call_write_iter(file, kiocb, &iter);
1771 ret2 = loop_rw_iter(WRITE, file, kiocb, &iter);
1772 if (!force_nonblock || ret2 != -EAGAIN)
1773 kiocb_done(kiocb, ret2, nxt, req->in_async);
1783 * IORING_OP_NOP just posts a completion event, nothing else.
1785 static int io_nop(struct io_kiocb *req)
1787 struct io_ring_ctx *ctx = req->ctx;
1789 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1792 io_cqring_add_event(req, 0);
1797 static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1799 struct io_ring_ctx *ctx = req->ctx;
1804 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1806 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1812 static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1813 struct io_kiocb **nxt, bool force_nonblock)
1815 loff_t sqe_off = READ_ONCE(sqe->off);
1816 loff_t sqe_len = READ_ONCE(sqe->len);
1817 loff_t end = sqe_off + sqe_len;
1818 unsigned fsync_flags;
1821 fsync_flags = READ_ONCE(sqe->fsync_flags);
1822 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1825 ret = io_prep_fsync(req, sqe);
1829 /* fsync always requires a blocking context */
1833 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1834 end > 0 ? end : LLONG_MAX,
1835 fsync_flags & IORING_FSYNC_DATASYNC);
1837 if (ret < 0 && (req->flags & REQ_F_LINK))
1838 req->flags |= REQ_F_FAIL_LINK;
1839 io_cqring_add_event(req, ret);
1840 io_put_req_find_next(req, nxt);
1844 static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1846 struct io_ring_ctx *ctx = req->ctx;
1852 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1854 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1860 static int io_sync_file_range(struct io_kiocb *req,
1861 const struct io_uring_sqe *sqe,
1862 struct io_kiocb **nxt,
1863 bool force_nonblock)
1870 ret = io_prep_sfr(req, sqe);
1874 /* sync_file_range always requires a blocking context */
1878 sqe_off = READ_ONCE(sqe->off);
1879 sqe_len = READ_ONCE(sqe->len);
1880 flags = READ_ONCE(sqe->sync_range_flags);
1882 ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
1884 if (ret < 0 && (req->flags & REQ_F_LINK))
1885 req->flags |= REQ_F_FAIL_LINK;
1886 io_cqring_add_event(req, ret);
1887 io_put_req_find_next(req, nxt);
1891 #if defined(CONFIG_NET)
1892 static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1893 struct io_kiocb **nxt, bool force_nonblock,
1894 long (*fn)(struct socket *, struct user_msghdr __user *,
1897 struct socket *sock;
1900 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1903 sock = sock_from_file(req->file, &ret);
1905 struct user_msghdr __user *msg;
1908 flags = READ_ONCE(sqe->msg_flags);
1909 if (flags & MSG_DONTWAIT)
1910 req->flags |= REQ_F_NOWAIT;
1911 else if (force_nonblock)
1912 flags |= MSG_DONTWAIT;
1914 msg = (struct user_msghdr __user *) (unsigned long)
1915 READ_ONCE(sqe->addr);
1917 ret = fn(sock, msg, flags);
1918 if (force_nonblock && ret == -EAGAIN)
1922 io_cqring_add_event(req, ret);
1923 if (ret < 0 && (req->flags & REQ_F_LINK))
1924 req->flags |= REQ_F_FAIL_LINK;
1925 io_put_req_find_next(req, nxt);
1930 static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1931 struct io_kiocb **nxt, bool force_nonblock)
1933 #if defined(CONFIG_NET)
1934 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1935 __sys_sendmsg_sock);
1941 static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1942 struct io_kiocb **nxt, bool force_nonblock)
1944 #if defined(CONFIG_NET)
1945 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1946 __sys_recvmsg_sock);
1952 static int io_accept(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1953 struct io_kiocb **nxt, bool force_nonblock)
1955 #if defined(CONFIG_NET)
1956 struct sockaddr __user *addr;
1957 int __user *addr_len;
1958 unsigned file_flags;
1961 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
1963 if (sqe->ioprio || sqe->len || sqe->buf_index)
1966 addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr);
1967 addr_len = (int __user *) (unsigned long) READ_ONCE(sqe->addr2);
1968 flags = READ_ONCE(sqe->accept_flags);
1969 file_flags = force_nonblock ? O_NONBLOCK : 0;
1971 ret = __sys_accept4_file(req->file, file_flags, addr, addr_len, flags);
1972 if (ret == -EAGAIN && force_nonblock) {
1973 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
1976 if (ret == -ERESTARTSYS)
1978 if (ret < 0 && (req->flags & REQ_F_LINK))
1979 req->flags |= REQ_F_FAIL_LINK;
1980 io_cqring_add_event(req, ret);
1981 io_put_req_find_next(req, nxt);
1988 static int io_connect(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1989 struct io_kiocb **nxt, bool force_nonblock)
1991 #if defined(CONFIG_NET)
1992 struct sockaddr __user *addr;
1993 unsigned file_flags;
1996 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
1998 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
2001 addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr);
2002 addr_len = READ_ONCE(sqe->addr2);
2003 file_flags = force_nonblock ? O_NONBLOCK : 0;
2005 ret = __sys_connect_file(req->file, addr, addr_len, file_flags);
2006 if (ret == -EAGAIN && force_nonblock)
2008 if (ret == -ERESTARTSYS)
2010 if (ret < 0 && (req->flags & REQ_F_LINK))
2011 req->flags |= REQ_F_FAIL_LINK;
2012 io_cqring_add_event(req, ret);
2013 io_put_req_find_next(req, nxt);
2020 static inline void io_poll_remove_req(struct io_kiocb *req)
2022 if (!RB_EMPTY_NODE(&req->rb_node)) {
2023 rb_erase(&req->rb_node, &req->ctx->cancel_tree);
2024 RB_CLEAR_NODE(&req->rb_node);
2028 static void io_poll_remove_one(struct io_kiocb *req)
2030 struct io_poll_iocb *poll = &req->poll;
2032 spin_lock(&poll->head->lock);
2033 WRITE_ONCE(poll->canceled, true);
2034 if (!list_empty(&poll->wait->entry)) {
2035 list_del_init(&poll->wait->entry);
2036 io_queue_async_work(req);
2038 spin_unlock(&poll->head->lock);
2039 io_poll_remove_req(req);
2042 static void io_poll_remove_all(struct io_ring_ctx *ctx)
2044 struct rb_node *node;
2045 struct io_kiocb *req;
2047 spin_lock_irq(&ctx->completion_lock);
2048 while ((node = rb_first(&ctx->cancel_tree)) != NULL) {
2049 req = rb_entry(node, struct io_kiocb, rb_node);
2050 io_poll_remove_one(req);
2052 spin_unlock_irq(&ctx->completion_lock);
2055 static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
2057 struct rb_node *p, *parent = NULL;
2058 struct io_kiocb *req;
2060 p = ctx->cancel_tree.rb_node;
2063 req = rb_entry(parent, struct io_kiocb, rb_node);
2064 if (sqe_addr < req->user_data) {
2066 } else if (sqe_addr > req->user_data) {
2069 io_poll_remove_one(req);
2078 * Find a running poll command that matches one specified in sqe->addr,
2079 * and remove it if found.
2081 static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2083 struct io_ring_ctx *ctx = req->ctx;
2086 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2088 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
2092 spin_lock_irq(&ctx->completion_lock);
2093 ret = io_poll_cancel(ctx, READ_ONCE(sqe->addr));
2094 spin_unlock_irq(&ctx->completion_lock);
2096 io_cqring_add_event(req, ret);
2097 if (ret < 0 && (req->flags & REQ_F_LINK))
2098 req->flags |= REQ_F_FAIL_LINK;
2103 static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
2105 struct io_ring_ctx *ctx = req->ctx;
2107 req->poll.done = true;
2108 kfree(req->poll.wait);
2110 io_cqring_fill_event(req, error);
2112 io_cqring_fill_event(req, mangle_poll(mask));
2113 io_commit_cqring(ctx);
2116 static void io_poll_complete_work(struct io_wq_work **workptr)
2118 struct io_wq_work *work = *workptr;
2119 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
2120 struct io_poll_iocb *poll = &req->poll;
2121 struct poll_table_struct pt = { ._key = poll->events };
2122 struct io_ring_ctx *ctx = req->ctx;
2123 struct io_kiocb *nxt = NULL;
2127 if (work->flags & IO_WQ_WORK_CANCEL) {
2128 WRITE_ONCE(poll->canceled, true);
2130 } else if (READ_ONCE(poll->canceled)) {
2134 if (ret != -ECANCELED)
2135 mask = vfs_poll(poll->file, &pt) & poll->events;
2138 * Note that ->ki_cancel callers also delete iocb from active_reqs after
2139 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
2140 * synchronize with them. In the cancellation case the list_del_init
2141 * itself is not actually needed, but harmless so we keep it in to
2142 * avoid further branches in the fast path.
2144 spin_lock_irq(&ctx->completion_lock);
2145 if (!mask && ret != -ECANCELED) {
2146 add_wait_queue(poll->head, poll->wait);
2147 spin_unlock_irq(&ctx->completion_lock);
2150 io_poll_remove_req(req);
2151 io_poll_complete(req, mask, ret);
2152 spin_unlock_irq(&ctx->completion_lock);
2154 io_cqring_ev_posted(ctx);
2156 if (ret < 0 && req->flags & REQ_F_LINK)
2157 req->flags |= REQ_F_FAIL_LINK;
2158 io_put_req_find_next(req, &nxt);
2160 *workptr = &nxt->work;
2163 static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
2166 struct io_poll_iocb *poll = wait->private;
2167 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
2168 struct io_ring_ctx *ctx = req->ctx;
2169 __poll_t mask = key_to_poll(key);
2170 unsigned long flags;
2172 /* for instances that support it check for an event match first: */
2173 if (mask && !(mask & poll->events))
2176 list_del_init(&poll->wait->entry);
2179 * Run completion inline if we can. We're using trylock here because
2180 * we are violating the completion_lock -> poll wq lock ordering.
2181 * If we have a link timeout we're going to need the completion_lock
2182 * for finalizing the request, mark us as having grabbed that already.
2184 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
2185 io_poll_remove_req(req);
2186 io_poll_complete(req, mask, 0);
2187 req->flags |= REQ_F_COMP_LOCKED;
2189 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2191 io_cqring_ev_posted(ctx);
2193 io_queue_async_work(req);
2199 struct io_poll_table {
2200 struct poll_table_struct pt;
2201 struct io_kiocb *req;
2205 static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
2206 struct poll_table_struct *p)
2208 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
2210 if (unlikely(pt->req->poll.head)) {
2211 pt->error = -EINVAL;
2216 pt->req->poll.head = head;
2217 add_wait_queue(head, pt->req->poll.wait);
2220 static void io_poll_req_insert(struct io_kiocb *req)
2222 struct io_ring_ctx *ctx = req->ctx;
2223 struct rb_node **p = &ctx->cancel_tree.rb_node;
2224 struct rb_node *parent = NULL;
2225 struct io_kiocb *tmp;
2229 tmp = rb_entry(parent, struct io_kiocb, rb_node);
2230 if (req->user_data < tmp->user_data)
2233 p = &(*p)->rb_right;
2235 rb_link_node(&req->rb_node, parent, p);
2236 rb_insert_color(&req->rb_node, &ctx->cancel_tree);
2239 static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2240 struct io_kiocb **nxt)
2242 struct io_poll_iocb *poll = &req->poll;
2243 struct io_ring_ctx *ctx = req->ctx;
2244 struct io_poll_table ipt;
2245 bool cancel = false;
2249 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2251 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
2256 poll->wait = kmalloc(sizeof(*poll->wait), GFP_KERNEL);
2261 INIT_IO_WORK(&req->work, io_poll_complete_work);
2262 events = READ_ONCE(sqe->poll_events);
2263 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
2264 RB_CLEAR_NODE(&req->rb_node);
2268 poll->canceled = false;
2270 ipt.pt._qproc = io_poll_queue_proc;
2271 ipt.pt._key = poll->events;
2273 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
2275 /* initialized the list so that we can do list_empty checks */
2276 INIT_LIST_HEAD(&poll->wait->entry);
2277 init_waitqueue_func_entry(poll->wait, io_poll_wake);
2278 poll->wait->private = poll;
2280 INIT_LIST_HEAD(&req->list);
2282 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
2284 spin_lock_irq(&ctx->completion_lock);
2285 if (likely(poll->head)) {
2286 spin_lock(&poll->head->lock);
2287 if (unlikely(list_empty(&poll->wait->entry))) {
2293 if (mask || ipt.error)
2294 list_del_init(&poll->wait->entry);
2296 WRITE_ONCE(poll->canceled, true);
2297 else if (!poll->done) /* actually waiting for an event */
2298 io_poll_req_insert(req);
2299 spin_unlock(&poll->head->lock);
2301 if (mask) { /* no async, we'd stolen it */
2303 io_poll_complete(req, mask, 0);
2305 spin_unlock_irq(&ctx->completion_lock);
2308 io_cqring_ev_posted(ctx);
2309 io_put_req_find_next(req, nxt);
2314 static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
2316 struct io_timeout_data *data = container_of(timer,
2317 struct io_timeout_data, timer);
2318 struct io_kiocb *req = data->req;
2319 struct io_ring_ctx *ctx = req->ctx;
2320 unsigned long flags;
2322 atomic_inc(&ctx->cq_timeouts);
2324 spin_lock_irqsave(&ctx->completion_lock, flags);
2326 * We could be racing with timeout deletion. If the list is empty,
2327 * then timeout lookup already found it and will be handling it.
2329 if (!list_empty(&req->list)) {
2330 struct io_kiocb *prev;
2333 * Adjust the reqs sequence before the current one because it
2334 * will consume a slot in the cq_ring and the the cq_tail
2335 * pointer will be increased, otherwise other timeout reqs may
2336 * return in advance without waiting for enough wait_nr.
2339 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
2341 list_del_init(&req->list);
2344 io_cqring_fill_event(req, -ETIME);
2345 io_commit_cqring(ctx);
2346 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2348 io_cqring_ev_posted(ctx);
2349 if (req->flags & REQ_F_LINK)
2350 req->flags |= REQ_F_FAIL_LINK;
2352 return HRTIMER_NORESTART;
2355 static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
2357 struct io_kiocb *req;
2360 list_for_each_entry(req, &ctx->timeout_list, list) {
2361 if (user_data == req->user_data) {
2362 list_del_init(&req->list);
2371 ret = hrtimer_try_to_cancel(&req->timeout.data->timer);
2375 if (req->flags & REQ_F_LINK)
2376 req->flags |= REQ_F_FAIL_LINK;
2377 io_cqring_fill_event(req, -ECANCELED);
2383 * Remove or update an existing timeout command
2385 static int io_timeout_remove(struct io_kiocb *req,
2386 const struct io_uring_sqe *sqe)
2388 struct io_ring_ctx *ctx = req->ctx;
2392 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2394 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
2396 flags = READ_ONCE(sqe->timeout_flags);
2400 spin_lock_irq(&ctx->completion_lock);
2401 ret = io_timeout_cancel(ctx, READ_ONCE(sqe->addr));
2403 io_cqring_fill_event(req, ret);
2404 io_commit_cqring(ctx);
2405 spin_unlock_irq(&ctx->completion_lock);
2406 io_cqring_ev_posted(ctx);
2407 if (ret < 0 && req->flags & REQ_F_LINK)
2408 req->flags |= REQ_F_FAIL_LINK;
2413 static int io_timeout_setup(struct io_kiocb *req)
2415 const struct io_uring_sqe *sqe = req->sqe;
2416 struct io_timeout_data *data;
2419 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2421 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
2423 flags = READ_ONCE(sqe->timeout_flags);
2424 if (flags & ~IORING_TIMEOUT_ABS)
2427 data = kzalloc(sizeof(struct io_timeout_data), GFP_KERNEL);
2431 req->timeout.data = data;
2432 req->flags |= REQ_F_TIMEOUT;
2434 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
2437 if (flags & IORING_TIMEOUT_ABS)
2438 data->mode = HRTIMER_MODE_ABS;
2440 data->mode = HRTIMER_MODE_REL;
2442 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
2446 static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2449 struct io_ring_ctx *ctx = req->ctx;
2450 struct io_timeout_data *data;
2451 struct list_head *entry;
2455 ret = io_timeout_setup(req);
2456 /* common setup allows flags (like links) set, we don't */
2457 if (!ret && sqe->flags)
2463 * sqe->off holds how many events that need to occur for this
2464 * timeout event to be satisfied. If it isn't set, then this is
2465 * a pure timeout request, sequence isn't used.
2467 count = READ_ONCE(sqe->off);
2469 req->flags |= REQ_F_TIMEOUT_NOSEQ;
2470 spin_lock_irq(&ctx->completion_lock);
2471 entry = ctx->timeout_list.prev;
2475 req->sequence = ctx->cached_sq_head + count - 1;
2476 req->timeout.data->seq_offset = count;
2479 * Insertion sort, ensuring the first entry in the list is always
2480 * the one we need first.
2482 spin_lock_irq(&ctx->completion_lock);
2483 list_for_each_prev(entry, &ctx->timeout_list) {
2484 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
2485 unsigned nxt_sq_head;
2486 long long tmp, tmp_nxt;
2487 u32 nxt_offset = nxt->timeout.data->seq_offset;
2489 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
2493 * Since cached_sq_head + count - 1 can overflow, use type long
2496 tmp = (long long)ctx->cached_sq_head + count - 1;
2497 nxt_sq_head = nxt->sequence - nxt_offset + 1;
2498 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
2501 * cached_sq_head may overflow, and it will never overflow twice
2502 * once there is some timeout req still be valid.
2504 if (ctx->cached_sq_head < nxt_sq_head)
2511 * Sequence of reqs after the insert one and itself should
2512 * be adjusted because each timeout req consumes a slot.
2517 req->sequence -= span;
2519 list_add(&req->list, entry);
2520 data = req->timeout.data;
2521 data->timer.function = io_timeout_fn;
2522 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
2523 spin_unlock_irq(&ctx->completion_lock);
2527 static bool io_cancel_cb(struct io_wq_work *work, void *data)
2529 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
2531 return req->user_data == (unsigned long) data;
2534 static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
2536 enum io_wq_cancel cancel_ret;
2539 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
2540 switch (cancel_ret) {
2541 case IO_WQ_CANCEL_OK:
2544 case IO_WQ_CANCEL_RUNNING:
2547 case IO_WQ_CANCEL_NOTFOUND:
2555 static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
2556 struct io_kiocb *req, __u64 sqe_addr,
2557 struct io_kiocb **nxt, int success_ret)
2559 unsigned long flags;
2562 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
2563 if (ret != -ENOENT) {
2564 spin_lock_irqsave(&ctx->completion_lock, flags);
2568 spin_lock_irqsave(&ctx->completion_lock, flags);
2569 ret = io_timeout_cancel(ctx, sqe_addr);
2572 ret = io_poll_cancel(ctx, sqe_addr);
2576 io_cqring_fill_event(req, ret);
2577 io_commit_cqring(ctx);
2578 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2579 io_cqring_ev_posted(ctx);
2581 if (ret < 0 && (req->flags & REQ_F_LINK))
2582 req->flags |= REQ_F_FAIL_LINK;
2583 io_put_req_find_next(req, nxt);
2586 static int io_async_cancel(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2587 struct io_kiocb **nxt)
2589 struct io_ring_ctx *ctx = req->ctx;
2591 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2593 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
2597 io_async_find_and_cancel(ctx, req, READ_ONCE(sqe->addr), nxt, 0);
2601 static int io_req_defer(struct io_kiocb *req)
2603 struct io_uring_sqe *sqe_copy;
2604 struct io_ring_ctx *ctx = req->ctx;
2606 /* Still need defer if there is pending req in defer list. */
2607 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
2610 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
2614 spin_lock_irq(&ctx->completion_lock);
2615 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
2616 spin_unlock_irq(&ctx->completion_lock);
2621 memcpy(sqe_copy, req->sqe, sizeof(*sqe_copy));
2622 req->flags |= REQ_F_FREE_SQE;
2623 req->sqe = sqe_copy;
2625 trace_io_uring_defer(ctx, req, req->user_data);
2626 list_add_tail(&req->list, &ctx->defer_list);
2627 spin_unlock_irq(&ctx->completion_lock);
2628 return -EIOCBQUEUED;
2631 __attribute__((nonnull))
2632 static int io_issue_sqe(struct io_kiocb *req, struct io_kiocb **nxt,
2633 bool force_nonblock)
2636 struct io_ring_ctx *ctx = req->ctx;
2638 opcode = READ_ONCE(req->sqe->opcode);
2643 case IORING_OP_READV:
2644 if (unlikely(req->sqe->buf_index))
2646 ret = io_read(req, nxt, force_nonblock);
2648 case IORING_OP_WRITEV:
2649 if (unlikely(req->sqe->buf_index))
2651 ret = io_write(req, nxt, force_nonblock);
2653 case IORING_OP_READ_FIXED:
2654 ret = io_read(req, nxt, force_nonblock);
2656 case IORING_OP_WRITE_FIXED:
2657 ret = io_write(req, nxt, force_nonblock);
2659 case IORING_OP_FSYNC:
2660 ret = io_fsync(req, req->sqe, nxt, force_nonblock);
2662 case IORING_OP_POLL_ADD:
2663 ret = io_poll_add(req, req->sqe, nxt);
2665 case IORING_OP_POLL_REMOVE:
2666 ret = io_poll_remove(req, req->sqe);
2668 case IORING_OP_SYNC_FILE_RANGE:
2669 ret = io_sync_file_range(req, req->sqe, nxt, force_nonblock);
2671 case IORING_OP_SENDMSG:
2672 ret = io_sendmsg(req, req->sqe, nxt, force_nonblock);
2674 case IORING_OP_RECVMSG:
2675 ret = io_recvmsg(req, req->sqe, nxt, force_nonblock);
2677 case IORING_OP_TIMEOUT:
2678 ret = io_timeout(req, req->sqe);
2680 case IORING_OP_TIMEOUT_REMOVE:
2681 ret = io_timeout_remove(req, req->sqe);
2683 case IORING_OP_ACCEPT:
2684 ret = io_accept(req, req->sqe, nxt, force_nonblock);
2686 case IORING_OP_CONNECT:
2687 ret = io_connect(req, req->sqe, nxt, force_nonblock);
2689 case IORING_OP_ASYNC_CANCEL:
2690 ret = io_async_cancel(req, req->sqe, nxt);
2700 if (ctx->flags & IORING_SETUP_IOPOLL) {
2701 if (req->result == -EAGAIN)
2704 /* workqueue context doesn't hold uring_lock, grab it now */
2706 mutex_lock(&ctx->uring_lock);
2707 io_iopoll_req_issued(req);
2709 mutex_unlock(&ctx->uring_lock);
2715 static void io_link_work_cb(struct io_wq_work **workptr)
2717 struct io_wq_work *work = *workptr;
2718 struct io_kiocb *link = work->data;
2720 io_queue_linked_timeout(link);
2721 work->func = io_wq_submit_work;
2724 static void io_wq_submit_work(struct io_wq_work **workptr)
2726 struct io_wq_work *work = *workptr;
2727 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
2728 struct io_kiocb *nxt = NULL;
2731 /* Ensure we clear previously set non-block flag */
2732 req->rw.ki_flags &= ~IOCB_NOWAIT;
2734 if (work->flags & IO_WQ_WORK_CANCEL)
2738 req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
2739 req->in_async = true;
2741 ret = io_issue_sqe(req, &nxt, false);
2743 * We can get EAGAIN for polled IO even though we're
2744 * forcing a sync submission from here, since we can't
2745 * wait for request slots on the block side.
2753 /* drop submission reference */
2757 if (req->flags & REQ_F_LINK)
2758 req->flags |= REQ_F_FAIL_LINK;
2759 io_cqring_add_event(req, ret);
2763 /* if a dependent link is ready, pass it back */
2765 struct io_kiocb *link;
2767 io_prep_async_work(nxt, &link);
2768 *workptr = &nxt->work;
2770 nxt->work.flags |= IO_WQ_WORK_CB;
2771 nxt->work.func = io_link_work_cb;
2772 nxt->work.data = link;
2777 static bool io_op_needs_file(const struct io_uring_sqe *sqe)
2779 int op = READ_ONCE(sqe->opcode);
2783 case IORING_OP_POLL_REMOVE:
2784 case IORING_OP_TIMEOUT:
2785 case IORING_OP_TIMEOUT_REMOVE:
2786 case IORING_OP_ASYNC_CANCEL:
2787 case IORING_OP_LINK_TIMEOUT:
2794 static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
2797 struct fixed_file_table *table;
2799 table = &ctx->file_table[index >> IORING_FILE_TABLE_SHIFT];
2800 return table->files[index & IORING_FILE_TABLE_MASK];
2803 static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req)
2805 struct io_ring_ctx *ctx = req->ctx;
2809 flags = READ_ONCE(req->sqe->flags);
2810 fd = READ_ONCE(req->sqe->fd);
2812 if (flags & IOSQE_IO_DRAIN)
2813 req->flags |= REQ_F_IO_DRAIN;
2815 if (!io_op_needs_file(req->sqe))
2818 if (flags & IOSQE_FIXED_FILE) {
2819 if (unlikely(!ctx->file_table ||
2820 (unsigned) fd >= ctx->nr_user_files))
2822 fd = array_index_nospec(fd, ctx->nr_user_files);
2823 req->file = io_file_from_index(ctx, fd);
2826 req->flags |= REQ_F_FIXED_FILE;
2828 if (req->needs_fixed_file)
2830 trace_io_uring_file_get(ctx, fd);
2831 req->file = io_file_get(state, fd);
2832 if (unlikely(!req->file))
2839 static int io_grab_files(struct io_kiocb *req)
2842 struct io_ring_ctx *ctx = req->ctx;
2845 spin_lock_irq(&ctx->inflight_lock);
2847 * We use the f_ops->flush() handler to ensure that we can flush
2848 * out work accessing these files if the fd is closed. Check if
2849 * the fd has changed since we started down this path, and disallow
2850 * this operation if it has.
2852 if (fcheck(req->ring_fd) == req->ring_file) {
2853 list_add(&req->inflight_entry, &ctx->inflight_list);
2854 req->flags |= REQ_F_INFLIGHT;
2855 req->work.files = current->files;
2858 spin_unlock_irq(&ctx->inflight_lock);
2864 static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
2866 struct io_timeout_data *data = container_of(timer,
2867 struct io_timeout_data, timer);
2868 struct io_kiocb *req = data->req;
2869 struct io_ring_ctx *ctx = req->ctx;
2870 struct io_kiocb *prev = NULL;
2871 unsigned long flags;
2873 spin_lock_irqsave(&ctx->completion_lock, flags);
2876 * We don't expect the list to be empty, that will only happen if we
2877 * race with the completion of the linked work.
2879 if (!list_empty(&req->list)) {
2880 prev = list_entry(req->list.prev, struct io_kiocb, link_list);
2881 if (refcount_inc_not_zero(&prev->refs)) {
2882 list_del_init(&req->list);
2883 prev->flags &= ~REQ_F_LINK_TIMEOUT;
2888 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2891 if (prev->flags & REQ_F_LINK)
2892 prev->flags |= REQ_F_FAIL_LINK;
2893 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
2897 io_cqring_add_event(req, -ETIME);
2900 return HRTIMER_NORESTART;
2903 static void io_queue_linked_timeout(struct io_kiocb *req)
2905 struct io_ring_ctx *ctx = req->ctx;
2908 * If the list is now empty, then our linked request finished before
2909 * we got a chance to setup the timer
2911 spin_lock_irq(&ctx->completion_lock);
2912 if (!list_empty(&req->list)) {
2913 struct io_timeout_data *data = req->timeout.data;
2915 data->timer.function = io_link_timeout_fn;
2916 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
2919 spin_unlock_irq(&ctx->completion_lock);
2921 /* drop submission reference */
2925 static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
2927 struct io_kiocb *nxt;
2929 if (!(req->flags & REQ_F_LINK))
2932 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
2933 if (!nxt || nxt->sqe->opcode != IORING_OP_LINK_TIMEOUT)
2936 req->flags |= REQ_F_LINK_TIMEOUT;
2940 static void __io_queue_sqe(struct io_kiocb *req)
2942 struct io_kiocb *linked_timeout = io_prep_linked_timeout(req);
2943 struct io_kiocb *nxt = NULL;
2946 ret = io_issue_sqe(req, &nxt, true);
2948 io_queue_async_work(nxt);
2951 * We async punt it if the file wasn't marked NOWAIT, or if the file
2952 * doesn't support non-blocking read/write attempts
2954 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
2955 (req->flags & REQ_F_MUST_PUNT))) {
2956 struct io_uring_sqe *sqe_copy;
2958 sqe_copy = kmemdup(req->sqe, sizeof(*sqe_copy), GFP_KERNEL);
2962 req->sqe = sqe_copy;
2963 req->flags |= REQ_F_FREE_SQE;
2965 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
2966 ret = io_grab_files(req);
2972 * Queued up for async execution, worker will release
2973 * submit reference when the iocb is actually submitted.
2975 io_queue_async_work(req);
2980 /* drop submission reference */
2983 if (linked_timeout) {
2985 io_queue_linked_timeout(linked_timeout);
2987 io_put_req(linked_timeout);
2990 /* and drop final reference, if we failed */
2992 io_cqring_add_event(req, ret);
2993 if (req->flags & REQ_F_LINK)
2994 req->flags |= REQ_F_FAIL_LINK;
2999 static void io_queue_sqe(struct io_kiocb *req)
3003 if (unlikely(req->ctx->drain_next)) {
3004 req->flags |= REQ_F_IO_DRAIN;
3005 req->ctx->drain_next = false;
3007 req->ctx->drain_next = (req->flags & REQ_F_DRAIN_LINK);
3009 ret = io_req_defer(req);
3011 if (ret != -EIOCBQUEUED) {
3012 io_cqring_add_event(req, ret);
3013 if (req->flags & REQ_F_LINK)
3014 req->flags |= REQ_F_FAIL_LINK;
3015 io_double_put_req(req);
3018 __io_queue_sqe(req);
3021 static inline void io_queue_link_head(struct io_kiocb *req)
3023 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
3024 io_cqring_add_event(req, -ECANCELED);
3025 io_double_put_req(req);
3031 #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK)
3033 static void io_submit_sqe(struct io_kiocb *req, struct io_submit_state *state,
3034 struct io_kiocb **link)
3036 struct io_ring_ctx *ctx = req->ctx;
3039 req->user_data = req->sqe->user_data;
3041 /* enforce forwards compatibility on users */
3042 if (unlikely(req->sqe->flags & ~SQE_VALID_FLAGS)) {
3047 ret = io_req_set_file(state, req);
3048 if (unlikely(ret)) {
3050 io_cqring_add_event(req, ret);
3051 io_double_put_req(req);
3056 * If we already have a head request, queue this one for async
3057 * submittal once the head completes. If we don't have a head but
3058 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
3059 * submitted sync once the chain is complete. If none of those
3060 * conditions are true (normal request), then just queue it.
3063 struct io_kiocb *prev = *link;
3064 struct io_uring_sqe *sqe_copy;
3066 if (req->sqe->flags & IOSQE_IO_DRAIN)
3067 (*link)->flags |= REQ_F_DRAIN_LINK | REQ_F_IO_DRAIN;
3069 if (READ_ONCE(req->sqe->opcode) == IORING_OP_LINK_TIMEOUT) {
3070 ret = io_timeout_setup(req);
3071 /* common setup allows offset being set, we don't */
3072 if (!ret && req->sqe->off)
3075 prev->flags |= REQ_F_FAIL_LINK;
3080 sqe_copy = kmemdup(req->sqe, sizeof(*sqe_copy), GFP_KERNEL);
3086 req->sqe = sqe_copy;
3087 req->flags |= REQ_F_FREE_SQE;
3088 trace_io_uring_link(ctx, req, prev);
3089 list_add_tail(&req->list, &prev->link_list);
3090 } else if (req->sqe->flags & IOSQE_IO_LINK) {
3091 req->flags |= REQ_F_LINK;
3093 INIT_LIST_HEAD(&req->link_list);
3101 * Batched submission is done, ensure local IO is flushed out.
3103 static void io_submit_state_end(struct io_submit_state *state)
3105 blk_finish_plug(&state->plug);
3107 if (state->free_reqs)
3108 kmem_cache_free_bulk(req_cachep, state->free_reqs,
3109 &state->reqs[state->cur_req]);
3113 * Start submission side cache.
3115 static void io_submit_state_start(struct io_submit_state *state,
3116 struct io_ring_ctx *ctx, unsigned max_ios)
3118 blk_start_plug(&state->plug);
3119 state->free_reqs = 0;
3121 state->ios_left = max_ios;
3124 static void io_commit_sqring(struct io_ring_ctx *ctx)
3126 struct io_rings *rings = ctx->rings;
3128 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
3130 * Ensure any loads from the SQEs are done at this point,
3131 * since once we write the new head, the application could
3132 * write new data to them.
3134 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
3139 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
3140 * that is mapped by userspace. This means that care needs to be taken to
3141 * ensure that reads are stable, as we cannot rely on userspace always
3142 * being a good citizen. If members of the sqe are validated and then later
3143 * used, it's important that those reads are done through READ_ONCE() to
3144 * prevent a re-load down the line.
3146 static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req)
3148 struct io_rings *rings = ctx->rings;
3149 u32 *sq_array = ctx->sq_array;
3153 * The cached sq head (or cq tail) serves two purposes:
3155 * 1) allows us to batch the cost of updating the user visible
3157 * 2) allows the kernel side to track the head on its own, even
3158 * though the application is the one updating it.
3160 head = ctx->cached_sq_head;
3161 /* make sure SQ entry isn't read before tail */
3162 if (unlikely(head == smp_load_acquire(&rings->sq.tail)))
3165 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
3166 if (likely(head < ctx->sq_entries)) {
3168 * All io need record the previous position, if LINK vs DARIN,
3169 * it can be used to mark the position of the first IO in the
3172 req->sequence = ctx->cached_sq_head;
3173 req->sqe = &ctx->sq_sqes[head];
3174 ctx->cached_sq_head++;
3178 /* drop invalid entries */
3179 ctx->cached_sq_head++;
3180 ctx->cached_sq_dropped++;
3181 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
3185 static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
3186 struct file *ring_file, int ring_fd,
3187 struct mm_struct **mm, bool async)
3189 struct io_submit_state state, *statep = NULL;
3190 struct io_kiocb *link = NULL;
3191 int i, submitted = 0;
3192 bool mm_fault = false;
3194 /* if we have a backlog and couldn't flush it all, return BUSY */
3195 if (!list_empty(&ctx->cq_overflow_list) &&
3196 !io_cqring_overflow_flush(ctx, false))
3199 if (nr > IO_PLUG_THRESHOLD) {
3200 io_submit_state_start(&state, ctx, nr);
3204 for (i = 0; i < nr; i++) {
3205 struct io_kiocb *req;
3206 unsigned int sqe_flags;
3208 req = io_get_req(ctx, statep);
3209 if (unlikely(!req)) {
3211 submitted = -EAGAIN;
3214 if (!io_get_sqring(ctx, req)) {
3219 if (io_sqe_needs_user(req->sqe) && !*mm) {
3220 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
3222 use_mm(ctx->sqo_mm);
3227 sqe_flags = req->sqe->flags;
3229 req->ring_file = ring_file;
3230 req->ring_fd = ring_fd;
3231 req->has_user = *mm != NULL;
3232 req->in_async = async;
3233 req->needs_fixed_file = async;
3234 trace_io_uring_submit_sqe(ctx, req->sqe->user_data,
3236 io_submit_sqe(req, statep, &link);
3240 * If previous wasn't linked and we have a linked command,
3241 * that's the end of the chain. Submit the previous link.
3243 if (!(sqe_flags & IOSQE_IO_LINK) && link) {
3244 io_queue_link_head(link);
3250 io_queue_link_head(link);
3252 io_submit_state_end(&state);
3254 /* Commit SQ ring head once we've consumed and submitted all SQEs */
3255 io_commit_sqring(ctx);
3260 static int io_sq_thread(void *data)
3262 struct io_ring_ctx *ctx = data;
3263 struct mm_struct *cur_mm = NULL;
3264 const struct cred *old_cred;
3265 mm_segment_t old_fs;
3268 unsigned long timeout;
3271 complete(&ctx->completions[1]);
3275 old_cred = override_creds(ctx->creds);
3277 ret = timeout = inflight = 0;
3278 while (!kthread_should_park()) {
3279 unsigned int to_submit;
3282 unsigned nr_events = 0;
3284 if (ctx->flags & IORING_SETUP_IOPOLL) {
3286 * inflight is the count of the maximum possible
3287 * entries we submitted, but it can be smaller
3288 * if we dropped some of them. If we don't have
3289 * poll entries available, then we know that we
3290 * have nothing left to poll for. Reset the
3291 * inflight count to zero in that case.
3293 mutex_lock(&ctx->uring_lock);
3294 if (!list_empty(&ctx->poll_list))
3295 __io_iopoll_check(ctx, &nr_events, 0);
3298 mutex_unlock(&ctx->uring_lock);
3301 * Normal IO, just pretend everything completed.
3302 * We don't have to poll completions for that.
3304 nr_events = inflight;
3307 inflight -= nr_events;
3309 timeout = jiffies + ctx->sq_thread_idle;
3312 to_submit = io_sqring_entries(ctx);
3315 * If submit got -EBUSY, flag us as needing the application
3316 * to enter the kernel to reap and flush events.
3318 if (!to_submit || ret == -EBUSY) {
3320 * We're polling. If we're within the defined idle
3321 * period, then let us spin without work before going
3322 * to sleep. The exception is if we got EBUSY doing
3323 * more IO, we should wait for the application to
3324 * reap events and wake us up.
3327 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
3333 * Drop cur_mm before scheduling, we can't hold it for
3334 * long periods (or over schedule()). Do this before
3335 * adding ourselves to the waitqueue, as the unuse/drop
3344 prepare_to_wait(&ctx->sqo_wait, &wait,
3345 TASK_INTERRUPTIBLE);
3347 /* Tell userspace we may need a wakeup call */
3348 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
3349 /* make sure to read SQ tail after writing flags */
3352 to_submit = io_sqring_entries(ctx);
3353 if (!to_submit || ret == -EBUSY) {
3354 if (kthread_should_park()) {
3355 finish_wait(&ctx->sqo_wait, &wait);
3358 if (signal_pending(current))
3359 flush_signals(current);
3361 finish_wait(&ctx->sqo_wait, &wait);
3363 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
3366 finish_wait(&ctx->sqo_wait, &wait);
3368 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
3371 to_submit = min(to_submit, ctx->sq_entries);
3372 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
3382 revert_creds(old_cred);
3389 struct io_wait_queue {
3390 struct wait_queue_entry wq;
3391 struct io_ring_ctx *ctx;
3393 unsigned nr_timeouts;
3396 static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
3398 struct io_ring_ctx *ctx = iowq->ctx;
3401 * Wake up if we have enough events, or if a timeout occured since we
3402 * started waiting. For timeouts, we always want to return to userspace,
3403 * regardless of event count.
3405 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
3406 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
3409 static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
3410 int wake_flags, void *key)
3412 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
3415 /* use noflush == true, as we can't safely rely on locking context */
3416 if (!io_should_wake(iowq, true))
3419 return autoremove_wake_function(curr, mode, wake_flags, key);
3423 * Wait until events become available, if we don't already have some. The
3424 * application must reap them itself, as they reside on the shared cq ring.
3426 static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
3427 const sigset_t __user *sig, size_t sigsz)
3429 struct io_wait_queue iowq = {
3432 .func = io_wake_function,
3433 .entry = LIST_HEAD_INIT(iowq.wq.entry),
3436 .to_wait = min_events,
3438 struct io_rings *rings = ctx->rings;
3441 if (io_cqring_events(ctx, false) >= min_events)
3445 #ifdef CONFIG_COMPAT
3446 if (in_compat_syscall())
3447 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
3451 ret = set_user_sigmask(sig, sigsz);
3457 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
3458 trace_io_uring_cqring_wait(ctx, min_events);
3460 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
3461 TASK_INTERRUPTIBLE);
3462 if (io_should_wake(&iowq, false))
3465 if (signal_pending(current)) {
3470 finish_wait(&ctx->wait, &iowq.wq);
3472 restore_saved_sigmask_unless(ret == -EINTR);
3474 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
3477 static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
3479 #if defined(CONFIG_UNIX)
3480 if (ctx->ring_sock) {
3481 struct sock *sock = ctx->ring_sock->sk;
3482 struct sk_buff *skb;
3484 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
3490 for (i = 0; i < ctx->nr_user_files; i++) {
3493 file = io_file_from_index(ctx, i);
3500 static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
3502 unsigned nr_tables, i;
3504 if (!ctx->file_table)
3507 __io_sqe_files_unregister(ctx);
3508 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
3509 for (i = 0; i < nr_tables; i++)
3510 kfree(ctx->file_table[i].files);
3511 kfree(ctx->file_table);
3512 ctx->file_table = NULL;
3513 ctx->nr_user_files = 0;
3517 static void io_sq_thread_stop(struct io_ring_ctx *ctx)
3519 if (ctx->sqo_thread) {
3520 wait_for_completion(&ctx->completions[1]);
3522 * The park is a bit of a work-around, without it we get
3523 * warning spews on shutdown with SQPOLL set and affinity
3524 * set to a single CPU.
3526 kthread_park(ctx->sqo_thread);
3527 kthread_stop(ctx->sqo_thread);
3528 ctx->sqo_thread = NULL;
3532 static void io_finish_async(struct io_ring_ctx *ctx)
3534 io_sq_thread_stop(ctx);
3537 io_wq_destroy(ctx->io_wq);
3542 #if defined(CONFIG_UNIX)
3543 static void io_destruct_skb(struct sk_buff *skb)
3545 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
3548 io_wq_flush(ctx->io_wq);
3550 unix_destruct_scm(skb);
3554 * Ensure the UNIX gc is aware of our file set, so we are certain that
3555 * the io_uring can be safely unregistered on process exit, even if we have
3556 * loops in the file referencing.
3558 static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
3560 struct sock *sk = ctx->ring_sock->sk;
3561 struct scm_fp_list *fpl;
3562 struct sk_buff *skb;
3565 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
3566 unsigned long inflight = ctx->user->unix_inflight + nr;
3568 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
3572 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
3576 skb = alloc_skb(0, GFP_KERNEL);
3585 fpl->user = get_uid(ctx->user);
3586 for (i = 0; i < nr; i++) {
3587 struct file *file = io_file_from_index(ctx, i + offset);
3591 fpl->fp[nr_files] = get_file(file);
3592 unix_inflight(fpl->user, fpl->fp[nr_files]);
3597 fpl->max = SCM_MAX_FD;
3598 fpl->count = nr_files;
3599 UNIXCB(skb).fp = fpl;
3600 skb->destructor = io_destruct_skb;
3601 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
3602 skb_queue_head(&sk->sk_receive_queue, skb);
3604 for (i = 0; i < nr_files; i++)
3615 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
3616 * causes regular reference counting to break down. We rely on the UNIX
3617 * garbage collection to take care of this problem for us.
3619 static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3621 unsigned left, total;
3625 left = ctx->nr_user_files;
3627 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
3629 ret = __io_sqe_files_scm(ctx, this_files, total);
3633 total += this_files;
3639 while (total < ctx->nr_user_files) {
3640 struct file *file = io_file_from_index(ctx, total);
3650 static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3656 static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
3661 for (i = 0; i < nr_tables; i++) {
3662 struct fixed_file_table *table = &ctx->file_table[i];
3663 unsigned this_files;
3665 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
3666 table->files = kcalloc(this_files, sizeof(struct file *),
3670 nr_files -= this_files;
3676 for (i = 0; i < nr_tables; i++) {
3677 struct fixed_file_table *table = &ctx->file_table[i];
3678 kfree(table->files);
3683 static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
3686 __s32 __user *fds = (__s32 __user *) arg;
3691 if (ctx->file_table)
3695 if (nr_args > IORING_MAX_FIXED_FILES)
3698 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
3699 ctx->file_table = kcalloc(nr_tables, sizeof(struct fixed_file_table),
3701 if (!ctx->file_table)
3704 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
3705 kfree(ctx->file_table);
3706 ctx->file_table = NULL;
3710 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
3711 struct fixed_file_table *table;
3715 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
3717 /* allow sparse sets */
3723 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3724 index = i & IORING_FILE_TABLE_MASK;
3725 table->files[index] = fget(fd);
3728 if (!table->files[index])
3731 * Don't allow io_uring instances to be registered. If UNIX
3732 * isn't enabled, then this causes a reference cycle and this
3733 * instance can never get freed. If UNIX is enabled we'll
3734 * handle it just fine, but there's still no point in allowing
3735 * a ring fd as it doesn't support regular read/write anyway.
3737 if (table->files[index]->f_op == &io_uring_fops) {
3738 fput(table->files[index]);
3745 for (i = 0; i < ctx->nr_user_files; i++) {
3748 file = io_file_from_index(ctx, i);
3752 for (i = 0; i < nr_tables; i++)
3753 kfree(ctx->file_table[i].files);
3755 kfree(ctx->file_table);
3756 ctx->file_table = NULL;
3757 ctx->nr_user_files = 0;
3761 ret = io_sqe_files_scm(ctx);
3763 io_sqe_files_unregister(ctx);
3768 static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index)
3770 #if defined(CONFIG_UNIX)
3771 struct file *file = io_file_from_index(ctx, index);
3772 struct sock *sock = ctx->ring_sock->sk;
3773 struct sk_buff_head list, *head = &sock->sk_receive_queue;
3774 struct sk_buff *skb;
3777 __skb_queue_head_init(&list);
3780 * Find the skb that holds this file in its SCM_RIGHTS. When found,
3781 * remove this entry and rearrange the file array.
3783 skb = skb_dequeue(head);
3785 struct scm_fp_list *fp;
3787 fp = UNIXCB(skb).fp;
3788 for (i = 0; i < fp->count; i++) {
3791 if (fp->fp[i] != file)
3794 unix_notinflight(fp->user, fp->fp[i]);
3795 left = fp->count - 1 - i;
3797 memmove(&fp->fp[i], &fp->fp[i + 1],
3798 left * sizeof(struct file *));
3805 __skb_queue_tail(&list, skb);
3815 __skb_queue_tail(&list, skb);
3817 skb = skb_dequeue(head);
3820 if (skb_peek(&list)) {
3821 spin_lock_irq(&head->lock);
3822 while ((skb = __skb_dequeue(&list)) != NULL)
3823 __skb_queue_tail(head, skb);
3824 spin_unlock_irq(&head->lock);
3827 fput(io_file_from_index(ctx, index));
3831 static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
3834 #if defined(CONFIG_UNIX)
3835 struct sock *sock = ctx->ring_sock->sk;
3836 struct sk_buff_head *head = &sock->sk_receive_queue;
3837 struct sk_buff *skb;
3840 * See if we can merge this file into an existing skb SCM_RIGHTS
3841 * file set. If there's no room, fall back to allocating a new skb
3842 * and filling it in.
3844 spin_lock_irq(&head->lock);
3845 skb = skb_peek(head);
3847 struct scm_fp_list *fpl = UNIXCB(skb).fp;
3849 if (fpl->count < SCM_MAX_FD) {
3850 __skb_unlink(skb, head);
3851 spin_unlock_irq(&head->lock);
3852 fpl->fp[fpl->count] = get_file(file);
3853 unix_inflight(fpl->user, fpl->fp[fpl->count]);
3855 spin_lock_irq(&head->lock);
3856 __skb_queue_head(head, skb);
3861 spin_unlock_irq(&head->lock);
3868 return __io_sqe_files_scm(ctx, 1, index);
3874 static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
3877 struct io_uring_files_update up;
3882 if (!ctx->file_table)
3886 if (copy_from_user(&up, arg, sizeof(up)))
3888 if (check_add_overflow(up.offset, nr_args, &done))
3890 if (done > ctx->nr_user_files)
3894 fds = (__s32 __user *) up.fds;
3896 struct fixed_file_table *table;
3900 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
3904 i = array_index_nospec(up.offset, ctx->nr_user_files);
3905 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3906 index = i & IORING_FILE_TABLE_MASK;
3907 if (table->files[index]) {
3908 io_sqe_file_unregister(ctx, i);
3909 table->files[index] = NULL;
3920 * Don't allow io_uring instances to be registered. If
3921 * UNIX isn't enabled, then this causes a reference
3922 * cycle and this instance can never get freed. If UNIX
3923 * is enabled we'll handle it just fine, but there's
3924 * still no point in allowing a ring fd as it doesn't
3925 * support regular read/write anyway.
3927 if (file->f_op == &io_uring_fops) {
3932 table->files[index] = file;
3933 err = io_sqe_file_register(ctx, file, i);
3942 return done ? done : err;
3945 static void io_put_work(struct io_wq_work *work)
3947 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3952 static void io_get_work(struct io_wq_work *work)
3954 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3956 refcount_inc(&req->refs);
3959 static int io_sq_offload_start(struct io_ring_ctx *ctx,
3960 struct io_uring_params *p)
3962 struct io_wq_data data;
3963 unsigned concurrency;
3966 init_waitqueue_head(&ctx->sqo_wait);
3967 mmgrab(current->mm);
3968 ctx->sqo_mm = current->mm;
3970 if (ctx->flags & IORING_SETUP_SQPOLL) {
3972 if (!capable(CAP_SYS_ADMIN))
3975 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
3976 if (!ctx->sq_thread_idle)
3977 ctx->sq_thread_idle = HZ;
3979 if (p->flags & IORING_SETUP_SQ_AFF) {
3980 int cpu = p->sq_thread_cpu;
3983 if (cpu >= nr_cpu_ids)
3985 if (!cpu_online(cpu))
3988 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
3992 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
3995 if (IS_ERR(ctx->sqo_thread)) {
3996 ret = PTR_ERR(ctx->sqo_thread);
3997 ctx->sqo_thread = NULL;
4000 wake_up_process(ctx->sqo_thread);
4001 } else if (p->flags & IORING_SETUP_SQ_AFF) {
4002 /* Can't have SQ_AFF without SQPOLL */
4007 data.mm = ctx->sqo_mm;
4008 data.user = ctx->user;
4009 data.creds = ctx->creds;
4010 data.get_work = io_get_work;
4011 data.put_work = io_put_work;
4013 /* Do QD, or 4 * CPUS, whatever is smallest */
4014 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
4015 ctx->io_wq = io_wq_create(concurrency, &data);
4016 if (IS_ERR(ctx->io_wq)) {
4017 ret = PTR_ERR(ctx->io_wq);
4024 io_finish_async(ctx);
4025 mmdrop(ctx->sqo_mm);
4030 static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
4032 atomic_long_sub(nr_pages, &user->locked_vm);
4035 static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
4037 unsigned long page_limit, cur_pages, new_pages;
4039 /* Don't allow more pages than we can safely lock */
4040 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
4043 cur_pages = atomic_long_read(&user->locked_vm);
4044 new_pages = cur_pages + nr_pages;
4045 if (new_pages > page_limit)
4047 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
4048 new_pages) != cur_pages);
4053 static void io_mem_free(void *ptr)
4060 page = virt_to_head_page(ptr);
4061 if (put_page_testzero(page))
4062 free_compound_page(page);
4065 static void *io_mem_alloc(size_t size)
4067 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
4070 return (void *) __get_free_pages(gfp_flags, get_order(size));
4073 static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
4076 struct io_rings *rings;
4077 size_t off, sq_array_size;
4079 off = struct_size(rings, cqes, cq_entries);
4080 if (off == SIZE_MAX)
4084 off = ALIGN(off, SMP_CACHE_BYTES);
4089 sq_array_size = array_size(sizeof(u32), sq_entries);
4090 if (sq_array_size == SIZE_MAX)
4093 if (check_add_overflow(off, sq_array_size, &off))
4102 static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
4106 pages = (size_t)1 << get_order(
4107 rings_size(sq_entries, cq_entries, NULL));
4108 pages += (size_t)1 << get_order(
4109 array_size(sizeof(struct io_uring_sqe), sq_entries));
4114 static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
4118 if (!ctx->user_bufs)
4121 for (i = 0; i < ctx->nr_user_bufs; i++) {
4122 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4124 for (j = 0; j < imu->nr_bvecs; j++)
4125 put_user_page(imu->bvec[j].bv_page);
4127 if (ctx->account_mem)
4128 io_unaccount_mem(ctx->user, imu->nr_bvecs);
4133 kfree(ctx->user_bufs);
4134 ctx->user_bufs = NULL;
4135 ctx->nr_user_bufs = 0;
4139 static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
4140 void __user *arg, unsigned index)
4142 struct iovec __user *src;
4144 #ifdef CONFIG_COMPAT
4146 struct compat_iovec __user *ciovs;
4147 struct compat_iovec ciov;
4149 ciovs = (struct compat_iovec __user *) arg;
4150 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
4153 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
4154 dst->iov_len = ciov.iov_len;
4158 src = (struct iovec __user *) arg;
4159 if (copy_from_user(dst, &src[index], sizeof(*dst)))
4164 static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
4167 struct vm_area_struct **vmas = NULL;
4168 struct page **pages = NULL;
4169 int i, j, got_pages = 0;
4174 if (!nr_args || nr_args > UIO_MAXIOV)
4177 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
4179 if (!ctx->user_bufs)
4182 for (i = 0; i < nr_args; i++) {
4183 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4184 unsigned long off, start, end, ubuf;
4189 ret = io_copy_iov(ctx, &iov, arg, i);
4194 * Don't impose further limits on the size and buffer
4195 * constraints here, we'll -EINVAL later when IO is
4196 * submitted if they are wrong.
4199 if (!iov.iov_base || !iov.iov_len)
4202 /* arbitrary limit, but we need something */
4203 if (iov.iov_len > SZ_1G)
4206 ubuf = (unsigned long) iov.iov_base;
4207 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
4208 start = ubuf >> PAGE_SHIFT;
4209 nr_pages = end - start;
4211 if (ctx->account_mem) {
4212 ret = io_account_mem(ctx->user, nr_pages);
4218 if (!pages || nr_pages > got_pages) {
4221 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
4223 vmas = kvmalloc_array(nr_pages,
4224 sizeof(struct vm_area_struct *),
4226 if (!pages || !vmas) {
4228 if (ctx->account_mem)
4229 io_unaccount_mem(ctx->user, nr_pages);
4232 got_pages = nr_pages;
4235 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
4239 if (ctx->account_mem)
4240 io_unaccount_mem(ctx->user, nr_pages);
4245 down_read(¤t->mm->mmap_sem);
4246 pret = get_user_pages(ubuf, nr_pages,
4247 FOLL_WRITE | FOLL_LONGTERM,
4249 if (pret == nr_pages) {
4250 /* don't support file backed memory */
4251 for (j = 0; j < nr_pages; j++) {
4252 struct vm_area_struct *vma = vmas[j];
4255 !is_file_hugepages(vma->vm_file)) {
4261 ret = pret < 0 ? pret : -EFAULT;
4263 up_read(¤t->mm->mmap_sem);
4266 * if we did partial map, or found file backed vmas,
4267 * release any pages we did get
4270 put_user_pages(pages, pret);
4271 if (ctx->account_mem)
4272 io_unaccount_mem(ctx->user, nr_pages);
4277 off = ubuf & ~PAGE_MASK;
4279 for (j = 0; j < nr_pages; j++) {
4282 vec_len = min_t(size_t, size, PAGE_SIZE - off);
4283 imu->bvec[j].bv_page = pages[j];
4284 imu->bvec[j].bv_len = vec_len;
4285 imu->bvec[j].bv_offset = off;
4289 /* store original address for later verification */
4291 imu->len = iov.iov_len;
4292 imu->nr_bvecs = nr_pages;
4294 ctx->nr_user_bufs++;
4302 io_sqe_buffer_unregister(ctx);
4306 static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
4308 __s32 __user *fds = arg;
4314 if (copy_from_user(&fd, fds, sizeof(*fds)))
4317 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
4318 if (IS_ERR(ctx->cq_ev_fd)) {
4319 int ret = PTR_ERR(ctx->cq_ev_fd);
4320 ctx->cq_ev_fd = NULL;
4327 static int io_eventfd_unregister(struct io_ring_ctx *ctx)
4329 if (ctx->cq_ev_fd) {
4330 eventfd_ctx_put(ctx->cq_ev_fd);
4331 ctx->cq_ev_fd = NULL;
4338 static void io_ring_ctx_free(struct io_ring_ctx *ctx)
4340 io_finish_async(ctx);
4342 mmdrop(ctx->sqo_mm);
4344 io_iopoll_reap_events(ctx);
4345 io_sqe_buffer_unregister(ctx);
4346 io_sqe_files_unregister(ctx);
4347 io_eventfd_unregister(ctx);
4349 #if defined(CONFIG_UNIX)
4350 if (ctx->ring_sock) {
4351 ctx->ring_sock->file = NULL; /* so that iput() is called */
4352 sock_release(ctx->ring_sock);
4356 io_mem_free(ctx->rings);
4357 io_mem_free(ctx->sq_sqes);
4359 percpu_ref_exit(&ctx->refs);
4360 if (ctx->account_mem)
4361 io_unaccount_mem(ctx->user,
4362 ring_pages(ctx->sq_entries, ctx->cq_entries));
4363 free_uid(ctx->user);
4364 put_cred(ctx->creds);
4365 kfree(ctx->completions);
4366 kmem_cache_free(req_cachep, ctx->fallback_req);
4370 static __poll_t io_uring_poll(struct file *file, poll_table *wait)
4372 struct io_ring_ctx *ctx = file->private_data;
4375 poll_wait(file, &ctx->cq_wait, wait);
4377 * synchronizes with barrier from wq_has_sleeper call in
4381 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
4382 ctx->rings->sq_ring_entries)
4383 mask |= EPOLLOUT | EPOLLWRNORM;
4384 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
4385 mask |= EPOLLIN | EPOLLRDNORM;
4390 static int io_uring_fasync(int fd, struct file *file, int on)
4392 struct io_ring_ctx *ctx = file->private_data;
4394 return fasync_helper(fd, file, on, &ctx->cq_fasync);
4397 static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
4399 mutex_lock(&ctx->uring_lock);
4400 percpu_ref_kill(&ctx->refs);
4401 mutex_unlock(&ctx->uring_lock);
4403 io_kill_timeouts(ctx);
4404 io_poll_remove_all(ctx);
4407 io_wq_cancel_all(ctx->io_wq);
4409 io_iopoll_reap_events(ctx);
4410 /* if we failed setting up the ctx, we might not have any rings */
4412 io_cqring_overflow_flush(ctx, true);
4413 wait_for_completion(&ctx->completions[0]);
4414 io_ring_ctx_free(ctx);
4417 static int io_uring_release(struct inode *inode, struct file *file)
4419 struct io_ring_ctx *ctx = file->private_data;
4421 file->private_data = NULL;
4422 io_ring_ctx_wait_and_kill(ctx);
4426 static void io_uring_cancel_files(struct io_ring_ctx *ctx,
4427 struct files_struct *files)
4429 struct io_kiocb *req;
4432 while (!list_empty_careful(&ctx->inflight_list)) {
4433 struct io_kiocb *cancel_req = NULL;
4435 spin_lock_irq(&ctx->inflight_lock);
4436 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
4437 if (req->work.files != files)
4439 /* req is being completed, ignore */
4440 if (!refcount_inc_not_zero(&req->refs))
4446 prepare_to_wait(&ctx->inflight_wait, &wait,
4447 TASK_UNINTERRUPTIBLE);
4448 spin_unlock_irq(&ctx->inflight_lock);
4450 /* We need to keep going until we don't find a matching req */
4454 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
4455 io_put_req(cancel_req);
4458 finish_wait(&ctx->inflight_wait, &wait);
4461 static int io_uring_flush(struct file *file, void *data)
4463 struct io_ring_ctx *ctx = file->private_data;
4465 io_uring_cancel_files(ctx, data);
4466 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
4467 io_cqring_overflow_flush(ctx, true);
4468 io_wq_cancel_all(ctx->io_wq);
4473 static void *io_uring_validate_mmap_request(struct file *file,
4474 loff_t pgoff, size_t sz)
4476 struct io_ring_ctx *ctx = file->private_data;
4477 loff_t offset = pgoff << PAGE_SHIFT;
4482 case IORING_OFF_SQ_RING:
4483 case IORING_OFF_CQ_RING:
4486 case IORING_OFF_SQES:
4490 return ERR_PTR(-EINVAL);
4493 page = virt_to_head_page(ptr);
4494 if (sz > page_size(page))
4495 return ERR_PTR(-EINVAL);
4502 static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
4504 size_t sz = vma->vm_end - vma->vm_start;
4508 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
4510 return PTR_ERR(ptr);
4512 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
4513 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
4516 #else /* !CONFIG_MMU */
4518 static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
4520 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
4523 static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
4525 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
4528 static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
4529 unsigned long addr, unsigned long len,
4530 unsigned long pgoff, unsigned long flags)
4534 ptr = io_uring_validate_mmap_request(file, pgoff, len);
4536 return PTR_ERR(ptr);
4538 return (unsigned long) ptr;
4541 #endif /* !CONFIG_MMU */
4543 SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
4544 u32, min_complete, u32, flags, const sigset_t __user *, sig,
4547 struct io_ring_ctx *ctx;
4552 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
4560 if (f.file->f_op != &io_uring_fops)
4564 ctx = f.file->private_data;
4565 if (!percpu_ref_tryget(&ctx->refs))
4569 * For SQ polling, the thread will do all submissions and completions.
4570 * Just return the requested submit count, and wake the thread if
4574 if (ctx->flags & IORING_SETUP_SQPOLL) {
4575 if (!list_empty_careful(&ctx->cq_overflow_list))
4576 io_cqring_overflow_flush(ctx, false);
4577 if (flags & IORING_ENTER_SQ_WAKEUP)
4578 wake_up(&ctx->sqo_wait);
4579 submitted = to_submit;
4580 } else if (to_submit) {
4581 struct mm_struct *cur_mm;
4583 to_submit = min(to_submit, ctx->sq_entries);
4584 mutex_lock(&ctx->uring_lock);
4585 /* already have mm, so io_submit_sqes() won't try to grab it */
4586 cur_mm = ctx->sqo_mm;
4587 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
4589 mutex_unlock(&ctx->uring_lock);
4591 if (flags & IORING_ENTER_GETEVENTS) {
4592 unsigned nr_events = 0;
4594 min_complete = min(min_complete, ctx->cq_entries);
4596 if (ctx->flags & IORING_SETUP_IOPOLL) {
4597 ret = io_iopoll_check(ctx, &nr_events, min_complete);
4599 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
4603 percpu_ref_put(&ctx->refs);
4606 return submitted ? submitted : ret;
4609 static const struct file_operations io_uring_fops = {
4610 .release = io_uring_release,
4611 .flush = io_uring_flush,
4612 .mmap = io_uring_mmap,
4614 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
4615 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
4617 .poll = io_uring_poll,
4618 .fasync = io_uring_fasync,
4621 static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
4622 struct io_uring_params *p)
4624 struct io_rings *rings;
4625 size_t size, sq_array_offset;
4627 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
4628 if (size == SIZE_MAX)
4631 rings = io_mem_alloc(size);
4636 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
4637 rings->sq_ring_mask = p->sq_entries - 1;
4638 rings->cq_ring_mask = p->cq_entries - 1;
4639 rings->sq_ring_entries = p->sq_entries;
4640 rings->cq_ring_entries = p->cq_entries;
4641 ctx->sq_mask = rings->sq_ring_mask;
4642 ctx->cq_mask = rings->cq_ring_mask;
4643 ctx->sq_entries = rings->sq_ring_entries;
4644 ctx->cq_entries = rings->cq_ring_entries;
4646 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
4647 if (size == SIZE_MAX) {
4648 io_mem_free(ctx->rings);
4653 ctx->sq_sqes = io_mem_alloc(size);
4654 if (!ctx->sq_sqes) {
4655 io_mem_free(ctx->rings);
4664 * Allocate an anonymous fd, this is what constitutes the application
4665 * visible backing of an io_uring instance. The application mmaps this
4666 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
4667 * we have to tie this fd to a socket for file garbage collection purposes.
4669 static int io_uring_get_fd(struct io_ring_ctx *ctx)
4674 #if defined(CONFIG_UNIX)
4675 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
4681 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
4685 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
4686 O_RDWR | O_CLOEXEC);
4689 ret = PTR_ERR(file);
4693 #if defined(CONFIG_UNIX)
4694 ctx->ring_sock->file = file;
4695 ctx->ring_sock->sk->sk_user_data = ctx;
4697 fd_install(ret, file);
4700 #if defined(CONFIG_UNIX)
4701 sock_release(ctx->ring_sock);
4702 ctx->ring_sock = NULL;
4707 static int io_uring_create(unsigned entries, struct io_uring_params *p)
4709 struct user_struct *user = NULL;
4710 struct io_ring_ctx *ctx;
4714 if (!entries || entries > IORING_MAX_ENTRIES)
4718 * Use twice as many entries for the CQ ring. It's possible for the
4719 * application to drive a higher depth than the size of the SQ ring,
4720 * since the sqes are only used at submission time. This allows for
4721 * some flexibility in overcommitting a bit. If the application has
4722 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
4723 * of CQ ring entries manually.
4725 p->sq_entries = roundup_pow_of_two(entries);
4726 if (p->flags & IORING_SETUP_CQSIZE) {
4728 * If IORING_SETUP_CQSIZE is set, we do the same roundup
4729 * to a power-of-two, if it isn't already. We do NOT impose
4730 * any cq vs sq ring sizing.
4732 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
4734 p->cq_entries = roundup_pow_of_two(p->cq_entries);
4736 p->cq_entries = 2 * p->sq_entries;
4739 user = get_uid(current_user());
4740 account_mem = !capable(CAP_IPC_LOCK);
4743 ret = io_account_mem(user,
4744 ring_pages(p->sq_entries, p->cq_entries));
4751 ctx = io_ring_ctx_alloc(p);
4754 io_unaccount_mem(user, ring_pages(p->sq_entries,
4759 ctx->compat = in_compat_syscall();
4760 ctx->account_mem = account_mem;
4762 ctx->creds = prepare_creds();
4764 ret = io_allocate_scq_urings(ctx, p);
4768 ret = io_sq_offload_start(ctx, p);
4772 memset(&p->sq_off, 0, sizeof(p->sq_off));
4773 p->sq_off.head = offsetof(struct io_rings, sq.head);
4774 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
4775 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
4776 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
4777 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
4778 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
4779 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
4781 memset(&p->cq_off, 0, sizeof(p->cq_off));
4782 p->cq_off.head = offsetof(struct io_rings, cq.head);
4783 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
4784 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
4785 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
4786 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
4787 p->cq_off.cqes = offsetof(struct io_rings, cqes);
4790 * Install ring fd as the very last thing, so we don't risk someone
4791 * having closed it before we finish setup
4793 ret = io_uring_get_fd(ctx);
4797 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP;
4798 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
4801 io_ring_ctx_wait_and_kill(ctx);
4806 * Sets up an aio uring context, and returns the fd. Applications asks for a
4807 * ring size, we return the actual sq/cq ring sizes (among other things) in the
4808 * params structure passed in.
4810 static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
4812 struct io_uring_params p;
4816 if (copy_from_user(&p, params, sizeof(p)))
4818 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
4823 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
4824 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
4827 ret = io_uring_create(entries, &p);
4831 if (copy_to_user(params, &p, sizeof(p)))
4837 SYSCALL_DEFINE2(io_uring_setup, u32, entries,
4838 struct io_uring_params __user *, params)
4840 return io_uring_setup(entries, params);
4843 static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
4844 void __user *arg, unsigned nr_args)
4845 __releases(ctx->uring_lock)
4846 __acquires(ctx->uring_lock)
4851 * We're inside the ring mutex, if the ref is already dying, then
4852 * someone else killed the ctx or is already going through
4853 * io_uring_register().
4855 if (percpu_ref_is_dying(&ctx->refs))
4858 percpu_ref_kill(&ctx->refs);
4861 * Drop uring mutex before waiting for references to exit. If another
4862 * thread is currently inside io_uring_enter() it might need to grab
4863 * the uring_lock to make progress. If we hold it here across the drain
4864 * wait, then we can deadlock. It's safe to drop the mutex here, since
4865 * no new references will come in after we've killed the percpu ref.
4867 mutex_unlock(&ctx->uring_lock);
4868 wait_for_completion(&ctx->completions[0]);
4869 mutex_lock(&ctx->uring_lock);
4872 case IORING_REGISTER_BUFFERS:
4873 ret = io_sqe_buffer_register(ctx, arg, nr_args);
4875 case IORING_UNREGISTER_BUFFERS:
4879 ret = io_sqe_buffer_unregister(ctx);
4881 case IORING_REGISTER_FILES:
4882 ret = io_sqe_files_register(ctx, arg, nr_args);
4884 case IORING_UNREGISTER_FILES:
4888 ret = io_sqe_files_unregister(ctx);
4890 case IORING_REGISTER_FILES_UPDATE:
4891 ret = io_sqe_files_update(ctx, arg, nr_args);
4893 case IORING_REGISTER_EVENTFD:
4897 ret = io_eventfd_register(ctx, arg);
4899 case IORING_UNREGISTER_EVENTFD:
4903 ret = io_eventfd_unregister(ctx);
4910 /* bring the ctx back to life */
4911 reinit_completion(&ctx->completions[0]);
4912 percpu_ref_reinit(&ctx->refs);
4916 SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
4917 void __user *, arg, unsigned int, nr_args)
4919 struct io_ring_ctx *ctx;
4928 if (f.file->f_op != &io_uring_fops)
4931 ctx = f.file->private_data;
4933 mutex_lock(&ctx->uring_lock);
4934 ret = __io_uring_register(ctx, opcode, arg, nr_args);
4935 mutex_unlock(&ctx->uring_lock);
4936 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
4937 ctx->cq_ev_fd != NULL, ret);
4943 static int __init io_uring_init(void)
4945 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
4948 __initcall(io_uring_init);