This source file includes following definitions.
- io_uring_get_socket
- io_ring_ctx_ref_free
- io_ring_ctx_alloc
- __io_sequence_defer
- io_sequence_defer
- io_get_deferred_req
- io_get_timeout_req
- __io_commit_cqring
- io_queue_async_work
- io_kill_timeout
- io_kill_timeouts
- io_commit_cqring
- io_get_cqring
- io_cqring_fill_event
- io_cqring_ev_posted
- io_cqring_add_event
- io_get_req
- io_free_req_many
- __io_free_req
- io_req_link_next
- io_fail_links
- io_free_req
- io_put_req
- io_cqring_events
- io_sqring_entries
- io_iopoll_complete
- io_do_iopoll
- io_iopoll_getevents
- io_iopoll_reap_events
- io_iopoll_check
- kiocb_end_write
- io_complete_rw
- io_complete_rw_iopoll
- io_iopoll_req_issued
- io_file_put
- io_file_get
- io_file_supports_async
- io_prep_rw
- io_rw_done
- io_import_fixed
- io_import_iovec
- io_should_merge
- io_async_list_note
- loop_rw_iter
- io_read
- io_write
- io_nop
- io_prep_fsync
- io_fsync
- io_prep_sfr
- io_sync_file_range
- io_send_recvmsg
- io_sendmsg
- io_recvmsg
- io_poll_remove_one
- io_poll_remove_all
- io_poll_remove
- io_poll_complete
- io_poll_complete_work
- io_poll_wake
- io_poll_queue_proc
- io_poll_add
- io_timeout_fn
- io_timeout
- io_req_defer
- __io_submit_sqe
- io_async_list_from_sqe
- io_sqe_needs_user
- io_sq_wq_submit_work
- io_add_to_prev_work
- io_op_needs_file
- io_req_set_file
- __io_queue_sqe
- io_queue_sqe
- io_queue_link_head
- io_submit_sqe
- io_submit_state_end
- io_submit_state_start
- io_commit_sqring
- io_get_sqring
- io_submit_sqes
- io_sq_thread
- io_ring_submit
- io_should_wake
- io_wake_function
- io_cqring_wait
- __io_sqe_files_unregister
- io_sqe_files_unregister
- io_sq_thread_stop
- io_finish_async
- io_destruct_skb
- __io_sqe_files_scm
- io_sqe_files_scm
- io_sqe_files_scm
- io_sqe_files_register
- io_sq_offload_start
- io_unaccount_mem
- io_account_mem
- io_mem_free
- io_mem_alloc
- rings_size
- ring_pages
- io_sqe_buffer_unregister
- io_copy_iov
- io_sqe_buffer_register
- io_eventfd_register
- io_eventfd_unregister
- io_ring_ctx_free
- io_uring_poll
- io_uring_fasync
- io_ring_ctx_wait_and_kill
- io_uring_release
- io_uring_mmap
- SYSCALL_DEFINE6
- io_allocate_scq_urings
- io_uring_get_fd
- io_uring_create
- io_uring_setup
- SYSCALL_DEFINE2
- __io_uring_register
- SYSCALL_DEFINE4
- io_uring_init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 #include <linux/kernel.h>
43 #include <linux/init.h>
44 #include <linux/errno.h>
45 #include <linux/syscalls.h>
46 #include <linux/compat.h>
47 #include <linux/refcount.h>
48 #include <linux/uio.h>
49
50 #include <linux/sched/signal.h>
51 #include <linux/fs.h>
52 #include <linux/file.h>
53 #include <linux/fdtable.h>
54 #include <linux/mm.h>
55 #include <linux/mman.h>
56 #include <linux/mmu_context.h>
57 #include <linux/percpu.h>
58 #include <linux/slab.h>
59 #include <linux/workqueue.h>
60 #include <linux/kthread.h>
61 #include <linux/blkdev.h>
62 #include <linux/bvec.h>
63 #include <linux/net.h>
64 #include <net/sock.h>
65 #include <net/af_unix.h>
66 #include <net/scm.h>
67 #include <linux/anon_inodes.h>
68 #include <linux/sched/mm.h>
69 #include <linux/uaccess.h>
70 #include <linux/nospec.h>
71 #include <linux/sizes.h>
72 #include <linux/hugetlb.h>
73 #include <linux/highmem.h>
74 #include <linux/fs_struct.h>
75
76 #include <uapi/linux/io_uring.h>
77
78 #include "internal.h"
79
80 #define IORING_MAX_ENTRIES 32768
81 #define IORING_MAX_FIXED_FILES 1024
82
83 struct io_uring {
84 u32 head ____cacheline_aligned_in_smp;
85 u32 tail ____cacheline_aligned_in_smp;
86 };
87
88
89
90
91
92
93
94
95 struct io_rings {
96
97
98
99
100
101
102
103
104 struct io_uring sq, cq;
105
106
107
108
109 u32 sq_ring_mask, cq_ring_mask;
110
111 u32 sq_ring_entries, cq_ring_entries;
112
113
114
115
116
117
118
119
120
121
122
123
124 u32 sq_dropped;
125
126
127
128
129
130
131
132
133
134 u32 sq_flags;
135
136
137
138
139
140
141
142
143
144
145
146
147
148 u32 cq_overflow;
149
150
151
152
153
154
155
156 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
157 };
158
159 struct io_mapped_ubuf {
160 u64 ubuf;
161 size_t len;
162 struct bio_vec *bvec;
163 unsigned int nr_bvecs;
164 };
165
166 struct async_list {
167 spinlock_t lock;
168 atomic_t cnt;
169 struct list_head list;
170
171 struct file *file;
172 off_t io_start;
173 size_t io_len;
174 };
175
176 struct io_ring_ctx {
177 struct {
178 struct percpu_ref refs;
179 } ____cacheline_aligned_in_smp;
180
181 struct {
182 unsigned int flags;
183 bool compat;
184 bool account_mem;
185
186
187
188
189
190
191
192
193
194
195
196
197 u32 *sq_array;
198 unsigned cached_sq_head;
199 unsigned sq_entries;
200 unsigned sq_mask;
201 unsigned sq_thread_idle;
202 unsigned cached_sq_dropped;
203 struct io_uring_sqe *sq_sqes;
204
205 struct list_head defer_list;
206 struct list_head timeout_list;
207 } ____cacheline_aligned_in_smp;
208
209
210 struct workqueue_struct *sqo_wq[2];
211 struct task_struct *sqo_thread;
212 struct mm_struct *sqo_mm;
213 wait_queue_head_t sqo_wait;
214 struct completion sqo_thread_started;
215
216 struct {
217 unsigned cached_cq_tail;
218 atomic_t cached_cq_overflow;
219 unsigned cq_entries;
220 unsigned cq_mask;
221 struct wait_queue_head cq_wait;
222 struct fasync_struct *cq_fasync;
223 struct eventfd_ctx *cq_ev_fd;
224 atomic_t cq_timeouts;
225 } ____cacheline_aligned_in_smp;
226
227 struct io_rings *rings;
228
229
230
231
232
233
234 struct file **user_files;
235 unsigned nr_user_files;
236
237
238 unsigned nr_user_bufs;
239 struct io_mapped_ubuf *user_bufs;
240
241 struct user_struct *user;
242
243 const struct cred *creds;
244
245 struct completion ctx_done;
246
247 struct {
248 struct mutex uring_lock;
249 wait_queue_head_t wait;
250 } ____cacheline_aligned_in_smp;
251
252 struct {
253 spinlock_t completion_lock;
254 bool poll_multi_file;
255
256
257
258
259
260
261 struct list_head poll_list;
262 struct list_head cancel_list;
263 } ____cacheline_aligned_in_smp;
264
265 struct async_list pending_async[2];
266
267 #if defined(CONFIG_UNIX)
268 struct socket *ring_sock;
269 #endif
270 };
271
272 struct sqe_submit {
273 const struct io_uring_sqe *sqe;
274 unsigned short index;
275 u32 sequence;
276 bool has_user;
277 bool needs_lock;
278 bool needs_fixed_file;
279 };
280
281
282
283
284
285 struct io_poll_iocb {
286 struct file *file;
287 struct wait_queue_head *head;
288 __poll_t events;
289 bool done;
290 bool canceled;
291 struct wait_queue_entry wait;
292 };
293
294 struct io_timeout {
295 struct file *file;
296 struct hrtimer timer;
297 };
298
299
300
301
302
303
304
305 struct io_kiocb {
306 union {
307 struct file *file;
308 struct kiocb rw;
309 struct io_poll_iocb poll;
310 struct io_timeout timeout;
311 };
312
313 struct sqe_submit submit;
314
315 struct io_ring_ctx *ctx;
316 struct list_head list;
317 struct list_head link_list;
318 unsigned int flags;
319 refcount_t refs;
320 #define REQ_F_NOWAIT 1
321 #define REQ_F_IOPOLL_COMPLETED 2
322 #define REQ_F_FIXED_FILE 4
323 #define REQ_F_SEQ_PREV 8
324 #define REQ_F_IO_DRAIN 16
325 #define REQ_F_IO_DRAINED 32
326 #define REQ_F_LINK 64
327 #define REQ_F_LINK_DONE 128
328 #define REQ_F_FAIL_LINK 256
329 #define REQ_F_SHADOW_DRAIN 512
330 #define REQ_F_TIMEOUT 1024
331 #define REQ_F_ISREG 2048
332 #define REQ_F_MUST_PUNT 4096
333 #define REQ_F_TIMEOUT_NOSEQ 8192
334 unsigned long fsize;
335 u64 user_data;
336 u32 result;
337 u32 sequence;
338
339 struct fs_struct *fs;
340
341 struct work_struct work;
342 };
343
344 #define IO_PLUG_THRESHOLD 2
345 #define IO_IOPOLL_BATCH 8
346
347 struct io_submit_state {
348 struct blk_plug plug;
349
350
351
352
353 void *reqs[IO_IOPOLL_BATCH];
354 unsigned int free_reqs;
355 unsigned int cur_req;
356
357
358
359
360 struct file *file;
361 unsigned int fd;
362 unsigned int has_refs;
363 unsigned int used_refs;
364 unsigned int ios_left;
365 };
366
367 static void io_sq_wq_submit_work(struct work_struct *work);
368 static void io_cqring_fill_event(struct io_ring_ctx *ctx, u64 ki_user_data,
369 long res);
370 static void __io_free_req(struct io_kiocb *req);
371
372 static struct kmem_cache *req_cachep;
373
374 static const struct file_operations io_uring_fops;
375
376 struct sock *io_uring_get_socket(struct file *file)
377 {
378 #if defined(CONFIG_UNIX)
379 if (file->f_op == &io_uring_fops) {
380 struct io_ring_ctx *ctx = file->private_data;
381
382 return ctx->ring_sock->sk;
383 }
384 #endif
385 return NULL;
386 }
387 EXPORT_SYMBOL(io_uring_get_socket);
388
389 static void io_ring_ctx_ref_free(struct percpu_ref *ref)
390 {
391 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
392
393 complete(&ctx->ctx_done);
394 }
395
396 static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
397 {
398 struct io_ring_ctx *ctx;
399 int i;
400
401 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
402 if (!ctx)
403 return NULL;
404
405 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
406 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
407 kfree(ctx);
408 return NULL;
409 }
410
411 ctx->flags = p->flags;
412 init_waitqueue_head(&ctx->sqo_wait);
413 init_waitqueue_head(&ctx->cq_wait);
414 init_completion(&ctx->ctx_done);
415 init_completion(&ctx->sqo_thread_started);
416 mutex_init(&ctx->uring_lock);
417 init_waitqueue_head(&ctx->wait);
418 for (i = 0; i < ARRAY_SIZE(ctx->pending_async); i++) {
419 spin_lock_init(&ctx->pending_async[i].lock);
420 INIT_LIST_HEAD(&ctx->pending_async[i].list);
421 atomic_set(&ctx->pending_async[i].cnt, 0);
422 }
423 spin_lock_init(&ctx->completion_lock);
424 INIT_LIST_HEAD(&ctx->poll_list);
425 INIT_LIST_HEAD(&ctx->cancel_list);
426 INIT_LIST_HEAD(&ctx->defer_list);
427 INIT_LIST_HEAD(&ctx->timeout_list);
428 return ctx;
429 }
430
431 static inline bool __io_sequence_defer(struct io_ring_ctx *ctx,
432 struct io_kiocb *req)
433 {
434 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
435 + atomic_read(&ctx->cached_cq_overflow);
436 }
437
438 static inline bool io_sequence_defer(struct io_ring_ctx *ctx,
439 struct io_kiocb *req)
440 {
441 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) != REQ_F_IO_DRAIN)
442 return false;
443
444 return __io_sequence_defer(ctx, req);
445 }
446
447 static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
448 {
449 struct io_kiocb *req;
450
451 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
452 if (req && !io_sequence_defer(ctx, req)) {
453 list_del_init(&req->list);
454 return req;
455 }
456
457 return NULL;
458 }
459
460 static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
461 {
462 struct io_kiocb *req;
463
464 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
465 if (req) {
466 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
467 return NULL;
468 if (!__io_sequence_defer(ctx, req)) {
469 list_del_init(&req->list);
470 return req;
471 }
472 }
473
474 return NULL;
475 }
476
477 static void __io_commit_cqring(struct io_ring_ctx *ctx)
478 {
479 struct io_rings *rings = ctx->rings;
480
481 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
482
483 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
484
485 if (wq_has_sleeper(&ctx->cq_wait)) {
486 wake_up_interruptible(&ctx->cq_wait);
487 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
488 }
489 }
490 }
491
492 static inline void io_queue_async_work(struct io_ring_ctx *ctx,
493 struct io_kiocb *req)
494 {
495 int rw = 0;
496
497 if (req->submit.sqe) {
498 switch (req->submit.sqe->opcode) {
499 case IORING_OP_WRITEV:
500 case IORING_OP_WRITE_FIXED:
501 rw = !(req->rw.ki_flags & IOCB_DIRECT);
502 break;
503 }
504 }
505
506 queue_work(ctx->sqo_wq[rw], &req->work);
507 }
508
509 static void io_kill_timeout(struct io_kiocb *req)
510 {
511 int ret;
512
513 ret = hrtimer_try_to_cancel(&req->timeout.timer);
514 if (ret != -1) {
515 atomic_inc(&req->ctx->cq_timeouts);
516 list_del(&req->list);
517 io_cqring_fill_event(req->ctx, req->user_data, 0);
518 __io_free_req(req);
519 }
520 }
521
522 static void io_kill_timeouts(struct io_ring_ctx *ctx)
523 {
524 struct io_kiocb *req, *tmp;
525
526 spin_lock_irq(&ctx->completion_lock);
527 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
528 io_kill_timeout(req);
529 spin_unlock_irq(&ctx->completion_lock);
530 }
531
532 static void io_commit_cqring(struct io_ring_ctx *ctx)
533 {
534 struct io_kiocb *req;
535
536 while ((req = io_get_timeout_req(ctx)) != NULL)
537 io_kill_timeout(req);
538
539 __io_commit_cqring(ctx);
540
541 while ((req = io_get_deferred_req(ctx)) != NULL) {
542 if (req->flags & REQ_F_SHADOW_DRAIN) {
543
544 __io_free_req(req);
545 continue;
546 }
547 req->flags |= REQ_F_IO_DRAINED;
548 io_queue_async_work(ctx, req);
549 }
550 }
551
552 static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
553 {
554 struct io_rings *rings = ctx->rings;
555 unsigned tail;
556
557 tail = ctx->cached_cq_tail;
558
559
560
561
562
563 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
564 return NULL;
565
566 ctx->cached_cq_tail++;
567 return &rings->cqes[tail & ctx->cq_mask];
568 }
569
570 static void io_cqring_fill_event(struct io_ring_ctx *ctx, u64 ki_user_data,
571 long res)
572 {
573 struct io_uring_cqe *cqe;
574
575
576
577
578
579
580 cqe = io_get_cqring(ctx);
581 if (cqe) {
582 WRITE_ONCE(cqe->user_data, ki_user_data);
583 WRITE_ONCE(cqe->res, res);
584 WRITE_ONCE(cqe->flags, 0);
585 } else {
586 WRITE_ONCE(ctx->rings->cq_overflow,
587 atomic_inc_return(&ctx->cached_cq_overflow));
588 }
589 }
590
591 static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
592 {
593 if (waitqueue_active(&ctx->wait))
594 wake_up(&ctx->wait);
595 if (waitqueue_active(&ctx->sqo_wait))
596 wake_up(&ctx->sqo_wait);
597 if (ctx->cq_ev_fd)
598 eventfd_signal(ctx->cq_ev_fd, 1);
599 }
600
601 static void io_cqring_add_event(struct io_ring_ctx *ctx, u64 user_data,
602 long res)
603 {
604 unsigned long flags;
605
606 spin_lock_irqsave(&ctx->completion_lock, flags);
607 io_cqring_fill_event(ctx, user_data, res);
608 io_commit_cqring(ctx);
609 spin_unlock_irqrestore(&ctx->completion_lock, flags);
610
611 io_cqring_ev_posted(ctx);
612 }
613
614 static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
615 struct io_submit_state *state)
616 {
617 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
618 struct io_kiocb *req;
619
620 if (!percpu_ref_tryget(&ctx->refs))
621 return NULL;
622
623 if (!state) {
624 req = kmem_cache_alloc(req_cachep, gfp);
625 if (unlikely(!req))
626 goto out;
627 } else if (!state->free_reqs) {
628 size_t sz;
629 int ret;
630
631 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
632 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
633
634
635
636
637
638 if (unlikely(ret <= 0)) {
639 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
640 if (!state->reqs[0])
641 goto out;
642 ret = 1;
643 }
644 state->free_reqs = ret - 1;
645 state->cur_req = 1;
646 req = state->reqs[0];
647 } else {
648 req = state->reqs[state->cur_req];
649 state->free_reqs--;
650 state->cur_req++;
651 }
652
653 req->file = NULL;
654 req->ctx = ctx;
655 req->flags = 0;
656
657 refcount_set(&req->refs, 2);
658 req->result = 0;
659 req->fs = NULL;
660 return req;
661 out:
662 percpu_ref_put(&ctx->refs);
663 return NULL;
664 }
665
666 static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
667 {
668 if (*nr) {
669 kmem_cache_free_bulk(req_cachep, *nr, reqs);
670 percpu_ref_put_many(&ctx->refs, *nr);
671 *nr = 0;
672 }
673 }
674
675 static void __io_free_req(struct io_kiocb *req)
676 {
677 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
678 fput(req->file);
679 percpu_ref_put(&req->ctx->refs);
680 kmem_cache_free(req_cachep, req);
681 }
682
683 static void io_req_link_next(struct io_kiocb *req)
684 {
685 struct io_kiocb *nxt;
686
687
688
689
690
691
692 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
693 if (nxt) {
694 list_del(&nxt->list);
695 if (!list_empty(&req->link_list)) {
696 INIT_LIST_HEAD(&nxt->link_list);
697 list_splice(&req->link_list, &nxt->link_list);
698 nxt->flags |= REQ_F_LINK;
699 }
700
701 nxt->flags |= REQ_F_LINK_DONE;
702 INIT_WORK(&nxt->work, io_sq_wq_submit_work);
703 io_queue_async_work(req->ctx, nxt);
704 }
705 }
706
707
708
709
710 static void io_fail_links(struct io_kiocb *req)
711 {
712 struct io_kiocb *link;
713
714 while (!list_empty(&req->link_list)) {
715 link = list_first_entry(&req->link_list, struct io_kiocb, list);
716 list_del(&link->list);
717
718 io_cqring_add_event(req->ctx, link->user_data, -ECANCELED);
719 __io_free_req(link);
720 }
721 }
722
723 static void io_free_req(struct io_kiocb *req)
724 {
725
726
727
728
729
730
731 if (req->flags & REQ_F_LINK) {
732 if (req->flags & REQ_F_FAIL_LINK)
733 io_fail_links(req);
734 else
735 io_req_link_next(req);
736 }
737
738 __io_free_req(req);
739 }
740
741 static void io_put_req(struct io_kiocb *req)
742 {
743 if (refcount_dec_and_test(&req->refs))
744 io_free_req(req);
745 }
746
747 static unsigned io_cqring_events(struct io_rings *rings)
748 {
749
750 smp_rmb();
751 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
752 }
753
754 static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
755 {
756 struct io_rings *rings = ctx->rings;
757
758
759 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
760 }
761
762
763
764
765 static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
766 struct list_head *done)
767 {
768 void *reqs[IO_IOPOLL_BATCH];
769 struct io_kiocb *req;
770 int to_free;
771
772 to_free = 0;
773 while (!list_empty(done)) {
774 req = list_first_entry(done, struct io_kiocb, list);
775 list_del(&req->list);
776
777 io_cqring_fill_event(ctx, req->user_data, req->result);
778 (*nr_events)++;
779
780 if (refcount_dec_and_test(&req->refs)) {
781
782
783
784
785
786 if ((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) ==
787 REQ_F_FIXED_FILE) {
788 reqs[to_free++] = req;
789 if (to_free == ARRAY_SIZE(reqs))
790 io_free_req_many(ctx, reqs, &to_free);
791 } else {
792 io_free_req(req);
793 }
794 }
795 }
796
797 io_commit_cqring(ctx);
798 io_free_req_many(ctx, reqs, &to_free);
799 }
800
801 static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
802 long min)
803 {
804 struct io_kiocb *req, *tmp;
805 LIST_HEAD(done);
806 bool spin;
807 int ret;
808
809
810
811
812
813 spin = !ctx->poll_multi_file && *nr_events < min;
814
815 ret = 0;
816 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
817 struct kiocb *kiocb = &req->rw;
818
819
820
821
822
823
824 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
825 list_move_tail(&req->list, &done);
826 continue;
827 }
828 if (!list_empty(&done))
829 break;
830
831 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
832 if (ret < 0)
833 break;
834
835 if (ret && spin)
836 spin = false;
837 ret = 0;
838 }
839
840 if (!list_empty(&done))
841 io_iopoll_complete(ctx, nr_events, &done);
842
843 return ret;
844 }
845
846
847
848
849
850
851 static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
852 long min)
853 {
854 while (!list_empty(&ctx->poll_list) && !need_resched()) {
855 int ret;
856
857 ret = io_do_iopoll(ctx, nr_events, min);
858 if (ret < 0)
859 return ret;
860 if (!min || *nr_events >= min)
861 return 0;
862 }
863
864 return 1;
865 }
866
867
868
869
870
871 static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
872 {
873 if (!(ctx->flags & IORING_SETUP_IOPOLL))
874 return;
875
876 mutex_lock(&ctx->uring_lock);
877 while (!list_empty(&ctx->poll_list)) {
878 unsigned int nr_events = 0;
879
880 io_iopoll_getevents(ctx, &nr_events, 1);
881
882
883
884
885
886 cond_resched();
887 }
888 mutex_unlock(&ctx->uring_lock);
889 }
890
891 static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
892 long min)
893 {
894 int iters = 0, ret = 0;
895
896
897
898
899
900
901 mutex_lock(&ctx->uring_lock);
902 do {
903 int tmin = 0;
904
905
906
907
908
909
910 if (io_cqring_events(ctx->rings))
911 break;
912
913
914
915
916
917
918
919
920
921
922
923 if (!(++iters & 7)) {
924 mutex_unlock(&ctx->uring_lock);
925 mutex_lock(&ctx->uring_lock);
926 }
927
928 if (*nr_events < min)
929 tmin = min - *nr_events;
930
931 ret = io_iopoll_getevents(ctx, nr_events, tmin);
932 if (ret <= 0)
933 break;
934 ret = 0;
935 } while (min && !*nr_events && !need_resched());
936
937 mutex_unlock(&ctx->uring_lock);
938 return ret;
939 }
940
941 static void kiocb_end_write(struct io_kiocb *req)
942 {
943
944
945
946
947 if (req->flags & REQ_F_ISREG) {
948 struct inode *inode = file_inode(req->file);
949
950 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
951 }
952 file_end_write(req->file);
953 }
954
955 static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
956 {
957 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
958
959 if (kiocb->ki_flags & IOCB_WRITE)
960 kiocb_end_write(req);
961
962 if ((req->flags & REQ_F_LINK) && res != req->result)
963 req->flags |= REQ_F_FAIL_LINK;
964 io_cqring_add_event(req->ctx, req->user_data, res);
965 io_put_req(req);
966 }
967
968 static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
969 {
970 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
971
972 if (kiocb->ki_flags & IOCB_WRITE)
973 kiocb_end_write(req);
974
975 if ((req->flags & REQ_F_LINK) && res != req->result)
976 req->flags |= REQ_F_FAIL_LINK;
977 req->result = res;
978 if (res != -EAGAIN)
979 req->flags |= REQ_F_IOPOLL_COMPLETED;
980 }
981
982
983
984
985
986
987
988 static void io_iopoll_req_issued(struct io_kiocb *req)
989 {
990 struct io_ring_ctx *ctx = req->ctx;
991
992
993
994
995
996
997 if (list_empty(&ctx->poll_list)) {
998 ctx->poll_multi_file = false;
999 } else if (!ctx->poll_multi_file) {
1000 struct io_kiocb *list_req;
1001
1002 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1003 list);
1004 if (list_req->rw.ki_filp != req->rw.ki_filp)
1005 ctx->poll_multi_file = true;
1006 }
1007
1008
1009
1010
1011
1012 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1013 list_add(&req->list, &ctx->poll_list);
1014 else
1015 list_add_tail(&req->list, &ctx->poll_list);
1016 }
1017
1018 static void io_file_put(struct io_submit_state *state)
1019 {
1020 if (state->file) {
1021 int diff = state->has_refs - state->used_refs;
1022
1023 if (diff)
1024 fput_many(state->file, diff);
1025 state->file = NULL;
1026 }
1027 }
1028
1029
1030
1031
1032
1033
1034 static struct file *io_file_get(struct io_submit_state *state, int fd)
1035 {
1036 if (!state)
1037 return fget(fd);
1038
1039 if (state->file) {
1040 if (state->fd == fd) {
1041 state->used_refs++;
1042 state->ios_left--;
1043 return state->file;
1044 }
1045 io_file_put(state);
1046 }
1047 state->file = fget_many(fd, state->ios_left);
1048 if (!state->file)
1049 return NULL;
1050
1051 state->fd = fd;
1052 state->has_refs = state->ios_left;
1053 state->used_refs = 1;
1054 state->ios_left--;
1055 return state->file;
1056 }
1057
1058
1059
1060
1061
1062
1063 static bool io_file_supports_async(struct file *file)
1064 {
1065 umode_t mode = file_inode(file)->i_mode;
1066
1067 if (S_ISBLK(mode) || S_ISCHR(mode))
1068 return true;
1069 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1070 return true;
1071
1072 return false;
1073 }
1074
1075 static int io_prep_rw(struct io_kiocb *req, const struct sqe_submit *s,
1076 bool force_nonblock)
1077 {
1078 const struct io_uring_sqe *sqe = s->sqe;
1079 struct io_ring_ctx *ctx = req->ctx;
1080 struct kiocb *kiocb = &req->rw;
1081 unsigned ioprio;
1082 int ret;
1083
1084 if (!req->file)
1085 return -EBADF;
1086
1087 if (S_ISREG(file_inode(req->file)->i_mode))
1088 req->flags |= REQ_F_ISREG;
1089
1090 if (force_nonblock)
1091 req->fsize = rlimit(RLIMIT_FSIZE);
1092
1093
1094
1095
1096
1097 if (force_nonblock && !io_file_supports_async(req->file)) {
1098 req->flags |= REQ_F_MUST_PUNT;
1099 return -EAGAIN;
1100 }
1101
1102 kiocb->ki_pos = READ_ONCE(sqe->off);
1103 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1104 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1105
1106 ioprio = READ_ONCE(sqe->ioprio);
1107 if (ioprio) {
1108 ret = ioprio_check_cap(ioprio);
1109 if (ret)
1110 return ret;
1111
1112 kiocb->ki_ioprio = ioprio;
1113 } else
1114 kiocb->ki_ioprio = get_current_ioprio();
1115
1116 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1117 if (unlikely(ret))
1118 return ret;
1119
1120
1121 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1122 (req->file->f_flags & O_NONBLOCK))
1123 req->flags |= REQ_F_NOWAIT;
1124
1125 if (force_nonblock)
1126 kiocb->ki_flags |= IOCB_NOWAIT;
1127
1128 if (ctx->flags & IORING_SETUP_IOPOLL) {
1129 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1130 !kiocb->ki_filp->f_op->iopoll)
1131 return -EOPNOTSUPP;
1132
1133 kiocb->ki_flags |= IOCB_HIPRI;
1134 kiocb->ki_complete = io_complete_rw_iopoll;
1135 req->result = 0;
1136 } else {
1137 if (kiocb->ki_flags & IOCB_HIPRI)
1138 return -EINVAL;
1139 kiocb->ki_complete = io_complete_rw;
1140 }
1141 return 0;
1142 }
1143
1144 static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1145 {
1146 switch (ret) {
1147 case -EIOCBQUEUED:
1148 break;
1149 case -ERESTARTSYS:
1150 case -ERESTARTNOINTR:
1151 case -ERESTARTNOHAND:
1152 case -ERESTART_RESTARTBLOCK:
1153
1154
1155
1156
1157
1158 ret = -EINTR;
1159
1160 default:
1161 kiocb->ki_complete(kiocb, ret, 0);
1162 }
1163 }
1164
1165 static int io_import_fixed(struct io_ring_ctx *ctx, int rw,
1166 const struct io_uring_sqe *sqe,
1167 struct iov_iter *iter)
1168 {
1169 size_t len = READ_ONCE(sqe->len);
1170 struct io_mapped_ubuf *imu;
1171 unsigned index, buf_index;
1172 size_t offset;
1173 u64 buf_addr;
1174
1175
1176 if (unlikely(!ctx->user_bufs))
1177 return -EFAULT;
1178
1179 buf_index = READ_ONCE(sqe->buf_index);
1180 if (unlikely(buf_index >= ctx->nr_user_bufs))
1181 return -EFAULT;
1182
1183 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1184 imu = &ctx->user_bufs[index];
1185 buf_addr = READ_ONCE(sqe->addr);
1186
1187
1188 if (buf_addr + len < buf_addr)
1189 return -EFAULT;
1190
1191 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1192 return -EFAULT;
1193
1194
1195
1196
1197
1198 offset = buf_addr - imu->ubuf;
1199 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
1200
1201 if (offset) {
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218 const struct bio_vec *bvec = imu->bvec;
1219
1220 if (offset <= bvec->bv_len) {
1221 iov_iter_advance(iter, offset);
1222 } else {
1223 unsigned long seg_skip;
1224
1225
1226 offset -= bvec->bv_len;
1227 seg_skip = 1 + (offset >> PAGE_SHIFT);
1228
1229 iter->bvec = bvec + seg_skip;
1230 iter->nr_segs -= seg_skip;
1231 iter->count -= bvec->bv_len + offset;
1232 iter->iov_offset = offset & ~PAGE_MASK;
1233 }
1234 }
1235
1236 return len;
1237 }
1238
1239 static ssize_t io_import_iovec(struct io_ring_ctx *ctx, int rw,
1240 const struct sqe_submit *s, struct iovec **iovec,
1241 struct iov_iter *iter)
1242 {
1243 const struct io_uring_sqe *sqe = s->sqe;
1244 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1245 size_t sqe_len = READ_ONCE(sqe->len);
1246 u8 opcode;
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256 opcode = READ_ONCE(sqe->opcode);
1257 if (opcode == IORING_OP_READ_FIXED ||
1258 opcode == IORING_OP_WRITE_FIXED) {
1259 ssize_t ret = io_import_fixed(ctx, rw, sqe, iter);
1260 *iovec = NULL;
1261 return ret;
1262 }
1263
1264 if (!s->has_user)
1265 return -EFAULT;
1266
1267 #ifdef CONFIG_COMPAT
1268 if (ctx->compat)
1269 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1270 iovec, iter);
1271 #endif
1272
1273 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1274 }
1275
1276 static inline bool io_should_merge(struct async_list *al, struct kiocb *kiocb)
1277 {
1278 if (al->file == kiocb->ki_filp) {
1279 off_t start, end;
1280
1281
1282
1283
1284
1285
1286
1287
1288 start = al->io_start & PAGE_MASK;
1289 end = (al->io_start + al->io_len + PAGE_SIZE - 1) & PAGE_MASK;
1290 if (kiocb->ki_pos >= start && kiocb->ki_pos <= end)
1291 return true;
1292 }
1293
1294 al->file = NULL;
1295 return false;
1296 }
1297
1298
1299
1300
1301
1302
1303
1304 static void io_async_list_note(int rw, struct io_kiocb *req, size_t len)
1305 {
1306 struct async_list *async_list = &req->ctx->pending_async[rw];
1307 struct kiocb *kiocb = &req->rw;
1308 struct file *filp = kiocb->ki_filp;
1309
1310 if (io_should_merge(async_list, kiocb)) {
1311 unsigned long max_bytes;
1312
1313
1314 max_bytes = filp->f_ra.ra_pages << (PAGE_SHIFT + 3);
1315 if (!max_bytes)
1316 max_bytes = VM_READAHEAD_PAGES << (PAGE_SHIFT + 3);
1317
1318
1319 if (async_list->io_len + len <= max_bytes) {
1320 req->flags |= REQ_F_SEQ_PREV;
1321 async_list->io_len += len;
1322 } else {
1323 async_list->file = NULL;
1324 }
1325 }
1326
1327
1328 if (async_list->file != filp) {
1329 async_list->io_start = kiocb->ki_pos;
1330 async_list->io_len = len;
1331 async_list->file = filp;
1332 }
1333 }
1334
1335
1336
1337
1338
1339 static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1340 struct iov_iter *iter)
1341 {
1342 ssize_t ret = 0;
1343
1344
1345
1346
1347
1348
1349 if (kiocb->ki_flags & IOCB_HIPRI)
1350 return -EOPNOTSUPP;
1351 if (kiocb->ki_flags & IOCB_NOWAIT)
1352 return -EAGAIN;
1353
1354 while (iov_iter_count(iter)) {
1355 struct iovec iovec;
1356 ssize_t nr;
1357
1358 if (!iov_iter_is_bvec(iter)) {
1359 iovec = iov_iter_iovec(iter);
1360 } else {
1361
1362 iovec.iov_base = kmap(iter->bvec->bv_page)
1363 + iter->iov_offset;
1364 iovec.iov_len = min(iter->count,
1365 iter->bvec->bv_len - iter->iov_offset);
1366 }
1367
1368 if (rw == READ) {
1369 nr = file->f_op->read(file, iovec.iov_base,
1370 iovec.iov_len, &kiocb->ki_pos);
1371 } else {
1372 nr = file->f_op->write(file, iovec.iov_base,
1373 iovec.iov_len, &kiocb->ki_pos);
1374 }
1375
1376 if (iov_iter_is_bvec(iter))
1377 kunmap(iter->bvec->bv_page);
1378
1379 if (nr < 0) {
1380 if (!ret)
1381 ret = nr;
1382 break;
1383 }
1384 ret += nr;
1385 if (nr != iovec.iov_len)
1386 break;
1387 iov_iter_advance(iter, nr);
1388 }
1389
1390 return ret;
1391 }
1392
1393 static int io_read(struct io_kiocb *req, const struct sqe_submit *s,
1394 bool force_nonblock)
1395 {
1396 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1397 struct kiocb *kiocb = &req->rw;
1398 struct iov_iter iter;
1399 struct file *file;
1400 size_t iov_count;
1401 ssize_t read_size, ret;
1402
1403 ret = io_prep_rw(req, s, force_nonblock);
1404 if (ret)
1405 return ret;
1406 file = kiocb->ki_filp;
1407
1408 if (unlikely(!(file->f_mode & FMODE_READ)))
1409 return -EBADF;
1410
1411 ret = io_import_iovec(req->ctx, READ, s, &iovec, &iter);
1412 if (ret < 0)
1413 return ret;
1414
1415 read_size = ret;
1416 if (req->flags & REQ_F_LINK)
1417 req->result = read_size;
1418
1419 iov_count = iov_iter_count(&iter);
1420 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
1421 if (!ret) {
1422 ssize_t ret2;
1423
1424 if (file->f_op->read_iter)
1425 ret2 = call_read_iter(file, kiocb, &iter);
1426 else
1427 ret2 = loop_rw_iter(READ, file, kiocb, &iter);
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437 if (force_nonblock && !(req->flags & REQ_F_NOWAIT) &&
1438 (req->flags & REQ_F_ISREG) &&
1439 ret2 > 0 && ret2 < read_size)
1440 ret2 = -EAGAIN;
1441
1442 if (!force_nonblock || ret2 != -EAGAIN) {
1443 io_rw_done(kiocb, ret2);
1444 } else {
1445
1446
1447
1448
1449 if (!s->needs_lock)
1450 io_async_list_note(READ, req, iov_count);
1451 ret = -EAGAIN;
1452 }
1453 }
1454 kfree(iovec);
1455 return ret;
1456 }
1457
1458 static int io_write(struct io_kiocb *req, const struct sqe_submit *s,
1459 bool force_nonblock)
1460 {
1461 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1462 struct kiocb *kiocb = &req->rw;
1463 struct iov_iter iter;
1464 struct file *file;
1465 size_t iov_count;
1466 ssize_t ret;
1467
1468 ret = io_prep_rw(req, s, force_nonblock);
1469 if (ret)
1470 return ret;
1471
1472 file = kiocb->ki_filp;
1473 if (unlikely(!(file->f_mode & FMODE_WRITE)))
1474 return -EBADF;
1475
1476 ret = io_import_iovec(req->ctx, WRITE, s, &iovec, &iter);
1477 if (ret < 0)
1478 return ret;
1479
1480 if (req->flags & REQ_F_LINK)
1481 req->result = ret;
1482
1483 iov_count = iov_iter_count(&iter);
1484
1485 ret = -EAGAIN;
1486 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT)) {
1487
1488 if (!s->needs_lock)
1489 io_async_list_note(WRITE, req, iov_count);
1490 goto out_free;
1491 }
1492
1493 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
1494 if (!ret) {
1495 ssize_t ret2;
1496
1497
1498
1499
1500
1501
1502
1503
1504 if (req->flags & REQ_F_ISREG) {
1505 __sb_start_write(file_inode(file)->i_sb,
1506 SB_FREEZE_WRITE, true);
1507 __sb_writers_release(file_inode(file)->i_sb,
1508 SB_FREEZE_WRITE);
1509 }
1510 kiocb->ki_flags |= IOCB_WRITE;
1511
1512 if (!force_nonblock)
1513 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = req->fsize;
1514
1515 if (file->f_op->write_iter)
1516 ret2 = call_write_iter(file, kiocb, &iter);
1517 else
1518 ret2 = loop_rw_iter(WRITE, file, kiocb, &iter);
1519
1520 if (!force_nonblock)
1521 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
1522
1523 if (!force_nonblock || ret2 != -EAGAIN) {
1524 io_rw_done(kiocb, ret2);
1525 } else {
1526
1527
1528
1529
1530 if (!s->needs_lock)
1531 io_async_list_note(WRITE, req, iov_count);
1532 ret = -EAGAIN;
1533 }
1534 }
1535 out_free:
1536 kfree(iovec);
1537 return ret;
1538 }
1539
1540
1541
1542
1543 static int io_nop(struct io_kiocb *req, u64 user_data)
1544 {
1545 struct io_ring_ctx *ctx = req->ctx;
1546 long err = 0;
1547
1548 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1549 return -EINVAL;
1550
1551 io_cqring_add_event(ctx, user_data, err);
1552 io_put_req(req);
1553 return 0;
1554 }
1555
1556 static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1557 {
1558 struct io_ring_ctx *ctx = req->ctx;
1559
1560 if (!req->file)
1561 return -EBADF;
1562
1563 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1564 return -EINVAL;
1565 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1566 return -EINVAL;
1567
1568 return 0;
1569 }
1570
1571 static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1572 bool force_nonblock)
1573 {
1574 loff_t sqe_off = READ_ONCE(sqe->off);
1575 loff_t sqe_len = READ_ONCE(sqe->len);
1576 loff_t end = sqe_off + sqe_len;
1577 unsigned fsync_flags;
1578 int ret;
1579
1580 fsync_flags = READ_ONCE(sqe->fsync_flags);
1581 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1582 return -EINVAL;
1583
1584 ret = io_prep_fsync(req, sqe);
1585 if (ret)
1586 return ret;
1587
1588
1589 if (force_nonblock)
1590 return -EAGAIN;
1591
1592 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1593 end > 0 ? end : LLONG_MAX,
1594 fsync_flags & IORING_FSYNC_DATASYNC);
1595
1596 if (ret < 0 && (req->flags & REQ_F_LINK))
1597 req->flags |= REQ_F_FAIL_LINK;
1598 io_cqring_add_event(req->ctx, sqe->user_data, ret);
1599 io_put_req(req);
1600 return 0;
1601 }
1602
1603 static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1604 {
1605 struct io_ring_ctx *ctx = req->ctx;
1606 int ret = 0;
1607
1608 if (!req->file)
1609 return -EBADF;
1610
1611 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1612 return -EINVAL;
1613 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1614 return -EINVAL;
1615
1616 return ret;
1617 }
1618
1619 static int io_sync_file_range(struct io_kiocb *req,
1620 const struct io_uring_sqe *sqe,
1621 bool force_nonblock)
1622 {
1623 loff_t sqe_off;
1624 loff_t sqe_len;
1625 unsigned flags;
1626 int ret;
1627
1628 ret = io_prep_sfr(req, sqe);
1629 if (ret)
1630 return ret;
1631
1632
1633 if (force_nonblock)
1634 return -EAGAIN;
1635
1636 sqe_off = READ_ONCE(sqe->off);
1637 sqe_len = READ_ONCE(sqe->len);
1638 flags = READ_ONCE(sqe->sync_range_flags);
1639
1640 ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
1641
1642 if (ret < 0 && (req->flags & REQ_F_LINK))
1643 req->flags |= REQ_F_FAIL_LINK;
1644 io_cqring_add_event(req->ctx, sqe->user_data, ret);
1645 io_put_req(req);
1646 return 0;
1647 }
1648
1649 #if defined(CONFIG_NET)
1650 static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1651 bool force_nonblock,
1652 long (*fn)(struct socket *, struct user_msghdr __user *,
1653 unsigned int))
1654 {
1655 struct socket *sock;
1656 int ret;
1657
1658 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1659 return -EINVAL;
1660
1661 sock = sock_from_file(req->file, &ret);
1662 if (sock) {
1663 struct user_msghdr __user *msg;
1664 unsigned flags;
1665
1666 flags = READ_ONCE(sqe->msg_flags);
1667 if (flags & MSG_DONTWAIT)
1668 req->flags |= REQ_F_NOWAIT;
1669 else if (force_nonblock)
1670 flags |= MSG_DONTWAIT;
1671
1672 #ifdef CONFIG_COMPAT
1673 if (req->ctx->compat)
1674 flags |= MSG_CMSG_COMPAT;
1675 #endif
1676
1677 msg = (struct user_msghdr __user *) (unsigned long)
1678 READ_ONCE(sqe->addr);
1679
1680 ret = fn(sock, msg, flags);
1681 if (force_nonblock && ret == -EAGAIN)
1682 return ret;
1683 if (ret == -ERESTARTSYS)
1684 ret = -EINTR;
1685 }
1686
1687 if (req->fs) {
1688 struct fs_struct *fs = req->fs;
1689
1690 spin_lock(&req->fs->lock);
1691 if (--fs->users)
1692 fs = NULL;
1693 spin_unlock(&req->fs->lock);
1694 if (fs)
1695 free_fs_struct(fs);
1696 }
1697 io_cqring_add_event(req->ctx, sqe->user_data, ret);
1698 io_put_req(req);
1699 return 0;
1700 }
1701 #endif
1702
1703 static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1704 bool force_nonblock)
1705 {
1706 #if defined(CONFIG_NET)
1707 return io_send_recvmsg(req, sqe, force_nonblock, __sys_sendmsg_sock);
1708 #else
1709 return -EOPNOTSUPP;
1710 #endif
1711 }
1712
1713 static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1714 bool force_nonblock)
1715 {
1716 #if defined(CONFIG_NET)
1717 return io_send_recvmsg(req, sqe, force_nonblock, __sys_recvmsg_sock);
1718 #else
1719 return -EOPNOTSUPP;
1720 #endif
1721 }
1722
1723 static void io_poll_remove_one(struct io_kiocb *req)
1724 {
1725 struct io_poll_iocb *poll = &req->poll;
1726
1727 spin_lock(&poll->head->lock);
1728 WRITE_ONCE(poll->canceled, true);
1729 if (!list_empty(&poll->wait.entry)) {
1730 list_del_init(&poll->wait.entry);
1731 io_queue_async_work(req->ctx, req);
1732 }
1733 spin_unlock(&poll->head->lock);
1734
1735 list_del_init(&req->list);
1736 }
1737
1738 static void io_poll_remove_all(struct io_ring_ctx *ctx)
1739 {
1740 struct io_kiocb *req;
1741
1742 spin_lock_irq(&ctx->completion_lock);
1743 while (!list_empty(&ctx->cancel_list)) {
1744 req = list_first_entry(&ctx->cancel_list, struct io_kiocb,list);
1745 io_poll_remove_one(req);
1746 }
1747 spin_unlock_irq(&ctx->completion_lock);
1748 }
1749
1750
1751
1752
1753
1754 static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1755 {
1756 struct io_ring_ctx *ctx = req->ctx;
1757 struct io_kiocb *poll_req, *next;
1758 int ret = -ENOENT;
1759
1760 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1761 return -EINVAL;
1762 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
1763 sqe->poll_events)
1764 return -EINVAL;
1765
1766 spin_lock_irq(&ctx->completion_lock);
1767 list_for_each_entry_safe(poll_req, next, &ctx->cancel_list, list) {
1768 if (READ_ONCE(sqe->addr) == poll_req->user_data) {
1769 io_poll_remove_one(poll_req);
1770 ret = 0;
1771 break;
1772 }
1773 }
1774 spin_unlock_irq(&ctx->completion_lock);
1775
1776 io_cqring_add_event(req->ctx, sqe->user_data, ret);
1777 io_put_req(req);
1778 return 0;
1779 }
1780
1781 static void io_poll_complete(struct io_ring_ctx *ctx, struct io_kiocb *req,
1782 __poll_t mask)
1783 {
1784 req->poll.done = true;
1785 io_cqring_fill_event(ctx, req->user_data, mangle_poll(mask));
1786 io_commit_cqring(ctx);
1787 }
1788
1789 static void io_poll_complete_work(struct work_struct *work)
1790 {
1791 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
1792 struct io_poll_iocb *poll = &req->poll;
1793 struct poll_table_struct pt = { ._key = poll->events };
1794 struct io_ring_ctx *ctx = req->ctx;
1795 const struct cred *old_cred;
1796 __poll_t mask = 0;
1797
1798 old_cred = override_creds(ctx->creds);
1799
1800 if (!READ_ONCE(poll->canceled))
1801 mask = vfs_poll(poll->file, &pt) & poll->events;
1802
1803
1804
1805
1806
1807
1808
1809
1810 spin_lock_irq(&ctx->completion_lock);
1811 if (!mask && !READ_ONCE(poll->canceled)) {
1812 add_wait_queue(poll->head, &poll->wait);
1813 spin_unlock_irq(&ctx->completion_lock);
1814 goto out;
1815 }
1816 list_del_init(&req->list);
1817 io_poll_complete(ctx, req, mask);
1818 spin_unlock_irq(&ctx->completion_lock);
1819
1820 io_cqring_ev_posted(ctx);
1821 io_put_req(req);
1822 out:
1823 revert_creds(old_cred);
1824 }
1825
1826 static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
1827 void *key)
1828 {
1829 struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb,
1830 wait);
1831 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
1832 struct io_ring_ctx *ctx = req->ctx;
1833 __poll_t mask = key_to_poll(key);
1834 unsigned long flags;
1835
1836
1837 if (mask && !(mask & poll->events))
1838 return 0;
1839
1840 list_del_init(&poll->wait.entry);
1841
1842 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
1843 list_del(&req->list);
1844 io_poll_complete(ctx, req, mask);
1845 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1846
1847 io_cqring_ev_posted(ctx);
1848 io_put_req(req);
1849 } else {
1850 io_queue_async_work(ctx, req);
1851 }
1852
1853 return 1;
1854 }
1855
1856 struct io_poll_table {
1857 struct poll_table_struct pt;
1858 struct io_kiocb *req;
1859 int error;
1860 };
1861
1862 static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
1863 struct poll_table_struct *p)
1864 {
1865 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
1866
1867 if (unlikely(pt->req->poll.head)) {
1868 pt->error = -EINVAL;
1869 return;
1870 }
1871
1872 pt->error = 0;
1873 pt->req->poll.head = head;
1874 add_wait_queue(head, &pt->req->poll.wait);
1875 }
1876
1877 static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1878 {
1879 struct io_poll_iocb *poll = &req->poll;
1880 struct io_ring_ctx *ctx = req->ctx;
1881 struct io_poll_table ipt;
1882 bool cancel = false;
1883 __poll_t mask;
1884 u16 events;
1885
1886 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1887 return -EINVAL;
1888 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
1889 return -EINVAL;
1890 if (!poll->file)
1891 return -EBADF;
1892
1893 req->submit.sqe = NULL;
1894 INIT_WORK(&req->work, io_poll_complete_work);
1895 events = READ_ONCE(sqe->poll_events);
1896 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
1897
1898 poll->head = NULL;
1899 poll->done = false;
1900 poll->canceled = false;
1901
1902 ipt.pt._qproc = io_poll_queue_proc;
1903 ipt.pt._key = poll->events;
1904 ipt.req = req;
1905 ipt.error = -EINVAL;
1906
1907
1908 INIT_LIST_HEAD(&poll->wait.entry);
1909 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
1910
1911 INIT_LIST_HEAD(&req->list);
1912
1913 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
1914
1915 spin_lock_irq(&ctx->completion_lock);
1916 if (likely(poll->head)) {
1917 spin_lock(&poll->head->lock);
1918 if (unlikely(list_empty(&poll->wait.entry))) {
1919 if (ipt.error)
1920 cancel = true;
1921 ipt.error = 0;
1922 mask = 0;
1923 }
1924 if (mask || ipt.error)
1925 list_del_init(&poll->wait.entry);
1926 else if (cancel)
1927 WRITE_ONCE(poll->canceled, true);
1928 else if (!poll->done)
1929 list_add_tail(&req->list, &ctx->cancel_list);
1930 spin_unlock(&poll->head->lock);
1931 }
1932 if (mask) {
1933 ipt.error = 0;
1934 io_poll_complete(ctx, req, mask);
1935 }
1936 spin_unlock_irq(&ctx->completion_lock);
1937
1938 if (mask) {
1939 io_cqring_ev_posted(ctx);
1940 io_put_req(req);
1941 }
1942 return ipt.error;
1943 }
1944
1945 static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
1946 {
1947 struct io_ring_ctx *ctx;
1948 struct io_kiocb *req, *prev;
1949 unsigned long flags;
1950
1951 req = container_of(timer, struct io_kiocb, timeout.timer);
1952 ctx = req->ctx;
1953 atomic_inc(&ctx->cq_timeouts);
1954
1955 spin_lock_irqsave(&ctx->completion_lock, flags);
1956
1957
1958
1959
1960
1961
1962 prev = req;
1963 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
1964 prev->sequence++;
1965 list_del(&req->list);
1966
1967 io_cqring_fill_event(ctx, req->user_data, -ETIME);
1968 io_commit_cqring(ctx);
1969 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1970
1971 io_cqring_ev_posted(ctx);
1972
1973 io_put_req(req);
1974 return HRTIMER_NORESTART;
1975 }
1976
1977 static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1978 {
1979 unsigned count;
1980 struct io_ring_ctx *ctx = req->ctx;
1981 struct list_head *entry;
1982 struct timespec64 ts;
1983 unsigned span = 0;
1984
1985 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1986 return -EINVAL;
1987 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->timeout_flags ||
1988 sqe->len != 1)
1989 return -EINVAL;
1990
1991 if (get_timespec64(&ts, u64_to_user_ptr(sqe->addr)))
1992 return -EFAULT;
1993
1994 req->flags |= REQ_F_TIMEOUT;
1995
1996
1997
1998
1999
2000
2001 count = READ_ONCE(sqe->off);
2002 if (!count) {
2003 req->flags |= REQ_F_TIMEOUT_NOSEQ;
2004 spin_lock_irq(&ctx->completion_lock);
2005 entry = ctx->timeout_list.prev;
2006 goto add;
2007 }
2008
2009 req->sequence = ctx->cached_sq_head + count - 1;
2010
2011 req->submit.sequence = count;
2012
2013
2014
2015
2016
2017 spin_lock_irq(&ctx->completion_lock);
2018 list_for_each_prev(entry, &ctx->timeout_list) {
2019 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
2020 unsigned nxt_sq_head;
2021 long long tmp, tmp_nxt;
2022
2023 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
2024 continue;
2025
2026
2027
2028
2029
2030 tmp = (long long)ctx->cached_sq_head + count - 1;
2031 nxt_sq_head = nxt->sequence - nxt->submit.sequence + 1;
2032 tmp_nxt = (long long)nxt_sq_head + nxt->submit.sequence - 1;
2033
2034
2035
2036
2037
2038 if (ctx->cached_sq_head < nxt_sq_head)
2039 tmp += UINT_MAX;
2040
2041 if (tmp > tmp_nxt)
2042 break;
2043
2044
2045
2046
2047
2048 span++;
2049 nxt->sequence++;
2050 }
2051 req->sequence -= span;
2052 add:
2053 list_add(&req->list, entry);
2054 spin_unlock_irq(&ctx->completion_lock);
2055
2056 hrtimer_init(&req->timeout.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2057 req->timeout.timer.function = io_timeout_fn;
2058 hrtimer_start(&req->timeout.timer, timespec64_to_ktime(ts),
2059 HRTIMER_MODE_REL);
2060 return 0;
2061 }
2062
2063 static int io_req_defer(struct io_ring_ctx *ctx, struct io_kiocb *req,
2064 struct sqe_submit *s)
2065 {
2066 struct io_uring_sqe *sqe_copy;
2067
2068 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list))
2069 return 0;
2070
2071 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
2072 if (!sqe_copy)
2073 return -EAGAIN;
2074
2075 spin_lock_irq(&ctx->completion_lock);
2076 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list)) {
2077 spin_unlock_irq(&ctx->completion_lock);
2078 kfree(sqe_copy);
2079 return 0;
2080 }
2081
2082 memcpy(&req->submit, s, sizeof(*s));
2083 memcpy(sqe_copy, s->sqe, sizeof(*sqe_copy));
2084 req->submit.sqe = sqe_copy;
2085
2086 INIT_WORK(&req->work, io_sq_wq_submit_work);
2087 list_add_tail(&req->list, &ctx->defer_list);
2088 spin_unlock_irq(&ctx->completion_lock);
2089 return -EIOCBQUEUED;
2090 }
2091
2092 static int __io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
2093 const struct sqe_submit *s, bool force_nonblock)
2094 {
2095 int ret, opcode;
2096
2097 req->user_data = READ_ONCE(s->sqe->user_data);
2098
2099 if (unlikely(s->index >= ctx->sq_entries))
2100 return -EINVAL;
2101
2102 opcode = READ_ONCE(s->sqe->opcode);
2103 switch (opcode) {
2104 case IORING_OP_NOP:
2105 ret = io_nop(req, req->user_data);
2106 break;
2107 case IORING_OP_READV:
2108 if (unlikely(s->sqe->buf_index))
2109 return -EINVAL;
2110 ret = io_read(req, s, force_nonblock);
2111 break;
2112 case IORING_OP_WRITEV:
2113 if (unlikely(s->sqe->buf_index))
2114 return -EINVAL;
2115 ret = io_write(req, s, force_nonblock);
2116 break;
2117 case IORING_OP_READ_FIXED:
2118 ret = io_read(req, s, force_nonblock);
2119 break;
2120 case IORING_OP_WRITE_FIXED:
2121 ret = io_write(req, s, force_nonblock);
2122 break;
2123 case IORING_OP_FSYNC:
2124 ret = io_fsync(req, s->sqe, force_nonblock);
2125 break;
2126 case IORING_OP_POLL_ADD:
2127 ret = io_poll_add(req, s->sqe);
2128 break;
2129 case IORING_OP_POLL_REMOVE:
2130 ret = io_poll_remove(req, s->sqe);
2131 break;
2132 case IORING_OP_SYNC_FILE_RANGE:
2133 ret = io_sync_file_range(req, s->sqe, force_nonblock);
2134 break;
2135 case IORING_OP_SENDMSG:
2136 ret = io_sendmsg(req, s->sqe, force_nonblock);
2137 break;
2138 case IORING_OP_RECVMSG:
2139 ret = io_recvmsg(req, s->sqe, force_nonblock);
2140 break;
2141 case IORING_OP_TIMEOUT:
2142 ret = io_timeout(req, s->sqe);
2143 break;
2144 default:
2145 ret = -EINVAL;
2146 break;
2147 }
2148
2149 if (ret)
2150 return ret;
2151
2152 if (ctx->flags & IORING_SETUP_IOPOLL) {
2153 if (req->result == -EAGAIN)
2154 return -EAGAIN;
2155
2156
2157 if (s->needs_lock)
2158 mutex_lock(&ctx->uring_lock);
2159 io_iopoll_req_issued(req);
2160 if (s->needs_lock)
2161 mutex_unlock(&ctx->uring_lock);
2162 }
2163
2164 return 0;
2165 }
2166
2167 static struct async_list *io_async_list_from_sqe(struct io_ring_ctx *ctx,
2168 const struct io_uring_sqe *sqe)
2169 {
2170 switch (sqe->opcode) {
2171 case IORING_OP_READV:
2172 case IORING_OP_READ_FIXED:
2173 return &ctx->pending_async[READ];
2174 case IORING_OP_WRITEV:
2175 case IORING_OP_WRITE_FIXED:
2176 return &ctx->pending_async[WRITE];
2177 default:
2178 return NULL;
2179 }
2180 }
2181
2182 static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
2183 {
2184 u8 opcode = READ_ONCE(sqe->opcode);
2185
2186 return !(opcode == IORING_OP_READ_FIXED ||
2187 opcode == IORING_OP_WRITE_FIXED);
2188 }
2189
2190 static void io_sq_wq_submit_work(struct work_struct *work)
2191 {
2192 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
2193 struct fs_struct *old_fs_struct = current->fs;
2194 struct io_ring_ctx *ctx = req->ctx;
2195 struct mm_struct *cur_mm = NULL;
2196 struct async_list *async_list;
2197 const struct cred *old_cred;
2198 LIST_HEAD(req_list);
2199 mm_segment_t old_fs;
2200 int ret;
2201
2202 old_cred = override_creds(ctx->creds);
2203 async_list = io_async_list_from_sqe(ctx, req->submit.sqe);
2204 restart:
2205 do {
2206 struct sqe_submit *s = &req->submit;
2207 const struct io_uring_sqe *sqe = s->sqe;
2208 unsigned int flags = req->flags;
2209
2210
2211 req->rw.ki_flags &= ~IOCB_NOWAIT;
2212
2213 if (req->fs != current->fs && current->fs != old_fs_struct) {
2214 task_lock(current);
2215 if (req->fs)
2216 current->fs = req->fs;
2217 else
2218 current->fs = old_fs_struct;
2219 task_unlock(current);
2220 }
2221
2222 ret = 0;
2223 if (io_sqe_needs_user(sqe) && !cur_mm) {
2224 if (!mmget_not_zero(ctx->sqo_mm)) {
2225 ret = -EFAULT;
2226 } else {
2227 cur_mm = ctx->sqo_mm;
2228 use_mm(cur_mm);
2229 old_fs = get_fs();
2230 set_fs(USER_DS);
2231 }
2232 }
2233
2234 if (!ret) {
2235 s->has_user = cur_mm != NULL;
2236 s->needs_lock = true;
2237 do {
2238 ret = __io_submit_sqe(ctx, req, s, false);
2239
2240
2241
2242
2243
2244
2245 if (ret != -EAGAIN)
2246 break;
2247 cond_resched();
2248 } while (1);
2249 }
2250
2251
2252 io_put_req(req);
2253
2254 if (ret) {
2255 io_cqring_add_event(ctx, sqe->user_data, ret);
2256 io_put_req(req);
2257 }
2258
2259
2260 kfree(sqe);
2261
2262
2263 if (flags & (REQ_F_IO_DRAINED | REQ_F_LINK_DONE))
2264 goto out;
2265
2266 if (!async_list)
2267 break;
2268 if (!list_empty(&req_list)) {
2269 req = list_first_entry(&req_list, struct io_kiocb,
2270 list);
2271 list_del(&req->list);
2272 continue;
2273 }
2274 if (list_empty(&async_list->list))
2275 break;
2276
2277 req = NULL;
2278 spin_lock(&async_list->lock);
2279 if (list_empty(&async_list->list)) {
2280 spin_unlock(&async_list->lock);
2281 break;
2282 }
2283 list_splice_init(&async_list->list, &req_list);
2284 spin_unlock(&async_list->lock);
2285
2286 req = list_first_entry(&req_list, struct io_kiocb, list);
2287 list_del(&req->list);
2288 } while (req);
2289
2290
2291
2292
2293
2294
2295 if (async_list) {
2296 ret = atomic_dec_return(&async_list->cnt);
2297 while (!ret && !list_empty(&async_list->list)) {
2298 spin_lock(&async_list->lock);
2299 atomic_inc(&async_list->cnt);
2300 list_splice_init(&async_list->list, &req_list);
2301 spin_unlock(&async_list->lock);
2302
2303 if (!list_empty(&req_list)) {
2304 req = list_first_entry(&req_list,
2305 struct io_kiocb, list);
2306 list_del(&req->list);
2307 goto restart;
2308 }
2309 ret = atomic_dec_return(&async_list->cnt);
2310 }
2311 }
2312
2313 out:
2314 if (cur_mm) {
2315 set_fs(old_fs);
2316 unuse_mm(cur_mm);
2317 mmput(cur_mm);
2318 }
2319 revert_creds(old_cred);
2320 if (old_fs_struct) {
2321 task_lock(current);
2322 current->fs = old_fs_struct;
2323 task_unlock(current);
2324 }
2325 }
2326
2327
2328
2329
2330
2331
2332 static bool io_add_to_prev_work(struct async_list *list, struct io_kiocb *req)
2333 {
2334 bool ret;
2335
2336 if (!list)
2337 return false;
2338 if (!(req->flags & REQ_F_SEQ_PREV))
2339 return false;
2340 if (!atomic_read(&list->cnt))
2341 return false;
2342
2343 ret = true;
2344 spin_lock(&list->lock);
2345 list_add_tail(&req->list, &list->list);
2346
2347
2348
2349 smp_mb();
2350 if (!atomic_read(&list->cnt)) {
2351 list_del_init(&req->list);
2352 ret = false;
2353 }
2354 spin_unlock(&list->lock);
2355 return ret;
2356 }
2357
2358 static bool io_op_needs_file(const struct io_uring_sqe *sqe)
2359 {
2360 int op = READ_ONCE(sqe->opcode);
2361
2362 switch (op) {
2363 case IORING_OP_NOP:
2364 case IORING_OP_POLL_REMOVE:
2365 case IORING_OP_TIMEOUT:
2366 return false;
2367 default:
2368 return true;
2369 }
2370 }
2371
2372 static int io_req_set_file(struct io_ring_ctx *ctx, const struct sqe_submit *s,
2373 struct io_submit_state *state, struct io_kiocb *req)
2374 {
2375 unsigned flags;
2376 int fd;
2377
2378 flags = READ_ONCE(s->sqe->flags);
2379 fd = READ_ONCE(s->sqe->fd);
2380
2381 if (flags & IOSQE_IO_DRAIN)
2382 req->flags |= REQ_F_IO_DRAIN;
2383
2384
2385
2386
2387
2388 req->sequence = s->sequence;
2389
2390 if (!io_op_needs_file(s->sqe))
2391 return 0;
2392
2393 if (flags & IOSQE_FIXED_FILE) {
2394 if (unlikely(!ctx->user_files ||
2395 (unsigned) fd >= ctx->nr_user_files))
2396 return -EBADF;
2397 req->file = ctx->user_files[fd];
2398 req->flags |= REQ_F_FIXED_FILE;
2399 } else {
2400 if (s->needs_fixed_file)
2401 return -EBADF;
2402 req->file = io_file_get(state, fd);
2403 if (unlikely(!req->file))
2404 return -EBADF;
2405 }
2406
2407 return 0;
2408 }
2409
2410 static int __io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
2411 struct sqe_submit *s)
2412 {
2413 int ret;
2414
2415 ret = __io_submit_sqe(ctx, req, s, true);
2416
2417
2418
2419
2420
2421 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
2422 (req->flags & REQ_F_MUST_PUNT))) {
2423 struct io_uring_sqe *sqe_copy;
2424
2425 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
2426 if (sqe_copy) {
2427 struct async_list *list;
2428
2429 s->sqe = sqe_copy;
2430 memcpy(&req->submit, s, sizeof(*s));
2431 list = io_async_list_from_sqe(ctx, s->sqe);
2432 if (!io_add_to_prev_work(list, req)) {
2433 if (list)
2434 atomic_inc(&list->cnt);
2435 INIT_WORK(&req->work, io_sq_wq_submit_work);
2436 io_queue_async_work(ctx, req);
2437 }
2438
2439
2440
2441
2442
2443 return 0;
2444 }
2445 }
2446
2447
2448 io_put_req(req);
2449
2450
2451 if (ret) {
2452 io_cqring_add_event(ctx, req->user_data, ret);
2453 if (req->flags & REQ_F_LINK)
2454 req->flags |= REQ_F_FAIL_LINK;
2455 io_put_req(req);
2456 }
2457
2458 return ret;
2459 }
2460
2461 static int io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
2462 struct sqe_submit *s)
2463 {
2464 int ret;
2465
2466 ret = io_req_defer(ctx, req, s);
2467 if (ret) {
2468 if (ret != -EIOCBQUEUED) {
2469 io_free_req(req);
2470 io_cqring_add_event(ctx, s->sqe->user_data, ret);
2471 }
2472 return 0;
2473 }
2474
2475 return __io_queue_sqe(ctx, req, s);
2476 }
2477
2478 static int io_queue_link_head(struct io_ring_ctx *ctx, struct io_kiocb *req,
2479 struct sqe_submit *s, struct io_kiocb *shadow)
2480 {
2481 int ret;
2482 int need_submit = false;
2483
2484 if (!shadow)
2485 return io_queue_sqe(ctx, req, s);
2486
2487
2488
2489
2490
2491
2492 req->flags |= REQ_F_IO_DRAIN;
2493 ret = io_req_defer(ctx, req, s);
2494 if (ret) {
2495 if (ret != -EIOCBQUEUED) {
2496 io_free_req(req);
2497 __io_free_req(shadow);
2498 io_cqring_add_event(ctx, s->sqe->user_data, ret);
2499 return 0;
2500 }
2501 } else {
2502
2503
2504
2505
2506 need_submit = true;
2507 }
2508
2509
2510 spin_lock_irq(&ctx->completion_lock);
2511 list_add_tail(&shadow->list, &ctx->defer_list);
2512 spin_unlock_irq(&ctx->completion_lock);
2513
2514 if (need_submit)
2515 return __io_queue_sqe(ctx, req, s);
2516
2517 return 0;
2518 }
2519
2520 #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK)
2521
2522 static void io_submit_sqe(struct io_ring_ctx *ctx, struct sqe_submit *s,
2523 struct io_submit_state *state, struct io_kiocb **link)
2524 {
2525 struct io_uring_sqe *sqe_copy;
2526 struct io_kiocb *req;
2527 int ret;
2528
2529
2530 if (unlikely(s->sqe->flags & ~SQE_VALID_FLAGS)) {
2531 ret = -EINVAL;
2532 goto err;
2533 }
2534
2535 req = io_get_req(ctx, state);
2536 if (unlikely(!req)) {
2537 ret = -EAGAIN;
2538 goto err;
2539 }
2540
2541 ret = io_req_set_file(ctx, s, state, req);
2542 if (unlikely(ret)) {
2543 err_req:
2544 io_free_req(req);
2545 err:
2546 io_cqring_add_event(ctx, s->sqe->user_data, ret);
2547 return;
2548 }
2549
2550 req->user_data = s->sqe->user_data;
2551
2552 #if defined(CONFIG_NET)
2553 switch (READ_ONCE(s->sqe->opcode)) {
2554 case IORING_OP_SENDMSG:
2555 case IORING_OP_RECVMSG:
2556 spin_lock(¤t->fs->lock);
2557 if (!current->fs->in_exec) {
2558 req->fs = current->fs;
2559 req->fs->users++;
2560 }
2561 spin_unlock(¤t->fs->lock);
2562 if (!req->fs) {
2563 ret = -EAGAIN;
2564 goto err_req;
2565 }
2566 }
2567 #endif
2568
2569
2570
2571
2572
2573
2574
2575
2576 if (*link) {
2577 struct io_kiocb *prev = *link;
2578
2579 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
2580 if (!sqe_copy) {
2581 ret = -EAGAIN;
2582 goto err_req;
2583 }
2584
2585 s->sqe = sqe_copy;
2586 memcpy(&req->submit, s, sizeof(*s));
2587 list_add_tail(&req->list, &prev->link_list);
2588 } else if (s->sqe->flags & IOSQE_IO_LINK) {
2589 req->flags |= REQ_F_LINK;
2590
2591 memcpy(&req->submit, s, sizeof(*s));
2592 INIT_LIST_HEAD(&req->link_list);
2593 *link = req;
2594 } else {
2595 io_queue_sqe(ctx, req, s);
2596 }
2597 }
2598
2599
2600
2601
2602 static void io_submit_state_end(struct io_submit_state *state)
2603 {
2604 blk_finish_plug(&state->plug);
2605 io_file_put(state);
2606 if (state->free_reqs)
2607 kmem_cache_free_bulk(req_cachep, state->free_reqs,
2608 &state->reqs[state->cur_req]);
2609 }
2610
2611
2612
2613
2614 static void io_submit_state_start(struct io_submit_state *state,
2615 struct io_ring_ctx *ctx, unsigned max_ios)
2616 {
2617 blk_start_plug(&state->plug);
2618 state->free_reqs = 0;
2619 state->file = NULL;
2620 state->ios_left = max_ios;
2621 }
2622
2623 static void io_commit_sqring(struct io_ring_ctx *ctx)
2624 {
2625 struct io_rings *rings = ctx->rings;
2626
2627 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
2628
2629
2630
2631
2632
2633 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
2634 }
2635 }
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645 static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
2646 {
2647 struct io_rings *rings = ctx->rings;
2648 u32 *sq_array = ctx->sq_array;
2649 unsigned head;
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659 head = ctx->cached_sq_head;
2660
2661 if (head == smp_load_acquire(&rings->sq.tail))
2662 return false;
2663
2664 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
2665 if (head < ctx->sq_entries) {
2666 s->index = head;
2667 s->sqe = &ctx->sq_sqes[head];
2668 s->sequence = ctx->cached_sq_head;
2669 ctx->cached_sq_head++;
2670 return true;
2671 }
2672
2673
2674 ctx->cached_sq_head++;
2675 ctx->cached_sq_dropped++;
2676 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
2677 return false;
2678 }
2679
2680 static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
2681 bool has_user, bool mm_fault)
2682 {
2683 struct io_submit_state state, *statep = NULL;
2684 struct io_kiocb *link = NULL;
2685 struct io_kiocb *shadow_req = NULL;
2686 bool prev_was_link = false;
2687 int i, submitted = 0;
2688
2689 if (nr > IO_PLUG_THRESHOLD) {
2690 io_submit_state_start(&state, ctx, nr);
2691 statep = &state;
2692 }
2693
2694 for (i = 0; i < nr; i++) {
2695 struct sqe_submit s;
2696
2697 if (!io_get_sqring(ctx, &s))
2698 break;
2699
2700
2701
2702
2703
2704 if (!prev_was_link && link) {
2705 io_queue_link_head(ctx, link, &link->submit, shadow_req);
2706 link = NULL;
2707 shadow_req = NULL;
2708 }
2709 prev_was_link = (s.sqe->flags & IOSQE_IO_LINK) != 0;
2710
2711 if (link && (s.sqe->flags & IOSQE_IO_DRAIN)) {
2712 if (!shadow_req) {
2713 shadow_req = io_get_req(ctx, NULL);
2714 if (unlikely(!shadow_req))
2715 goto out;
2716 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
2717 refcount_dec(&shadow_req->refs);
2718 }
2719 shadow_req->sequence = s.sequence;
2720 }
2721
2722 out:
2723 if (unlikely(mm_fault)) {
2724 io_cqring_add_event(ctx, s.sqe->user_data,
2725 -EFAULT);
2726 } else {
2727 s.has_user = has_user;
2728 s.needs_lock = true;
2729 s.needs_fixed_file = true;
2730 io_submit_sqe(ctx, &s, statep, &link);
2731 submitted++;
2732 }
2733 }
2734
2735 if (link)
2736 io_queue_link_head(ctx, link, &link->submit, shadow_req);
2737 if (statep)
2738 io_submit_state_end(&state);
2739
2740 return submitted;
2741 }
2742
2743 static int io_sq_thread(void *data)
2744 {
2745 struct io_ring_ctx *ctx = data;
2746 struct mm_struct *cur_mm = NULL;
2747 const struct cred *old_cred;
2748 mm_segment_t old_fs;
2749 DEFINE_WAIT(wait);
2750 unsigned inflight;
2751 unsigned long timeout;
2752
2753 complete(&ctx->sqo_thread_started);
2754
2755 old_fs = get_fs();
2756 set_fs(USER_DS);
2757 old_cred = override_creds(ctx->creds);
2758
2759 timeout = inflight = 0;
2760 while (!kthread_should_park()) {
2761 bool mm_fault = false;
2762 unsigned int to_submit;
2763
2764 if (inflight) {
2765 unsigned nr_events = 0;
2766
2767 if (ctx->flags & IORING_SETUP_IOPOLL) {
2768
2769
2770
2771
2772
2773
2774
2775
2776 mutex_lock(&ctx->uring_lock);
2777 if (!list_empty(&ctx->poll_list))
2778 io_iopoll_getevents(ctx, &nr_events, 0);
2779 else
2780 inflight = 0;
2781 mutex_unlock(&ctx->uring_lock);
2782 } else {
2783
2784
2785
2786
2787 nr_events = inflight;
2788 }
2789
2790 inflight -= nr_events;
2791 if (!inflight)
2792 timeout = jiffies + ctx->sq_thread_idle;
2793 }
2794
2795 to_submit = io_sqring_entries(ctx);
2796 if (!to_submit) {
2797
2798
2799
2800
2801
2802
2803 if (cur_mm) {
2804 unuse_mm(cur_mm);
2805 mmput(cur_mm);
2806 cur_mm = NULL;
2807 }
2808
2809
2810
2811
2812
2813
2814 if (inflight || !time_after(jiffies, timeout)) {
2815 cond_resched();
2816 continue;
2817 }
2818
2819 prepare_to_wait(&ctx->sqo_wait, &wait,
2820 TASK_INTERRUPTIBLE);
2821
2822
2823 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
2824
2825 smp_mb();
2826
2827 to_submit = io_sqring_entries(ctx);
2828 if (!to_submit) {
2829 if (kthread_should_park()) {
2830 finish_wait(&ctx->sqo_wait, &wait);
2831 break;
2832 }
2833 if (signal_pending(current))
2834 flush_signals(current);
2835 schedule();
2836 finish_wait(&ctx->sqo_wait, &wait);
2837
2838 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
2839 continue;
2840 }
2841 finish_wait(&ctx->sqo_wait, &wait);
2842
2843 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
2844 }
2845
2846
2847 if (!cur_mm) {
2848 mm_fault = !mmget_not_zero(ctx->sqo_mm);
2849 if (!mm_fault) {
2850 use_mm(ctx->sqo_mm);
2851 cur_mm = ctx->sqo_mm;
2852 }
2853 }
2854
2855 to_submit = min(to_submit, ctx->sq_entries);
2856 inflight += io_submit_sqes(ctx, to_submit, cur_mm != NULL,
2857 mm_fault);
2858
2859
2860 io_commit_sqring(ctx);
2861 }
2862
2863 set_fs(old_fs);
2864 if (cur_mm) {
2865 unuse_mm(cur_mm);
2866 mmput(cur_mm);
2867 }
2868 revert_creds(old_cred);
2869
2870 kthread_parkme();
2871
2872 return 0;
2873 }
2874
2875 static int io_ring_submit(struct io_ring_ctx *ctx, unsigned int to_submit)
2876 {
2877 struct io_submit_state state, *statep = NULL;
2878 struct io_kiocb *link = NULL;
2879 struct io_kiocb *shadow_req = NULL;
2880 bool prev_was_link = false;
2881 int i, submit = 0;
2882
2883 if (to_submit > IO_PLUG_THRESHOLD) {
2884 io_submit_state_start(&state, ctx, to_submit);
2885 statep = &state;
2886 }
2887
2888 for (i = 0; i < to_submit; i++) {
2889 struct sqe_submit s;
2890
2891 if (!io_get_sqring(ctx, &s))
2892 break;
2893
2894
2895
2896
2897
2898 if (!prev_was_link && link) {
2899 io_queue_link_head(ctx, link, &link->submit, shadow_req);
2900 link = NULL;
2901 shadow_req = NULL;
2902 }
2903 prev_was_link = (s.sqe->flags & IOSQE_IO_LINK) != 0;
2904
2905 if (link && (s.sqe->flags & IOSQE_IO_DRAIN)) {
2906 if (!shadow_req) {
2907 shadow_req = io_get_req(ctx, NULL);
2908 if (unlikely(!shadow_req))
2909 goto out;
2910 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
2911 refcount_dec(&shadow_req->refs);
2912 }
2913 shadow_req->sequence = s.sequence;
2914 }
2915
2916 out:
2917 s.has_user = true;
2918 s.needs_lock = false;
2919 s.needs_fixed_file = false;
2920 submit++;
2921 io_submit_sqe(ctx, &s, statep, &link);
2922 }
2923
2924 if (link)
2925 io_queue_link_head(ctx, link, &link->submit, shadow_req);
2926 if (statep)
2927 io_submit_state_end(statep);
2928
2929 io_commit_sqring(ctx);
2930
2931 return submit;
2932 }
2933
2934 struct io_wait_queue {
2935 struct wait_queue_entry wq;
2936 struct io_ring_ctx *ctx;
2937 unsigned to_wait;
2938 unsigned nr_timeouts;
2939 };
2940
2941 static inline bool io_should_wake(struct io_wait_queue *iowq)
2942 {
2943 struct io_ring_ctx *ctx = iowq->ctx;
2944
2945
2946
2947
2948
2949
2950 return io_cqring_events(ctx->rings) >= iowq->to_wait ||
2951 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
2952 }
2953
2954 static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
2955 int wake_flags, void *key)
2956 {
2957 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
2958 wq);
2959
2960 if (!io_should_wake(iowq))
2961 return -1;
2962
2963 return autoremove_wake_function(curr, mode, wake_flags, key);
2964 }
2965
2966
2967
2968
2969
2970 static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
2971 const sigset_t __user *sig, size_t sigsz)
2972 {
2973 struct io_wait_queue iowq = {
2974 .wq = {
2975 .private = current,
2976 .func = io_wake_function,
2977 .entry = LIST_HEAD_INIT(iowq.wq.entry),
2978 },
2979 .ctx = ctx,
2980 .to_wait = min_events,
2981 };
2982 struct io_rings *rings = ctx->rings;
2983 int ret;
2984
2985 if (io_cqring_events(rings) >= min_events)
2986 return 0;
2987
2988 if (sig) {
2989 #ifdef CONFIG_COMPAT
2990 if (in_compat_syscall())
2991 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
2992 sigsz);
2993 else
2994 #endif
2995 ret = set_user_sigmask(sig, sigsz);
2996
2997 if (ret)
2998 return ret;
2999 }
3000
3001 ret = 0;
3002 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
3003 do {
3004 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
3005 TASK_INTERRUPTIBLE);
3006 if (io_should_wake(&iowq))
3007 break;
3008 schedule();
3009 if (signal_pending(current)) {
3010 ret = -ERESTARTSYS;
3011 break;
3012 }
3013 } while (1);
3014 finish_wait(&ctx->wait, &iowq.wq);
3015
3016 restore_saved_sigmask_unless(ret == -ERESTARTSYS);
3017 if (ret == -ERESTARTSYS)
3018 ret = -EINTR;
3019
3020 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
3021 }
3022
3023 static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
3024 {
3025 #if defined(CONFIG_UNIX)
3026 if (ctx->ring_sock) {
3027 struct sock *sock = ctx->ring_sock->sk;
3028 struct sk_buff *skb;
3029
3030 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
3031 kfree_skb(skb);
3032 }
3033 #else
3034 int i;
3035
3036 for (i = 0; i < ctx->nr_user_files; i++)
3037 fput(ctx->user_files[i]);
3038 #endif
3039 }
3040
3041 static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
3042 {
3043 if (!ctx->user_files)
3044 return -ENXIO;
3045
3046 __io_sqe_files_unregister(ctx);
3047 kfree(ctx->user_files);
3048 ctx->user_files = NULL;
3049 ctx->nr_user_files = 0;
3050 return 0;
3051 }
3052
3053 static void io_sq_thread_stop(struct io_ring_ctx *ctx)
3054 {
3055 if (ctx->sqo_thread) {
3056 wait_for_completion(&ctx->sqo_thread_started);
3057
3058
3059
3060
3061
3062 kthread_park(ctx->sqo_thread);
3063 kthread_stop(ctx->sqo_thread);
3064 ctx->sqo_thread = NULL;
3065 }
3066 }
3067
3068 static void io_finish_async(struct io_ring_ctx *ctx)
3069 {
3070 int i;
3071
3072 io_sq_thread_stop(ctx);
3073
3074 for (i = 0; i < ARRAY_SIZE(ctx->sqo_wq); i++) {
3075 if (ctx->sqo_wq[i]) {
3076 destroy_workqueue(ctx->sqo_wq[i]);
3077 ctx->sqo_wq[i] = NULL;
3078 }
3079 }
3080 }
3081
3082 #if defined(CONFIG_UNIX)
3083 static void io_destruct_skb(struct sk_buff *skb)
3084 {
3085 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
3086 int i;
3087
3088 for (i = 0; i < ARRAY_SIZE(ctx->sqo_wq); i++)
3089 if (ctx->sqo_wq[i])
3090 flush_workqueue(ctx->sqo_wq[i]);
3091
3092 unix_destruct_scm(skb);
3093 }
3094
3095
3096
3097
3098
3099
3100 static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
3101 {
3102 struct sock *sk = ctx->ring_sock->sk;
3103 struct scm_fp_list *fpl;
3104 struct sk_buff *skb;
3105 int i;
3106
3107 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
3108 if (!fpl)
3109 return -ENOMEM;
3110
3111 skb = alloc_skb(0, GFP_KERNEL);
3112 if (!skb) {
3113 kfree(fpl);
3114 return -ENOMEM;
3115 }
3116
3117 skb->sk = sk;
3118 skb->destructor = io_destruct_skb;
3119
3120 fpl->user = get_uid(ctx->user);
3121 for (i = 0; i < nr; i++) {
3122 fpl->fp[i] = get_file(ctx->user_files[i + offset]);
3123 unix_inflight(fpl->user, fpl->fp[i]);
3124 }
3125
3126 fpl->max = fpl->count = nr;
3127 UNIXCB(skb).fp = fpl;
3128 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
3129 skb_queue_head(&sk->sk_receive_queue, skb);
3130
3131 for (i = 0; i < nr; i++)
3132 fput(fpl->fp[i]);
3133
3134 return 0;
3135 }
3136
3137
3138
3139
3140
3141
3142 static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3143 {
3144 unsigned left, total;
3145 int ret = 0;
3146
3147 total = 0;
3148 left = ctx->nr_user_files;
3149 while (left) {
3150 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
3151
3152 ret = __io_sqe_files_scm(ctx, this_files, total);
3153 if (ret)
3154 break;
3155 left -= this_files;
3156 total += this_files;
3157 }
3158
3159 if (!ret)
3160 return 0;
3161
3162 while (total < ctx->nr_user_files) {
3163 fput(ctx->user_files[total]);
3164 total++;
3165 }
3166
3167 return ret;
3168 }
3169 #else
3170 static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3171 {
3172 return 0;
3173 }
3174 #endif
3175
3176 static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
3177 unsigned nr_args)
3178 {
3179 __s32 __user *fds = (__s32 __user *) arg;
3180 int fd, ret = 0;
3181 unsigned i;
3182
3183 if (ctx->user_files)
3184 return -EBUSY;
3185 if (!nr_args)
3186 return -EINVAL;
3187 if (nr_args > IORING_MAX_FIXED_FILES)
3188 return -EMFILE;
3189
3190 ctx->user_files = kcalloc(nr_args, sizeof(struct file *), GFP_KERNEL);
3191 if (!ctx->user_files)
3192 return -ENOMEM;
3193
3194 for (i = 0; i < nr_args; i++) {
3195 ret = -EFAULT;
3196 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
3197 break;
3198
3199 ctx->user_files[i] = fget(fd);
3200
3201 ret = -EBADF;
3202 if (!ctx->user_files[i])
3203 break;
3204
3205
3206
3207
3208
3209
3210
3211 if (ctx->user_files[i]->f_op == &io_uring_fops) {
3212 fput(ctx->user_files[i]);
3213 break;
3214 }
3215 ctx->nr_user_files++;
3216 ret = 0;
3217 }
3218
3219 if (ret) {
3220 for (i = 0; i < ctx->nr_user_files; i++)
3221 fput(ctx->user_files[i]);
3222
3223 kfree(ctx->user_files);
3224 ctx->user_files = NULL;
3225 ctx->nr_user_files = 0;
3226 return ret;
3227 }
3228
3229 ret = io_sqe_files_scm(ctx);
3230 if (ret)
3231 io_sqe_files_unregister(ctx);
3232
3233 return ret;
3234 }
3235
3236 static int io_sq_offload_start(struct io_ring_ctx *ctx,
3237 struct io_uring_params *p)
3238 {
3239 int ret;
3240
3241 mmgrab(current->mm);
3242 ctx->sqo_mm = current->mm;
3243
3244 if (ctx->flags & IORING_SETUP_SQPOLL) {
3245 ret = -EPERM;
3246 if (!capable(CAP_SYS_ADMIN))
3247 goto err;
3248
3249 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
3250 if (!ctx->sq_thread_idle)
3251 ctx->sq_thread_idle = HZ;
3252
3253 if (p->flags & IORING_SETUP_SQ_AFF) {
3254 int cpu = p->sq_thread_cpu;
3255
3256 ret = -EINVAL;
3257 if (cpu >= nr_cpu_ids)
3258 goto err;
3259 if (!cpu_online(cpu))
3260 goto err;
3261
3262 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
3263 ctx, cpu,
3264 "io_uring-sq");
3265 } else {
3266 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
3267 "io_uring-sq");
3268 }
3269 if (IS_ERR(ctx->sqo_thread)) {
3270 ret = PTR_ERR(ctx->sqo_thread);
3271 ctx->sqo_thread = NULL;
3272 goto err;
3273 }
3274 wake_up_process(ctx->sqo_thread);
3275 } else if (p->flags & IORING_SETUP_SQ_AFF) {
3276
3277 ret = -EINVAL;
3278 goto err;
3279 }
3280
3281
3282 ctx->sqo_wq[0] = alloc_workqueue("io_ring-wq",
3283 WQ_UNBOUND | WQ_FREEZABLE,
3284 min(ctx->sq_entries - 1, 2 * num_online_cpus()));
3285 if (!ctx->sqo_wq[0]) {
3286 ret = -ENOMEM;
3287 goto err;
3288 }
3289
3290
3291
3292
3293
3294
3295
3296
3297 ctx->sqo_wq[1] = alloc_workqueue("io_ring-write-wq",
3298 WQ_UNBOUND | WQ_FREEZABLE, 2);
3299 if (!ctx->sqo_wq[1]) {
3300 ret = -ENOMEM;
3301 goto err;
3302 }
3303
3304 return 0;
3305 err:
3306 io_finish_async(ctx);
3307 mmdrop(ctx->sqo_mm);
3308 ctx->sqo_mm = NULL;
3309 return ret;
3310 }
3311
3312 static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
3313 {
3314 atomic_long_sub(nr_pages, &user->locked_vm);
3315 }
3316
3317 static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
3318 {
3319 unsigned long page_limit, cur_pages, new_pages;
3320
3321
3322 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
3323
3324 do {
3325 cur_pages = atomic_long_read(&user->locked_vm);
3326 new_pages = cur_pages + nr_pages;
3327 if (new_pages > page_limit)
3328 return -ENOMEM;
3329 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
3330 new_pages) != cur_pages);
3331
3332 return 0;
3333 }
3334
3335 static void io_mem_free(void *ptr)
3336 {
3337 struct page *page;
3338
3339 if (!ptr)
3340 return;
3341
3342 page = virt_to_head_page(ptr);
3343 if (put_page_testzero(page))
3344 free_compound_page(page);
3345 }
3346
3347 static void *io_mem_alloc(size_t size)
3348 {
3349 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
3350 __GFP_NORETRY;
3351
3352 return (void *) __get_free_pages(gfp_flags, get_order(size));
3353 }
3354
3355 static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
3356 size_t *sq_offset)
3357 {
3358 struct io_rings *rings;
3359 size_t off, sq_array_size;
3360
3361 off = struct_size(rings, cqes, cq_entries);
3362 if (off == SIZE_MAX)
3363 return SIZE_MAX;
3364
3365 #ifdef CONFIG_SMP
3366 off = ALIGN(off, SMP_CACHE_BYTES);
3367 if (off == 0)
3368 return SIZE_MAX;
3369 #endif
3370
3371 sq_array_size = array_size(sizeof(u32), sq_entries);
3372 if (sq_array_size == SIZE_MAX)
3373 return SIZE_MAX;
3374
3375 if (check_add_overflow(off, sq_array_size, &off))
3376 return SIZE_MAX;
3377
3378 if (sq_offset)
3379 *sq_offset = off;
3380
3381 return off;
3382 }
3383
3384 static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
3385 {
3386 size_t pages;
3387
3388 pages = (size_t)1 << get_order(
3389 rings_size(sq_entries, cq_entries, NULL));
3390 pages += (size_t)1 << get_order(
3391 array_size(sizeof(struct io_uring_sqe), sq_entries));
3392
3393 return pages;
3394 }
3395
3396 static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
3397 {
3398 int i, j;
3399
3400 if (!ctx->user_bufs)
3401 return -ENXIO;
3402
3403 for (i = 0; i < ctx->nr_user_bufs; i++) {
3404 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3405
3406 for (j = 0; j < imu->nr_bvecs; j++)
3407 put_user_page(imu->bvec[j].bv_page);
3408
3409 if (ctx->account_mem)
3410 io_unaccount_mem(ctx->user, imu->nr_bvecs);
3411 kvfree(imu->bvec);
3412 imu->nr_bvecs = 0;
3413 }
3414
3415 kfree(ctx->user_bufs);
3416 ctx->user_bufs = NULL;
3417 ctx->nr_user_bufs = 0;
3418 return 0;
3419 }
3420
3421 static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
3422 void __user *arg, unsigned index)
3423 {
3424 struct iovec __user *src;
3425
3426 #ifdef CONFIG_COMPAT
3427 if (ctx->compat) {
3428 struct compat_iovec __user *ciovs;
3429 struct compat_iovec ciov;
3430
3431 ciovs = (struct compat_iovec __user *) arg;
3432 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
3433 return -EFAULT;
3434
3435 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
3436 dst->iov_len = ciov.iov_len;
3437 return 0;
3438 }
3439 #endif
3440 src = (struct iovec __user *) arg;
3441 if (copy_from_user(dst, &src[index], sizeof(*dst)))
3442 return -EFAULT;
3443 return 0;
3444 }
3445
3446 static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
3447 unsigned nr_args)
3448 {
3449 struct vm_area_struct **vmas = NULL;
3450 struct page **pages = NULL;
3451 int i, j, got_pages = 0;
3452 int ret = -EINVAL;
3453
3454 if (ctx->user_bufs)
3455 return -EBUSY;
3456 if (!nr_args || nr_args > UIO_MAXIOV)
3457 return -EINVAL;
3458
3459 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
3460 GFP_KERNEL);
3461 if (!ctx->user_bufs)
3462 return -ENOMEM;
3463
3464 for (i = 0; i < nr_args; i++) {
3465 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3466 unsigned long off, start, end, ubuf;
3467 int pret, nr_pages;
3468 struct iovec iov;
3469 size_t size;
3470
3471 ret = io_copy_iov(ctx, &iov, arg, i);
3472 if (ret)
3473 goto err;
3474
3475
3476
3477
3478
3479
3480 ret = -EFAULT;
3481 if (!iov.iov_base || !iov.iov_len)
3482 goto err;
3483
3484
3485 if (iov.iov_len > SZ_1G)
3486 goto err;
3487
3488 ubuf = (unsigned long) iov.iov_base;
3489 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
3490 start = ubuf >> PAGE_SHIFT;
3491 nr_pages = end - start;
3492
3493 if (ctx->account_mem) {
3494 ret = io_account_mem(ctx->user, nr_pages);
3495 if (ret)
3496 goto err;
3497 }
3498
3499 ret = 0;
3500 if (!pages || nr_pages > got_pages) {
3501 kvfree(vmas);
3502 kvfree(pages);
3503 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
3504 GFP_KERNEL);
3505 vmas = kvmalloc_array(nr_pages,
3506 sizeof(struct vm_area_struct *),
3507 GFP_KERNEL);
3508 if (!pages || !vmas) {
3509 ret = -ENOMEM;
3510 if (ctx->account_mem)
3511 io_unaccount_mem(ctx->user, nr_pages);
3512 goto err;
3513 }
3514 got_pages = nr_pages;
3515 }
3516
3517 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
3518 GFP_KERNEL);
3519 ret = -ENOMEM;
3520 if (!imu->bvec) {
3521 if (ctx->account_mem)
3522 io_unaccount_mem(ctx->user, nr_pages);
3523 goto err;
3524 }
3525
3526 ret = 0;
3527 down_read(¤t->mm->mmap_sem);
3528 pret = get_user_pages(ubuf, nr_pages,
3529 FOLL_WRITE | FOLL_LONGTERM,
3530 pages, vmas);
3531 if (pret == nr_pages) {
3532
3533 for (j = 0; j < nr_pages; j++) {
3534 struct vm_area_struct *vma = vmas[j];
3535
3536 if (vma->vm_file &&
3537 !is_file_hugepages(vma->vm_file)) {
3538 ret = -EOPNOTSUPP;
3539 break;
3540 }
3541 }
3542 } else {
3543 ret = pret < 0 ? pret : -EFAULT;
3544 }
3545 up_read(¤t->mm->mmap_sem);
3546 if (ret) {
3547
3548
3549
3550
3551 if (pret > 0)
3552 put_user_pages(pages, pret);
3553 if (ctx->account_mem)
3554 io_unaccount_mem(ctx->user, nr_pages);
3555 kvfree(imu->bvec);
3556 goto err;
3557 }
3558
3559 off = ubuf & ~PAGE_MASK;
3560 size = iov.iov_len;
3561 for (j = 0; j < nr_pages; j++) {
3562 size_t vec_len;
3563
3564 vec_len = min_t(size_t, size, PAGE_SIZE - off);
3565 imu->bvec[j].bv_page = pages[j];
3566 imu->bvec[j].bv_len = vec_len;
3567 imu->bvec[j].bv_offset = off;
3568 off = 0;
3569 size -= vec_len;
3570 }
3571
3572 imu->ubuf = ubuf;
3573 imu->len = iov.iov_len;
3574 imu->nr_bvecs = nr_pages;
3575
3576 ctx->nr_user_bufs++;
3577 }
3578 kvfree(pages);
3579 kvfree(vmas);
3580 return 0;
3581 err:
3582 kvfree(pages);
3583 kvfree(vmas);
3584 io_sqe_buffer_unregister(ctx);
3585 return ret;
3586 }
3587
3588 static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
3589 {
3590 __s32 __user *fds = arg;
3591 int fd;
3592
3593 if (ctx->cq_ev_fd)
3594 return -EBUSY;
3595
3596 if (copy_from_user(&fd, fds, sizeof(*fds)))
3597 return -EFAULT;
3598
3599 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
3600 if (IS_ERR(ctx->cq_ev_fd)) {
3601 int ret = PTR_ERR(ctx->cq_ev_fd);
3602 ctx->cq_ev_fd = NULL;
3603 return ret;
3604 }
3605
3606 return 0;
3607 }
3608
3609 static int io_eventfd_unregister(struct io_ring_ctx *ctx)
3610 {
3611 if (ctx->cq_ev_fd) {
3612 eventfd_ctx_put(ctx->cq_ev_fd);
3613 ctx->cq_ev_fd = NULL;
3614 return 0;
3615 }
3616
3617 return -ENXIO;
3618 }
3619
3620 static void io_ring_ctx_free(struct io_ring_ctx *ctx)
3621 {
3622 io_finish_async(ctx);
3623 if (ctx->sqo_mm)
3624 mmdrop(ctx->sqo_mm);
3625
3626 io_iopoll_reap_events(ctx);
3627 io_sqe_buffer_unregister(ctx);
3628 io_sqe_files_unregister(ctx);
3629 io_eventfd_unregister(ctx);
3630
3631 #if defined(CONFIG_UNIX)
3632 if (ctx->ring_sock) {
3633 ctx->ring_sock->file = NULL;
3634 sock_release(ctx->ring_sock);
3635 }
3636 #endif
3637
3638 io_mem_free(ctx->rings);
3639 io_mem_free(ctx->sq_sqes);
3640
3641 percpu_ref_exit(&ctx->refs);
3642 if (ctx->account_mem)
3643 io_unaccount_mem(ctx->user,
3644 ring_pages(ctx->sq_entries, ctx->cq_entries));
3645 free_uid(ctx->user);
3646 if (ctx->creds)
3647 put_cred(ctx->creds);
3648 kfree(ctx);
3649 }
3650
3651 static __poll_t io_uring_poll(struct file *file, poll_table *wait)
3652 {
3653 struct io_ring_ctx *ctx = file->private_data;
3654 __poll_t mask = 0;
3655
3656 poll_wait(file, &ctx->cq_wait, wait);
3657
3658
3659
3660
3661 smp_rmb();
3662 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
3663 ctx->rings->sq_ring_entries)
3664 mask |= EPOLLOUT | EPOLLWRNORM;
3665 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
3666 mask |= EPOLLIN | EPOLLRDNORM;
3667
3668 return mask;
3669 }
3670
3671 static int io_uring_fasync(int fd, struct file *file, int on)
3672 {
3673 struct io_ring_ctx *ctx = file->private_data;
3674
3675 return fasync_helper(fd, file, on, &ctx->cq_fasync);
3676 }
3677
3678 static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
3679 {
3680 mutex_lock(&ctx->uring_lock);
3681 percpu_ref_kill(&ctx->refs);
3682 mutex_unlock(&ctx->uring_lock);
3683
3684 io_kill_timeouts(ctx);
3685 io_poll_remove_all(ctx);
3686 io_iopoll_reap_events(ctx);
3687 wait_for_completion(&ctx->ctx_done);
3688 io_ring_ctx_free(ctx);
3689 }
3690
3691 static int io_uring_release(struct inode *inode, struct file *file)
3692 {
3693 struct io_ring_ctx *ctx = file->private_data;
3694
3695 file->private_data = NULL;
3696 io_ring_ctx_wait_and_kill(ctx);
3697 return 0;
3698 }
3699
3700 static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
3701 {
3702 loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT;
3703 unsigned long sz = vma->vm_end - vma->vm_start;
3704 struct io_ring_ctx *ctx = file->private_data;
3705 unsigned long pfn;
3706 struct page *page;
3707 void *ptr;
3708
3709 switch (offset) {
3710 case IORING_OFF_SQ_RING:
3711 case IORING_OFF_CQ_RING:
3712 ptr = ctx->rings;
3713 break;
3714 case IORING_OFF_SQES:
3715 ptr = ctx->sq_sqes;
3716 break;
3717 default:
3718 return -EINVAL;
3719 }
3720
3721 page = virt_to_head_page(ptr);
3722 if (sz > page_size(page))
3723 return -EINVAL;
3724
3725 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
3726 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
3727 }
3728
3729 SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
3730 u32, min_complete, u32, flags, const sigset_t __user *, sig,
3731 size_t, sigsz)
3732 {
3733 struct io_ring_ctx *ctx;
3734 long ret = -EBADF;
3735 int submitted = 0;
3736 struct fd f;
3737
3738 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
3739 return -EINVAL;
3740
3741 f = fdget(fd);
3742 if (!f.file)
3743 return -EBADF;
3744
3745 ret = -EOPNOTSUPP;
3746 if (f.file->f_op != &io_uring_fops)
3747 goto out_fput;
3748
3749 ret = -ENXIO;
3750 ctx = f.file->private_data;
3751 if (!percpu_ref_tryget(&ctx->refs))
3752 goto out_fput;
3753
3754
3755
3756
3757
3758
3759 ret = 0;
3760 if (ctx->flags & IORING_SETUP_SQPOLL) {
3761 if (flags & IORING_ENTER_SQ_WAKEUP)
3762 wake_up(&ctx->sqo_wait);
3763 submitted = to_submit;
3764 } else if (to_submit) {
3765 to_submit = min(to_submit, ctx->sq_entries);
3766
3767 mutex_lock(&ctx->uring_lock);
3768 submitted = io_ring_submit(ctx, to_submit);
3769 mutex_unlock(&ctx->uring_lock);
3770
3771 if (submitted != to_submit)
3772 goto out;
3773 }
3774 if (flags & IORING_ENTER_GETEVENTS) {
3775 unsigned nr_events = 0;
3776
3777 min_complete = min(min_complete, ctx->cq_entries);
3778
3779 if (ctx->flags & IORING_SETUP_IOPOLL) {
3780 ret = io_iopoll_check(ctx, &nr_events, min_complete);
3781 } else {
3782 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
3783 }
3784 }
3785
3786 out:
3787 percpu_ref_put(&ctx->refs);
3788 out_fput:
3789 fdput(f);
3790 return submitted ? submitted : ret;
3791 }
3792
3793 static const struct file_operations io_uring_fops = {
3794 .release = io_uring_release,
3795 .mmap = io_uring_mmap,
3796 .poll = io_uring_poll,
3797 .fasync = io_uring_fasync,
3798 };
3799
3800 static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
3801 struct io_uring_params *p)
3802 {
3803 struct io_rings *rings;
3804 size_t size, sq_array_offset;
3805
3806 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
3807 if (size == SIZE_MAX)
3808 return -EOVERFLOW;
3809
3810 rings = io_mem_alloc(size);
3811 if (!rings)
3812 return -ENOMEM;
3813
3814 ctx->rings = rings;
3815 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
3816 rings->sq_ring_mask = p->sq_entries - 1;
3817 rings->cq_ring_mask = p->cq_entries - 1;
3818 rings->sq_ring_entries = p->sq_entries;
3819 rings->cq_ring_entries = p->cq_entries;
3820 ctx->sq_mask = rings->sq_ring_mask;
3821 ctx->cq_mask = rings->cq_ring_mask;
3822 ctx->sq_entries = rings->sq_ring_entries;
3823 ctx->cq_entries = rings->cq_ring_entries;
3824
3825 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
3826 if (size == SIZE_MAX) {
3827 io_mem_free(ctx->rings);
3828 ctx->rings = NULL;
3829 return -EOVERFLOW;
3830 }
3831
3832 ctx->sq_sqes = io_mem_alloc(size);
3833 if (!ctx->sq_sqes) {
3834 io_mem_free(ctx->rings);
3835 ctx->rings = NULL;
3836 return -ENOMEM;
3837 }
3838
3839 return 0;
3840 }
3841
3842
3843
3844
3845
3846
3847
3848 static int io_uring_get_fd(struct io_ring_ctx *ctx)
3849 {
3850 struct file *file;
3851 int ret;
3852
3853 #if defined(CONFIG_UNIX)
3854 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
3855 &ctx->ring_sock);
3856 if (ret)
3857 return ret;
3858 #endif
3859
3860 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
3861 if (ret < 0)
3862 goto err;
3863
3864 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
3865 O_RDWR | O_CLOEXEC);
3866 if (IS_ERR(file)) {
3867 put_unused_fd(ret);
3868 ret = PTR_ERR(file);
3869 goto err;
3870 }
3871
3872 #if defined(CONFIG_UNIX)
3873 ctx->ring_sock->file = file;
3874 ctx->ring_sock->sk->sk_user_data = ctx;
3875 #endif
3876 fd_install(ret, file);
3877 return ret;
3878 err:
3879 #if defined(CONFIG_UNIX)
3880 sock_release(ctx->ring_sock);
3881 ctx->ring_sock = NULL;
3882 #endif
3883 return ret;
3884 }
3885
3886 static int io_uring_create(unsigned entries, struct io_uring_params *p)
3887 {
3888 struct user_struct *user = NULL;
3889 struct io_ring_ctx *ctx;
3890 bool account_mem;
3891 int ret;
3892
3893 if (!entries || entries > IORING_MAX_ENTRIES)
3894 return -EINVAL;
3895
3896
3897
3898
3899
3900
3901
3902 p->sq_entries = roundup_pow_of_two(entries);
3903 p->cq_entries = 2 * p->sq_entries;
3904
3905 user = get_uid(current_user());
3906 account_mem = !capable(CAP_IPC_LOCK);
3907
3908 if (account_mem) {
3909 ret = io_account_mem(user,
3910 ring_pages(p->sq_entries, p->cq_entries));
3911 if (ret) {
3912 free_uid(user);
3913 return ret;
3914 }
3915 }
3916
3917 ctx = io_ring_ctx_alloc(p);
3918 if (!ctx) {
3919 if (account_mem)
3920 io_unaccount_mem(user, ring_pages(p->sq_entries,
3921 p->cq_entries));
3922 free_uid(user);
3923 return -ENOMEM;
3924 }
3925 ctx->compat = in_compat_syscall();
3926 ctx->account_mem = account_mem;
3927 ctx->user = user;
3928
3929 ctx->creds = get_current_cred();
3930 if (!ctx->creds) {
3931 ret = -ENOMEM;
3932 goto err;
3933 }
3934
3935 ret = io_allocate_scq_urings(ctx, p);
3936 if (ret)
3937 goto err;
3938
3939 ret = io_sq_offload_start(ctx, p);
3940 if (ret)
3941 goto err;
3942
3943 memset(&p->sq_off, 0, sizeof(p->sq_off));
3944 p->sq_off.head = offsetof(struct io_rings, sq.head);
3945 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
3946 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
3947 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
3948 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
3949 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
3950 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
3951
3952 memset(&p->cq_off, 0, sizeof(p->cq_off));
3953 p->cq_off.head = offsetof(struct io_rings, cq.head);
3954 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
3955 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
3956 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
3957 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
3958 p->cq_off.cqes = offsetof(struct io_rings, cqes);
3959
3960
3961
3962
3963
3964 ret = io_uring_get_fd(ctx);
3965 if (ret < 0)
3966 goto err;
3967
3968 p->features = IORING_FEAT_SINGLE_MMAP;
3969 return ret;
3970 err:
3971 io_ring_ctx_wait_and_kill(ctx);
3972 return ret;
3973 }
3974
3975
3976
3977
3978
3979
3980 static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
3981 {
3982 struct io_uring_params p;
3983 long ret;
3984 int i;
3985
3986 if (copy_from_user(&p, params, sizeof(p)))
3987 return -EFAULT;
3988 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
3989 if (p.resv[i])
3990 return -EINVAL;
3991 }
3992
3993 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
3994 IORING_SETUP_SQ_AFF))
3995 return -EINVAL;
3996
3997 ret = io_uring_create(entries, &p);
3998 if (ret < 0)
3999 return ret;
4000
4001 if (copy_to_user(params, &p, sizeof(p)))
4002 return -EFAULT;
4003
4004 return ret;
4005 }
4006
4007 SYSCALL_DEFINE2(io_uring_setup, u32, entries,
4008 struct io_uring_params __user *, params)
4009 {
4010 return io_uring_setup(entries, params);
4011 }
4012
4013 static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
4014 void __user *arg, unsigned nr_args)
4015 __releases(ctx->uring_lock)
4016 __acquires(ctx->uring_lock)
4017 {
4018 int ret;
4019
4020
4021
4022
4023
4024
4025 if (percpu_ref_is_dying(&ctx->refs))
4026 return -ENXIO;
4027
4028 percpu_ref_kill(&ctx->refs);
4029
4030
4031
4032
4033
4034
4035
4036
4037 mutex_unlock(&ctx->uring_lock);
4038 wait_for_completion(&ctx->ctx_done);
4039 mutex_lock(&ctx->uring_lock);
4040
4041 switch (opcode) {
4042 case IORING_REGISTER_BUFFERS:
4043 ret = io_sqe_buffer_register(ctx, arg, nr_args);
4044 break;
4045 case IORING_UNREGISTER_BUFFERS:
4046 ret = -EINVAL;
4047 if (arg || nr_args)
4048 break;
4049 ret = io_sqe_buffer_unregister(ctx);
4050 break;
4051 case IORING_REGISTER_FILES:
4052 ret = io_sqe_files_register(ctx, arg, nr_args);
4053 break;
4054 case IORING_UNREGISTER_FILES:
4055 ret = -EINVAL;
4056 if (arg || nr_args)
4057 break;
4058 ret = io_sqe_files_unregister(ctx);
4059 break;
4060 case IORING_REGISTER_EVENTFD:
4061 ret = -EINVAL;
4062 if (nr_args != 1)
4063 break;
4064 ret = io_eventfd_register(ctx, arg);
4065 break;
4066 case IORING_UNREGISTER_EVENTFD:
4067 ret = -EINVAL;
4068 if (arg || nr_args)
4069 break;
4070 ret = io_eventfd_unregister(ctx);
4071 break;
4072 default:
4073 ret = -EINVAL;
4074 break;
4075 }
4076
4077
4078 reinit_completion(&ctx->ctx_done);
4079 percpu_ref_reinit(&ctx->refs);
4080 return ret;
4081 }
4082
4083 SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
4084 void __user *, arg, unsigned int, nr_args)
4085 {
4086 struct io_ring_ctx *ctx;
4087 long ret = -EBADF;
4088 struct fd f;
4089
4090 f = fdget(fd);
4091 if (!f.file)
4092 return -EBADF;
4093
4094 ret = -EOPNOTSUPP;
4095 if (f.file->f_op != &io_uring_fops)
4096 goto out_fput;
4097
4098 ctx = f.file->private_data;
4099
4100 mutex_lock(&ctx->uring_lock);
4101 ret = __io_uring_register(ctx, opcode, arg, nr_args);
4102 mutex_unlock(&ctx->uring_lock);
4103 out_fput:
4104 fdput(f);
4105 return ret;
4106 }
4107
4108 static int __init io_uring_init(void)
4109 {
4110 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
4111 return 0;
4112 };
4113 __initcall(io_uring_init);