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. When the application reads the CQ ring
8 * tail, it must use an appropriate smp_rmb() to order with the smp_wmb()
9 * the kernel uses after writing the tail. Failure to do so could cause a
10 * delay in when the application notices that completion events available.
11 * This isn't a fatal condition. Likewise, the application must use an
12 * appropriate smp_wmb() both before writing the SQ tail, and after writing
13 * the SQ tail. The first one orders the sqe writes with the tail write, and
14 * the latter is paired with the smp_rmb() the kernel will issue before
15 * reading the SQ tail on submission.
17 * Also see the examples in the liburing library:
19 * git://git.kernel.dk/liburing
21 * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens
22 * from data shared between the kernel and application. This is done both
23 * for ordering purposes, but also to ensure that once a value is loaded from
24 * data that the application could potentially modify, it remains stable.
26 * Copyright (C) 2018-2019 Jens Axboe
28 #include <linux/kernel.h>
29 #include <linux/init.h>
30 #include <linux/errno.h>
31 #include <linux/syscalls.h>
32 #include <linux/compat.h>
33 #include <linux/refcount.h>
34 #include <linux/uio.h>
36 #include <linux/sched/signal.h>
38 #include <linux/file.h>
39 #include <linux/fdtable.h>
41 #include <linux/mman.h>
42 #include <linux/mmu_context.h>
43 #include <linux/percpu.h>
44 #include <linux/slab.h>
45 #include <linux/workqueue.h>
46 #include <linux/blkdev.h>
47 #include <linux/net.h>
49 #include <net/af_unix.h>
50 #include <linux/anon_inodes.h>
51 #include <linux/sched/mm.h>
52 #include <linux/uaccess.h>
53 #include <linux/nospec.h>
55 #include <uapi/linux/io_uring.h>
59 #define IORING_MAX_ENTRIES 4096
62 u32 head ____cacheline_aligned_in_smp;
63 u32 tail ____cacheline_aligned_in_smp;
80 struct io_uring_cqe cqes[];
85 struct percpu_ref refs;
86 } ____cacheline_aligned_in_smp;
94 struct io_sq_ring *sq_ring;
95 unsigned cached_sq_head;
98 struct io_uring_sqe *sq_sqes;
99 } ____cacheline_aligned_in_smp;
102 struct workqueue_struct *sqo_wq;
103 struct mm_struct *sqo_mm;
107 struct io_cq_ring *cq_ring;
108 unsigned cached_cq_tail;
111 struct wait_queue_head cq_wait;
112 struct fasync_struct *cq_fasync;
113 } ____cacheline_aligned_in_smp;
115 struct user_struct *user;
117 struct completion ctx_done;
120 struct mutex uring_lock;
121 wait_queue_head_t wait;
122 } ____cacheline_aligned_in_smp;
125 spinlock_t completion_lock;
126 } ____cacheline_aligned_in_smp;
128 #if defined(CONFIG_UNIX)
129 struct socket *ring_sock;
134 const struct io_uring_sqe *sqe;
135 unsigned short index;
142 struct sqe_submit submit;
144 struct io_ring_ctx *ctx;
145 struct list_head list;
147 #define REQ_F_FORCE_NONBLOCK 1 /* inline submission attempt */
150 struct work_struct work;
153 #define IO_PLUG_THRESHOLD 2
155 static struct kmem_cache *req_cachep;
157 static const struct file_operations io_uring_fops;
159 struct sock *io_uring_get_socket(struct file *file)
161 #if defined(CONFIG_UNIX)
162 if (file->f_op == &io_uring_fops) {
163 struct io_ring_ctx *ctx = file->private_data;
165 return ctx->ring_sock->sk;
170 EXPORT_SYMBOL(io_uring_get_socket);
172 static void io_ring_ctx_ref_free(struct percpu_ref *ref)
174 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
176 complete(&ctx->ctx_done);
179 static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
181 struct io_ring_ctx *ctx;
183 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
187 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, 0, GFP_KERNEL)) {
192 ctx->flags = p->flags;
193 init_waitqueue_head(&ctx->cq_wait);
194 init_completion(&ctx->ctx_done);
195 mutex_init(&ctx->uring_lock);
196 init_waitqueue_head(&ctx->wait);
197 spin_lock_init(&ctx->completion_lock);
201 static void io_commit_cqring(struct io_ring_ctx *ctx)
203 struct io_cq_ring *ring = ctx->cq_ring;
205 if (ctx->cached_cq_tail != READ_ONCE(ring->r.tail)) {
206 /* order cqe stores with ring update */
207 smp_store_release(&ring->r.tail, ctx->cached_cq_tail);
210 * Write sider barrier of tail update, app has read side. See
211 * comment at the top of this file.
215 if (wq_has_sleeper(&ctx->cq_wait)) {
216 wake_up_interruptible(&ctx->cq_wait);
217 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
222 static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
224 struct io_cq_ring *ring = ctx->cq_ring;
227 tail = ctx->cached_cq_tail;
228 /* See comment at the top of the file */
230 if (tail + 1 == READ_ONCE(ring->r.head))
233 ctx->cached_cq_tail++;
234 return &ring->cqes[tail & ctx->cq_mask];
237 static void io_cqring_fill_event(struct io_ring_ctx *ctx, u64 ki_user_data,
238 long res, unsigned ev_flags)
240 struct io_uring_cqe *cqe;
243 * If we can't get a cq entry, userspace overflowed the
244 * submission (by quite a lot). Increment the overflow count in
247 cqe = io_get_cqring(ctx);
249 WRITE_ONCE(cqe->user_data, ki_user_data);
250 WRITE_ONCE(cqe->res, res);
251 WRITE_ONCE(cqe->flags, ev_flags);
253 unsigned overflow = READ_ONCE(ctx->cq_ring->overflow);
255 WRITE_ONCE(ctx->cq_ring->overflow, overflow + 1);
259 static void io_cqring_add_event(struct io_ring_ctx *ctx, u64 ki_user_data,
260 long res, unsigned ev_flags)
264 spin_lock_irqsave(&ctx->completion_lock, flags);
265 io_cqring_fill_event(ctx, ki_user_data, res, ev_flags);
266 io_commit_cqring(ctx);
267 spin_unlock_irqrestore(&ctx->completion_lock, flags);
269 if (waitqueue_active(&ctx->wait))
273 static void io_ring_drop_ctx_refs(struct io_ring_ctx *ctx, unsigned refs)
275 percpu_ref_put_many(&ctx->refs, refs);
277 if (waitqueue_active(&ctx->wait))
281 static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx)
283 struct io_kiocb *req;
285 if (!percpu_ref_tryget(&ctx->refs))
288 req = kmem_cache_alloc(req_cachep, __GFP_NOWARN);
295 io_ring_drop_ctx_refs(ctx, 1);
299 static void io_free_req(struct io_kiocb *req)
301 io_ring_drop_ctx_refs(req->ctx, 1);
302 kmem_cache_free(req_cachep, req);
305 static void kiocb_end_write(struct kiocb *kiocb)
307 if (kiocb->ki_flags & IOCB_WRITE) {
308 struct inode *inode = file_inode(kiocb->ki_filp);
311 * Tell lockdep we inherited freeze protection from submission
314 if (S_ISREG(inode->i_mode))
315 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
316 file_end_write(kiocb->ki_filp);
320 static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
322 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
324 kiocb_end_write(kiocb);
326 fput(kiocb->ki_filp);
327 io_cqring_add_event(req->ctx, req->user_data, res, 0);
332 * If we tracked the file through the SCM inflight mechanism, we could support
333 * any file. For now, just ensure that anything potentially problematic is done
336 static bool io_file_supports_async(struct file *file)
338 umode_t mode = file_inode(file)->i_mode;
340 if (S_ISBLK(mode) || S_ISCHR(mode))
342 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
348 static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
351 struct kiocb *kiocb = &req->rw;
355 /* For -EAGAIN retry, everything is already prepped */
359 fd = READ_ONCE(sqe->fd);
360 kiocb->ki_filp = fget(fd);
361 if (unlikely(!kiocb->ki_filp))
363 if (force_nonblock && !io_file_supports_async(kiocb->ki_filp))
364 force_nonblock = false;
365 kiocb->ki_pos = READ_ONCE(sqe->off);
366 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
367 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
369 ioprio = READ_ONCE(sqe->ioprio);
371 ret = ioprio_check_cap(ioprio);
375 kiocb->ki_ioprio = ioprio;
377 kiocb->ki_ioprio = get_current_ioprio();
379 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
382 if (force_nonblock) {
383 kiocb->ki_flags |= IOCB_NOWAIT;
384 req->flags |= REQ_F_FORCE_NONBLOCK;
386 if (kiocb->ki_flags & IOCB_HIPRI) {
391 kiocb->ki_complete = io_complete_rw;
394 fput(kiocb->ki_filp);
398 static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
404 case -ERESTARTNOINTR:
405 case -ERESTARTNOHAND:
406 case -ERESTART_RESTARTBLOCK:
408 * We can't just restart the syscall, since previously
409 * submitted sqes may already be in progress. Just fail this
415 kiocb->ki_complete(kiocb, ret, 0);
419 static int io_import_iovec(struct io_ring_ctx *ctx, int rw,
420 const struct sqe_submit *s, struct iovec **iovec,
421 struct iov_iter *iter)
423 const struct io_uring_sqe *sqe = s->sqe;
424 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
425 size_t sqe_len = READ_ONCE(sqe->len);
432 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
436 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
439 static ssize_t io_read(struct io_kiocb *req, const struct sqe_submit *s,
442 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
443 struct kiocb *kiocb = &req->rw;
444 struct iov_iter iter;
448 ret = io_prep_rw(req, s->sqe, force_nonblock);
451 file = kiocb->ki_filp;
454 if (unlikely(!(file->f_mode & FMODE_READ)))
457 if (unlikely(!file->f_op->read_iter))
460 ret = io_import_iovec(req->ctx, READ, s, &iovec, &iter);
464 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_iter_count(&iter));
468 /* Catch -EAGAIN return for forced non-blocking submission */
469 ret2 = call_read_iter(file, kiocb, &iter);
470 if (!force_nonblock || ret2 != -EAGAIN)
471 io_rw_done(kiocb, ret2);
477 /* Hold on to the file for -EAGAIN */
478 if (unlikely(ret && ret != -EAGAIN))
483 static ssize_t io_write(struct io_kiocb *req, const struct sqe_submit *s,
486 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
487 struct kiocb *kiocb = &req->rw;
488 struct iov_iter iter;
492 ret = io_prep_rw(req, s->sqe, force_nonblock);
495 /* Hold on to the file for -EAGAIN */
496 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT))
500 file = kiocb->ki_filp;
501 if (unlikely(!(file->f_mode & FMODE_WRITE)))
504 if (unlikely(!file->f_op->write_iter))
507 ret = io_import_iovec(req->ctx, WRITE, s, &iovec, &iter);
511 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos,
512 iov_iter_count(&iter));
515 * Open-code file_start_write here to grab freeze protection,
516 * which will be released by another thread in
517 * io_complete_rw(). Fool lockdep by telling it the lock got
518 * released so that it doesn't complain about the held lock when
519 * we return to userspace.
521 if (S_ISREG(file_inode(file)->i_mode)) {
522 __sb_start_write(file_inode(file)->i_sb,
523 SB_FREEZE_WRITE, true);
524 __sb_writers_release(file_inode(file)->i_sb,
527 kiocb->ki_flags |= IOCB_WRITE;
528 io_rw_done(kiocb, call_write_iter(file, kiocb, &iter));
538 * IORING_OP_NOP just posts a completion event, nothing else.
540 static int io_nop(struct io_kiocb *req, u64 user_data)
542 struct io_ring_ctx *ctx = req->ctx;
546 * Twilight zone - it's possible that someone issued an opcode that
547 * has a file attached, then got -EAGAIN on submission, and changed
548 * the sqe before we retried it from async context. Avoid dropping
549 * a file reference for this malicious case, and flag the error.
551 if (req->rw.ki_filp) {
553 fput(req->rw.ki_filp);
555 io_cqring_add_event(ctx, user_data, err, 0);
560 static int __io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
561 const struct sqe_submit *s, bool force_nonblock)
566 if (unlikely(s->index >= ctx->sq_entries))
568 req->user_data = READ_ONCE(s->sqe->user_data);
570 opcode = READ_ONCE(s->sqe->opcode);
573 ret = io_nop(req, req->user_data);
575 case IORING_OP_READV:
576 ret = io_read(req, s, force_nonblock);
578 case IORING_OP_WRITEV:
579 ret = io_write(req, s, force_nonblock);
589 static void io_sq_wq_submit_work(struct work_struct *work)
591 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
592 struct sqe_submit *s = &req->submit;
593 const struct io_uring_sqe *sqe = s->sqe;
594 struct io_ring_ctx *ctx = req->ctx;
595 mm_segment_t old_fs = get_fs();
598 /* Ensure we clear previously set forced non-block flag */
599 req->flags &= ~REQ_F_FORCE_NONBLOCK;
600 req->rw.ki_flags &= ~IOCB_NOWAIT;
602 if (!mmget_not_zero(ctx->sqo_mm)) {
611 ret = __io_submit_sqe(ctx, req, s, false);
614 unuse_mm(ctx->sqo_mm);
618 io_cqring_add_event(ctx, sqe->user_data, ret, 0);
622 /* async context always use a copy of the sqe */
626 static int io_submit_sqe(struct io_ring_ctx *ctx, struct sqe_submit *s)
628 struct io_kiocb *req;
631 /* enforce forwards compatibility on users */
632 if (unlikely(s->sqe->flags))
635 req = io_get_req(ctx);
639 req->rw.ki_filp = NULL;
641 ret = __io_submit_sqe(ctx, req, s, true);
642 if (ret == -EAGAIN) {
643 struct io_uring_sqe *sqe_copy;
645 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
647 memcpy(sqe_copy, s->sqe, sizeof(*sqe_copy));
650 memcpy(&req->submit, s, sizeof(*s));
651 INIT_WORK(&req->work, io_sq_wq_submit_work);
652 queue_work(ctx->sqo_wq, &req->work);
662 static void io_commit_sqring(struct io_ring_ctx *ctx)
664 struct io_sq_ring *ring = ctx->sq_ring;
666 if (ctx->cached_sq_head != READ_ONCE(ring->r.head)) {
668 * Ensure any loads from the SQEs are done at this point,
669 * since once we write the new head, the application could
670 * write new data to them.
672 smp_store_release(&ring->r.head, ctx->cached_sq_head);
675 * write side barrier of head update, app has read side. See
676 * comment at the top of this file
683 * Undo last io_get_sqring()
685 static void io_drop_sqring(struct io_ring_ctx *ctx)
687 ctx->cached_sq_head--;
691 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
692 * that is mapped by userspace. This means that care needs to be taken to
693 * ensure that reads are stable, as we cannot rely on userspace always
694 * being a good citizen. If members of the sqe are validated and then later
695 * used, it's important that those reads are done through READ_ONCE() to
696 * prevent a re-load down the line.
698 static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
700 struct io_sq_ring *ring = ctx->sq_ring;
704 * The cached sq head (or cq tail) serves two purposes:
706 * 1) allows us to batch the cost of updating the user visible
708 * 2) allows the kernel side to track the head on its own, even
709 * though the application is the one updating it.
711 head = ctx->cached_sq_head;
712 /* See comment at the top of this file */
714 if (head == READ_ONCE(ring->r.tail))
717 head = READ_ONCE(ring->array[head & ctx->sq_mask]);
718 if (head < ctx->sq_entries) {
720 s->sqe = &ctx->sq_sqes[head];
721 ctx->cached_sq_head++;
725 /* drop invalid entries */
726 ctx->cached_sq_head++;
728 /* See comment at the top of this file */
733 static int io_ring_submit(struct io_ring_ctx *ctx, unsigned int to_submit)
735 int i, ret = 0, submit = 0;
736 struct blk_plug plug;
738 if (to_submit > IO_PLUG_THRESHOLD)
739 blk_start_plug(&plug);
741 for (i = 0; i < to_submit; i++) {
744 if (!io_get_sqring(ctx, &s))
748 ret = io_submit_sqe(ctx, &s);
756 io_commit_sqring(ctx);
758 if (to_submit > IO_PLUG_THRESHOLD)
759 blk_finish_plug(&plug);
761 return submit ? submit : ret;
764 static unsigned io_cqring_events(struct io_cq_ring *ring)
766 return READ_ONCE(ring->r.tail) - READ_ONCE(ring->r.head);
770 * Wait until events become available, if we don't already have some. The
771 * application must reap them itself, as they reside on the shared cq ring.
773 static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
774 const sigset_t __user *sig, size_t sigsz)
776 struct io_cq_ring *ring = ctx->cq_ring;
777 sigset_t ksigmask, sigsaved;
781 /* See comment at the top of this file */
783 if (io_cqring_events(ring) >= min_events)
787 ret = set_user_sigmask(sig, &ksigmask, &sigsaved, sigsz);
793 prepare_to_wait(&ctx->wait, &wait, TASK_INTERRUPTIBLE);
796 /* See comment at the top of this file */
798 if (io_cqring_events(ring) >= min_events)
804 if (signal_pending(current))
808 finish_wait(&ctx->wait, &wait);
811 restore_user_sigmask(sig, &sigsaved);
813 return READ_ONCE(ring->r.head) == READ_ONCE(ring->r.tail) ? ret : 0;
816 static int io_sq_offload_start(struct io_ring_ctx *ctx)
821 ctx->sqo_mm = current->mm;
823 /* Do QD, or 2 * CPUS, whatever is smallest */
824 ctx->sqo_wq = alloc_workqueue("io_ring-wq", WQ_UNBOUND | WQ_FREEZABLE,
825 min(ctx->sq_entries - 1, 2 * num_online_cpus()));
838 static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
840 atomic_long_sub(nr_pages, &user->locked_vm);
843 static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
845 unsigned long page_limit, cur_pages, new_pages;
847 /* Don't allow more pages than we can safely lock */
848 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
851 cur_pages = atomic_long_read(&user->locked_vm);
852 new_pages = cur_pages + nr_pages;
853 if (new_pages > page_limit)
855 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
856 new_pages) != cur_pages);
861 static void io_mem_free(void *ptr)
863 struct page *page = virt_to_head_page(ptr);
865 if (put_page_testzero(page))
866 free_compound_page(page);
869 static void *io_mem_alloc(size_t size)
871 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
874 return (void *) __get_free_pages(gfp_flags, get_order(size));
877 static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
879 struct io_sq_ring *sq_ring;
880 struct io_cq_ring *cq_ring;
883 bytes = struct_size(sq_ring, array, sq_entries);
884 bytes += array_size(sizeof(struct io_uring_sqe), sq_entries);
885 bytes += struct_size(cq_ring, cqes, cq_entries);
887 return (bytes + PAGE_SIZE - 1) / PAGE_SIZE;
890 static void io_ring_ctx_free(struct io_ring_ctx *ctx)
893 destroy_workqueue(ctx->sqo_wq);
896 #if defined(CONFIG_UNIX)
898 sock_release(ctx->ring_sock);
901 io_mem_free(ctx->sq_ring);
902 io_mem_free(ctx->sq_sqes);
903 io_mem_free(ctx->cq_ring);
905 percpu_ref_exit(&ctx->refs);
906 if (ctx->account_mem)
907 io_unaccount_mem(ctx->user,
908 ring_pages(ctx->sq_entries, ctx->cq_entries));
913 static __poll_t io_uring_poll(struct file *file, poll_table *wait)
915 struct io_ring_ctx *ctx = file->private_data;
918 poll_wait(file, &ctx->cq_wait, wait);
919 /* See comment at the top of this file */
921 if (READ_ONCE(ctx->sq_ring->r.tail) + 1 != ctx->cached_sq_head)
922 mask |= EPOLLOUT | EPOLLWRNORM;
923 if (READ_ONCE(ctx->cq_ring->r.head) != ctx->cached_cq_tail)
924 mask |= EPOLLIN | EPOLLRDNORM;
929 static int io_uring_fasync(int fd, struct file *file, int on)
931 struct io_ring_ctx *ctx = file->private_data;
933 return fasync_helper(fd, file, on, &ctx->cq_fasync);
936 static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
938 mutex_lock(&ctx->uring_lock);
939 percpu_ref_kill(&ctx->refs);
940 mutex_unlock(&ctx->uring_lock);
942 wait_for_completion(&ctx->ctx_done);
943 io_ring_ctx_free(ctx);
946 static int io_uring_release(struct inode *inode, struct file *file)
948 struct io_ring_ctx *ctx = file->private_data;
950 file->private_data = NULL;
951 io_ring_ctx_wait_and_kill(ctx);
955 static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
957 loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT;
958 unsigned long sz = vma->vm_end - vma->vm_start;
959 struct io_ring_ctx *ctx = file->private_data;
965 case IORING_OFF_SQ_RING:
968 case IORING_OFF_SQES:
971 case IORING_OFF_CQ_RING:
978 page = virt_to_head_page(ptr);
979 if (sz > (PAGE_SIZE << compound_order(page)))
982 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
983 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
986 SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
987 u32, min_complete, u32, flags, const sigset_t __user *, sig,
990 struct io_ring_ctx *ctx;
995 if (flags & ~IORING_ENTER_GETEVENTS)
1003 if (f.file->f_op != &io_uring_fops)
1007 ctx = f.file->private_data;
1008 if (!percpu_ref_tryget(&ctx->refs))
1013 to_submit = min(to_submit, ctx->sq_entries);
1015 mutex_lock(&ctx->uring_lock);
1016 submitted = io_ring_submit(ctx, to_submit);
1017 mutex_unlock(&ctx->uring_lock);
1022 if (flags & IORING_ENTER_GETEVENTS) {
1023 min_complete = min(min_complete, ctx->cq_entries);
1026 * The application could have included the 'to_submit' count
1027 * in how many events it wanted to wait for. If we failed to
1028 * submit the desired count, we may need to adjust the number
1029 * of events to poll/wait for.
1031 if (submitted < to_submit)
1032 min_complete = min_t(unsigned, submitted, min_complete);
1034 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
1038 io_ring_drop_ctx_refs(ctx, 1);
1041 return submitted ? submitted : ret;
1044 static const struct file_operations io_uring_fops = {
1045 .release = io_uring_release,
1046 .mmap = io_uring_mmap,
1047 .poll = io_uring_poll,
1048 .fasync = io_uring_fasync,
1051 static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
1052 struct io_uring_params *p)
1054 struct io_sq_ring *sq_ring;
1055 struct io_cq_ring *cq_ring;
1058 sq_ring = io_mem_alloc(struct_size(sq_ring, array, p->sq_entries));
1062 ctx->sq_ring = sq_ring;
1063 sq_ring->ring_mask = p->sq_entries - 1;
1064 sq_ring->ring_entries = p->sq_entries;
1065 ctx->sq_mask = sq_ring->ring_mask;
1066 ctx->sq_entries = sq_ring->ring_entries;
1068 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
1069 if (size == SIZE_MAX)
1072 ctx->sq_sqes = io_mem_alloc(size);
1073 if (!ctx->sq_sqes) {
1074 io_mem_free(ctx->sq_ring);
1078 cq_ring = io_mem_alloc(struct_size(cq_ring, cqes, p->cq_entries));
1080 io_mem_free(ctx->sq_ring);
1081 io_mem_free(ctx->sq_sqes);
1085 ctx->cq_ring = cq_ring;
1086 cq_ring->ring_mask = p->cq_entries - 1;
1087 cq_ring->ring_entries = p->cq_entries;
1088 ctx->cq_mask = cq_ring->ring_mask;
1089 ctx->cq_entries = cq_ring->ring_entries;
1094 * Allocate an anonymous fd, this is what constitutes the application
1095 * visible backing of an io_uring instance. The application mmaps this
1096 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
1097 * we have to tie this fd to a socket for file garbage collection purposes.
1099 static int io_uring_get_fd(struct io_ring_ctx *ctx)
1104 #if defined(CONFIG_UNIX)
1105 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
1111 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
1115 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
1116 O_RDWR | O_CLOEXEC);
1119 ret = PTR_ERR(file);
1123 #if defined(CONFIG_UNIX)
1124 ctx->ring_sock->file = file;
1126 fd_install(ret, file);
1129 #if defined(CONFIG_UNIX)
1130 sock_release(ctx->ring_sock);
1131 ctx->ring_sock = NULL;
1136 static int io_uring_create(unsigned entries, struct io_uring_params *p)
1138 struct user_struct *user = NULL;
1139 struct io_ring_ctx *ctx;
1143 if (!entries || entries > IORING_MAX_ENTRIES)
1147 * Use twice as many entries for the CQ ring. It's possible for the
1148 * application to drive a higher depth than the size of the SQ ring,
1149 * since the sqes are only used at submission time. This allows for
1150 * some flexibility in overcommitting a bit.
1152 p->sq_entries = roundup_pow_of_two(entries);
1153 p->cq_entries = 2 * p->sq_entries;
1155 user = get_uid(current_user());
1156 account_mem = !capable(CAP_IPC_LOCK);
1159 ret = io_account_mem(user,
1160 ring_pages(p->sq_entries, p->cq_entries));
1167 ctx = io_ring_ctx_alloc(p);
1170 io_unaccount_mem(user, ring_pages(p->sq_entries,
1175 ctx->compat = in_compat_syscall();
1176 ctx->account_mem = account_mem;
1179 ret = io_allocate_scq_urings(ctx, p);
1183 ret = io_sq_offload_start(ctx);
1187 ret = io_uring_get_fd(ctx);
1191 memset(&p->sq_off, 0, sizeof(p->sq_off));
1192 p->sq_off.head = offsetof(struct io_sq_ring, r.head);
1193 p->sq_off.tail = offsetof(struct io_sq_ring, r.tail);
1194 p->sq_off.ring_mask = offsetof(struct io_sq_ring, ring_mask);
1195 p->sq_off.ring_entries = offsetof(struct io_sq_ring, ring_entries);
1196 p->sq_off.flags = offsetof(struct io_sq_ring, flags);
1197 p->sq_off.dropped = offsetof(struct io_sq_ring, dropped);
1198 p->sq_off.array = offsetof(struct io_sq_ring, array);
1200 memset(&p->cq_off, 0, sizeof(p->cq_off));
1201 p->cq_off.head = offsetof(struct io_cq_ring, r.head);
1202 p->cq_off.tail = offsetof(struct io_cq_ring, r.tail);
1203 p->cq_off.ring_mask = offsetof(struct io_cq_ring, ring_mask);
1204 p->cq_off.ring_entries = offsetof(struct io_cq_ring, ring_entries);
1205 p->cq_off.overflow = offsetof(struct io_cq_ring, overflow);
1206 p->cq_off.cqes = offsetof(struct io_cq_ring, cqes);
1209 io_ring_ctx_wait_and_kill(ctx);
1214 * Sets up an aio uring context, and returns the fd. Applications asks for a
1215 * ring size, we return the actual sq/cq ring sizes (among other things) in the
1216 * params structure passed in.
1218 static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
1220 struct io_uring_params p;
1224 if (copy_from_user(&p, params, sizeof(p)))
1226 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
1234 ret = io_uring_create(entries, &p);
1238 if (copy_to_user(params, &p, sizeof(p)))
1244 SYSCALL_DEFINE2(io_uring_setup, u32, entries,
1245 struct io_uring_params __user *, params)
1247 return io_uring_setup(entries, params);
1250 static int __init io_uring_init(void)
1252 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
1255 __initcall(io_uring_init);