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
27 * Copyright (c) 2018-2019 Christoph Hellwig
29 #include <linux/kernel.h>
30 #include <linux/init.h>
31 #include <linux/errno.h>
32 #include <linux/syscalls.h>
33 #include <linux/compat.h>
34 #include <linux/refcount.h>
35 #include <linux/uio.h>
37 #include <linux/sched/signal.h>
39 #include <linux/file.h>
40 #include <linux/fdtable.h>
42 #include <linux/mman.h>
43 #include <linux/mmu_context.h>
44 #include <linux/percpu.h>
45 #include <linux/slab.h>
46 #include <linux/workqueue.h>
47 #include <linux/kthread.h>
48 #include <linux/blkdev.h>
49 #include <linux/bvec.h>
50 #include <linux/net.h>
52 #include <net/af_unix.h>
54 #include <linux/anon_inodes.h>
55 #include <linux/sched/mm.h>
56 #include <linux/uaccess.h>
57 #include <linux/nospec.h>
58 #include <linux/sizes.h>
59 #include <linux/hugetlb.h>
61 #include <uapi/linux/io_uring.h>
65 #define IORING_MAX_ENTRIES 4096
66 #define IORING_MAX_FIXED_FILES 1024
69 u32 head ____cacheline_aligned_in_smp;
70 u32 tail ____cacheline_aligned_in_smp;
87 struct io_uring_cqe cqes[];
90 struct io_mapped_ubuf {
94 unsigned int nr_bvecs;
100 struct list_head list;
109 struct percpu_ref refs;
110 } ____cacheline_aligned_in_smp;
118 struct io_sq_ring *sq_ring;
119 unsigned cached_sq_head;
122 unsigned sq_thread_idle;
123 struct io_uring_sqe *sq_sqes;
124 } ____cacheline_aligned_in_smp;
127 struct workqueue_struct *sqo_wq;
128 struct task_struct *sqo_thread; /* if using sq thread polling */
129 struct mm_struct *sqo_mm;
130 wait_queue_head_t sqo_wait;
135 struct io_cq_ring *cq_ring;
136 unsigned cached_cq_tail;
139 struct wait_queue_head cq_wait;
140 struct fasync_struct *cq_fasync;
141 } ____cacheline_aligned_in_smp;
144 * If used, fixed file set. Writers must ensure that ->refs is dead,
145 * readers must ensure that ->refs is alive as long as the file* is
146 * used. Only updated through io_uring_register(2).
148 struct file **user_files;
149 unsigned nr_user_files;
151 /* if used, fixed mapped user buffers */
152 unsigned nr_user_bufs;
153 struct io_mapped_ubuf *user_bufs;
155 struct user_struct *user;
157 struct completion ctx_done;
160 struct mutex uring_lock;
161 wait_queue_head_t wait;
162 } ____cacheline_aligned_in_smp;
165 spinlock_t completion_lock;
166 bool poll_multi_file;
168 * ->poll_list is protected by the ctx->uring_lock for
169 * io_uring instances that don't use IORING_SETUP_SQPOLL.
170 * For SQPOLL, only the single threaded io_sq_thread() will
171 * manipulate the list, hence no extra locking is needed there.
173 struct list_head poll_list;
174 struct list_head cancel_list;
175 } ____cacheline_aligned_in_smp;
177 struct async_list pending_async[2];
179 #if defined(CONFIG_UNIX)
180 struct socket *ring_sock;
185 const struct io_uring_sqe *sqe;
186 unsigned short index;
189 bool needs_fixed_file;
193 * First field must be the file pointer in all the
194 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
196 struct io_poll_iocb {
198 struct wait_queue_head *head;
202 struct wait_queue_entry wait;
206 * NOTE! Each of the iocb union members has the file pointer
207 * as the first entry in their struct definition. So you can
208 * access the file pointer through any of the sub-structs,
209 * or directly as just 'ki_filp' in this struct.
215 struct io_poll_iocb poll;
218 struct sqe_submit submit;
220 struct io_ring_ctx *ctx;
221 struct list_head list;
224 #define REQ_F_FORCE_NONBLOCK 1 /* inline submission attempt */
225 #define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
226 #define REQ_F_FIXED_FILE 4 /* ctx owns file */
227 #define REQ_F_SEQ_PREV 8 /* sequential with previous */
228 #define REQ_F_PREPPED 16 /* prep already done */
232 struct work_struct work;
235 #define IO_PLUG_THRESHOLD 2
236 #define IO_IOPOLL_BATCH 8
238 struct io_submit_state {
239 struct blk_plug plug;
242 * io_kiocb alloc cache
244 void *reqs[IO_IOPOLL_BATCH];
245 unsigned int free_reqs;
246 unsigned int cur_req;
249 * File reference cache
253 unsigned int has_refs;
254 unsigned int used_refs;
255 unsigned int ios_left;
258 static struct kmem_cache *req_cachep;
260 static const struct file_operations io_uring_fops;
262 struct sock *io_uring_get_socket(struct file *file)
264 #if defined(CONFIG_UNIX)
265 if (file->f_op == &io_uring_fops) {
266 struct io_ring_ctx *ctx = file->private_data;
268 return ctx->ring_sock->sk;
273 EXPORT_SYMBOL(io_uring_get_socket);
275 static void io_ring_ctx_ref_free(struct percpu_ref *ref)
277 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
279 complete(&ctx->ctx_done);
282 static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
284 struct io_ring_ctx *ctx;
287 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
291 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, 0, GFP_KERNEL)) {
296 ctx->flags = p->flags;
297 init_waitqueue_head(&ctx->cq_wait);
298 init_completion(&ctx->ctx_done);
299 mutex_init(&ctx->uring_lock);
300 init_waitqueue_head(&ctx->wait);
301 for (i = 0; i < ARRAY_SIZE(ctx->pending_async); i++) {
302 spin_lock_init(&ctx->pending_async[i].lock);
303 INIT_LIST_HEAD(&ctx->pending_async[i].list);
304 atomic_set(&ctx->pending_async[i].cnt, 0);
306 spin_lock_init(&ctx->completion_lock);
307 INIT_LIST_HEAD(&ctx->poll_list);
308 INIT_LIST_HEAD(&ctx->cancel_list);
312 static void io_commit_cqring(struct io_ring_ctx *ctx)
314 struct io_cq_ring *ring = ctx->cq_ring;
316 if (ctx->cached_cq_tail != READ_ONCE(ring->r.tail)) {
317 /* order cqe stores with ring update */
318 smp_store_release(&ring->r.tail, ctx->cached_cq_tail);
321 * Write sider barrier of tail update, app has read side. See
322 * comment at the top of this file.
326 if (wq_has_sleeper(&ctx->cq_wait)) {
327 wake_up_interruptible(&ctx->cq_wait);
328 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
333 static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
335 struct io_cq_ring *ring = ctx->cq_ring;
338 tail = ctx->cached_cq_tail;
339 /* See comment at the top of the file */
341 if (tail + 1 == READ_ONCE(ring->r.head))
344 ctx->cached_cq_tail++;
345 return &ring->cqes[tail & ctx->cq_mask];
348 static void io_cqring_fill_event(struct io_ring_ctx *ctx, u64 ki_user_data,
349 long res, unsigned ev_flags)
351 struct io_uring_cqe *cqe;
354 * If we can't get a cq entry, userspace overflowed the
355 * submission (by quite a lot). Increment the overflow count in
358 cqe = io_get_cqring(ctx);
360 WRITE_ONCE(cqe->user_data, ki_user_data);
361 WRITE_ONCE(cqe->res, res);
362 WRITE_ONCE(cqe->flags, ev_flags);
364 unsigned overflow = READ_ONCE(ctx->cq_ring->overflow);
366 WRITE_ONCE(ctx->cq_ring->overflow, overflow + 1);
370 static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
372 if (waitqueue_active(&ctx->wait))
374 if (waitqueue_active(&ctx->sqo_wait))
375 wake_up(&ctx->sqo_wait);
378 static void io_cqring_add_event(struct io_ring_ctx *ctx, u64 user_data,
379 long res, unsigned ev_flags)
383 spin_lock_irqsave(&ctx->completion_lock, flags);
384 io_cqring_fill_event(ctx, user_data, res, ev_flags);
385 io_commit_cqring(ctx);
386 spin_unlock_irqrestore(&ctx->completion_lock, flags);
388 io_cqring_ev_posted(ctx);
391 static void io_ring_drop_ctx_refs(struct io_ring_ctx *ctx, unsigned refs)
393 percpu_ref_put_many(&ctx->refs, refs);
395 if (waitqueue_active(&ctx->wait))
399 static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
400 struct io_submit_state *state)
402 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
403 struct io_kiocb *req;
405 if (!percpu_ref_tryget(&ctx->refs))
409 req = kmem_cache_alloc(req_cachep, gfp);
412 } else if (!state->free_reqs) {
416 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
417 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
420 * Bulk alloc is all-or-nothing. If we fail to get a batch,
421 * retry single alloc to be on the safe side.
423 if (unlikely(ret <= 0)) {
424 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
429 state->free_reqs = ret - 1;
431 req = state->reqs[0];
433 req = state->reqs[state->cur_req];
440 /* one is dropped after submission, the other at completion */
441 refcount_set(&req->refs, 2);
444 io_ring_drop_ctx_refs(ctx, 1);
448 static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
451 kmem_cache_free_bulk(req_cachep, *nr, reqs);
452 io_ring_drop_ctx_refs(ctx, *nr);
457 static void io_free_req(struct io_kiocb *req)
459 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
461 io_ring_drop_ctx_refs(req->ctx, 1);
462 kmem_cache_free(req_cachep, req);
465 static void io_put_req(struct io_kiocb *req)
467 if (refcount_dec_and_test(&req->refs))
472 * Find and free completed poll iocbs
474 static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
475 struct list_head *done)
477 void *reqs[IO_IOPOLL_BATCH];
478 struct io_kiocb *req;
482 while (!list_empty(done)) {
483 req = list_first_entry(done, struct io_kiocb, list);
484 list_del(&req->list);
486 io_cqring_fill_event(ctx, req->user_data, req->error, 0);
489 if (refcount_dec_and_test(&req->refs)) {
490 /* If we're not using fixed files, we have to pair the
491 * completion part with the file put. Use regular
492 * completions for those, only batch free for fixed
495 if (req->flags & REQ_F_FIXED_FILE) {
496 reqs[to_free++] = req;
497 if (to_free == ARRAY_SIZE(reqs))
498 io_free_req_many(ctx, reqs, &to_free);
505 io_commit_cqring(ctx);
506 io_free_req_many(ctx, reqs, &to_free);
509 static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
512 struct io_kiocb *req, *tmp;
518 * Only spin for completions if we don't have multiple devices hanging
519 * off our complete list, and we're under the requested amount.
521 spin = !ctx->poll_multi_file && *nr_events < min;
524 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
525 struct kiocb *kiocb = &req->rw;
528 * Move completed entries to our local list. If we find a
529 * request that requires polling, break out and complete
530 * the done list first, if we have entries there.
532 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
533 list_move_tail(&req->list, &done);
536 if (!list_empty(&done))
539 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
548 if (!list_empty(&done))
549 io_iopoll_complete(ctx, nr_events, &done);
555 * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a
556 * non-spinning poll check - we'll still enter the driver poll loop, but only
557 * as a non-spinning completion check.
559 static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
562 while (!list_empty(&ctx->poll_list)) {
565 ret = io_do_iopoll(ctx, nr_events, min);
568 if (!min || *nr_events >= min)
576 * We can't just wait for polled events to come to us, we have to actively
577 * find and complete them.
579 static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
581 if (!(ctx->flags & IORING_SETUP_IOPOLL))
584 mutex_lock(&ctx->uring_lock);
585 while (!list_empty(&ctx->poll_list)) {
586 unsigned int nr_events = 0;
588 io_iopoll_getevents(ctx, &nr_events, 1);
590 mutex_unlock(&ctx->uring_lock);
593 static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
601 if (*nr_events < min)
602 tmin = min - *nr_events;
604 ret = io_iopoll_getevents(ctx, nr_events, tmin);
608 } while (min && !*nr_events && !need_resched());
613 static void kiocb_end_write(struct kiocb *kiocb)
615 if (kiocb->ki_flags & IOCB_WRITE) {
616 struct inode *inode = file_inode(kiocb->ki_filp);
619 * Tell lockdep we inherited freeze protection from submission
622 if (S_ISREG(inode->i_mode))
623 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
624 file_end_write(kiocb->ki_filp);
628 static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
630 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
632 kiocb_end_write(kiocb);
634 io_cqring_add_event(req->ctx, req->user_data, res, 0);
638 static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
640 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
642 kiocb_end_write(kiocb);
646 req->flags |= REQ_F_IOPOLL_COMPLETED;
650 * After the iocb has been issued, it's safe to be found on the poll list.
651 * Adding the kiocb to the list AFTER submission ensures that we don't
652 * find it from a io_iopoll_getevents() thread before the issuer is done
653 * accessing the kiocb cookie.
655 static void io_iopoll_req_issued(struct io_kiocb *req)
657 struct io_ring_ctx *ctx = req->ctx;
660 * Track whether we have multiple files in our lists. This will impact
661 * how we do polling eventually, not spinning if we're on potentially
664 if (list_empty(&ctx->poll_list)) {
665 ctx->poll_multi_file = false;
666 } else if (!ctx->poll_multi_file) {
667 struct io_kiocb *list_req;
669 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
671 if (list_req->rw.ki_filp != req->rw.ki_filp)
672 ctx->poll_multi_file = true;
676 * For fast devices, IO may have already completed. If it has, add
677 * it to the front so we find it first.
679 if (req->flags & REQ_F_IOPOLL_COMPLETED)
680 list_add(&req->list, &ctx->poll_list);
682 list_add_tail(&req->list, &ctx->poll_list);
685 static void io_file_put(struct io_submit_state *state, struct file *file)
689 } else if (state->file) {
690 int diff = state->has_refs - state->used_refs;
693 fput_many(state->file, diff);
699 * Get as many references to a file as we have IOs left in this submission,
700 * assuming most submissions are for one file, or at least that each file
701 * has more than one submission.
703 static struct file *io_file_get(struct io_submit_state *state, int fd)
709 if (state->fd == fd) {
714 io_file_put(state, NULL);
716 state->file = fget_many(fd, state->ios_left);
721 state->has_refs = state->ios_left;
722 state->used_refs = 1;
728 * If we tracked the file through the SCM inflight mechanism, we could support
729 * any file. For now, just ensure that anything potentially problematic is done
732 static bool io_file_supports_async(struct file *file)
734 umode_t mode = file_inode(file)->i_mode;
736 if (S_ISBLK(mode) || S_ISCHR(mode))
738 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
744 static int io_prep_rw(struct io_kiocb *req, const struct sqe_submit *s,
745 bool force_nonblock, struct io_submit_state *state)
747 const struct io_uring_sqe *sqe = s->sqe;
748 struct io_ring_ctx *ctx = req->ctx;
749 struct kiocb *kiocb = &req->rw;
755 /* For -EAGAIN retry, everything is already prepped */
756 if (req->flags & REQ_F_PREPPED)
759 if (force_nonblock && !io_file_supports_async(req->file))
760 force_nonblock = false;
762 kiocb->ki_pos = READ_ONCE(sqe->off);
763 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
764 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
766 ioprio = READ_ONCE(sqe->ioprio);
768 ret = ioprio_check_cap(ioprio);
772 kiocb->ki_ioprio = ioprio;
774 kiocb->ki_ioprio = get_current_ioprio();
776 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
779 if (force_nonblock) {
780 kiocb->ki_flags |= IOCB_NOWAIT;
781 req->flags |= REQ_F_FORCE_NONBLOCK;
783 if (ctx->flags & IORING_SETUP_IOPOLL) {
784 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
785 !kiocb->ki_filp->f_op->iopoll)
789 kiocb->ki_flags |= IOCB_HIPRI;
790 kiocb->ki_complete = io_complete_rw_iopoll;
792 if (kiocb->ki_flags & IOCB_HIPRI)
794 kiocb->ki_complete = io_complete_rw;
796 req->flags |= REQ_F_PREPPED;
800 static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
806 case -ERESTARTNOINTR:
807 case -ERESTARTNOHAND:
808 case -ERESTART_RESTARTBLOCK:
810 * We can't just restart the syscall, since previously
811 * submitted sqes may already be in progress. Just fail this
817 kiocb->ki_complete(kiocb, ret, 0);
821 static int io_import_fixed(struct io_ring_ctx *ctx, int rw,
822 const struct io_uring_sqe *sqe,
823 struct iov_iter *iter)
825 size_t len = READ_ONCE(sqe->len);
826 struct io_mapped_ubuf *imu;
827 unsigned index, buf_index;
831 /* attempt to use fixed buffers without having provided iovecs */
832 if (unlikely(!ctx->user_bufs))
835 buf_index = READ_ONCE(sqe->buf_index);
836 if (unlikely(buf_index >= ctx->nr_user_bufs))
839 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
840 imu = &ctx->user_bufs[index];
841 buf_addr = READ_ONCE(sqe->addr);
844 if (buf_addr + len < buf_addr)
846 /* not inside the mapped region */
847 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
851 * May not be a start of buffer, set size appropriately
852 * and advance us to the beginning.
854 offset = buf_addr - imu->ubuf;
855 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
857 iov_iter_advance(iter, offset);
859 /* don't drop a reference to these pages */
860 iter->type |= ITER_BVEC_FLAG_NO_REF;
864 static int io_import_iovec(struct io_ring_ctx *ctx, int rw,
865 const struct sqe_submit *s, struct iovec **iovec,
866 struct iov_iter *iter)
868 const struct io_uring_sqe *sqe = s->sqe;
869 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
870 size_t sqe_len = READ_ONCE(sqe->len);
874 * We're reading ->opcode for the second time, but the first read
875 * doesn't care whether it's _FIXED or not, so it doesn't matter
876 * whether ->opcode changes concurrently. The first read does care
877 * about whether it is a READ or a WRITE, so we don't trust this read
878 * for that purpose and instead let the caller pass in the read/write
881 opcode = READ_ONCE(sqe->opcode);
882 if (opcode == IORING_OP_READ_FIXED ||
883 opcode == IORING_OP_WRITE_FIXED) {
884 int ret = io_import_fixed(ctx, rw, sqe, iter);
894 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
898 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
902 * Make a note of the last file/offset/direction we punted to async
903 * context. We'll use this information to see if we can piggy back a
904 * sequential request onto the previous one, if it's still hasn't been
905 * completed by the async worker.
907 static void io_async_list_note(int rw, struct io_kiocb *req, size_t len)
909 struct async_list *async_list = &req->ctx->pending_async[rw];
910 struct kiocb *kiocb = &req->rw;
911 struct file *filp = kiocb->ki_filp;
912 off_t io_end = kiocb->ki_pos + len;
914 if (filp == async_list->file && kiocb->ki_pos == async_list->io_end) {
915 unsigned long max_pages;
917 /* Use 8x RA size as a decent limiter for both reads/writes */
918 max_pages = filp->f_ra.ra_pages;
920 max_pages = VM_READAHEAD_PAGES;
923 /* If max pages are exceeded, reset the state */
925 if (async_list->io_pages + len <= max_pages) {
926 req->flags |= REQ_F_SEQ_PREV;
927 async_list->io_pages += len;
930 async_list->io_pages = 0;
934 /* New file? Reset state. */
935 if (async_list->file != filp) {
936 async_list->io_pages = 0;
937 async_list->file = filp;
939 async_list->io_end = io_end;
942 static int io_read(struct io_kiocb *req, const struct sqe_submit *s,
943 bool force_nonblock, struct io_submit_state *state)
945 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
946 struct kiocb *kiocb = &req->rw;
947 struct iov_iter iter;
952 ret = io_prep_rw(req, s, force_nonblock, state);
955 file = kiocb->ki_filp;
957 if (unlikely(!(file->f_mode & FMODE_READ)))
959 if (unlikely(!file->f_op->read_iter))
962 ret = io_import_iovec(req->ctx, READ, s, &iovec, &iter);
966 iov_count = iov_iter_count(&iter);
967 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
971 /* Catch -EAGAIN return for forced non-blocking submission */
972 ret2 = call_read_iter(file, kiocb, &iter);
973 if (!force_nonblock || ret2 != -EAGAIN) {
974 io_rw_done(kiocb, ret2);
977 * If ->needs_lock is true, we're already in async
981 io_async_list_note(READ, req, iov_count);
989 static int io_write(struct io_kiocb *req, const struct sqe_submit *s,
990 bool force_nonblock, struct io_submit_state *state)
992 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
993 struct kiocb *kiocb = &req->rw;
994 struct iov_iter iter;
999 ret = io_prep_rw(req, s, force_nonblock, state);
1003 file = kiocb->ki_filp;
1004 if (unlikely(!(file->f_mode & FMODE_WRITE)))
1006 if (unlikely(!file->f_op->write_iter))
1009 ret = io_import_iovec(req->ctx, WRITE, s, &iovec, &iter);
1013 iov_count = iov_iter_count(&iter);
1016 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT)) {
1017 /* If ->needs_lock is true, we're already in async context. */
1019 io_async_list_note(WRITE, req, iov_count);
1023 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
1028 * Open-code file_start_write here to grab freeze protection,
1029 * which will be released by another thread in
1030 * io_complete_rw(). Fool lockdep by telling it the lock got
1031 * released so that it doesn't complain about the held lock when
1032 * we return to userspace.
1034 if (S_ISREG(file_inode(file)->i_mode)) {
1035 __sb_start_write(file_inode(file)->i_sb,
1036 SB_FREEZE_WRITE, true);
1037 __sb_writers_release(file_inode(file)->i_sb,
1040 kiocb->ki_flags |= IOCB_WRITE;
1042 ret2 = call_write_iter(file, kiocb, &iter);
1043 if (!force_nonblock || ret2 != -EAGAIN) {
1044 io_rw_done(kiocb, ret2);
1047 * If ->needs_lock is true, we're already in async
1051 io_async_list_note(WRITE, req, iov_count);
1061 * IORING_OP_NOP just posts a completion event, nothing else.
1063 static int io_nop(struct io_kiocb *req, u64 user_data)
1065 struct io_ring_ctx *ctx = req->ctx;
1068 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1071 io_cqring_add_event(ctx, user_data, err, 0);
1076 static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1078 struct io_ring_ctx *ctx = req->ctx;
1082 /* Prep already done (EAGAIN retry) */
1083 if (req->flags & REQ_F_PREPPED)
1086 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1088 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1091 req->flags |= REQ_F_PREPPED;
1095 static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1096 bool force_nonblock)
1098 loff_t sqe_off = READ_ONCE(sqe->off);
1099 loff_t sqe_len = READ_ONCE(sqe->len);
1100 loff_t end = sqe_off + sqe_len;
1101 unsigned fsync_flags;
1104 fsync_flags = READ_ONCE(sqe->fsync_flags);
1105 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1108 ret = io_prep_fsync(req, sqe);
1112 /* fsync always requires a blocking context */
1116 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1117 end > 0 ? end : LLONG_MAX,
1118 fsync_flags & IORING_FSYNC_DATASYNC);
1120 io_cqring_add_event(req->ctx, sqe->user_data, ret, 0);
1125 static void io_poll_remove_one(struct io_kiocb *req)
1127 struct io_poll_iocb *poll = &req->poll;
1129 spin_lock(&poll->head->lock);
1130 WRITE_ONCE(poll->canceled, true);
1131 if (!list_empty(&poll->wait.entry)) {
1132 list_del_init(&poll->wait.entry);
1133 queue_work(req->ctx->sqo_wq, &req->work);
1135 spin_unlock(&poll->head->lock);
1137 list_del_init(&req->list);
1140 static void io_poll_remove_all(struct io_ring_ctx *ctx)
1142 struct io_kiocb *req;
1144 spin_lock_irq(&ctx->completion_lock);
1145 while (!list_empty(&ctx->cancel_list)) {
1146 req = list_first_entry(&ctx->cancel_list, struct io_kiocb,list);
1147 io_poll_remove_one(req);
1149 spin_unlock_irq(&ctx->completion_lock);
1153 * Find a running poll command that matches one specified in sqe->addr,
1154 * and remove it if found.
1156 static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1158 struct io_ring_ctx *ctx = req->ctx;
1159 struct io_kiocb *poll_req, *next;
1162 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1164 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
1168 spin_lock_irq(&ctx->completion_lock);
1169 list_for_each_entry_safe(poll_req, next, &ctx->cancel_list, list) {
1170 if (READ_ONCE(sqe->addr) == poll_req->user_data) {
1171 io_poll_remove_one(poll_req);
1176 spin_unlock_irq(&ctx->completion_lock);
1178 io_cqring_add_event(req->ctx, sqe->user_data, ret, 0);
1183 static void io_poll_complete(struct io_ring_ctx *ctx, struct io_kiocb *req,
1186 req->poll.done = true;
1187 io_cqring_fill_event(ctx, req->user_data, mangle_poll(mask), 0);
1188 io_commit_cqring(ctx);
1191 static void io_poll_complete_work(struct work_struct *work)
1193 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
1194 struct io_poll_iocb *poll = &req->poll;
1195 struct poll_table_struct pt = { ._key = poll->events };
1196 struct io_ring_ctx *ctx = req->ctx;
1199 if (!READ_ONCE(poll->canceled))
1200 mask = vfs_poll(poll->file, &pt) & poll->events;
1203 * Note that ->ki_cancel callers also delete iocb from active_reqs after
1204 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
1205 * synchronize with them. In the cancellation case the list_del_init
1206 * itself is not actually needed, but harmless so we keep it in to
1207 * avoid further branches in the fast path.
1209 spin_lock_irq(&ctx->completion_lock);
1210 if (!mask && !READ_ONCE(poll->canceled)) {
1211 add_wait_queue(poll->head, &poll->wait);
1212 spin_unlock_irq(&ctx->completion_lock);
1215 list_del_init(&req->list);
1216 io_poll_complete(ctx, req, mask);
1217 spin_unlock_irq(&ctx->completion_lock);
1219 io_cqring_ev_posted(ctx);
1223 static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
1226 struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb,
1228 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
1229 struct io_ring_ctx *ctx = req->ctx;
1230 __poll_t mask = key_to_poll(key);
1231 unsigned long flags;
1233 /* for instances that support it check for an event match first: */
1234 if (mask && !(mask & poll->events))
1237 list_del_init(&poll->wait.entry);
1239 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
1240 list_del(&req->list);
1241 io_poll_complete(ctx, req, mask);
1242 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1244 io_cqring_ev_posted(ctx);
1247 queue_work(ctx->sqo_wq, &req->work);
1253 struct io_poll_table {
1254 struct poll_table_struct pt;
1255 struct io_kiocb *req;
1259 static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
1260 struct poll_table_struct *p)
1262 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
1264 if (unlikely(pt->req->poll.head)) {
1265 pt->error = -EINVAL;
1270 pt->req->poll.head = head;
1271 add_wait_queue(head, &pt->req->poll.wait);
1274 static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1276 struct io_poll_iocb *poll = &req->poll;
1277 struct io_ring_ctx *ctx = req->ctx;
1278 struct io_poll_table ipt;
1279 bool cancel = false;
1283 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1285 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
1290 INIT_WORK(&req->work, io_poll_complete_work);
1291 events = READ_ONCE(sqe->poll_events);
1292 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
1296 poll->canceled = false;
1298 ipt.pt._qproc = io_poll_queue_proc;
1299 ipt.pt._key = poll->events;
1301 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
1303 /* initialized the list so that we can do list_empty checks */
1304 INIT_LIST_HEAD(&poll->wait.entry);
1305 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
1307 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
1309 spin_lock_irq(&ctx->completion_lock);
1310 if (likely(poll->head)) {
1311 spin_lock(&poll->head->lock);
1312 if (unlikely(list_empty(&poll->wait.entry))) {
1318 if (mask || ipt.error)
1319 list_del_init(&poll->wait.entry);
1321 WRITE_ONCE(poll->canceled, true);
1322 else if (!poll->done) /* actually waiting for an event */
1323 list_add_tail(&req->list, &ctx->cancel_list);
1324 spin_unlock(&poll->head->lock);
1326 if (mask) { /* no async, we'd stolen it */
1327 req->error = mangle_poll(mask);
1329 io_poll_complete(ctx, req, mask);
1331 spin_unlock_irq(&ctx->completion_lock);
1334 io_cqring_ev_posted(ctx);
1340 static int __io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
1341 const struct sqe_submit *s, bool force_nonblock,
1342 struct io_submit_state *state)
1346 if (unlikely(s->index >= ctx->sq_entries))
1348 req->user_data = READ_ONCE(s->sqe->user_data);
1350 opcode = READ_ONCE(s->sqe->opcode);
1353 ret = io_nop(req, req->user_data);
1355 case IORING_OP_READV:
1356 if (unlikely(s->sqe->buf_index))
1358 ret = io_read(req, s, force_nonblock, state);
1360 case IORING_OP_WRITEV:
1361 if (unlikely(s->sqe->buf_index))
1363 ret = io_write(req, s, force_nonblock, state);
1365 case IORING_OP_READ_FIXED:
1366 ret = io_read(req, s, force_nonblock, state);
1368 case IORING_OP_WRITE_FIXED:
1369 ret = io_write(req, s, force_nonblock, state);
1371 case IORING_OP_FSYNC:
1372 ret = io_fsync(req, s->sqe, force_nonblock);
1374 case IORING_OP_POLL_ADD:
1375 ret = io_poll_add(req, s->sqe);
1377 case IORING_OP_POLL_REMOVE:
1378 ret = io_poll_remove(req, s->sqe);
1388 if (ctx->flags & IORING_SETUP_IOPOLL) {
1389 if (req->error == -EAGAIN)
1392 /* workqueue context doesn't hold uring_lock, grab it now */
1394 mutex_lock(&ctx->uring_lock);
1395 io_iopoll_req_issued(req);
1397 mutex_unlock(&ctx->uring_lock);
1403 static struct async_list *io_async_list_from_sqe(struct io_ring_ctx *ctx,
1404 const struct io_uring_sqe *sqe)
1406 switch (sqe->opcode) {
1407 case IORING_OP_READV:
1408 case IORING_OP_READ_FIXED:
1409 return &ctx->pending_async[READ];
1410 case IORING_OP_WRITEV:
1411 case IORING_OP_WRITE_FIXED:
1412 return &ctx->pending_async[WRITE];
1418 static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
1420 u8 opcode = READ_ONCE(sqe->opcode);
1422 return !(opcode == IORING_OP_READ_FIXED ||
1423 opcode == IORING_OP_WRITE_FIXED);
1426 static void io_sq_wq_submit_work(struct work_struct *work)
1428 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
1429 struct io_ring_ctx *ctx = req->ctx;
1430 struct mm_struct *cur_mm = NULL;
1431 struct async_list *async_list;
1432 LIST_HEAD(req_list);
1433 mm_segment_t old_fs;
1436 async_list = io_async_list_from_sqe(ctx, req->submit.sqe);
1439 struct sqe_submit *s = &req->submit;
1440 const struct io_uring_sqe *sqe = s->sqe;
1442 /* Ensure we clear previously set forced non-block flag */
1443 req->flags &= ~REQ_F_FORCE_NONBLOCK;
1444 req->rw.ki_flags &= ~IOCB_NOWAIT;
1447 if (io_sqe_needs_user(sqe) && !cur_mm) {
1448 if (!mmget_not_zero(ctx->sqo_mm)) {
1451 cur_mm = ctx->sqo_mm;
1459 s->has_user = cur_mm != NULL;
1460 s->needs_lock = true;
1462 ret = __io_submit_sqe(ctx, req, s, false, NULL);
1464 * We can get EAGAIN for polled IO even though
1465 * we're forcing a sync submission from here,
1466 * since we can't wait for request slots on the
1474 /* drop submission reference */
1478 io_cqring_add_event(ctx, sqe->user_data, ret, 0);
1482 /* async context always use a copy of the sqe */
1487 if (!list_empty(&req_list)) {
1488 req = list_first_entry(&req_list, struct io_kiocb,
1490 list_del(&req->list);
1493 if (list_empty(&async_list->list))
1497 spin_lock(&async_list->lock);
1498 if (list_empty(&async_list->list)) {
1499 spin_unlock(&async_list->lock);
1502 list_splice_init(&async_list->list, &req_list);
1503 spin_unlock(&async_list->lock);
1505 req = list_first_entry(&req_list, struct io_kiocb, list);
1506 list_del(&req->list);
1510 * Rare case of racing with a submitter. If we find the count has
1511 * dropped to zero AND we have pending work items, then restart
1512 * the processing. This is a tiny race window.
1515 ret = atomic_dec_return(&async_list->cnt);
1516 while (!ret && !list_empty(&async_list->list)) {
1517 spin_lock(&async_list->lock);
1518 atomic_inc(&async_list->cnt);
1519 list_splice_init(&async_list->list, &req_list);
1520 spin_unlock(&async_list->lock);
1522 if (!list_empty(&req_list)) {
1523 req = list_first_entry(&req_list,
1524 struct io_kiocb, list);
1525 list_del(&req->list);
1528 ret = atomic_dec_return(&async_list->cnt);
1540 * See if we can piggy back onto previously submitted work, that is still
1541 * running. We currently only allow this if the new request is sequential
1542 * to the previous one we punted.
1544 static bool io_add_to_prev_work(struct async_list *list, struct io_kiocb *req)
1550 if (!(req->flags & REQ_F_SEQ_PREV))
1552 if (!atomic_read(&list->cnt))
1556 spin_lock(&list->lock);
1557 list_add_tail(&req->list, &list->list);
1558 if (!atomic_read(&list->cnt)) {
1559 list_del_init(&req->list);
1562 spin_unlock(&list->lock);
1566 static bool io_op_needs_file(const struct io_uring_sqe *sqe)
1568 int op = READ_ONCE(sqe->opcode);
1572 case IORING_OP_POLL_REMOVE:
1579 static int io_req_set_file(struct io_ring_ctx *ctx, const struct sqe_submit *s,
1580 struct io_submit_state *state, struct io_kiocb *req)
1585 flags = READ_ONCE(s->sqe->flags);
1586 fd = READ_ONCE(s->sqe->fd);
1588 if (!io_op_needs_file(s->sqe)) {
1593 if (flags & IOSQE_FIXED_FILE) {
1594 if (unlikely(!ctx->user_files ||
1595 (unsigned) fd >= ctx->nr_user_files))
1597 req->file = ctx->user_files[fd];
1598 req->flags |= REQ_F_FIXED_FILE;
1600 if (s->needs_fixed_file)
1602 req->file = io_file_get(state, fd);
1603 if (unlikely(!req->file))
1610 static int io_submit_sqe(struct io_ring_ctx *ctx, struct sqe_submit *s,
1611 struct io_submit_state *state)
1613 struct io_kiocb *req;
1616 /* enforce forwards compatibility on users */
1617 if (unlikely(s->sqe->flags & ~IOSQE_FIXED_FILE))
1620 req = io_get_req(ctx, state);
1624 ret = io_req_set_file(ctx, s, state, req);
1628 ret = __io_submit_sqe(ctx, req, s, true, state);
1629 if (ret == -EAGAIN) {
1630 struct io_uring_sqe *sqe_copy;
1632 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
1634 struct async_list *list;
1636 memcpy(sqe_copy, s->sqe, sizeof(*sqe_copy));
1639 memcpy(&req->submit, s, sizeof(*s));
1640 list = io_async_list_from_sqe(ctx, s->sqe);
1641 if (!io_add_to_prev_work(list, req)) {
1643 atomic_inc(&list->cnt);
1644 INIT_WORK(&req->work, io_sq_wq_submit_work);
1645 queue_work(ctx->sqo_wq, &req->work);
1649 * Queued up for async execution, worker will release
1650 * submit reference when the iocb is actually
1658 /* drop submission reference */
1661 /* and drop final reference, if we failed */
1669 * Batched submission is done, ensure local IO is flushed out.
1671 static void io_submit_state_end(struct io_submit_state *state)
1673 blk_finish_plug(&state->plug);
1674 io_file_put(state, NULL);
1675 if (state->free_reqs)
1676 kmem_cache_free_bulk(req_cachep, state->free_reqs,
1677 &state->reqs[state->cur_req]);
1681 * Start submission side cache.
1683 static void io_submit_state_start(struct io_submit_state *state,
1684 struct io_ring_ctx *ctx, unsigned max_ios)
1686 blk_start_plug(&state->plug);
1687 state->free_reqs = 0;
1689 state->ios_left = max_ios;
1692 static void io_commit_sqring(struct io_ring_ctx *ctx)
1694 struct io_sq_ring *ring = ctx->sq_ring;
1696 if (ctx->cached_sq_head != READ_ONCE(ring->r.head)) {
1698 * Ensure any loads from the SQEs are done at this point,
1699 * since once we write the new head, the application could
1700 * write new data to them.
1702 smp_store_release(&ring->r.head, ctx->cached_sq_head);
1705 * write side barrier of head update, app has read side. See
1706 * comment at the top of this file
1713 * Undo last io_get_sqring()
1715 static void io_drop_sqring(struct io_ring_ctx *ctx)
1717 ctx->cached_sq_head--;
1721 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
1722 * that is mapped by userspace. This means that care needs to be taken to
1723 * ensure that reads are stable, as we cannot rely on userspace always
1724 * being a good citizen. If members of the sqe are validated and then later
1725 * used, it's important that those reads are done through READ_ONCE() to
1726 * prevent a re-load down the line.
1728 static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
1730 struct io_sq_ring *ring = ctx->sq_ring;
1734 * The cached sq head (or cq tail) serves two purposes:
1736 * 1) allows us to batch the cost of updating the user visible
1738 * 2) allows the kernel side to track the head on its own, even
1739 * though the application is the one updating it.
1741 head = ctx->cached_sq_head;
1742 /* See comment at the top of this file */
1744 if (head == READ_ONCE(ring->r.tail))
1747 head = READ_ONCE(ring->array[head & ctx->sq_mask]);
1748 if (head < ctx->sq_entries) {
1750 s->sqe = &ctx->sq_sqes[head];
1751 ctx->cached_sq_head++;
1755 /* drop invalid entries */
1756 ctx->cached_sq_head++;
1758 /* See comment at the top of this file */
1763 static int io_submit_sqes(struct io_ring_ctx *ctx, struct sqe_submit *sqes,
1764 unsigned int nr, bool has_user, bool mm_fault)
1766 struct io_submit_state state, *statep = NULL;
1767 int ret, i, submitted = 0;
1769 if (nr > IO_PLUG_THRESHOLD) {
1770 io_submit_state_start(&state, ctx, nr);
1774 for (i = 0; i < nr; i++) {
1775 if (unlikely(mm_fault)) {
1778 sqes[i].has_user = has_user;
1779 sqes[i].needs_lock = true;
1780 sqes[i].needs_fixed_file = true;
1781 ret = io_submit_sqe(ctx, &sqes[i], statep);
1788 io_cqring_add_event(ctx, sqes[i].sqe->user_data, ret, 0);
1792 io_submit_state_end(&state);
1797 static int io_sq_thread(void *data)
1799 struct sqe_submit sqes[IO_IOPOLL_BATCH];
1800 struct io_ring_ctx *ctx = data;
1801 struct mm_struct *cur_mm = NULL;
1802 mm_segment_t old_fs;
1805 unsigned long timeout;
1810 timeout = inflight = 0;
1811 while (!kthread_should_stop() && !ctx->sqo_stop) {
1812 bool all_fixed, mm_fault = false;
1816 unsigned nr_events = 0;
1818 if (ctx->flags & IORING_SETUP_IOPOLL) {
1820 * We disallow the app entering submit/complete
1821 * with polling, but we still need to lock the
1822 * ring to prevent racing with polled issue
1823 * that got punted to a workqueue.
1825 mutex_lock(&ctx->uring_lock);
1826 io_iopoll_check(ctx, &nr_events, 0);
1827 mutex_unlock(&ctx->uring_lock);
1830 * Normal IO, just pretend everything completed.
1831 * We don't have to poll completions for that.
1833 nr_events = inflight;
1836 inflight -= nr_events;
1838 timeout = jiffies + ctx->sq_thread_idle;
1841 if (!io_get_sqring(ctx, &sqes[0])) {
1843 * We're polling. If we're within the defined idle
1844 * period, then let us spin without work before going
1847 if (inflight || !time_after(jiffies, timeout)) {
1853 * Drop cur_mm before scheduling, we can't hold it for
1854 * long periods (or over schedule()). Do this before
1855 * adding ourselves to the waitqueue, as the unuse/drop
1864 prepare_to_wait(&ctx->sqo_wait, &wait,
1865 TASK_INTERRUPTIBLE);
1867 /* Tell userspace we may need a wakeup call */
1868 ctx->sq_ring->flags |= IORING_SQ_NEED_WAKEUP;
1871 if (!io_get_sqring(ctx, &sqes[0])) {
1872 if (kthread_should_stop()) {
1873 finish_wait(&ctx->sqo_wait, &wait);
1876 if (signal_pending(current))
1877 flush_signals(current);
1879 finish_wait(&ctx->sqo_wait, &wait);
1881 ctx->sq_ring->flags &= ~IORING_SQ_NEED_WAKEUP;
1885 finish_wait(&ctx->sqo_wait, &wait);
1887 ctx->sq_ring->flags &= ~IORING_SQ_NEED_WAKEUP;
1894 if (all_fixed && io_sqe_needs_user(sqes[i].sqe))
1898 if (i == ARRAY_SIZE(sqes))
1900 } while (io_get_sqring(ctx, &sqes[i]));
1902 /* Unless all new commands are FIXED regions, grab mm */
1903 if (!all_fixed && !cur_mm) {
1904 mm_fault = !mmget_not_zero(ctx->sqo_mm);
1906 use_mm(ctx->sqo_mm);
1907 cur_mm = ctx->sqo_mm;
1911 inflight += io_submit_sqes(ctx, sqes, i, cur_mm != NULL,
1914 /* Commit SQ ring head once we've consumed all SQEs */
1915 io_commit_sqring(ctx);
1926 static int io_ring_submit(struct io_ring_ctx *ctx, unsigned int to_submit)
1928 struct io_submit_state state, *statep = NULL;
1929 int i, ret = 0, submit = 0;
1931 if (to_submit > IO_PLUG_THRESHOLD) {
1932 io_submit_state_start(&state, ctx, to_submit);
1936 for (i = 0; i < to_submit; i++) {
1937 struct sqe_submit s;
1939 if (!io_get_sqring(ctx, &s))
1943 s.needs_lock = false;
1944 s.needs_fixed_file = false;
1946 ret = io_submit_sqe(ctx, &s, statep);
1948 io_drop_sqring(ctx);
1954 io_commit_sqring(ctx);
1957 io_submit_state_end(statep);
1959 return submit ? submit : ret;
1962 static unsigned io_cqring_events(struct io_cq_ring *ring)
1964 return READ_ONCE(ring->r.tail) - READ_ONCE(ring->r.head);
1968 * Wait until events become available, if we don't already have some. The
1969 * application must reap them itself, as they reside on the shared cq ring.
1971 static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
1972 const sigset_t __user *sig, size_t sigsz)
1974 struct io_cq_ring *ring = ctx->cq_ring;
1975 sigset_t ksigmask, sigsaved;
1979 /* See comment at the top of this file */
1981 if (io_cqring_events(ring) >= min_events)
1985 #ifdef CONFIG_COMPAT
1986 if (in_compat_syscall())
1987 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
1988 &ksigmask, &sigsaved, sigsz);
1991 ret = set_user_sigmask(sig, &ksigmask,
1999 prepare_to_wait(&ctx->wait, &wait, TASK_INTERRUPTIBLE);
2002 /* See comment at the top of this file */
2004 if (io_cqring_events(ring) >= min_events)
2010 if (signal_pending(current))
2014 finish_wait(&ctx->wait, &wait);
2017 restore_user_sigmask(sig, &sigsaved);
2019 return READ_ONCE(ring->r.head) == READ_ONCE(ring->r.tail) ? ret : 0;
2022 static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
2024 #if defined(CONFIG_UNIX)
2025 if (ctx->ring_sock) {
2026 struct sock *sock = ctx->ring_sock->sk;
2027 struct sk_buff *skb;
2029 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
2035 for (i = 0; i < ctx->nr_user_files; i++)
2036 fput(ctx->user_files[i]);
2040 static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
2042 if (!ctx->user_files)
2045 __io_sqe_files_unregister(ctx);
2046 kfree(ctx->user_files);
2047 ctx->user_files = NULL;
2048 ctx->nr_user_files = 0;
2052 static void io_sq_thread_stop(struct io_ring_ctx *ctx)
2054 if (ctx->sqo_thread) {
2057 kthread_stop(ctx->sqo_thread);
2058 ctx->sqo_thread = NULL;
2062 static void io_finish_async(struct io_ring_ctx *ctx)
2064 io_sq_thread_stop(ctx);
2067 destroy_workqueue(ctx->sqo_wq);
2072 #if defined(CONFIG_UNIX)
2073 static void io_destruct_skb(struct sk_buff *skb)
2075 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
2077 io_finish_async(ctx);
2078 unix_destruct_scm(skb);
2082 * Ensure the UNIX gc is aware of our file set, so we are certain that
2083 * the io_uring can be safely unregistered on process exit, even if we have
2084 * loops in the file referencing.
2086 static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
2088 struct sock *sk = ctx->ring_sock->sk;
2089 struct scm_fp_list *fpl;
2090 struct sk_buff *skb;
2093 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
2094 unsigned long inflight = ctx->user->unix_inflight + nr;
2096 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
2100 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
2104 skb = alloc_skb(0, GFP_KERNEL);
2111 skb->destructor = io_destruct_skb;
2113 fpl->user = get_uid(ctx->user);
2114 for (i = 0; i < nr; i++) {
2115 fpl->fp[i] = get_file(ctx->user_files[i + offset]);
2116 unix_inflight(fpl->user, fpl->fp[i]);
2119 fpl->max = fpl->count = nr;
2120 UNIXCB(skb).fp = fpl;
2121 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
2122 skb_queue_head(&sk->sk_receive_queue, skb);
2124 for (i = 0; i < nr; i++)
2131 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
2132 * causes regular reference counting to break down. We rely on the UNIX
2133 * garbage collection to take care of this problem for us.
2135 static int io_sqe_files_scm(struct io_ring_ctx *ctx)
2137 unsigned left, total;
2141 left = ctx->nr_user_files;
2143 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
2146 ret = __io_sqe_files_scm(ctx, this_files, total);
2150 total += this_files;
2156 while (total < ctx->nr_user_files) {
2157 fput(ctx->user_files[total]);
2164 static int io_sqe_files_scm(struct io_ring_ctx *ctx)
2170 static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
2173 __s32 __user *fds = (__s32 __user *) arg;
2177 if (ctx->user_files)
2181 if (nr_args > IORING_MAX_FIXED_FILES)
2184 ctx->user_files = kcalloc(nr_args, sizeof(struct file *), GFP_KERNEL);
2185 if (!ctx->user_files)
2188 for (i = 0; i < nr_args; i++) {
2190 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
2193 ctx->user_files[i] = fget(fd);
2196 if (!ctx->user_files[i])
2199 * Don't allow io_uring instances to be registered. If UNIX
2200 * isn't enabled, then this causes a reference cycle and this
2201 * instance can never get freed. If UNIX is enabled we'll
2202 * handle it just fine, but there's still no point in allowing
2203 * a ring fd as it doesn't support regular read/write anyway.
2205 if (ctx->user_files[i]->f_op == &io_uring_fops) {
2206 fput(ctx->user_files[i]);
2209 ctx->nr_user_files++;
2214 for (i = 0; i < ctx->nr_user_files; i++)
2215 fput(ctx->user_files[i]);
2217 kfree(ctx->user_files);
2218 ctx->user_files = NULL;
2219 ctx->nr_user_files = 0;
2223 ret = io_sqe_files_scm(ctx);
2225 io_sqe_files_unregister(ctx);
2230 static int io_sq_offload_start(struct io_ring_ctx *ctx,
2231 struct io_uring_params *p)
2235 init_waitqueue_head(&ctx->sqo_wait);
2236 mmgrab(current->mm);
2237 ctx->sqo_mm = current->mm;
2239 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
2240 if (!ctx->sq_thread_idle)
2241 ctx->sq_thread_idle = HZ;
2244 if (!cpu_possible(p->sq_thread_cpu))
2247 if (ctx->flags & IORING_SETUP_SQPOLL) {
2249 if (!capable(CAP_SYS_ADMIN))
2252 if (p->flags & IORING_SETUP_SQ_AFF) {
2255 cpu = array_index_nospec(p->sq_thread_cpu, NR_CPUS);
2256 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
2260 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
2263 if (IS_ERR(ctx->sqo_thread)) {
2264 ret = PTR_ERR(ctx->sqo_thread);
2265 ctx->sqo_thread = NULL;
2268 wake_up_process(ctx->sqo_thread);
2269 } else if (p->flags & IORING_SETUP_SQ_AFF) {
2270 /* Can't have SQ_AFF without SQPOLL */
2275 /* Do QD, or 2 * CPUS, whatever is smallest */
2276 ctx->sqo_wq = alloc_workqueue("io_ring-wq", WQ_UNBOUND | WQ_FREEZABLE,
2277 min(ctx->sq_entries - 1, 2 * num_online_cpus()));
2285 io_sq_thread_stop(ctx);
2286 mmdrop(ctx->sqo_mm);
2291 static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
2293 atomic_long_sub(nr_pages, &user->locked_vm);
2296 static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
2298 unsigned long page_limit, cur_pages, new_pages;
2300 /* Don't allow more pages than we can safely lock */
2301 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
2304 cur_pages = atomic_long_read(&user->locked_vm);
2305 new_pages = cur_pages + nr_pages;
2306 if (new_pages > page_limit)
2308 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
2309 new_pages) != cur_pages);
2314 static void io_mem_free(void *ptr)
2316 struct page *page = virt_to_head_page(ptr);
2318 if (put_page_testzero(page))
2319 free_compound_page(page);
2322 static void *io_mem_alloc(size_t size)
2324 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
2327 return (void *) __get_free_pages(gfp_flags, get_order(size));
2330 static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
2332 struct io_sq_ring *sq_ring;
2333 struct io_cq_ring *cq_ring;
2336 bytes = struct_size(sq_ring, array, sq_entries);
2337 bytes += array_size(sizeof(struct io_uring_sqe), sq_entries);
2338 bytes += struct_size(cq_ring, cqes, cq_entries);
2340 return (bytes + PAGE_SIZE - 1) / PAGE_SIZE;
2343 static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
2347 if (!ctx->user_bufs)
2350 for (i = 0; i < ctx->nr_user_bufs; i++) {
2351 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
2353 for (j = 0; j < imu->nr_bvecs; j++)
2354 put_page(imu->bvec[j].bv_page);
2356 if (ctx->account_mem)
2357 io_unaccount_mem(ctx->user, imu->nr_bvecs);
2362 kfree(ctx->user_bufs);
2363 ctx->user_bufs = NULL;
2364 ctx->nr_user_bufs = 0;
2368 static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
2369 void __user *arg, unsigned index)
2371 struct iovec __user *src;
2373 #ifdef CONFIG_COMPAT
2375 struct compat_iovec __user *ciovs;
2376 struct compat_iovec ciov;
2378 ciovs = (struct compat_iovec __user *) arg;
2379 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
2382 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
2383 dst->iov_len = ciov.iov_len;
2387 src = (struct iovec __user *) arg;
2388 if (copy_from_user(dst, &src[index], sizeof(*dst)))
2393 static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
2396 struct vm_area_struct **vmas = NULL;
2397 struct page **pages = NULL;
2398 int i, j, got_pages = 0;
2403 if (!nr_args || nr_args > UIO_MAXIOV)
2406 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
2408 if (!ctx->user_bufs)
2411 for (i = 0; i < nr_args; i++) {
2412 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
2413 unsigned long off, start, end, ubuf;
2418 ret = io_copy_iov(ctx, &iov, arg, i);
2423 * Don't impose further limits on the size and buffer
2424 * constraints here, we'll -EINVAL later when IO is
2425 * submitted if they are wrong.
2428 if (!iov.iov_base || !iov.iov_len)
2431 /* arbitrary limit, but we need something */
2432 if (iov.iov_len > SZ_1G)
2435 ubuf = (unsigned long) iov.iov_base;
2436 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
2437 start = ubuf >> PAGE_SHIFT;
2438 nr_pages = end - start;
2440 if (ctx->account_mem) {
2441 ret = io_account_mem(ctx->user, nr_pages);
2447 if (!pages || nr_pages > got_pages) {
2450 pages = kmalloc_array(nr_pages, sizeof(struct page *),
2452 vmas = kmalloc_array(nr_pages,
2453 sizeof(struct vm_area_struct *),
2455 if (!pages || !vmas) {
2457 if (ctx->account_mem)
2458 io_unaccount_mem(ctx->user, nr_pages);
2461 got_pages = nr_pages;
2464 imu->bvec = kmalloc_array(nr_pages, sizeof(struct bio_vec),
2468 if (ctx->account_mem)
2469 io_unaccount_mem(ctx->user, nr_pages);
2474 down_read(¤t->mm->mmap_sem);
2475 pret = get_user_pages_longterm(ubuf, nr_pages, FOLL_WRITE,
2477 if (pret == nr_pages) {
2478 /* don't support file backed memory */
2479 for (j = 0; j < nr_pages; j++) {
2480 struct vm_area_struct *vma = vmas[j];
2483 !is_file_hugepages(vma->vm_file)) {
2489 ret = pret < 0 ? pret : -EFAULT;
2491 up_read(¤t->mm->mmap_sem);
2494 * if we did partial map, or found file backed vmas,
2495 * release any pages we did get
2498 for (j = 0; j < pret; j++)
2501 if (ctx->account_mem)
2502 io_unaccount_mem(ctx->user, nr_pages);
2506 off = ubuf & ~PAGE_MASK;
2508 for (j = 0; j < nr_pages; j++) {
2511 vec_len = min_t(size_t, size, PAGE_SIZE - off);
2512 imu->bvec[j].bv_page = pages[j];
2513 imu->bvec[j].bv_len = vec_len;
2514 imu->bvec[j].bv_offset = off;
2518 /* store original address for later verification */
2520 imu->len = iov.iov_len;
2521 imu->nr_bvecs = nr_pages;
2523 ctx->nr_user_bufs++;
2531 io_sqe_buffer_unregister(ctx);
2535 static void io_ring_ctx_free(struct io_ring_ctx *ctx)
2537 io_finish_async(ctx);
2539 mmdrop(ctx->sqo_mm);
2541 io_iopoll_reap_events(ctx);
2542 io_sqe_buffer_unregister(ctx);
2543 io_sqe_files_unregister(ctx);
2545 #if defined(CONFIG_UNIX)
2547 sock_release(ctx->ring_sock);
2550 io_mem_free(ctx->sq_ring);
2551 io_mem_free(ctx->sq_sqes);
2552 io_mem_free(ctx->cq_ring);
2554 percpu_ref_exit(&ctx->refs);
2555 if (ctx->account_mem)
2556 io_unaccount_mem(ctx->user,
2557 ring_pages(ctx->sq_entries, ctx->cq_entries));
2558 free_uid(ctx->user);
2562 static __poll_t io_uring_poll(struct file *file, poll_table *wait)
2564 struct io_ring_ctx *ctx = file->private_data;
2567 poll_wait(file, &ctx->cq_wait, wait);
2568 /* See comment at the top of this file */
2570 if (READ_ONCE(ctx->sq_ring->r.tail) + 1 != ctx->cached_sq_head)
2571 mask |= EPOLLOUT | EPOLLWRNORM;
2572 if (READ_ONCE(ctx->cq_ring->r.head) != ctx->cached_cq_tail)
2573 mask |= EPOLLIN | EPOLLRDNORM;
2578 static int io_uring_fasync(int fd, struct file *file, int on)
2580 struct io_ring_ctx *ctx = file->private_data;
2582 return fasync_helper(fd, file, on, &ctx->cq_fasync);
2585 static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
2587 mutex_lock(&ctx->uring_lock);
2588 percpu_ref_kill(&ctx->refs);
2589 mutex_unlock(&ctx->uring_lock);
2591 io_poll_remove_all(ctx);
2592 io_iopoll_reap_events(ctx);
2593 wait_for_completion(&ctx->ctx_done);
2594 io_ring_ctx_free(ctx);
2597 static int io_uring_release(struct inode *inode, struct file *file)
2599 struct io_ring_ctx *ctx = file->private_data;
2601 file->private_data = NULL;
2602 io_ring_ctx_wait_and_kill(ctx);
2606 static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
2608 loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT;
2609 unsigned long sz = vma->vm_end - vma->vm_start;
2610 struct io_ring_ctx *ctx = file->private_data;
2616 case IORING_OFF_SQ_RING:
2619 case IORING_OFF_SQES:
2622 case IORING_OFF_CQ_RING:
2629 page = virt_to_head_page(ptr);
2630 if (sz > (PAGE_SIZE << compound_order(page)))
2633 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
2634 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
2637 SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
2638 u32, min_complete, u32, flags, const sigset_t __user *, sig,
2641 struct io_ring_ctx *ctx;
2646 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
2654 if (f.file->f_op != &io_uring_fops)
2658 ctx = f.file->private_data;
2659 if (!percpu_ref_tryget(&ctx->refs))
2663 * For SQ polling, the thread will do all submissions and completions.
2664 * Just return the requested submit count, and wake the thread if
2667 if (ctx->flags & IORING_SETUP_SQPOLL) {
2668 if (flags & IORING_ENTER_SQ_WAKEUP)
2669 wake_up(&ctx->sqo_wait);
2670 submitted = to_submit;
2676 to_submit = min(to_submit, ctx->sq_entries);
2678 mutex_lock(&ctx->uring_lock);
2679 submitted = io_ring_submit(ctx, to_submit);
2680 mutex_unlock(&ctx->uring_lock);
2685 if (flags & IORING_ENTER_GETEVENTS) {
2686 unsigned nr_events = 0;
2688 min_complete = min(min_complete, ctx->cq_entries);
2691 * The application could have included the 'to_submit' count
2692 * in how many events it wanted to wait for. If we failed to
2693 * submit the desired count, we may need to adjust the number
2694 * of events to poll/wait for.
2696 if (submitted < to_submit)
2697 min_complete = min_t(unsigned, submitted, min_complete);
2699 if (ctx->flags & IORING_SETUP_IOPOLL) {
2700 mutex_lock(&ctx->uring_lock);
2701 ret = io_iopoll_check(ctx, &nr_events, min_complete);
2702 mutex_unlock(&ctx->uring_lock);
2704 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
2709 io_ring_drop_ctx_refs(ctx, 1);
2712 return submitted ? submitted : ret;
2715 static const struct file_operations io_uring_fops = {
2716 .release = io_uring_release,
2717 .mmap = io_uring_mmap,
2718 .poll = io_uring_poll,
2719 .fasync = io_uring_fasync,
2722 static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
2723 struct io_uring_params *p)
2725 struct io_sq_ring *sq_ring;
2726 struct io_cq_ring *cq_ring;
2729 sq_ring = io_mem_alloc(struct_size(sq_ring, array, p->sq_entries));
2733 ctx->sq_ring = sq_ring;
2734 sq_ring->ring_mask = p->sq_entries - 1;
2735 sq_ring->ring_entries = p->sq_entries;
2736 ctx->sq_mask = sq_ring->ring_mask;
2737 ctx->sq_entries = sq_ring->ring_entries;
2739 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
2740 if (size == SIZE_MAX)
2743 ctx->sq_sqes = io_mem_alloc(size);
2744 if (!ctx->sq_sqes) {
2745 io_mem_free(ctx->sq_ring);
2749 cq_ring = io_mem_alloc(struct_size(cq_ring, cqes, p->cq_entries));
2751 io_mem_free(ctx->sq_ring);
2752 io_mem_free(ctx->sq_sqes);
2756 ctx->cq_ring = cq_ring;
2757 cq_ring->ring_mask = p->cq_entries - 1;
2758 cq_ring->ring_entries = p->cq_entries;
2759 ctx->cq_mask = cq_ring->ring_mask;
2760 ctx->cq_entries = cq_ring->ring_entries;
2765 * Allocate an anonymous fd, this is what constitutes the application
2766 * visible backing of an io_uring instance. The application mmaps this
2767 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
2768 * we have to tie this fd to a socket for file garbage collection purposes.
2770 static int io_uring_get_fd(struct io_ring_ctx *ctx)
2775 #if defined(CONFIG_UNIX)
2776 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
2782 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
2786 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
2787 O_RDWR | O_CLOEXEC);
2790 ret = PTR_ERR(file);
2794 #if defined(CONFIG_UNIX)
2795 ctx->ring_sock->file = file;
2796 ctx->ring_sock->sk->sk_user_data = ctx;
2798 fd_install(ret, file);
2801 #if defined(CONFIG_UNIX)
2802 sock_release(ctx->ring_sock);
2803 ctx->ring_sock = NULL;
2808 static int io_uring_create(unsigned entries, struct io_uring_params *p)
2810 struct user_struct *user = NULL;
2811 struct io_ring_ctx *ctx;
2815 if (!entries || entries > IORING_MAX_ENTRIES)
2819 * Use twice as many entries for the CQ ring. It's possible for the
2820 * application to drive a higher depth than the size of the SQ ring,
2821 * since the sqes are only used at submission time. This allows for
2822 * some flexibility in overcommitting a bit.
2824 p->sq_entries = roundup_pow_of_two(entries);
2825 p->cq_entries = 2 * p->sq_entries;
2827 user = get_uid(current_user());
2828 account_mem = !capable(CAP_IPC_LOCK);
2831 ret = io_account_mem(user,
2832 ring_pages(p->sq_entries, p->cq_entries));
2839 ctx = io_ring_ctx_alloc(p);
2842 io_unaccount_mem(user, ring_pages(p->sq_entries,
2847 ctx->compat = in_compat_syscall();
2848 ctx->account_mem = account_mem;
2851 ret = io_allocate_scq_urings(ctx, p);
2855 ret = io_sq_offload_start(ctx, p);
2859 ret = io_uring_get_fd(ctx);
2863 memset(&p->sq_off, 0, sizeof(p->sq_off));
2864 p->sq_off.head = offsetof(struct io_sq_ring, r.head);
2865 p->sq_off.tail = offsetof(struct io_sq_ring, r.tail);
2866 p->sq_off.ring_mask = offsetof(struct io_sq_ring, ring_mask);
2867 p->sq_off.ring_entries = offsetof(struct io_sq_ring, ring_entries);
2868 p->sq_off.flags = offsetof(struct io_sq_ring, flags);
2869 p->sq_off.dropped = offsetof(struct io_sq_ring, dropped);
2870 p->sq_off.array = offsetof(struct io_sq_ring, array);
2872 memset(&p->cq_off, 0, sizeof(p->cq_off));
2873 p->cq_off.head = offsetof(struct io_cq_ring, r.head);
2874 p->cq_off.tail = offsetof(struct io_cq_ring, r.tail);
2875 p->cq_off.ring_mask = offsetof(struct io_cq_ring, ring_mask);
2876 p->cq_off.ring_entries = offsetof(struct io_cq_ring, ring_entries);
2877 p->cq_off.overflow = offsetof(struct io_cq_ring, overflow);
2878 p->cq_off.cqes = offsetof(struct io_cq_ring, cqes);
2881 io_ring_ctx_wait_and_kill(ctx);
2886 * Sets up an aio uring context, and returns the fd. Applications asks for a
2887 * ring size, we return the actual sq/cq ring sizes (among other things) in the
2888 * params structure passed in.
2890 static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
2892 struct io_uring_params p;
2896 if (copy_from_user(&p, params, sizeof(p)))
2898 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
2903 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
2904 IORING_SETUP_SQ_AFF))
2907 ret = io_uring_create(entries, &p);
2911 if (copy_to_user(params, &p, sizeof(p)))
2917 SYSCALL_DEFINE2(io_uring_setup, u32, entries,
2918 struct io_uring_params __user *, params)
2920 return io_uring_setup(entries, params);
2923 static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
2924 void __user *arg, unsigned nr_args)
2928 percpu_ref_kill(&ctx->refs);
2929 wait_for_completion(&ctx->ctx_done);
2932 case IORING_REGISTER_BUFFERS:
2933 ret = io_sqe_buffer_register(ctx, arg, nr_args);
2935 case IORING_UNREGISTER_BUFFERS:
2939 ret = io_sqe_buffer_unregister(ctx);
2941 case IORING_REGISTER_FILES:
2942 ret = io_sqe_files_register(ctx, arg, nr_args);
2944 case IORING_UNREGISTER_FILES:
2948 ret = io_sqe_files_unregister(ctx);
2955 /* bring the ctx back to life */
2956 reinit_completion(&ctx->ctx_done);
2957 percpu_ref_reinit(&ctx->refs);
2961 SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
2962 void __user *, arg, unsigned int, nr_args)
2964 struct io_ring_ctx *ctx;
2973 if (f.file->f_op != &io_uring_fops)
2976 ctx = f.file->private_data;
2978 mutex_lock(&ctx->uring_lock);
2979 ret = __io_uring_register(ctx, opcode, arg, nr_args);
2980 mutex_unlock(&ctx->uring_lock);
2986 static int __init io_uring_init(void)
2988 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
2991 __initcall(io_uring_init);