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
;
99 struct percpu_ref refs
;
100 } ____cacheline_aligned_in_smp
;
108 struct io_sq_ring
*sq_ring
;
109 unsigned cached_sq_head
;
112 unsigned sq_thread_idle
;
113 struct io_uring_sqe
*sq_sqes
;
114 } ____cacheline_aligned_in_smp
;
117 struct workqueue_struct
*sqo_wq
;
118 struct task_struct
*sqo_thread
; /* if using sq thread polling */
119 struct mm_struct
*sqo_mm
;
120 wait_queue_head_t sqo_wait
;
125 struct io_cq_ring
*cq_ring
;
126 unsigned cached_cq_tail
;
129 struct wait_queue_head cq_wait
;
130 struct fasync_struct
*cq_fasync
;
131 } ____cacheline_aligned_in_smp
;
134 * If used, fixed file set. Writers must ensure that ->refs is dead,
135 * readers must ensure that ->refs is alive as long as the file* is
136 * used. Only updated through io_uring_register(2).
138 struct file
**user_files
;
139 unsigned nr_user_files
;
141 /* if used, fixed mapped user buffers */
142 unsigned nr_user_bufs
;
143 struct io_mapped_ubuf
*user_bufs
;
145 struct user_struct
*user
;
147 struct completion ctx_done
;
150 struct mutex uring_lock
;
151 wait_queue_head_t wait
;
152 } ____cacheline_aligned_in_smp
;
155 spinlock_t completion_lock
;
156 bool poll_multi_file
;
158 * ->poll_list is protected by the ctx->uring_lock for
159 * io_uring instances that don't use IORING_SETUP_SQPOLL.
160 * For SQPOLL, only the single threaded io_sq_thread() will
161 * manipulate the list, hence no extra locking is needed there.
163 struct list_head poll_list
;
164 } ____cacheline_aligned_in_smp
;
166 #if defined(CONFIG_UNIX)
167 struct socket
*ring_sock
;
172 const struct io_uring_sqe
*sqe
;
173 unsigned short index
;
176 bool needs_fixed_file
;
182 struct sqe_submit submit
;
184 struct io_ring_ctx
*ctx
;
185 struct list_head list
;
188 #define REQ_F_FORCE_NONBLOCK 1 /* inline submission attempt */
189 #define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
190 #define REQ_F_FIXED_FILE 4 /* ctx owns file */
194 struct work_struct work
;
197 #define IO_PLUG_THRESHOLD 2
198 #define IO_IOPOLL_BATCH 8
200 struct io_submit_state
{
201 struct blk_plug plug
;
204 * io_kiocb alloc cache
206 void *reqs
[IO_IOPOLL_BATCH
];
207 unsigned int free_reqs
;
208 unsigned int cur_req
;
211 * File reference cache
215 unsigned int has_refs
;
216 unsigned int used_refs
;
217 unsigned int ios_left
;
220 static struct kmem_cache
*req_cachep
;
222 static const struct file_operations io_uring_fops
;
224 struct sock
*io_uring_get_socket(struct file
*file
)
226 #if defined(CONFIG_UNIX)
227 if (file
->f_op
== &io_uring_fops
) {
228 struct io_ring_ctx
*ctx
= file
->private_data
;
230 return ctx
->ring_sock
->sk
;
235 EXPORT_SYMBOL(io_uring_get_socket
);
237 static void io_ring_ctx_ref_free(struct percpu_ref
*ref
)
239 struct io_ring_ctx
*ctx
= container_of(ref
, struct io_ring_ctx
, refs
);
241 complete(&ctx
->ctx_done
);
244 static struct io_ring_ctx
*io_ring_ctx_alloc(struct io_uring_params
*p
)
246 struct io_ring_ctx
*ctx
;
248 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
252 if (percpu_ref_init(&ctx
->refs
, io_ring_ctx_ref_free
, 0, GFP_KERNEL
)) {
257 ctx
->flags
= p
->flags
;
258 init_waitqueue_head(&ctx
->cq_wait
);
259 init_completion(&ctx
->ctx_done
);
260 mutex_init(&ctx
->uring_lock
);
261 init_waitqueue_head(&ctx
->wait
);
262 spin_lock_init(&ctx
->completion_lock
);
263 INIT_LIST_HEAD(&ctx
->poll_list
);
267 static void io_commit_cqring(struct io_ring_ctx
*ctx
)
269 struct io_cq_ring
*ring
= ctx
->cq_ring
;
271 if (ctx
->cached_cq_tail
!= READ_ONCE(ring
->r
.tail
)) {
272 /* order cqe stores with ring update */
273 smp_store_release(&ring
->r
.tail
, ctx
->cached_cq_tail
);
276 * Write sider barrier of tail update, app has read side. See
277 * comment at the top of this file.
281 if (wq_has_sleeper(&ctx
->cq_wait
)) {
282 wake_up_interruptible(&ctx
->cq_wait
);
283 kill_fasync(&ctx
->cq_fasync
, SIGIO
, POLL_IN
);
288 static struct io_uring_cqe
*io_get_cqring(struct io_ring_ctx
*ctx
)
290 struct io_cq_ring
*ring
= ctx
->cq_ring
;
293 tail
= ctx
->cached_cq_tail
;
294 /* See comment at the top of the file */
296 if (tail
+ 1 == READ_ONCE(ring
->r
.head
))
299 ctx
->cached_cq_tail
++;
300 return &ring
->cqes
[tail
& ctx
->cq_mask
];
303 static void io_cqring_fill_event(struct io_ring_ctx
*ctx
, u64 ki_user_data
,
304 long res
, unsigned ev_flags
)
306 struct io_uring_cqe
*cqe
;
309 * If we can't get a cq entry, userspace overflowed the
310 * submission (by quite a lot). Increment the overflow count in
313 cqe
= io_get_cqring(ctx
);
315 WRITE_ONCE(cqe
->user_data
, ki_user_data
);
316 WRITE_ONCE(cqe
->res
, res
);
317 WRITE_ONCE(cqe
->flags
, ev_flags
);
319 unsigned overflow
= READ_ONCE(ctx
->cq_ring
->overflow
);
321 WRITE_ONCE(ctx
->cq_ring
->overflow
, overflow
+ 1);
325 static void io_cqring_add_event(struct io_ring_ctx
*ctx
, u64 ki_user_data
,
326 long res
, unsigned ev_flags
)
330 spin_lock_irqsave(&ctx
->completion_lock
, flags
);
331 io_cqring_fill_event(ctx
, ki_user_data
, res
, ev_flags
);
332 io_commit_cqring(ctx
);
333 spin_unlock_irqrestore(&ctx
->completion_lock
, flags
);
335 if (waitqueue_active(&ctx
->wait
))
337 if (waitqueue_active(&ctx
->sqo_wait
))
338 wake_up(&ctx
->sqo_wait
);
341 static void io_ring_drop_ctx_refs(struct io_ring_ctx
*ctx
, unsigned refs
)
343 percpu_ref_put_many(&ctx
->refs
, refs
);
345 if (waitqueue_active(&ctx
->wait
))
349 static struct io_kiocb
*io_get_req(struct io_ring_ctx
*ctx
,
350 struct io_submit_state
*state
)
352 struct io_kiocb
*req
;
354 if (!percpu_ref_tryget(&ctx
->refs
))
358 req
= kmem_cache_alloc(req_cachep
, __GFP_NOWARN
);
361 } else if (!state
->free_reqs
) {
365 sz
= min_t(size_t, state
->ios_left
, ARRAY_SIZE(state
->reqs
));
366 ret
= kmem_cache_alloc_bulk(req_cachep
, __GFP_NOWARN
, sz
,
368 if (unlikely(ret
<= 0))
370 state
->free_reqs
= ret
- 1;
372 req
= state
->reqs
[0];
374 req
= state
->reqs
[state
->cur_req
];
381 refcount_set(&req
->refs
, 0);
384 io_ring_drop_ctx_refs(ctx
, 1);
388 static void io_free_req_many(struct io_ring_ctx
*ctx
, void **reqs
, int *nr
)
391 kmem_cache_free_bulk(req_cachep
, *nr
, reqs
);
392 io_ring_drop_ctx_refs(ctx
, *nr
);
397 static void io_free_req(struct io_kiocb
*req
)
399 if (!refcount_read(&req
->refs
) || refcount_dec_and_test(&req
->refs
)) {
400 io_ring_drop_ctx_refs(req
->ctx
, 1);
401 kmem_cache_free(req_cachep
, req
);
406 * Find and free completed poll iocbs
408 static void io_iopoll_complete(struct io_ring_ctx
*ctx
, unsigned int *nr_events
,
409 struct list_head
*done
)
411 void *reqs
[IO_IOPOLL_BATCH
];
412 int file_count
, to_free
;
413 struct file
*file
= NULL
;
414 struct io_kiocb
*req
;
416 file_count
= to_free
= 0;
417 while (!list_empty(done
)) {
418 req
= list_first_entry(done
, struct io_kiocb
, list
);
419 list_del(&req
->list
);
421 io_cqring_fill_event(ctx
, req
->user_data
, req
->error
, 0);
423 reqs
[to_free
++] = req
;
427 * Batched puts of the same file, to avoid dirtying the
428 * file usage count multiple times, if avoidable.
430 if (!(req
->flags
& REQ_F_FIXED_FILE
)) {
432 file
= req
->rw
.ki_filp
;
434 } else if (file
== req
->rw
.ki_filp
) {
437 fput_many(file
, file_count
);
438 file
= req
->rw
.ki_filp
;
443 if (to_free
== ARRAY_SIZE(reqs
))
444 io_free_req_many(ctx
, reqs
, &to_free
);
446 io_commit_cqring(ctx
);
449 fput_many(file
, file_count
);
450 io_free_req_many(ctx
, reqs
, &to_free
);
453 static int io_do_iopoll(struct io_ring_ctx
*ctx
, unsigned int *nr_events
,
456 struct io_kiocb
*req
, *tmp
;
462 * Only spin for completions if we don't have multiple devices hanging
463 * off our complete list, and we're under the requested amount.
465 spin
= !ctx
->poll_multi_file
&& *nr_events
< min
;
468 list_for_each_entry_safe(req
, tmp
, &ctx
->poll_list
, list
) {
469 struct kiocb
*kiocb
= &req
->rw
;
472 * Move completed entries to our local list. If we find a
473 * request that requires polling, break out and complete
474 * the done list first, if we have entries there.
476 if (req
->flags
& REQ_F_IOPOLL_COMPLETED
) {
477 list_move_tail(&req
->list
, &done
);
480 if (!list_empty(&done
))
483 ret
= kiocb
->ki_filp
->f_op
->iopoll(kiocb
, spin
);
492 if (!list_empty(&done
))
493 io_iopoll_complete(ctx
, nr_events
, &done
);
499 * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a
500 * non-spinning poll check - we'll still enter the driver poll loop, but only
501 * as a non-spinning completion check.
503 static int io_iopoll_getevents(struct io_ring_ctx
*ctx
, unsigned int *nr_events
,
506 while (!list_empty(&ctx
->poll_list
)) {
509 ret
= io_do_iopoll(ctx
, nr_events
, min
);
512 if (!min
|| *nr_events
>= min
)
520 * We can't just wait for polled events to come to us, we have to actively
521 * find and complete them.
523 static void io_iopoll_reap_events(struct io_ring_ctx
*ctx
)
525 if (!(ctx
->flags
& IORING_SETUP_IOPOLL
))
528 mutex_lock(&ctx
->uring_lock
);
529 while (!list_empty(&ctx
->poll_list
)) {
530 unsigned int nr_events
= 0;
532 io_iopoll_getevents(ctx
, &nr_events
, 1);
534 mutex_unlock(&ctx
->uring_lock
);
537 static int io_iopoll_check(struct io_ring_ctx
*ctx
, unsigned *nr_events
,
545 if (*nr_events
< min
)
546 tmin
= min
- *nr_events
;
548 ret
= io_iopoll_getevents(ctx
, nr_events
, tmin
);
552 } while (min
&& !*nr_events
&& !need_resched());
557 static void kiocb_end_write(struct kiocb
*kiocb
)
559 if (kiocb
->ki_flags
& IOCB_WRITE
) {
560 struct inode
*inode
= file_inode(kiocb
->ki_filp
);
563 * Tell lockdep we inherited freeze protection from submission
566 if (S_ISREG(inode
->i_mode
))
567 __sb_writers_acquired(inode
->i_sb
, SB_FREEZE_WRITE
);
568 file_end_write(kiocb
->ki_filp
);
572 static void io_fput(struct io_kiocb
*req
)
574 if (!(req
->flags
& REQ_F_FIXED_FILE
))
575 fput(req
->rw
.ki_filp
);
578 static void io_complete_rw(struct kiocb
*kiocb
, long res
, long res2
)
580 struct io_kiocb
*req
= container_of(kiocb
, struct io_kiocb
, rw
);
582 kiocb_end_write(kiocb
);
585 io_cqring_add_event(req
->ctx
, req
->user_data
, res
, 0);
589 static void io_complete_rw_iopoll(struct kiocb
*kiocb
, long res
, long res2
)
591 struct io_kiocb
*req
= container_of(kiocb
, struct io_kiocb
, rw
);
593 kiocb_end_write(kiocb
);
597 req
->flags
|= REQ_F_IOPOLL_COMPLETED
;
601 * After the iocb has been issued, it's safe to be found on the poll list.
602 * Adding the kiocb to the list AFTER submission ensures that we don't
603 * find it from a io_iopoll_getevents() thread before the issuer is done
604 * accessing the kiocb cookie.
606 static void io_iopoll_req_issued(struct io_kiocb
*req
)
608 struct io_ring_ctx
*ctx
= req
->ctx
;
611 * Track whether we have multiple files in our lists. This will impact
612 * how we do polling eventually, not spinning if we're on potentially
615 if (list_empty(&ctx
->poll_list
)) {
616 ctx
->poll_multi_file
= false;
617 } else if (!ctx
->poll_multi_file
) {
618 struct io_kiocb
*list_req
;
620 list_req
= list_first_entry(&ctx
->poll_list
, struct io_kiocb
,
622 if (list_req
->rw
.ki_filp
!= req
->rw
.ki_filp
)
623 ctx
->poll_multi_file
= true;
627 * For fast devices, IO may have already completed. If it has, add
628 * it to the front so we find it first.
630 if (req
->flags
& REQ_F_IOPOLL_COMPLETED
)
631 list_add(&req
->list
, &ctx
->poll_list
);
633 list_add_tail(&req
->list
, &ctx
->poll_list
);
636 static void io_file_put(struct io_submit_state
*state
, struct file
*file
)
640 } else if (state
->file
) {
641 int diff
= state
->has_refs
- state
->used_refs
;
644 fput_many(state
->file
, diff
);
650 * Get as many references to a file as we have IOs left in this submission,
651 * assuming most submissions are for one file, or at least that each file
652 * has more than one submission.
654 static struct file
*io_file_get(struct io_submit_state
*state
, int fd
)
660 if (state
->fd
== fd
) {
665 io_file_put(state
, NULL
);
667 state
->file
= fget_many(fd
, state
->ios_left
);
672 state
->has_refs
= state
->ios_left
;
673 state
->used_refs
= 1;
679 * If we tracked the file through the SCM inflight mechanism, we could support
680 * any file. For now, just ensure that anything potentially problematic is done
683 static bool io_file_supports_async(struct file
*file
)
685 umode_t mode
= file_inode(file
)->i_mode
;
687 if (S_ISBLK(mode
) || S_ISCHR(mode
))
689 if (S_ISREG(mode
) && file
->f_op
!= &io_uring_fops
)
695 static int io_prep_rw(struct io_kiocb
*req
, const struct sqe_submit
*s
,
696 bool force_nonblock
, struct io_submit_state
*state
)
698 const struct io_uring_sqe
*sqe
= s
->sqe
;
699 struct io_ring_ctx
*ctx
= req
->ctx
;
700 struct kiocb
*kiocb
= &req
->rw
;
701 unsigned ioprio
, flags
;
704 /* For -EAGAIN retry, everything is already prepped */
708 flags
= READ_ONCE(sqe
->flags
);
709 fd
= READ_ONCE(sqe
->fd
);
711 if (flags
& IOSQE_FIXED_FILE
) {
712 if (unlikely(!ctx
->user_files
||
713 (unsigned) fd
>= ctx
->nr_user_files
))
715 kiocb
->ki_filp
= ctx
->user_files
[fd
];
716 req
->flags
|= REQ_F_FIXED_FILE
;
718 if (s
->needs_fixed_file
)
720 kiocb
->ki_filp
= io_file_get(state
, fd
);
721 if (unlikely(!kiocb
->ki_filp
))
723 if (force_nonblock
&& !io_file_supports_async(kiocb
->ki_filp
))
724 force_nonblock
= false;
726 kiocb
->ki_pos
= READ_ONCE(sqe
->off
);
727 kiocb
->ki_flags
= iocb_flags(kiocb
->ki_filp
);
728 kiocb
->ki_hint
= ki_hint_validate(file_write_hint(kiocb
->ki_filp
));
730 ioprio
= READ_ONCE(sqe
->ioprio
);
732 ret
= ioprio_check_cap(ioprio
);
736 kiocb
->ki_ioprio
= ioprio
;
738 kiocb
->ki_ioprio
= get_current_ioprio();
740 ret
= kiocb_set_rw_flags(kiocb
, READ_ONCE(sqe
->rw_flags
));
743 if (force_nonblock
) {
744 kiocb
->ki_flags
|= IOCB_NOWAIT
;
745 req
->flags
|= REQ_F_FORCE_NONBLOCK
;
747 if (ctx
->flags
& IORING_SETUP_IOPOLL
) {
749 if (!(kiocb
->ki_flags
& IOCB_DIRECT
) ||
750 !kiocb
->ki_filp
->f_op
->iopoll
)
754 kiocb
->ki_flags
|= IOCB_HIPRI
;
755 kiocb
->ki_complete
= io_complete_rw_iopoll
;
757 if (kiocb
->ki_flags
& IOCB_HIPRI
) {
761 kiocb
->ki_complete
= io_complete_rw
;
765 if (!(flags
& IOSQE_FIXED_FILE
)) {
767 * in case of error, we didn't use this file reference. drop it.
771 io_file_put(state
, kiocb
->ki_filp
);
776 static inline void io_rw_done(struct kiocb
*kiocb
, ssize_t ret
)
782 case -ERESTARTNOINTR
:
783 case -ERESTARTNOHAND
:
784 case -ERESTART_RESTARTBLOCK
:
786 * We can't just restart the syscall, since previously
787 * submitted sqes may already be in progress. Just fail this
793 kiocb
->ki_complete(kiocb
, ret
, 0);
797 static int io_import_fixed(struct io_ring_ctx
*ctx
, int rw
,
798 const struct io_uring_sqe
*sqe
,
799 struct iov_iter
*iter
)
801 size_t len
= READ_ONCE(sqe
->len
);
802 struct io_mapped_ubuf
*imu
;
803 unsigned index
, buf_index
;
807 /* attempt to use fixed buffers without having provided iovecs */
808 if (unlikely(!ctx
->user_bufs
))
811 buf_index
= READ_ONCE(sqe
->buf_index
);
812 if (unlikely(buf_index
>= ctx
->nr_user_bufs
))
815 index
= array_index_nospec(buf_index
, ctx
->nr_user_bufs
);
816 imu
= &ctx
->user_bufs
[index
];
817 buf_addr
= READ_ONCE(sqe
->addr
);
820 if (buf_addr
+ len
< buf_addr
)
822 /* not inside the mapped region */
823 if (buf_addr
< imu
->ubuf
|| buf_addr
+ len
> imu
->ubuf
+ imu
->len
)
827 * May not be a start of buffer, set size appropriately
828 * and advance us to the beginning.
830 offset
= buf_addr
- imu
->ubuf
;
831 iov_iter_bvec(iter
, rw
, imu
->bvec
, imu
->nr_bvecs
, offset
+ len
);
833 iov_iter_advance(iter
, offset
);
837 static int io_import_iovec(struct io_ring_ctx
*ctx
, int rw
,
838 const struct sqe_submit
*s
, struct iovec
**iovec
,
839 struct iov_iter
*iter
)
841 const struct io_uring_sqe
*sqe
= s
->sqe
;
842 void __user
*buf
= u64_to_user_ptr(READ_ONCE(sqe
->addr
));
843 size_t sqe_len
= READ_ONCE(sqe
->len
);
847 * We're reading ->opcode for the second time, but the first read
848 * doesn't care whether it's _FIXED or not, so it doesn't matter
849 * whether ->opcode changes concurrently. The first read does care
850 * about whether it is a READ or a WRITE, so we don't trust this read
851 * for that purpose and instead let the caller pass in the read/write
854 opcode
= READ_ONCE(sqe
->opcode
);
855 if (opcode
== IORING_OP_READ_FIXED
||
856 opcode
== IORING_OP_WRITE_FIXED
) {
857 ssize_t ret
= io_import_fixed(ctx
, rw
, sqe
, iter
);
867 return compat_import_iovec(rw
, buf
, sqe_len
, UIO_FASTIOV
,
871 return import_iovec(rw
, buf
, sqe_len
, UIO_FASTIOV
, iovec
, iter
);
874 static ssize_t
io_read(struct io_kiocb
*req
, const struct sqe_submit
*s
,
875 bool force_nonblock
, struct io_submit_state
*state
)
877 struct iovec inline_vecs
[UIO_FASTIOV
], *iovec
= inline_vecs
;
878 struct kiocb
*kiocb
= &req
->rw
;
879 struct iov_iter iter
;
883 ret
= io_prep_rw(req
, s
, force_nonblock
, state
);
886 file
= kiocb
->ki_filp
;
889 if (unlikely(!(file
->f_mode
& FMODE_READ
)))
892 if (unlikely(!file
->f_op
->read_iter
))
895 ret
= io_import_iovec(req
->ctx
, READ
, s
, &iovec
, &iter
);
899 ret
= rw_verify_area(READ
, file
, &kiocb
->ki_pos
, iov_iter_count(&iter
));
903 /* Catch -EAGAIN return for forced non-blocking submission */
904 ret2
= call_read_iter(file
, kiocb
, &iter
);
905 if (!force_nonblock
|| ret2
!= -EAGAIN
)
906 io_rw_done(kiocb
, ret2
);
912 /* Hold on to the file for -EAGAIN */
913 if (unlikely(ret
&& ret
!= -EAGAIN
))
918 static ssize_t
io_write(struct io_kiocb
*req
, const struct sqe_submit
*s
,
919 bool force_nonblock
, struct io_submit_state
*state
)
921 struct iovec inline_vecs
[UIO_FASTIOV
], *iovec
= inline_vecs
;
922 struct kiocb
*kiocb
= &req
->rw
;
923 struct iov_iter iter
;
927 ret
= io_prep_rw(req
, s
, force_nonblock
, state
);
930 /* Hold on to the file for -EAGAIN */
931 if (force_nonblock
&& !(kiocb
->ki_flags
& IOCB_DIRECT
))
935 file
= kiocb
->ki_filp
;
936 if (unlikely(!(file
->f_mode
& FMODE_WRITE
)))
939 if (unlikely(!file
->f_op
->write_iter
))
942 ret
= io_import_iovec(req
->ctx
, WRITE
, s
, &iovec
, &iter
);
946 ret
= rw_verify_area(WRITE
, file
, &kiocb
->ki_pos
,
947 iov_iter_count(&iter
));
950 * Open-code file_start_write here to grab freeze protection,
951 * which will be released by another thread in
952 * io_complete_rw(). Fool lockdep by telling it the lock got
953 * released so that it doesn't complain about the held lock when
954 * we return to userspace.
956 if (S_ISREG(file_inode(file
)->i_mode
)) {
957 __sb_start_write(file_inode(file
)->i_sb
,
958 SB_FREEZE_WRITE
, true);
959 __sb_writers_release(file_inode(file
)->i_sb
,
962 kiocb
->ki_flags
|= IOCB_WRITE
;
963 io_rw_done(kiocb
, call_write_iter(file
, kiocb
, &iter
));
973 * IORING_OP_NOP just posts a completion event, nothing else.
975 static int io_nop(struct io_kiocb
*req
, u64 user_data
)
977 struct io_ring_ctx
*ctx
= req
->ctx
;
980 if (unlikely(ctx
->flags
& IORING_SETUP_IOPOLL
))
984 * Twilight zone - it's possible that someone issued an opcode that
985 * has a file attached, then got -EAGAIN on submission, and changed
986 * the sqe before we retried it from async context. Avoid dropping
987 * a file reference for this malicious case, and flag the error.
989 if (req
->rw
.ki_filp
) {
993 io_cqring_add_event(ctx
, user_data
, err
, 0);
998 static int io_prep_fsync(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
)
1000 struct io_ring_ctx
*ctx
= req
->ctx
;
1004 /* Prep already done */
1005 if (req
->rw
.ki_filp
)
1008 if (unlikely(ctx
->flags
& IORING_SETUP_IOPOLL
))
1010 if (unlikely(sqe
->addr
|| sqe
->ioprio
|| sqe
->buf_index
))
1013 fd
= READ_ONCE(sqe
->fd
);
1014 flags
= READ_ONCE(sqe
->flags
);
1016 if (flags
& IOSQE_FIXED_FILE
) {
1017 if (unlikely(!ctx
->user_files
|| fd
>= ctx
->nr_user_files
))
1019 req
->rw
.ki_filp
= ctx
->user_files
[fd
];
1020 req
->flags
|= REQ_F_FIXED_FILE
;
1022 req
->rw
.ki_filp
= fget(fd
);
1023 if (unlikely(!req
->rw
.ki_filp
))
1030 static int io_fsync(struct io_kiocb
*req
, const struct io_uring_sqe
*sqe
,
1031 bool force_nonblock
)
1033 loff_t sqe_off
= READ_ONCE(sqe
->off
);
1034 loff_t sqe_len
= READ_ONCE(sqe
->len
);
1035 loff_t end
= sqe_off
+ sqe_len
;
1036 unsigned fsync_flags
;
1039 fsync_flags
= READ_ONCE(sqe
->fsync_flags
);
1040 if (unlikely(fsync_flags
& ~IORING_FSYNC_DATASYNC
))
1043 ret
= io_prep_fsync(req
, sqe
);
1047 /* fsync always requires a blocking context */
1051 ret
= vfs_fsync_range(req
->rw
.ki_filp
, sqe_off
,
1052 end
> 0 ? end
: LLONG_MAX
,
1053 fsync_flags
& IORING_FSYNC_DATASYNC
);
1056 io_cqring_add_event(req
->ctx
, sqe
->user_data
, ret
, 0);
1061 static int __io_submit_sqe(struct io_ring_ctx
*ctx
, struct io_kiocb
*req
,
1062 const struct sqe_submit
*s
, bool force_nonblock
,
1063 struct io_submit_state
*state
)
1068 if (unlikely(s
->index
>= ctx
->sq_entries
))
1070 req
->user_data
= READ_ONCE(s
->sqe
->user_data
);
1072 opcode
= READ_ONCE(s
->sqe
->opcode
);
1075 ret
= io_nop(req
, req
->user_data
);
1077 case IORING_OP_READV
:
1078 if (unlikely(s
->sqe
->buf_index
))
1080 ret
= io_read(req
, s
, force_nonblock
, state
);
1082 case IORING_OP_WRITEV
:
1083 if (unlikely(s
->sqe
->buf_index
))
1085 ret
= io_write(req
, s
, force_nonblock
, state
);
1087 case IORING_OP_READ_FIXED
:
1088 ret
= io_read(req
, s
, force_nonblock
, state
);
1090 case IORING_OP_WRITE_FIXED
:
1091 ret
= io_write(req
, s
, force_nonblock
, state
);
1093 case IORING_OP_FSYNC
:
1094 ret
= io_fsync(req
, s
->sqe
, force_nonblock
);
1104 if (ctx
->flags
& IORING_SETUP_IOPOLL
) {
1105 if (req
->error
== -EAGAIN
)
1108 /* workqueue context doesn't hold uring_lock, grab it now */
1110 mutex_lock(&ctx
->uring_lock
);
1111 io_iopoll_req_issued(req
);
1113 mutex_unlock(&ctx
->uring_lock
);
1119 static inline bool io_sqe_needs_user(const struct io_uring_sqe
*sqe
)
1121 u8 opcode
= READ_ONCE(sqe
->opcode
);
1123 return !(opcode
== IORING_OP_READ_FIXED
||
1124 opcode
== IORING_OP_WRITE_FIXED
);
1127 static void io_sq_wq_submit_work(struct work_struct
*work
)
1129 struct io_kiocb
*req
= container_of(work
, struct io_kiocb
, work
);
1130 struct sqe_submit
*s
= &req
->submit
;
1131 const struct io_uring_sqe
*sqe
= s
->sqe
;
1132 struct io_ring_ctx
*ctx
= req
->ctx
;
1133 mm_segment_t old_fs
;
1137 /* Ensure we clear previously set forced non-block flag */
1138 req
->flags
&= ~REQ_F_FORCE_NONBLOCK
;
1139 req
->rw
.ki_flags
&= ~IOCB_NOWAIT
;
1141 s
->needs_lock
= true;
1142 s
->has_user
= false;
1145 * If we're doing IO to fixed buffers, we don't need to get/set
1148 needs_user
= io_sqe_needs_user(s
->sqe
);
1150 if (!mmget_not_zero(ctx
->sqo_mm
)) {
1154 use_mm(ctx
->sqo_mm
);
1161 ret
= __io_submit_sqe(ctx
, req
, s
, false, NULL
);
1163 * We can get EAGAIN for polled IO even though we're forcing
1164 * a sync submission from here, since we can't wait for
1165 * request slots on the block side.
1174 unuse_mm(ctx
->sqo_mm
);
1179 io_cqring_add_event(ctx
, sqe
->user_data
, ret
, 0);
1183 /* async context always use a copy of the sqe */
1187 static int io_submit_sqe(struct io_ring_ctx
*ctx
, struct sqe_submit
*s
,
1188 struct io_submit_state
*state
)
1190 struct io_kiocb
*req
;
1193 /* enforce forwards compatibility on users */
1194 if (unlikely(s
->sqe
->flags
& ~IOSQE_FIXED_FILE
))
1197 req
= io_get_req(ctx
, state
);
1201 req
->rw
.ki_filp
= NULL
;
1203 ret
= __io_submit_sqe(ctx
, req
, s
, true, state
);
1204 if (ret
== -EAGAIN
) {
1205 struct io_uring_sqe
*sqe_copy
;
1207 sqe_copy
= kmalloc(sizeof(*sqe_copy
), GFP_KERNEL
);
1209 memcpy(sqe_copy
, s
->sqe
, sizeof(*sqe_copy
));
1212 memcpy(&req
->submit
, s
, sizeof(*s
));
1213 INIT_WORK(&req
->work
, io_sq_wq_submit_work
);
1214 queue_work(ctx
->sqo_wq
, &req
->work
);
1225 * Batched submission is done, ensure local IO is flushed out.
1227 static void io_submit_state_end(struct io_submit_state
*state
)
1229 blk_finish_plug(&state
->plug
);
1230 io_file_put(state
, NULL
);
1231 if (state
->free_reqs
)
1232 kmem_cache_free_bulk(req_cachep
, state
->free_reqs
,
1233 &state
->reqs
[state
->cur_req
]);
1237 * Start submission side cache.
1239 static void io_submit_state_start(struct io_submit_state
*state
,
1240 struct io_ring_ctx
*ctx
, unsigned max_ios
)
1242 blk_start_plug(&state
->plug
);
1243 state
->free_reqs
= 0;
1245 state
->ios_left
= max_ios
;
1248 static void io_commit_sqring(struct io_ring_ctx
*ctx
)
1250 struct io_sq_ring
*ring
= ctx
->sq_ring
;
1252 if (ctx
->cached_sq_head
!= READ_ONCE(ring
->r
.head
)) {
1254 * Ensure any loads from the SQEs are done at this point,
1255 * since once we write the new head, the application could
1256 * write new data to them.
1258 smp_store_release(&ring
->r
.head
, ctx
->cached_sq_head
);
1261 * write side barrier of head update, app has read side. See
1262 * comment at the top of this file
1269 * Undo last io_get_sqring()
1271 static void io_drop_sqring(struct io_ring_ctx
*ctx
)
1273 ctx
->cached_sq_head
--;
1277 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
1278 * that is mapped by userspace. This means that care needs to be taken to
1279 * ensure that reads are stable, as we cannot rely on userspace always
1280 * being a good citizen. If members of the sqe are validated and then later
1281 * used, it's important that those reads are done through READ_ONCE() to
1282 * prevent a re-load down the line.
1284 static bool io_get_sqring(struct io_ring_ctx
*ctx
, struct sqe_submit
*s
)
1286 struct io_sq_ring
*ring
= ctx
->sq_ring
;
1290 * The cached sq head (or cq tail) serves two purposes:
1292 * 1) allows us to batch the cost of updating the user visible
1294 * 2) allows the kernel side to track the head on its own, even
1295 * though the application is the one updating it.
1297 head
= ctx
->cached_sq_head
;
1298 /* See comment at the top of this file */
1300 if (head
== READ_ONCE(ring
->r
.tail
))
1303 head
= READ_ONCE(ring
->array
[head
& ctx
->sq_mask
]);
1304 if (head
< ctx
->sq_entries
) {
1306 s
->sqe
= &ctx
->sq_sqes
[head
];
1307 ctx
->cached_sq_head
++;
1311 /* drop invalid entries */
1312 ctx
->cached_sq_head
++;
1314 /* See comment at the top of this file */
1319 static int io_submit_sqes(struct io_ring_ctx
*ctx
, struct sqe_submit
*sqes
,
1320 unsigned int nr
, bool has_user
, bool mm_fault
)
1322 struct io_submit_state state
, *statep
= NULL
;
1323 int ret
, i
, submitted
= 0;
1325 if (nr
> IO_PLUG_THRESHOLD
) {
1326 io_submit_state_start(&state
, ctx
, nr
);
1330 for (i
= 0; i
< nr
; i
++) {
1331 if (unlikely(mm_fault
)) {
1334 sqes
[i
].has_user
= has_user
;
1335 sqes
[i
].needs_lock
= true;
1336 sqes
[i
].needs_fixed_file
= true;
1337 ret
= io_submit_sqe(ctx
, &sqes
[i
], statep
);
1344 io_cqring_add_event(ctx
, sqes
[i
].sqe
->user_data
, ret
, 0);
1348 io_submit_state_end(&state
);
1353 static int io_sq_thread(void *data
)
1355 struct sqe_submit sqes
[IO_IOPOLL_BATCH
];
1356 struct io_ring_ctx
*ctx
= data
;
1357 struct mm_struct
*cur_mm
= NULL
;
1358 mm_segment_t old_fs
;
1361 unsigned long timeout
;
1366 timeout
= inflight
= 0;
1367 while (!kthread_should_stop() && !ctx
->sqo_stop
) {
1368 bool all_fixed
, mm_fault
= false;
1372 unsigned nr_events
= 0;
1374 if (ctx
->flags
& IORING_SETUP_IOPOLL
) {
1376 * We disallow the app entering submit/complete
1377 * with polling, but we still need to lock the
1378 * ring to prevent racing with polled issue
1379 * that got punted to a workqueue.
1381 mutex_lock(&ctx
->uring_lock
);
1382 io_iopoll_check(ctx
, &nr_events
, 0);
1383 mutex_unlock(&ctx
->uring_lock
);
1386 * Normal IO, just pretend everything completed.
1387 * We don't have to poll completions for that.
1389 nr_events
= inflight
;
1392 inflight
-= nr_events
;
1394 timeout
= jiffies
+ ctx
->sq_thread_idle
;
1397 if (!io_get_sqring(ctx
, &sqes
[0])) {
1399 * We're polling. If we're within the defined idle
1400 * period, then let us spin without work before going
1403 if (inflight
|| !time_after(jiffies
, timeout
)) {
1409 * Drop cur_mm before scheduling, we can't hold it for
1410 * long periods (or over schedule()). Do this before
1411 * adding ourselves to the waitqueue, as the unuse/drop
1420 prepare_to_wait(&ctx
->sqo_wait
, &wait
,
1421 TASK_INTERRUPTIBLE
);
1423 /* Tell userspace we may need a wakeup call */
1424 ctx
->sq_ring
->flags
|= IORING_SQ_NEED_WAKEUP
;
1427 if (!io_get_sqring(ctx
, &sqes
[0])) {
1428 if (kthread_should_stop()) {
1429 finish_wait(&ctx
->sqo_wait
, &wait
);
1432 if (signal_pending(current
))
1433 flush_signals(current
);
1435 finish_wait(&ctx
->sqo_wait
, &wait
);
1437 ctx
->sq_ring
->flags
&= ~IORING_SQ_NEED_WAKEUP
;
1441 finish_wait(&ctx
->sqo_wait
, &wait
);
1443 ctx
->sq_ring
->flags
&= ~IORING_SQ_NEED_WAKEUP
;
1450 if (all_fixed
&& io_sqe_needs_user(sqes
[i
].sqe
))
1454 if (i
== ARRAY_SIZE(sqes
))
1456 } while (io_get_sqring(ctx
, &sqes
[i
]));
1458 /* Unless all new commands are FIXED regions, grab mm */
1459 if (!all_fixed
&& !cur_mm
) {
1460 mm_fault
= !mmget_not_zero(ctx
->sqo_mm
);
1462 use_mm(ctx
->sqo_mm
);
1463 cur_mm
= ctx
->sqo_mm
;
1467 inflight
+= io_submit_sqes(ctx
, sqes
, i
, cur_mm
!= NULL
,
1470 /* Commit SQ ring head once we've consumed all SQEs */
1471 io_commit_sqring(ctx
);
1482 static int io_ring_submit(struct io_ring_ctx
*ctx
, unsigned int to_submit
)
1484 struct io_submit_state state
, *statep
= NULL
;
1485 int i
, ret
= 0, submit
= 0;
1487 if (to_submit
> IO_PLUG_THRESHOLD
) {
1488 io_submit_state_start(&state
, ctx
, to_submit
);
1492 for (i
= 0; i
< to_submit
; i
++) {
1493 struct sqe_submit s
;
1495 if (!io_get_sqring(ctx
, &s
))
1499 s
.needs_lock
= false;
1500 s
.needs_fixed_file
= false;
1502 ret
= io_submit_sqe(ctx
, &s
, statep
);
1504 io_drop_sqring(ctx
);
1510 io_commit_sqring(ctx
);
1513 io_submit_state_end(statep
);
1515 return submit
? submit
: ret
;
1518 static unsigned io_cqring_events(struct io_cq_ring
*ring
)
1520 return READ_ONCE(ring
->r
.tail
) - READ_ONCE(ring
->r
.head
);
1524 * Wait until events become available, if we don't already have some. The
1525 * application must reap them itself, as they reside on the shared cq ring.
1527 static int io_cqring_wait(struct io_ring_ctx
*ctx
, int min_events
,
1528 const sigset_t __user
*sig
, size_t sigsz
)
1530 struct io_cq_ring
*ring
= ctx
->cq_ring
;
1531 sigset_t ksigmask
, sigsaved
;
1535 /* See comment at the top of this file */
1537 if (io_cqring_events(ring
) >= min_events
)
1541 ret
= set_user_sigmask(sig
, &ksigmask
, &sigsaved
, sigsz
);
1547 prepare_to_wait(&ctx
->wait
, &wait
, TASK_INTERRUPTIBLE
);
1550 /* See comment at the top of this file */
1552 if (io_cqring_events(ring
) >= min_events
)
1558 if (signal_pending(current
))
1562 finish_wait(&ctx
->wait
, &wait
);
1565 restore_user_sigmask(sig
, &sigsaved
);
1567 return READ_ONCE(ring
->r
.head
) == READ_ONCE(ring
->r
.tail
) ? ret
: 0;
1570 static void __io_sqe_files_unregister(struct io_ring_ctx
*ctx
)
1572 #if defined(CONFIG_UNIX)
1573 if (ctx
->ring_sock
) {
1574 struct sock
*sock
= ctx
->ring_sock
->sk
;
1575 struct sk_buff
*skb
;
1577 while ((skb
= skb_dequeue(&sock
->sk_receive_queue
)) != NULL
)
1583 for (i
= 0; i
< ctx
->nr_user_files
; i
++)
1584 fput(ctx
->user_files
[i
]);
1588 static int io_sqe_files_unregister(struct io_ring_ctx
*ctx
)
1590 if (!ctx
->user_files
)
1593 __io_sqe_files_unregister(ctx
);
1594 kfree(ctx
->user_files
);
1595 ctx
->user_files
= NULL
;
1596 ctx
->nr_user_files
= 0;
1600 static void io_sq_thread_stop(struct io_ring_ctx
*ctx
)
1602 if (ctx
->sqo_thread
) {
1605 kthread_stop(ctx
->sqo_thread
);
1606 ctx
->sqo_thread
= NULL
;
1610 static void io_finish_async(struct io_ring_ctx
*ctx
)
1612 io_sq_thread_stop(ctx
);
1615 destroy_workqueue(ctx
->sqo_wq
);
1620 #if defined(CONFIG_UNIX)
1621 static void io_destruct_skb(struct sk_buff
*skb
)
1623 struct io_ring_ctx
*ctx
= skb
->sk
->sk_user_data
;
1625 io_finish_async(ctx
);
1626 unix_destruct_scm(skb
);
1630 * Ensure the UNIX gc is aware of our file set, so we are certain that
1631 * the io_uring can be safely unregistered on process exit, even if we have
1632 * loops in the file referencing.
1634 static int __io_sqe_files_scm(struct io_ring_ctx
*ctx
, int nr
, int offset
)
1636 struct sock
*sk
= ctx
->ring_sock
->sk
;
1637 struct scm_fp_list
*fpl
;
1638 struct sk_buff
*skb
;
1641 if (!capable(CAP_SYS_RESOURCE
) && !capable(CAP_SYS_ADMIN
)) {
1642 unsigned long inflight
= ctx
->user
->unix_inflight
+ nr
;
1644 if (inflight
> task_rlimit(current
, RLIMIT_NOFILE
))
1648 fpl
= kzalloc(sizeof(*fpl
), GFP_KERNEL
);
1652 skb
= alloc_skb(0, GFP_KERNEL
);
1659 skb
->destructor
= io_destruct_skb
;
1661 fpl
->user
= get_uid(ctx
->user
);
1662 for (i
= 0; i
< nr
; i
++) {
1663 fpl
->fp
[i
] = get_file(ctx
->user_files
[i
+ offset
]);
1664 unix_inflight(fpl
->user
, fpl
->fp
[i
]);
1667 fpl
->max
= fpl
->count
= nr
;
1668 UNIXCB(skb
).fp
= fpl
;
1669 refcount_add(skb
->truesize
, &sk
->sk_wmem_alloc
);
1670 skb_queue_head(&sk
->sk_receive_queue
, skb
);
1672 for (i
= 0; i
< nr
; i
++)
1679 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
1680 * causes regular reference counting to break down. We rely on the UNIX
1681 * garbage collection to take care of this problem for us.
1683 static int io_sqe_files_scm(struct io_ring_ctx
*ctx
)
1685 unsigned left
, total
;
1689 left
= ctx
->nr_user_files
;
1691 unsigned this_files
= min_t(unsigned, left
, SCM_MAX_FD
);
1694 ret
= __io_sqe_files_scm(ctx
, this_files
, total
);
1698 total
+= this_files
;
1704 while (total
< ctx
->nr_user_files
) {
1705 fput(ctx
->user_files
[total
]);
1712 static int io_sqe_files_scm(struct io_ring_ctx
*ctx
)
1718 static int io_sqe_files_register(struct io_ring_ctx
*ctx
, void __user
*arg
,
1721 __s32 __user
*fds
= (__s32 __user
*) arg
;
1725 if (ctx
->user_files
)
1729 if (nr_args
> IORING_MAX_FIXED_FILES
)
1732 ctx
->user_files
= kcalloc(nr_args
, sizeof(struct file
*), GFP_KERNEL
);
1733 if (!ctx
->user_files
)
1736 for (i
= 0; i
< nr_args
; i
++) {
1738 if (copy_from_user(&fd
, &fds
[i
], sizeof(fd
)))
1741 ctx
->user_files
[i
] = fget(fd
);
1744 if (!ctx
->user_files
[i
])
1747 * Don't allow io_uring instances to be registered. If UNIX
1748 * isn't enabled, then this causes a reference cycle and this
1749 * instance can never get freed. If UNIX is enabled we'll
1750 * handle it just fine, but there's still no point in allowing
1751 * a ring fd as it doesn't support regular read/write anyway.
1753 if (ctx
->user_files
[i
]->f_op
== &io_uring_fops
) {
1754 fput(ctx
->user_files
[i
]);
1757 ctx
->nr_user_files
++;
1762 for (i
= 0; i
< ctx
->nr_user_files
; i
++)
1763 fput(ctx
->user_files
[i
]);
1765 kfree(ctx
->user_files
);
1766 ctx
->nr_user_files
= 0;
1770 ret
= io_sqe_files_scm(ctx
);
1772 io_sqe_files_unregister(ctx
);
1777 static int io_sq_offload_start(struct io_ring_ctx
*ctx
,
1778 struct io_uring_params
*p
)
1782 init_waitqueue_head(&ctx
->sqo_wait
);
1783 mmgrab(current
->mm
);
1784 ctx
->sqo_mm
= current
->mm
;
1786 ctx
->sq_thread_idle
= msecs_to_jiffies(p
->sq_thread_idle
);
1787 if (!ctx
->sq_thread_idle
)
1788 ctx
->sq_thread_idle
= HZ
;
1791 if (!cpu_possible(p
->sq_thread_cpu
))
1794 if (ctx
->flags
& IORING_SETUP_SQPOLL
) {
1795 if (p
->flags
& IORING_SETUP_SQ_AFF
) {
1798 cpu
= array_index_nospec(p
->sq_thread_cpu
, NR_CPUS
);
1799 ctx
->sqo_thread
= kthread_create_on_cpu(io_sq_thread
,
1803 ctx
->sqo_thread
= kthread_create(io_sq_thread
, ctx
,
1806 if (IS_ERR(ctx
->sqo_thread
)) {
1807 ret
= PTR_ERR(ctx
->sqo_thread
);
1808 ctx
->sqo_thread
= NULL
;
1811 wake_up_process(ctx
->sqo_thread
);
1812 } else if (p
->flags
& IORING_SETUP_SQ_AFF
) {
1813 /* Can't have SQ_AFF without SQPOLL */
1818 /* Do QD, or 2 * CPUS, whatever is smallest */
1819 ctx
->sqo_wq
= alloc_workqueue("io_ring-wq", WQ_UNBOUND
| WQ_FREEZABLE
,
1820 min(ctx
->sq_entries
- 1, 2 * num_online_cpus()));
1828 io_sq_thread_stop(ctx
);
1829 mmdrop(ctx
->sqo_mm
);
1834 static void io_unaccount_mem(struct user_struct
*user
, unsigned long nr_pages
)
1836 atomic_long_sub(nr_pages
, &user
->locked_vm
);
1839 static int io_account_mem(struct user_struct
*user
, unsigned long nr_pages
)
1841 unsigned long page_limit
, cur_pages
, new_pages
;
1843 /* Don't allow more pages than we can safely lock */
1844 page_limit
= rlimit(RLIMIT_MEMLOCK
) >> PAGE_SHIFT
;
1847 cur_pages
= atomic_long_read(&user
->locked_vm
);
1848 new_pages
= cur_pages
+ nr_pages
;
1849 if (new_pages
> page_limit
)
1851 } while (atomic_long_cmpxchg(&user
->locked_vm
, cur_pages
,
1852 new_pages
) != cur_pages
);
1857 static void io_mem_free(void *ptr
)
1859 struct page
*page
= virt_to_head_page(ptr
);
1861 if (put_page_testzero(page
))
1862 free_compound_page(page
);
1865 static void *io_mem_alloc(size_t size
)
1867 gfp_t gfp_flags
= GFP_KERNEL
| __GFP_ZERO
| __GFP_NOWARN
| __GFP_COMP
|
1870 return (void *) __get_free_pages(gfp_flags
, get_order(size
));
1873 static unsigned long ring_pages(unsigned sq_entries
, unsigned cq_entries
)
1875 struct io_sq_ring
*sq_ring
;
1876 struct io_cq_ring
*cq_ring
;
1879 bytes
= struct_size(sq_ring
, array
, sq_entries
);
1880 bytes
+= array_size(sizeof(struct io_uring_sqe
), sq_entries
);
1881 bytes
+= struct_size(cq_ring
, cqes
, cq_entries
);
1883 return (bytes
+ PAGE_SIZE
- 1) / PAGE_SIZE
;
1886 static int io_sqe_buffer_unregister(struct io_ring_ctx
*ctx
)
1890 if (!ctx
->user_bufs
)
1893 for (i
= 0; i
< ctx
->nr_user_bufs
; i
++) {
1894 struct io_mapped_ubuf
*imu
= &ctx
->user_bufs
[i
];
1896 for (j
= 0; j
< imu
->nr_bvecs
; j
++)
1897 put_page(imu
->bvec
[j
].bv_page
);
1899 if (ctx
->account_mem
)
1900 io_unaccount_mem(ctx
->user
, imu
->nr_bvecs
);
1905 kfree(ctx
->user_bufs
);
1906 ctx
->user_bufs
= NULL
;
1907 ctx
->nr_user_bufs
= 0;
1911 static int io_copy_iov(struct io_ring_ctx
*ctx
, struct iovec
*dst
,
1912 void __user
*arg
, unsigned index
)
1914 struct iovec __user
*src
;
1916 #ifdef CONFIG_COMPAT
1918 struct compat_iovec __user
*ciovs
;
1919 struct compat_iovec ciov
;
1921 ciovs
= (struct compat_iovec __user
*) arg
;
1922 if (copy_from_user(&ciov
, &ciovs
[index
], sizeof(ciov
)))
1925 dst
->iov_base
= (void __user
*) (unsigned long) ciov
.iov_base
;
1926 dst
->iov_len
= ciov
.iov_len
;
1930 src
= (struct iovec __user
*) arg
;
1931 if (copy_from_user(dst
, &src
[index
], sizeof(*dst
)))
1936 static int io_sqe_buffer_register(struct io_ring_ctx
*ctx
, void __user
*arg
,
1939 struct vm_area_struct
**vmas
= NULL
;
1940 struct page
**pages
= NULL
;
1941 int i
, j
, got_pages
= 0;
1946 if (!nr_args
|| nr_args
> UIO_MAXIOV
)
1949 ctx
->user_bufs
= kcalloc(nr_args
, sizeof(struct io_mapped_ubuf
),
1951 if (!ctx
->user_bufs
)
1954 for (i
= 0; i
< nr_args
; i
++) {
1955 struct io_mapped_ubuf
*imu
= &ctx
->user_bufs
[i
];
1956 unsigned long off
, start
, end
, ubuf
;
1961 ret
= io_copy_iov(ctx
, &iov
, arg
, i
);
1966 * Don't impose further limits on the size and buffer
1967 * constraints here, we'll -EINVAL later when IO is
1968 * submitted if they are wrong.
1971 if (!iov
.iov_base
|| !iov
.iov_len
)
1974 /* arbitrary limit, but we need something */
1975 if (iov
.iov_len
> SZ_1G
)
1978 ubuf
= (unsigned long) iov
.iov_base
;
1979 end
= (ubuf
+ iov
.iov_len
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1980 start
= ubuf
>> PAGE_SHIFT
;
1981 nr_pages
= end
- start
;
1983 if (ctx
->account_mem
) {
1984 ret
= io_account_mem(ctx
->user
, nr_pages
);
1990 if (!pages
|| nr_pages
> got_pages
) {
1993 pages
= kmalloc_array(nr_pages
, sizeof(struct page
*),
1995 vmas
= kmalloc_array(nr_pages
,
1996 sizeof(struct vm_area_struct
*),
1998 if (!pages
|| !vmas
) {
2000 if (ctx
->account_mem
)
2001 io_unaccount_mem(ctx
->user
, nr_pages
);
2004 got_pages
= nr_pages
;
2007 imu
->bvec
= kmalloc_array(nr_pages
, sizeof(struct bio_vec
),
2011 if (ctx
->account_mem
)
2012 io_unaccount_mem(ctx
->user
, nr_pages
);
2017 down_read(¤t
->mm
->mmap_sem
);
2018 pret
= get_user_pages_longterm(ubuf
, nr_pages
, FOLL_WRITE
,
2020 if (pret
== nr_pages
) {
2021 /* don't support file backed memory */
2022 for (j
= 0; j
< nr_pages
; j
++) {
2023 struct vm_area_struct
*vma
= vmas
[j
];
2026 !is_file_hugepages(vma
->vm_file
)) {
2032 ret
= pret
< 0 ? pret
: -EFAULT
;
2034 up_read(¤t
->mm
->mmap_sem
);
2037 * if we did partial map, or found file backed vmas,
2038 * release any pages we did get
2041 for (j
= 0; j
< pret
; j
++)
2044 if (ctx
->account_mem
)
2045 io_unaccount_mem(ctx
->user
, nr_pages
);
2049 off
= ubuf
& ~PAGE_MASK
;
2051 for (j
= 0; j
< nr_pages
; j
++) {
2054 vec_len
= min_t(size_t, size
, PAGE_SIZE
- off
);
2055 imu
->bvec
[j
].bv_page
= pages
[j
];
2056 imu
->bvec
[j
].bv_len
= vec_len
;
2057 imu
->bvec
[j
].bv_offset
= off
;
2061 /* store original address for later verification */
2063 imu
->len
= iov
.iov_len
;
2064 imu
->nr_bvecs
= nr_pages
;
2066 ctx
->nr_user_bufs
++;
2074 io_sqe_buffer_unregister(ctx
);
2078 static void io_ring_ctx_free(struct io_ring_ctx
*ctx
)
2080 io_finish_async(ctx
);
2082 mmdrop(ctx
->sqo_mm
);
2084 io_iopoll_reap_events(ctx
);
2085 io_sqe_buffer_unregister(ctx
);
2086 io_sqe_files_unregister(ctx
);
2088 #if defined(CONFIG_UNIX)
2090 sock_release(ctx
->ring_sock
);
2093 io_mem_free(ctx
->sq_ring
);
2094 io_mem_free(ctx
->sq_sqes
);
2095 io_mem_free(ctx
->cq_ring
);
2097 percpu_ref_exit(&ctx
->refs
);
2098 if (ctx
->account_mem
)
2099 io_unaccount_mem(ctx
->user
,
2100 ring_pages(ctx
->sq_entries
, ctx
->cq_entries
));
2101 free_uid(ctx
->user
);
2105 static __poll_t
io_uring_poll(struct file
*file
, poll_table
*wait
)
2107 struct io_ring_ctx
*ctx
= file
->private_data
;
2110 poll_wait(file
, &ctx
->cq_wait
, wait
);
2111 /* See comment at the top of this file */
2113 if (READ_ONCE(ctx
->sq_ring
->r
.tail
) + 1 != ctx
->cached_sq_head
)
2114 mask
|= EPOLLOUT
| EPOLLWRNORM
;
2115 if (READ_ONCE(ctx
->cq_ring
->r
.head
) != ctx
->cached_cq_tail
)
2116 mask
|= EPOLLIN
| EPOLLRDNORM
;
2121 static int io_uring_fasync(int fd
, struct file
*file
, int on
)
2123 struct io_ring_ctx
*ctx
= file
->private_data
;
2125 return fasync_helper(fd
, file
, on
, &ctx
->cq_fasync
);
2128 static void io_ring_ctx_wait_and_kill(struct io_ring_ctx
*ctx
)
2130 mutex_lock(&ctx
->uring_lock
);
2131 percpu_ref_kill(&ctx
->refs
);
2132 mutex_unlock(&ctx
->uring_lock
);
2134 io_iopoll_reap_events(ctx
);
2135 wait_for_completion(&ctx
->ctx_done
);
2136 io_ring_ctx_free(ctx
);
2139 static int io_uring_release(struct inode
*inode
, struct file
*file
)
2141 struct io_ring_ctx
*ctx
= file
->private_data
;
2143 file
->private_data
= NULL
;
2144 io_ring_ctx_wait_and_kill(ctx
);
2148 static int io_uring_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2150 loff_t offset
= (loff_t
) vma
->vm_pgoff
<< PAGE_SHIFT
;
2151 unsigned long sz
= vma
->vm_end
- vma
->vm_start
;
2152 struct io_ring_ctx
*ctx
= file
->private_data
;
2158 case IORING_OFF_SQ_RING
:
2161 case IORING_OFF_SQES
:
2164 case IORING_OFF_CQ_RING
:
2171 page
= virt_to_head_page(ptr
);
2172 if (sz
> (PAGE_SIZE
<< compound_order(page
)))
2175 pfn
= virt_to_phys(ptr
) >> PAGE_SHIFT
;
2176 return remap_pfn_range(vma
, vma
->vm_start
, pfn
, sz
, vma
->vm_page_prot
);
2179 SYSCALL_DEFINE6(io_uring_enter
, unsigned int, fd
, u32
, to_submit
,
2180 u32
, min_complete
, u32
, flags
, const sigset_t __user
*, sig
,
2183 struct io_ring_ctx
*ctx
;
2188 if (flags
& ~(IORING_ENTER_GETEVENTS
| IORING_ENTER_SQ_WAKEUP
))
2196 if (f
.file
->f_op
!= &io_uring_fops
)
2200 ctx
= f
.file
->private_data
;
2201 if (!percpu_ref_tryget(&ctx
->refs
))
2205 * For SQ polling, the thread will do all submissions and completions.
2206 * Just return the requested submit count, and wake the thread if
2209 if (ctx
->flags
& IORING_SETUP_SQPOLL
) {
2210 if (flags
& IORING_ENTER_SQ_WAKEUP
)
2211 wake_up(&ctx
->sqo_wait
);
2212 submitted
= to_submit
;
2218 to_submit
= min(to_submit
, ctx
->sq_entries
);
2220 mutex_lock(&ctx
->uring_lock
);
2221 submitted
= io_ring_submit(ctx
, to_submit
);
2222 mutex_unlock(&ctx
->uring_lock
);
2227 if (flags
& IORING_ENTER_GETEVENTS
) {
2228 unsigned nr_events
= 0;
2230 min_complete
= min(min_complete
, ctx
->cq_entries
);
2233 * The application could have included the 'to_submit' count
2234 * in how many events it wanted to wait for. If we failed to
2235 * submit the desired count, we may need to adjust the number
2236 * of events to poll/wait for.
2238 if (submitted
< to_submit
)
2239 min_complete
= min_t(unsigned, submitted
, min_complete
);
2241 if (ctx
->flags
& IORING_SETUP_IOPOLL
) {
2242 mutex_lock(&ctx
->uring_lock
);
2243 ret
= io_iopoll_check(ctx
, &nr_events
, min_complete
);
2244 mutex_unlock(&ctx
->uring_lock
);
2246 ret
= io_cqring_wait(ctx
, min_complete
, sig
, sigsz
);
2251 io_ring_drop_ctx_refs(ctx
, 1);
2254 return submitted
? submitted
: ret
;
2257 static const struct file_operations io_uring_fops
= {
2258 .release
= io_uring_release
,
2259 .mmap
= io_uring_mmap
,
2260 .poll
= io_uring_poll
,
2261 .fasync
= io_uring_fasync
,
2264 static int io_allocate_scq_urings(struct io_ring_ctx
*ctx
,
2265 struct io_uring_params
*p
)
2267 struct io_sq_ring
*sq_ring
;
2268 struct io_cq_ring
*cq_ring
;
2271 sq_ring
= io_mem_alloc(struct_size(sq_ring
, array
, p
->sq_entries
));
2275 ctx
->sq_ring
= sq_ring
;
2276 sq_ring
->ring_mask
= p
->sq_entries
- 1;
2277 sq_ring
->ring_entries
= p
->sq_entries
;
2278 ctx
->sq_mask
= sq_ring
->ring_mask
;
2279 ctx
->sq_entries
= sq_ring
->ring_entries
;
2281 size
= array_size(sizeof(struct io_uring_sqe
), p
->sq_entries
);
2282 if (size
== SIZE_MAX
)
2285 ctx
->sq_sqes
= io_mem_alloc(size
);
2286 if (!ctx
->sq_sqes
) {
2287 io_mem_free(ctx
->sq_ring
);
2291 cq_ring
= io_mem_alloc(struct_size(cq_ring
, cqes
, p
->cq_entries
));
2293 io_mem_free(ctx
->sq_ring
);
2294 io_mem_free(ctx
->sq_sqes
);
2298 ctx
->cq_ring
= cq_ring
;
2299 cq_ring
->ring_mask
= p
->cq_entries
- 1;
2300 cq_ring
->ring_entries
= p
->cq_entries
;
2301 ctx
->cq_mask
= cq_ring
->ring_mask
;
2302 ctx
->cq_entries
= cq_ring
->ring_entries
;
2307 * Allocate an anonymous fd, this is what constitutes the application
2308 * visible backing of an io_uring instance. The application mmaps this
2309 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
2310 * we have to tie this fd to a socket for file garbage collection purposes.
2312 static int io_uring_get_fd(struct io_ring_ctx
*ctx
)
2317 #if defined(CONFIG_UNIX)
2318 ret
= sock_create_kern(&init_net
, PF_UNIX
, SOCK_RAW
, IPPROTO_IP
,
2324 ret
= get_unused_fd_flags(O_RDWR
| O_CLOEXEC
);
2328 file
= anon_inode_getfile("[io_uring]", &io_uring_fops
, ctx
,
2329 O_RDWR
| O_CLOEXEC
);
2332 ret
= PTR_ERR(file
);
2336 #if defined(CONFIG_UNIX)
2337 ctx
->ring_sock
->file
= file
;
2338 ctx
->ring_sock
->sk
->sk_user_data
= ctx
;
2340 fd_install(ret
, file
);
2343 #if defined(CONFIG_UNIX)
2344 sock_release(ctx
->ring_sock
);
2345 ctx
->ring_sock
= NULL
;
2350 static int io_uring_create(unsigned entries
, struct io_uring_params
*p
)
2352 struct user_struct
*user
= NULL
;
2353 struct io_ring_ctx
*ctx
;
2357 if (!entries
|| entries
> IORING_MAX_ENTRIES
)
2361 * Use twice as many entries for the CQ ring. It's possible for the
2362 * application to drive a higher depth than the size of the SQ ring,
2363 * since the sqes are only used at submission time. This allows for
2364 * some flexibility in overcommitting a bit.
2366 p
->sq_entries
= roundup_pow_of_two(entries
);
2367 p
->cq_entries
= 2 * p
->sq_entries
;
2369 user
= get_uid(current_user());
2370 account_mem
= !capable(CAP_IPC_LOCK
);
2373 ret
= io_account_mem(user
,
2374 ring_pages(p
->sq_entries
, p
->cq_entries
));
2381 ctx
= io_ring_ctx_alloc(p
);
2384 io_unaccount_mem(user
, ring_pages(p
->sq_entries
,
2389 ctx
->compat
= in_compat_syscall();
2390 ctx
->account_mem
= account_mem
;
2393 ret
= io_allocate_scq_urings(ctx
, p
);
2397 ret
= io_sq_offload_start(ctx
, p
);
2401 ret
= io_uring_get_fd(ctx
);
2405 memset(&p
->sq_off
, 0, sizeof(p
->sq_off
));
2406 p
->sq_off
.head
= offsetof(struct io_sq_ring
, r
.head
);
2407 p
->sq_off
.tail
= offsetof(struct io_sq_ring
, r
.tail
);
2408 p
->sq_off
.ring_mask
= offsetof(struct io_sq_ring
, ring_mask
);
2409 p
->sq_off
.ring_entries
= offsetof(struct io_sq_ring
, ring_entries
);
2410 p
->sq_off
.flags
= offsetof(struct io_sq_ring
, flags
);
2411 p
->sq_off
.dropped
= offsetof(struct io_sq_ring
, dropped
);
2412 p
->sq_off
.array
= offsetof(struct io_sq_ring
, array
);
2414 memset(&p
->cq_off
, 0, sizeof(p
->cq_off
));
2415 p
->cq_off
.head
= offsetof(struct io_cq_ring
, r
.head
);
2416 p
->cq_off
.tail
= offsetof(struct io_cq_ring
, r
.tail
);
2417 p
->cq_off
.ring_mask
= offsetof(struct io_cq_ring
, ring_mask
);
2418 p
->cq_off
.ring_entries
= offsetof(struct io_cq_ring
, ring_entries
);
2419 p
->cq_off
.overflow
= offsetof(struct io_cq_ring
, overflow
);
2420 p
->cq_off
.cqes
= offsetof(struct io_cq_ring
, cqes
);
2423 io_ring_ctx_wait_and_kill(ctx
);
2428 * Sets up an aio uring context, and returns the fd. Applications asks for a
2429 * ring size, we return the actual sq/cq ring sizes (among other things) in the
2430 * params structure passed in.
2432 static long io_uring_setup(u32 entries
, struct io_uring_params __user
*params
)
2434 struct io_uring_params p
;
2438 if (copy_from_user(&p
, params
, sizeof(p
)))
2440 for (i
= 0; i
< ARRAY_SIZE(p
.resv
); i
++) {
2445 if (p
.flags
& ~(IORING_SETUP_IOPOLL
| IORING_SETUP_SQPOLL
|
2446 IORING_SETUP_SQ_AFF
))
2449 ret
= io_uring_create(entries
, &p
);
2453 if (copy_to_user(params
, &p
, sizeof(p
)))
2459 SYSCALL_DEFINE2(io_uring_setup
, u32
, entries
,
2460 struct io_uring_params __user
*, params
)
2462 return io_uring_setup(entries
, params
);
2465 static int __io_uring_register(struct io_ring_ctx
*ctx
, unsigned opcode
,
2466 void __user
*arg
, unsigned nr_args
)
2470 percpu_ref_kill(&ctx
->refs
);
2471 wait_for_completion(&ctx
->ctx_done
);
2474 case IORING_REGISTER_BUFFERS
:
2475 ret
= io_sqe_buffer_register(ctx
, arg
, nr_args
);
2477 case IORING_UNREGISTER_BUFFERS
:
2481 ret
= io_sqe_buffer_unregister(ctx
);
2483 case IORING_REGISTER_FILES
:
2484 ret
= io_sqe_files_register(ctx
, arg
, nr_args
);
2486 case IORING_UNREGISTER_FILES
:
2490 ret
= io_sqe_files_unregister(ctx
);
2497 /* bring the ctx back to life */
2498 reinit_completion(&ctx
->ctx_done
);
2499 percpu_ref_reinit(&ctx
->refs
);
2503 SYSCALL_DEFINE4(io_uring_register
, unsigned int, fd
, unsigned int, opcode
,
2504 void __user
*, arg
, unsigned int, nr_args
)
2506 struct io_ring_ctx
*ctx
;
2515 if (f
.file
->f_op
!= &io_uring_fops
)
2518 ctx
= f
.file
->private_data
;
2520 mutex_lock(&ctx
->uring_lock
);
2521 ret
= __io_uring_register(ctx
, opcode
, arg
, nr_args
);
2522 mutex_unlock(&ctx
->uring_lock
);
2528 static int __init
io_uring_init(void)
2530 req_cachep
= KMEM_CACHE(io_kiocb
, SLAB_HWCACHE_ALIGN
| SLAB_PANIC
);
2533 __initcall(io_uring_init
);