405be10da73d4bb37b2fea9a3ca9f9247e01a5b4
[linux-2.6-block.git] / fs / io_uring.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Shared application/kernel submission and completion ring pairs, for
4  * supporting fast/efficient IO.
5  *
6  * A note on the read/write ordering memory barriers that are matched between
7  * the application and kernel side.
8  *
9  * After the application reads the CQ ring tail, it must use an
10  * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses
11  * before writing the tail (using smp_load_acquire to read the tail will
12  * do). It also needs a smp_mb() before updating CQ head (ordering the
13  * entry load(s) with the head store), pairing with an implicit barrier
14  * through a control-dependency in io_get_cqring (smp_store_release to
15  * store head will do). Failure to do so could lead to reading invalid
16  * CQ entries.
17  *
18  * Likewise, the application must use an appropriate smp_wmb() before
19  * writing the SQ tail (ordering SQ entry stores with the tail store),
20  * which pairs with smp_load_acquire in io_get_sqring (smp_store_release
21  * to store the tail will do). And it needs a barrier ordering the SQ
22  * head load before writing new SQ entries (smp_load_acquire to read
23  * head will do).
24  *
25  * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application
26  * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after*
27  * updating the SQ tail; a full memory barrier smp_mb() is needed
28  * between.
29  *
30  * Also see the examples in the liburing library:
31  *
32  *      git://git.kernel.dk/liburing
33  *
34  * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens
35  * from data shared between the kernel and application. This is done both
36  * for ordering purposes, but also to ensure that once a value is loaded from
37  * data that the application could potentially modify, it remains stable.
38  *
39  * Copyright (C) 2018-2019 Jens Axboe
40  * Copyright (c) 2018-2019 Christoph Hellwig
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/kthread.h>
60 #include <linux/blkdev.h>
61 #include <linux/bvec.h>
62 #include <linux/net.h>
63 #include <net/sock.h>
64 #include <net/af_unix.h>
65 #include <net/scm.h>
66 #include <linux/anon_inodes.h>
67 #include <linux/sched/mm.h>
68 #include <linux/uaccess.h>
69 #include <linux/nospec.h>
70 #include <linux/sizes.h>
71 #include <linux/hugetlb.h>
72 #include <linux/highmem.h>
73
74 #define CREATE_TRACE_POINTS
75 #include <trace/events/io_uring.h>
76
77 #include <uapi/linux/io_uring.h>
78
79 #include "internal.h"
80 #include "io-wq.h"
81
82 #define IORING_MAX_ENTRIES      32768
83 #define IORING_MAX_CQ_ENTRIES   (2 * IORING_MAX_ENTRIES)
84
85 /*
86  * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
87  */
88 #define IORING_FILE_TABLE_SHIFT 9
89 #define IORING_MAX_FILES_TABLE  (1U << IORING_FILE_TABLE_SHIFT)
90 #define IORING_FILE_TABLE_MASK  (IORING_MAX_FILES_TABLE - 1)
91 #define IORING_MAX_FIXED_FILES  (64 * IORING_MAX_FILES_TABLE)
92
93 struct io_uring {
94         u32 head ____cacheline_aligned_in_smp;
95         u32 tail ____cacheline_aligned_in_smp;
96 };
97
98 /*
99  * This data is shared with the application through the mmap at offsets
100  * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
101  *
102  * The offsets to the member fields are published through struct
103  * io_sqring_offsets when calling io_uring_setup.
104  */
105 struct io_rings {
106         /*
107          * Head and tail offsets into the ring; the offsets need to be
108          * masked to get valid indices.
109          *
110          * The kernel controls head of the sq ring and the tail of the cq ring,
111          * and the application controls tail of the sq ring and the head of the
112          * cq ring.
113          */
114         struct io_uring         sq, cq;
115         /*
116          * Bitmasks to apply to head and tail offsets (constant, equals
117          * ring_entries - 1)
118          */
119         u32                     sq_ring_mask, cq_ring_mask;
120         /* Ring sizes (constant, power of 2) */
121         u32                     sq_ring_entries, cq_ring_entries;
122         /*
123          * Number of invalid entries dropped by the kernel due to
124          * invalid index stored in array
125          *
126          * Written by the kernel, shouldn't be modified by the
127          * application (i.e. get number of "new events" by comparing to
128          * cached value).
129          *
130          * After a new SQ head value was read by the application this
131          * counter includes all submissions that were dropped reaching
132          * the new SQ head (and possibly more).
133          */
134         u32                     sq_dropped;
135         /*
136          * Runtime flags
137          *
138          * Written by the kernel, shouldn't be modified by the
139          * application.
140          *
141          * The application needs a full memory barrier before checking
142          * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
143          */
144         u32                     sq_flags;
145         /*
146          * Number of completion events lost because the queue was full;
147          * this should be avoided by the application by making sure
148          * there are not more requests pending than there is space in
149          * the completion queue.
150          *
151          * Written by the kernel, shouldn't be modified by the
152          * application (i.e. get number of "new events" by comparing to
153          * cached value).
154          *
155          * As completion events come in out of order this counter is not
156          * ordered with any other data.
157          */
158         u32                     cq_overflow;
159         /*
160          * Ring buffer of completion events.
161          *
162          * The kernel writes completion events fresh every time they are
163          * produced, so the application is allowed to modify pending
164          * entries.
165          */
166         struct io_uring_cqe     cqes[] ____cacheline_aligned_in_smp;
167 };
168
169 struct io_mapped_ubuf {
170         u64             ubuf;
171         size_t          len;
172         struct          bio_vec *bvec;
173         unsigned int    nr_bvecs;
174 };
175
176 struct fixed_file_table {
177         struct file             **files;
178 };
179
180 struct io_ring_ctx {
181         struct {
182                 struct percpu_ref       refs;
183         } ____cacheline_aligned_in_smp;
184
185         struct {
186                 unsigned int            flags;
187                 bool                    compat;
188                 bool                    account_mem;
189                 bool                    cq_overflow_flushed;
190                 bool                    drain_next;
191
192                 /*
193                  * Ring buffer of indices into array of io_uring_sqe, which is
194                  * mmapped by the application using the IORING_OFF_SQES offset.
195                  *
196                  * This indirection could e.g. be used to assign fixed
197                  * io_uring_sqe entries to operations and only submit them to
198                  * the queue when needed.
199                  *
200                  * The kernel modifies neither the indices array nor the entries
201                  * array.
202                  */
203                 u32                     *sq_array;
204                 unsigned                cached_sq_head;
205                 unsigned                sq_entries;
206                 unsigned                sq_mask;
207                 unsigned                sq_thread_idle;
208                 unsigned                cached_sq_dropped;
209                 atomic_t                cached_cq_overflow;
210                 struct io_uring_sqe     *sq_sqes;
211
212                 struct list_head        defer_list;
213                 struct list_head        timeout_list;
214                 struct list_head        cq_overflow_list;
215
216                 wait_queue_head_t       inflight_wait;
217         } ____cacheline_aligned_in_smp;
218
219         struct io_rings *rings;
220
221         /* IO offload */
222         struct io_wq            *io_wq;
223         struct task_struct      *sqo_thread;    /* if using sq thread polling */
224         struct mm_struct        *sqo_mm;
225         wait_queue_head_t       sqo_wait;
226
227         /*
228          * If used, fixed file set. Writers must ensure that ->refs is dead,
229          * readers must ensure that ->refs is alive as long as the file* is
230          * used. Only updated through io_uring_register(2).
231          */
232         struct fixed_file_table *file_table;
233         unsigned                nr_user_files;
234
235         /* if used, fixed mapped user buffers */
236         unsigned                nr_user_bufs;
237         struct io_mapped_ubuf   *user_bufs;
238
239         struct user_struct      *user;
240
241         const struct cred       *creds;
242
243         /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */
244         struct completion       *completions;
245
246         /* if all else fails... */
247         struct io_kiocb         *fallback_req;
248
249 #if defined(CONFIG_UNIX)
250         struct socket           *ring_sock;
251 #endif
252
253         struct {
254                 unsigned                cached_cq_tail;
255                 unsigned                cq_entries;
256                 unsigned                cq_mask;
257                 atomic_t                cq_timeouts;
258                 struct wait_queue_head  cq_wait;
259                 struct fasync_struct    *cq_fasync;
260                 struct eventfd_ctx      *cq_ev_fd;
261         } ____cacheline_aligned_in_smp;
262
263         struct {
264                 struct mutex            uring_lock;
265                 wait_queue_head_t       wait;
266         } ____cacheline_aligned_in_smp;
267
268         struct {
269                 spinlock_t              completion_lock;
270                 bool                    poll_multi_file;
271                 /*
272                  * ->poll_list is protected by the ctx->uring_lock for
273                  * io_uring instances that don't use IORING_SETUP_SQPOLL.
274                  * For SQPOLL, only the single threaded io_sq_thread() will
275                  * manipulate the list, hence no extra locking is needed there.
276                  */
277                 struct list_head        poll_list;
278                 struct hlist_head       *cancel_hash;
279                 unsigned                cancel_hash_bits;
280
281                 spinlock_t              inflight_lock;
282                 struct list_head        inflight_list;
283         } ____cacheline_aligned_in_smp;
284 };
285
286 /*
287  * First field must be the file pointer in all the
288  * iocb unions! See also 'struct kiocb' in <linux/fs.h>
289  */
290 struct io_poll_iocb {
291         struct file                     *file;
292         struct wait_queue_head          *head;
293         __poll_t                        events;
294         bool                            done;
295         bool                            canceled;
296         struct wait_queue_entry         *wait;
297 };
298
299 struct io_timeout_data {
300         struct io_kiocb                 *req;
301         struct hrtimer                  timer;
302         struct timespec64               ts;
303         enum hrtimer_mode               mode;
304         u32                             seq_offset;
305 };
306
307 struct io_async_connect {
308         struct sockaddr_storage         address;
309 };
310
311 struct io_async_msghdr {
312         struct iovec                    fast_iov[UIO_FASTIOV];
313         struct iovec                    *iov;
314         struct sockaddr __user          *uaddr;
315         struct msghdr                   msg;
316 };
317
318 struct io_async_rw {
319         struct iovec                    fast_iov[UIO_FASTIOV];
320         struct iovec                    *iov;
321         ssize_t                         nr_segs;
322         ssize_t                         size;
323 };
324
325 struct io_async_ctx {
326         struct io_uring_sqe             sqe;
327         union {
328                 struct io_async_rw      rw;
329                 struct io_async_msghdr  msg;
330                 struct io_async_connect connect;
331                 struct io_timeout_data  timeout;
332         };
333 };
334
335 /*
336  * NOTE! Each of the iocb union members has the file pointer
337  * as the first entry in their struct definition. So you can
338  * access the file pointer through any of the sub-structs,
339  * or directly as just 'ki_filp' in this struct.
340  */
341 struct io_kiocb {
342         union {
343                 struct file             *file;
344                 struct kiocb            rw;
345                 struct io_poll_iocb     poll;
346         };
347
348         const struct io_uring_sqe       *sqe;
349         struct io_async_ctx             *io;
350         struct file                     *ring_file;
351         int                             ring_fd;
352         bool                            has_user;
353         bool                            in_async;
354         bool                            needs_fixed_file;
355
356         struct io_ring_ctx      *ctx;
357         union {
358                 struct list_head        list;
359                 struct hlist_node       hash_node;
360         };
361         struct list_head        link_list;
362         unsigned int            flags;
363         refcount_t              refs;
364 #define REQ_F_NOWAIT            1       /* must not punt to workers */
365 #define REQ_F_IOPOLL_COMPLETED  2       /* polled IO has completed */
366 #define REQ_F_FIXED_FILE        4       /* ctx owns file */
367 #define REQ_F_LINK_NEXT         8       /* already grabbed next link */
368 #define REQ_F_IO_DRAIN          16      /* drain existing IO first */
369 #define REQ_F_IO_DRAINED        32      /* drain done */
370 #define REQ_F_LINK              64      /* linked sqes */
371 #define REQ_F_LINK_TIMEOUT      128     /* has linked timeout */
372 #define REQ_F_FAIL_LINK         256     /* fail rest of links */
373 #define REQ_F_DRAIN_LINK        512     /* link should be fully drained */
374 #define REQ_F_TIMEOUT           1024    /* timeout request */
375 #define REQ_F_ISREG             2048    /* regular file */
376 #define REQ_F_MUST_PUNT         4096    /* must be punted even for NONBLOCK */
377 #define REQ_F_TIMEOUT_NOSEQ     8192    /* no timeout sequence */
378 #define REQ_F_INFLIGHT          16384   /* on inflight list */
379 #define REQ_F_COMP_LOCKED       32768   /* completion under lock */
380         u64                     user_data;
381         u32                     result;
382         u32                     sequence;
383
384         struct list_head        inflight_entry;
385
386         struct io_wq_work       work;
387 };
388
389 #define IO_PLUG_THRESHOLD               2
390 #define IO_IOPOLL_BATCH                 8
391
392 struct io_submit_state {
393         struct blk_plug         plug;
394
395         /*
396          * io_kiocb alloc cache
397          */
398         void                    *reqs[IO_IOPOLL_BATCH];
399         unsigned                int free_reqs;
400         unsigned                int cur_req;
401
402         /*
403          * File reference cache
404          */
405         struct file             *file;
406         unsigned int            fd;
407         unsigned int            has_refs;
408         unsigned int            used_refs;
409         unsigned int            ios_left;
410 };
411
412 static void io_wq_submit_work(struct io_wq_work **workptr);
413 static void io_cqring_fill_event(struct io_kiocb *req, long res);
414 static void __io_free_req(struct io_kiocb *req);
415 static void io_put_req(struct io_kiocb *req);
416 static void io_double_put_req(struct io_kiocb *req);
417 static void __io_double_put_req(struct io_kiocb *req);
418 static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
419 static void io_queue_linked_timeout(struct io_kiocb *req);
420
421 static struct kmem_cache *req_cachep;
422
423 static const struct file_operations io_uring_fops;
424
425 struct sock *io_uring_get_socket(struct file *file)
426 {
427 #if defined(CONFIG_UNIX)
428         if (file->f_op == &io_uring_fops) {
429                 struct io_ring_ctx *ctx = file->private_data;
430
431                 return ctx->ring_sock->sk;
432         }
433 #endif
434         return NULL;
435 }
436 EXPORT_SYMBOL(io_uring_get_socket);
437
438 static void io_ring_ctx_ref_free(struct percpu_ref *ref)
439 {
440         struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
441
442         complete(&ctx->completions[0]);
443 }
444
445 static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
446 {
447         struct io_ring_ctx *ctx;
448         int hash_bits;
449
450         ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
451         if (!ctx)
452                 return NULL;
453
454         ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
455         if (!ctx->fallback_req)
456                 goto err;
457
458         ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
459         if (!ctx->completions)
460                 goto err;
461
462         /*
463          * Use 5 bits less than the max cq entries, that should give us around
464          * 32 entries per hash list if totally full and uniformly spread.
465          */
466         hash_bits = ilog2(p->cq_entries);
467         hash_bits -= 5;
468         if (hash_bits <= 0)
469                 hash_bits = 1;
470         ctx->cancel_hash_bits = hash_bits;
471         ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
472                                         GFP_KERNEL);
473         if (!ctx->cancel_hash)
474                 goto err;
475         __hash_init(ctx->cancel_hash, 1U << hash_bits);
476
477         if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
478                             PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
479                 goto err;
480
481         ctx->flags = p->flags;
482         init_waitqueue_head(&ctx->cq_wait);
483         INIT_LIST_HEAD(&ctx->cq_overflow_list);
484         init_completion(&ctx->completions[0]);
485         init_completion(&ctx->completions[1]);
486         mutex_init(&ctx->uring_lock);
487         init_waitqueue_head(&ctx->wait);
488         spin_lock_init(&ctx->completion_lock);
489         INIT_LIST_HEAD(&ctx->poll_list);
490         INIT_LIST_HEAD(&ctx->defer_list);
491         INIT_LIST_HEAD(&ctx->timeout_list);
492         init_waitqueue_head(&ctx->inflight_wait);
493         spin_lock_init(&ctx->inflight_lock);
494         INIT_LIST_HEAD(&ctx->inflight_list);
495         return ctx;
496 err:
497         if (ctx->fallback_req)
498                 kmem_cache_free(req_cachep, ctx->fallback_req);
499         kfree(ctx->completions);
500         kfree(ctx->cancel_hash);
501         kfree(ctx);
502         return NULL;
503 }
504
505 static inline bool __req_need_defer(struct io_kiocb *req)
506 {
507         struct io_ring_ctx *ctx = req->ctx;
508
509         return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
510                                         + atomic_read(&ctx->cached_cq_overflow);
511 }
512
513 static inline bool req_need_defer(struct io_kiocb *req)
514 {
515         if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) == REQ_F_IO_DRAIN)
516                 return __req_need_defer(req);
517
518         return false;
519 }
520
521 static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
522 {
523         struct io_kiocb *req;
524
525         req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
526         if (req && !req_need_defer(req)) {
527                 list_del_init(&req->list);
528                 return req;
529         }
530
531         return NULL;
532 }
533
534 static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
535 {
536         struct io_kiocb *req;
537
538         req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
539         if (req) {
540                 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
541                         return NULL;
542                 if (!__req_need_defer(req)) {
543                         list_del_init(&req->list);
544                         return req;
545                 }
546         }
547
548         return NULL;
549 }
550
551 static void __io_commit_cqring(struct io_ring_ctx *ctx)
552 {
553         struct io_rings *rings = ctx->rings;
554
555         if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
556                 /* order cqe stores with ring update */
557                 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
558
559                 if (wq_has_sleeper(&ctx->cq_wait)) {
560                         wake_up_interruptible(&ctx->cq_wait);
561                         kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
562                 }
563         }
564 }
565
566 static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
567 {
568         u8 opcode = READ_ONCE(sqe->opcode);
569
570         return !(opcode == IORING_OP_READ_FIXED ||
571                  opcode == IORING_OP_WRITE_FIXED);
572 }
573
574 static inline bool io_prep_async_work(struct io_kiocb *req,
575                                       struct io_kiocb **link)
576 {
577         bool do_hashed = false;
578
579         if (req->sqe) {
580                 switch (req->sqe->opcode) {
581                 case IORING_OP_WRITEV:
582                 case IORING_OP_WRITE_FIXED:
583                         do_hashed = true;
584                         /* fall-through */
585                 case IORING_OP_READV:
586                 case IORING_OP_READ_FIXED:
587                 case IORING_OP_SENDMSG:
588                 case IORING_OP_RECVMSG:
589                 case IORING_OP_ACCEPT:
590                 case IORING_OP_POLL_ADD:
591                 case IORING_OP_CONNECT:
592                         /*
593                          * We know REQ_F_ISREG is not set on some of these
594                          * opcodes, but this enables us to keep the check in
595                          * just one place.
596                          */
597                         if (!(req->flags & REQ_F_ISREG))
598                                 req->work.flags |= IO_WQ_WORK_UNBOUND;
599                         break;
600                 }
601                 if (io_sqe_needs_user(req->sqe))
602                         req->work.flags |= IO_WQ_WORK_NEEDS_USER;
603         }
604
605         *link = io_prep_linked_timeout(req);
606         return do_hashed;
607 }
608
609 static inline void io_queue_async_work(struct io_kiocb *req)
610 {
611         struct io_ring_ctx *ctx = req->ctx;
612         struct io_kiocb *link;
613         bool do_hashed;
614
615         do_hashed = io_prep_async_work(req, &link);
616
617         trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
618                                         req->flags);
619         if (!do_hashed) {
620                 io_wq_enqueue(ctx->io_wq, &req->work);
621         } else {
622                 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
623                                         file_inode(req->file));
624         }
625
626         if (link)
627                 io_queue_linked_timeout(link);
628 }
629
630 static void io_kill_timeout(struct io_kiocb *req)
631 {
632         int ret;
633
634         ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
635         if (ret != -1) {
636                 atomic_inc(&req->ctx->cq_timeouts);
637                 list_del_init(&req->list);
638                 io_cqring_fill_event(req, 0);
639                 io_put_req(req);
640         }
641 }
642
643 static void io_kill_timeouts(struct io_ring_ctx *ctx)
644 {
645         struct io_kiocb *req, *tmp;
646
647         spin_lock_irq(&ctx->completion_lock);
648         list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
649                 io_kill_timeout(req);
650         spin_unlock_irq(&ctx->completion_lock);
651 }
652
653 static void io_commit_cqring(struct io_ring_ctx *ctx)
654 {
655         struct io_kiocb *req;
656
657         while ((req = io_get_timeout_req(ctx)) != NULL)
658                 io_kill_timeout(req);
659
660         __io_commit_cqring(ctx);
661
662         while ((req = io_get_deferred_req(ctx)) != NULL) {
663                 req->flags |= REQ_F_IO_DRAINED;
664                 io_queue_async_work(req);
665         }
666 }
667
668 static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
669 {
670         struct io_rings *rings = ctx->rings;
671         unsigned tail;
672
673         tail = ctx->cached_cq_tail;
674         /*
675          * writes to the cq entry need to come after reading head; the
676          * control dependency is enough as we're using WRITE_ONCE to
677          * fill the cq entry
678          */
679         if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
680                 return NULL;
681
682         ctx->cached_cq_tail++;
683         return &rings->cqes[tail & ctx->cq_mask];
684 }
685
686 static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
687 {
688         if (waitqueue_active(&ctx->wait))
689                 wake_up(&ctx->wait);
690         if (waitqueue_active(&ctx->sqo_wait))
691                 wake_up(&ctx->sqo_wait);
692         if (ctx->cq_ev_fd)
693                 eventfd_signal(ctx->cq_ev_fd, 1);
694 }
695
696 /* Returns true if there are no backlogged entries after the flush */
697 static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
698 {
699         struct io_rings *rings = ctx->rings;
700         struct io_uring_cqe *cqe;
701         struct io_kiocb *req;
702         unsigned long flags;
703         LIST_HEAD(list);
704
705         if (!force) {
706                 if (list_empty_careful(&ctx->cq_overflow_list))
707                         return true;
708                 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
709                     rings->cq_ring_entries))
710                         return false;
711         }
712
713         spin_lock_irqsave(&ctx->completion_lock, flags);
714
715         /* if force is set, the ring is going away. always drop after that */
716         if (force)
717                 ctx->cq_overflow_flushed = true;
718
719         cqe = NULL;
720         while (!list_empty(&ctx->cq_overflow_list)) {
721                 cqe = io_get_cqring(ctx);
722                 if (!cqe && !force)
723                         break;
724
725                 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
726                                                 list);
727                 list_move(&req->list, &list);
728                 if (cqe) {
729                         WRITE_ONCE(cqe->user_data, req->user_data);
730                         WRITE_ONCE(cqe->res, req->result);
731                         WRITE_ONCE(cqe->flags, 0);
732                 } else {
733                         WRITE_ONCE(ctx->rings->cq_overflow,
734                                 atomic_inc_return(&ctx->cached_cq_overflow));
735                 }
736         }
737
738         io_commit_cqring(ctx);
739         spin_unlock_irqrestore(&ctx->completion_lock, flags);
740         io_cqring_ev_posted(ctx);
741
742         while (!list_empty(&list)) {
743                 req = list_first_entry(&list, struct io_kiocb, list);
744                 list_del(&req->list);
745                 io_put_req(req);
746         }
747
748         return cqe != NULL;
749 }
750
751 static void io_cqring_fill_event(struct io_kiocb *req, long res)
752 {
753         struct io_ring_ctx *ctx = req->ctx;
754         struct io_uring_cqe *cqe;
755
756         trace_io_uring_complete(ctx, req->user_data, res);
757
758         /*
759          * If we can't get a cq entry, userspace overflowed the
760          * submission (by quite a lot). Increment the overflow count in
761          * the ring.
762          */
763         cqe = io_get_cqring(ctx);
764         if (likely(cqe)) {
765                 WRITE_ONCE(cqe->user_data, req->user_data);
766                 WRITE_ONCE(cqe->res, res);
767                 WRITE_ONCE(cqe->flags, 0);
768         } else if (ctx->cq_overflow_flushed) {
769                 WRITE_ONCE(ctx->rings->cq_overflow,
770                                 atomic_inc_return(&ctx->cached_cq_overflow));
771         } else {
772                 refcount_inc(&req->refs);
773                 req->result = res;
774                 list_add_tail(&req->list, &ctx->cq_overflow_list);
775         }
776 }
777
778 static void io_cqring_add_event(struct io_kiocb *req, long res)
779 {
780         struct io_ring_ctx *ctx = req->ctx;
781         unsigned long flags;
782
783         spin_lock_irqsave(&ctx->completion_lock, flags);
784         io_cqring_fill_event(req, res);
785         io_commit_cqring(ctx);
786         spin_unlock_irqrestore(&ctx->completion_lock, flags);
787
788         io_cqring_ev_posted(ctx);
789 }
790
791 static inline bool io_is_fallback_req(struct io_kiocb *req)
792 {
793         return req == (struct io_kiocb *)
794                         ((unsigned long) req->ctx->fallback_req & ~1UL);
795 }
796
797 static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
798 {
799         struct io_kiocb *req;
800
801         req = ctx->fallback_req;
802         if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
803                 return req;
804
805         return NULL;
806 }
807
808 static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
809                                    struct io_submit_state *state)
810 {
811         gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
812         struct io_kiocb *req;
813
814         if (!percpu_ref_tryget(&ctx->refs))
815                 return NULL;
816
817         if (!state) {
818                 req = kmem_cache_alloc(req_cachep, gfp);
819                 if (unlikely(!req))
820                         goto fallback;
821         } else if (!state->free_reqs) {
822                 size_t sz;
823                 int ret;
824
825                 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
826                 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
827
828                 /*
829                  * Bulk alloc is all-or-nothing. If we fail to get a batch,
830                  * retry single alloc to be on the safe side.
831                  */
832                 if (unlikely(ret <= 0)) {
833                         state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
834                         if (!state->reqs[0])
835                                 goto fallback;
836                         ret = 1;
837                 }
838                 state->free_reqs = ret - 1;
839                 state->cur_req = 1;
840                 req = state->reqs[0];
841         } else {
842                 req = state->reqs[state->cur_req];
843                 state->free_reqs--;
844                 state->cur_req++;
845         }
846
847 got_it:
848         req->io = NULL;
849         req->ring_file = NULL;
850         req->file = NULL;
851         req->ctx = ctx;
852         req->flags = 0;
853         /* one is dropped after submission, the other at completion */
854         refcount_set(&req->refs, 2);
855         req->result = 0;
856         INIT_IO_WORK(&req->work, io_wq_submit_work);
857         return req;
858 fallback:
859         req = io_get_fallback_req(ctx);
860         if (req)
861                 goto got_it;
862         percpu_ref_put(&ctx->refs);
863         return NULL;
864 }
865
866 static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
867 {
868         if (*nr) {
869                 kmem_cache_free_bulk(req_cachep, *nr, reqs);
870                 percpu_ref_put_many(&ctx->refs, *nr);
871                 *nr = 0;
872         }
873 }
874
875 static void __io_free_req(struct io_kiocb *req)
876 {
877         struct io_ring_ctx *ctx = req->ctx;
878
879         if (req->io)
880                 kfree(req->io);
881         if (req->file && !(req->flags & REQ_F_FIXED_FILE))
882                 fput(req->file);
883         if (req->flags & REQ_F_INFLIGHT) {
884                 unsigned long flags;
885
886                 spin_lock_irqsave(&ctx->inflight_lock, flags);
887                 list_del(&req->inflight_entry);
888                 if (waitqueue_active(&ctx->inflight_wait))
889                         wake_up(&ctx->inflight_wait);
890                 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
891         }
892         percpu_ref_put(&ctx->refs);
893         if (likely(!io_is_fallback_req(req)))
894                 kmem_cache_free(req_cachep, req);
895         else
896                 clear_bit_unlock(0, (unsigned long *) ctx->fallback_req);
897 }
898
899 static bool io_link_cancel_timeout(struct io_kiocb *req)
900 {
901         struct io_ring_ctx *ctx = req->ctx;
902         int ret;
903
904         ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
905         if (ret != -1) {
906                 io_cqring_fill_event(req, -ECANCELED);
907                 io_commit_cqring(ctx);
908                 req->flags &= ~REQ_F_LINK;
909                 io_put_req(req);
910                 return true;
911         }
912
913         return false;
914 }
915
916 static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
917 {
918         struct io_ring_ctx *ctx = req->ctx;
919         bool wake_ev = false;
920
921         /* Already got next link */
922         if (req->flags & REQ_F_LINK_NEXT)
923                 return;
924
925         /*
926          * The list should never be empty when we are called here. But could
927          * potentially happen if the chain is messed up, check to be on the
928          * safe side.
929          */
930         while (!list_empty(&req->link_list)) {
931                 struct io_kiocb *nxt = list_first_entry(&req->link_list,
932                                                 struct io_kiocb, link_list);
933
934                 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
935                              (nxt->flags & REQ_F_TIMEOUT))) {
936                         list_del_init(&nxt->link_list);
937                         wake_ev |= io_link_cancel_timeout(nxt);
938                         req->flags &= ~REQ_F_LINK_TIMEOUT;
939                         continue;
940                 }
941
942                 list_del_init(&req->link_list);
943                 if (!list_empty(&nxt->link_list))
944                         nxt->flags |= REQ_F_LINK;
945                 *nxtptr = nxt;
946                 break;
947         }
948
949         req->flags |= REQ_F_LINK_NEXT;
950         if (wake_ev)
951                 io_cqring_ev_posted(ctx);
952 }
953
954 /*
955  * Called if REQ_F_LINK is set, and we fail the head request
956  */
957 static void io_fail_links(struct io_kiocb *req)
958 {
959         struct io_ring_ctx *ctx = req->ctx;
960         unsigned long flags;
961
962         spin_lock_irqsave(&ctx->completion_lock, flags);
963
964         while (!list_empty(&req->link_list)) {
965                 struct io_kiocb *link = list_first_entry(&req->link_list,
966                                                 struct io_kiocb, link_list);
967
968                 list_del_init(&link->link_list);
969                 trace_io_uring_fail_link(req, link);
970
971                 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
972                     link->sqe->opcode == IORING_OP_LINK_TIMEOUT) {
973                         io_link_cancel_timeout(link);
974                 } else {
975                         io_cqring_fill_event(link, -ECANCELED);
976                         __io_double_put_req(link);
977                 }
978                 req->flags &= ~REQ_F_LINK_TIMEOUT;
979         }
980
981         io_commit_cqring(ctx);
982         spin_unlock_irqrestore(&ctx->completion_lock, flags);
983         io_cqring_ev_posted(ctx);
984 }
985
986 static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
987 {
988         if (likely(!(req->flags & REQ_F_LINK)))
989                 return;
990
991         /*
992          * If LINK is set, we have dependent requests in this chain. If we
993          * didn't fail this request, queue the first one up, moving any other
994          * dependencies to the next request. In case of failure, fail the rest
995          * of the chain.
996          */
997         if (req->flags & REQ_F_FAIL_LINK) {
998                 io_fail_links(req);
999         } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1000                         REQ_F_LINK_TIMEOUT) {
1001                 struct io_ring_ctx *ctx = req->ctx;
1002                 unsigned long flags;
1003
1004                 /*
1005                  * If this is a timeout link, we could be racing with the
1006                  * timeout timer. Grab the completion lock for this case to
1007                  * protect against that.
1008                  */
1009                 spin_lock_irqsave(&ctx->completion_lock, flags);
1010                 io_req_link_next(req, nxt);
1011                 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1012         } else {
1013                 io_req_link_next(req, nxt);
1014         }
1015 }
1016
1017 static void io_free_req(struct io_kiocb *req)
1018 {
1019         struct io_kiocb *nxt = NULL;
1020
1021         io_req_find_next(req, &nxt);
1022         __io_free_req(req);
1023
1024         if (nxt)
1025                 io_queue_async_work(nxt);
1026 }
1027
1028 /*
1029  * Drop reference to request, return next in chain (if there is one) if this
1030  * was the last reference to this request.
1031  */
1032 __attribute__((nonnull))
1033 static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
1034 {
1035         io_req_find_next(req, nxtptr);
1036
1037         if (refcount_dec_and_test(&req->refs))
1038                 __io_free_req(req);
1039 }
1040
1041 static void io_put_req(struct io_kiocb *req)
1042 {
1043         if (refcount_dec_and_test(&req->refs))
1044                 io_free_req(req);
1045 }
1046
1047 /*
1048  * Must only be used if we don't need to care about links, usually from
1049  * within the completion handling itself.
1050  */
1051 static void __io_double_put_req(struct io_kiocb *req)
1052 {
1053         /* drop both submit and complete references */
1054         if (refcount_sub_and_test(2, &req->refs))
1055                 __io_free_req(req);
1056 }
1057
1058 static void io_double_put_req(struct io_kiocb *req)
1059 {
1060         /* drop both submit and complete references */
1061         if (refcount_sub_and_test(2, &req->refs))
1062                 io_free_req(req);
1063 }
1064
1065 static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
1066 {
1067         struct io_rings *rings = ctx->rings;
1068
1069         /*
1070          * noflush == true is from the waitqueue handler, just ensure we wake
1071          * up the task, and the next invocation will flush the entries. We
1072          * cannot safely to it from here.
1073          */
1074         if (noflush && !list_empty(&ctx->cq_overflow_list))
1075                 return -1U;
1076
1077         io_cqring_overflow_flush(ctx, false);
1078
1079         /* See comment at the top of this file */
1080         smp_rmb();
1081         return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
1082 }
1083
1084 static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1085 {
1086         struct io_rings *rings = ctx->rings;
1087
1088         /* make sure SQ entry isn't read before tail */
1089         return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1090 }
1091
1092 /*
1093  * Find and free completed poll iocbs
1094  */
1095 static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1096                                struct list_head *done)
1097 {
1098         void *reqs[IO_IOPOLL_BATCH];
1099         struct io_kiocb *req;
1100         int to_free;
1101
1102         to_free = 0;
1103         while (!list_empty(done)) {
1104                 req = list_first_entry(done, struct io_kiocb, list);
1105                 list_del(&req->list);
1106
1107                 io_cqring_fill_event(req, req->result);
1108                 (*nr_events)++;
1109
1110                 if (refcount_dec_and_test(&req->refs)) {
1111                         /* If we're not using fixed files, we have to pair the
1112                          * completion part with the file put. Use regular
1113                          * completions for those, only batch free for fixed
1114                          * file and non-linked commands.
1115                          */
1116                         if (((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) ==
1117                             REQ_F_FIXED_FILE) && !io_is_fallback_req(req) &&
1118                             !req->io) {
1119                                 reqs[to_free++] = req;
1120                                 if (to_free == ARRAY_SIZE(reqs))
1121                                         io_free_req_many(ctx, reqs, &to_free);
1122                         } else {
1123                                 io_free_req(req);
1124                         }
1125                 }
1126         }
1127
1128         io_commit_cqring(ctx);
1129         io_free_req_many(ctx, reqs, &to_free);
1130 }
1131
1132 static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1133                         long min)
1134 {
1135         struct io_kiocb *req, *tmp;
1136         LIST_HEAD(done);
1137         bool spin;
1138         int ret;
1139
1140         /*
1141          * Only spin for completions if we don't have multiple devices hanging
1142          * off our complete list, and we're under the requested amount.
1143          */
1144         spin = !ctx->poll_multi_file && *nr_events < min;
1145
1146         ret = 0;
1147         list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
1148                 struct kiocb *kiocb = &req->rw;
1149
1150                 /*
1151                  * Move completed entries to our local list. If we find a
1152                  * request that requires polling, break out and complete
1153                  * the done list first, if we have entries there.
1154                  */
1155                 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1156                         list_move_tail(&req->list, &done);
1157                         continue;
1158                 }
1159                 if (!list_empty(&done))
1160                         break;
1161
1162                 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1163                 if (ret < 0)
1164                         break;
1165
1166                 if (ret && spin)
1167                         spin = false;
1168                 ret = 0;
1169         }
1170
1171         if (!list_empty(&done))
1172                 io_iopoll_complete(ctx, nr_events, &done);
1173
1174         return ret;
1175 }
1176
1177 /*
1178  * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a
1179  * non-spinning poll check - we'll still enter the driver poll loop, but only
1180  * as a non-spinning completion check.
1181  */
1182 static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1183                                 long min)
1184 {
1185         while (!list_empty(&ctx->poll_list) && !need_resched()) {
1186                 int ret;
1187
1188                 ret = io_do_iopoll(ctx, nr_events, min);
1189                 if (ret < 0)
1190                         return ret;
1191                 if (!min || *nr_events >= min)
1192                         return 0;
1193         }
1194
1195         return 1;
1196 }
1197
1198 /*
1199  * We can't just wait for polled events to come to us, we have to actively
1200  * find and complete them.
1201  */
1202 static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1203 {
1204         if (!(ctx->flags & IORING_SETUP_IOPOLL))
1205                 return;
1206
1207         mutex_lock(&ctx->uring_lock);
1208         while (!list_empty(&ctx->poll_list)) {
1209                 unsigned int nr_events = 0;
1210
1211                 io_iopoll_getevents(ctx, &nr_events, 1);
1212
1213                 /*
1214                  * Ensure we allow local-to-the-cpu processing to take place,
1215                  * in this case we need to ensure that we reap all events.
1216                  */
1217                 cond_resched();
1218         }
1219         mutex_unlock(&ctx->uring_lock);
1220 }
1221
1222 static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1223                             long min)
1224 {
1225         int iters = 0, ret = 0;
1226
1227         do {
1228                 int tmin = 0;
1229
1230                 /*
1231                  * Don't enter poll loop if we already have events pending.
1232                  * If we do, we can potentially be spinning for commands that
1233                  * already triggered a CQE (eg in error).
1234                  */
1235                 if (io_cqring_events(ctx, false))
1236                         break;
1237
1238                 /*
1239                  * If a submit got punted to a workqueue, we can have the
1240                  * application entering polling for a command before it gets
1241                  * issued. That app will hold the uring_lock for the duration
1242                  * of the poll right here, so we need to take a breather every
1243                  * now and then to ensure that the issue has a chance to add
1244                  * the poll to the issued list. Otherwise we can spin here
1245                  * forever, while the workqueue is stuck trying to acquire the
1246                  * very same mutex.
1247                  */
1248                 if (!(++iters & 7)) {
1249                         mutex_unlock(&ctx->uring_lock);
1250                         mutex_lock(&ctx->uring_lock);
1251                 }
1252
1253                 if (*nr_events < min)
1254                         tmin = min - *nr_events;
1255
1256                 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1257                 if (ret <= 0)
1258                         break;
1259                 ret = 0;
1260         } while (min && !*nr_events && !need_resched());
1261
1262         return ret;
1263 }
1264
1265 static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1266                            long min)
1267 {
1268         int ret;
1269
1270         /*
1271          * We disallow the app entering submit/complete with polling, but we
1272          * still need to lock the ring to prevent racing with polled issue
1273          * that got punted to a workqueue.
1274          */
1275         mutex_lock(&ctx->uring_lock);
1276         ret = __io_iopoll_check(ctx, nr_events, min);
1277         mutex_unlock(&ctx->uring_lock);
1278         return ret;
1279 }
1280
1281 static void kiocb_end_write(struct io_kiocb *req)
1282 {
1283         /*
1284          * Tell lockdep we inherited freeze protection from submission
1285          * thread.
1286          */
1287         if (req->flags & REQ_F_ISREG) {
1288                 struct inode *inode = file_inode(req->file);
1289
1290                 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
1291         }
1292         file_end_write(req->file);
1293 }
1294
1295 static void io_complete_rw_common(struct kiocb *kiocb, long res)
1296 {
1297         struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1298
1299         if (kiocb->ki_flags & IOCB_WRITE)
1300                 kiocb_end_write(req);
1301
1302         if ((req->flags & REQ_F_LINK) && res != req->result)
1303                 req->flags |= REQ_F_FAIL_LINK;
1304         io_cqring_add_event(req, res);
1305 }
1306
1307 static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1308 {
1309         struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1310
1311         io_complete_rw_common(kiocb, res);
1312         io_put_req(req);
1313 }
1314
1315 static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1316 {
1317         struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1318         struct io_kiocb *nxt = NULL;
1319
1320         io_complete_rw_common(kiocb, res);
1321         io_put_req_find_next(req, &nxt);
1322
1323         return nxt;
1324 }
1325
1326 static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1327 {
1328         struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1329
1330         if (kiocb->ki_flags & IOCB_WRITE)
1331                 kiocb_end_write(req);
1332
1333         if ((req->flags & REQ_F_LINK) && res != req->result)
1334                 req->flags |= REQ_F_FAIL_LINK;
1335         req->result = res;
1336         if (res != -EAGAIN)
1337                 req->flags |= REQ_F_IOPOLL_COMPLETED;
1338 }
1339
1340 /*
1341  * After the iocb has been issued, it's safe to be found on the poll list.
1342  * Adding the kiocb to the list AFTER submission ensures that we don't
1343  * find it from a io_iopoll_getevents() thread before the issuer is done
1344  * accessing the kiocb cookie.
1345  */
1346 static void io_iopoll_req_issued(struct io_kiocb *req)
1347 {
1348         struct io_ring_ctx *ctx = req->ctx;
1349
1350         /*
1351          * Track whether we have multiple files in our lists. This will impact
1352          * how we do polling eventually, not spinning if we're on potentially
1353          * different devices.
1354          */
1355         if (list_empty(&ctx->poll_list)) {
1356                 ctx->poll_multi_file = false;
1357         } else if (!ctx->poll_multi_file) {
1358                 struct io_kiocb *list_req;
1359
1360                 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1361                                                 list);
1362                 if (list_req->rw.ki_filp != req->rw.ki_filp)
1363                         ctx->poll_multi_file = true;
1364         }
1365
1366         /*
1367          * For fast devices, IO may have already completed. If it has, add
1368          * it to the front so we find it first.
1369          */
1370         if (req->flags & REQ_F_IOPOLL_COMPLETED)
1371                 list_add(&req->list, &ctx->poll_list);
1372         else
1373                 list_add_tail(&req->list, &ctx->poll_list);
1374 }
1375
1376 static void io_file_put(struct io_submit_state *state)
1377 {
1378         if (state->file) {
1379                 int diff = state->has_refs - state->used_refs;
1380
1381                 if (diff)
1382                         fput_many(state->file, diff);
1383                 state->file = NULL;
1384         }
1385 }
1386
1387 /*
1388  * Get as many references to a file as we have IOs left in this submission,
1389  * assuming most submissions are for one file, or at least that each file
1390  * has more than one submission.
1391  */
1392 static struct file *io_file_get(struct io_submit_state *state, int fd)
1393 {
1394         if (!state)
1395                 return fget(fd);
1396
1397         if (state->file) {
1398                 if (state->fd == fd) {
1399                         state->used_refs++;
1400                         state->ios_left--;
1401                         return state->file;
1402                 }
1403                 io_file_put(state);
1404         }
1405         state->file = fget_many(fd, state->ios_left);
1406         if (!state->file)
1407                 return NULL;
1408
1409         state->fd = fd;
1410         state->has_refs = state->ios_left;
1411         state->used_refs = 1;
1412         state->ios_left--;
1413         return state->file;
1414 }
1415
1416 /*
1417  * If we tracked the file through the SCM inflight mechanism, we could support
1418  * any file. For now, just ensure that anything potentially problematic is done
1419  * inline.
1420  */
1421 static bool io_file_supports_async(struct file *file)
1422 {
1423         umode_t mode = file_inode(file)->i_mode;
1424
1425         if (S_ISBLK(mode) || S_ISCHR(mode))
1426                 return true;
1427         if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1428                 return true;
1429
1430         return false;
1431 }
1432
1433 static int io_prep_rw(struct io_kiocb *req, bool force_nonblock)
1434 {
1435         const struct io_uring_sqe *sqe = req->sqe;
1436         struct io_ring_ctx *ctx = req->ctx;
1437         struct kiocb *kiocb = &req->rw;
1438         unsigned ioprio;
1439         int ret;
1440
1441         if (!req->file)
1442                 return -EBADF;
1443
1444         if (S_ISREG(file_inode(req->file)->i_mode))
1445                 req->flags |= REQ_F_ISREG;
1446
1447         kiocb->ki_pos = READ_ONCE(sqe->off);
1448         kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1449         kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1450
1451         ioprio = READ_ONCE(sqe->ioprio);
1452         if (ioprio) {
1453                 ret = ioprio_check_cap(ioprio);
1454                 if (ret)
1455                         return ret;
1456
1457                 kiocb->ki_ioprio = ioprio;
1458         } else
1459                 kiocb->ki_ioprio = get_current_ioprio();
1460
1461         ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1462         if (unlikely(ret))
1463                 return ret;
1464
1465         /* don't allow async punt if RWF_NOWAIT was requested */
1466         if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1467             (req->file->f_flags & O_NONBLOCK))
1468                 req->flags |= REQ_F_NOWAIT;
1469
1470         if (force_nonblock)
1471                 kiocb->ki_flags |= IOCB_NOWAIT;
1472
1473         if (ctx->flags & IORING_SETUP_IOPOLL) {
1474                 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1475                     !kiocb->ki_filp->f_op->iopoll)
1476                         return -EOPNOTSUPP;
1477
1478                 kiocb->ki_flags |= IOCB_HIPRI;
1479                 kiocb->ki_complete = io_complete_rw_iopoll;
1480                 req->result = 0;
1481         } else {
1482                 if (kiocb->ki_flags & IOCB_HIPRI)
1483                         return -EINVAL;
1484                 kiocb->ki_complete = io_complete_rw;
1485         }
1486         return 0;
1487 }
1488
1489 static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1490 {
1491         switch (ret) {
1492         case -EIOCBQUEUED:
1493                 break;
1494         case -ERESTARTSYS:
1495         case -ERESTARTNOINTR:
1496         case -ERESTARTNOHAND:
1497         case -ERESTART_RESTARTBLOCK:
1498                 /*
1499                  * We can't just restart the syscall, since previously
1500                  * submitted sqes may already be in progress. Just fail this
1501                  * IO with EINTR.
1502                  */
1503                 ret = -EINTR;
1504                 /* fall through */
1505         default:
1506                 kiocb->ki_complete(kiocb, ret, 0);
1507         }
1508 }
1509
1510 static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1511                        bool in_async)
1512 {
1513         if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
1514                 *nxt = __io_complete_rw(kiocb, ret);
1515         else
1516                 io_rw_done(kiocb, ret);
1517 }
1518
1519 static ssize_t io_import_fixed(struct io_ring_ctx *ctx, int rw,
1520                                const struct io_uring_sqe *sqe,
1521                                struct iov_iter *iter)
1522 {
1523         size_t len = READ_ONCE(sqe->len);
1524         struct io_mapped_ubuf *imu;
1525         unsigned index, buf_index;
1526         size_t offset;
1527         u64 buf_addr;
1528
1529         /* attempt to use fixed buffers without having provided iovecs */
1530         if (unlikely(!ctx->user_bufs))
1531                 return -EFAULT;
1532
1533         buf_index = READ_ONCE(sqe->buf_index);
1534         if (unlikely(buf_index >= ctx->nr_user_bufs))
1535                 return -EFAULT;
1536
1537         index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1538         imu = &ctx->user_bufs[index];
1539         buf_addr = READ_ONCE(sqe->addr);
1540
1541         /* overflow */
1542         if (buf_addr + len < buf_addr)
1543                 return -EFAULT;
1544         /* not inside the mapped region */
1545         if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1546                 return -EFAULT;
1547
1548         /*
1549          * May not be a start of buffer, set size appropriately
1550          * and advance us to the beginning.
1551          */
1552         offset = buf_addr - imu->ubuf;
1553         iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
1554
1555         if (offset) {
1556                 /*
1557                  * Don't use iov_iter_advance() here, as it's really slow for
1558                  * using the latter parts of a big fixed buffer - it iterates
1559                  * over each segment manually. We can cheat a bit here, because
1560                  * we know that:
1561                  *
1562                  * 1) it's a BVEC iter, we set it up
1563                  * 2) all bvecs are PAGE_SIZE in size, except potentially the
1564                  *    first and last bvec
1565                  *
1566                  * So just find our index, and adjust the iterator afterwards.
1567                  * If the offset is within the first bvec (or the whole first
1568                  * bvec, just use iov_iter_advance(). This makes it easier
1569                  * since we can just skip the first segment, which may not
1570                  * be PAGE_SIZE aligned.
1571                  */
1572                 const struct bio_vec *bvec = imu->bvec;
1573
1574                 if (offset <= bvec->bv_len) {
1575                         iov_iter_advance(iter, offset);
1576                 } else {
1577                         unsigned long seg_skip;
1578
1579                         /* skip first vec */
1580                         offset -= bvec->bv_len;
1581                         seg_skip = 1 + (offset >> PAGE_SHIFT);
1582
1583                         iter->bvec = bvec + seg_skip;
1584                         iter->nr_segs -= seg_skip;
1585                         iter->count -= bvec->bv_len + offset;
1586                         iter->iov_offset = offset & ~PAGE_MASK;
1587                 }
1588         }
1589
1590         return len;
1591 }
1592
1593 static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
1594                                struct iovec **iovec, struct iov_iter *iter)
1595 {
1596         const struct io_uring_sqe *sqe = req->sqe;
1597         void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1598         size_t sqe_len = READ_ONCE(sqe->len);
1599         u8 opcode;
1600
1601         /*
1602          * We're reading ->opcode for the second time, but the first read
1603          * doesn't care whether it's _FIXED or not, so it doesn't matter
1604          * whether ->opcode changes concurrently. The first read does care
1605          * about whether it is a READ or a WRITE, so we don't trust this read
1606          * for that purpose and instead let the caller pass in the read/write
1607          * flag.
1608          */
1609         opcode = READ_ONCE(sqe->opcode);
1610         if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
1611                 *iovec = NULL;
1612                 return io_import_fixed(req->ctx, rw, sqe, iter);
1613         }
1614
1615         if (req->io) {
1616                 struct io_async_rw *iorw = &req->io->rw;
1617
1618                 *iovec = iorw->iov;
1619                 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
1620                 if (iorw->iov == iorw->fast_iov)
1621                         *iovec = NULL;
1622                 return iorw->size;
1623         }
1624
1625         if (!req->has_user)
1626                 return -EFAULT;
1627
1628 #ifdef CONFIG_COMPAT
1629         if (req->ctx->compat)
1630                 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1631                                                 iovec, iter);
1632 #endif
1633
1634         return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1635 }
1636
1637 /*
1638  * For files that don't have ->read_iter() and ->write_iter(), handle them
1639  * by looping over ->read() or ->write() manually.
1640  */
1641 static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1642                            struct iov_iter *iter)
1643 {
1644         ssize_t ret = 0;
1645
1646         /*
1647          * Don't support polled IO through this interface, and we can't
1648          * support non-blocking either. For the latter, this just causes
1649          * the kiocb to be handled from an async context.
1650          */
1651         if (kiocb->ki_flags & IOCB_HIPRI)
1652                 return -EOPNOTSUPP;
1653         if (kiocb->ki_flags & IOCB_NOWAIT)
1654                 return -EAGAIN;
1655
1656         while (iov_iter_count(iter)) {
1657                 struct iovec iovec;
1658                 ssize_t nr;
1659
1660                 if (!iov_iter_is_bvec(iter)) {
1661                         iovec = iov_iter_iovec(iter);
1662                 } else {
1663                         /* fixed buffers import bvec */
1664                         iovec.iov_base = kmap(iter->bvec->bv_page)
1665                                                 + iter->iov_offset;
1666                         iovec.iov_len = min(iter->count,
1667                                         iter->bvec->bv_len - iter->iov_offset);
1668                 }
1669
1670                 if (rw == READ) {
1671                         nr = file->f_op->read(file, iovec.iov_base,
1672                                               iovec.iov_len, &kiocb->ki_pos);
1673                 } else {
1674                         nr = file->f_op->write(file, iovec.iov_base,
1675                                                iovec.iov_len, &kiocb->ki_pos);
1676                 }
1677
1678                 if (iov_iter_is_bvec(iter))
1679                         kunmap(iter->bvec->bv_page);
1680
1681                 if (nr < 0) {
1682                         if (!ret)
1683                                 ret = nr;
1684                         break;
1685                 }
1686                 ret += nr;
1687                 if (nr != iovec.iov_len)
1688                         break;
1689                 iov_iter_advance(iter, nr);
1690         }
1691
1692         return ret;
1693 }
1694
1695 static void io_req_map_io(struct io_kiocb *req, ssize_t io_size,
1696                           struct iovec *iovec, struct iovec *fast_iov,
1697                           struct iov_iter *iter)
1698 {
1699         req->io->rw.nr_segs = iter->nr_segs;
1700         req->io->rw.size = io_size;
1701         req->io->rw.iov = iovec;
1702         if (!req->io->rw.iov) {
1703                 req->io->rw.iov = req->io->rw.fast_iov;
1704                 memcpy(req->io->rw.iov, fast_iov,
1705                         sizeof(struct iovec) * iter->nr_segs);
1706         }
1707 }
1708
1709 static int io_setup_async_io(struct io_kiocb *req, ssize_t io_size,
1710                              struct iovec *iovec, struct iovec *fast_iov,
1711                              struct iov_iter *iter)
1712 {
1713         req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
1714         if (req->io) {
1715                 io_req_map_io(req, io_size, iovec, fast_iov, iter);
1716                 memcpy(&req->io->sqe, req->sqe, sizeof(req->io->sqe));
1717                 req->sqe = &req->io->sqe;
1718                 return 0;
1719         }
1720
1721         return -ENOMEM;
1722 }
1723
1724 static int io_read_prep(struct io_kiocb *req, struct iovec **iovec,
1725                         struct iov_iter *iter, bool force_nonblock)
1726 {
1727         ssize_t ret;
1728
1729         ret = io_prep_rw(req, force_nonblock);
1730         if (ret)
1731                 return ret;
1732
1733         if (unlikely(!(req->file->f_mode & FMODE_READ)))
1734                 return -EBADF;
1735
1736         return io_import_iovec(READ, req, iovec, iter);
1737 }
1738
1739 static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
1740                    bool force_nonblock)
1741 {
1742         struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1743         struct kiocb *kiocb = &req->rw;
1744         struct iov_iter iter;
1745         struct file *file;
1746         size_t iov_count;
1747         ssize_t io_size, ret;
1748
1749         if (!req->io) {
1750                 ret = io_read_prep(req, &iovec, &iter, force_nonblock);
1751                 if (ret < 0)
1752                         return ret;
1753         } else {
1754                 ret = io_import_iovec(READ, req, &iovec, &iter);
1755                 if (ret < 0)
1756                         return ret;
1757         }
1758
1759         file = req->file;
1760         io_size = ret;
1761         if (req->flags & REQ_F_LINK)
1762                 req->result = io_size;
1763
1764         /*
1765          * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1766          * we know to async punt it even if it was opened O_NONBLOCK
1767          */
1768         if (force_nonblock && !io_file_supports_async(file)) {
1769                 req->flags |= REQ_F_MUST_PUNT;
1770                 goto copy_iov;
1771         }
1772
1773         iov_count = iov_iter_count(&iter);
1774         ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
1775         if (!ret) {
1776                 ssize_t ret2;
1777
1778                 if (file->f_op->read_iter)
1779                         ret2 = call_read_iter(file, kiocb, &iter);
1780                 else
1781                         ret2 = loop_rw_iter(READ, file, kiocb, &iter);
1782
1783                 /*
1784                  * In case of a short read, punt to async. This can happen
1785                  * if we have data partially cached. Alternatively we can
1786                  * return the short read, in which case the application will
1787                  * need to issue another SQE and wait for it. That SQE will
1788                  * need async punt anyway, so it's more efficient to do it
1789                  * here.
1790                  */
1791                 if (force_nonblock && !(req->flags & REQ_F_NOWAIT) &&
1792                     (req->flags & REQ_F_ISREG) &&
1793                     ret2 > 0 && ret2 < io_size)
1794                         ret2 = -EAGAIN;
1795                 /* Catch -EAGAIN return for forced non-blocking submission */
1796                 if (!force_nonblock || ret2 != -EAGAIN) {
1797                         kiocb_done(kiocb, ret2, nxt, req->in_async);
1798                 } else {
1799 copy_iov:
1800                         ret = io_setup_async_io(req, io_size, iovec,
1801                                                 inline_vecs, &iter);
1802                         if (ret)
1803                                 goto out_free;
1804                         return -EAGAIN;
1805                 }
1806         }
1807 out_free:
1808         kfree(iovec);
1809         return ret;
1810 }
1811
1812 static int io_write_prep(struct io_kiocb *req, struct iovec **iovec,
1813                          struct iov_iter *iter, bool force_nonblock)
1814 {
1815         ssize_t ret;
1816
1817         ret = io_prep_rw(req, force_nonblock);
1818         if (ret)
1819                 return ret;
1820
1821         if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
1822                 return -EBADF;
1823
1824         return io_import_iovec(WRITE, req, iovec, iter);
1825 }
1826
1827 static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
1828                     bool force_nonblock)
1829 {
1830         struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1831         struct kiocb *kiocb = &req->rw;
1832         struct iov_iter iter;
1833         struct file *file;
1834         size_t iov_count;
1835         ssize_t ret, io_size;
1836
1837         if (!req->io) {
1838                 ret = io_write_prep(req, &iovec, &iter, force_nonblock);
1839                 if (ret < 0)
1840                         return ret;
1841         } else {
1842                 ret = io_import_iovec(WRITE, req, &iovec, &iter);
1843                 if (ret < 0)
1844                         return ret;
1845         }
1846
1847         file = kiocb->ki_filp;
1848         io_size = ret;
1849         if (req->flags & REQ_F_LINK)
1850                 req->result = io_size;
1851
1852         /*
1853          * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1854          * we know to async punt it even if it was opened O_NONBLOCK
1855          */
1856         if (force_nonblock && !io_file_supports_async(req->file)) {
1857                 req->flags |= REQ_F_MUST_PUNT;
1858                 goto copy_iov;
1859         }
1860
1861         if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT))
1862                 goto copy_iov;
1863
1864         iov_count = iov_iter_count(&iter);
1865         ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
1866         if (!ret) {
1867                 ssize_t ret2;
1868
1869                 /*
1870                  * Open-code file_start_write here to grab freeze protection,
1871                  * which will be released by another thread in
1872                  * io_complete_rw().  Fool lockdep by telling it the lock got
1873                  * released so that it doesn't complain about the held lock when
1874                  * we return to userspace.
1875                  */
1876                 if (req->flags & REQ_F_ISREG) {
1877                         __sb_start_write(file_inode(file)->i_sb,
1878                                                 SB_FREEZE_WRITE, true);
1879                         __sb_writers_release(file_inode(file)->i_sb,
1880                                                 SB_FREEZE_WRITE);
1881                 }
1882                 kiocb->ki_flags |= IOCB_WRITE;
1883
1884                 if (file->f_op->write_iter)
1885                         ret2 = call_write_iter(file, kiocb, &iter);
1886                 else
1887                         ret2 = loop_rw_iter(WRITE, file, kiocb, &iter);
1888                 if (!force_nonblock || ret2 != -EAGAIN) {
1889                         kiocb_done(kiocb, ret2, nxt, req->in_async);
1890                 } else {
1891 copy_iov:
1892                         ret = io_setup_async_io(req, io_size, iovec,
1893                                                 inline_vecs, &iter);
1894                         if (ret)
1895                                 goto out_free;
1896                         return -EAGAIN;
1897                 }
1898         }
1899 out_free:
1900         kfree(iovec);
1901         return ret;
1902 }
1903
1904 /*
1905  * IORING_OP_NOP just posts a completion event, nothing else.
1906  */
1907 static int io_nop(struct io_kiocb *req)
1908 {
1909         struct io_ring_ctx *ctx = req->ctx;
1910
1911         if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1912                 return -EINVAL;
1913
1914         io_cqring_add_event(req, 0);
1915         io_put_req(req);
1916         return 0;
1917 }
1918
1919 static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1920 {
1921         struct io_ring_ctx *ctx = req->ctx;
1922
1923         if (!req->file)
1924                 return -EBADF;
1925
1926         if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1927                 return -EINVAL;
1928         if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1929                 return -EINVAL;
1930
1931         return 0;
1932 }
1933
1934 static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1935                     struct io_kiocb **nxt, bool force_nonblock)
1936 {
1937         loff_t sqe_off = READ_ONCE(sqe->off);
1938         loff_t sqe_len = READ_ONCE(sqe->len);
1939         loff_t end = sqe_off + sqe_len;
1940         unsigned fsync_flags;
1941         int ret;
1942
1943         fsync_flags = READ_ONCE(sqe->fsync_flags);
1944         if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1945                 return -EINVAL;
1946
1947         ret = io_prep_fsync(req, sqe);
1948         if (ret)
1949                 return ret;
1950
1951         /* fsync always requires a blocking context */
1952         if (force_nonblock)
1953                 return -EAGAIN;
1954
1955         ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1956                                 end > 0 ? end : LLONG_MAX,
1957                                 fsync_flags & IORING_FSYNC_DATASYNC);
1958
1959         if (ret < 0 && (req->flags & REQ_F_LINK))
1960                 req->flags |= REQ_F_FAIL_LINK;
1961         io_cqring_add_event(req, ret);
1962         io_put_req_find_next(req, nxt);
1963         return 0;
1964 }
1965
1966 static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1967 {
1968         struct io_ring_ctx *ctx = req->ctx;
1969         int ret = 0;
1970
1971         if (!req->file)
1972                 return -EBADF;
1973
1974         if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1975                 return -EINVAL;
1976         if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1977                 return -EINVAL;
1978
1979         return ret;
1980 }
1981
1982 static int io_sync_file_range(struct io_kiocb *req,
1983                               const struct io_uring_sqe *sqe,
1984                               struct io_kiocb **nxt,
1985                               bool force_nonblock)
1986 {
1987         loff_t sqe_off;
1988         loff_t sqe_len;
1989         unsigned flags;
1990         int ret;
1991
1992         ret = io_prep_sfr(req, sqe);
1993         if (ret)
1994                 return ret;
1995
1996         /* sync_file_range always requires a blocking context */
1997         if (force_nonblock)
1998                 return -EAGAIN;
1999
2000         sqe_off = READ_ONCE(sqe->off);
2001         sqe_len = READ_ONCE(sqe->len);
2002         flags = READ_ONCE(sqe->sync_range_flags);
2003
2004         ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
2005
2006         if (ret < 0 && (req->flags & REQ_F_LINK))
2007                 req->flags |= REQ_F_FAIL_LINK;
2008         io_cqring_add_event(req, ret);
2009         io_put_req_find_next(req, nxt);
2010         return 0;
2011 }
2012
2013 static int io_sendmsg_prep(struct io_kiocb *req, struct io_async_ctx *io)
2014 {
2015 #if defined(CONFIG_NET)
2016         const struct io_uring_sqe *sqe = req->sqe;
2017         struct user_msghdr __user *msg;
2018         unsigned flags;
2019
2020         flags = READ_ONCE(sqe->msg_flags);
2021         msg = (struct user_msghdr __user *)(unsigned long) READ_ONCE(sqe->addr);
2022         return sendmsg_copy_msghdr(&io->msg.msg, msg, flags, &io->msg.iov);
2023 #else
2024         return 0;
2025 #endif
2026 }
2027
2028 static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2029                       struct io_kiocb **nxt, bool force_nonblock)
2030 {
2031 #if defined(CONFIG_NET)
2032         struct socket *sock;
2033         int ret;
2034
2035         if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2036                 return -EINVAL;
2037
2038         sock = sock_from_file(req->file, &ret);
2039         if (sock) {
2040                 struct io_async_ctx io, *copy;
2041                 struct sockaddr_storage addr;
2042                 struct msghdr *kmsg;
2043                 unsigned flags;
2044
2045                 flags = READ_ONCE(sqe->msg_flags);
2046                 if (flags & MSG_DONTWAIT)
2047                         req->flags |= REQ_F_NOWAIT;
2048                 else if (force_nonblock)
2049                         flags |= MSG_DONTWAIT;
2050
2051                 if (req->io) {
2052                         kmsg = &req->io->msg.msg;
2053                         kmsg->msg_name = &addr;
2054                 } else {
2055                         kmsg = &io.msg.msg;
2056                         kmsg->msg_name = &addr;
2057                         io.msg.iov = io.msg.fast_iov;
2058                         ret = io_sendmsg_prep(req, &io);
2059                         if (ret)
2060                                 goto out;
2061                 }
2062
2063                 ret = __sys_sendmsg_sock(sock, kmsg, flags);
2064                 if (force_nonblock && ret == -EAGAIN) {
2065                         copy = kmalloc(sizeof(*copy), GFP_KERNEL);
2066                         if (!copy) {
2067                                 ret = -ENOMEM;
2068                                 goto out;
2069                         }
2070                         memcpy(&copy->msg, &io.msg, sizeof(copy->msg));
2071                         req->io = copy;
2072                         memcpy(&req->io->sqe, req->sqe, sizeof(*req->sqe));
2073                         req->sqe = &req->io->sqe;
2074                         return ret;
2075                 }
2076                 if (ret == -ERESTARTSYS)
2077                         ret = -EINTR;
2078         }
2079
2080 out:
2081         io_cqring_add_event(req, ret);
2082         if (ret < 0 && (req->flags & REQ_F_LINK))
2083                 req->flags |= REQ_F_FAIL_LINK;
2084         io_put_req_find_next(req, nxt);
2085         return 0;
2086 #else
2087         return -EOPNOTSUPP;
2088 #endif
2089 }
2090
2091 static int io_recvmsg_prep(struct io_kiocb *req, struct io_async_ctx *io)
2092 {
2093 #if defined(CONFIG_NET)
2094         const struct io_uring_sqe *sqe = req->sqe;
2095         struct user_msghdr __user *msg;
2096         unsigned flags;
2097
2098         flags = READ_ONCE(sqe->msg_flags);
2099         msg = (struct user_msghdr __user *)(unsigned long) READ_ONCE(sqe->addr);
2100         return recvmsg_copy_msghdr(&io->msg.msg, msg, flags, &io->msg.uaddr,
2101                                         &io->msg.iov);
2102 #else
2103         return 0;
2104 #endif
2105 }
2106
2107 static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2108                       struct io_kiocb **nxt, bool force_nonblock)
2109 {
2110 #if defined(CONFIG_NET)
2111         struct socket *sock;
2112         int ret;
2113
2114         if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2115                 return -EINVAL;
2116
2117         sock = sock_from_file(req->file, &ret);
2118         if (sock) {
2119                 struct user_msghdr __user *msg;
2120                 struct io_async_ctx io, *copy;
2121                 struct sockaddr_storage addr;
2122                 struct msghdr *kmsg;
2123                 unsigned flags;
2124
2125                 flags = READ_ONCE(sqe->msg_flags);
2126                 if (flags & MSG_DONTWAIT)
2127                         req->flags |= REQ_F_NOWAIT;
2128                 else if (force_nonblock)
2129                         flags |= MSG_DONTWAIT;
2130
2131                 msg = (struct user_msghdr __user *) (unsigned long)
2132                         READ_ONCE(sqe->addr);
2133                 if (req->io) {
2134                         kmsg = &req->io->msg.msg;
2135                         kmsg->msg_name = &addr;
2136                 } else {
2137                         kmsg = &io.msg.msg;
2138                         kmsg->msg_name = &addr;
2139                         io.msg.iov = io.msg.fast_iov;
2140                         ret = io_recvmsg_prep(req, &io);
2141                         if (ret)
2142                                 goto out;
2143                 }
2144
2145                 ret = __sys_recvmsg_sock(sock, kmsg, msg, io.msg.uaddr, flags);
2146                 if (force_nonblock && ret == -EAGAIN) {
2147                         copy = kmalloc(sizeof(*copy), GFP_KERNEL);
2148                         if (!copy) {
2149                                 ret = -ENOMEM;
2150                                 goto out;
2151                         }
2152                         memcpy(copy, &io, sizeof(*copy));
2153                         req->io = copy;
2154                         memcpy(&req->io->sqe, req->sqe, sizeof(*req->sqe));
2155                         req->sqe = &req->io->sqe;
2156                         return ret;
2157                 }
2158                 if (ret == -ERESTARTSYS)
2159                         ret = -EINTR;
2160         }
2161
2162 out:
2163         io_cqring_add_event(req, ret);
2164         if (ret < 0 && (req->flags & REQ_F_LINK))
2165                 req->flags |= REQ_F_FAIL_LINK;
2166         io_put_req_find_next(req, nxt);
2167         return 0;
2168 #else
2169         return -EOPNOTSUPP;
2170 #endif
2171 }
2172
2173 static int io_accept(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2174                      struct io_kiocb **nxt, bool force_nonblock)
2175 {
2176 #if defined(CONFIG_NET)
2177         struct sockaddr __user *addr;
2178         int __user *addr_len;
2179         unsigned file_flags;
2180         int flags, ret;
2181
2182         if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
2183                 return -EINVAL;
2184         if (sqe->ioprio || sqe->len || sqe->buf_index)
2185                 return -EINVAL;
2186
2187         addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr);
2188         addr_len = (int __user *) (unsigned long) READ_ONCE(sqe->addr2);
2189         flags = READ_ONCE(sqe->accept_flags);
2190         file_flags = force_nonblock ? O_NONBLOCK : 0;
2191
2192         ret = __sys_accept4_file(req->file, file_flags, addr, addr_len, flags);
2193         if (ret == -EAGAIN && force_nonblock) {
2194                 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2195                 return -EAGAIN;
2196         }
2197         if (ret == -ERESTARTSYS)
2198                 ret = -EINTR;
2199         if (ret < 0 && (req->flags & REQ_F_LINK))
2200                 req->flags |= REQ_F_FAIL_LINK;
2201         io_cqring_add_event(req, ret);
2202         io_put_req_find_next(req, nxt);
2203         return 0;
2204 #else
2205         return -EOPNOTSUPP;
2206 #endif
2207 }
2208
2209 static int io_connect_prep(struct io_kiocb *req, struct io_async_ctx *io)
2210 {
2211 #if defined(CONFIG_NET)
2212         const struct io_uring_sqe *sqe = req->sqe;
2213         struct sockaddr __user *addr;
2214         int addr_len;
2215
2216         addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr);
2217         addr_len = READ_ONCE(sqe->addr2);
2218         return move_addr_to_kernel(addr, addr_len, &io->connect.address);
2219 #else
2220         return 0;
2221 #endif
2222 }
2223
2224 static int io_connect(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2225                       struct io_kiocb **nxt, bool force_nonblock)
2226 {
2227 #if defined(CONFIG_NET)
2228         struct io_async_ctx __io, *io;
2229         unsigned file_flags;
2230         int addr_len, ret;
2231
2232         if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
2233                 return -EINVAL;
2234         if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
2235                 return -EINVAL;
2236
2237         addr_len = READ_ONCE(sqe->addr2);
2238         file_flags = force_nonblock ? O_NONBLOCK : 0;
2239
2240         if (req->io) {
2241                 io = req->io;
2242         } else {
2243                 ret = io_connect_prep(req, &__io);
2244                 if (ret)
2245                         goto out;
2246                 io = &__io;
2247         }
2248
2249         ret = __sys_connect_file(req->file, &io->connect.address, addr_len,
2250                                         file_flags);
2251         if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
2252                 io = kmalloc(sizeof(*io), GFP_KERNEL);
2253                 if (!io) {
2254                         ret = -ENOMEM;
2255                         goto out;
2256                 }
2257                 memcpy(&io->connect, &__io.connect, sizeof(io->connect));
2258                 req->io = io;
2259                 memcpy(&io->sqe, req->sqe, sizeof(*req->sqe));
2260                 req->sqe = &io->sqe;
2261                 return -EAGAIN;
2262         }
2263         if (ret == -ERESTARTSYS)
2264                 ret = -EINTR;
2265 out:
2266         if (ret < 0 && (req->flags & REQ_F_LINK))
2267                 req->flags |= REQ_F_FAIL_LINK;
2268         io_cqring_add_event(req, ret);
2269         io_put_req_find_next(req, nxt);
2270         return 0;
2271 #else
2272         return -EOPNOTSUPP;
2273 #endif
2274 }
2275
2276 static void io_poll_remove_one(struct io_kiocb *req)
2277 {
2278         struct io_poll_iocb *poll = &req->poll;
2279
2280         spin_lock(&poll->head->lock);
2281         WRITE_ONCE(poll->canceled, true);
2282         if (!list_empty(&poll->wait->entry)) {
2283                 list_del_init(&poll->wait->entry);
2284                 io_queue_async_work(req);
2285         }
2286         spin_unlock(&poll->head->lock);
2287         hash_del(&req->hash_node);
2288 }
2289
2290 static void io_poll_remove_all(struct io_ring_ctx *ctx)
2291 {
2292         struct hlist_node *tmp;
2293         struct io_kiocb *req;
2294         int i;
2295
2296         spin_lock_irq(&ctx->completion_lock);
2297         for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
2298                 struct hlist_head *list;
2299
2300                 list = &ctx->cancel_hash[i];
2301                 hlist_for_each_entry_safe(req, tmp, list, hash_node)
2302                         io_poll_remove_one(req);
2303         }
2304         spin_unlock_irq(&ctx->completion_lock);
2305 }
2306
2307 static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
2308 {
2309         struct hlist_head *list;
2310         struct io_kiocb *req;
2311
2312         list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
2313         hlist_for_each_entry(req, list, hash_node) {
2314                 if (sqe_addr == req->user_data) {
2315                         io_poll_remove_one(req);
2316                         return 0;
2317                 }
2318         }
2319
2320         return -ENOENT;
2321 }
2322
2323 /*
2324  * Find a running poll command that matches one specified in sqe->addr,
2325  * and remove it if found.
2326  */
2327 static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2328 {
2329         struct io_ring_ctx *ctx = req->ctx;
2330         int ret;
2331
2332         if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2333                 return -EINVAL;
2334         if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
2335             sqe->poll_events)
2336                 return -EINVAL;
2337
2338         spin_lock_irq(&ctx->completion_lock);
2339         ret = io_poll_cancel(ctx, READ_ONCE(sqe->addr));
2340         spin_unlock_irq(&ctx->completion_lock);
2341
2342         io_cqring_add_event(req, ret);
2343         if (ret < 0 && (req->flags & REQ_F_LINK))
2344                 req->flags |= REQ_F_FAIL_LINK;
2345         io_put_req(req);
2346         return 0;
2347 }
2348
2349 static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
2350 {
2351         struct io_ring_ctx *ctx = req->ctx;
2352
2353         req->poll.done = true;
2354         kfree(req->poll.wait);
2355         if (error)
2356                 io_cqring_fill_event(req, error);
2357         else
2358                 io_cqring_fill_event(req, mangle_poll(mask));
2359         io_commit_cqring(ctx);
2360 }
2361
2362 static void io_poll_complete_work(struct io_wq_work **workptr)
2363 {
2364         struct io_wq_work *work = *workptr;
2365         struct io_kiocb *req = container_of(work, struct io_kiocb, work);
2366         struct io_poll_iocb *poll = &req->poll;
2367         struct poll_table_struct pt = { ._key = poll->events };
2368         struct io_ring_ctx *ctx = req->ctx;
2369         struct io_kiocb *nxt = NULL;
2370         __poll_t mask = 0;
2371         int ret = 0;
2372
2373         if (work->flags & IO_WQ_WORK_CANCEL) {
2374                 WRITE_ONCE(poll->canceled, true);
2375                 ret = -ECANCELED;
2376         } else if (READ_ONCE(poll->canceled)) {
2377                 ret = -ECANCELED;
2378         }
2379
2380         if (ret != -ECANCELED)
2381                 mask = vfs_poll(poll->file, &pt) & poll->events;
2382
2383         /*
2384          * Note that ->ki_cancel callers also delete iocb from active_reqs after
2385          * calling ->ki_cancel.  We need the ctx_lock roundtrip here to
2386          * synchronize with them.  In the cancellation case the list_del_init
2387          * itself is not actually needed, but harmless so we keep it in to
2388          * avoid further branches in the fast path.
2389          */
2390         spin_lock_irq(&ctx->completion_lock);
2391         if (!mask && ret != -ECANCELED) {
2392                 add_wait_queue(poll->head, poll->wait);
2393                 spin_unlock_irq(&ctx->completion_lock);
2394                 return;
2395         }
2396         hash_del(&req->hash_node);
2397         io_poll_complete(req, mask, ret);
2398         spin_unlock_irq(&ctx->completion_lock);
2399
2400         io_cqring_ev_posted(ctx);
2401
2402         if (ret < 0 && req->flags & REQ_F_LINK)
2403                 req->flags |= REQ_F_FAIL_LINK;
2404         io_put_req_find_next(req, &nxt);
2405         if (nxt)
2406                 *workptr = &nxt->work;
2407 }
2408
2409 static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
2410                         void *key)
2411 {
2412         struct io_poll_iocb *poll = wait->private;
2413         struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
2414         struct io_ring_ctx *ctx = req->ctx;
2415         __poll_t mask = key_to_poll(key);
2416         unsigned long flags;
2417
2418         /* for instances that support it check for an event match first: */
2419         if (mask && !(mask & poll->events))
2420                 return 0;
2421
2422         list_del_init(&poll->wait->entry);
2423
2424         /*
2425          * Run completion inline if we can. We're using trylock here because
2426          * we are violating the completion_lock -> poll wq lock ordering.
2427          * If we have a link timeout we're going to need the completion_lock
2428          * for finalizing the request, mark us as having grabbed that already.
2429          */
2430         if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
2431                 hash_del(&req->hash_node);
2432                 io_poll_complete(req, mask, 0);
2433                 req->flags |= REQ_F_COMP_LOCKED;
2434                 io_put_req(req);
2435                 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2436
2437                 io_cqring_ev_posted(ctx);
2438         } else {
2439                 io_queue_async_work(req);
2440         }
2441
2442         return 1;
2443 }
2444
2445 struct io_poll_table {
2446         struct poll_table_struct pt;
2447         struct io_kiocb *req;
2448         int error;
2449 };
2450
2451 static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
2452                                struct poll_table_struct *p)
2453 {
2454         struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
2455
2456         if (unlikely(pt->req->poll.head)) {
2457                 pt->error = -EINVAL;
2458                 return;
2459         }
2460
2461         pt->error = 0;
2462         pt->req->poll.head = head;
2463         add_wait_queue(head, pt->req->poll.wait);
2464 }
2465
2466 static void io_poll_req_insert(struct io_kiocb *req)
2467 {
2468         struct io_ring_ctx *ctx = req->ctx;
2469         struct hlist_head *list;
2470
2471         list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
2472         hlist_add_head(&req->hash_node, list);
2473 }
2474
2475 static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2476                        struct io_kiocb **nxt)
2477 {
2478         struct io_poll_iocb *poll = &req->poll;
2479         struct io_ring_ctx *ctx = req->ctx;
2480         struct io_poll_table ipt;
2481         bool cancel = false;
2482         __poll_t mask;
2483         u16 events;
2484
2485         if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2486                 return -EINVAL;
2487         if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
2488                 return -EINVAL;
2489         if (!poll->file)
2490                 return -EBADF;
2491
2492         poll->wait = kmalloc(sizeof(*poll->wait), GFP_KERNEL);
2493         if (!poll->wait)
2494                 return -ENOMEM;
2495
2496         req->io = NULL;
2497         INIT_IO_WORK(&req->work, io_poll_complete_work);
2498         events = READ_ONCE(sqe->poll_events);
2499         poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
2500         INIT_HLIST_NODE(&req->hash_node);
2501
2502         poll->head = NULL;
2503         poll->done = false;
2504         poll->canceled = false;
2505
2506         ipt.pt._qproc = io_poll_queue_proc;
2507         ipt.pt._key = poll->events;
2508         ipt.req = req;
2509         ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
2510
2511         /* initialized the list so that we can do list_empty checks */
2512         INIT_LIST_HEAD(&poll->wait->entry);
2513         init_waitqueue_func_entry(poll->wait, io_poll_wake);
2514         poll->wait->private = poll;
2515
2516         INIT_LIST_HEAD(&req->list);
2517
2518         mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
2519
2520         spin_lock_irq(&ctx->completion_lock);
2521         if (likely(poll->head)) {
2522                 spin_lock(&poll->head->lock);
2523                 if (unlikely(list_empty(&poll->wait->entry))) {
2524                         if (ipt.error)
2525                                 cancel = true;
2526                         ipt.error = 0;
2527                         mask = 0;
2528                 }
2529                 if (mask || ipt.error)
2530                         list_del_init(&poll->wait->entry);
2531                 else if (cancel)
2532                         WRITE_ONCE(poll->canceled, true);
2533                 else if (!poll->done) /* actually waiting for an event */
2534                         io_poll_req_insert(req);
2535                 spin_unlock(&poll->head->lock);
2536         }
2537         if (mask) { /* no async, we'd stolen it */
2538                 ipt.error = 0;
2539                 io_poll_complete(req, mask, 0);
2540         }
2541         spin_unlock_irq(&ctx->completion_lock);
2542
2543         if (mask) {
2544                 io_cqring_ev_posted(ctx);
2545                 io_put_req_find_next(req, nxt);
2546         }
2547         return ipt.error;
2548 }
2549
2550 static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
2551 {
2552         struct io_timeout_data *data = container_of(timer,
2553                                                 struct io_timeout_data, timer);
2554         struct io_kiocb *req = data->req;
2555         struct io_ring_ctx *ctx = req->ctx;
2556         unsigned long flags;
2557
2558         atomic_inc(&ctx->cq_timeouts);
2559
2560         spin_lock_irqsave(&ctx->completion_lock, flags);
2561         /*
2562          * We could be racing with timeout deletion. If the list is empty,
2563          * then timeout lookup already found it and will be handling it.
2564          */
2565         if (!list_empty(&req->list)) {
2566                 struct io_kiocb *prev;
2567
2568                 /*
2569                  * Adjust the reqs sequence before the current one because it
2570                  * will consume a slot in the cq_ring and the the cq_tail
2571                  * pointer will be increased, otherwise other timeout reqs may
2572                  * return in advance without waiting for enough wait_nr.
2573                  */
2574                 prev = req;
2575                 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
2576                         prev->sequence++;
2577                 list_del_init(&req->list);
2578         }
2579
2580         io_cqring_fill_event(req, -ETIME);
2581         io_commit_cqring(ctx);
2582         spin_unlock_irqrestore(&ctx->completion_lock, flags);
2583
2584         io_cqring_ev_posted(ctx);
2585         if (req->flags & REQ_F_LINK)
2586                 req->flags |= REQ_F_FAIL_LINK;
2587         io_put_req(req);
2588         return HRTIMER_NORESTART;
2589 }
2590
2591 static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
2592 {
2593         struct io_kiocb *req;
2594         int ret = -ENOENT;
2595
2596         list_for_each_entry(req, &ctx->timeout_list, list) {
2597                 if (user_data == req->user_data) {
2598                         list_del_init(&req->list);
2599                         ret = 0;
2600                         break;
2601                 }
2602         }
2603
2604         if (ret == -ENOENT)
2605                 return ret;
2606
2607         ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
2608         if (ret == -1)
2609                 return -EALREADY;
2610
2611         if (req->flags & REQ_F_LINK)
2612                 req->flags |= REQ_F_FAIL_LINK;
2613         io_cqring_fill_event(req, -ECANCELED);
2614         io_put_req(req);
2615         return 0;
2616 }
2617
2618 /*
2619  * Remove or update an existing timeout command
2620  */
2621 static int io_timeout_remove(struct io_kiocb *req,
2622                              const struct io_uring_sqe *sqe)
2623 {
2624         struct io_ring_ctx *ctx = req->ctx;
2625         unsigned flags;
2626         int ret;
2627
2628         if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2629                 return -EINVAL;
2630         if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
2631                 return -EINVAL;
2632         flags = READ_ONCE(sqe->timeout_flags);
2633         if (flags)
2634                 return -EINVAL;
2635
2636         spin_lock_irq(&ctx->completion_lock);
2637         ret = io_timeout_cancel(ctx, READ_ONCE(sqe->addr));
2638
2639         io_cqring_fill_event(req, ret);
2640         io_commit_cqring(ctx);
2641         spin_unlock_irq(&ctx->completion_lock);
2642         io_cqring_ev_posted(ctx);
2643         if (ret < 0 && req->flags & REQ_F_LINK)
2644                 req->flags |= REQ_F_FAIL_LINK;
2645         io_put_req(req);
2646         return 0;
2647 }
2648
2649 static int io_timeout_prep(struct io_kiocb *req, struct io_async_ctx *io,
2650                            bool is_timeout_link)
2651 {
2652         const struct io_uring_sqe *sqe = req->sqe;
2653         struct io_timeout_data *data;
2654         unsigned flags;
2655
2656         if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2657                 return -EINVAL;
2658         if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
2659                 return -EINVAL;
2660         if (sqe->off && is_timeout_link)
2661                 return -EINVAL;
2662         flags = READ_ONCE(sqe->timeout_flags);
2663         if (flags & ~IORING_TIMEOUT_ABS)
2664                 return -EINVAL;
2665
2666         data = &io->timeout;
2667         data->req = req;
2668         req->flags |= REQ_F_TIMEOUT;
2669
2670         if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
2671                 return -EFAULT;
2672
2673         if (flags & IORING_TIMEOUT_ABS)
2674                 data->mode = HRTIMER_MODE_ABS;
2675         else
2676                 data->mode = HRTIMER_MODE_REL;
2677
2678         hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
2679         req->io = io;
2680         return 0;
2681 }
2682
2683 static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2684 {
2685         unsigned count;
2686         struct io_ring_ctx *ctx = req->ctx;
2687         struct io_timeout_data *data;
2688         struct io_async_ctx *io;
2689         struct list_head *entry;
2690         unsigned span = 0;
2691
2692         io = req->io;
2693         if (!io) {
2694                 int ret;
2695
2696                 io = kmalloc(sizeof(*io), GFP_KERNEL);
2697                 if (!io)
2698                         return -ENOMEM;
2699                 ret = io_timeout_prep(req, io, false);
2700                 if (ret) {
2701                         kfree(io);
2702                         return ret;
2703                 }
2704         }
2705         data = &req->io->timeout;
2706
2707         /*
2708          * sqe->off holds how many events that need to occur for this
2709          * timeout event to be satisfied. If it isn't set, then this is
2710          * a pure timeout request, sequence isn't used.
2711          */
2712         count = READ_ONCE(sqe->off);
2713         if (!count) {
2714                 req->flags |= REQ_F_TIMEOUT_NOSEQ;
2715                 spin_lock_irq(&ctx->completion_lock);
2716                 entry = ctx->timeout_list.prev;
2717                 goto add;
2718         }
2719
2720         req->sequence = ctx->cached_sq_head + count - 1;
2721         data->seq_offset = count;
2722
2723         /*
2724          * Insertion sort, ensuring the first entry in the list is always
2725          * the one we need first.
2726          */
2727         spin_lock_irq(&ctx->completion_lock);
2728         list_for_each_prev(entry, &ctx->timeout_list) {
2729                 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
2730                 unsigned nxt_sq_head;
2731                 long long tmp, tmp_nxt;
2732                 u32 nxt_offset = nxt->io->timeout.seq_offset;
2733
2734                 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
2735                         continue;
2736
2737                 /*
2738                  * Since cached_sq_head + count - 1 can overflow, use type long
2739                  * long to store it.
2740                  */
2741                 tmp = (long long)ctx->cached_sq_head + count - 1;
2742                 nxt_sq_head = nxt->sequence - nxt_offset + 1;
2743                 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
2744
2745                 /*
2746                  * cached_sq_head may overflow, and it will never overflow twice
2747                  * once there is some timeout req still be valid.
2748                  */
2749                 if (ctx->cached_sq_head < nxt_sq_head)
2750                         tmp += UINT_MAX;
2751
2752                 if (tmp > tmp_nxt)
2753                         break;
2754
2755                 /*
2756                  * Sequence of reqs after the insert one and itself should
2757                  * be adjusted because each timeout req consumes a slot.
2758                  */
2759                 span++;
2760                 nxt->sequence++;
2761         }
2762         req->sequence -= span;
2763 add:
2764         list_add(&req->list, entry);
2765         data->timer.function = io_timeout_fn;
2766         hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
2767         spin_unlock_irq(&ctx->completion_lock);
2768         return 0;
2769 }
2770
2771 static bool io_cancel_cb(struct io_wq_work *work, void *data)
2772 {
2773         struct io_kiocb *req = container_of(work, struct io_kiocb, work);
2774
2775         return req->user_data == (unsigned long) data;
2776 }
2777
2778 static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
2779 {
2780         enum io_wq_cancel cancel_ret;
2781         int ret = 0;
2782
2783         cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
2784         switch (cancel_ret) {
2785         case IO_WQ_CANCEL_OK:
2786                 ret = 0;
2787                 break;
2788         case IO_WQ_CANCEL_RUNNING:
2789                 ret = -EALREADY;
2790                 break;
2791         case IO_WQ_CANCEL_NOTFOUND:
2792                 ret = -ENOENT;
2793                 break;
2794         }
2795
2796         return ret;
2797 }
2798
2799 static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
2800                                      struct io_kiocb *req, __u64 sqe_addr,
2801                                      struct io_kiocb **nxt, int success_ret)
2802 {
2803         unsigned long flags;
2804         int ret;
2805
2806         ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
2807         if (ret != -ENOENT) {
2808                 spin_lock_irqsave(&ctx->completion_lock, flags);
2809                 goto done;
2810         }
2811
2812         spin_lock_irqsave(&ctx->completion_lock, flags);
2813         ret = io_timeout_cancel(ctx, sqe_addr);
2814         if (ret != -ENOENT)
2815                 goto done;
2816         ret = io_poll_cancel(ctx, sqe_addr);
2817 done:
2818         if (!ret)
2819                 ret = success_ret;
2820         io_cqring_fill_event(req, ret);
2821         io_commit_cqring(ctx);
2822         spin_unlock_irqrestore(&ctx->completion_lock, flags);
2823         io_cqring_ev_posted(ctx);
2824
2825         if (ret < 0 && (req->flags & REQ_F_LINK))
2826                 req->flags |= REQ_F_FAIL_LINK;
2827         io_put_req_find_next(req, nxt);
2828 }
2829
2830 static int io_async_cancel(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2831                            struct io_kiocb **nxt)
2832 {
2833         struct io_ring_ctx *ctx = req->ctx;
2834
2835         if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2836                 return -EINVAL;
2837         if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
2838             sqe->cancel_flags)
2839                 return -EINVAL;
2840
2841         io_async_find_and_cancel(ctx, req, READ_ONCE(sqe->addr), nxt, 0);
2842         return 0;
2843 }
2844
2845 static int io_req_defer_prep(struct io_kiocb *req, struct io_async_ctx *io)
2846 {
2847         struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
2848         struct iov_iter iter;
2849         ssize_t ret;
2850
2851         memcpy(&io->sqe, req->sqe, sizeof(io->sqe));
2852         req->sqe = &io->sqe;
2853
2854         switch (io->sqe.opcode) {
2855         case IORING_OP_READV:
2856         case IORING_OP_READ_FIXED:
2857                 ret = io_read_prep(req, &iovec, &iter, true);
2858                 break;
2859         case IORING_OP_WRITEV:
2860         case IORING_OP_WRITE_FIXED:
2861                 ret = io_write_prep(req, &iovec, &iter, true);
2862                 break;
2863         case IORING_OP_SENDMSG:
2864                 ret = io_sendmsg_prep(req, io);
2865                 break;
2866         case IORING_OP_RECVMSG:
2867                 ret = io_recvmsg_prep(req, io);
2868                 break;
2869         case IORING_OP_CONNECT:
2870                 ret = io_connect_prep(req, io);
2871                 break;
2872         case IORING_OP_TIMEOUT:
2873                 return io_timeout_prep(req, io, false);
2874         case IORING_OP_LINK_TIMEOUT:
2875                 return io_timeout_prep(req, io, true);
2876         default:
2877                 req->io = io;
2878                 return 0;
2879         }
2880
2881         if (ret < 0)
2882                 return ret;
2883
2884         req->io = io;
2885         io_req_map_io(req, ret, iovec, inline_vecs, &iter);
2886         return 0;
2887 }
2888
2889 static int io_req_defer(struct io_kiocb *req)
2890 {
2891         struct io_ring_ctx *ctx = req->ctx;
2892         struct io_async_ctx *io;
2893         int ret;
2894
2895         /* Still need defer if there is pending req in defer list. */
2896         if (!req_need_defer(req) && list_empty(&ctx->defer_list))
2897                 return 0;
2898
2899         io = kmalloc(sizeof(*io), GFP_KERNEL);
2900         if (!io)
2901                 return -EAGAIN;
2902
2903         ret = io_req_defer_prep(req, io);
2904         if (ret < 0) {
2905                 kfree(io);
2906                 return ret;
2907         }
2908
2909         spin_lock_irq(&ctx->completion_lock);
2910         if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
2911                 spin_unlock_irq(&ctx->completion_lock);
2912                 return 0;
2913         }
2914
2915         trace_io_uring_defer(ctx, req, req->user_data);
2916         list_add_tail(&req->list, &ctx->defer_list);
2917         spin_unlock_irq(&ctx->completion_lock);
2918         return -EIOCBQUEUED;
2919 }
2920
2921 __attribute__((nonnull))
2922 static int io_issue_sqe(struct io_kiocb *req, struct io_kiocb **nxt,
2923                         bool force_nonblock)
2924 {
2925         int ret, opcode;
2926         struct io_ring_ctx *ctx = req->ctx;
2927
2928         opcode = READ_ONCE(req->sqe->opcode);
2929         switch (opcode) {
2930         case IORING_OP_NOP:
2931                 ret = io_nop(req);
2932                 break;
2933         case IORING_OP_READV:
2934                 if (unlikely(req->sqe->buf_index))
2935                         return -EINVAL;
2936                 ret = io_read(req, nxt, force_nonblock);
2937                 break;
2938         case IORING_OP_WRITEV:
2939                 if (unlikely(req->sqe->buf_index))
2940                         return -EINVAL;
2941                 ret = io_write(req, nxt, force_nonblock);
2942                 break;
2943         case IORING_OP_READ_FIXED:
2944                 ret = io_read(req, nxt, force_nonblock);
2945                 break;
2946         case IORING_OP_WRITE_FIXED:
2947                 ret = io_write(req, nxt, force_nonblock);
2948                 break;
2949         case IORING_OP_FSYNC:
2950                 ret = io_fsync(req, req->sqe, nxt, force_nonblock);
2951                 break;
2952         case IORING_OP_POLL_ADD:
2953                 ret = io_poll_add(req, req->sqe, nxt);
2954                 break;
2955         case IORING_OP_POLL_REMOVE:
2956                 ret = io_poll_remove(req, req->sqe);
2957                 break;
2958         case IORING_OP_SYNC_FILE_RANGE:
2959                 ret = io_sync_file_range(req, req->sqe, nxt, force_nonblock);
2960                 break;
2961         case IORING_OP_SENDMSG:
2962                 ret = io_sendmsg(req, req->sqe, nxt, force_nonblock);
2963                 break;
2964         case IORING_OP_RECVMSG:
2965                 ret = io_recvmsg(req, req->sqe, nxt, force_nonblock);
2966                 break;
2967         case IORING_OP_TIMEOUT:
2968                 ret = io_timeout(req, req->sqe);
2969                 break;
2970         case IORING_OP_TIMEOUT_REMOVE:
2971                 ret = io_timeout_remove(req, req->sqe);
2972                 break;
2973         case IORING_OP_ACCEPT:
2974                 ret = io_accept(req, req->sqe, nxt, force_nonblock);
2975                 break;
2976         case IORING_OP_CONNECT:
2977                 ret = io_connect(req, req->sqe, nxt, force_nonblock);
2978                 break;
2979         case IORING_OP_ASYNC_CANCEL:
2980                 ret = io_async_cancel(req, req->sqe, nxt);
2981                 break;
2982         default:
2983                 ret = -EINVAL;
2984                 break;
2985         }
2986
2987         if (ret)
2988                 return ret;
2989
2990         if (ctx->flags & IORING_SETUP_IOPOLL) {
2991                 if (req->result == -EAGAIN)
2992                         return -EAGAIN;
2993
2994                 /* workqueue context doesn't hold uring_lock, grab it now */
2995                 if (req->in_async)
2996                         mutex_lock(&ctx->uring_lock);
2997                 io_iopoll_req_issued(req);
2998                 if (req->in_async)
2999                         mutex_unlock(&ctx->uring_lock);
3000         }
3001
3002         return 0;
3003 }
3004
3005 static void io_link_work_cb(struct io_wq_work **workptr)
3006 {
3007         struct io_wq_work *work = *workptr;
3008         struct io_kiocb *link = work->data;
3009
3010         io_queue_linked_timeout(link);
3011         work->func = io_wq_submit_work;
3012 }
3013
3014 static void io_wq_submit_work(struct io_wq_work **workptr)
3015 {
3016         struct io_wq_work *work = *workptr;
3017         struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3018         struct io_kiocb *nxt = NULL;
3019         int ret = 0;
3020
3021         /* Ensure we clear previously set non-block flag */
3022         req->rw.ki_flags &= ~IOCB_NOWAIT;
3023
3024         if (work->flags & IO_WQ_WORK_CANCEL)
3025                 ret = -ECANCELED;
3026
3027         if (!ret) {
3028                 req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
3029                 req->in_async = true;
3030                 do {
3031                         ret = io_issue_sqe(req, &nxt, false);
3032                         /*
3033                          * We can get EAGAIN for polled IO even though we're
3034                          * forcing a sync submission from here, since we can't
3035                          * wait for request slots on the block side.
3036                          */
3037                         if (ret != -EAGAIN)
3038                                 break;
3039                         cond_resched();
3040                 } while (1);
3041         }
3042
3043         /* drop submission reference */
3044         io_put_req(req);
3045
3046         if (ret) {
3047                 if (req->flags & REQ_F_LINK)
3048                         req->flags |= REQ_F_FAIL_LINK;
3049                 io_cqring_add_event(req, ret);
3050                 io_put_req(req);
3051         }
3052
3053         /* if a dependent link is ready, pass it back */
3054         if (!ret && nxt) {
3055                 struct io_kiocb *link;
3056
3057                 io_prep_async_work(nxt, &link);
3058                 *workptr = &nxt->work;
3059                 if (link) {
3060                         nxt->work.flags |= IO_WQ_WORK_CB;
3061                         nxt->work.func = io_link_work_cb;
3062                         nxt->work.data = link;
3063                 }
3064         }
3065 }
3066
3067 static bool io_op_needs_file(const struct io_uring_sqe *sqe)
3068 {
3069         int op = READ_ONCE(sqe->opcode);
3070
3071         switch (op) {
3072         case IORING_OP_NOP:
3073         case IORING_OP_POLL_REMOVE:
3074         case IORING_OP_TIMEOUT:
3075         case IORING_OP_TIMEOUT_REMOVE:
3076         case IORING_OP_ASYNC_CANCEL:
3077         case IORING_OP_LINK_TIMEOUT:
3078                 return false;
3079         default:
3080                 return true;
3081         }
3082 }
3083
3084 static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
3085                                               int index)
3086 {
3087         struct fixed_file_table *table;
3088
3089         table = &ctx->file_table[index >> IORING_FILE_TABLE_SHIFT];
3090         return table->files[index & IORING_FILE_TABLE_MASK];
3091 }
3092
3093 static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req)
3094 {
3095         struct io_ring_ctx *ctx = req->ctx;
3096         unsigned flags;
3097         int fd;
3098
3099         flags = READ_ONCE(req->sqe->flags);
3100         fd = READ_ONCE(req->sqe->fd);
3101
3102         if (flags & IOSQE_IO_DRAIN)
3103                 req->flags |= REQ_F_IO_DRAIN;
3104
3105         if (!io_op_needs_file(req->sqe))
3106                 return 0;
3107
3108         if (flags & IOSQE_FIXED_FILE) {
3109                 if (unlikely(!ctx->file_table ||
3110                     (unsigned) fd >= ctx->nr_user_files))
3111                         return -EBADF;
3112                 fd = array_index_nospec(fd, ctx->nr_user_files);
3113                 req->file = io_file_from_index(ctx, fd);
3114                 if (!req->file)
3115                         return -EBADF;
3116                 req->flags |= REQ_F_FIXED_FILE;
3117         } else {
3118                 if (req->needs_fixed_file)
3119                         return -EBADF;
3120                 trace_io_uring_file_get(ctx, fd);
3121                 req->file = io_file_get(state, fd);
3122                 if (unlikely(!req->file))
3123                         return -EBADF;
3124         }
3125
3126         return 0;
3127 }
3128
3129 static int io_grab_files(struct io_kiocb *req)
3130 {
3131         int ret = -EBADF;
3132         struct io_ring_ctx *ctx = req->ctx;
3133
3134         rcu_read_lock();
3135         spin_lock_irq(&ctx->inflight_lock);
3136         /*
3137          * We use the f_ops->flush() handler to ensure that we can flush
3138          * out work accessing these files if the fd is closed. Check if
3139          * the fd has changed since we started down this path, and disallow
3140          * this operation if it has.
3141          */
3142         if (fcheck(req->ring_fd) == req->ring_file) {
3143                 list_add(&req->inflight_entry, &ctx->inflight_list);
3144                 req->flags |= REQ_F_INFLIGHT;
3145                 req->work.files = current->files;
3146                 ret = 0;
3147         }
3148         spin_unlock_irq(&ctx->inflight_lock);
3149         rcu_read_unlock();
3150
3151         return ret;
3152 }
3153
3154 static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
3155 {
3156         struct io_timeout_data *data = container_of(timer,
3157                                                 struct io_timeout_data, timer);
3158         struct io_kiocb *req = data->req;
3159         struct io_ring_ctx *ctx = req->ctx;
3160         struct io_kiocb *prev = NULL;
3161         unsigned long flags;
3162
3163         spin_lock_irqsave(&ctx->completion_lock, flags);
3164
3165         /*
3166          * We don't expect the list to be empty, that will only happen if we
3167          * race with the completion of the linked work.
3168          */
3169         if (!list_empty(&req->link_list)) {
3170                 prev = list_entry(req->link_list.prev, struct io_kiocb,
3171                                   link_list);
3172                 if (refcount_inc_not_zero(&prev->refs)) {
3173                         list_del_init(&req->link_list);
3174                         prev->flags &= ~REQ_F_LINK_TIMEOUT;
3175                 } else
3176                         prev = NULL;
3177         }
3178
3179         spin_unlock_irqrestore(&ctx->completion_lock, flags);
3180
3181         if (prev) {
3182                 if (prev->flags & REQ_F_LINK)
3183                         prev->flags |= REQ_F_FAIL_LINK;
3184                 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
3185                                                 -ETIME);
3186                 io_put_req(prev);
3187         } else {
3188                 io_cqring_add_event(req, -ETIME);
3189                 io_put_req(req);
3190         }
3191         return HRTIMER_NORESTART;
3192 }
3193
3194 static void io_queue_linked_timeout(struct io_kiocb *req)
3195 {
3196         struct io_ring_ctx *ctx = req->ctx;
3197
3198         /*
3199          * If the list is now empty, then our linked request finished before
3200          * we got a chance to setup the timer
3201          */
3202         spin_lock_irq(&ctx->completion_lock);
3203         if (!list_empty(&req->link_list)) {
3204                 struct io_timeout_data *data = &req->io->timeout;
3205
3206                 data->timer.function = io_link_timeout_fn;
3207                 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
3208                                 data->mode);
3209         }
3210         spin_unlock_irq(&ctx->completion_lock);
3211
3212         /* drop submission reference */
3213         io_put_req(req);
3214 }
3215
3216 static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
3217 {
3218         struct io_kiocb *nxt;
3219
3220         if (!(req->flags & REQ_F_LINK))
3221                 return NULL;
3222
3223         nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
3224                                         link_list);
3225         if (!nxt || nxt->sqe->opcode != IORING_OP_LINK_TIMEOUT)
3226                 return NULL;
3227
3228         req->flags |= REQ_F_LINK_TIMEOUT;
3229         return nxt;
3230 }
3231
3232 static void __io_queue_sqe(struct io_kiocb *req)
3233 {
3234         struct io_kiocb *linked_timeout = io_prep_linked_timeout(req);
3235         struct io_kiocb *nxt = NULL;
3236         int ret;
3237
3238         ret = io_issue_sqe(req, &nxt, true);
3239         if (nxt)
3240                 io_queue_async_work(nxt);
3241
3242         /*
3243          * We async punt it if the file wasn't marked NOWAIT, or if the file
3244          * doesn't support non-blocking read/write attempts
3245          */
3246         if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
3247             (req->flags & REQ_F_MUST_PUNT))) {
3248                 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
3249                         ret = io_grab_files(req);
3250                         if (ret)
3251                                 goto err;
3252                 }
3253
3254                 /*
3255                  * Queued up for async execution, worker will release
3256                  * submit reference when the iocb is actually submitted.
3257                  */
3258                 io_queue_async_work(req);
3259                 return;
3260         }
3261
3262 err:
3263         /* drop submission reference */
3264         io_put_req(req);
3265
3266         if (linked_timeout) {
3267                 if (!ret)
3268                         io_queue_linked_timeout(linked_timeout);
3269                 else
3270                         io_put_req(linked_timeout);
3271         }
3272
3273         /* and drop final reference, if we failed */
3274         if (ret) {
3275                 io_cqring_add_event(req, ret);
3276                 if (req->flags & REQ_F_LINK)
3277                         req->flags |= REQ_F_FAIL_LINK;
3278                 io_put_req(req);
3279         }
3280 }
3281
3282 static void io_queue_sqe(struct io_kiocb *req)
3283 {
3284         int ret;
3285
3286         if (unlikely(req->ctx->drain_next)) {
3287                 req->flags |= REQ_F_IO_DRAIN;
3288                 req->ctx->drain_next = false;
3289         }
3290         req->ctx->drain_next = (req->flags & REQ_F_DRAIN_LINK);
3291
3292         ret = io_req_defer(req);
3293         if (ret) {
3294                 if (ret != -EIOCBQUEUED) {
3295                         io_cqring_add_event(req, ret);
3296                         if (req->flags & REQ_F_LINK)
3297                                 req->flags |= REQ_F_FAIL_LINK;
3298                         io_double_put_req(req);
3299                 }
3300         } else
3301                 __io_queue_sqe(req);
3302 }
3303
3304 static inline void io_queue_link_head(struct io_kiocb *req)
3305 {
3306         if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
3307                 io_cqring_add_event(req, -ECANCELED);
3308                 io_double_put_req(req);
3309         } else
3310                 io_queue_sqe(req);
3311 }
3312
3313
3314 #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK)
3315
3316 static bool io_submit_sqe(struct io_kiocb *req, struct io_submit_state *state,
3317                           struct io_kiocb **link)
3318 {
3319         struct io_ring_ctx *ctx = req->ctx;
3320         int ret;
3321
3322         req->user_data = req->sqe->user_data;
3323
3324         /* enforce forwards compatibility on users */
3325         if (unlikely(req->sqe->flags & ~SQE_VALID_FLAGS)) {
3326                 ret = -EINVAL;
3327                 goto err_req;
3328         }
3329
3330         ret = io_req_set_file(state, req);
3331         if (unlikely(ret)) {
3332 err_req:
3333                 io_cqring_add_event(req, ret);
3334                 io_double_put_req(req);
3335                 return false;
3336         }
3337
3338         /*
3339          * If we already have a head request, queue this one for async
3340          * submittal once the head completes. If we don't have a head but
3341          * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
3342          * submitted sync once the chain is complete. If none of those
3343          * conditions are true (normal request), then just queue it.
3344          */
3345         if (*link) {
3346                 struct io_kiocb *prev = *link;
3347                 struct io_async_ctx *io;
3348
3349                 if (req->sqe->flags & IOSQE_IO_DRAIN)
3350                         (*link)->flags |= REQ_F_DRAIN_LINK | REQ_F_IO_DRAIN;
3351
3352                 io = kmalloc(sizeof(*io), GFP_KERNEL);
3353                 if (!io) {
3354                         ret = -EAGAIN;
3355                         goto err_req;
3356                 }
3357
3358                 ret = io_req_defer_prep(req, io);
3359                 if (ret) {
3360                         kfree(io);
3361                         prev->flags |= REQ_F_FAIL_LINK;
3362                         goto err_req;
3363                 }
3364                 trace_io_uring_link(ctx, req, prev);
3365                 list_add_tail(&req->link_list, &prev->link_list);
3366         } else if (req->sqe->flags & IOSQE_IO_LINK) {
3367                 req->flags |= REQ_F_LINK;
3368
3369                 INIT_LIST_HEAD(&req->link_list);
3370                 *link = req;
3371         } else {
3372                 io_queue_sqe(req);
3373         }
3374
3375         return true;
3376 }
3377
3378 /*
3379  * Batched submission is done, ensure local IO is flushed out.
3380  */
3381 static void io_submit_state_end(struct io_submit_state *state)
3382 {
3383         blk_finish_plug(&state->plug);
3384         io_file_put(state);
3385         if (state->free_reqs)
3386                 kmem_cache_free_bulk(req_cachep, state->free_reqs,
3387                                         &state->reqs[state->cur_req]);
3388 }
3389
3390 /*
3391  * Start submission side cache.
3392  */
3393 static void io_submit_state_start(struct io_submit_state *state,
3394                                   unsigned int max_ios)
3395 {
3396         blk_start_plug(&state->plug);
3397         state->free_reqs = 0;
3398         state->file = NULL;
3399         state->ios_left = max_ios;
3400 }
3401
3402 static void io_commit_sqring(struct io_ring_ctx *ctx)
3403 {
3404         struct io_rings *rings = ctx->rings;
3405
3406         if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
3407                 /*
3408                  * Ensure any loads from the SQEs are done at this point,
3409                  * since once we write the new head, the application could
3410                  * write new data to them.
3411                  */
3412                 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
3413         }
3414 }
3415
3416 /*
3417  * Fetch an sqe, if one is available. Note that s->sqe will point to memory
3418  * that is mapped by userspace. This means that care needs to be taken to
3419  * ensure that reads are stable, as we cannot rely on userspace always
3420  * being a good citizen. If members of the sqe are validated and then later
3421  * used, it's important that those reads are done through READ_ONCE() to
3422  * prevent a re-load down the line.
3423  */
3424 static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req)
3425 {
3426         struct io_rings *rings = ctx->rings;
3427         u32 *sq_array = ctx->sq_array;
3428         unsigned head;
3429
3430         /*
3431          * The cached sq head (or cq tail) serves two purposes:
3432          *
3433          * 1) allows us to batch the cost of updating the user visible
3434          *    head updates.
3435          * 2) allows the kernel side to track the head on its own, even
3436          *    though the application is the one updating it.
3437          */
3438         head = ctx->cached_sq_head;
3439         /* make sure SQ entry isn't read before tail */
3440         if (unlikely(head == smp_load_acquire(&rings->sq.tail)))
3441                 return false;
3442
3443         head = READ_ONCE(sq_array[head & ctx->sq_mask]);
3444         if (likely(head < ctx->sq_entries)) {
3445                 /*
3446                  * All io need record the previous position, if LINK vs DARIN,
3447                  * it can be used to mark the position of the first IO in the
3448                  * link list.
3449                  */
3450                 req->sequence = ctx->cached_sq_head;
3451                 req->sqe = &ctx->sq_sqes[head];
3452                 ctx->cached_sq_head++;
3453                 return true;
3454         }
3455
3456         /* drop invalid entries */
3457         ctx->cached_sq_head++;
3458         ctx->cached_sq_dropped++;
3459         WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
3460         return false;
3461 }
3462
3463 static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
3464                           struct file *ring_file, int ring_fd,
3465                           struct mm_struct **mm, bool async)
3466 {
3467         struct io_submit_state state, *statep = NULL;
3468         struct io_kiocb *link = NULL;
3469         int i, submitted = 0;
3470         bool mm_fault = false;
3471
3472         /* if we have a backlog and couldn't flush it all, return BUSY */
3473         if (!list_empty(&ctx->cq_overflow_list) &&
3474             !io_cqring_overflow_flush(ctx, false))
3475                 return -EBUSY;
3476
3477         if (nr > IO_PLUG_THRESHOLD) {
3478                 io_submit_state_start(&state, nr);
3479                 statep = &state;
3480         }
3481
3482         for (i = 0; i < nr; i++) {
3483                 struct io_kiocb *req;
3484                 unsigned int sqe_flags;
3485
3486                 req = io_get_req(ctx, statep);
3487                 if (unlikely(!req)) {
3488                         if (!submitted)
3489                                 submitted = -EAGAIN;
3490                         break;
3491                 }
3492                 if (!io_get_sqring(ctx, req)) {
3493                         __io_free_req(req);
3494                         break;
3495                 }
3496
3497                 if (io_sqe_needs_user(req->sqe) && !*mm) {
3498                         mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
3499                         if (!mm_fault) {
3500                                 use_mm(ctx->sqo_mm);
3501                                 *mm = ctx->sqo_mm;
3502                         }
3503                 }
3504
3505                 submitted++;
3506                 sqe_flags = req->sqe->flags;
3507
3508                 req->ring_file = ring_file;
3509                 req->ring_fd = ring_fd;
3510                 req->has_user = *mm != NULL;
3511                 req->in_async = async;
3512                 req->needs_fixed_file = async;
3513                 trace_io_uring_submit_sqe(ctx, req->sqe->user_data,
3514                                           true, async);
3515                 if (!io_submit_sqe(req, statep, &link))
3516                         break;
3517                 /*
3518                  * If previous wasn't linked and we have a linked command,
3519                  * that's the end of the chain. Submit the previous link.
3520                  */
3521                 if (!(sqe_flags & IOSQE_IO_LINK) && link) {
3522                         io_queue_link_head(link);
3523                         link = NULL;
3524                 }
3525         }
3526
3527         if (link)
3528                 io_queue_link_head(link);
3529         if (statep)
3530                 io_submit_state_end(&state);
3531
3532          /* Commit SQ ring head once we've consumed and submitted all SQEs */
3533         io_commit_sqring(ctx);
3534
3535         return submitted;
3536 }
3537
3538 static int io_sq_thread(void *data)
3539 {
3540         struct io_ring_ctx *ctx = data;
3541         struct mm_struct *cur_mm = NULL;
3542         const struct cred *old_cred;
3543         mm_segment_t old_fs;
3544         DEFINE_WAIT(wait);
3545         unsigned inflight;
3546         unsigned long timeout;
3547         int ret;
3548
3549         complete(&ctx->completions[1]);
3550
3551         old_fs = get_fs();
3552         set_fs(USER_DS);
3553         old_cred = override_creds(ctx->creds);
3554
3555         ret = timeout = inflight = 0;
3556         while (!kthread_should_park()) {
3557                 unsigned int to_submit;
3558
3559                 if (inflight) {
3560                         unsigned nr_events = 0;
3561
3562                         if (ctx->flags & IORING_SETUP_IOPOLL) {
3563                                 /*
3564                                  * inflight is the count of the maximum possible
3565                                  * entries we submitted, but it can be smaller
3566                                  * if we dropped some of them. If we don't have
3567                                  * poll entries available, then we know that we
3568                                  * have nothing left to poll for. Reset the
3569                                  * inflight count to zero in that case.
3570                                  */
3571                                 mutex_lock(&ctx->uring_lock);
3572                                 if (!list_empty(&ctx->poll_list))
3573                                         __io_iopoll_check(ctx, &nr_events, 0);
3574                                 else
3575                                         inflight = 0;
3576                                 mutex_unlock(&ctx->uring_lock);
3577                         } else {
3578                                 /*
3579                                  * Normal IO, just pretend everything completed.
3580                                  * We don't have to poll completions for that.
3581                                  */
3582                                 nr_events = inflight;
3583                         }
3584
3585                         inflight -= nr_events;
3586                         if (!inflight)
3587                                 timeout = jiffies + ctx->sq_thread_idle;
3588                 }
3589
3590                 to_submit = io_sqring_entries(ctx);
3591
3592                 /*
3593                  * If submit got -EBUSY, flag us as needing the application
3594                  * to enter the kernel to reap and flush events.
3595                  */
3596                 if (!to_submit || ret == -EBUSY) {
3597                         /*
3598                          * We're polling. If we're within the defined idle
3599                          * period, then let us spin without work before going
3600                          * to sleep. The exception is if we got EBUSY doing
3601                          * more IO, we should wait for the application to
3602                          * reap events and wake us up.
3603                          */
3604                         if (inflight ||
3605                             (!time_after(jiffies, timeout) && ret != -EBUSY)) {
3606                                 cond_resched();
3607                                 continue;
3608                         }
3609
3610                         /*
3611                          * Drop cur_mm before scheduling, we can't hold it for
3612                          * long periods (or over schedule()). Do this before
3613                          * adding ourselves to the waitqueue, as the unuse/drop
3614                          * may sleep.
3615                          */
3616                         if (cur_mm) {
3617                                 unuse_mm(cur_mm);
3618                                 mmput(cur_mm);
3619                                 cur_mm = NULL;
3620                         }
3621
3622                         prepare_to_wait(&ctx->sqo_wait, &wait,
3623                                                 TASK_INTERRUPTIBLE);
3624
3625                         /* Tell userspace we may need a wakeup call */
3626                         ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
3627                         /* make sure to read SQ tail after writing flags */
3628                         smp_mb();
3629
3630                         to_submit = io_sqring_entries(ctx);
3631                         if (!to_submit || ret == -EBUSY) {
3632                                 if (kthread_should_park()) {
3633                                         finish_wait(&ctx->sqo_wait, &wait);
3634                                         break;
3635                                 }
3636                                 if (signal_pending(current))
3637                                         flush_signals(current);
3638                                 schedule();
3639                                 finish_wait(&ctx->sqo_wait, &wait);
3640
3641                                 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
3642                                 continue;
3643                         }
3644                         finish_wait(&ctx->sqo_wait, &wait);
3645
3646                         ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
3647                 }
3648
3649                 to_submit = min(to_submit, ctx->sq_entries);
3650                 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
3651                 if (ret > 0)
3652                         inflight += ret;
3653         }
3654
3655         set_fs(old_fs);
3656         if (cur_mm) {
3657                 unuse_mm(cur_mm);
3658                 mmput(cur_mm);
3659         }
3660         revert_creds(old_cred);
3661
3662         kthread_parkme();
3663
3664         return 0;
3665 }
3666
3667 struct io_wait_queue {
3668         struct wait_queue_entry wq;
3669         struct io_ring_ctx *ctx;
3670         unsigned to_wait;
3671         unsigned nr_timeouts;
3672 };
3673
3674 static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
3675 {
3676         struct io_ring_ctx *ctx = iowq->ctx;
3677
3678         /*
3679          * Wake up if we have enough events, or if a timeout occured since we
3680          * started waiting. For timeouts, we always want to return to userspace,
3681          * regardless of event count.
3682          */
3683         return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
3684                         atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
3685 }
3686
3687 static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
3688                             int wake_flags, void *key)
3689 {
3690         struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
3691                                                         wq);
3692
3693         /* use noflush == true, as we can't safely rely on locking context */
3694         if (!io_should_wake(iowq, true))
3695                 return -1;
3696
3697         return autoremove_wake_function(curr, mode, wake_flags, key);
3698 }
3699
3700 /*
3701  * Wait until events become available, if we don't already have some. The
3702  * application must reap them itself, as they reside on the shared cq ring.
3703  */
3704 static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
3705                           const sigset_t __user *sig, size_t sigsz)
3706 {
3707         struct io_wait_queue iowq = {
3708                 .wq = {
3709                         .private        = current,
3710                         .func           = io_wake_function,
3711                         .entry          = LIST_HEAD_INIT(iowq.wq.entry),
3712                 },
3713                 .ctx            = ctx,
3714                 .to_wait        = min_events,
3715         };
3716         struct io_rings *rings = ctx->rings;
3717         int ret = 0;
3718
3719         if (io_cqring_events(ctx, false) >= min_events)
3720                 return 0;
3721
3722         if (sig) {
3723 #ifdef CONFIG_COMPAT
3724                 if (in_compat_syscall())
3725                         ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
3726                                                       sigsz);
3727                 else
3728 #endif
3729                         ret = set_user_sigmask(sig, sigsz);
3730
3731                 if (ret)
3732                         return ret;
3733         }
3734
3735         iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
3736         trace_io_uring_cqring_wait(ctx, min_events);
3737         do {
3738                 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
3739                                                 TASK_INTERRUPTIBLE);
3740                 if (io_should_wake(&iowq, false))
3741                         break;
3742                 schedule();
3743                 if (signal_pending(current)) {
3744                         ret = -EINTR;
3745                         break;
3746                 }
3747         } while (1);
3748         finish_wait(&ctx->wait, &iowq.wq);
3749
3750         restore_saved_sigmask_unless(ret == -EINTR);
3751
3752         return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
3753 }
3754
3755 static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
3756 {
3757 #if defined(CONFIG_UNIX)
3758         if (ctx->ring_sock) {
3759                 struct sock *sock = ctx->ring_sock->sk;
3760                 struct sk_buff *skb;
3761
3762                 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
3763                         kfree_skb(skb);
3764         }
3765 #else
3766         int i;
3767
3768         for (i = 0; i < ctx->nr_user_files; i++) {
3769                 struct file *file;
3770
3771                 file = io_file_from_index(ctx, i);
3772                 if (file)
3773                         fput(file);
3774         }
3775 #endif
3776 }
3777
3778 static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
3779 {
3780         unsigned nr_tables, i;
3781
3782         if (!ctx->file_table)
3783                 return -ENXIO;
3784
3785         __io_sqe_files_unregister(ctx);
3786         nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
3787         for (i = 0; i < nr_tables; i++)
3788                 kfree(ctx->file_table[i].files);
3789         kfree(ctx->file_table);
3790         ctx->file_table = NULL;
3791         ctx->nr_user_files = 0;
3792         return 0;
3793 }
3794
3795 static void io_sq_thread_stop(struct io_ring_ctx *ctx)
3796 {
3797         if (ctx->sqo_thread) {
3798                 wait_for_completion(&ctx->completions[1]);
3799                 /*
3800                  * The park is a bit of a work-around, without it we get
3801                  * warning spews on shutdown with SQPOLL set and affinity
3802                  * set to a single CPU.
3803                  */
3804                 kthread_park(ctx->sqo_thread);
3805                 kthread_stop(ctx->sqo_thread);
3806                 ctx->sqo_thread = NULL;
3807         }
3808 }
3809
3810 static void io_finish_async(struct io_ring_ctx *ctx)
3811 {
3812         io_sq_thread_stop(ctx);
3813
3814         if (ctx->io_wq) {
3815                 io_wq_destroy(ctx->io_wq);
3816                 ctx->io_wq = NULL;
3817         }
3818 }
3819
3820 #if defined(CONFIG_UNIX)
3821 static void io_destruct_skb(struct sk_buff *skb)
3822 {
3823         struct io_ring_ctx *ctx = skb->sk->sk_user_data;
3824
3825         if (ctx->io_wq)
3826                 io_wq_flush(ctx->io_wq);
3827
3828         unix_destruct_scm(skb);
3829 }
3830
3831 /*
3832  * Ensure the UNIX gc is aware of our file set, so we are certain that
3833  * the io_uring can be safely unregistered on process exit, even if we have
3834  * loops in the file referencing.
3835  */
3836 static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
3837 {
3838         struct sock *sk = ctx->ring_sock->sk;
3839         struct scm_fp_list *fpl;
3840         struct sk_buff *skb;
3841         int i, nr_files;
3842
3843         if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
3844                 unsigned long inflight = ctx->user->unix_inflight + nr;
3845
3846                 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
3847                         return -EMFILE;
3848         }
3849
3850         fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
3851         if (!fpl)
3852                 return -ENOMEM;
3853
3854         skb = alloc_skb(0, GFP_KERNEL);
3855         if (!skb) {
3856                 kfree(fpl);
3857                 return -ENOMEM;
3858         }
3859
3860         skb->sk = sk;
3861
3862         nr_files = 0;
3863         fpl->user = get_uid(ctx->user);
3864         for (i = 0; i < nr; i++) {
3865                 struct file *file = io_file_from_index(ctx, i + offset);
3866
3867                 if (!file)
3868                         continue;
3869                 fpl->fp[nr_files] = get_file(file);
3870                 unix_inflight(fpl->user, fpl->fp[nr_files]);
3871                 nr_files++;
3872         }
3873
3874         if (nr_files) {
3875                 fpl->max = SCM_MAX_FD;
3876                 fpl->count = nr_files;
3877                 UNIXCB(skb).fp = fpl;
3878                 skb->destructor = io_destruct_skb;
3879                 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
3880                 skb_queue_head(&sk->sk_receive_queue, skb);
3881
3882                 for (i = 0; i < nr_files; i++)
3883                         fput(fpl->fp[i]);
3884         } else {
3885                 kfree_skb(skb);
3886                 kfree(fpl);
3887         }
3888
3889         return 0;
3890 }
3891
3892 /*
3893  * If UNIX sockets are enabled, fd passing can cause a reference cycle which
3894  * causes regular reference counting to break down. We rely on the UNIX
3895  * garbage collection to take care of this problem for us.
3896  */
3897 static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3898 {
3899         unsigned left, total;
3900         int ret = 0;
3901
3902         total = 0;
3903         left = ctx->nr_user_files;
3904         while (left) {
3905                 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
3906
3907                 ret = __io_sqe_files_scm(ctx, this_files, total);
3908                 if (ret)
3909                         break;
3910                 left -= this_files;
3911                 total += this_files;
3912         }
3913
3914         if (!ret)
3915                 return 0;
3916
3917         while (total < ctx->nr_user_files) {
3918                 struct file *file = io_file_from_index(ctx, total);
3919
3920                 if (file)
3921                         fput(file);
3922                 total++;
3923         }
3924
3925         return ret;
3926 }
3927 #else
3928 static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3929 {
3930         return 0;
3931 }
3932 #endif
3933
3934 static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
3935                                     unsigned nr_files)
3936 {
3937         int i;
3938
3939         for (i = 0; i < nr_tables; i++) {
3940                 struct fixed_file_table *table = &ctx->file_table[i];
3941                 unsigned this_files;
3942
3943                 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
3944                 table->files = kcalloc(this_files, sizeof(struct file *),
3945                                         GFP_KERNEL);
3946                 if (!table->files)
3947                         break;
3948                 nr_files -= this_files;
3949         }
3950
3951         if (i == nr_tables)
3952                 return 0;
3953
3954         for (i = 0; i < nr_tables; i++) {
3955                 struct fixed_file_table *table = &ctx->file_table[i];
3956                 kfree(table->files);
3957         }
3958         return 1;
3959 }
3960
3961 static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
3962                                  unsigned nr_args)
3963 {
3964         __s32 __user *fds = (__s32 __user *) arg;
3965         unsigned nr_tables;
3966         int fd, ret = 0;
3967         unsigned i;
3968
3969         if (ctx->file_table)
3970                 return -EBUSY;
3971         if (!nr_args)
3972                 return -EINVAL;
3973         if (nr_args > IORING_MAX_FIXED_FILES)
3974                 return -EMFILE;
3975
3976         nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
3977         ctx->file_table = kcalloc(nr_tables, sizeof(struct fixed_file_table),
3978                                         GFP_KERNEL);
3979         if (!ctx->file_table)
3980                 return -ENOMEM;
3981
3982         if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
3983                 kfree(ctx->file_table);
3984                 ctx->file_table = NULL;
3985                 return -ENOMEM;
3986         }
3987
3988         for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
3989                 struct fixed_file_table *table;
3990                 unsigned index;
3991
3992                 ret = -EFAULT;
3993                 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
3994                         break;
3995                 /* allow sparse sets */
3996                 if (fd == -1) {
3997                         ret = 0;
3998                         continue;
3999                 }
4000
4001                 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
4002                 index = i & IORING_FILE_TABLE_MASK;
4003                 table->files[index] = fget(fd);
4004
4005                 ret = -EBADF;
4006                 if (!table->files[index])
4007                         break;
4008                 /*
4009                  * Don't allow io_uring instances to be registered. If UNIX
4010                  * isn't enabled, then this causes a reference cycle and this
4011                  * instance can never get freed. If UNIX is enabled we'll
4012                  * handle it just fine, but there's still no point in allowing
4013                  * a ring fd as it doesn't support regular read/write anyway.
4014                  */
4015                 if (table->files[index]->f_op == &io_uring_fops) {
4016                         fput(table->files[index]);
4017                         break;
4018                 }
4019                 ret = 0;
4020         }
4021
4022         if (ret) {
4023                 for (i = 0; i < ctx->nr_user_files; i++) {
4024                         struct file *file;
4025
4026                         file = io_file_from_index(ctx, i);
4027                         if (file)
4028                                 fput(file);
4029                 }
4030                 for (i = 0; i < nr_tables; i++)
4031                         kfree(ctx->file_table[i].files);
4032
4033                 kfree(ctx->file_table);
4034                 ctx->file_table = NULL;
4035                 ctx->nr_user_files = 0;
4036                 return ret;
4037         }
4038
4039         ret = io_sqe_files_scm(ctx);
4040         if (ret)
4041                 io_sqe_files_unregister(ctx);
4042
4043         return ret;
4044 }
4045
4046 static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index)
4047 {
4048 #if defined(CONFIG_UNIX)
4049         struct file *file = io_file_from_index(ctx, index);
4050         struct sock *sock = ctx->ring_sock->sk;
4051         struct sk_buff_head list, *head = &sock->sk_receive_queue;
4052         struct sk_buff *skb;
4053         int i;
4054
4055         __skb_queue_head_init(&list);
4056
4057         /*
4058          * Find the skb that holds this file in its SCM_RIGHTS. When found,
4059          * remove this entry and rearrange the file array.
4060          */
4061         skb = skb_dequeue(head);
4062         while (skb) {
4063                 struct scm_fp_list *fp;
4064
4065                 fp = UNIXCB(skb).fp;
4066                 for (i = 0; i < fp->count; i++) {
4067                         int left;
4068
4069                         if (fp->fp[i] != file)
4070                                 continue;
4071
4072                         unix_notinflight(fp->user, fp->fp[i]);
4073                         left = fp->count - 1 - i;
4074                         if (left) {
4075                                 memmove(&fp->fp[i], &fp->fp[i + 1],
4076                                                 left * sizeof(struct file *));
4077                         }
4078                         fp->count--;
4079                         if (!fp->count) {
4080                                 kfree_skb(skb);
4081                                 skb = NULL;
4082                         } else {
4083                                 __skb_queue_tail(&list, skb);
4084                         }
4085                         fput(file);
4086                         file = NULL;
4087                         break;
4088                 }
4089
4090                 if (!file)
4091                         break;
4092
4093                 __skb_queue_tail(&list, skb);
4094
4095                 skb = skb_dequeue(head);
4096         }
4097
4098         if (skb_peek(&list)) {
4099                 spin_lock_irq(&head->lock);
4100                 while ((skb = __skb_dequeue(&list)) != NULL)
4101                         __skb_queue_tail(head, skb);
4102                 spin_unlock_irq(&head->lock);
4103         }
4104 #else
4105         fput(io_file_from_index(ctx, index));
4106 #endif
4107 }
4108
4109 static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
4110                                 int index)
4111 {
4112 #if defined(CONFIG_UNIX)
4113         struct sock *sock = ctx->ring_sock->sk;
4114         struct sk_buff_head *head = &sock->sk_receive_queue;
4115         struct sk_buff *skb;
4116
4117         /*
4118          * See if we can merge this file into an existing skb SCM_RIGHTS
4119          * file set. If there's no room, fall back to allocating a new skb
4120          * and filling it in.
4121          */
4122         spin_lock_irq(&head->lock);
4123         skb = skb_peek(head);
4124         if (skb) {
4125                 struct scm_fp_list *fpl = UNIXCB(skb).fp;
4126
4127                 if (fpl->count < SCM_MAX_FD) {
4128                         __skb_unlink(skb, head);
4129                         spin_unlock_irq(&head->lock);
4130                         fpl->fp[fpl->count] = get_file(file);
4131                         unix_inflight(fpl->user, fpl->fp[fpl->count]);
4132                         fpl->count++;
4133                         spin_lock_irq(&head->lock);
4134                         __skb_queue_head(head, skb);
4135                 } else {
4136                         skb = NULL;
4137                 }
4138         }
4139         spin_unlock_irq(&head->lock);
4140
4141         if (skb) {
4142                 fput(file);
4143                 return 0;
4144         }
4145
4146         return __io_sqe_files_scm(ctx, 1, index);
4147 #else
4148         return 0;
4149 #endif
4150 }
4151
4152 static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
4153                                unsigned nr_args)
4154 {
4155         struct io_uring_files_update up;
4156         __s32 __user *fds;
4157         int fd, i, err;
4158         __u32 done;
4159
4160         if (!ctx->file_table)
4161                 return -ENXIO;
4162         if (!nr_args)
4163                 return -EINVAL;
4164         if (copy_from_user(&up, arg, sizeof(up)))
4165                 return -EFAULT;
4166         if (check_add_overflow(up.offset, nr_args, &done))
4167                 return -EOVERFLOW;
4168         if (done > ctx->nr_user_files)
4169                 return -EINVAL;
4170
4171         done = 0;
4172         fds = (__s32 __user *) up.fds;
4173         while (nr_args) {
4174                 struct fixed_file_table *table;
4175                 unsigned index;
4176
4177                 err = 0;
4178                 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
4179                         err = -EFAULT;
4180                         break;
4181                 }
4182                 i = array_index_nospec(up.offset, ctx->nr_user_files);
4183                 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
4184                 index = i & IORING_FILE_TABLE_MASK;
4185                 if (table->files[index]) {
4186                         io_sqe_file_unregister(ctx, i);
4187                         table->files[index] = NULL;
4188                 }
4189                 if (fd != -1) {
4190                         struct file *file;
4191
4192                         file = fget(fd);
4193                         if (!file) {
4194                                 err = -EBADF;
4195                                 break;
4196                         }
4197                         /*
4198                          * Don't allow io_uring instances to be registered. If
4199                          * UNIX isn't enabled, then this causes a reference
4200                          * cycle and this instance can never get freed. If UNIX
4201                          * is enabled we'll handle it just fine, but there's
4202                          * still no point in allowing a ring fd as it doesn't
4203                          * support regular read/write anyway.
4204                          */
4205                         if (file->f_op == &io_uring_fops) {
4206                                 fput(file);
4207                                 err = -EBADF;
4208                                 break;
4209                         }
4210                         table->files[index] = file;
4211                         err = io_sqe_file_register(ctx, file, i);
4212                         if (err)
4213                                 break;
4214                 }
4215                 nr_args--;
4216                 done++;
4217                 up.offset++;
4218         }
4219
4220         return done ? done : err;
4221 }
4222
4223 static void io_put_work(struct io_wq_work *work)
4224 {
4225         struct io_kiocb *req = container_of(work, struct io_kiocb, work);
4226
4227         io_put_req(req);
4228 }
4229
4230 static void io_get_work(struct io_wq_work *work)
4231 {
4232         struct io_kiocb *req = container_of(work, struct io_kiocb, work);
4233
4234         refcount_inc(&req->refs);
4235 }
4236
4237 static int io_sq_offload_start(struct io_ring_ctx *ctx,
4238                                struct io_uring_params *p)
4239 {
4240         struct io_wq_data data;
4241         unsigned concurrency;
4242         int ret;
4243
4244         init_waitqueue_head(&ctx->sqo_wait);
4245         mmgrab(current->mm);
4246         ctx->sqo_mm = current->mm;
4247
4248         if (ctx->flags & IORING_SETUP_SQPOLL) {
4249                 ret = -EPERM;
4250                 if (!capable(CAP_SYS_ADMIN))
4251                         goto err;
4252
4253                 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
4254                 if (!ctx->sq_thread_idle)
4255                         ctx->sq_thread_idle = HZ;
4256
4257                 if (p->flags & IORING_SETUP_SQ_AFF) {
4258                         int cpu = p->sq_thread_cpu;
4259
4260                         ret = -EINVAL;
4261                         if (cpu >= nr_cpu_ids)
4262                                 goto err;
4263                         if (!cpu_online(cpu))
4264                                 goto err;
4265
4266                         ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
4267                                                         ctx, cpu,
4268                                                         "io_uring-sq");
4269                 } else {
4270                         ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
4271                                                         "io_uring-sq");
4272                 }
4273                 if (IS_ERR(ctx->sqo_thread)) {
4274                         ret = PTR_ERR(ctx->sqo_thread);
4275                         ctx->sqo_thread = NULL;
4276                         goto err;
4277                 }
4278                 wake_up_process(ctx->sqo_thread);
4279         } else if (p->flags & IORING_SETUP_SQ_AFF) {
4280                 /* Can't have SQ_AFF without SQPOLL */
4281                 ret = -EINVAL;
4282                 goto err;
4283         }
4284
4285         data.mm = ctx->sqo_mm;
4286         data.user = ctx->user;
4287         data.creds = ctx->creds;
4288         data.get_work = io_get_work;
4289         data.put_work = io_put_work;
4290
4291         /* Do QD, or 4 * CPUS, whatever is smallest */
4292         concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
4293         ctx->io_wq = io_wq_create(concurrency, &data);
4294         if (IS_ERR(ctx->io_wq)) {
4295                 ret = PTR_ERR(ctx->io_wq);
4296                 ctx->io_wq = NULL;
4297                 goto err;
4298         }
4299
4300         return 0;
4301 err:
4302         io_finish_async(ctx);
4303         mmdrop(ctx->sqo_mm);
4304         ctx->sqo_mm = NULL;
4305         return ret;
4306 }
4307
4308 static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
4309 {
4310         atomic_long_sub(nr_pages, &user->locked_vm);
4311 }
4312
4313 static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
4314 {
4315         unsigned long page_limit, cur_pages, new_pages;
4316
4317         /* Don't allow more pages than we can safely lock */
4318         page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
4319
4320         do {
4321                 cur_pages = atomic_long_read(&user->locked_vm);
4322                 new_pages = cur_pages + nr_pages;
4323                 if (new_pages > page_limit)
4324                         return -ENOMEM;
4325         } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
4326                                         new_pages) != cur_pages);
4327
4328         return 0;
4329 }
4330
4331 static void io_mem_free(void *ptr)
4332 {
4333         struct page *page;
4334
4335         if (!ptr)
4336                 return;
4337
4338         page = virt_to_head_page(ptr);
4339         if (put_page_testzero(page))
4340                 free_compound_page(page);
4341 }
4342
4343 static void *io_mem_alloc(size_t size)
4344 {
4345         gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
4346                                 __GFP_NORETRY;
4347
4348         return (void *) __get_free_pages(gfp_flags, get_order(size));
4349 }
4350
4351 static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
4352                                 size_t *sq_offset)
4353 {
4354         struct io_rings *rings;
4355         size_t off, sq_array_size;
4356
4357         off = struct_size(rings, cqes, cq_entries);
4358         if (off == SIZE_MAX)
4359                 return SIZE_MAX;
4360
4361 #ifdef CONFIG_SMP
4362         off = ALIGN(off, SMP_CACHE_BYTES);
4363         if (off == 0)
4364                 return SIZE_MAX;
4365 #endif
4366
4367         sq_array_size = array_size(sizeof(u32), sq_entries);
4368         if (sq_array_size == SIZE_MAX)
4369                 return SIZE_MAX;
4370
4371         if (check_add_overflow(off, sq_array_size, &off))
4372                 return SIZE_MAX;
4373
4374         if (sq_offset)
4375                 *sq_offset = off;
4376
4377         return off;
4378 }
4379
4380 static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
4381 {
4382         size_t pages;
4383
4384         pages = (size_t)1 << get_order(
4385                 rings_size(sq_entries, cq_entries, NULL));
4386         pages += (size_t)1 << get_order(
4387                 array_size(sizeof(struct io_uring_sqe), sq_entries));
4388
4389         return pages;
4390 }
4391
4392 static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
4393 {
4394         int i, j;
4395
4396         if (!ctx->user_bufs)
4397                 return -ENXIO;
4398
4399         for (i = 0; i < ctx->nr_user_bufs; i++) {
4400                 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4401
4402                 for (j = 0; j < imu->nr_bvecs; j++)
4403                         put_user_page(imu->bvec[j].bv_page);
4404
4405                 if (ctx->account_mem)
4406                         io_unaccount_mem(ctx->user, imu->nr_bvecs);
4407                 kvfree(imu->bvec);
4408                 imu->nr_bvecs = 0;
4409         }
4410
4411         kfree(ctx->user_bufs);
4412         ctx->user_bufs = NULL;
4413         ctx->nr_user_bufs = 0;
4414         return 0;
4415 }
4416
4417 static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
4418                        void __user *arg, unsigned index)
4419 {
4420         struct iovec __user *src;
4421
4422 #ifdef CONFIG_COMPAT
4423         if (ctx->compat) {
4424                 struct compat_iovec __user *ciovs;
4425                 struct compat_iovec ciov;
4426
4427                 ciovs = (struct compat_iovec __user *) arg;
4428                 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
4429                         return -EFAULT;
4430
4431                 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
4432                 dst->iov_len = ciov.iov_len;
4433                 return 0;
4434         }
4435 #endif
4436         src = (struct iovec __user *) arg;
4437         if (copy_from_user(dst, &src[index], sizeof(*dst)))
4438                 return -EFAULT;
4439         return 0;
4440 }
4441
4442 static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
4443                                   unsigned nr_args)
4444 {
4445         struct vm_area_struct **vmas = NULL;
4446         struct page **pages = NULL;
4447         int i, j, got_pages = 0;
4448         int ret = -EINVAL;
4449
4450         if (ctx->user_bufs)
4451                 return -EBUSY;
4452         if (!nr_args || nr_args > UIO_MAXIOV)
4453                 return -EINVAL;
4454
4455         ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
4456                                         GFP_KERNEL);
4457         if (!ctx->user_bufs)
4458                 return -ENOMEM;
4459
4460         for (i = 0; i < nr_args; i++) {
4461                 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4462                 unsigned long off, start, end, ubuf;
4463                 int pret, nr_pages;
4464                 struct iovec iov;
4465                 size_t size;
4466
4467                 ret = io_copy_iov(ctx, &iov, arg, i);
4468                 if (ret)
4469                         goto err;
4470
4471                 /*
4472                  * Don't impose further limits on the size and buffer
4473                  * constraints here, we'll -EINVAL later when IO is
4474                  * submitted if they are wrong.
4475                  */
4476                 ret = -EFAULT;
4477                 if (!iov.iov_base || !iov.iov_len)
4478                         goto err;
4479
4480                 /* arbitrary limit, but we need something */
4481                 if (iov.iov_len > SZ_1G)
4482                         goto err;
4483
4484                 ubuf = (unsigned long) iov.iov_base;
4485                 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
4486                 start = ubuf >> PAGE_SHIFT;
4487                 nr_pages = end - start;
4488
4489                 if (ctx->account_mem) {
4490                         ret = io_account_mem(ctx->user, nr_pages);
4491                         if (ret)
4492                                 goto err;
4493                 }
4494
4495                 ret = 0;
4496                 if (!pages || nr_pages > got_pages) {
4497                         kfree(vmas);
4498                         kfree(pages);
4499                         pages = kvmalloc_array(nr_pages, sizeof(struct page *),
4500                                                 GFP_KERNEL);
4501                         vmas = kvmalloc_array(nr_pages,
4502                                         sizeof(struct vm_area_struct *),
4503                                         GFP_KERNEL);
4504                         if (!pages || !vmas) {
4505                                 ret = -ENOMEM;
4506                                 if (ctx->account_mem)
4507                                         io_unaccount_mem(ctx->user, nr_pages);
4508                                 goto err;
4509                         }
4510                         got_pages = nr_pages;
4511                 }
4512
4513                 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
4514                                                 GFP_KERNEL);
4515                 ret = -ENOMEM;
4516                 if (!imu->bvec) {
4517                         if (ctx->account_mem)
4518                                 io_unaccount_mem(ctx->user, nr_pages);
4519                         goto err;
4520                 }
4521
4522                 ret = 0;
4523                 down_read(&current->mm->mmap_sem);
4524                 pret = get_user_pages(ubuf, nr_pages,
4525                                       FOLL_WRITE | FOLL_LONGTERM,
4526                                       pages, vmas);
4527                 if (pret == nr_pages) {
4528                         /* don't support file backed memory */
4529                         for (j = 0; j < nr_pages; j++) {
4530                                 struct vm_area_struct *vma = vmas[j];
4531
4532                                 if (vma->vm_file &&
4533                                     !is_file_hugepages(vma->vm_file)) {
4534                                         ret = -EOPNOTSUPP;
4535                                         break;
4536                                 }
4537                         }
4538                 } else {
4539                         ret = pret < 0 ? pret : -EFAULT;
4540                 }
4541                 up_read(&current->mm->mmap_sem);
4542                 if (ret) {
4543                         /*
4544                          * if we did partial map, or found file backed vmas,
4545                          * release any pages we did get
4546                          */
4547                         if (pret > 0)
4548                                 put_user_pages(pages, pret);
4549                         if (ctx->account_mem)
4550                                 io_unaccount_mem(ctx->user, nr_pages);
4551                         kvfree(imu->bvec);
4552                         goto err;
4553                 }
4554
4555                 off = ubuf & ~PAGE_MASK;
4556                 size = iov.iov_len;
4557                 for (j = 0; j < nr_pages; j++) {
4558                         size_t vec_len;
4559
4560                         vec_len = min_t(size_t, size, PAGE_SIZE - off);
4561                         imu->bvec[j].bv_page = pages[j];
4562                         imu->bvec[j].bv_len = vec_len;
4563                         imu->bvec[j].bv_offset = off;
4564                         off = 0;
4565                         size -= vec_len;
4566                 }
4567                 /* store original address for later verification */
4568                 imu->ubuf = ubuf;
4569                 imu->len = iov.iov_len;
4570                 imu->nr_bvecs = nr_pages;
4571
4572                 ctx->nr_user_bufs++;
4573         }
4574         kvfree(pages);
4575         kvfree(vmas);
4576         return 0;
4577 err:
4578         kvfree(pages);
4579         kvfree(vmas);
4580         io_sqe_buffer_unregister(ctx);
4581         return ret;
4582 }
4583
4584 static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
4585 {
4586         __s32 __user *fds = arg;
4587         int fd;
4588
4589         if (ctx->cq_ev_fd)
4590                 return -EBUSY;
4591
4592         if (copy_from_user(&fd, fds, sizeof(*fds)))
4593                 return -EFAULT;
4594
4595         ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
4596         if (IS_ERR(ctx->cq_ev_fd)) {
4597                 int ret = PTR_ERR(ctx->cq_ev_fd);
4598                 ctx->cq_ev_fd = NULL;
4599                 return ret;
4600         }
4601
4602         return 0;
4603 }
4604
4605 static int io_eventfd_unregister(struct io_ring_ctx *ctx)
4606 {
4607         if (ctx->cq_ev_fd) {
4608                 eventfd_ctx_put(ctx->cq_ev_fd);
4609                 ctx->cq_ev_fd = NULL;
4610                 return 0;
4611         }
4612
4613         return -ENXIO;
4614 }
4615
4616 static void io_ring_ctx_free(struct io_ring_ctx *ctx)
4617 {
4618         io_finish_async(ctx);
4619         if (ctx->sqo_mm)
4620                 mmdrop(ctx->sqo_mm);
4621
4622         io_iopoll_reap_events(ctx);
4623         io_sqe_buffer_unregister(ctx);
4624         io_sqe_files_unregister(ctx);
4625         io_eventfd_unregister(ctx);
4626
4627 #if defined(CONFIG_UNIX)
4628         if (ctx->ring_sock) {
4629                 ctx->ring_sock->file = NULL; /* so that iput() is called */
4630                 sock_release(ctx->ring_sock);
4631         }
4632 #endif
4633
4634         io_mem_free(ctx->rings);
4635         io_mem_free(ctx->sq_sqes);
4636
4637         percpu_ref_exit(&ctx->refs);
4638         if (ctx->account_mem)
4639                 io_unaccount_mem(ctx->user,
4640                                 ring_pages(ctx->sq_entries, ctx->cq_entries));
4641         free_uid(ctx->user);
4642         put_cred(ctx->creds);
4643         kfree(ctx->completions);
4644         kfree(ctx->cancel_hash);
4645         kmem_cache_free(req_cachep, ctx->fallback_req);
4646         kfree(ctx);
4647 }
4648
4649 static __poll_t io_uring_poll(struct file *file, poll_table *wait)
4650 {
4651         struct io_ring_ctx *ctx = file->private_data;
4652         __poll_t mask = 0;
4653
4654         poll_wait(file, &ctx->cq_wait, wait);
4655         /*
4656          * synchronizes with barrier from wq_has_sleeper call in
4657          * io_commit_cqring
4658          */
4659         smp_rmb();
4660         if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
4661             ctx->rings->sq_ring_entries)
4662                 mask |= EPOLLOUT | EPOLLWRNORM;
4663         if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
4664                 mask |= EPOLLIN | EPOLLRDNORM;
4665
4666         return mask;
4667 }
4668
4669 static int io_uring_fasync(int fd, struct file *file, int on)
4670 {
4671         struct io_ring_ctx *ctx = file->private_data;
4672
4673         return fasync_helper(fd, file, on, &ctx->cq_fasync);
4674 }
4675
4676 static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
4677 {
4678         mutex_lock(&ctx->uring_lock);
4679         percpu_ref_kill(&ctx->refs);
4680         mutex_unlock(&ctx->uring_lock);
4681
4682         io_kill_timeouts(ctx);
4683         io_poll_remove_all(ctx);
4684
4685         if (ctx->io_wq)
4686                 io_wq_cancel_all(ctx->io_wq);
4687
4688         io_iopoll_reap_events(ctx);
4689         /* if we failed setting up the ctx, we might not have any rings */
4690         if (ctx->rings)
4691                 io_cqring_overflow_flush(ctx, true);
4692         wait_for_completion(&ctx->completions[0]);
4693         io_ring_ctx_free(ctx);
4694 }
4695
4696 static int io_uring_release(struct inode *inode, struct file *file)
4697 {
4698         struct io_ring_ctx *ctx = file->private_data;
4699
4700         file->private_data = NULL;
4701         io_ring_ctx_wait_and_kill(ctx);
4702         return 0;
4703 }
4704
4705 static void io_uring_cancel_files(struct io_ring_ctx *ctx,
4706                                   struct files_struct *files)
4707 {
4708         struct io_kiocb *req;
4709         DEFINE_WAIT(wait);
4710
4711         while (!list_empty_careful(&ctx->inflight_list)) {
4712                 struct io_kiocb *cancel_req = NULL;
4713
4714                 spin_lock_irq(&ctx->inflight_lock);
4715                 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
4716                         if (req->work.files != files)
4717                                 continue;
4718                         /* req is being completed, ignore */
4719                         if (!refcount_inc_not_zero(&req->refs))
4720                                 continue;
4721                         cancel_req = req;
4722                         break;
4723                 }
4724                 if (cancel_req)
4725                         prepare_to_wait(&ctx->inflight_wait, &wait,
4726                                                 TASK_UNINTERRUPTIBLE);
4727                 spin_unlock_irq(&ctx->inflight_lock);
4728
4729                 /* We need to keep going until we don't find a matching req */
4730                 if (!cancel_req)
4731                         break;
4732
4733                 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
4734                 io_put_req(cancel_req);
4735                 schedule();
4736         }
4737         finish_wait(&ctx->inflight_wait, &wait);
4738 }
4739
4740 static int io_uring_flush(struct file *file, void *data)
4741 {
4742         struct io_ring_ctx *ctx = file->private_data;
4743
4744         io_uring_cancel_files(ctx, data);
4745         if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
4746                 io_cqring_overflow_flush(ctx, true);
4747                 io_wq_cancel_all(ctx->io_wq);
4748         }
4749         return 0;
4750 }
4751
4752 static void *io_uring_validate_mmap_request(struct file *file,
4753                                             loff_t pgoff, size_t sz)
4754 {
4755         struct io_ring_ctx *ctx = file->private_data;
4756         loff_t offset = pgoff << PAGE_SHIFT;
4757         struct page *page;
4758         void *ptr;
4759
4760         switch (offset) {
4761         case IORING_OFF_SQ_RING:
4762         case IORING_OFF_CQ_RING:
4763                 ptr = ctx->rings;
4764                 break;
4765         case IORING_OFF_SQES:
4766                 ptr = ctx->sq_sqes;
4767                 break;
4768         default:
4769                 return ERR_PTR(-EINVAL);
4770         }
4771
4772         page = virt_to_head_page(ptr);
4773         if (sz > page_size(page))
4774                 return ERR_PTR(-EINVAL);
4775
4776         return ptr;
4777 }
4778
4779 #ifdef CONFIG_MMU
4780
4781 static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
4782 {
4783         size_t sz = vma->vm_end - vma->vm_start;
4784         unsigned long pfn;
4785         void *ptr;
4786
4787         ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
4788         if (IS_ERR(ptr))
4789                 return PTR_ERR(ptr);
4790
4791         pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
4792         return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
4793 }
4794
4795 #else /* !CONFIG_MMU */
4796
4797 static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
4798 {
4799         return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
4800 }
4801
4802 static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
4803 {
4804         return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
4805 }
4806
4807 static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
4808         unsigned long addr, unsigned long len,
4809         unsigned long pgoff, unsigned long flags)
4810 {
4811         void *ptr;
4812
4813         ptr = io_uring_validate_mmap_request(file, pgoff, len);
4814         if (IS_ERR(ptr))
4815                 return PTR_ERR(ptr);
4816
4817         return (unsigned long) ptr;
4818 }
4819
4820 #endif /* !CONFIG_MMU */
4821
4822 SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
4823                 u32, min_complete, u32, flags, const sigset_t __user *, sig,
4824                 size_t, sigsz)
4825 {
4826         struct io_ring_ctx *ctx;
4827         long ret = -EBADF;
4828         int submitted = 0;
4829         struct fd f;
4830
4831         if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
4832                 return -EINVAL;
4833
4834         f = fdget(fd);
4835         if (!f.file)
4836                 return -EBADF;
4837
4838         ret = -EOPNOTSUPP;
4839         if (f.file->f_op != &io_uring_fops)
4840                 goto out_fput;
4841
4842         ret = -ENXIO;
4843         ctx = f.file->private_data;
4844         if (!percpu_ref_tryget(&ctx->refs))
4845                 goto out_fput;
4846
4847         /*
4848          * For SQ polling, the thread will do all submissions and completions.
4849          * Just return the requested submit count, and wake the thread if
4850          * we were asked to.
4851          */
4852         ret = 0;
4853         if (ctx->flags & IORING_SETUP_SQPOLL) {
4854                 if (!list_empty_careful(&ctx->cq_overflow_list))
4855                         io_cqring_overflow_flush(ctx, false);
4856                 if (flags & IORING_ENTER_SQ_WAKEUP)
4857                         wake_up(&ctx->sqo_wait);
4858                 submitted = to_submit;
4859         } else if (to_submit) {
4860                 struct mm_struct *cur_mm;
4861
4862                 to_submit = min(to_submit, ctx->sq_entries);
4863                 mutex_lock(&ctx->uring_lock);
4864                 /* already have mm, so io_submit_sqes() won't try to grab it */
4865                 cur_mm = ctx->sqo_mm;
4866                 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
4867                                            &cur_mm, false);
4868                 mutex_unlock(&ctx->uring_lock);
4869         }
4870         if (flags & IORING_ENTER_GETEVENTS) {
4871                 unsigned nr_events = 0;
4872
4873                 min_complete = min(min_complete, ctx->cq_entries);
4874
4875                 if (ctx->flags & IORING_SETUP_IOPOLL) {
4876                         ret = io_iopoll_check(ctx, &nr_events, min_complete);
4877                 } else {
4878                         ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
4879                 }
4880         }
4881
4882         percpu_ref_put(&ctx->refs);
4883 out_fput:
4884         fdput(f);
4885         return submitted ? submitted : ret;
4886 }
4887
4888 static const struct file_operations io_uring_fops = {
4889         .release        = io_uring_release,
4890         .flush          = io_uring_flush,
4891         .mmap           = io_uring_mmap,
4892 #ifndef CONFIG_MMU
4893         .get_unmapped_area = io_uring_nommu_get_unmapped_area,
4894         .mmap_capabilities = io_uring_nommu_mmap_capabilities,
4895 #endif
4896         .poll           = io_uring_poll,
4897         .fasync         = io_uring_fasync,
4898 };
4899
4900 static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
4901                                   struct io_uring_params *p)
4902 {
4903         struct io_rings *rings;
4904         size_t size, sq_array_offset;
4905
4906         size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
4907         if (size == SIZE_MAX)
4908                 return -EOVERFLOW;
4909
4910         rings = io_mem_alloc(size);
4911         if (!rings)
4912                 return -ENOMEM;
4913
4914         ctx->rings = rings;
4915         ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
4916         rings->sq_ring_mask = p->sq_entries - 1;
4917         rings->cq_ring_mask = p->cq_entries - 1;
4918         rings->sq_ring_entries = p->sq_entries;
4919         rings->cq_ring_entries = p->cq_entries;
4920         ctx->sq_mask = rings->sq_ring_mask;
4921         ctx->cq_mask = rings->cq_ring_mask;
4922         ctx->sq_entries = rings->sq_ring_entries;
4923         ctx->cq_entries = rings->cq_ring_entries;
4924
4925         size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
4926         if (size == SIZE_MAX) {
4927                 io_mem_free(ctx->rings);
4928                 ctx->rings = NULL;
4929                 return -EOVERFLOW;
4930         }
4931
4932         ctx->sq_sqes = io_mem_alloc(size);
4933         if (!ctx->sq_sqes) {
4934                 io_mem_free(ctx->rings);
4935                 ctx->rings = NULL;
4936                 return -ENOMEM;
4937         }
4938
4939         return 0;
4940 }
4941
4942 /*
4943  * Allocate an anonymous fd, this is what constitutes the application
4944  * visible backing of an io_uring instance. The application mmaps this
4945  * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
4946  * we have to tie this fd to a socket for file garbage collection purposes.
4947  */
4948 static int io_uring_get_fd(struct io_ring_ctx *ctx)
4949 {
4950         struct file *file;
4951         int ret;
4952
4953 #if defined(CONFIG_UNIX)
4954         ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
4955                                 &ctx->ring_sock);
4956         if (ret)
4957                 return ret;
4958 #endif
4959
4960         ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
4961         if (ret < 0)
4962                 goto err;
4963
4964         file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
4965                                         O_RDWR | O_CLOEXEC);
4966         if (IS_ERR(file)) {
4967                 put_unused_fd(ret);
4968                 ret = PTR_ERR(file);
4969                 goto err;
4970         }
4971
4972 #if defined(CONFIG_UNIX)
4973         ctx->ring_sock->file = file;
4974         ctx->ring_sock->sk->sk_user_data = ctx;
4975 #endif
4976         fd_install(ret, file);
4977         return ret;
4978 err:
4979 #if defined(CONFIG_UNIX)
4980         sock_release(ctx->ring_sock);
4981         ctx->ring_sock = NULL;
4982 #endif
4983         return ret;
4984 }
4985
4986 static int io_uring_create(unsigned entries, struct io_uring_params *p)
4987 {
4988         struct user_struct *user = NULL;
4989         struct io_ring_ctx *ctx;
4990         bool account_mem;
4991         int ret;
4992
4993         if (!entries || entries > IORING_MAX_ENTRIES)
4994                 return -EINVAL;
4995
4996         /*
4997          * Use twice as many entries for the CQ ring. It's possible for the
4998          * application to drive a higher depth than the size of the SQ ring,
4999          * since the sqes are only used at submission time. This allows for
5000          * some flexibility in overcommitting a bit. If the application has
5001          * set IORING_SETUP_CQSIZE, it will have passed in the desired number
5002          * of CQ ring entries manually.
5003          */
5004         p->sq_entries = roundup_pow_of_two(entries);
5005         if (p->flags & IORING_SETUP_CQSIZE) {
5006                 /*
5007                  * If IORING_SETUP_CQSIZE is set, we do the same roundup
5008                  * to a power-of-two, if it isn't already. We do NOT impose
5009                  * any cq vs sq ring sizing.
5010                  */
5011                 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
5012                         return -EINVAL;
5013                 p->cq_entries = roundup_pow_of_two(p->cq_entries);
5014         } else {
5015                 p->cq_entries = 2 * p->sq_entries;
5016         }
5017
5018         user = get_uid(current_user());
5019         account_mem = !capable(CAP_IPC_LOCK);
5020
5021         if (account_mem) {
5022                 ret = io_account_mem(user,
5023                                 ring_pages(p->sq_entries, p->cq_entries));
5024                 if (ret) {
5025                         free_uid(user);
5026                         return ret;
5027                 }
5028         }
5029
5030         ctx = io_ring_ctx_alloc(p);
5031         if (!ctx) {
5032                 if (account_mem)
5033                         io_unaccount_mem(user, ring_pages(p->sq_entries,
5034                                                                 p->cq_entries));
5035                 free_uid(user);
5036                 return -ENOMEM;
5037         }
5038         ctx->compat = in_compat_syscall();
5039         ctx->account_mem = account_mem;
5040         ctx->user = user;
5041         ctx->creds = get_current_cred();
5042
5043         ret = io_allocate_scq_urings(ctx, p);
5044         if (ret)
5045                 goto err;
5046
5047         ret = io_sq_offload_start(ctx, p);
5048         if (ret)
5049                 goto err;
5050
5051         memset(&p->sq_off, 0, sizeof(p->sq_off));
5052         p->sq_off.head = offsetof(struct io_rings, sq.head);
5053         p->sq_off.tail = offsetof(struct io_rings, sq.tail);
5054         p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
5055         p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
5056         p->sq_off.flags = offsetof(struct io_rings, sq_flags);
5057         p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
5058         p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
5059
5060         memset(&p->cq_off, 0, sizeof(p->cq_off));
5061         p->cq_off.head = offsetof(struct io_rings, cq.head);
5062         p->cq_off.tail = offsetof(struct io_rings, cq.tail);
5063         p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
5064         p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
5065         p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
5066         p->cq_off.cqes = offsetof(struct io_rings, cqes);
5067
5068         /*
5069          * Install ring fd as the very last thing, so we don't risk someone
5070          * having closed it before we finish setup
5071          */
5072         ret = io_uring_get_fd(ctx);
5073         if (ret < 0)
5074                 goto err;
5075
5076         p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
5077                         IORING_FEAT_SUBMIT_STABLE;
5078         trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
5079         return ret;
5080 err:
5081         io_ring_ctx_wait_and_kill(ctx);
5082         return ret;
5083 }
5084
5085 /*
5086  * Sets up an aio uring context, and returns the fd. Applications asks for a
5087  * ring size, we return the actual sq/cq ring sizes (among other things) in the
5088  * params structure passed in.
5089  */
5090 static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
5091 {
5092         struct io_uring_params p;
5093         long ret;
5094         int i;
5095
5096         if (copy_from_user(&p, params, sizeof(p)))
5097                 return -EFAULT;
5098         for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
5099                 if (p.resv[i])
5100                         return -EINVAL;
5101         }
5102
5103         if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
5104                         IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
5105                 return -EINVAL;
5106
5107         ret = io_uring_create(entries, &p);
5108         if (ret < 0)
5109                 return ret;
5110
5111         if (copy_to_user(params, &p, sizeof(p)))
5112                 return -EFAULT;
5113
5114         return ret;
5115 }
5116
5117 SYSCALL_DEFINE2(io_uring_setup, u32, entries,
5118                 struct io_uring_params __user *, params)
5119 {
5120         return io_uring_setup(entries, params);
5121 }
5122
5123 static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
5124                                void __user *arg, unsigned nr_args)
5125         __releases(ctx->uring_lock)
5126         __acquires(ctx->uring_lock)
5127 {
5128         int ret;
5129
5130         /*
5131          * We're inside the ring mutex, if the ref is already dying, then
5132          * someone else killed the ctx or is already going through
5133          * io_uring_register().
5134          */
5135         if (percpu_ref_is_dying(&ctx->refs))
5136                 return -ENXIO;
5137
5138         percpu_ref_kill(&ctx->refs);
5139
5140         /*
5141          * Drop uring mutex before waiting for references to exit. If another
5142          * thread is currently inside io_uring_enter() it might need to grab
5143          * the uring_lock to make progress. If we hold it here across the drain
5144          * wait, then we can deadlock. It's safe to drop the mutex here, since
5145          * no new references will come in after we've killed the percpu ref.
5146          */
5147         mutex_unlock(&ctx->uring_lock);
5148         wait_for_completion(&ctx->completions[0]);
5149         mutex_lock(&ctx->uring_lock);
5150
5151         switch (opcode) {
5152         case IORING_REGISTER_BUFFERS:
5153                 ret = io_sqe_buffer_register(ctx, arg, nr_args);
5154                 break;
5155         case IORING_UNREGISTER_BUFFERS:
5156                 ret = -EINVAL;
5157                 if (arg || nr_args)
5158                         break;
5159                 ret = io_sqe_buffer_unregister(ctx);
5160                 break;
5161         case IORING_REGISTER_FILES:
5162                 ret = io_sqe_files_register(ctx, arg, nr_args);
5163                 break;
5164         case IORING_UNREGISTER_FILES:
5165                 ret = -EINVAL;
5166                 if (arg || nr_args)
5167                         break;
5168                 ret = io_sqe_files_unregister(ctx);
5169                 break;
5170         case IORING_REGISTER_FILES_UPDATE:
5171                 ret = io_sqe_files_update(ctx, arg, nr_args);
5172                 break;
5173         case IORING_REGISTER_EVENTFD:
5174                 ret = -EINVAL;
5175                 if (nr_args != 1)
5176                         break;
5177                 ret = io_eventfd_register(ctx, arg);
5178                 break;
5179         case IORING_UNREGISTER_EVENTFD:
5180                 ret = -EINVAL;
5181                 if (arg || nr_args)
5182                         break;
5183                 ret = io_eventfd_unregister(ctx);
5184                 break;
5185         default:
5186                 ret = -EINVAL;
5187                 break;
5188         }
5189
5190         /* bring the ctx back to life */
5191         reinit_completion(&ctx->completions[0]);
5192         percpu_ref_reinit(&ctx->refs);
5193         return ret;
5194 }
5195
5196 SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
5197                 void __user *, arg, unsigned int, nr_args)
5198 {
5199         struct io_ring_ctx *ctx;
5200         long ret = -EBADF;
5201         struct fd f;
5202
5203         f = fdget(fd);
5204         if (!f.file)
5205                 return -EBADF;
5206
5207         ret = -EOPNOTSUPP;
5208         if (f.file->f_op != &io_uring_fops)
5209                 goto out_fput;
5210
5211         ctx = f.file->private_data;
5212
5213         mutex_lock(&ctx->uring_lock);
5214         ret = __io_uring_register(ctx, opcode, arg, nr_args);
5215         mutex_unlock(&ctx->uring_lock);
5216         trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
5217                                                         ctx->cq_ev_fd != NULL, ret);
5218 out_fput:
5219         fdput(f);
5220         return ret;
5221 }
5222
5223 static int __init io_uring_init(void)
5224 {
5225         req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
5226         return 0;
5227 };
5228 __initcall(io_uring_init);