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_add_event(struct io_ring_ctx *ctx, u64 ki_user_data,
371 long res, unsigned ev_flags)
375 spin_lock_irqsave(&ctx->completion_lock, flags);
376 io_cqring_fill_event(ctx, ki_user_data, res, ev_flags);
377 io_commit_cqring(ctx);
378 spin_unlock_irqrestore(&ctx->completion_lock, flags);
380 if (waitqueue_active(&ctx->wait))
382 if (waitqueue_active(&ctx->sqo_wait))
383 wake_up(&ctx->sqo_wait);
386 static void io_ring_drop_ctx_refs(struct io_ring_ctx *ctx, unsigned refs)
388 percpu_ref_put_many(&ctx->refs, refs);
390 if (waitqueue_active(&ctx->wait))
394 static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
395 struct io_submit_state *state)
397 struct io_kiocb *req;
399 if (!percpu_ref_tryget(&ctx->refs))
403 req = kmem_cache_alloc(req_cachep, __GFP_NOWARN);
406 } else if (!state->free_reqs) {
410 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
411 ret = kmem_cache_alloc_bulk(req_cachep, __GFP_NOWARN, sz,
413 if (unlikely(ret <= 0))
415 state->free_reqs = ret - 1;
417 req = state->reqs[0];
419 req = state->reqs[state->cur_req];
426 /* one is dropped after submission, the other at completion */
427 refcount_set(&req->refs, 2);
430 io_ring_drop_ctx_refs(ctx, 1);
434 static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
437 kmem_cache_free_bulk(req_cachep, *nr, reqs);
438 io_ring_drop_ctx_refs(ctx, *nr);
443 static void io_free_req(struct io_kiocb *req)
445 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
447 io_ring_drop_ctx_refs(req->ctx, 1);
448 kmem_cache_free(req_cachep, req);
451 static void io_put_req(struct io_kiocb *req)
453 if (refcount_dec_and_test(&req->refs))
458 * Find and free completed poll iocbs
460 static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
461 struct list_head *done)
463 void *reqs[IO_IOPOLL_BATCH];
464 struct io_kiocb *req;
468 while (!list_empty(done)) {
469 req = list_first_entry(done, struct io_kiocb, list);
470 list_del(&req->list);
472 io_cqring_fill_event(ctx, req->user_data, req->error, 0);
475 if (refcount_dec_and_test(&req->refs)) {
476 /* If we're not using fixed files, we have to pair the
477 * completion part with the file put. Use regular
478 * completions for those, only batch free for fixed
481 if (req->flags & REQ_F_FIXED_FILE) {
482 reqs[to_free++] = req;
483 if (to_free == ARRAY_SIZE(reqs))
484 io_free_req_many(ctx, reqs, &to_free);
491 io_commit_cqring(ctx);
492 io_free_req_many(ctx, reqs, &to_free);
495 static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
498 struct io_kiocb *req, *tmp;
504 * Only spin for completions if we don't have multiple devices hanging
505 * off our complete list, and we're under the requested amount.
507 spin = !ctx->poll_multi_file && *nr_events < min;
510 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
511 struct kiocb *kiocb = &req->rw;
514 * Move completed entries to our local list. If we find a
515 * request that requires polling, break out and complete
516 * the done list first, if we have entries there.
518 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
519 list_move_tail(&req->list, &done);
522 if (!list_empty(&done))
525 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
534 if (!list_empty(&done))
535 io_iopoll_complete(ctx, nr_events, &done);
541 * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a
542 * non-spinning poll check - we'll still enter the driver poll loop, but only
543 * as a non-spinning completion check.
545 static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
548 while (!list_empty(&ctx->poll_list)) {
551 ret = io_do_iopoll(ctx, nr_events, min);
554 if (!min || *nr_events >= min)
562 * We can't just wait for polled events to come to us, we have to actively
563 * find and complete them.
565 static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
567 if (!(ctx->flags & IORING_SETUP_IOPOLL))
570 mutex_lock(&ctx->uring_lock);
571 while (!list_empty(&ctx->poll_list)) {
572 unsigned int nr_events = 0;
574 io_iopoll_getevents(ctx, &nr_events, 1);
576 mutex_unlock(&ctx->uring_lock);
579 static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
587 if (*nr_events < min)
588 tmin = min - *nr_events;
590 ret = io_iopoll_getevents(ctx, nr_events, tmin);
594 } while (min && !*nr_events && !need_resched());
599 static void kiocb_end_write(struct kiocb *kiocb)
601 if (kiocb->ki_flags & IOCB_WRITE) {
602 struct inode *inode = file_inode(kiocb->ki_filp);
605 * Tell lockdep we inherited freeze protection from submission
608 if (S_ISREG(inode->i_mode))
609 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
610 file_end_write(kiocb->ki_filp);
614 static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
616 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
618 kiocb_end_write(kiocb);
620 io_cqring_add_event(req->ctx, req->user_data, res, 0);
624 static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
626 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
628 kiocb_end_write(kiocb);
632 req->flags |= REQ_F_IOPOLL_COMPLETED;
636 * After the iocb has been issued, it's safe to be found on the poll list.
637 * Adding the kiocb to the list AFTER submission ensures that we don't
638 * find it from a io_iopoll_getevents() thread before the issuer is done
639 * accessing the kiocb cookie.
641 static void io_iopoll_req_issued(struct io_kiocb *req)
643 struct io_ring_ctx *ctx = req->ctx;
646 * Track whether we have multiple files in our lists. This will impact
647 * how we do polling eventually, not spinning if we're on potentially
650 if (list_empty(&ctx->poll_list)) {
651 ctx->poll_multi_file = false;
652 } else if (!ctx->poll_multi_file) {
653 struct io_kiocb *list_req;
655 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
657 if (list_req->rw.ki_filp != req->rw.ki_filp)
658 ctx->poll_multi_file = true;
662 * For fast devices, IO may have already completed. If it has, add
663 * it to the front so we find it first.
665 if (req->flags & REQ_F_IOPOLL_COMPLETED)
666 list_add(&req->list, &ctx->poll_list);
668 list_add_tail(&req->list, &ctx->poll_list);
671 static void io_file_put(struct io_submit_state *state, struct file *file)
675 } else if (state->file) {
676 int diff = state->has_refs - state->used_refs;
679 fput_many(state->file, diff);
685 * Get as many references to a file as we have IOs left in this submission,
686 * assuming most submissions are for one file, or at least that each file
687 * has more than one submission.
689 static struct file *io_file_get(struct io_submit_state *state, int fd)
695 if (state->fd == fd) {
700 io_file_put(state, NULL);
702 state->file = fget_many(fd, state->ios_left);
707 state->has_refs = state->ios_left;
708 state->used_refs = 1;
714 * If we tracked the file through the SCM inflight mechanism, we could support
715 * any file. For now, just ensure that anything potentially problematic is done
718 static bool io_file_supports_async(struct file *file)
720 umode_t mode = file_inode(file)->i_mode;
722 if (S_ISBLK(mode) || S_ISCHR(mode))
724 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
730 static int io_prep_rw(struct io_kiocb *req, const struct sqe_submit *s,
731 bool force_nonblock, struct io_submit_state *state)
733 const struct io_uring_sqe *sqe = s->sqe;
734 struct io_ring_ctx *ctx = req->ctx;
735 struct kiocb *kiocb = &req->rw;
741 /* For -EAGAIN retry, everything is already prepped */
742 if (req->flags & REQ_F_PREPPED)
745 if (force_nonblock && !io_file_supports_async(req->file))
746 force_nonblock = false;
748 kiocb->ki_pos = READ_ONCE(sqe->off);
749 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
750 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
752 ioprio = READ_ONCE(sqe->ioprio);
754 ret = ioprio_check_cap(ioprio);
758 kiocb->ki_ioprio = ioprio;
760 kiocb->ki_ioprio = get_current_ioprio();
762 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
765 if (force_nonblock) {
766 kiocb->ki_flags |= IOCB_NOWAIT;
767 req->flags |= REQ_F_FORCE_NONBLOCK;
769 if (ctx->flags & IORING_SETUP_IOPOLL) {
770 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
771 !kiocb->ki_filp->f_op->iopoll)
775 kiocb->ki_flags |= IOCB_HIPRI;
776 kiocb->ki_complete = io_complete_rw_iopoll;
778 if (kiocb->ki_flags & IOCB_HIPRI)
780 kiocb->ki_complete = io_complete_rw;
782 req->flags |= REQ_F_PREPPED;
786 static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
792 case -ERESTARTNOINTR:
793 case -ERESTARTNOHAND:
794 case -ERESTART_RESTARTBLOCK:
796 * We can't just restart the syscall, since previously
797 * submitted sqes may already be in progress. Just fail this
803 kiocb->ki_complete(kiocb, ret, 0);
807 static int io_import_fixed(struct io_ring_ctx *ctx, int rw,
808 const struct io_uring_sqe *sqe,
809 struct iov_iter *iter)
811 size_t len = READ_ONCE(sqe->len);
812 struct io_mapped_ubuf *imu;
813 unsigned index, buf_index;
817 /* attempt to use fixed buffers without having provided iovecs */
818 if (unlikely(!ctx->user_bufs))
821 buf_index = READ_ONCE(sqe->buf_index);
822 if (unlikely(buf_index >= ctx->nr_user_bufs))
825 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
826 imu = &ctx->user_bufs[index];
827 buf_addr = READ_ONCE(sqe->addr);
830 if (buf_addr + len < buf_addr)
832 /* not inside the mapped region */
833 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
837 * May not be a start of buffer, set size appropriately
838 * and advance us to the beginning.
840 offset = buf_addr - imu->ubuf;
841 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
843 iov_iter_advance(iter, offset);
847 static int io_import_iovec(struct io_ring_ctx *ctx, int rw,
848 const struct sqe_submit *s, struct iovec **iovec,
849 struct iov_iter *iter)
851 const struct io_uring_sqe *sqe = s->sqe;
852 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
853 size_t sqe_len = READ_ONCE(sqe->len);
857 * We're reading ->opcode for the second time, but the first read
858 * doesn't care whether it's _FIXED or not, so it doesn't matter
859 * whether ->opcode changes concurrently. The first read does care
860 * about whether it is a READ or a WRITE, so we don't trust this read
861 * for that purpose and instead let the caller pass in the read/write
864 opcode = READ_ONCE(sqe->opcode);
865 if (opcode == IORING_OP_READ_FIXED ||
866 opcode == IORING_OP_WRITE_FIXED) {
867 int ret = io_import_fixed(ctx, rw, sqe, iter);
877 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
881 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
885 * Make a note of the last file/offset/direction we punted to async
886 * context. We'll use this information to see if we can piggy back a
887 * sequential request onto the previous one, if it's still hasn't been
888 * completed by the async worker.
890 static void io_async_list_note(int rw, struct io_kiocb *req, size_t len)
892 struct async_list *async_list = &req->ctx->pending_async[rw];
893 struct kiocb *kiocb = &req->rw;
894 struct file *filp = kiocb->ki_filp;
895 off_t io_end = kiocb->ki_pos + len;
897 if (filp == async_list->file && kiocb->ki_pos == async_list->io_end) {
898 unsigned long max_pages;
900 /* Use 8x RA size as a decent limiter for both reads/writes */
901 max_pages = filp->f_ra.ra_pages;
903 max_pages = VM_MAX_READAHEAD >> (PAGE_SHIFT - 10);
906 /* If max pages are exceeded, reset the state */
908 if (async_list->io_pages + len <= max_pages) {
909 req->flags |= REQ_F_SEQ_PREV;
910 async_list->io_pages += len;
913 async_list->io_pages = 0;
917 /* New file? Reset state. */
918 if (async_list->file != filp) {
919 async_list->io_pages = 0;
920 async_list->file = filp;
922 async_list->io_end = io_end;
925 static int io_read(struct io_kiocb *req, const struct sqe_submit *s,
926 bool force_nonblock, struct io_submit_state *state)
928 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
929 struct kiocb *kiocb = &req->rw;
930 struct iov_iter iter;
935 ret = io_prep_rw(req, s, force_nonblock, state);
938 file = kiocb->ki_filp;
940 if (unlikely(!(file->f_mode & FMODE_READ)))
942 if (unlikely(!file->f_op->read_iter))
945 ret = io_import_iovec(req->ctx, READ, s, &iovec, &iter);
949 iov_count = iov_iter_count(&iter);
950 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
954 /* Catch -EAGAIN return for forced non-blocking submission */
955 ret2 = call_read_iter(file, kiocb, &iter);
956 if (!force_nonblock || ret2 != -EAGAIN) {
957 io_rw_done(kiocb, ret2);
960 * If ->needs_lock is true, we're already in async
964 io_async_list_note(READ, req, iov_count);
972 static int io_write(struct io_kiocb *req, const struct sqe_submit *s,
973 bool force_nonblock, struct io_submit_state *state)
975 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
976 struct kiocb *kiocb = &req->rw;
977 struct iov_iter iter;
982 ret = io_prep_rw(req, s, force_nonblock, state);
986 file = kiocb->ki_filp;
987 if (unlikely(!(file->f_mode & FMODE_WRITE)))
989 if (unlikely(!file->f_op->write_iter))
992 ret = io_import_iovec(req->ctx, WRITE, s, &iovec, &iter);
996 iov_count = iov_iter_count(&iter);
999 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT)) {
1000 /* If ->needs_lock is true, we're already in async context. */
1002 io_async_list_note(WRITE, req, iov_count);
1006 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
1009 * Open-code file_start_write here to grab freeze protection,
1010 * which will be released by another thread in
1011 * io_complete_rw(). Fool lockdep by telling it the lock got
1012 * released so that it doesn't complain about the held lock when
1013 * we return to userspace.
1015 if (S_ISREG(file_inode(file)->i_mode)) {
1016 __sb_start_write(file_inode(file)->i_sb,
1017 SB_FREEZE_WRITE, true);
1018 __sb_writers_release(file_inode(file)->i_sb,
1021 kiocb->ki_flags |= IOCB_WRITE;
1022 io_rw_done(kiocb, call_write_iter(file, kiocb, &iter));
1030 * IORING_OP_NOP just posts a completion event, nothing else.
1032 static int io_nop(struct io_kiocb *req, u64 user_data)
1034 struct io_ring_ctx *ctx = req->ctx;
1037 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1040 io_cqring_add_event(ctx, user_data, err, 0);
1045 static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1047 struct io_ring_ctx *ctx = req->ctx;
1051 /* Prep already done (EAGAIN retry) */
1052 if (req->flags & REQ_F_PREPPED)
1055 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1057 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1060 req->flags |= REQ_F_PREPPED;
1064 static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1065 bool force_nonblock)
1067 loff_t sqe_off = READ_ONCE(sqe->off);
1068 loff_t sqe_len = READ_ONCE(sqe->len);
1069 loff_t end = sqe_off + sqe_len;
1070 unsigned fsync_flags;
1073 fsync_flags = READ_ONCE(sqe->fsync_flags);
1074 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1077 ret = io_prep_fsync(req, sqe);
1081 /* fsync always requires a blocking context */
1085 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1086 end > 0 ? end : LLONG_MAX,
1087 fsync_flags & IORING_FSYNC_DATASYNC);
1089 io_cqring_add_event(req->ctx, sqe->user_data, ret, 0);
1094 static void io_poll_remove_one(struct io_kiocb *req)
1096 struct io_poll_iocb *poll = &req->poll;
1098 spin_lock(&poll->head->lock);
1099 WRITE_ONCE(poll->canceled, true);
1100 if (!list_empty(&poll->wait.entry)) {
1101 list_del_init(&poll->wait.entry);
1102 queue_work(req->ctx->sqo_wq, &req->work);
1104 spin_unlock(&poll->head->lock);
1106 list_del_init(&req->list);
1109 static void io_poll_remove_all(struct io_ring_ctx *ctx)
1111 struct io_kiocb *req;
1113 spin_lock_irq(&ctx->completion_lock);
1114 while (!list_empty(&ctx->cancel_list)) {
1115 req = list_first_entry(&ctx->cancel_list, struct io_kiocb,list);
1116 io_poll_remove_one(req);
1118 spin_unlock_irq(&ctx->completion_lock);
1122 * Find a running poll command that matches one specified in sqe->addr,
1123 * and remove it if found.
1125 static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1127 struct io_ring_ctx *ctx = req->ctx;
1128 struct io_kiocb *poll_req, *next;
1131 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1133 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
1137 spin_lock_irq(&ctx->completion_lock);
1138 list_for_each_entry_safe(poll_req, next, &ctx->cancel_list, list) {
1139 if (READ_ONCE(sqe->addr) == poll_req->user_data) {
1140 io_poll_remove_one(poll_req);
1145 spin_unlock_irq(&ctx->completion_lock);
1147 io_cqring_add_event(req->ctx, sqe->user_data, ret, 0);
1152 static void io_poll_complete(struct io_kiocb *req, __poll_t mask)
1154 io_cqring_add_event(req->ctx, req->user_data, mangle_poll(mask), 0);
1158 static void io_poll_complete_work(struct work_struct *work)
1160 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
1161 struct io_poll_iocb *poll = &req->poll;
1162 struct poll_table_struct pt = { ._key = poll->events };
1163 struct io_ring_ctx *ctx = req->ctx;
1166 if (!READ_ONCE(poll->canceled))
1167 mask = vfs_poll(poll->file, &pt) & poll->events;
1170 * Note that ->ki_cancel callers also delete iocb from active_reqs after
1171 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
1172 * synchronize with them. In the cancellation case the list_del_init
1173 * itself is not actually needed, but harmless so we keep it in to
1174 * avoid further branches in the fast path.
1176 spin_lock_irq(&ctx->completion_lock);
1177 if (!mask && !READ_ONCE(poll->canceled)) {
1178 add_wait_queue(poll->head, &poll->wait);
1179 spin_unlock_irq(&ctx->completion_lock);
1182 list_del_init(&req->list);
1183 spin_unlock_irq(&ctx->completion_lock);
1185 io_poll_complete(req, mask);
1188 static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
1191 struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb,
1193 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
1194 struct io_ring_ctx *ctx = req->ctx;
1195 __poll_t mask = key_to_poll(key);
1199 /* for instances that support it check for an event match first: */
1201 unsigned long flags;
1203 if (!(mask & poll->events))
1206 /* try to complete the iocb inline if we can: */
1207 if (spin_trylock_irqsave(&ctx->completion_lock, flags)) {
1208 list_del(&req->list);
1209 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1211 list_del_init(&poll->wait.entry);
1212 io_poll_complete(req, mask);
1217 list_del_init(&poll->wait.entry);
1218 queue_work(ctx->sqo_wq, &req->work);
1222 struct io_poll_table {
1223 struct poll_table_struct pt;
1224 struct io_kiocb *req;
1228 static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
1229 struct poll_table_struct *p)
1231 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
1233 if (unlikely(pt->req->poll.head)) {
1234 pt->error = -EINVAL;
1239 pt->req->poll.head = head;
1240 add_wait_queue(head, &pt->req->poll.wait);
1243 static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1245 struct io_poll_iocb *poll = &req->poll;
1246 struct io_ring_ctx *ctx = req->ctx;
1247 struct io_poll_table ipt;
1251 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1253 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
1258 INIT_WORK(&req->work, io_poll_complete_work);
1259 events = READ_ONCE(sqe->poll_events);
1260 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
1263 poll->woken = false;
1264 poll->canceled = false;
1266 ipt.pt._qproc = io_poll_queue_proc;
1267 ipt.pt._key = poll->events;
1269 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
1271 /* initialized the list so that we can do list_empty checks */
1272 INIT_LIST_HEAD(&poll->wait.entry);
1273 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
1275 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
1276 if (unlikely(!poll->head)) {
1277 /* we did not manage to set up a waitqueue, done */
1281 spin_lock_irq(&ctx->completion_lock);
1282 spin_lock(&poll->head->lock);
1284 /* wake_up context handles the rest */
1287 } else if (mask || ipt.error) {
1288 /* if we get an error or a mask we are done */
1289 WARN_ON_ONCE(list_empty(&poll->wait.entry));
1290 list_del_init(&poll->wait.entry);
1292 /* actually waiting for an event */
1293 list_add_tail(&req->list, &ctx->cancel_list);
1295 spin_unlock(&poll->head->lock);
1296 spin_unlock_irq(&ctx->completion_lock);
1299 if (unlikely(ipt.error)) {
1301 * Drop one of our refs to this req, __io_submit_sqe() will
1302 * drop the other one since we're returning an error.
1309 io_poll_complete(req, mask);
1313 static int __io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
1314 const struct sqe_submit *s, bool force_nonblock,
1315 struct io_submit_state *state)
1319 if (unlikely(s->index >= ctx->sq_entries))
1321 req->user_data = READ_ONCE(s->sqe->user_data);
1323 opcode = READ_ONCE(s->sqe->opcode);
1326 ret = io_nop(req, req->user_data);
1328 case IORING_OP_READV:
1329 if (unlikely(s->sqe->buf_index))
1331 ret = io_read(req, s, force_nonblock, state);
1333 case IORING_OP_WRITEV:
1334 if (unlikely(s->sqe->buf_index))
1336 ret = io_write(req, s, force_nonblock, state);
1338 case IORING_OP_READ_FIXED:
1339 ret = io_read(req, s, force_nonblock, state);
1341 case IORING_OP_WRITE_FIXED:
1342 ret = io_write(req, s, force_nonblock, state);
1344 case IORING_OP_FSYNC:
1345 ret = io_fsync(req, s->sqe, force_nonblock);
1347 case IORING_OP_POLL_ADD:
1348 ret = io_poll_add(req, s->sqe);
1350 case IORING_OP_POLL_REMOVE:
1351 ret = io_poll_remove(req, s->sqe);
1361 if (ctx->flags & IORING_SETUP_IOPOLL) {
1362 if (req->error == -EAGAIN)
1365 /* workqueue context doesn't hold uring_lock, grab it now */
1367 mutex_lock(&ctx->uring_lock);
1368 io_iopoll_req_issued(req);
1370 mutex_unlock(&ctx->uring_lock);
1376 static struct async_list *io_async_list_from_sqe(struct io_ring_ctx *ctx,
1377 const struct io_uring_sqe *sqe)
1379 switch (sqe->opcode) {
1380 case IORING_OP_READV:
1381 case IORING_OP_READ_FIXED:
1382 return &ctx->pending_async[READ];
1383 case IORING_OP_WRITEV:
1384 case IORING_OP_WRITE_FIXED:
1385 return &ctx->pending_async[WRITE];
1391 static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
1393 u8 opcode = READ_ONCE(sqe->opcode);
1395 return !(opcode == IORING_OP_READ_FIXED ||
1396 opcode == IORING_OP_WRITE_FIXED);
1399 static void io_sq_wq_submit_work(struct work_struct *work)
1401 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
1402 struct io_ring_ctx *ctx = req->ctx;
1403 struct mm_struct *cur_mm = NULL;
1404 struct async_list *async_list;
1405 LIST_HEAD(req_list);
1406 mm_segment_t old_fs;
1409 async_list = io_async_list_from_sqe(ctx, req->submit.sqe);
1412 struct sqe_submit *s = &req->submit;
1413 const struct io_uring_sqe *sqe = s->sqe;
1415 /* Ensure we clear previously set forced non-block flag */
1416 req->flags &= ~REQ_F_FORCE_NONBLOCK;
1417 req->rw.ki_flags &= ~IOCB_NOWAIT;
1420 if (io_sqe_needs_user(sqe) && !cur_mm) {
1421 if (!mmget_not_zero(ctx->sqo_mm)) {
1424 cur_mm = ctx->sqo_mm;
1432 s->has_user = cur_mm != NULL;
1433 s->needs_lock = true;
1435 ret = __io_submit_sqe(ctx, req, s, false, NULL);
1437 * We can get EAGAIN for polled IO even though
1438 * we're forcing a sync submission from here,
1439 * since we can't wait for request slots on the
1447 /* drop submission reference */
1451 io_cqring_add_event(ctx, sqe->user_data, ret, 0);
1455 /* async context always use a copy of the sqe */
1460 if (!list_empty(&req_list)) {
1461 req = list_first_entry(&req_list, struct io_kiocb,
1463 list_del(&req->list);
1466 if (list_empty(&async_list->list))
1470 spin_lock(&async_list->lock);
1471 if (list_empty(&async_list->list)) {
1472 spin_unlock(&async_list->lock);
1475 list_splice_init(&async_list->list, &req_list);
1476 spin_unlock(&async_list->lock);
1478 req = list_first_entry(&req_list, struct io_kiocb, list);
1479 list_del(&req->list);
1483 * Rare case of racing with a submitter. If we find the count has
1484 * dropped to zero AND we have pending work items, then restart
1485 * the processing. This is a tiny race window.
1488 ret = atomic_dec_return(&async_list->cnt);
1489 while (!ret && !list_empty(&async_list->list)) {
1490 spin_lock(&async_list->lock);
1491 atomic_inc(&async_list->cnt);
1492 list_splice_init(&async_list->list, &req_list);
1493 spin_unlock(&async_list->lock);
1495 if (!list_empty(&req_list)) {
1496 req = list_first_entry(&req_list,
1497 struct io_kiocb, list);
1498 list_del(&req->list);
1501 ret = atomic_dec_return(&async_list->cnt);
1513 * See if we can piggy back onto previously submitted work, that is still
1514 * running. We currently only allow this if the new request is sequential
1515 * to the previous one we punted.
1517 static bool io_add_to_prev_work(struct async_list *list, struct io_kiocb *req)
1523 if (!(req->flags & REQ_F_SEQ_PREV))
1525 if (!atomic_read(&list->cnt))
1529 spin_lock(&list->lock);
1530 list_add_tail(&req->list, &list->list);
1531 if (!atomic_read(&list->cnt)) {
1532 list_del_init(&req->list);
1535 spin_unlock(&list->lock);
1539 static bool io_op_needs_file(const struct io_uring_sqe *sqe)
1541 int op = READ_ONCE(sqe->opcode);
1545 case IORING_OP_POLL_REMOVE:
1552 static int io_req_set_file(struct io_ring_ctx *ctx, const struct sqe_submit *s,
1553 struct io_submit_state *state, struct io_kiocb *req)
1558 flags = READ_ONCE(s->sqe->flags);
1559 fd = READ_ONCE(s->sqe->fd);
1561 if (!io_op_needs_file(s->sqe)) {
1566 if (flags & IOSQE_FIXED_FILE) {
1567 if (unlikely(!ctx->user_files ||
1568 (unsigned) fd >= ctx->nr_user_files))
1570 req->file = ctx->user_files[fd];
1571 req->flags |= REQ_F_FIXED_FILE;
1573 if (s->needs_fixed_file)
1575 req->file = io_file_get(state, fd);
1576 if (unlikely(!req->file))
1583 static int io_submit_sqe(struct io_ring_ctx *ctx, struct sqe_submit *s,
1584 struct io_submit_state *state)
1586 struct io_kiocb *req;
1589 /* enforce forwards compatibility on users */
1590 if (unlikely(s->sqe->flags & ~IOSQE_FIXED_FILE))
1593 req = io_get_req(ctx, state);
1597 ret = io_req_set_file(ctx, s, state, req);
1601 ret = __io_submit_sqe(ctx, req, s, true, state);
1602 if (ret == -EAGAIN) {
1603 struct io_uring_sqe *sqe_copy;
1605 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
1607 struct async_list *list;
1609 memcpy(sqe_copy, s->sqe, sizeof(*sqe_copy));
1612 memcpy(&req->submit, s, sizeof(*s));
1613 list = io_async_list_from_sqe(ctx, s->sqe);
1614 if (!io_add_to_prev_work(list, req)) {
1616 atomic_inc(&list->cnt);
1617 INIT_WORK(&req->work, io_sq_wq_submit_work);
1618 queue_work(ctx->sqo_wq, &req->work);
1622 * Queued up for async execution, worker will release
1623 * submit reference when the iocb is actually
1631 /* drop submission reference */
1634 /* and drop final reference, if we failed */
1642 * Batched submission is done, ensure local IO is flushed out.
1644 static void io_submit_state_end(struct io_submit_state *state)
1646 blk_finish_plug(&state->plug);
1647 io_file_put(state, NULL);
1648 if (state->free_reqs)
1649 kmem_cache_free_bulk(req_cachep, state->free_reqs,
1650 &state->reqs[state->cur_req]);
1654 * Start submission side cache.
1656 static void io_submit_state_start(struct io_submit_state *state,
1657 struct io_ring_ctx *ctx, unsigned max_ios)
1659 blk_start_plug(&state->plug);
1660 state->free_reqs = 0;
1662 state->ios_left = max_ios;
1665 static void io_commit_sqring(struct io_ring_ctx *ctx)
1667 struct io_sq_ring *ring = ctx->sq_ring;
1669 if (ctx->cached_sq_head != READ_ONCE(ring->r.head)) {
1671 * Ensure any loads from the SQEs are done at this point,
1672 * since once we write the new head, the application could
1673 * write new data to them.
1675 smp_store_release(&ring->r.head, ctx->cached_sq_head);
1678 * write side barrier of head update, app has read side. See
1679 * comment at the top of this file
1686 * Undo last io_get_sqring()
1688 static void io_drop_sqring(struct io_ring_ctx *ctx)
1690 ctx->cached_sq_head--;
1694 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
1695 * that is mapped by userspace. This means that care needs to be taken to
1696 * ensure that reads are stable, as we cannot rely on userspace always
1697 * being a good citizen. If members of the sqe are validated and then later
1698 * used, it's important that those reads are done through READ_ONCE() to
1699 * prevent a re-load down the line.
1701 static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
1703 struct io_sq_ring *ring = ctx->sq_ring;
1707 * The cached sq head (or cq tail) serves two purposes:
1709 * 1) allows us to batch the cost of updating the user visible
1711 * 2) allows the kernel side to track the head on its own, even
1712 * though the application is the one updating it.
1714 head = ctx->cached_sq_head;
1715 /* See comment at the top of this file */
1717 if (head == READ_ONCE(ring->r.tail))
1720 head = READ_ONCE(ring->array[head & ctx->sq_mask]);
1721 if (head < ctx->sq_entries) {
1723 s->sqe = &ctx->sq_sqes[head];
1724 ctx->cached_sq_head++;
1728 /* drop invalid entries */
1729 ctx->cached_sq_head++;
1731 /* See comment at the top of this file */
1736 static int io_submit_sqes(struct io_ring_ctx *ctx, struct sqe_submit *sqes,
1737 unsigned int nr, bool has_user, bool mm_fault)
1739 struct io_submit_state state, *statep = NULL;
1740 int ret, i, submitted = 0;
1742 if (nr > IO_PLUG_THRESHOLD) {
1743 io_submit_state_start(&state, ctx, nr);
1747 for (i = 0; i < nr; i++) {
1748 if (unlikely(mm_fault)) {
1751 sqes[i].has_user = has_user;
1752 sqes[i].needs_lock = true;
1753 sqes[i].needs_fixed_file = true;
1754 ret = io_submit_sqe(ctx, &sqes[i], statep);
1761 io_cqring_add_event(ctx, sqes[i].sqe->user_data, ret, 0);
1765 io_submit_state_end(&state);
1770 static int io_sq_thread(void *data)
1772 struct sqe_submit sqes[IO_IOPOLL_BATCH];
1773 struct io_ring_ctx *ctx = data;
1774 struct mm_struct *cur_mm = NULL;
1775 mm_segment_t old_fs;
1778 unsigned long timeout;
1783 timeout = inflight = 0;
1784 while (!kthread_should_stop() && !ctx->sqo_stop) {
1785 bool all_fixed, mm_fault = false;
1789 unsigned nr_events = 0;
1791 if (ctx->flags & IORING_SETUP_IOPOLL) {
1793 * We disallow the app entering submit/complete
1794 * with polling, but we still need to lock the
1795 * ring to prevent racing with polled issue
1796 * that got punted to a workqueue.
1798 mutex_lock(&ctx->uring_lock);
1799 io_iopoll_check(ctx, &nr_events, 0);
1800 mutex_unlock(&ctx->uring_lock);
1803 * Normal IO, just pretend everything completed.
1804 * We don't have to poll completions for that.
1806 nr_events = inflight;
1809 inflight -= nr_events;
1811 timeout = jiffies + ctx->sq_thread_idle;
1814 if (!io_get_sqring(ctx, &sqes[0])) {
1816 * We're polling. If we're within the defined idle
1817 * period, then let us spin without work before going
1820 if (inflight || !time_after(jiffies, timeout)) {
1826 * Drop cur_mm before scheduling, we can't hold it for
1827 * long periods (or over schedule()). Do this before
1828 * adding ourselves to the waitqueue, as the unuse/drop
1837 prepare_to_wait(&ctx->sqo_wait, &wait,
1838 TASK_INTERRUPTIBLE);
1840 /* Tell userspace we may need a wakeup call */
1841 ctx->sq_ring->flags |= IORING_SQ_NEED_WAKEUP;
1844 if (!io_get_sqring(ctx, &sqes[0])) {
1845 if (kthread_should_stop()) {
1846 finish_wait(&ctx->sqo_wait, &wait);
1849 if (signal_pending(current))
1850 flush_signals(current);
1852 finish_wait(&ctx->sqo_wait, &wait);
1854 ctx->sq_ring->flags &= ~IORING_SQ_NEED_WAKEUP;
1858 finish_wait(&ctx->sqo_wait, &wait);
1860 ctx->sq_ring->flags &= ~IORING_SQ_NEED_WAKEUP;
1867 if (all_fixed && io_sqe_needs_user(sqes[i].sqe))
1871 if (i == ARRAY_SIZE(sqes))
1873 } while (io_get_sqring(ctx, &sqes[i]));
1875 /* Unless all new commands are FIXED regions, grab mm */
1876 if (!all_fixed && !cur_mm) {
1877 mm_fault = !mmget_not_zero(ctx->sqo_mm);
1879 use_mm(ctx->sqo_mm);
1880 cur_mm = ctx->sqo_mm;
1884 inflight += io_submit_sqes(ctx, sqes, i, cur_mm != NULL,
1887 /* Commit SQ ring head once we've consumed all SQEs */
1888 io_commit_sqring(ctx);
1899 static int io_ring_submit(struct io_ring_ctx *ctx, unsigned int to_submit)
1901 struct io_submit_state state, *statep = NULL;
1902 int i, ret = 0, submit = 0;
1904 if (to_submit > IO_PLUG_THRESHOLD) {
1905 io_submit_state_start(&state, ctx, to_submit);
1909 for (i = 0; i < to_submit; i++) {
1910 struct sqe_submit s;
1912 if (!io_get_sqring(ctx, &s))
1916 s.needs_lock = false;
1917 s.needs_fixed_file = false;
1919 ret = io_submit_sqe(ctx, &s, statep);
1921 io_drop_sqring(ctx);
1927 io_commit_sqring(ctx);
1930 io_submit_state_end(statep);
1932 return submit ? submit : ret;
1935 static unsigned io_cqring_events(struct io_cq_ring *ring)
1937 return READ_ONCE(ring->r.tail) - READ_ONCE(ring->r.head);
1941 * Wait until events become available, if we don't already have some. The
1942 * application must reap them itself, as they reside on the shared cq ring.
1944 static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
1945 const sigset_t __user *sig, size_t sigsz)
1947 struct io_cq_ring *ring = ctx->cq_ring;
1948 sigset_t ksigmask, sigsaved;
1952 /* See comment at the top of this file */
1954 if (io_cqring_events(ring) >= min_events)
1958 ret = set_user_sigmask(sig, &ksigmask, &sigsaved, sigsz);
1964 prepare_to_wait(&ctx->wait, &wait, TASK_INTERRUPTIBLE);
1967 /* See comment at the top of this file */
1969 if (io_cqring_events(ring) >= min_events)
1975 if (signal_pending(current))
1979 finish_wait(&ctx->wait, &wait);
1982 restore_user_sigmask(sig, &sigsaved);
1984 return READ_ONCE(ring->r.head) == READ_ONCE(ring->r.tail) ? ret : 0;
1987 static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
1989 #if defined(CONFIG_UNIX)
1990 if (ctx->ring_sock) {
1991 struct sock *sock = ctx->ring_sock->sk;
1992 struct sk_buff *skb;
1994 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
2000 for (i = 0; i < ctx->nr_user_files; i++)
2001 fput(ctx->user_files[i]);
2005 static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
2007 if (!ctx->user_files)
2010 __io_sqe_files_unregister(ctx);
2011 kfree(ctx->user_files);
2012 ctx->user_files = NULL;
2013 ctx->nr_user_files = 0;
2017 static void io_sq_thread_stop(struct io_ring_ctx *ctx)
2019 if (ctx->sqo_thread) {
2022 kthread_stop(ctx->sqo_thread);
2023 ctx->sqo_thread = NULL;
2027 static void io_finish_async(struct io_ring_ctx *ctx)
2029 io_sq_thread_stop(ctx);
2032 destroy_workqueue(ctx->sqo_wq);
2037 #if defined(CONFIG_UNIX)
2038 static void io_destruct_skb(struct sk_buff *skb)
2040 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
2042 io_finish_async(ctx);
2043 unix_destruct_scm(skb);
2047 * Ensure the UNIX gc is aware of our file set, so we are certain that
2048 * the io_uring can be safely unregistered on process exit, even if we have
2049 * loops in the file referencing.
2051 static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
2053 struct sock *sk = ctx->ring_sock->sk;
2054 struct scm_fp_list *fpl;
2055 struct sk_buff *skb;
2058 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
2059 unsigned long inflight = ctx->user->unix_inflight + nr;
2061 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
2065 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
2069 skb = alloc_skb(0, GFP_KERNEL);
2076 skb->destructor = io_destruct_skb;
2078 fpl->user = get_uid(ctx->user);
2079 for (i = 0; i < nr; i++) {
2080 fpl->fp[i] = get_file(ctx->user_files[i + offset]);
2081 unix_inflight(fpl->user, fpl->fp[i]);
2084 fpl->max = fpl->count = nr;
2085 UNIXCB(skb).fp = fpl;
2086 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
2087 skb_queue_head(&sk->sk_receive_queue, skb);
2089 for (i = 0; i < nr; i++)
2096 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
2097 * causes regular reference counting to break down. We rely on the UNIX
2098 * garbage collection to take care of this problem for us.
2100 static int io_sqe_files_scm(struct io_ring_ctx *ctx)
2102 unsigned left, total;
2106 left = ctx->nr_user_files;
2108 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
2111 ret = __io_sqe_files_scm(ctx, this_files, total);
2115 total += this_files;
2121 while (total < ctx->nr_user_files) {
2122 fput(ctx->user_files[total]);
2129 static int io_sqe_files_scm(struct io_ring_ctx *ctx)
2135 static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
2138 __s32 __user *fds = (__s32 __user *) arg;
2142 if (ctx->user_files)
2146 if (nr_args > IORING_MAX_FIXED_FILES)
2149 ctx->user_files = kcalloc(nr_args, sizeof(struct file *), GFP_KERNEL);
2150 if (!ctx->user_files)
2153 for (i = 0; i < nr_args; i++) {
2155 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
2158 ctx->user_files[i] = fget(fd);
2161 if (!ctx->user_files[i])
2164 * Don't allow io_uring instances to be registered. If UNIX
2165 * isn't enabled, then this causes a reference cycle and this
2166 * instance can never get freed. If UNIX is enabled we'll
2167 * handle it just fine, but there's still no point in allowing
2168 * a ring fd as it doesn't support regular read/write anyway.
2170 if (ctx->user_files[i]->f_op == &io_uring_fops) {
2171 fput(ctx->user_files[i]);
2174 ctx->nr_user_files++;
2179 for (i = 0; i < ctx->nr_user_files; i++)
2180 fput(ctx->user_files[i]);
2182 kfree(ctx->user_files);
2183 ctx->nr_user_files = 0;
2187 ret = io_sqe_files_scm(ctx);
2189 io_sqe_files_unregister(ctx);
2194 static int io_sq_offload_start(struct io_ring_ctx *ctx,
2195 struct io_uring_params *p)
2199 init_waitqueue_head(&ctx->sqo_wait);
2200 mmgrab(current->mm);
2201 ctx->sqo_mm = current->mm;
2203 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
2204 if (!ctx->sq_thread_idle)
2205 ctx->sq_thread_idle = HZ;
2208 if (!cpu_possible(p->sq_thread_cpu))
2211 if (ctx->flags & IORING_SETUP_SQPOLL) {
2212 if (p->flags & IORING_SETUP_SQ_AFF) {
2215 cpu = array_index_nospec(p->sq_thread_cpu, NR_CPUS);
2216 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
2220 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
2223 if (IS_ERR(ctx->sqo_thread)) {
2224 ret = PTR_ERR(ctx->sqo_thread);
2225 ctx->sqo_thread = NULL;
2228 wake_up_process(ctx->sqo_thread);
2229 } else if (p->flags & IORING_SETUP_SQ_AFF) {
2230 /* Can't have SQ_AFF without SQPOLL */
2235 /* Do QD, or 2 * CPUS, whatever is smallest */
2236 ctx->sqo_wq = alloc_workqueue("io_ring-wq", WQ_UNBOUND | WQ_FREEZABLE,
2237 min(ctx->sq_entries - 1, 2 * num_online_cpus()));
2245 io_sq_thread_stop(ctx);
2246 mmdrop(ctx->sqo_mm);
2251 static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
2253 atomic_long_sub(nr_pages, &user->locked_vm);
2256 static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
2258 unsigned long page_limit, cur_pages, new_pages;
2260 /* Don't allow more pages than we can safely lock */
2261 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
2264 cur_pages = atomic_long_read(&user->locked_vm);
2265 new_pages = cur_pages + nr_pages;
2266 if (new_pages > page_limit)
2268 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
2269 new_pages) != cur_pages);
2274 static void io_mem_free(void *ptr)
2276 struct page *page = virt_to_head_page(ptr);
2278 if (put_page_testzero(page))
2279 free_compound_page(page);
2282 static void *io_mem_alloc(size_t size)
2284 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
2287 return (void *) __get_free_pages(gfp_flags, get_order(size));
2290 static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
2292 struct io_sq_ring *sq_ring;
2293 struct io_cq_ring *cq_ring;
2296 bytes = struct_size(sq_ring, array, sq_entries);
2297 bytes += array_size(sizeof(struct io_uring_sqe), sq_entries);
2298 bytes += struct_size(cq_ring, cqes, cq_entries);
2300 return (bytes + PAGE_SIZE - 1) / PAGE_SIZE;
2303 static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
2307 if (!ctx->user_bufs)
2310 for (i = 0; i < ctx->nr_user_bufs; i++) {
2311 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
2313 for (j = 0; j < imu->nr_bvecs; j++)
2314 put_page(imu->bvec[j].bv_page);
2316 if (ctx->account_mem)
2317 io_unaccount_mem(ctx->user, imu->nr_bvecs);
2322 kfree(ctx->user_bufs);
2323 ctx->user_bufs = NULL;
2324 ctx->nr_user_bufs = 0;
2328 static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
2329 void __user *arg, unsigned index)
2331 struct iovec __user *src;
2333 #ifdef CONFIG_COMPAT
2335 struct compat_iovec __user *ciovs;
2336 struct compat_iovec ciov;
2338 ciovs = (struct compat_iovec __user *) arg;
2339 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
2342 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
2343 dst->iov_len = ciov.iov_len;
2347 src = (struct iovec __user *) arg;
2348 if (copy_from_user(dst, &src[index], sizeof(*dst)))
2353 static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
2356 struct vm_area_struct **vmas = NULL;
2357 struct page **pages = NULL;
2358 int i, j, got_pages = 0;
2363 if (!nr_args || nr_args > UIO_MAXIOV)
2366 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
2368 if (!ctx->user_bufs)
2371 for (i = 0; i < nr_args; i++) {
2372 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
2373 unsigned long off, start, end, ubuf;
2378 ret = io_copy_iov(ctx, &iov, arg, i);
2383 * Don't impose further limits on the size and buffer
2384 * constraints here, we'll -EINVAL later when IO is
2385 * submitted if they are wrong.
2388 if (!iov.iov_base || !iov.iov_len)
2391 /* arbitrary limit, but we need something */
2392 if (iov.iov_len > SZ_1G)
2395 ubuf = (unsigned long) iov.iov_base;
2396 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
2397 start = ubuf >> PAGE_SHIFT;
2398 nr_pages = end - start;
2400 if (ctx->account_mem) {
2401 ret = io_account_mem(ctx->user, nr_pages);
2407 if (!pages || nr_pages > got_pages) {
2410 pages = kmalloc_array(nr_pages, sizeof(struct page *),
2412 vmas = kmalloc_array(nr_pages,
2413 sizeof(struct vm_area_struct *),
2415 if (!pages || !vmas) {
2417 if (ctx->account_mem)
2418 io_unaccount_mem(ctx->user, nr_pages);
2421 got_pages = nr_pages;
2424 imu->bvec = kmalloc_array(nr_pages, sizeof(struct bio_vec),
2428 if (ctx->account_mem)
2429 io_unaccount_mem(ctx->user, nr_pages);
2434 down_read(¤t->mm->mmap_sem);
2435 pret = get_user_pages_longterm(ubuf, nr_pages, FOLL_WRITE,
2437 if (pret == nr_pages) {
2438 /* don't support file backed memory */
2439 for (j = 0; j < nr_pages; j++) {
2440 struct vm_area_struct *vma = vmas[j];
2443 !is_file_hugepages(vma->vm_file)) {
2449 ret = pret < 0 ? pret : -EFAULT;
2451 up_read(¤t->mm->mmap_sem);
2454 * if we did partial map, or found file backed vmas,
2455 * release any pages we did get
2458 for (j = 0; j < pret; j++)
2461 if (ctx->account_mem)
2462 io_unaccount_mem(ctx->user, nr_pages);
2466 off = ubuf & ~PAGE_MASK;
2468 for (j = 0; j < nr_pages; j++) {
2471 vec_len = min_t(size_t, size, PAGE_SIZE - off);
2472 imu->bvec[j].bv_page = pages[j];
2473 imu->bvec[j].bv_len = vec_len;
2474 imu->bvec[j].bv_offset = off;
2478 /* store original address for later verification */
2480 imu->len = iov.iov_len;
2481 imu->nr_bvecs = nr_pages;
2483 ctx->nr_user_bufs++;
2491 io_sqe_buffer_unregister(ctx);
2495 static void io_ring_ctx_free(struct io_ring_ctx *ctx)
2497 io_finish_async(ctx);
2499 mmdrop(ctx->sqo_mm);
2501 io_iopoll_reap_events(ctx);
2502 io_sqe_buffer_unregister(ctx);
2503 io_sqe_files_unregister(ctx);
2505 #if defined(CONFIG_UNIX)
2507 sock_release(ctx->ring_sock);
2510 io_mem_free(ctx->sq_ring);
2511 io_mem_free(ctx->sq_sqes);
2512 io_mem_free(ctx->cq_ring);
2514 percpu_ref_exit(&ctx->refs);
2515 if (ctx->account_mem)
2516 io_unaccount_mem(ctx->user,
2517 ring_pages(ctx->sq_entries, ctx->cq_entries));
2518 free_uid(ctx->user);
2522 static __poll_t io_uring_poll(struct file *file, poll_table *wait)
2524 struct io_ring_ctx *ctx = file->private_data;
2527 poll_wait(file, &ctx->cq_wait, wait);
2528 /* See comment at the top of this file */
2530 if (READ_ONCE(ctx->sq_ring->r.tail) + 1 != ctx->cached_sq_head)
2531 mask |= EPOLLOUT | EPOLLWRNORM;
2532 if (READ_ONCE(ctx->cq_ring->r.head) != ctx->cached_cq_tail)
2533 mask |= EPOLLIN | EPOLLRDNORM;
2538 static int io_uring_fasync(int fd, struct file *file, int on)
2540 struct io_ring_ctx *ctx = file->private_data;
2542 return fasync_helper(fd, file, on, &ctx->cq_fasync);
2545 static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
2547 mutex_lock(&ctx->uring_lock);
2548 percpu_ref_kill(&ctx->refs);
2549 mutex_unlock(&ctx->uring_lock);
2551 io_poll_remove_all(ctx);
2552 io_iopoll_reap_events(ctx);
2553 wait_for_completion(&ctx->ctx_done);
2554 io_ring_ctx_free(ctx);
2557 static int io_uring_release(struct inode *inode, struct file *file)
2559 struct io_ring_ctx *ctx = file->private_data;
2561 file->private_data = NULL;
2562 io_ring_ctx_wait_and_kill(ctx);
2566 static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
2568 loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT;
2569 unsigned long sz = vma->vm_end - vma->vm_start;
2570 struct io_ring_ctx *ctx = file->private_data;
2576 case IORING_OFF_SQ_RING:
2579 case IORING_OFF_SQES:
2582 case IORING_OFF_CQ_RING:
2589 page = virt_to_head_page(ptr);
2590 if (sz > (PAGE_SIZE << compound_order(page)))
2593 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
2594 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
2597 SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
2598 u32, min_complete, u32, flags, const sigset_t __user *, sig,
2601 struct io_ring_ctx *ctx;
2606 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
2614 if (f.file->f_op != &io_uring_fops)
2618 ctx = f.file->private_data;
2619 if (!percpu_ref_tryget(&ctx->refs))
2623 * For SQ polling, the thread will do all submissions and completions.
2624 * Just return the requested submit count, and wake the thread if
2627 if (ctx->flags & IORING_SETUP_SQPOLL) {
2628 if (flags & IORING_ENTER_SQ_WAKEUP)
2629 wake_up(&ctx->sqo_wait);
2630 submitted = to_submit;
2636 to_submit = min(to_submit, ctx->sq_entries);
2638 mutex_lock(&ctx->uring_lock);
2639 submitted = io_ring_submit(ctx, to_submit);
2640 mutex_unlock(&ctx->uring_lock);
2645 if (flags & IORING_ENTER_GETEVENTS) {
2646 unsigned nr_events = 0;
2648 min_complete = min(min_complete, ctx->cq_entries);
2651 * The application could have included the 'to_submit' count
2652 * in how many events it wanted to wait for. If we failed to
2653 * submit the desired count, we may need to adjust the number
2654 * of events to poll/wait for.
2656 if (submitted < to_submit)
2657 min_complete = min_t(unsigned, submitted, min_complete);
2659 if (ctx->flags & IORING_SETUP_IOPOLL) {
2660 mutex_lock(&ctx->uring_lock);
2661 ret = io_iopoll_check(ctx, &nr_events, min_complete);
2662 mutex_unlock(&ctx->uring_lock);
2664 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
2669 io_ring_drop_ctx_refs(ctx, 1);
2672 return submitted ? submitted : ret;
2675 static const struct file_operations io_uring_fops = {
2676 .release = io_uring_release,
2677 .mmap = io_uring_mmap,
2678 .poll = io_uring_poll,
2679 .fasync = io_uring_fasync,
2682 static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
2683 struct io_uring_params *p)
2685 struct io_sq_ring *sq_ring;
2686 struct io_cq_ring *cq_ring;
2689 sq_ring = io_mem_alloc(struct_size(sq_ring, array, p->sq_entries));
2693 ctx->sq_ring = sq_ring;
2694 sq_ring->ring_mask = p->sq_entries - 1;
2695 sq_ring->ring_entries = p->sq_entries;
2696 ctx->sq_mask = sq_ring->ring_mask;
2697 ctx->sq_entries = sq_ring->ring_entries;
2699 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
2700 if (size == SIZE_MAX)
2703 ctx->sq_sqes = io_mem_alloc(size);
2704 if (!ctx->sq_sqes) {
2705 io_mem_free(ctx->sq_ring);
2709 cq_ring = io_mem_alloc(struct_size(cq_ring, cqes, p->cq_entries));
2711 io_mem_free(ctx->sq_ring);
2712 io_mem_free(ctx->sq_sqes);
2716 ctx->cq_ring = cq_ring;
2717 cq_ring->ring_mask = p->cq_entries - 1;
2718 cq_ring->ring_entries = p->cq_entries;
2719 ctx->cq_mask = cq_ring->ring_mask;
2720 ctx->cq_entries = cq_ring->ring_entries;
2725 * Allocate an anonymous fd, this is what constitutes the application
2726 * visible backing of an io_uring instance. The application mmaps this
2727 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
2728 * we have to tie this fd to a socket for file garbage collection purposes.
2730 static int io_uring_get_fd(struct io_ring_ctx *ctx)
2735 #if defined(CONFIG_UNIX)
2736 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
2742 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
2746 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
2747 O_RDWR | O_CLOEXEC);
2750 ret = PTR_ERR(file);
2754 #if defined(CONFIG_UNIX)
2755 ctx->ring_sock->file = file;
2756 ctx->ring_sock->sk->sk_user_data = ctx;
2758 fd_install(ret, file);
2761 #if defined(CONFIG_UNIX)
2762 sock_release(ctx->ring_sock);
2763 ctx->ring_sock = NULL;
2768 static int io_uring_create(unsigned entries, struct io_uring_params *p)
2770 struct user_struct *user = NULL;
2771 struct io_ring_ctx *ctx;
2775 if (!entries || entries > IORING_MAX_ENTRIES)
2779 * Use twice as many entries for the CQ ring. It's possible for the
2780 * application to drive a higher depth than the size of the SQ ring,
2781 * since the sqes are only used at submission time. This allows for
2782 * some flexibility in overcommitting a bit.
2784 p->sq_entries = roundup_pow_of_two(entries);
2785 p->cq_entries = 2 * p->sq_entries;
2787 user = get_uid(current_user());
2788 account_mem = !capable(CAP_IPC_LOCK);
2791 ret = io_account_mem(user,
2792 ring_pages(p->sq_entries, p->cq_entries));
2799 ctx = io_ring_ctx_alloc(p);
2802 io_unaccount_mem(user, ring_pages(p->sq_entries,
2807 ctx->compat = in_compat_syscall();
2808 ctx->account_mem = account_mem;
2811 ret = io_allocate_scq_urings(ctx, p);
2815 ret = io_sq_offload_start(ctx, p);
2819 ret = io_uring_get_fd(ctx);
2823 memset(&p->sq_off, 0, sizeof(p->sq_off));
2824 p->sq_off.head = offsetof(struct io_sq_ring, r.head);
2825 p->sq_off.tail = offsetof(struct io_sq_ring, r.tail);
2826 p->sq_off.ring_mask = offsetof(struct io_sq_ring, ring_mask);
2827 p->sq_off.ring_entries = offsetof(struct io_sq_ring, ring_entries);
2828 p->sq_off.flags = offsetof(struct io_sq_ring, flags);
2829 p->sq_off.dropped = offsetof(struct io_sq_ring, dropped);
2830 p->sq_off.array = offsetof(struct io_sq_ring, array);
2832 memset(&p->cq_off, 0, sizeof(p->cq_off));
2833 p->cq_off.head = offsetof(struct io_cq_ring, r.head);
2834 p->cq_off.tail = offsetof(struct io_cq_ring, r.tail);
2835 p->cq_off.ring_mask = offsetof(struct io_cq_ring, ring_mask);
2836 p->cq_off.ring_entries = offsetof(struct io_cq_ring, ring_entries);
2837 p->cq_off.overflow = offsetof(struct io_cq_ring, overflow);
2838 p->cq_off.cqes = offsetof(struct io_cq_ring, cqes);
2841 io_ring_ctx_wait_and_kill(ctx);
2846 * Sets up an aio uring context, and returns the fd. Applications asks for a
2847 * ring size, we return the actual sq/cq ring sizes (among other things) in the
2848 * params structure passed in.
2850 static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
2852 struct io_uring_params p;
2856 if (copy_from_user(&p, params, sizeof(p)))
2858 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
2863 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
2864 IORING_SETUP_SQ_AFF))
2867 ret = io_uring_create(entries, &p);
2871 if (copy_to_user(params, &p, sizeof(p)))
2877 SYSCALL_DEFINE2(io_uring_setup, u32, entries,
2878 struct io_uring_params __user *, params)
2880 return io_uring_setup(entries, params);
2883 static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
2884 void __user *arg, unsigned nr_args)
2888 percpu_ref_kill(&ctx->refs);
2889 wait_for_completion(&ctx->ctx_done);
2892 case IORING_REGISTER_BUFFERS:
2893 ret = io_sqe_buffer_register(ctx, arg, nr_args);
2895 case IORING_UNREGISTER_BUFFERS:
2899 ret = io_sqe_buffer_unregister(ctx);
2901 case IORING_REGISTER_FILES:
2902 ret = io_sqe_files_register(ctx, arg, nr_args);
2904 case IORING_UNREGISTER_FILES:
2908 ret = io_sqe_files_unregister(ctx);
2915 /* bring the ctx back to life */
2916 reinit_completion(&ctx->ctx_done);
2917 percpu_ref_reinit(&ctx->refs);
2921 SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
2922 void __user *, arg, unsigned int, nr_args)
2924 struct io_ring_ctx *ctx;
2933 if (f.file->f_op != &io_uring_fops)
2936 ctx = f.file->private_data;
2938 mutex_lock(&ctx->uring_lock);
2939 ret = __io_uring_register(ctx, opcode, arg, nr_args);
2940 mutex_unlock(&ctx->uring_lock);
2946 static int __init io_uring_init(void)
2948 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
2951 __initcall(io_uring_init);