Merge tag 'for-5.16-rc2-tag' of git://git.kernel.org/pub/scm/linux/kernel/git/kdave...
[linux-2.6-microblaze.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_cqe (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 <net/compat.h>
48 #include <linux/refcount.h>
49 #include <linux/uio.h>
50 #include <linux/bits.h>
51
52 #include <linux/sched/signal.h>
53 #include <linux/fs.h>
54 #include <linux/file.h>
55 #include <linux/fdtable.h>
56 #include <linux/mm.h>
57 #include <linux/mman.h>
58 #include <linux/percpu.h>
59 #include <linux/slab.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 #include <linux/namei.h>
74 #include <linux/fsnotify.h>
75 #include <linux/fadvise.h>
76 #include <linux/eventpoll.h>
77 #include <linux/splice.h>
78 #include <linux/task_work.h>
79 #include <linux/pagemap.h>
80 #include <linux/io_uring.h>
81 #include <linux/tracehook.h>
82 #include <linux/audit.h>
83 #include <linux/security.h>
84
85 #define CREATE_TRACE_POINTS
86 #include <trace/events/io_uring.h>
87
88 #include <uapi/linux/io_uring.h>
89
90 #include "internal.h"
91 #include "io-wq.h"
92
93 #define IORING_MAX_ENTRIES      32768
94 #define IORING_MAX_CQ_ENTRIES   (2 * IORING_MAX_ENTRIES)
95 #define IORING_SQPOLL_CAP_ENTRIES_VALUE 8
96
97 /* only define max */
98 #define IORING_MAX_FIXED_FILES  (1U << 15)
99 #define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \
100                                  IORING_REGISTER_LAST + IORING_OP_LAST)
101
102 #define IO_RSRC_TAG_TABLE_SHIFT (PAGE_SHIFT - 3)
103 #define IO_RSRC_TAG_TABLE_MAX   (1U << IO_RSRC_TAG_TABLE_SHIFT)
104 #define IO_RSRC_TAG_TABLE_MASK  (IO_RSRC_TAG_TABLE_MAX - 1)
105
106 #define IORING_MAX_REG_BUFFERS  (1U << 14)
107
108 #define SQE_COMMON_FLAGS (IOSQE_FIXED_FILE | IOSQE_IO_LINK | \
109                           IOSQE_IO_HARDLINK | IOSQE_ASYNC)
110
111 #define SQE_VALID_FLAGS (SQE_COMMON_FLAGS|IOSQE_BUFFER_SELECT|IOSQE_IO_DRAIN)
112
113 #define IO_REQ_CLEAN_FLAGS (REQ_F_BUFFER_SELECTED | REQ_F_NEED_CLEANUP | \
114                                 REQ_F_POLLED | REQ_F_INFLIGHT | REQ_F_CREDS | \
115                                 REQ_F_ASYNC_DATA)
116
117 #define IO_TCTX_REFS_CACHE_NR   (1U << 10)
118
119 struct io_uring {
120         u32 head ____cacheline_aligned_in_smp;
121         u32 tail ____cacheline_aligned_in_smp;
122 };
123
124 /*
125  * This data is shared with the application through the mmap at offsets
126  * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
127  *
128  * The offsets to the member fields are published through struct
129  * io_sqring_offsets when calling io_uring_setup.
130  */
131 struct io_rings {
132         /*
133          * Head and tail offsets into the ring; the offsets need to be
134          * masked to get valid indices.
135          *
136          * The kernel controls head of the sq ring and the tail of the cq ring,
137          * and the application controls tail of the sq ring and the head of the
138          * cq ring.
139          */
140         struct io_uring         sq, cq;
141         /*
142          * Bitmasks to apply to head and tail offsets (constant, equals
143          * ring_entries - 1)
144          */
145         u32                     sq_ring_mask, cq_ring_mask;
146         /* Ring sizes (constant, power of 2) */
147         u32                     sq_ring_entries, cq_ring_entries;
148         /*
149          * Number of invalid entries dropped by the kernel due to
150          * invalid index stored in array
151          *
152          * Written by the kernel, shouldn't be modified by the
153          * application (i.e. get number of "new events" by comparing to
154          * cached value).
155          *
156          * After a new SQ head value was read by the application this
157          * counter includes all submissions that were dropped reaching
158          * the new SQ head (and possibly more).
159          */
160         u32                     sq_dropped;
161         /*
162          * Runtime SQ flags
163          *
164          * Written by the kernel, shouldn't be modified by the
165          * application.
166          *
167          * The application needs a full memory barrier before checking
168          * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
169          */
170         u32                     sq_flags;
171         /*
172          * Runtime CQ flags
173          *
174          * Written by the application, shouldn't be modified by the
175          * kernel.
176          */
177         u32                     cq_flags;
178         /*
179          * Number of completion events lost because the queue was full;
180          * this should be avoided by the application by making sure
181          * there are not more requests pending than there is space in
182          * the completion queue.
183          *
184          * Written by the kernel, shouldn't be modified by the
185          * application (i.e. get number of "new events" by comparing to
186          * cached value).
187          *
188          * As completion events come in out of order this counter is not
189          * ordered with any other data.
190          */
191         u32                     cq_overflow;
192         /*
193          * Ring buffer of completion events.
194          *
195          * The kernel writes completion events fresh every time they are
196          * produced, so the application is allowed to modify pending
197          * entries.
198          */
199         struct io_uring_cqe     cqes[] ____cacheline_aligned_in_smp;
200 };
201
202 enum io_uring_cmd_flags {
203         IO_URING_F_COMPLETE_DEFER       = 1,
204         IO_URING_F_UNLOCKED             = 2,
205         /* int's last bit, sign checks are usually faster than a bit test */
206         IO_URING_F_NONBLOCK             = INT_MIN,
207 };
208
209 struct io_mapped_ubuf {
210         u64             ubuf;
211         u64             ubuf_end;
212         unsigned int    nr_bvecs;
213         unsigned long   acct_pages;
214         struct bio_vec  bvec[];
215 };
216
217 struct io_ring_ctx;
218
219 struct io_overflow_cqe {
220         struct io_uring_cqe cqe;
221         struct list_head list;
222 };
223
224 struct io_fixed_file {
225         /* file * with additional FFS_* flags */
226         unsigned long file_ptr;
227 };
228
229 struct io_rsrc_put {
230         struct list_head list;
231         u64 tag;
232         union {
233                 void *rsrc;
234                 struct file *file;
235                 struct io_mapped_ubuf *buf;
236         };
237 };
238
239 struct io_file_table {
240         struct io_fixed_file *files;
241 };
242
243 struct io_rsrc_node {
244         struct percpu_ref               refs;
245         struct list_head                node;
246         struct list_head                rsrc_list;
247         struct io_rsrc_data             *rsrc_data;
248         struct llist_node               llist;
249         bool                            done;
250 };
251
252 typedef void (rsrc_put_fn)(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc);
253
254 struct io_rsrc_data {
255         struct io_ring_ctx              *ctx;
256
257         u64                             **tags;
258         unsigned int                    nr;
259         rsrc_put_fn                     *do_put;
260         atomic_t                        refs;
261         struct completion               done;
262         bool                            quiesce;
263 };
264
265 struct io_buffer {
266         struct list_head list;
267         __u64 addr;
268         __u32 len;
269         __u16 bid;
270 };
271
272 struct io_restriction {
273         DECLARE_BITMAP(register_op, IORING_REGISTER_LAST);
274         DECLARE_BITMAP(sqe_op, IORING_OP_LAST);
275         u8 sqe_flags_allowed;
276         u8 sqe_flags_required;
277         bool registered;
278 };
279
280 enum {
281         IO_SQ_THREAD_SHOULD_STOP = 0,
282         IO_SQ_THREAD_SHOULD_PARK,
283 };
284
285 struct io_sq_data {
286         refcount_t              refs;
287         atomic_t                park_pending;
288         struct mutex            lock;
289
290         /* ctx's that are using this sqd */
291         struct list_head        ctx_list;
292
293         struct task_struct      *thread;
294         struct wait_queue_head  wait;
295
296         unsigned                sq_thread_idle;
297         int                     sq_cpu;
298         pid_t                   task_pid;
299         pid_t                   task_tgid;
300
301         unsigned long           state;
302         struct completion       exited;
303 };
304
305 #define IO_COMPL_BATCH                  32
306 #define IO_REQ_CACHE_SIZE               32
307 #define IO_REQ_ALLOC_BATCH              8
308
309 struct io_submit_link {
310         struct io_kiocb         *head;
311         struct io_kiocb         *last;
312 };
313
314 struct io_submit_state {
315         /* inline/task_work completion list, under ->uring_lock */
316         struct io_wq_work_node  free_list;
317         /* batch completion logic */
318         struct io_wq_work_list  compl_reqs;
319         struct io_submit_link   link;
320
321         bool                    plug_started;
322         bool                    need_plug;
323         unsigned short          submit_nr;
324         struct blk_plug         plug;
325 };
326
327 struct io_ring_ctx {
328         /* const or read-mostly hot data */
329         struct {
330                 struct percpu_ref       refs;
331
332                 struct io_rings         *rings;
333                 unsigned int            flags;
334                 unsigned int            compat: 1;
335                 unsigned int            drain_next: 1;
336                 unsigned int            eventfd_async: 1;
337                 unsigned int            restricted: 1;
338                 unsigned int            off_timeout_used: 1;
339                 unsigned int            drain_active: 1;
340         } ____cacheline_aligned_in_smp;
341
342         /* submission data */
343         struct {
344                 struct mutex            uring_lock;
345
346                 /*
347                  * Ring buffer of indices into array of io_uring_sqe, which is
348                  * mmapped by the application using the IORING_OFF_SQES offset.
349                  *
350                  * This indirection could e.g. be used to assign fixed
351                  * io_uring_sqe entries to operations and only submit them to
352                  * the queue when needed.
353                  *
354                  * The kernel modifies neither the indices array nor the entries
355                  * array.
356                  */
357                 u32                     *sq_array;
358                 struct io_uring_sqe     *sq_sqes;
359                 unsigned                cached_sq_head;
360                 unsigned                sq_entries;
361                 struct list_head        defer_list;
362
363                 /*
364                  * Fixed resources fast path, should be accessed only under
365                  * uring_lock, and updated through io_uring_register(2)
366                  */
367                 struct io_rsrc_node     *rsrc_node;
368                 int                     rsrc_cached_refs;
369                 struct io_file_table    file_table;
370                 unsigned                nr_user_files;
371                 unsigned                nr_user_bufs;
372                 struct io_mapped_ubuf   **user_bufs;
373
374                 struct io_submit_state  submit_state;
375                 struct list_head        timeout_list;
376                 struct list_head        ltimeout_list;
377                 struct list_head        cq_overflow_list;
378                 struct xarray           io_buffers;
379                 struct xarray           personalities;
380                 u32                     pers_next;
381                 unsigned                sq_thread_idle;
382         } ____cacheline_aligned_in_smp;
383
384         /* IRQ completion list, under ->completion_lock */
385         struct io_wq_work_list  locked_free_list;
386         unsigned int            locked_free_nr;
387
388         const struct cred       *sq_creds;      /* cred used for __io_sq_thread() */
389         struct io_sq_data       *sq_data;       /* if using sq thread polling */
390
391         struct wait_queue_head  sqo_sq_wait;
392         struct list_head        sqd_list;
393
394         unsigned long           check_cq_overflow;
395
396         struct {
397                 unsigned                cached_cq_tail;
398                 unsigned                cq_entries;
399                 struct eventfd_ctx      *cq_ev_fd;
400                 struct wait_queue_head  cq_wait;
401                 unsigned                cq_extra;
402                 atomic_t                cq_timeouts;
403                 unsigned                cq_last_tm_flush;
404         } ____cacheline_aligned_in_smp;
405
406         struct {
407                 spinlock_t              completion_lock;
408
409                 spinlock_t              timeout_lock;
410
411                 /*
412                  * ->iopoll_list is protected by the ctx->uring_lock for
413                  * io_uring instances that don't use IORING_SETUP_SQPOLL.
414                  * For SQPOLL, only the single threaded io_sq_thread() will
415                  * manipulate the list, hence no extra locking is needed there.
416                  */
417                 struct io_wq_work_list  iopoll_list;
418                 struct hlist_head       *cancel_hash;
419                 unsigned                cancel_hash_bits;
420                 bool                    poll_multi_queue;
421         } ____cacheline_aligned_in_smp;
422
423         struct io_restriction           restrictions;
424
425         /* slow path rsrc auxilary data, used by update/register */
426         struct {
427                 struct io_rsrc_node             *rsrc_backup_node;
428                 struct io_mapped_ubuf           *dummy_ubuf;
429                 struct io_rsrc_data             *file_data;
430                 struct io_rsrc_data             *buf_data;
431
432                 struct delayed_work             rsrc_put_work;
433                 struct llist_head               rsrc_put_llist;
434                 struct list_head                rsrc_ref_list;
435                 spinlock_t                      rsrc_ref_lock;
436         };
437
438         /* Keep this last, we don't need it for the fast path */
439         struct {
440                 #if defined(CONFIG_UNIX)
441                         struct socket           *ring_sock;
442                 #endif
443                 /* hashed buffered write serialization */
444                 struct io_wq_hash               *hash_map;
445
446                 /* Only used for accounting purposes */
447                 struct user_struct              *user;
448                 struct mm_struct                *mm_account;
449
450                 /* ctx exit and cancelation */
451                 struct llist_head               fallback_llist;
452                 struct delayed_work             fallback_work;
453                 struct work_struct              exit_work;
454                 struct list_head                tctx_list;
455                 struct completion               ref_comp;
456                 u32                             iowq_limits[2];
457                 bool                            iowq_limits_set;
458         };
459 };
460
461 struct io_uring_task {
462         /* submission side */
463         int                     cached_refs;
464         struct xarray           xa;
465         struct wait_queue_head  wait;
466         const struct io_ring_ctx *last;
467         struct io_wq            *io_wq;
468         struct percpu_counter   inflight;
469         atomic_t                inflight_tracked;
470         atomic_t                in_idle;
471
472         spinlock_t              task_lock;
473         struct io_wq_work_list  task_list;
474         struct callback_head    task_work;
475         bool                    task_running;
476 };
477
478 /*
479  * First field must be the file pointer in all the
480  * iocb unions! See also 'struct kiocb' in <linux/fs.h>
481  */
482 struct io_poll_iocb {
483         struct file                     *file;
484         struct wait_queue_head          *head;
485         __poll_t                        events;
486         bool                            done;
487         bool                            canceled;
488         struct wait_queue_entry         wait;
489 };
490
491 struct io_poll_update {
492         struct file                     *file;
493         u64                             old_user_data;
494         u64                             new_user_data;
495         __poll_t                        events;
496         bool                            update_events;
497         bool                            update_user_data;
498 };
499
500 struct io_close {
501         struct file                     *file;
502         int                             fd;
503         u32                             file_slot;
504 };
505
506 struct io_timeout_data {
507         struct io_kiocb                 *req;
508         struct hrtimer                  timer;
509         struct timespec64               ts;
510         enum hrtimer_mode               mode;
511         u32                             flags;
512 };
513
514 struct io_accept {
515         struct file                     *file;
516         struct sockaddr __user          *addr;
517         int __user                      *addr_len;
518         int                             flags;
519         u32                             file_slot;
520         unsigned long                   nofile;
521 };
522
523 struct io_sync {
524         struct file                     *file;
525         loff_t                          len;
526         loff_t                          off;
527         int                             flags;
528         int                             mode;
529 };
530
531 struct io_cancel {
532         struct file                     *file;
533         u64                             addr;
534 };
535
536 struct io_timeout {
537         struct file                     *file;
538         u32                             off;
539         u32                             target_seq;
540         struct list_head                list;
541         /* head of the link, used by linked timeouts only */
542         struct io_kiocb                 *head;
543         /* for linked completions */
544         struct io_kiocb                 *prev;
545 };
546
547 struct io_timeout_rem {
548         struct file                     *file;
549         u64                             addr;
550
551         /* timeout update */
552         struct timespec64               ts;
553         u32                             flags;
554         bool                            ltimeout;
555 };
556
557 struct io_rw {
558         /* NOTE: kiocb has the file as the first member, so don't do it here */
559         struct kiocb                    kiocb;
560         u64                             addr;
561         u64                             len;
562 };
563
564 struct io_connect {
565         struct file                     *file;
566         struct sockaddr __user          *addr;
567         int                             addr_len;
568 };
569
570 struct io_sr_msg {
571         struct file                     *file;
572         union {
573                 struct compat_msghdr __user     *umsg_compat;
574                 struct user_msghdr __user       *umsg;
575                 void __user                     *buf;
576         };
577         int                             msg_flags;
578         int                             bgid;
579         size_t                          len;
580 };
581
582 struct io_open {
583         struct file                     *file;
584         int                             dfd;
585         u32                             file_slot;
586         struct filename                 *filename;
587         struct open_how                 how;
588         unsigned long                   nofile;
589 };
590
591 struct io_rsrc_update {
592         struct file                     *file;
593         u64                             arg;
594         u32                             nr_args;
595         u32                             offset;
596 };
597
598 struct io_fadvise {
599         struct file                     *file;
600         u64                             offset;
601         u32                             len;
602         u32                             advice;
603 };
604
605 struct io_madvise {
606         struct file                     *file;
607         u64                             addr;
608         u32                             len;
609         u32                             advice;
610 };
611
612 struct io_epoll {
613         struct file                     *file;
614         int                             epfd;
615         int                             op;
616         int                             fd;
617         struct epoll_event              event;
618 };
619
620 struct io_splice {
621         struct file                     *file_out;
622         struct file                     *file_in;
623         loff_t                          off_out;
624         loff_t                          off_in;
625         u64                             len;
626         unsigned int                    flags;
627 };
628
629 struct io_provide_buf {
630         struct file                     *file;
631         __u64                           addr;
632         __u32                           len;
633         __u32                           bgid;
634         __u16                           nbufs;
635         __u16                           bid;
636 };
637
638 struct io_statx {
639         struct file                     *file;
640         int                             dfd;
641         unsigned int                    mask;
642         unsigned int                    flags;
643         const char __user               *filename;
644         struct statx __user             *buffer;
645 };
646
647 struct io_shutdown {
648         struct file                     *file;
649         int                             how;
650 };
651
652 struct io_rename {
653         struct file                     *file;
654         int                             old_dfd;
655         int                             new_dfd;
656         struct filename                 *oldpath;
657         struct filename                 *newpath;
658         int                             flags;
659 };
660
661 struct io_unlink {
662         struct file                     *file;
663         int                             dfd;
664         int                             flags;
665         struct filename                 *filename;
666 };
667
668 struct io_mkdir {
669         struct file                     *file;
670         int                             dfd;
671         umode_t                         mode;
672         struct filename                 *filename;
673 };
674
675 struct io_symlink {
676         struct file                     *file;
677         int                             new_dfd;
678         struct filename                 *oldpath;
679         struct filename                 *newpath;
680 };
681
682 struct io_hardlink {
683         struct file                     *file;
684         int                             old_dfd;
685         int                             new_dfd;
686         struct filename                 *oldpath;
687         struct filename                 *newpath;
688         int                             flags;
689 };
690
691 struct io_async_connect {
692         struct sockaddr_storage         address;
693 };
694
695 struct io_async_msghdr {
696         struct iovec                    fast_iov[UIO_FASTIOV];
697         /* points to an allocated iov, if NULL we use fast_iov instead */
698         struct iovec                    *free_iov;
699         struct sockaddr __user          *uaddr;
700         struct msghdr                   msg;
701         struct sockaddr_storage         addr;
702 };
703
704 struct io_rw_state {
705         struct iov_iter                 iter;
706         struct iov_iter_state           iter_state;
707         struct iovec                    fast_iov[UIO_FASTIOV];
708 };
709
710 struct io_async_rw {
711         struct io_rw_state              s;
712         const struct iovec              *free_iovec;
713         size_t                          bytes_done;
714         struct wait_page_queue          wpq;
715 };
716
717 enum {
718         REQ_F_FIXED_FILE_BIT    = IOSQE_FIXED_FILE_BIT,
719         REQ_F_IO_DRAIN_BIT      = IOSQE_IO_DRAIN_BIT,
720         REQ_F_LINK_BIT          = IOSQE_IO_LINK_BIT,
721         REQ_F_HARDLINK_BIT      = IOSQE_IO_HARDLINK_BIT,
722         REQ_F_FORCE_ASYNC_BIT   = IOSQE_ASYNC_BIT,
723         REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
724
725         /* first byte is taken by user flags, shift it to not overlap */
726         REQ_F_FAIL_BIT          = 8,
727         REQ_F_INFLIGHT_BIT,
728         REQ_F_CUR_POS_BIT,
729         REQ_F_NOWAIT_BIT,
730         REQ_F_LINK_TIMEOUT_BIT,
731         REQ_F_NEED_CLEANUP_BIT,
732         REQ_F_POLLED_BIT,
733         REQ_F_BUFFER_SELECTED_BIT,
734         REQ_F_COMPLETE_INLINE_BIT,
735         REQ_F_REISSUE_BIT,
736         REQ_F_CREDS_BIT,
737         REQ_F_REFCOUNT_BIT,
738         REQ_F_ARM_LTIMEOUT_BIT,
739         REQ_F_ASYNC_DATA_BIT,
740         /* keep async read/write and isreg together and in order */
741         REQ_F_SUPPORT_NOWAIT_BIT,
742         REQ_F_ISREG_BIT,
743
744         /* not a real bit, just to check we're not overflowing the space */
745         __REQ_F_LAST_BIT,
746 };
747
748 enum {
749         /* ctx owns file */
750         REQ_F_FIXED_FILE        = BIT(REQ_F_FIXED_FILE_BIT),
751         /* drain existing IO first */
752         REQ_F_IO_DRAIN          = BIT(REQ_F_IO_DRAIN_BIT),
753         /* linked sqes */
754         REQ_F_LINK              = BIT(REQ_F_LINK_BIT),
755         /* doesn't sever on completion < 0 */
756         REQ_F_HARDLINK          = BIT(REQ_F_HARDLINK_BIT),
757         /* IOSQE_ASYNC */
758         REQ_F_FORCE_ASYNC       = BIT(REQ_F_FORCE_ASYNC_BIT),
759         /* IOSQE_BUFFER_SELECT */
760         REQ_F_BUFFER_SELECT     = BIT(REQ_F_BUFFER_SELECT_BIT),
761
762         /* fail rest of links */
763         REQ_F_FAIL              = BIT(REQ_F_FAIL_BIT),
764         /* on inflight list, should be cancelled and waited on exit reliably */
765         REQ_F_INFLIGHT          = BIT(REQ_F_INFLIGHT_BIT),
766         /* read/write uses file position */
767         REQ_F_CUR_POS           = BIT(REQ_F_CUR_POS_BIT),
768         /* must not punt to workers */
769         REQ_F_NOWAIT            = BIT(REQ_F_NOWAIT_BIT),
770         /* has or had linked timeout */
771         REQ_F_LINK_TIMEOUT      = BIT(REQ_F_LINK_TIMEOUT_BIT),
772         /* needs cleanup */
773         REQ_F_NEED_CLEANUP      = BIT(REQ_F_NEED_CLEANUP_BIT),
774         /* already went through poll handler */
775         REQ_F_POLLED            = BIT(REQ_F_POLLED_BIT),
776         /* buffer already selected */
777         REQ_F_BUFFER_SELECTED   = BIT(REQ_F_BUFFER_SELECTED_BIT),
778         /* completion is deferred through io_comp_state */
779         REQ_F_COMPLETE_INLINE   = BIT(REQ_F_COMPLETE_INLINE_BIT),
780         /* caller should reissue async */
781         REQ_F_REISSUE           = BIT(REQ_F_REISSUE_BIT),
782         /* supports async reads/writes */
783         REQ_F_SUPPORT_NOWAIT    = BIT(REQ_F_SUPPORT_NOWAIT_BIT),
784         /* regular file */
785         REQ_F_ISREG             = BIT(REQ_F_ISREG_BIT),
786         /* has creds assigned */
787         REQ_F_CREDS             = BIT(REQ_F_CREDS_BIT),
788         /* skip refcounting if not set */
789         REQ_F_REFCOUNT          = BIT(REQ_F_REFCOUNT_BIT),
790         /* there is a linked timeout that has to be armed */
791         REQ_F_ARM_LTIMEOUT      = BIT(REQ_F_ARM_LTIMEOUT_BIT),
792         /* ->async_data allocated */
793         REQ_F_ASYNC_DATA        = BIT(REQ_F_ASYNC_DATA_BIT),
794 };
795
796 struct async_poll {
797         struct io_poll_iocb     poll;
798         struct io_poll_iocb     *double_poll;
799 };
800
801 typedef void (*io_req_tw_func_t)(struct io_kiocb *req, bool *locked);
802
803 struct io_task_work {
804         union {
805                 struct io_wq_work_node  node;
806                 struct llist_node       fallback_node;
807         };
808         io_req_tw_func_t                func;
809 };
810
811 enum {
812         IORING_RSRC_FILE                = 0,
813         IORING_RSRC_BUFFER              = 1,
814 };
815
816 /*
817  * NOTE! Each of the iocb union members has the file pointer
818  * as the first entry in their struct definition. So you can
819  * access the file pointer through any of the sub-structs,
820  * or directly as just 'ki_filp' in this struct.
821  */
822 struct io_kiocb {
823         union {
824                 struct file             *file;
825                 struct io_rw            rw;
826                 struct io_poll_iocb     poll;
827                 struct io_poll_update   poll_update;
828                 struct io_accept        accept;
829                 struct io_sync          sync;
830                 struct io_cancel        cancel;
831                 struct io_timeout       timeout;
832                 struct io_timeout_rem   timeout_rem;
833                 struct io_connect       connect;
834                 struct io_sr_msg        sr_msg;
835                 struct io_open          open;
836                 struct io_close         close;
837                 struct io_rsrc_update   rsrc_update;
838                 struct io_fadvise       fadvise;
839                 struct io_madvise       madvise;
840                 struct io_epoll         epoll;
841                 struct io_splice        splice;
842                 struct io_provide_buf   pbuf;
843                 struct io_statx         statx;
844                 struct io_shutdown      shutdown;
845                 struct io_rename        rename;
846                 struct io_unlink        unlink;
847                 struct io_mkdir         mkdir;
848                 struct io_symlink       symlink;
849                 struct io_hardlink      hardlink;
850         };
851
852         u8                              opcode;
853         /* polled IO has completed */
854         u8                              iopoll_completed;
855         u16                             buf_index;
856         unsigned int                    flags;
857
858         u64                             user_data;
859         u32                             result;
860         u32                             cflags;
861
862         struct io_ring_ctx              *ctx;
863         struct task_struct              *task;
864
865         struct percpu_ref               *fixed_rsrc_refs;
866         /* store used ubuf, so we can prevent reloading */
867         struct io_mapped_ubuf           *imu;
868
869         /* used by request caches, completion batching and iopoll */
870         struct io_wq_work_node          comp_list;
871         atomic_t                        refs;
872         struct io_kiocb                 *link;
873         struct io_task_work             io_task_work;
874         /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
875         struct hlist_node               hash_node;
876         /* internal polling, see IORING_FEAT_FAST_POLL */
877         struct async_poll               *apoll;
878         /* opcode allocated if it needs to store data for async defer */
879         void                            *async_data;
880         struct io_wq_work               work;
881         /* custom credentials, valid IFF REQ_F_CREDS is set */
882         const struct cred               *creds;
883         /* stores selected buf, valid IFF REQ_F_BUFFER_SELECTED is set */
884         struct io_buffer                *kbuf;
885 };
886
887 struct io_tctx_node {
888         struct list_head        ctx_node;
889         struct task_struct      *task;
890         struct io_ring_ctx      *ctx;
891 };
892
893 struct io_defer_entry {
894         struct list_head        list;
895         struct io_kiocb         *req;
896         u32                     seq;
897 };
898
899 struct io_op_def {
900         /* needs req->file assigned */
901         unsigned                needs_file : 1;
902         /* should block plug */
903         unsigned                plug : 1;
904         /* hash wq insertion if file is a regular file */
905         unsigned                hash_reg_file : 1;
906         /* unbound wq insertion if file is a non-regular file */
907         unsigned                unbound_nonreg_file : 1;
908         /* set if opcode supports polled "wait" */
909         unsigned                pollin : 1;
910         unsigned                pollout : 1;
911         /* op supports buffer selection */
912         unsigned                buffer_select : 1;
913         /* do prep async if is going to be punted */
914         unsigned                needs_async_setup : 1;
915         /* opcode is not supported by this kernel */
916         unsigned                not_supported : 1;
917         /* skip auditing */
918         unsigned                audit_skip : 1;
919         /* size of async data needed, if any */
920         unsigned short          async_size;
921 };
922
923 static const struct io_op_def io_op_defs[] = {
924         [IORING_OP_NOP] = {},
925         [IORING_OP_READV] = {
926                 .needs_file             = 1,
927                 .unbound_nonreg_file    = 1,
928                 .pollin                 = 1,
929                 .buffer_select          = 1,
930                 .needs_async_setup      = 1,
931                 .plug                   = 1,
932                 .audit_skip             = 1,
933                 .async_size             = sizeof(struct io_async_rw),
934         },
935         [IORING_OP_WRITEV] = {
936                 .needs_file             = 1,
937                 .hash_reg_file          = 1,
938                 .unbound_nonreg_file    = 1,
939                 .pollout                = 1,
940                 .needs_async_setup      = 1,
941                 .plug                   = 1,
942                 .audit_skip             = 1,
943                 .async_size             = sizeof(struct io_async_rw),
944         },
945         [IORING_OP_FSYNC] = {
946                 .needs_file             = 1,
947                 .audit_skip             = 1,
948         },
949         [IORING_OP_READ_FIXED] = {
950                 .needs_file             = 1,
951                 .unbound_nonreg_file    = 1,
952                 .pollin                 = 1,
953                 .plug                   = 1,
954                 .audit_skip             = 1,
955                 .async_size             = sizeof(struct io_async_rw),
956         },
957         [IORING_OP_WRITE_FIXED] = {
958                 .needs_file             = 1,
959                 .hash_reg_file          = 1,
960                 .unbound_nonreg_file    = 1,
961                 .pollout                = 1,
962                 .plug                   = 1,
963                 .audit_skip             = 1,
964                 .async_size             = sizeof(struct io_async_rw),
965         },
966         [IORING_OP_POLL_ADD] = {
967                 .needs_file             = 1,
968                 .unbound_nonreg_file    = 1,
969                 .audit_skip             = 1,
970         },
971         [IORING_OP_POLL_REMOVE] = {
972                 .audit_skip             = 1,
973         },
974         [IORING_OP_SYNC_FILE_RANGE] = {
975                 .needs_file             = 1,
976                 .audit_skip             = 1,
977         },
978         [IORING_OP_SENDMSG] = {
979                 .needs_file             = 1,
980                 .unbound_nonreg_file    = 1,
981                 .pollout                = 1,
982                 .needs_async_setup      = 1,
983                 .async_size             = sizeof(struct io_async_msghdr),
984         },
985         [IORING_OP_RECVMSG] = {
986                 .needs_file             = 1,
987                 .unbound_nonreg_file    = 1,
988                 .pollin                 = 1,
989                 .buffer_select          = 1,
990                 .needs_async_setup      = 1,
991                 .async_size             = sizeof(struct io_async_msghdr),
992         },
993         [IORING_OP_TIMEOUT] = {
994                 .audit_skip             = 1,
995                 .async_size             = sizeof(struct io_timeout_data),
996         },
997         [IORING_OP_TIMEOUT_REMOVE] = {
998                 /* used by timeout updates' prep() */
999                 .audit_skip             = 1,
1000         },
1001         [IORING_OP_ACCEPT] = {
1002                 .needs_file             = 1,
1003                 .unbound_nonreg_file    = 1,
1004                 .pollin                 = 1,
1005         },
1006         [IORING_OP_ASYNC_CANCEL] = {
1007                 .audit_skip             = 1,
1008         },
1009         [IORING_OP_LINK_TIMEOUT] = {
1010                 .audit_skip             = 1,
1011                 .async_size             = sizeof(struct io_timeout_data),
1012         },
1013         [IORING_OP_CONNECT] = {
1014                 .needs_file             = 1,
1015                 .unbound_nonreg_file    = 1,
1016                 .pollout                = 1,
1017                 .needs_async_setup      = 1,
1018                 .async_size             = sizeof(struct io_async_connect),
1019         },
1020         [IORING_OP_FALLOCATE] = {
1021                 .needs_file             = 1,
1022         },
1023         [IORING_OP_OPENAT] = {},
1024         [IORING_OP_CLOSE] = {},
1025         [IORING_OP_FILES_UPDATE] = {
1026                 .audit_skip             = 1,
1027         },
1028         [IORING_OP_STATX] = {
1029                 .audit_skip             = 1,
1030         },
1031         [IORING_OP_READ] = {
1032                 .needs_file             = 1,
1033                 .unbound_nonreg_file    = 1,
1034                 .pollin                 = 1,
1035                 .buffer_select          = 1,
1036                 .plug                   = 1,
1037                 .audit_skip             = 1,
1038                 .async_size             = sizeof(struct io_async_rw),
1039         },
1040         [IORING_OP_WRITE] = {
1041                 .needs_file             = 1,
1042                 .hash_reg_file          = 1,
1043                 .unbound_nonreg_file    = 1,
1044                 .pollout                = 1,
1045                 .plug                   = 1,
1046                 .audit_skip             = 1,
1047                 .async_size             = sizeof(struct io_async_rw),
1048         },
1049         [IORING_OP_FADVISE] = {
1050                 .needs_file             = 1,
1051                 .audit_skip             = 1,
1052         },
1053         [IORING_OP_MADVISE] = {},
1054         [IORING_OP_SEND] = {
1055                 .needs_file             = 1,
1056                 .unbound_nonreg_file    = 1,
1057                 .pollout                = 1,
1058                 .audit_skip             = 1,
1059         },
1060         [IORING_OP_RECV] = {
1061                 .needs_file             = 1,
1062                 .unbound_nonreg_file    = 1,
1063                 .pollin                 = 1,
1064                 .buffer_select          = 1,
1065                 .audit_skip             = 1,
1066         },
1067         [IORING_OP_OPENAT2] = {
1068         },
1069         [IORING_OP_EPOLL_CTL] = {
1070                 .unbound_nonreg_file    = 1,
1071                 .audit_skip             = 1,
1072         },
1073         [IORING_OP_SPLICE] = {
1074                 .needs_file             = 1,
1075                 .hash_reg_file          = 1,
1076                 .unbound_nonreg_file    = 1,
1077                 .audit_skip             = 1,
1078         },
1079         [IORING_OP_PROVIDE_BUFFERS] = {
1080                 .audit_skip             = 1,
1081         },
1082         [IORING_OP_REMOVE_BUFFERS] = {
1083                 .audit_skip             = 1,
1084         },
1085         [IORING_OP_TEE] = {
1086                 .needs_file             = 1,
1087                 .hash_reg_file          = 1,
1088                 .unbound_nonreg_file    = 1,
1089                 .audit_skip             = 1,
1090         },
1091         [IORING_OP_SHUTDOWN] = {
1092                 .needs_file             = 1,
1093         },
1094         [IORING_OP_RENAMEAT] = {},
1095         [IORING_OP_UNLINKAT] = {},
1096         [IORING_OP_MKDIRAT] = {},
1097         [IORING_OP_SYMLINKAT] = {},
1098         [IORING_OP_LINKAT] = {},
1099 };
1100
1101 /* requests with any of those set should undergo io_disarm_next() */
1102 #define IO_DISARM_MASK (REQ_F_ARM_LTIMEOUT | REQ_F_LINK_TIMEOUT | REQ_F_FAIL)
1103
1104 static bool io_disarm_next(struct io_kiocb *req);
1105 static void io_uring_del_tctx_node(unsigned long index);
1106 static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
1107                                          struct task_struct *task,
1108                                          bool cancel_all);
1109 static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd);
1110
1111 static bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data,
1112                                  s32 res, u32 cflags);
1113 static void io_put_req(struct io_kiocb *req);
1114 static void io_put_req_deferred(struct io_kiocb *req);
1115 static void io_dismantle_req(struct io_kiocb *req);
1116 static void io_queue_linked_timeout(struct io_kiocb *req);
1117 static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type,
1118                                      struct io_uring_rsrc_update2 *up,
1119                                      unsigned nr_args);
1120 static void io_clean_op(struct io_kiocb *req);
1121 static struct file *io_file_get(struct io_ring_ctx *ctx,
1122                                 struct io_kiocb *req, int fd, bool fixed);
1123 static void __io_queue_sqe(struct io_kiocb *req);
1124 static void io_rsrc_put_work(struct work_struct *work);
1125
1126 static void io_req_task_queue(struct io_kiocb *req);
1127 static void __io_submit_flush_completions(struct io_ring_ctx *ctx);
1128 static int io_req_prep_async(struct io_kiocb *req);
1129
1130 static int io_install_fixed_file(struct io_kiocb *req, struct file *file,
1131                                  unsigned int issue_flags, u32 slot_index);
1132 static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags);
1133
1134 static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer);
1135
1136 static struct kmem_cache *req_cachep;
1137
1138 static const struct file_operations io_uring_fops;
1139
1140 struct sock *io_uring_get_socket(struct file *file)
1141 {
1142 #if defined(CONFIG_UNIX)
1143         if (file->f_op == &io_uring_fops) {
1144                 struct io_ring_ctx *ctx = file->private_data;
1145
1146                 return ctx->ring_sock->sk;
1147         }
1148 #endif
1149         return NULL;
1150 }
1151 EXPORT_SYMBOL(io_uring_get_socket);
1152
1153 static inline void io_tw_lock(struct io_ring_ctx *ctx, bool *locked)
1154 {
1155         if (!*locked) {
1156                 mutex_lock(&ctx->uring_lock);
1157                 *locked = true;
1158         }
1159 }
1160
1161 #define io_for_each_link(pos, head) \
1162         for (pos = (head); pos; pos = pos->link)
1163
1164 /*
1165  * Shamelessly stolen from the mm implementation of page reference checking,
1166  * see commit f958d7b528b1 for details.
1167  */
1168 #define req_ref_zero_or_close_to_overflow(req)  \
1169         ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u)
1170
1171 static inline bool req_ref_inc_not_zero(struct io_kiocb *req)
1172 {
1173         WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
1174         return atomic_inc_not_zero(&req->refs);
1175 }
1176
1177 static inline bool req_ref_put_and_test(struct io_kiocb *req)
1178 {
1179         if (likely(!(req->flags & REQ_F_REFCOUNT)))
1180                 return true;
1181
1182         WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req));
1183         return atomic_dec_and_test(&req->refs);
1184 }
1185
1186 static inline void req_ref_put(struct io_kiocb *req)
1187 {
1188         WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
1189         WARN_ON_ONCE(req_ref_put_and_test(req));
1190 }
1191
1192 static inline void req_ref_get(struct io_kiocb *req)
1193 {
1194         WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
1195         WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req));
1196         atomic_inc(&req->refs);
1197 }
1198
1199 static inline void io_submit_flush_completions(struct io_ring_ctx *ctx)
1200 {
1201         if (!wq_list_empty(&ctx->submit_state.compl_reqs))
1202                 __io_submit_flush_completions(ctx);
1203 }
1204
1205 static inline void __io_req_set_refcount(struct io_kiocb *req, int nr)
1206 {
1207         if (!(req->flags & REQ_F_REFCOUNT)) {
1208                 req->flags |= REQ_F_REFCOUNT;
1209                 atomic_set(&req->refs, nr);
1210         }
1211 }
1212
1213 static inline void io_req_set_refcount(struct io_kiocb *req)
1214 {
1215         __io_req_set_refcount(req, 1);
1216 }
1217
1218 #define IO_RSRC_REF_BATCH       100
1219
1220 static inline void io_req_put_rsrc_locked(struct io_kiocb *req,
1221                                           struct io_ring_ctx *ctx)
1222         __must_hold(&ctx->uring_lock)
1223 {
1224         struct percpu_ref *ref = req->fixed_rsrc_refs;
1225
1226         if (ref) {
1227                 if (ref == &ctx->rsrc_node->refs)
1228                         ctx->rsrc_cached_refs++;
1229                 else
1230                         percpu_ref_put(ref);
1231         }
1232 }
1233
1234 static inline void io_req_put_rsrc(struct io_kiocb *req, struct io_ring_ctx *ctx)
1235 {
1236         if (req->fixed_rsrc_refs)
1237                 percpu_ref_put(req->fixed_rsrc_refs);
1238 }
1239
1240 static __cold void io_rsrc_refs_drop(struct io_ring_ctx *ctx)
1241         __must_hold(&ctx->uring_lock)
1242 {
1243         if (ctx->rsrc_cached_refs) {
1244                 percpu_ref_put_many(&ctx->rsrc_node->refs, ctx->rsrc_cached_refs);
1245                 ctx->rsrc_cached_refs = 0;
1246         }
1247 }
1248
1249 static void io_rsrc_refs_refill(struct io_ring_ctx *ctx)
1250         __must_hold(&ctx->uring_lock)
1251 {
1252         ctx->rsrc_cached_refs += IO_RSRC_REF_BATCH;
1253         percpu_ref_get_many(&ctx->rsrc_node->refs, IO_RSRC_REF_BATCH);
1254 }
1255
1256 static inline void io_req_set_rsrc_node(struct io_kiocb *req,
1257                                         struct io_ring_ctx *ctx)
1258 {
1259         if (!req->fixed_rsrc_refs) {
1260                 req->fixed_rsrc_refs = &ctx->rsrc_node->refs;
1261                 ctx->rsrc_cached_refs--;
1262                 if (unlikely(ctx->rsrc_cached_refs < 0))
1263                         io_rsrc_refs_refill(ctx);
1264         }
1265 }
1266
1267 static void io_refs_resurrect(struct percpu_ref *ref, struct completion *compl)
1268 {
1269         bool got = percpu_ref_tryget(ref);
1270
1271         /* already at zero, wait for ->release() */
1272         if (!got)
1273                 wait_for_completion(compl);
1274         percpu_ref_resurrect(ref);
1275         if (got)
1276                 percpu_ref_put(ref);
1277 }
1278
1279 static bool io_match_task(struct io_kiocb *head, struct task_struct *task,
1280                           bool cancel_all)
1281 {
1282         struct io_kiocb *req;
1283
1284         if (task && head->task != task)
1285                 return false;
1286         if (cancel_all)
1287                 return true;
1288
1289         io_for_each_link(req, head) {
1290                 if (req->flags & REQ_F_INFLIGHT)
1291                         return true;
1292         }
1293         return false;
1294 }
1295
1296 static inline bool req_has_async_data(struct io_kiocb *req)
1297 {
1298         return req->flags & REQ_F_ASYNC_DATA;
1299 }
1300
1301 static inline void req_set_fail(struct io_kiocb *req)
1302 {
1303         req->flags |= REQ_F_FAIL;
1304 }
1305
1306 static inline void req_fail_link_node(struct io_kiocb *req, int res)
1307 {
1308         req_set_fail(req);
1309         req->result = res;
1310 }
1311
1312 static __cold void io_ring_ctx_ref_free(struct percpu_ref *ref)
1313 {
1314         struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1315
1316         complete(&ctx->ref_comp);
1317 }
1318
1319 static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1320 {
1321         return !req->timeout.off;
1322 }
1323
1324 static __cold void io_fallback_req_func(struct work_struct *work)
1325 {
1326         struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
1327                                                 fallback_work.work);
1328         struct llist_node *node = llist_del_all(&ctx->fallback_llist);
1329         struct io_kiocb *req, *tmp;
1330         bool locked = false;
1331
1332         percpu_ref_get(&ctx->refs);
1333         llist_for_each_entry_safe(req, tmp, node, io_task_work.fallback_node)
1334                 req->io_task_work.func(req, &locked);
1335
1336         if (locked) {
1337                 io_submit_flush_completions(ctx);
1338                 mutex_unlock(&ctx->uring_lock);
1339         }
1340         percpu_ref_put(&ctx->refs);
1341 }
1342
1343 static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1344 {
1345         struct io_ring_ctx *ctx;
1346         int hash_bits;
1347
1348         ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1349         if (!ctx)
1350                 return NULL;
1351
1352         /*
1353          * Use 5 bits less than the max cq entries, that should give us around
1354          * 32 entries per hash list if totally full and uniformly spread.
1355          */
1356         hash_bits = ilog2(p->cq_entries);
1357         hash_bits -= 5;
1358         if (hash_bits <= 0)
1359                 hash_bits = 1;
1360         ctx->cancel_hash_bits = hash_bits;
1361         ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1362                                         GFP_KERNEL);
1363         if (!ctx->cancel_hash)
1364                 goto err;
1365         __hash_init(ctx->cancel_hash, 1U << hash_bits);
1366
1367         ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL);
1368         if (!ctx->dummy_ubuf)
1369                 goto err;
1370         /* set invalid range, so io_import_fixed() fails meeting it */
1371         ctx->dummy_ubuf->ubuf = -1UL;
1372
1373         if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
1374                             PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1375                 goto err;
1376
1377         ctx->flags = p->flags;
1378         init_waitqueue_head(&ctx->sqo_sq_wait);
1379         INIT_LIST_HEAD(&ctx->sqd_list);
1380         INIT_LIST_HEAD(&ctx->cq_overflow_list);
1381         init_completion(&ctx->ref_comp);
1382         xa_init_flags(&ctx->io_buffers, XA_FLAGS_ALLOC1);
1383         xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1);
1384         mutex_init(&ctx->uring_lock);
1385         init_waitqueue_head(&ctx->cq_wait);
1386         spin_lock_init(&ctx->completion_lock);
1387         spin_lock_init(&ctx->timeout_lock);
1388         INIT_WQ_LIST(&ctx->iopoll_list);
1389         INIT_LIST_HEAD(&ctx->defer_list);
1390         INIT_LIST_HEAD(&ctx->timeout_list);
1391         INIT_LIST_HEAD(&ctx->ltimeout_list);
1392         spin_lock_init(&ctx->rsrc_ref_lock);
1393         INIT_LIST_HEAD(&ctx->rsrc_ref_list);
1394         INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work);
1395         init_llist_head(&ctx->rsrc_put_llist);
1396         INIT_LIST_HEAD(&ctx->tctx_list);
1397         ctx->submit_state.free_list.next = NULL;
1398         INIT_WQ_LIST(&ctx->locked_free_list);
1399         INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func);
1400         INIT_WQ_LIST(&ctx->submit_state.compl_reqs);
1401         return ctx;
1402 err:
1403         kfree(ctx->dummy_ubuf);
1404         kfree(ctx->cancel_hash);
1405         kfree(ctx);
1406         return NULL;
1407 }
1408
1409 static void io_account_cq_overflow(struct io_ring_ctx *ctx)
1410 {
1411         struct io_rings *r = ctx->rings;
1412
1413         WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1);
1414         ctx->cq_extra--;
1415 }
1416
1417 static bool req_need_defer(struct io_kiocb *req, u32 seq)
1418 {
1419         if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1420                 struct io_ring_ctx *ctx = req->ctx;
1421
1422                 return seq + READ_ONCE(ctx->cq_extra) != ctx->cached_cq_tail;
1423         }
1424
1425         return false;
1426 }
1427
1428 #define FFS_NOWAIT              0x1UL
1429 #define FFS_ISREG               0x2UL
1430 #define FFS_MASK                ~(FFS_NOWAIT|FFS_ISREG)
1431
1432 static inline bool io_req_ffs_set(struct io_kiocb *req)
1433 {
1434         return req->flags & REQ_F_FIXED_FILE;
1435 }
1436
1437 static inline void io_req_track_inflight(struct io_kiocb *req)
1438 {
1439         if (!(req->flags & REQ_F_INFLIGHT)) {
1440                 req->flags |= REQ_F_INFLIGHT;
1441                 atomic_inc(&current->io_uring->inflight_tracked);
1442         }
1443 }
1444
1445 static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req)
1446 {
1447         if (WARN_ON_ONCE(!req->link))
1448                 return NULL;
1449
1450         req->flags &= ~REQ_F_ARM_LTIMEOUT;
1451         req->flags |= REQ_F_LINK_TIMEOUT;
1452
1453         /* linked timeouts should have two refs once prep'ed */
1454         io_req_set_refcount(req);
1455         __io_req_set_refcount(req->link, 2);
1456         return req->link;
1457 }
1458
1459 static inline struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
1460 {
1461         if (likely(!(req->flags & REQ_F_ARM_LTIMEOUT)))
1462                 return NULL;
1463         return __io_prep_linked_timeout(req);
1464 }
1465
1466 static void io_prep_async_work(struct io_kiocb *req)
1467 {
1468         const struct io_op_def *def = &io_op_defs[req->opcode];
1469         struct io_ring_ctx *ctx = req->ctx;
1470
1471         if (!(req->flags & REQ_F_CREDS)) {
1472                 req->flags |= REQ_F_CREDS;
1473                 req->creds = get_current_cred();
1474         }
1475
1476         req->work.list.next = NULL;
1477         req->work.flags = 0;
1478         if (req->flags & REQ_F_FORCE_ASYNC)
1479                 req->work.flags |= IO_WQ_WORK_CONCURRENT;
1480
1481         if (req->flags & REQ_F_ISREG) {
1482                 if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL))
1483                         io_wq_hash_work(&req->work, file_inode(req->file));
1484         } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) {
1485                 if (def->unbound_nonreg_file)
1486                         req->work.flags |= IO_WQ_WORK_UNBOUND;
1487         }
1488
1489         switch (req->opcode) {
1490         case IORING_OP_SPLICE:
1491         case IORING_OP_TEE:
1492                 if (!S_ISREG(file_inode(req->splice.file_in)->i_mode))
1493                         req->work.flags |= IO_WQ_WORK_UNBOUND;
1494                 break;
1495         }
1496 }
1497
1498 static void io_prep_async_link(struct io_kiocb *req)
1499 {
1500         struct io_kiocb *cur;
1501
1502         if (req->flags & REQ_F_LINK_TIMEOUT) {
1503                 struct io_ring_ctx *ctx = req->ctx;
1504
1505                 spin_lock_irq(&ctx->timeout_lock);
1506                 io_for_each_link(cur, req)
1507                         io_prep_async_work(cur);
1508                 spin_unlock_irq(&ctx->timeout_lock);
1509         } else {
1510                 io_for_each_link(cur, req)
1511                         io_prep_async_work(cur);
1512         }
1513 }
1514
1515 static inline void io_req_add_compl_list(struct io_kiocb *req)
1516 {
1517         struct io_submit_state *state = &req->ctx->submit_state;
1518
1519         wq_list_add_tail(&req->comp_list, &state->compl_reqs);
1520 }
1521
1522 static void io_queue_async_work(struct io_kiocb *req, bool *dont_use)
1523 {
1524         struct io_ring_ctx *ctx = req->ctx;
1525         struct io_kiocb *link = io_prep_linked_timeout(req);
1526         struct io_uring_task *tctx = req->task->io_uring;
1527
1528         BUG_ON(!tctx);
1529         BUG_ON(!tctx->io_wq);
1530
1531         /* init ->work of the whole link before punting */
1532         io_prep_async_link(req);
1533
1534         /*
1535          * Not expected to happen, but if we do have a bug where this _can_
1536          * happen, catch it here and ensure the request is marked as
1537          * canceled. That will make io-wq go through the usual work cancel
1538          * procedure rather than attempt to run this request (or create a new
1539          * worker for it).
1540          */
1541         if (WARN_ON_ONCE(!same_thread_group(req->task, current)))
1542                 req->work.flags |= IO_WQ_WORK_CANCEL;
1543
1544         trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1545                                         &req->work, req->flags);
1546         io_wq_enqueue(tctx->io_wq, &req->work);
1547         if (link)
1548                 io_queue_linked_timeout(link);
1549 }
1550
1551 static void io_kill_timeout(struct io_kiocb *req, int status)
1552         __must_hold(&req->ctx->completion_lock)
1553         __must_hold(&req->ctx->timeout_lock)
1554 {
1555         struct io_timeout_data *io = req->async_data;
1556
1557         if (hrtimer_try_to_cancel(&io->timer) != -1) {
1558                 if (status)
1559                         req_set_fail(req);
1560                 atomic_set(&req->ctx->cq_timeouts,
1561                         atomic_read(&req->ctx->cq_timeouts) + 1);
1562                 list_del_init(&req->timeout.list);
1563                 io_cqring_fill_event(req->ctx, req->user_data, status, 0);
1564                 io_put_req_deferred(req);
1565         }
1566 }
1567
1568 static __cold void io_queue_deferred(struct io_ring_ctx *ctx)
1569 {
1570         while (!list_empty(&ctx->defer_list)) {
1571                 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1572                                                 struct io_defer_entry, list);
1573
1574                 if (req_need_defer(de->req, de->seq))
1575                         break;
1576                 list_del_init(&de->list);
1577                 io_req_task_queue(de->req);
1578                 kfree(de);
1579         }
1580 }
1581
1582 static __cold void io_flush_timeouts(struct io_ring_ctx *ctx)
1583         __must_hold(&ctx->completion_lock)
1584 {
1585         u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
1586
1587         spin_lock_irq(&ctx->timeout_lock);
1588         while (!list_empty(&ctx->timeout_list)) {
1589                 u32 events_needed, events_got;
1590                 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
1591                                                 struct io_kiocb, timeout.list);
1592
1593                 if (io_is_timeout_noseq(req))
1594                         break;
1595
1596                 /*
1597                  * Since seq can easily wrap around over time, subtract
1598                  * the last seq at which timeouts were flushed before comparing.
1599                  * Assuming not more than 2^31-1 events have happened since,
1600                  * these subtractions won't have wrapped, so we can check if
1601                  * target is in [last_seq, current_seq] by comparing the two.
1602                  */
1603                 events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush;
1604                 events_got = seq - ctx->cq_last_tm_flush;
1605                 if (events_got < events_needed)
1606                         break;
1607
1608                 list_del_init(&req->timeout.list);
1609                 io_kill_timeout(req, 0);
1610         }
1611         ctx->cq_last_tm_flush = seq;
1612         spin_unlock_irq(&ctx->timeout_lock);
1613 }
1614
1615 static __cold void __io_commit_cqring_flush(struct io_ring_ctx *ctx)
1616 {
1617         if (ctx->off_timeout_used)
1618                 io_flush_timeouts(ctx);
1619         if (ctx->drain_active)
1620                 io_queue_deferred(ctx);
1621 }
1622
1623 static inline void io_commit_cqring(struct io_ring_ctx *ctx)
1624 {
1625         if (unlikely(ctx->off_timeout_used || ctx->drain_active))
1626                 __io_commit_cqring_flush(ctx);
1627         /* order cqe stores with ring update */
1628         smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail);
1629 }
1630
1631 static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1632 {
1633         struct io_rings *r = ctx->rings;
1634
1635         return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == ctx->sq_entries;
1636 }
1637
1638 static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx)
1639 {
1640         return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head);
1641 }
1642
1643 static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx)
1644 {
1645         struct io_rings *rings = ctx->rings;
1646         unsigned tail, mask = ctx->cq_entries - 1;
1647
1648         /*
1649          * writes to the cq entry need to come after reading head; the
1650          * control dependency is enough as we're using WRITE_ONCE to
1651          * fill the cq entry
1652          */
1653         if (__io_cqring_events(ctx) == ctx->cq_entries)
1654                 return NULL;
1655
1656         tail = ctx->cached_cq_tail++;
1657         return &rings->cqes[tail & mask];
1658 }
1659
1660 static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1661 {
1662         if (likely(!ctx->cq_ev_fd))
1663                 return false;
1664         if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1665                 return false;
1666         return !ctx->eventfd_async || io_wq_current_is_worker();
1667 }
1668
1669 /*
1670  * This should only get called when at least one event has been posted.
1671  * Some applications rely on the eventfd notification count only changing
1672  * IFF a new CQE has been added to the CQ ring. There's no depedency on
1673  * 1:1 relationship between how many times this function is called (and
1674  * hence the eventfd count) and number of CQEs posted to the CQ ring.
1675  */
1676 static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
1677 {
1678         /*
1679          * wake_up_all() may seem excessive, but io_wake_function() and
1680          * io_should_wake() handle the termination of the loop and only
1681          * wake as many waiters as we need to.
1682          */
1683         if (wq_has_sleeper(&ctx->cq_wait))
1684                 wake_up_all(&ctx->cq_wait);
1685         if (io_should_trigger_evfd(ctx))
1686                 eventfd_signal(ctx->cq_ev_fd, 1);
1687 }
1688
1689 static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx)
1690 {
1691         /* see waitqueue_active() comment */
1692         smp_mb();
1693
1694         if (ctx->flags & IORING_SETUP_SQPOLL) {
1695                 if (waitqueue_active(&ctx->cq_wait))
1696                         wake_up_all(&ctx->cq_wait);
1697         }
1698         if (io_should_trigger_evfd(ctx))
1699                 eventfd_signal(ctx->cq_ev_fd, 1);
1700 }
1701
1702 /* Returns true if there are no backlogged entries after the flush */
1703 static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
1704 {
1705         bool all_flushed, posted;
1706
1707         if (!force && __io_cqring_events(ctx) == ctx->cq_entries)
1708                 return false;
1709
1710         posted = false;
1711         spin_lock(&ctx->completion_lock);
1712         while (!list_empty(&ctx->cq_overflow_list)) {
1713                 struct io_uring_cqe *cqe = io_get_cqe(ctx);
1714                 struct io_overflow_cqe *ocqe;
1715
1716                 if (!cqe && !force)
1717                         break;
1718                 ocqe = list_first_entry(&ctx->cq_overflow_list,
1719                                         struct io_overflow_cqe, list);
1720                 if (cqe)
1721                         memcpy(cqe, &ocqe->cqe, sizeof(*cqe));
1722                 else
1723                         io_account_cq_overflow(ctx);
1724
1725                 posted = true;
1726                 list_del(&ocqe->list);
1727                 kfree(ocqe);
1728         }
1729
1730         all_flushed = list_empty(&ctx->cq_overflow_list);
1731         if (all_flushed) {
1732                 clear_bit(0, &ctx->check_cq_overflow);
1733                 WRITE_ONCE(ctx->rings->sq_flags,
1734                            ctx->rings->sq_flags & ~IORING_SQ_CQ_OVERFLOW);
1735         }
1736
1737         if (posted)
1738                 io_commit_cqring(ctx);
1739         spin_unlock(&ctx->completion_lock);
1740         if (posted)
1741                 io_cqring_ev_posted(ctx);
1742         return all_flushed;
1743 }
1744
1745 static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx)
1746 {
1747         bool ret = true;
1748
1749         if (test_bit(0, &ctx->check_cq_overflow)) {
1750                 /* iopoll syncs against uring_lock, not completion_lock */
1751                 if (ctx->flags & IORING_SETUP_IOPOLL)
1752                         mutex_lock(&ctx->uring_lock);
1753                 ret = __io_cqring_overflow_flush(ctx, false);
1754                 if (ctx->flags & IORING_SETUP_IOPOLL)
1755                         mutex_unlock(&ctx->uring_lock);
1756         }
1757
1758         return ret;
1759 }
1760
1761 /* must to be called somewhat shortly after putting a request */
1762 static inline void io_put_task(struct task_struct *task, int nr)
1763 {
1764         struct io_uring_task *tctx = task->io_uring;
1765
1766         if (likely(task == current)) {
1767                 tctx->cached_refs += nr;
1768         } else {
1769                 percpu_counter_sub(&tctx->inflight, nr);
1770                 if (unlikely(atomic_read(&tctx->in_idle)))
1771                         wake_up(&tctx->wait);
1772                 put_task_struct_many(task, nr);
1773         }
1774 }
1775
1776 static void io_task_refs_refill(struct io_uring_task *tctx)
1777 {
1778         unsigned int refill = -tctx->cached_refs + IO_TCTX_REFS_CACHE_NR;
1779
1780         percpu_counter_add(&tctx->inflight, refill);
1781         refcount_add(refill, &current->usage);
1782         tctx->cached_refs += refill;
1783 }
1784
1785 static inline void io_get_task_refs(int nr)
1786 {
1787         struct io_uring_task *tctx = current->io_uring;
1788
1789         tctx->cached_refs -= nr;
1790         if (unlikely(tctx->cached_refs < 0))
1791                 io_task_refs_refill(tctx);
1792 }
1793
1794 static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data,
1795                                      s32 res, u32 cflags)
1796 {
1797         struct io_overflow_cqe *ocqe;
1798
1799         ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT);
1800         if (!ocqe) {
1801                 /*
1802                  * If we're in ring overflow flush mode, or in task cancel mode,
1803                  * or cannot allocate an overflow entry, then we need to drop it
1804                  * on the floor.
1805                  */
1806                 io_account_cq_overflow(ctx);
1807                 return false;
1808         }
1809         if (list_empty(&ctx->cq_overflow_list)) {
1810                 set_bit(0, &ctx->check_cq_overflow);
1811                 WRITE_ONCE(ctx->rings->sq_flags,
1812                            ctx->rings->sq_flags | IORING_SQ_CQ_OVERFLOW);
1813
1814         }
1815         ocqe->cqe.user_data = user_data;
1816         ocqe->cqe.res = res;
1817         ocqe->cqe.flags = cflags;
1818         list_add_tail(&ocqe->list, &ctx->cq_overflow_list);
1819         return true;
1820 }
1821
1822 static inline bool __io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data,
1823                                           s32 res, u32 cflags)
1824 {
1825         struct io_uring_cqe *cqe;
1826
1827         trace_io_uring_complete(ctx, user_data, res, cflags);
1828
1829         /*
1830          * If we can't get a cq entry, userspace overflowed the
1831          * submission (by quite a lot). Increment the overflow count in
1832          * the ring.
1833          */
1834         cqe = io_get_cqe(ctx);
1835         if (likely(cqe)) {
1836                 WRITE_ONCE(cqe->user_data, user_data);
1837                 WRITE_ONCE(cqe->res, res);
1838                 WRITE_ONCE(cqe->flags, cflags);
1839                 return true;
1840         }
1841         return io_cqring_event_overflow(ctx, user_data, res, cflags);
1842 }
1843
1844 /* not as hot to bloat with inlining */
1845 static noinline bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data,
1846                                           s32 res, u32 cflags)
1847 {
1848         return __io_cqring_fill_event(ctx, user_data, res, cflags);
1849 }
1850
1851 static void io_req_complete_post(struct io_kiocb *req, s32 res,
1852                                  u32 cflags)
1853 {
1854         struct io_ring_ctx *ctx = req->ctx;
1855
1856         spin_lock(&ctx->completion_lock);
1857         __io_cqring_fill_event(ctx, req->user_data, res, cflags);
1858         /*
1859          * If we're the last reference to this request, add to our locked
1860          * free_list cache.
1861          */
1862         if (req_ref_put_and_test(req)) {
1863                 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
1864                         if (req->flags & IO_DISARM_MASK)
1865                                 io_disarm_next(req);
1866                         if (req->link) {
1867                                 io_req_task_queue(req->link);
1868                                 req->link = NULL;
1869                         }
1870                 }
1871                 io_req_put_rsrc(req, ctx);
1872                 io_dismantle_req(req);
1873                 io_put_task(req->task, 1);
1874                 wq_list_add_head(&req->comp_list, &ctx->locked_free_list);
1875                 ctx->locked_free_nr++;
1876         }
1877         io_commit_cqring(ctx);
1878         spin_unlock(&ctx->completion_lock);
1879         io_cqring_ev_posted(ctx);
1880 }
1881
1882 static inline void io_req_complete_state(struct io_kiocb *req, s32 res,
1883                                          u32 cflags)
1884 {
1885         req->result = res;
1886         req->cflags = cflags;
1887         req->flags |= REQ_F_COMPLETE_INLINE;
1888 }
1889
1890 static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags,
1891                                      s32 res, u32 cflags)
1892 {
1893         if (issue_flags & IO_URING_F_COMPLETE_DEFER)
1894                 io_req_complete_state(req, res, cflags);
1895         else
1896                 io_req_complete_post(req, res, cflags);
1897 }
1898
1899 static inline void io_req_complete(struct io_kiocb *req, s32 res)
1900 {
1901         __io_req_complete(req, 0, res, 0);
1902 }
1903
1904 static void io_req_complete_failed(struct io_kiocb *req, s32 res)
1905 {
1906         req_set_fail(req);
1907         io_req_complete_post(req, res, 0);
1908 }
1909
1910 static void io_req_complete_fail_submit(struct io_kiocb *req)
1911 {
1912         /*
1913          * We don't submit, fail them all, for that replace hardlinks with
1914          * normal links. Extra REQ_F_LINK is tolerated.
1915          */
1916         req->flags &= ~REQ_F_HARDLINK;
1917         req->flags |= REQ_F_LINK;
1918         io_req_complete_failed(req, req->result);
1919 }
1920
1921 /*
1922  * Don't initialise the fields below on every allocation, but do that in
1923  * advance and keep them valid across allocations.
1924  */
1925 static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx)
1926 {
1927         req->ctx = ctx;
1928         req->link = NULL;
1929         req->async_data = NULL;
1930         /* not necessary, but safer to zero */
1931         req->result = 0;
1932 }
1933
1934 static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx,
1935                                         struct io_submit_state *state)
1936 {
1937         spin_lock(&ctx->completion_lock);
1938         wq_list_splice(&ctx->locked_free_list, &state->free_list);
1939         ctx->locked_free_nr = 0;
1940         spin_unlock(&ctx->completion_lock);
1941 }
1942
1943 /* Returns true IFF there are requests in the cache */
1944 static bool io_flush_cached_reqs(struct io_ring_ctx *ctx)
1945 {
1946         struct io_submit_state *state = &ctx->submit_state;
1947
1948         /*
1949          * If we have more than a batch's worth of requests in our IRQ side
1950          * locked cache, grab the lock and move them over to our submission
1951          * side cache.
1952          */
1953         if (READ_ONCE(ctx->locked_free_nr) > IO_COMPL_BATCH)
1954                 io_flush_cached_locked_reqs(ctx, state);
1955         return !!state->free_list.next;
1956 }
1957
1958 /*
1959  * A request might get retired back into the request caches even before opcode
1960  * handlers and io_issue_sqe() are done with it, e.g. inline completion path.
1961  * Because of that, io_alloc_req() should be called only under ->uring_lock
1962  * and with extra caution to not get a request that is still worked on.
1963  */
1964 static __cold bool __io_alloc_req_refill(struct io_ring_ctx *ctx)
1965         __must_hold(&ctx->uring_lock)
1966 {
1967         struct io_submit_state *state = &ctx->submit_state;
1968         gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
1969         void *reqs[IO_REQ_ALLOC_BATCH];
1970         struct io_kiocb *req;
1971         int ret, i;
1972
1973         if (likely(state->free_list.next || io_flush_cached_reqs(ctx)))
1974                 return true;
1975
1976         ret = kmem_cache_alloc_bulk(req_cachep, gfp, ARRAY_SIZE(reqs), reqs);
1977
1978         /*
1979          * Bulk alloc is all-or-nothing. If we fail to get a batch,
1980          * retry single alloc to be on the safe side.
1981          */
1982         if (unlikely(ret <= 0)) {
1983                 reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1984                 if (!reqs[0])
1985                         return false;
1986                 ret = 1;
1987         }
1988
1989         percpu_ref_get_many(&ctx->refs, ret);
1990         for (i = 0; i < ret; i++) {
1991                 req = reqs[i];
1992
1993                 io_preinit_req(req, ctx);
1994                 wq_stack_add_head(&req->comp_list, &state->free_list);
1995         }
1996         return true;
1997 }
1998
1999 static inline bool io_alloc_req_refill(struct io_ring_ctx *ctx)
2000 {
2001         if (unlikely(!ctx->submit_state.free_list.next))
2002                 return __io_alloc_req_refill(ctx);
2003         return true;
2004 }
2005
2006 static inline struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx)
2007 {
2008         struct io_wq_work_node *node;
2009
2010         node = wq_stack_extract(&ctx->submit_state.free_list);
2011         return container_of(node, struct io_kiocb, comp_list);
2012 }
2013
2014 static inline void io_put_file(struct file *file)
2015 {
2016         if (file)
2017                 fput(file);
2018 }
2019
2020 static inline void io_dismantle_req(struct io_kiocb *req)
2021 {
2022         unsigned int flags = req->flags;
2023
2024         if (unlikely(flags & IO_REQ_CLEAN_FLAGS))
2025                 io_clean_op(req);
2026         if (!(flags & REQ_F_FIXED_FILE))
2027                 io_put_file(req->file);
2028 }
2029
2030 static __cold void __io_free_req(struct io_kiocb *req)
2031 {
2032         struct io_ring_ctx *ctx = req->ctx;
2033
2034         io_req_put_rsrc(req, ctx);
2035         io_dismantle_req(req);
2036         io_put_task(req->task, 1);
2037
2038         spin_lock(&ctx->completion_lock);
2039         wq_list_add_head(&req->comp_list, &ctx->locked_free_list);
2040         ctx->locked_free_nr++;
2041         spin_unlock(&ctx->completion_lock);
2042 }
2043
2044 static inline void io_remove_next_linked(struct io_kiocb *req)
2045 {
2046         struct io_kiocb *nxt = req->link;
2047
2048         req->link = nxt->link;
2049         nxt->link = NULL;
2050 }
2051
2052 static bool io_kill_linked_timeout(struct io_kiocb *req)
2053         __must_hold(&req->ctx->completion_lock)
2054         __must_hold(&req->ctx->timeout_lock)
2055 {
2056         struct io_kiocb *link = req->link;
2057
2058         if (link && link->opcode == IORING_OP_LINK_TIMEOUT) {
2059                 struct io_timeout_data *io = link->async_data;
2060
2061                 io_remove_next_linked(req);
2062                 link->timeout.head = NULL;
2063                 if (hrtimer_try_to_cancel(&io->timer) != -1) {
2064                         list_del(&link->timeout.list);
2065                         io_cqring_fill_event(link->ctx, link->user_data,
2066                                              -ECANCELED, 0);
2067                         io_put_req_deferred(link);
2068                         return true;
2069                 }
2070         }
2071         return false;
2072 }
2073
2074 static void io_fail_links(struct io_kiocb *req)
2075         __must_hold(&req->ctx->completion_lock)
2076 {
2077         struct io_kiocb *nxt, *link = req->link;
2078
2079         req->link = NULL;
2080         while (link) {
2081                 long res = -ECANCELED;
2082
2083                 if (link->flags & REQ_F_FAIL)
2084                         res = link->result;
2085
2086                 nxt = link->link;
2087                 link->link = NULL;
2088
2089                 trace_io_uring_fail_link(req, link);
2090                 io_cqring_fill_event(link->ctx, link->user_data, res, 0);
2091                 io_put_req_deferred(link);
2092                 link = nxt;
2093         }
2094 }
2095
2096 static bool io_disarm_next(struct io_kiocb *req)
2097         __must_hold(&req->ctx->completion_lock)
2098 {
2099         bool posted = false;
2100
2101         if (req->flags & REQ_F_ARM_LTIMEOUT) {
2102                 struct io_kiocb *link = req->link;
2103
2104                 req->flags &= ~REQ_F_ARM_LTIMEOUT;
2105                 if (link && link->opcode == IORING_OP_LINK_TIMEOUT) {
2106                         io_remove_next_linked(req);
2107                         io_cqring_fill_event(link->ctx, link->user_data,
2108                                              -ECANCELED, 0);
2109                         io_put_req_deferred(link);
2110                         posted = true;
2111                 }
2112         } else if (req->flags & REQ_F_LINK_TIMEOUT) {
2113                 struct io_ring_ctx *ctx = req->ctx;
2114
2115                 spin_lock_irq(&ctx->timeout_lock);
2116                 posted = io_kill_linked_timeout(req);
2117                 spin_unlock_irq(&ctx->timeout_lock);
2118         }
2119         if (unlikely((req->flags & REQ_F_FAIL) &&
2120                      !(req->flags & REQ_F_HARDLINK))) {
2121                 posted |= (req->link != NULL);
2122                 io_fail_links(req);
2123         }
2124         return posted;
2125 }
2126
2127 static void __io_req_find_next_prep(struct io_kiocb *req)
2128 {
2129         struct io_ring_ctx *ctx = req->ctx;
2130         bool posted;
2131
2132         spin_lock(&ctx->completion_lock);
2133         posted = io_disarm_next(req);
2134         if (posted)
2135                 io_commit_cqring(req->ctx);
2136         spin_unlock(&ctx->completion_lock);
2137         if (posted)
2138                 io_cqring_ev_posted(ctx);
2139 }
2140
2141 static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req)
2142 {
2143         struct io_kiocb *nxt;
2144
2145         if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK))))
2146                 return NULL;
2147         /*
2148          * If LINK is set, we have dependent requests in this chain. If we
2149          * didn't fail this request, queue the first one up, moving any other
2150          * dependencies to the next request. In case of failure, fail the rest
2151          * of the chain.
2152          */
2153         if (unlikely(req->flags & IO_DISARM_MASK))
2154                 __io_req_find_next_prep(req);
2155         nxt = req->link;
2156         req->link = NULL;
2157         return nxt;
2158 }
2159
2160 static void ctx_flush_and_put(struct io_ring_ctx *ctx, bool *locked)
2161 {
2162         if (!ctx)
2163                 return;
2164         if (*locked) {
2165                 io_submit_flush_completions(ctx);
2166                 mutex_unlock(&ctx->uring_lock);
2167                 *locked = false;
2168         }
2169         percpu_ref_put(&ctx->refs);
2170 }
2171
2172 static void tctx_task_work(struct callback_head *cb)
2173 {
2174         bool locked = false;
2175         struct io_ring_ctx *ctx = NULL;
2176         struct io_uring_task *tctx = container_of(cb, struct io_uring_task,
2177                                                   task_work);
2178
2179         while (1) {
2180                 struct io_wq_work_node *node;
2181
2182                 if (!tctx->task_list.first && locked)
2183                         io_submit_flush_completions(ctx);
2184
2185                 spin_lock_irq(&tctx->task_lock);
2186                 node = tctx->task_list.first;
2187                 INIT_WQ_LIST(&tctx->task_list);
2188                 if (!node)
2189                         tctx->task_running = false;
2190                 spin_unlock_irq(&tctx->task_lock);
2191                 if (!node)
2192                         break;
2193
2194                 do {
2195                         struct io_wq_work_node *next = node->next;
2196                         struct io_kiocb *req = container_of(node, struct io_kiocb,
2197                                                             io_task_work.node);
2198
2199                         if (req->ctx != ctx) {
2200                                 ctx_flush_and_put(ctx, &locked);
2201                                 ctx = req->ctx;
2202                                 /* if not contended, grab and improve batching */
2203                                 locked = mutex_trylock(&ctx->uring_lock);
2204                                 percpu_ref_get(&ctx->refs);
2205                         }
2206                         req->io_task_work.func(req, &locked);
2207                         node = next;
2208                 } while (node);
2209
2210                 cond_resched();
2211         }
2212
2213         ctx_flush_and_put(ctx, &locked);
2214 }
2215
2216 static void io_req_task_work_add(struct io_kiocb *req)
2217 {
2218         struct task_struct *tsk = req->task;
2219         struct io_uring_task *tctx = tsk->io_uring;
2220         enum task_work_notify_mode notify;
2221         struct io_wq_work_node *node;
2222         unsigned long flags;
2223         bool running;
2224
2225         WARN_ON_ONCE(!tctx);
2226
2227         spin_lock_irqsave(&tctx->task_lock, flags);
2228         wq_list_add_tail(&req->io_task_work.node, &tctx->task_list);
2229         running = tctx->task_running;
2230         if (!running)
2231                 tctx->task_running = true;
2232         spin_unlock_irqrestore(&tctx->task_lock, flags);
2233
2234         /* task_work already pending, we're done */
2235         if (running)
2236                 return;
2237
2238         /*
2239          * SQPOLL kernel thread doesn't need notification, just a wakeup. For
2240          * all other cases, use TWA_SIGNAL unconditionally to ensure we're
2241          * processing task_work. There's no reliable way to tell if TWA_RESUME
2242          * will do the job.
2243          */
2244         notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL;
2245         if (likely(!task_work_add(tsk, &tctx->task_work, notify))) {
2246                 if (notify == TWA_NONE)
2247                         wake_up_process(tsk);
2248                 return;
2249         }
2250
2251         spin_lock_irqsave(&tctx->task_lock, flags);
2252         tctx->task_running = false;
2253         node = tctx->task_list.first;
2254         INIT_WQ_LIST(&tctx->task_list);
2255         spin_unlock_irqrestore(&tctx->task_lock, flags);
2256
2257         while (node) {
2258                 req = container_of(node, struct io_kiocb, io_task_work.node);
2259                 node = node->next;
2260                 if (llist_add(&req->io_task_work.fallback_node,
2261                               &req->ctx->fallback_llist))
2262                         schedule_delayed_work(&req->ctx->fallback_work, 1);
2263         }
2264 }
2265
2266 static void io_req_task_cancel(struct io_kiocb *req, bool *locked)
2267 {
2268         struct io_ring_ctx *ctx = req->ctx;
2269
2270         /* not needed for normal modes, but SQPOLL depends on it */
2271         io_tw_lock(ctx, locked);
2272         io_req_complete_failed(req, req->result);
2273 }
2274
2275 static void io_req_task_submit(struct io_kiocb *req, bool *locked)
2276 {
2277         struct io_ring_ctx *ctx = req->ctx;
2278
2279         io_tw_lock(ctx, locked);
2280         /* req->task == current here, checking PF_EXITING is safe */
2281         if (likely(!(req->task->flags & PF_EXITING)))
2282                 __io_queue_sqe(req);
2283         else
2284                 io_req_complete_failed(req, -EFAULT);
2285 }
2286
2287 static void io_req_task_queue_fail(struct io_kiocb *req, int ret)
2288 {
2289         req->result = ret;
2290         req->io_task_work.func = io_req_task_cancel;
2291         io_req_task_work_add(req);
2292 }
2293
2294 static void io_req_task_queue(struct io_kiocb *req)
2295 {
2296         req->io_task_work.func = io_req_task_submit;
2297         io_req_task_work_add(req);
2298 }
2299
2300 static void io_req_task_queue_reissue(struct io_kiocb *req)
2301 {
2302         req->io_task_work.func = io_queue_async_work;
2303         io_req_task_work_add(req);
2304 }
2305
2306 static inline void io_queue_next(struct io_kiocb *req)
2307 {
2308         struct io_kiocb *nxt = io_req_find_next(req);
2309
2310         if (nxt)
2311                 io_req_task_queue(nxt);
2312 }
2313
2314 static void io_free_req(struct io_kiocb *req)
2315 {
2316         io_queue_next(req);
2317         __io_free_req(req);
2318 }
2319
2320 static void io_free_req_work(struct io_kiocb *req, bool *locked)
2321 {
2322         io_free_req(req);
2323 }
2324
2325 static void io_free_batch_list(struct io_ring_ctx *ctx,
2326                                 struct io_wq_work_node *node)
2327         __must_hold(&ctx->uring_lock)
2328 {
2329         struct task_struct *task = NULL;
2330         int task_refs = 0;
2331
2332         do {
2333                 struct io_kiocb *req = container_of(node, struct io_kiocb,
2334                                                     comp_list);
2335
2336                 if (unlikely(req->flags & REQ_F_REFCOUNT)) {
2337                         node = req->comp_list.next;
2338                         if (!req_ref_put_and_test(req))
2339                                 continue;
2340                 }
2341
2342                 io_req_put_rsrc_locked(req, ctx);
2343                 io_queue_next(req);
2344                 io_dismantle_req(req);
2345
2346                 if (req->task != task) {
2347                         if (task)
2348                                 io_put_task(task, task_refs);
2349                         task = req->task;
2350                         task_refs = 0;
2351                 }
2352                 task_refs++;
2353                 node = req->comp_list.next;
2354                 wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list);
2355         } while (node);
2356
2357         if (task)
2358                 io_put_task(task, task_refs);
2359 }
2360
2361 static void __io_submit_flush_completions(struct io_ring_ctx *ctx)
2362         __must_hold(&ctx->uring_lock)
2363 {
2364         struct io_wq_work_node *node, *prev;
2365         struct io_submit_state *state = &ctx->submit_state;
2366
2367         spin_lock(&ctx->completion_lock);
2368         wq_list_for_each(node, prev, &state->compl_reqs) {
2369                 struct io_kiocb *req = container_of(node, struct io_kiocb,
2370                                                     comp_list);
2371
2372                 __io_cqring_fill_event(ctx, req->user_data, req->result,
2373                                         req->cflags);
2374         }
2375         io_commit_cqring(ctx);
2376         spin_unlock(&ctx->completion_lock);
2377         io_cqring_ev_posted(ctx);
2378
2379         io_free_batch_list(ctx, state->compl_reqs.first);
2380         INIT_WQ_LIST(&state->compl_reqs);
2381 }
2382
2383 /*
2384  * Drop reference to request, return next in chain (if there is one) if this
2385  * was the last reference to this request.
2386  */
2387 static inline struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
2388 {
2389         struct io_kiocb *nxt = NULL;
2390
2391         if (req_ref_put_and_test(req)) {
2392                 nxt = io_req_find_next(req);
2393                 __io_free_req(req);
2394         }
2395         return nxt;
2396 }
2397
2398 static inline void io_put_req(struct io_kiocb *req)
2399 {
2400         if (req_ref_put_and_test(req))
2401                 io_free_req(req);
2402 }
2403
2404 static inline void io_put_req_deferred(struct io_kiocb *req)
2405 {
2406         if (req_ref_put_and_test(req)) {
2407                 req->io_task_work.func = io_free_req_work;
2408                 io_req_task_work_add(req);
2409         }
2410 }
2411
2412 static unsigned io_cqring_events(struct io_ring_ctx *ctx)
2413 {
2414         /* See comment at the top of this file */
2415         smp_rmb();
2416         return __io_cqring_events(ctx);
2417 }
2418
2419 static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2420 {
2421         struct io_rings *rings = ctx->rings;
2422
2423         /* make sure SQ entry isn't read before tail */
2424         return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2425 }
2426
2427 static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
2428 {
2429         unsigned int cflags;
2430
2431         cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2432         cflags |= IORING_CQE_F_BUFFER;
2433         req->flags &= ~REQ_F_BUFFER_SELECTED;
2434         kfree(kbuf);
2435         return cflags;
2436 }
2437
2438 static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2439 {
2440         if (likely(!(req->flags & REQ_F_BUFFER_SELECTED)))
2441                 return 0;
2442         return io_put_kbuf(req, req->kbuf);
2443 }
2444
2445 static inline bool io_run_task_work(void)
2446 {
2447         if (test_thread_flag(TIF_NOTIFY_SIGNAL) || current->task_works) {
2448                 __set_current_state(TASK_RUNNING);
2449                 tracehook_notify_signal();
2450                 return true;
2451         }
2452
2453         return false;
2454 }
2455
2456 static int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin)
2457 {
2458         struct io_wq_work_node *pos, *start, *prev;
2459         unsigned int poll_flags = BLK_POLL_NOSLEEP;
2460         DEFINE_IO_COMP_BATCH(iob);
2461         int nr_events = 0;
2462
2463         /*
2464          * Only spin for completions if we don't have multiple devices hanging
2465          * off our complete list.
2466          */
2467         if (ctx->poll_multi_queue || force_nonspin)
2468                 poll_flags |= BLK_POLL_ONESHOT;
2469
2470         wq_list_for_each(pos, start, &ctx->iopoll_list) {
2471                 struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list);
2472                 struct kiocb *kiocb = &req->rw.kiocb;
2473                 int ret;
2474
2475                 /*
2476                  * Move completed and retryable entries to our local lists.
2477                  * If we find a request that requires polling, break out
2478                  * and complete those lists first, if we have entries there.
2479                  */
2480                 if (READ_ONCE(req->iopoll_completed))
2481                         break;
2482
2483                 ret = kiocb->ki_filp->f_op->iopoll(kiocb, &iob, poll_flags);
2484                 if (unlikely(ret < 0))
2485                         return ret;
2486                 else if (ret)
2487                         poll_flags |= BLK_POLL_ONESHOT;
2488
2489                 /* iopoll may have completed current req */
2490                 if (!rq_list_empty(iob.req_list) ||
2491                     READ_ONCE(req->iopoll_completed))
2492                         break;
2493         }
2494
2495         if (!rq_list_empty(iob.req_list))
2496                 iob.complete(&iob);
2497         else if (!pos)
2498                 return 0;
2499
2500         prev = start;
2501         wq_list_for_each_resume(pos, prev) {
2502                 struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list);
2503
2504                 /* order with io_complete_rw_iopoll(), e.g. ->result updates */
2505                 if (!smp_load_acquire(&req->iopoll_completed))
2506                         break;
2507                 __io_cqring_fill_event(ctx, req->user_data, req->result,
2508                                         io_put_rw_kbuf(req));
2509                 nr_events++;
2510         }
2511
2512         if (unlikely(!nr_events))
2513                 return 0;
2514
2515         io_commit_cqring(ctx);
2516         io_cqring_ev_posted_iopoll(ctx);
2517         pos = start ? start->next : ctx->iopoll_list.first;
2518         wq_list_cut(&ctx->iopoll_list, prev, start);
2519         io_free_batch_list(ctx, pos);
2520         return nr_events;
2521 }
2522
2523 /*
2524  * We can't just wait for polled events to come to us, we have to actively
2525  * find and complete them.
2526  */
2527 static __cold void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
2528 {
2529         if (!(ctx->flags & IORING_SETUP_IOPOLL))
2530                 return;
2531
2532         mutex_lock(&ctx->uring_lock);
2533         while (!wq_list_empty(&ctx->iopoll_list)) {
2534                 /* let it sleep and repeat later if can't complete a request */
2535                 if (io_do_iopoll(ctx, true) == 0)
2536                         break;
2537                 /*
2538                  * Ensure we allow local-to-the-cpu processing to take place,
2539                  * in this case we need to ensure that we reap all events.
2540                  * Also let task_work, etc. to progress by releasing the mutex
2541                  */
2542                 if (need_resched()) {
2543                         mutex_unlock(&ctx->uring_lock);
2544                         cond_resched();
2545                         mutex_lock(&ctx->uring_lock);
2546                 }
2547         }
2548         mutex_unlock(&ctx->uring_lock);
2549 }
2550
2551 static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
2552 {
2553         unsigned int nr_events = 0;
2554         int ret = 0;
2555
2556         /*
2557          * We disallow the app entering submit/complete with polling, but we
2558          * still need to lock the ring to prevent racing with polled issue
2559          * that got punted to a workqueue.
2560          */
2561         mutex_lock(&ctx->uring_lock);
2562         /*
2563          * Don't enter poll loop if we already have events pending.
2564          * If we do, we can potentially be spinning for commands that
2565          * already triggered a CQE (eg in error).
2566          */
2567         if (test_bit(0, &ctx->check_cq_overflow))
2568                 __io_cqring_overflow_flush(ctx, false);
2569         if (io_cqring_events(ctx))
2570                 goto out;
2571         do {
2572                 /*
2573                  * If a submit got punted to a workqueue, we can have the
2574                  * application entering polling for a command before it gets
2575                  * issued. That app will hold the uring_lock for the duration
2576                  * of the poll right here, so we need to take a breather every
2577                  * now and then to ensure that the issue has a chance to add
2578                  * the poll to the issued list. Otherwise we can spin here
2579                  * forever, while the workqueue is stuck trying to acquire the
2580                  * very same mutex.
2581                  */
2582                 if (wq_list_empty(&ctx->iopoll_list)) {
2583                         u32 tail = ctx->cached_cq_tail;
2584
2585                         mutex_unlock(&ctx->uring_lock);
2586                         io_run_task_work();
2587                         mutex_lock(&ctx->uring_lock);
2588
2589                         /* some requests don't go through iopoll_list */
2590                         if (tail != ctx->cached_cq_tail ||
2591                             wq_list_empty(&ctx->iopoll_list))
2592                                 break;
2593                 }
2594                 ret = io_do_iopoll(ctx, !min);
2595                 if (ret < 0)
2596                         break;
2597                 nr_events += ret;
2598                 ret = 0;
2599         } while (nr_events < min && !need_resched());
2600 out:
2601         mutex_unlock(&ctx->uring_lock);
2602         return ret;
2603 }
2604
2605 static void kiocb_end_write(struct io_kiocb *req)
2606 {
2607         /*
2608          * Tell lockdep we inherited freeze protection from submission
2609          * thread.
2610          */
2611         if (req->flags & REQ_F_ISREG) {
2612                 struct super_block *sb = file_inode(req->file)->i_sb;
2613
2614                 __sb_writers_acquired(sb, SB_FREEZE_WRITE);
2615                 sb_end_write(sb);
2616         }
2617 }
2618
2619 #ifdef CONFIG_BLOCK
2620 static bool io_resubmit_prep(struct io_kiocb *req)
2621 {
2622         struct io_async_rw *rw = req->async_data;
2623
2624         if (!req_has_async_data(req))
2625                 return !io_req_prep_async(req);
2626         iov_iter_restore(&rw->s.iter, &rw->s.iter_state);
2627         return true;
2628 }
2629
2630 static bool io_rw_should_reissue(struct io_kiocb *req)
2631 {
2632         umode_t mode = file_inode(req->file)->i_mode;
2633         struct io_ring_ctx *ctx = req->ctx;
2634
2635         if (!S_ISBLK(mode) && !S_ISREG(mode))
2636                 return false;
2637         if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() &&
2638             !(ctx->flags & IORING_SETUP_IOPOLL)))
2639                 return false;
2640         /*
2641          * If ref is dying, we might be running poll reap from the exit work.
2642          * Don't attempt to reissue from that path, just let it fail with
2643          * -EAGAIN.
2644          */
2645         if (percpu_ref_is_dying(&ctx->refs))
2646                 return false;
2647         /*
2648          * Play it safe and assume not safe to re-import and reissue if we're
2649          * not in the original thread group (or in task context).
2650          */
2651         if (!same_thread_group(req->task, current) || !in_task())
2652                 return false;
2653         return true;
2654 }
2655 #else
2656 static bool io_resubmit_prep(struct io_kiocb *req)
2657 {
2658         return false;
2659 }
2660 static bool io_rw_should_reissue(struct io_kiocb *req)
2661 {
2662         return false;
2663 }
2664 #endif
2665
2666 static bool __io_complete_rw_common(struct io_kiocb *req, long res)
2667 {
2668         if (req->rw.kiocb.ki_flags & IOCB_WRITE)
2669                 kiocb_end_write(req);
2670         if (unlikely(res != req->result)) {
2671                 if ((res == -EAGAIN || res == -EOPNOTSUPP) &&
2672                     io_rw_should_reissue(req)) {
2673                         req->flags |= REQ_F_REISSUE;
2674                         return true;
2675                 }
2676                 req_set_fail(req);
2677                 req->result = res;
2678         }
2679         return false;
2680 }
2681
2682 static void io_req_task_complete(struct io_kiocb *req, bool *locked)
2683 {
2684         unsigned int cflags = io_put_rw_kbuf(req);
2685         int res = req->result;
2686
2687         if (*locked) {
2688                 io_req_complete_state(req, res, cflags);
2689                 io_req_add_compl_list(req);
2690         } else {
2691                 io_req_complete_post(req, res, cflags);
2692         }
2693 }
2694
2695 static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2696                              unsigned int issue_flags)
2697 {
2698         if (__io_complete_rw_common(req, res))
2699                 return;
2700         __io_req_complete(req, issue_flags, req->result, io_put_rw_kbuf(req));
2701 }
2702
2703 static void io_complete_rw(struct kiocb *kiocb, long res)
2704 {
2705         struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
2706
2707         if (__io_complete_rw_common(req, res))
2708                 return;
2709         req->result = res;
2710         req->io_task_work.func = io_req_task_complete;
2711         io_req_task_work_add(req);
2712 }
2713
2714 static void io_complete_rw_iopoll(struct kiocb *kiocb, long res)
2715 {
2716         struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
2717
2718         if (kiocb->ki_flags & IOCB_WRITE)
2719                 kiocb_end_write(req);
2720         if (unlikely(res != req->result)) {
2721                 if (res == -EAGAIN && io_rw_should_reissue(req)) {
2722                         req->flags |= REQ_F_REISSUE;
2723                         return;
2724                 }
2725                 req->result = res;
2726         }
2727
2728         /* order with io_iopoll_complete() checking ->iopoll_completed */
2729         smp_store_release(&req->iopoll_completed, 1);
2730 }
2731
2732 /*
2733  * After the iocb has been issued, it's safe to be found on the poll list.
2734  * Adding the kiocb to the list AFTER submission ensures that we don't
2735  * find it from a io_do_iopoll() thread before the issuer is done
2736  * accessing the kiocb cookie.
2737  */
2738 static void io_iopoll_req_issued(struct io_kiocb *req, unsigned int issue_flags)
2739 {
2740         struct io_ring_ctx *ctx = req->ctx;
2741         const bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
2742
2743         /* workqueue context doesn't hold uring_lock, grab it now */
2744         if (unlikely(needs_lock))
2745                 mutex_lock(&ctx->uring_lock);
2746
2747         /*
2748          * Track whether we have multiple files in our lists. This will impact
2749          * how we do polling eventually, not spinning if we're on potentially
2750          * different devices.
2751          */
2752         if (wq_list_empty(&ctx->iopoll_list)) {
2753                 ctx->poll_multi_queue = false;
2754         } else if (!ctx->poll_multi_queue) {
2755                 struct io_kiocb *list_req;
2756
2757                 list_req = container_of(ctx->iopoll_list.first, struct io_kiocb,
2758                                         comp_list);
2759                 if (list_req->file != req->file)
2760                         ctx->poll_multi_queue = true;
2761         }
2762
2763         /*
2764          * For fast devices, IO may have already completed. If it has, add
2765          * it to the front so we find it first.
2766          */
2767         if (READ_ONCE(req->iopoll_completed))
2768                 wq_list_add_head(&req->comp_list, &ctx->iopoll_list);
2769         else
2770                 wq_list_add_tail(&req->comp_list, &ctx->iopoll_list);
2771
2772         if (unlikely(needs_lock)) {
2773                 /*
2774                  * If IORING_SETUP_SQPOLL is enabled, sqes are either handle
2775                  * in sq thread task context or in io worker task context. If
2776                  * current task context is sq thread, we don't need to check
2777                  * whether should wake up sq thread.
2778                  */
2779                 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
2780                     wq_has_sleeper(&ctx->sq_data->wait))
2781                         wake_up(&ctx->sq_data->wait);
2782
2783                 mutex_unlock(&ctx->uring_lock);
2784         }
2785 }
2786
2787 static bool io_bdev_nowait(struct block_device *bdev)
2788 {
2789         return !bdev || blk_queue_nowait(bdev_get_queue(bdev));
2790 }
2791
2792 /*
2793  * If we tracked the file through the SCM inflight mechanism, we could support
2794  * any file. For now, just ensure that anything potentially problematic is done
2795  * inline.
2796  */
2797 static bool __io_file_supports_nowait(struct file *file, umode_t mode)
2798 {
2799         if (S_ISBLK(mode)) {
2800                 if (IS_ENABLED(CONFIG_BLOCK) &&
2801                     io_bdev_nowait(I_BDEV(file->f_mapping->host)))
2802                         return true;
2803                 return false;
2804         }
2805         if (S_ISSOCK(mode))
2806                 return true;
2807         if (S_ISREG(mode)) {
2808                 if (IS_ENABLED(CONFIG_BLOCK) &&
2809                     io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
2810                     file->f_op != &io_uring_fops)
2811                         return true;
2812                 return false;
2813         }
2814
2815         /* any ->read/write should understand O_NONBLOCK */
2816         if (file->f_flags & O_NONBLOCK)
2817                 return true;
2818         return file->f_mode & FMODE_NOWAIT;
2819 }
2820
2821 /*
2822  * If we tracked the file through the SCM inflight mechanism, we could support
2823  * any file. For now, just ensure that anything potentially problematic is done
2824  * inline.
2825  */
2826 static unsigned int io_file_get_flags(struct file *file)
2827 {
2828         umode_t mode = file_inode(file)->i_mode;
2829         unsigned int res = 0;
2830
2831         if (S_ISREG(mode))
2832                 res |= FFS_ISREG;
2833         if (__io_file_supports_nowait(file, mode))
2834                 res |= FFS_NOWAIT;
2835         return res;
2836 }
2837
2838 static inline bool io_file_supports_nowait(struct io_kiocb *req)
2839 {
2840         return req->flags & REQ_F_SUPPORT_NOWAIT;
2841 }
2842
2843 static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2844 {
2845         struct io_ring_ctx *ctx = req->ctx;
2846         struct kiocb *kiocb = &req->rw.kiocb;
2847         struct file *file = req->file;
2848         unsigned ioprio;
2849         int ret;
2850
2851         if (!io_req_ffs_set(req))
2852                 req->flags |= io_file_get_flags(file) << REQ_F_SUPPORT_NOWAIT_BIT;
2853
2854         kiocb->ki_pos = READ_ONCE(sqe->off);
2855         if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) {
2856                 req->flags |= REQ_F_CUR_POS;
2857                 kiocb->ki_pos = file->f_pos;
2858         }
2859         kiocb->ki_flags = iocb_flags(file);
2860         ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2861         if (unlikely(ret))
2862                 return ret;
2863
2864         /*
2865          * If the file is marked O_NONBLOCK, still allow retry for it if it
2866          * supports async. Otherwise it's impossible to use O_NONBLOCK files
2867          * reliably. If not, or it IOCB_NOWAIT is set, don't retry.
2868          */
2869         if ((kiocb->ki_flags & IOCB_NOWAIT) ||
2870             ((file->f_flags & O_NONBLOCK) && !io_file_supports_nowait(req)))
2871                 req->flags |= REQ_F_NOWAIT;
2872
2873         if (ctx->flags & IORING_SETUP_IOPOLL) {
2874                 if (!(kiocb->ki_flags & IOCB_DIRECT) || !file->f_op->iopoll)
2875                         return -EOPNOTSUPP;
2876
2877                 kiocb->ki_flags |= IOCB_HIPRI | IOCB_ALLOC_CACHE;
2878                 kiocb->ki_complete = io_complete_rw_iopoll;
2879                 req->iopoll_completed = 0;
2880         } else {
2881                 if (kiocb->ki_flags & IOCB_HIPRI)
2882                         return -EINVAL;
2883                 kiocb->ki_complete = io_complete_rw;
2884         }
2885
2886         ioprio = READ_ONCE(sqe->ioprio);
2887         if (ioprio) {
2888                 ret = ioprio_check_cap(ioprio);
2889                 if (ret)
2890                         return ret;
2891
2892                 kiocb->ki_ioprio = ioprio;
2893         } else {
2894                 kiocb->ki_ioprio = get_current_ioprio();
2895         }
2896
2897         req->imu = NULL;
2898         req->rw.addr = READ_ONCE(sqe->addr);
2899         req->rw.len = READ_ONCE(sqe->len);
2900         req->buf_index = READ_ONCE(sqe->buf_index);
2901         return 0;
2902 }
2903
2904 static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2905 {
2906         switch (ret) {
2907         case -EIOCBQUEUED:
2908                 break;
2909         case -ERESTARTSYS:
2910         case -ERESTARTNOINTR:
2911         case -ERESTARTNOHAND:
2912         case -ERESTART_RESTARTBLOCK:
2913                 /*
2914                  * We can't just restart the syscall, since previously
2915                  * submitted sqes may already be in progress. Just fail this
2916                  * IO with EINTR.
2917                  */
2918                 ret = -EINTR;
2919                 fallthrough;
2920         default:
2921                 kiocb->ki_complete(kiocb, ret);
2922         }
2923 }
2924
2925 static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
2926                        unsigned int issue_flags)
2927 {
2928         struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
2929         struct io_async_rw *io = req->async_data;
2930
2931         /* add previously done IO, if any */
2932         if (req_has_async_data(req) && io->bytes_done > 0) {
2933                 if (ret < 0)
2934                         ret = io->bytes_done;
2935                 else
2936                         ret += io->bytes_done;
2937         }
2938
2939         if (req->flags & REQ_F_CUR_POS)
2940                 req->file->f_pos = kiocb->ki_pos;
2941         if (ret >= 0 && (kiocb->ki_complete == io_complete_rw))
2942                 __io_complete_rw(req, ret, 0, issue_flags);
2943         else
2944                 io_rw_done(kiocb, ret);
2945
2946         if (req->flags & REQ_F_REISSUE) {
2947                 req->flags &= ~REQ_F_REISSUE;
2948                 if (io_resubmit_prep(req)) {
2949                         io_req_task_queue_reissue(req);
2950                 } else {
2951                         unsigned int cflags = io_put_rw_kbuf(req);
2952                         struct io_ring_ctx *ctx = req->ctx;
2953
2954                         req_set_fail(req);
2955                         if (issue_flags & IO_URING_F_UNLOCKED) {
2956                                 mutex_lock(&ctx->uring_lock);
2957                                 __io_req_complete(req, issue_flags, ret, cflags);
2958                                 mutex_unlock(&ctx->uring_lock);
2959                         } else {
2960                                 __io_req_complete(req, issue_flags, ret, cflags);
2961                         }
2962                 }
2963         }
2964 }
2965
2966 static int __io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter,
2967                              struct io_mapped_ubuf *imu)
2968 {
2969         size_t len = req->rw.len;
2970         u64 buf_end, buf_addr = req->rw.addr;
2971         size_t offset;
2972
2973         if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end)))
2974                 return -EFAULT;
2975         /* not inside the mapped region */
2976         if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end))
2977                 return -EFAULT;
2978
2979         /*
2980          * May not be a start of buffer, set size appropriately
2981          * and advance us to the beginning.
2982          */
2983         offset = buf_addr - imu->ubuf;
2984         iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
2985
2986         if (offset) {
2987                 /*
2988                  * Don't use iov_iter_advance() here, as it's really slow for
2989                  * using the latter parts of a big fixed buffer - it iterates
2990                  * over each segment manually. We can cheat a bit here, because
2991                  * we know that:
2992                  *
2993                  * 1) it's a BVEC iter, we set it up
2994                  * 2) all bvecs are PAGE_SIZE in size, except potentially the
2995                  *    first and last bvec
2996                  *
2997                  * So just find our index, and adjust the iterator afterwards.
2998                  * If the offset is within the first bvec (or the whole first
2999                  * bvec, just use iov_iter_advance(). This makes it easier
3000                  * since we can just skip the first segment, which may not
3001                  * be PAGE_SIZE aligned.
3002                  */
3003                 const struct bio_vec *bvec = imu->bvec;
3004
3005                 if (offset <= bvec->bv_len) {
3006                         iov_iter_advance(iter, offset);
3007                 } else {
3008                         unsigned long seg_skip;
3009
3010                         /* skip first vec */
3011                         offset -= bvec->bv_len;
3012                         seg_skip = 1 + (offset >> PAGE_SHIFT);
3013
3014                         iter->bvec = bvec + seg_skip;
3015                         iter->nr_segs -= seg_skip;
3016                         iter->count -= bvec->bv_len + offset;
3017                         iter->iov_offset = offset & ~PAGE_MASK;
3018                 }
3019         }
3020
3021         return 0;
3022 }
3023
3024 static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter)
3025 {
3026         struct io_mapped_ubuf *imu = req->imu;
3027         u16 index, buf_index = req->buf_index;
3028
3029         if (likely(!imu)) {
3030                 struct io_ring_ctx *ctx = req->ctx;
3031
3032                 if (unlikely(buf_index >= ctx->nr_user_bufs))
3033                         return -EFAULT;
3034                 io_req_set_rsrc_node(req, ctx);
3035                 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
3036                 imu = READ_ONCE(ctx->user_bufs[index]);
3037                 req->imu = imu;
3038         }
3039         return __io_import_fixed(req, rw, iter, imu);
3040 }
3041
3042 static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
3043 {
3044         if (needs_lock)
3045                 mutex_unlock(&ctx->uring_lock);
3046 }
3047
3048 static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
3049 {
3050         /*
3051          * "Normal" inline submissions always hold the uring_lock, since we
3052          * grab it from the system call. Same is true for the SQPOLL offload.
3053          * The only exception is when we've detached the request and issue it
3054          * from an async worker thread, grab the lock for that case.
3055          */
3056         if (needs_lock)
3057                 mutex_lock(&ctx->uring_lock);
3058 }
3059
3060 static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
3061                                           int bgid, unsigned int issue_flags)
3062 {
3063         struct io_buffer *kbuf = req->kbuf;
3064         struct io_buffer *head;
3065         bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
3066
3067         if (req->flags & REQ_F_BUFFER_SELECTED)
3068                 return kbuf;
3069
3070         io_ring_submit_lock(req->ctx, needs_lock);
3071
3072         lockdep_assert_held(&req->ctx->uring_lock);
3073
3074         head = xa_load(&req->ctx->io_buffers, bgid);
3075         if (head) {
3076                 if (!list_empty(&head->list)) {
3077                         kbuf = list_last_entry(&head->list, struct io_buffer,
3078                                                         list);
3079                         list_del(&kbuf->list);
3080                 } else {
3081                         kbuf = head;
3082                         xa_erase(&req->ctx->io_buffers, bgid);
3083                 }
3084                 if (*len > kbuf->len)
3085                         *len = kbuf->len;
3086                 req->flags |= REQ_F_BUFFER_SELECTED;
3087                 req->kbuf = kbuf;
3088         } else {
3089                 kbuf = ERR_PTR(-ENOBUFS);
3090         }
3091
3092         io_ring_submit_unlock(req->ctx, needs_lock);
3093         return kbuf;
3094 }
3095
3096 static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
3097                                         unsigned int issue_flags)
3098 {
3099         struct io_buffer *kbuf;
3100         u16 bgid;
3101
3102         bgid = req->buf_index;
3103         kbuf = io_buffer_select(req, len, bgid, issue_flags);
3104         if (IS_ERR(kbuf))
3105                 return kbuf;
3106         return u64_to_user_ptr(kbuf->addr);
3107 }
3108
3109 #ifdef CONFIG_COMPAT
3110 static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
3111                                 unsigned int issue_flags)
3112 {
3113         struct compat_iovec __user *uiov;
3114         compat_ssize_t clen;
3115         void __user *buf;
3116         ssize_t len;
3117
3118         uiov = u64_to_user_ptr(req->rw.addr);
3119         if (!access_ok(uiov, sizeof(*uiov)))
3120                 return -EFAULT;
3121         if (__get_user(clen, &uiov->iov_len))
3122                 return -EFAULT;
3123         if (clen < 0)
3124                 return -EINVAL;
3125
3126         len = clen;
3127         buf = io_rw_buffer_select(req, &len, issue_flags);
3128         if (IS_ERR(buf))
3129                 return PTR_ERR(buf);
3130         iov[0].iov_base = buf;
3131         iov[0].iov_len = (compat_size_t) len;
3132         return 0;
3133 }
3134 #endif
3135
3136 static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3137                                       unsigned int issue_flags)
3138 {
3139         struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
3140         void __user *buf;
3141         ssize_t len;
3142
3143         if (copy_from_user(iov, uiov, sizeof(*uiov)))
3144                 return -EFAULT;
3145
3146         len = iov[0].iov_len;
3147         if (len < 0)
3148                 return -EINVAL;
3149         buf = io_rw_buffer_select(req, &len, issue_flags);
3150         if (IS_ERR(buf))
3151                 return PTR_ERR(buf);
3152         iov[0].iov_base = buf;
3153         iov[0].iov_len = len;
3154         return 0;
3155 }
3156
3157 static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3158                                     unsigned int issue_flags)
3159 {
3160         if (req->flags & REQ_F_BUFFER_SELECTED) {
3161                 struct io_buffer *kbuf = req->kbuf;
3162
3163                 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3164                 iov[0].iov_len = kbuf->len;
3165                 return 0;
3166         }
3167         if (req->rw.len != 1)
3168                 return -EINVAL;
3169
3170 #ifdef CONFIG_COMPAT
3171         if (req->ctx->compat)
3172                 return io_compat_import(req, iov, issue_flags);
3173 #endif
3174
3175         return __io_iov_buffer_select(req, iov, issue_flags);
3176 }
3177
3178 static struct iovec *__io_import_iovec(int rw, struct io_kiocb *req,
3179                                        struct io_rw_state *s,
3180                                        unsigned int issue_flags)
3181 {
3182         struct iov_iter *iter = &s->iter;
3183         u8 opcode = req->opcode;
3184         struct iovec *iovec;
3185         void __user *buf;
3186         size_t sqe_len;
3187         ssize_t ret;
3188
3189         BUILD_BUG_ON(ERR_PTR(0) != NULL);
3190
3191         if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED)
3192                 return ERR_PTR(io_import_fixed(req, rw, iter));
3193
3194         /* buffer index only valid with fixed read/write, or buffer select  */
3195         if (unlikely(req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)))
3196                 return ERR_PTR(-EINVAL);
3197
3198         buf = u64_to_user_ptr(req->rw.addr);
3199         sqe_len = req->rw.len;
3200
3201         if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
3202                 if (req->flags & REQ_F_BUFFER_SELECT) {
3203                         buf = io_rw_buffer_select(req, &sqe_len, issue_flags);
3204                         if (IS_ERR(buf))
3205                                 return ERR_CAST(buf);
3206                         req->rw.len = sqe_len;
3207                 }
3208
3209                 ret = import_single_range(rw, buf, sqe_len, s->fast_iov, iter);
3210                 return ERR_PTR(ret);
3211         }
3212
3213         iovec = s->fast_iov;
3214         if (req->flags & REQ_F_BUFFER_SELECT) {
3215                 ret = io_iov_buffer_select(req, iovec, issue_flags);
3216                 if (!ret)
3217                         iov_iter_init(iter, rw, iovec, 1, iovec->iov_len);
3218                 return ERR_PTR(ret);
3219         }
3220
3221         ret = __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, &iovec, iter,
3222                               req->ctx->compat);
3223         if (unlikely(ret < 0))
3224                 return ERR_PTR(ret);
3225         return iovec;
3226 }
3227
3228 static inline int io_import_iovec(int rw, struct io_kiocb *req,
3229                                   struct iovec **iovec, struct io_rw_state *s,
3230                                   unsigned int issue_flags)
3231 {
3232         *iovec = __io_import_iovec(rw, req, s, issue_flags);
3233         if (unlikely(IS_ERR(*iovec)))
3234                 return PTR_ERR(*iovec);
3235
3236         iov_iter_save_state(&s->iter, &s->iter_state);
3237         return 0;
3238 }
3239
3240 static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
3241 {
3242         return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
3243 }
3244
3245 /*
3246  * For files that don't have ->read_iter() and ->write_iter(), handle them
3247  * by looping over ->read() or ->write() manually.
3248  */
3249 static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter)
3250 {
3251         struct kiocb *kiocb = &req->rw.kiocb;
3252         struct file *file = req->file;
3253         ssize_t ret = 0;
3254
3255         /*
3256          * Don't support polled IO through this interface, and we can't
3257          * support non-blocking either. For the latter, this just causes
3258          * the kiocb to be handled from an async context.
3259          */
3260         if (kiocb->ki_flags & IOCB_HIPRI)
3261                 return -EOPNOTSUPP;
3262         if ((kiocb->ki_flags & IOCB_NOWAIT) &&
3263             !(kiocb->ki_filp->f_flags & O_NONBLOCK))
3264                 return -EAGAIN;
3265
3266         while (iov_iter_count(iter)) {
3267                 struct iovec iovec;
3268                 ssize_t nr;
3269
3270                 if (!iov_iter_is_bvec(iter)) {
3271                         iovec = iov_iter_iovec(iter);
3272                 } else {
3273                         iovec.iov_base = u64_to_user_ptr(req->rw.addr);
3274                         iovec.iov_len = req->rw.len;
3275                 }
3276
3277                 if (rw == READ) {
3278                         nr = file->f_op->read(file, iovec.iov_base,
3279                                               iovec.iov_len, io_kiocb_ppos(kiocb));
3280                 } else {
3281                         nr = file->f_op->write(file, iovec.iov_base,
3282                                                iovec.iov_len, io_kiocb_ppos(kiocb));
3283                 }
3284
3285                 if (nr < 0) {
3286                         if (!ret)
3287                                 ret = nr;
3288                         break;
3289                 }
3290                 if (!iov_iter_is_bvec(iter)) {
3291                         iov_iter_advance(iter, nr);
3292                 } else {
3293                         req->rw.len -= nr;
3294                         req->rw.addr += nr;
3295                 }
3296                 ret += nr;
3297                 if (nr != iovec.iov_len)
3298                         break;
3299         }
3300
3301         return ret;
3302 }
3303
3304 static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3305                           const struct iovec *fast_iov, struct iov_iter *iter)
3306 {
3307         struct io_async_rw *rw = req->async_data;
3308
3309         memcpy(&rw->s.iter, iter, sizeof(*iter));
3310         rw->free_iovec = iovec;
3311         rw->bytes_done = 0;
3312         /* can only be fixed buffers, no need to do anything */
3313         if (iov_iter_is_bvec(iter))
3314                 return;
3315         if (!iovec) {
3316                 unsigned iov_off = 0;
3317
3318                 rw->s.iter.iov = rw->s.fast_iov;
3319                 if (iter->iov != fast_iov) {
3320                         iov_off = iter->iov - fast_iov;
3321                         rw->s.iter.iov += iov_off;
3322                 }
3323                 if (rw->s.fast_iov != fast_iov)
3324                         memcpy(rw->s.fast_iov + iov_off, fast_iov + iov_off,
3325                                sizeof(struct iovec) * iter->nr_segs);
3326         } else {
3327                 req->flags |= REQ_F_NEED_CLEANUP;
3328         }
3329 }
3330
3331 static inline bool io_alloc_async_data(struct io_kiocb *req)
3332 {
3333         WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3334         req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
3335         if (req->async_data) {
3336                 req->flags |= REQ_F_ASYNC_DATA;
3337                 return false;
3338         }
3339         return true;
3340 }
3341
3342 static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3343                              struct io_rw_state *s, bool force)
3344 {
3345         if (!force && !io_op_defs[req->opcode].needs_async_setup)
3346                 return 0;
3347         if (!req_has_async_data(req)) {
3348                 struct io_async_rw *iorw;
3349
3350                 if (io_alloc_async_data(req)) {
3351                         kfree(iovec);
3352                         return -ENOMEM;
3353                 }
3354
3355                 io_req_map_rw(req, iovec, s->fast_iov, &s->iter);
3356                 iorw = req->async_data;
3357                 /* we've copied and mapped the iter, ensure state is saved */
3358                 iov_iter_save_state(&iorw->s.iter, &iorw->s.iter_state);
3359         }
3360         return 0;
3361 }
3362
3363 static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
3364 {
3365         struct io_async_rw *iorw = req->async_data;
3366         struct iovec *iov;
3367         int ret;
3368
3369         /* submission path, ->uring_lock should already be taken */
3370         ret = io_import_iovec(rw, req, &iov, &iorw->s, 0);
3371         if (unlikely(ret < 0))
3372                 return ret;
3373
3374         iorw->bytes_done = 0;
3375         iorw->free_iovec = iov;
3376         if (iov)
3377                 req->flags |= REQ_F_NEED_CLEANUP;
3378         return 0;
3379 }
3380
3381 static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3382 {
3383         if (unlikely(!(req->file->f_mode & FMODE_READ)))
3384                 return -EBADF;
3385         return io_prep_rw(req, sqe);
3386 }
3387
3388 /*
3389  * This is our waitqueue callback handler, registered through __folio_lock_async()
3390  * when we initially tried to do the IO with the iocb armed our waitqueue.
3391  * This gets called when the page is unlocked, and we generally expect that to
3392  * happen when the page IO is completed and the page is now uptodate. This will
3393  * queue a task_work based retry of the operation, attempting to copy the data
3394  * again. If the latter fails because the page was NOT uptodate, then we will
3395  * do a thread based blocking retry of the operation. That's the unexpected
3396  * slow path.
3397  */
3398 static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3399                              int sync, void *arg)
3400 {
3401         struct wait_page_queue *wpq;
3402         struct io_kiocb *req = wait->private;
3403         struct wait_page_key *key = arg;
3404
3405         wpq = container_of(wait, struct wait_page_queue, wait);
3406
3407         if (!wake_page_match(wpq, key))
3408                 return 0;
3409
3410         req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
3411         list_del_init(&wait->entry);
3412         io_req_task_queue(req);
3413         return 1;
3414 }
3415
3416 /*
3417  * This controls whether a given IO request should be armed for async page
3418  * based retry. If we return false here, the request is handed to the async
3419  * worker threads for retry. If we're doing buffered reads on a regular file,
3420  * we prepare a private wait_page_queue entry and retry the operation. This
3421  * will either succeed because the page is now uptodate and unlocked, or it
3422  * will register a callback when the page is unlocked at IO completion. Through
3423  * that callback, io_uring uses task_work to setup a retry of the operation.
3424  * That retry will attempt the buffered read again. The retry will generally
3425  * succeed, or in rare cases where it fails, we then fall back to using the
3426  * async worker threads for a blocking retry.
3427  */
3428 static bool io_rw_should_retry(struct io_kiocb *req)
3429 {
3430         struct io_async_rw *rw = req->async_data;
3431         struct wait_page_queue *wait = &rw->wpq;
3432         struct kiocb *kiocb = &req->rw.kiocb;
3433
3434         /* never retry for NOWAIT, we just complete with -EAGAIN */
3435         if (req->flags & REQ_F_NOWAIT)
3436                 return false;
3437
3438         /* Only for buffered IO */
3439         if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
3440                 return false;
3441
3442         /*
3443          * just use poll if we can, and don't attempt if the fs doesn't
3444          * support callback based unlocks
3445          */
3446         if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3447                 return false;
3448
3449         wait->wait.func = io_async_buf_func;
3450         wait->wait.private = req;
3451         wait->wait.flags = 0;
3452         INIT_LIST_HEAD(&wait->wait.entry);
3453         kiocb->ki_flags |= IOCB_WAITQ;
3454         kiocb->ki_flags &= ~IOCB_NOWAIT;
3455         kiocb->ki_waitq = wait;
3456         return true;
3457 }
3458
3459 static inline int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3460 {
3461         if (likely(req->file->f_op->read_iter))
3462                 return call_read_iter(req->file, &req->rw.kiocb, iter);
3463         else if (req->file->f_op->read)
3464                 return loop_rw_iter(READ, req, iter);
3465         else
3466                 return -EINVAL;
3467 }
3468
3469 static bool need_read_all(struct io_kiocb *req)
3470 {
3471         return req->flags & REQ_F_ISREG ||
3472                 S_ISBLK(file_inode(req->file)->i_mode);
3473 }
3474
3475 static int io_read(struct io_kiocb *req, unsigned int issue_flags)
3476 {
3477         struct io_rw_state __s, *s = &__s;
3478         struct iovec *iovec;
3479         struct kiocb *kiocb = &req->rw.kiocb;
3480         bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
3481         struct io_async_rw *rw;
3482         ssize_t ret, ret2;
3483
3484         if (!req_has_async_data(req)) {
3485                 ret = io_import_iovec(READ, req, &iovec, s, issue_flags);
3486                 if (unlikely(ret < 0))
3487                         return ret;
3488         } else {
3489                 rw = req->async_data;
3490                 s = &rw->s;
3491                 /*
3492                  * We come here from an earlier attempt, restore our state to
3493                  * match in case it doesn't. It's cheap enough that we don't
3494                  * need to make this conditional.
3495                  */
3496                 iov_iter_restore(&s->iter, &s->iter_state);
3497                 iovec = NULL;
3498         }
3499         req->result = iov_iter_count(&s->iter);
3500
3501         if (force_nonblock) {
3502                 /* If the file doesn't support async, just async punt */
3503                 if (unlikely(!io_file_supports_nowait(req))) {
3504                         ret = io_setup_async_rw(req, iovec, s, true);
3505                         return ret ?: -EAGAIN;
3506                 }
3507                 kiocb->ki_flags |= IOCB_NOWAIT;
3508         } else {
3509                 /* Ensure we clear previously set non-block flag */
3510                 kiocb->ki_flags &= ~IOCB_NOWAIT;
3511         }
3512
3513         ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), req->result);
3514         if (unlikely(ret)) {
3515                 kfree(iovec);
3516                 return ret;
3517         }
3518
3519         ret = io_iter_do_read(req, &s->iter);
3520
3521         if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) {
3522                 req->flags &= ~REQ_F_REISSUE;
3523                 /* IOPOLL retry should happen for io-wq threads */
3524                 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
3525                         goto done;
3526                 /* no retry on NONBLOCK nor RWF_NOWAIT */
3527                 if (req->flags & REQ_F_NOWAIT)
3528                         goto done;
3529                 ret = 0;
3530         } else if (ret == -EIOCBQUEUED) {
3531                 goto out_free;
3532         } else if (ret == req->result || ret <= 0 || !force_nonblock ||
3533                    (req->flags & REQ_F_NOWAIT) || !need_read_all(req)) {
3534                 /* read all, failed, already did sync or don't want to retry */
3535                 goto done;
3536         }
3537
3538         /*
3539          * Don't depend on the iter state matching what was consumed, or being
3540          * untouched in case of error. Restore it and we'll advance it
3541          * manually if we need to.
3542          */
3543         iov_iter_restore(&s->iter, &s->iter_state);
3544
3545         ret2 = io_setup_async_rw(req, iovec, s, true);
3546         if (ret2)
3547                 return ret2;
3548
3549         iovec = NULL;
3550         rw = req->async_data;
3551         s = &rw->s;
3552         /*
3553          * Now use our persistent iterator and state, if we aren't already.
3554          * We've restored and mapped the iter to match.
3555          */
3556
3557         do {
3558                 /*
3559                  * We end up here because of a partial read, either from
3560                  * above or inside this loop. Advance the iter by the bytes
3561                  * that were consumed.
3562                  */
3563                 iov_iter_advance(&s->iter, ret);
3564                 if (!iov_iter_count(&s->iter))
3565                         break;
3566                 rw->bytes_done += ret;
3567                 iov_iter_save_state(&s->iter, &s->iter_state);
3568
3569                 /* if we can retry, do so with the callbacks armed */
3570                 if (!io_rw_should_retry(req)) {
3571                         kiocb->ki_flags &= ~IOCB_WAITQ;
3572                         return -EAGAIN;
3573                 }
3574
3575                 /*
3576                  * Now retry read with the IOCB_WAITQ parts set in the iocb. If
3577                  * we get -EIOCBQUEUED, then we'll get a notification when the
3578                  * desired page gets unlocked. We can also get a partial read
3579                  * here, and if we do, then just retry at the new offset.
3580                  */
3581                 ret = io_iter_do_read(req, &s->iter);
3582                 if (ret == -EIOCBQUEUED)
3583                         return 0;
3584                 /* we got some bytes, but not all. retry. */
3585                 kiocb->ki_flags &= ~IOCB_WAITQ;
3586                 iov_iter_restore(&s->iter, &s->iter_state);
3587         } while (ret > 0);
3588 done:
3589         kiocb_done(kiocb, ret, issue_flags);
3590 out_free:
3591         /* it's faster to check here then delegate to kfree */
3592         if (iovec)
3593                 kfree(iovec);
3594         return 0;
3595 }
3596
3597 static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3598 {
3599         if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3600                 return -EBADF;
3601         req->rw.kiocb.ki_hint = ki_hint_validate(file_write_hint(req->file));
3602         return io_prep_rw(req, sqe);
3603 }
3604
3605 static int io_write(struct io_kiocb *req, unsigned int issue_flags)
3606 {
3607         struct io_rw_state __s, *s = &__s;
3608         struct iovec *iovec;
3609         struct kiocb *kiocb = &req->rw.kiocb;
3610         bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
3611         ssize_t ret, ret2;
3612
3613         if (!req_has_async_data(req)) {
3614                 ret = io_import_iovec(WRITE, req, &iovec, s, issue_flags);
3615                 if (unlikely(ret < 0))
3616                         return ret;
3617         } else {
3618                 struct io_async_rw *rw = req->async_data;
3619
3620                 s = &rw->s;
3621                 iov_iter_restore(&s->iter, &s->iter_state);
3622                 iovec = NULL;
3623         }
3624         req->result = iov_iter_count(&s->iter);
3625
3626         if (force_nonblock) {
3627                 /* If the file doesn't support async, just async punt */
3628                 if (unlikely(!io_file_supports_nowait(req)))
3629                         goto copy_iov;
3630
3631                 /* file path doesn't support NOWAIT for non-direct_IO */
3632                 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3633                     (req->flags & REQ_F_ISREG))
3634                         goto copy_iov;
3635
3636                 kiocb->ki_flags |= IOCB_NOWAIT;
3637         } else {
3638                 /* Ensure we clear previously set non-block flag */
3639                 kiocb->ki_flags &= ~IOCB_NOWAIT;
3640         }
3641
3642         ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), req->result);
3643         if (unlikely(ret))
3644                 goto out_free;
3645
3646         /*
3647          * Open-code file_start_write here to grab freeze protection,
3648          * which will be released by another thread in
3649          * io_complete_rw().  Fool lockdep by telling it the lock got
3650          * released so that it doesn't complain about the held lock when
3651          * we return to userspace.
3652          */
3653         if (req->flags & REQ_F_ISREG) {
3654                 sb_start_write(file_inode(req->file)->i_sb);
3655                 __sb_writers_release(file_inode(req->file)->i_sb,
3656                                         SB_FREEZE_WRITE);
3657         }
3658         kiocb->ki_flags |= IOCB_WRITE;
3659
3660         if (likely(req->file->f_op->write_iter))
3661                 ret2 = call_write_iter(req->file, kiocb, &s->iter);
3662         else if (req->file->f_op->write)
3663                 ret2 = loop_rw_iter(WRITE, req, &s->iter);
3664         else
3665                 ret2 = -EINVAL;
3666
3667         if (req->flags & REQ_F_REISSUE) {
3668                 req->flags &= ~REQ_F_REISSUE;
3669                 ret2 = -EAGAIN;
3670         }
3671
3672         /*
3673          * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3674          * retry them without IOCB_NOWAIT.
3675          */
3676         if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3677                 ret2 = -EAGAIN;
3678         /* no retry on NONBLOCK nor RWF_NOWAIT */
3679         if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT))
3680                 goto done;
3681         if (!force_nonblock || ret2 != -EAGAIN) {
3682                 /* IOPOLL retry should happen for io-wq threads */
3683                 if (ret2 == -EAGAIN && (req->ctx->flags & IORING_SETUP_IOPOLL))
3684                         goto copy_iov;
3685 done:
3686                 kiocb_done(kiocb, ret2, issue_flags);
3687         } else {
3688 copy_iov:
3689                 iov_iter_restore(&s->iter, &s->iter_state);
3690                 ret = io_setup_async_rw(req, iovec, s, false);
3691                 return ret ?: -EAGAIN;
3692         }
3693 out_free:
3694         /* it's reportedly faster than delegating the null check to kfree() */
3695         if (iovec)
3696                 kfree(iovec);
3697         return ret;
3698 }
3699
3700 static int io_renameat_prep(struct io_kiocb *req,
3701                             const struct io_uring_sqe *sqe)
3702 {
3703         struct io_rename *ren = &req->rename;
3704         const char __user *oldf, *newf;
3705
3706         if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3707                 return -EINVAL;
3708         if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
3709                 return -EINVAL;
3710         if (unlikely(req->flags & REQ_F_FIXED_FILE))
3711                 return -EBADF;
3712
3713         ren->old_dfd = READ_ONCE(sqe->fd);
3714         oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3715         newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3716         ren->new_dfd = READ_ONCE(sqe->len);
3717         ren->flags = READ_ONCE(sqe->rename_flags);
3718
3719         ren->oldpath = getname(oldf);
3720         if (IS_ERR(ren->oldpath))
3721                 return PTR_ERR(ren->oldpath);
3722
3723         ren->newpath = getname(newf);
3724         if (IS_ERR(ren->newpath)) {
3725                 putname(ren->oldpath);
3726                 return PTR_ERR(ren->newpath);
3727         }
3728
3729         req->flags |= REQ_F_NEED_CLEANUP;
3730         return 0;
3731 }
3732
3733 static int io_renameat(struct io_kiocb *req, unsigned int issue_flags)
3734 {
3735         struct io_rename *ren = &req->rename;
3736         int ret;
3737
3738         if (issue_flags & IO_URING_F_NONBLOCK)
3739                 return -EAGAIN;
3740
3741         ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd,
3742                                 ren->newpath, ren->flags);
3743
3744         req->flags &= ~REQ_F_NEED_CLEANUP;
3745         if (ret < 0)
3746                 req_set_fail(req);
3747         io_req_complete(req, ret);
3748         return 0;
3749 }
3750
3751 static int io_unlinkat_prep(struct io_kiocb *req,
3752                             const struct io_uring_sqe *sqe)
3753 {
3754         struct io_unlink *un = &req->unlink;
3755         const char __user *fname;
3756
3757         if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3758                 return -EINVAL;
3759         if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3760             sqe->splice_fd_in)
3761                 return -EINVAL;
3762         if (unlikely(req->flags & REQ_F_FIXED_FILE))
3763                 return -EBADF;
3764
3765         un->dfd = READ_ONCE(sqe->fd);
3766
3767         un->flags = READ_ONCE(sqe->unlink_flags);
3768         if (un->flags & ~AT_REMOVEDIR)
3769                 return -EINVAL;
3770
3771         fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3772         un->filename = getname(fname);
3773         if (IS_ERR(un->filename))
3774                 return PTR_ERR(un->filename);
3775
3776         req->flags |= REQ_F_NEED_CLEANUP;
3777         return 0;
3778 }
3779
3780 static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags)
3781 {
3782         struct io_unlink *un = &req->unlink;
3783         int ret;
3784
3785         if (issue_flags & IO_URING_F_NONBLOCK)
3786                 return -EAGAIN;
3787
3788         if (un->flags & AT_REMOVEDIR)
3789                 ret = do_rmdir(un->dfd, un->filename);
3790         else
3791                 ret = do_unlinkat(un->dfd, un->filename);
3792
3793         req->flags &= ~REQ_F_NEED_CLEANUP;
3794         if (ret < 0)
3795                 req_set_fail(req);
3796         io_req_complete(req, ret);
3797         return 0;
3798 }
3799
3800 static int io_mkdirat_prep(struct io_kiocb *req,
3801                             const struct io_uring_sqe *sqe)
3802 {
3803         struct io_mkdir *mkd = &req->mkdir;
3804         const char __user *fname;
3805
3806         if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3807                 return -EINVAL;
3808         if (sqe->ioprio || sqe->off || sqe->rw_flags || sqe->buf_index ||
3809             sqe->splice_fd_in)
3810                 return -EINVAL;
3811         if (unlikely(req->flags & REQ_F_FIXED_FILE))
3812                 return -EBADF;
3813
3814         mkd->dfd = READ_ONCE(sqe->fd);
3815         mkd->mode = READ_ONCE(sqe->len);
3816
3817         fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3818         mkd->filename = getname(fname);
3819         if (IS_ERR(mkd->filename))
3820                 return PTR_ERR(mkd->filename);
3821
3822         req->flags |= REQ_F_NEED_CLEANUP;
3823         return 0;
3824 }
3825
3826 static int io_mkdirat(struct io_kiocb *req, unsigned int issue_flags)
3827 {
3828         struct io_mkdir *mkd = &req->mkdir;
3829         int ret;
3830
3831         if (issue_flags & IO_URING_F_NONBLOCK)
3832                 return -EAGAIN;
3833
3834         ret = do_mkdirat(mkd->dfd, mkd->filename, mkd->mode);
3835
3836         req->flags &= ~REQ_F_NEED_CLEANUP;
3837         if (ret < 0)
3838                 req_set_fail(req);
3839         io_req_complete(req, ret);
3840         return 0;
3841 }
3842
3843 static int io_symlinkat_prep(struct io_kiocb *req,
3844                             const struct io_uring_sqe *sqe)
3845 {
3846         struct io_symlink *sl = &req->symlink;
3847         const char __user *oldpath, *newpath;
3848
3849         if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3850                 return -EINVAL;
3851         if (sqe->ioprio || sqe->len || sqe->rw_flags || sqe->buf_index ||
3852             sqe->splice_fd_in)
3853                 return -EINVAL;
3854         if (unlikely(req->flags & REQ_F_FIXED_FILE))
3855                 return -EBADF;
3856
3857         sl->new_dfd = READ_ONCE(sqe->fd);
3858         oldpath = u64_to_user_ptr(READ_ONCE(sqe->addr));
3859         newpath = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3860
3861         sl->oldpath = getname(oldpath);
3862         if (IS_ERR(sl->oldpath))
3863                 return PTR_ERR(sl->oldpath);
3864
3865         sl->newpath = getname(newpath);
3866         if (IS_ERR(sl->newpath)) {
3867                 putname(sl->oldpath);
3868                 return PTR_ERR(sl->newpath);
3869         }
3870
3871         req->flags |= REQ_F_NEED_CLEANUP;
3872         return 0;
3873 }
3874
3875 static int io_symlinkat(struct io_kiocb *req, unsigned int issue_flags)
3876 {
3877         struct io_symlink *sl = &req->symlink;
3878         int ret;
3879
3880         if (issue_flags & IO_URING_F_NONBLOCK)
3881                 return -EAGAIN;
3882
3883         ret = do_symlinkat(sl->oldpath, sl->new_dfd, sl->newpath);
3884
3885         req->flags &= ~REQ_F_NEED_CLEANUP;
3886         if (ret < 0)
3887                 req_set_fail(req);
3888         io_req_complete(req, ret);
3889         return 0;
3890 }
3891
3892 static int io_linkat_prep(struct io_kiocb *req,
3893                             const struct io_uring_sqe *sqe)
3894 {
3895         struct io_hardlink *lnk = &req->hardlink;
3896         const char __user *oldf, *newf;
3897
3898         if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3899                 return -EINVAL;
3900         if (sqe->ioprio || sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in)
3901                 return -EINVAL;
3902         if (unlikely(req->flags & REQ_F_FIXED_FILE))
3903                 return -EBADF;
3904
3905         lnk->old_dfd = READ_ONCE(sqe->fd);
3906         lnk->new_dfd = READ_ONCE(sqe->len);
3907         oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3908         newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3909         lnk->flags = READ_ONCE(sqe->hardlink_flags);
3910
3911         lnk->oldpath = getname(oldf);
3912         if (IS_ERR(lnk->oldpath))
3913                 return PTR_ERR(lnk->oldpath);
3914
3915         lnk->newpath = getname(newf);
3916         if (IS_ERR(lnk->newpath)) {
3917                 putname(lnk->oldpath);
3918                 return PTR_ERR(lnk->newpath);
3919         }
3920
3921         req->flags |= REQ_F_NEED_CLEANUP;
3922         return 0;
3923 }
3924
3925 static int io_linkat(struct io_kiocb *req, unsigned int issue_flags)
3926 {
3927         struct io_hardlink *lnk = &req->hardlink;
3928         int ret;
3929
3930         if (issue_flags & IO_URING_F_NONBLOCK)
3931                 return -EAGAIN;
3932
3933         ret = do_linkat(lnk->old_dfd, lnk->oldpath, lnk->new_dfd,
3934                                 lnk->newpath, lnk->flags);
3935
3936         req->flags &= ~REQ_F_NEED_CLEANUP;
3937         if (ret < 0)
3938                 req_set_fail(req);
3939         io_req_complete(req, ret);
3940         return 0;
3941 }
3942
3943 static int io_shutdown_prep(struct io_kiocb *req,
3944                             const struct io_uring_sqe *sqe)
3945 {
3946 #if defined(CONFIG_NET)
3947         if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3948                 return -EINVAL;
3949         if (unlikely(sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags ||
3950                      sqe->buf_index || sqe->splice_fd_in))
3951                 return -EINVAL;
3952
3953         req->shutdown.how = READ_ONCE(sqe->len);
3954         return 0;
3955 #else
3956         return -EOPNOTSUPP;
3957 #endif
3958 }
3959
3960 static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags)
3961 {
3962 #if defined(CONFIG_NET)
3963         struct socket *sock;
3964         int ret;
3965
3966         if (issue_flags & IO_URING_F_NONBLOCK)
3967                 return -EAGAIN;
3968
3969         sock = sock_from_file(req->file);
3970         if (unlikely(!sock))
3971                 return -ENOTSOCK;
3972
3973         ret = __sys_shutdown_sock(sock, req->shutdown.how);
3974         if (ret < 0)
3975                 req_set_fail(req);
3976         io_req_complete(req, ret);
3977         return 0;
3978 #else
3979         return -EOPNOTSUPP;
3980 #endif
3981 }
3982
3983 static int __io_splice_prep(struct io_kiocb *req,
3984                             const struct io_uring_sqe *sqe)
3985 {
3986         struct io_splice *sp = &req->splice;
3987         unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
3988
3989         if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3990                 return -EINVAL;
3991
3992         sp->file_in = NULL;
3993         sp->len = READ_ONCE(sqe->len);
3994         sp->flags = READ_ONCE(sqe->splice_flags);
3995
3996         if (unlikely(sp->flags & ~valid_flags))
3997                 return -EINVAL;
3998
3999         sp->file_in = io_file_get(req->ctx, req, READ_ONCE(sqe->splice_fd_in),
4000                                   (sp->flags & SPLICE_F_FD_IN_FIXED));
4001         if (!sp->file_in)
4002                 return -EBADF;
4003         req->flags |= REQ_F_NEED_CLEANUP;
4004         return 0;
4005 }
4006
4007 static int io_tee_prep(struct io_kiocb *req,
4008                        const struct io_uring_sqe *sqe)
4009 {
4010         if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
4011                 return -EINVAL;
4012         return __io_splice_prep(req, sqe);
4013 }
4014
4015 static int io_tee(struct io_kiocb *req, unsigned int issue_flags)
4016 {
4017         struct io_splice *sp = &req->splice;
4018         struct file *in = sp->file_in;
4019         struct file *out = sp->file_out;
4020         unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
4021         long ret = 0;
4022
4023         if (issue_flags & IO_URING_F_NONBLOCK)
4024                 return -EAGAIN;
4025         if (sp->len)
4026                 ret = do_tee(in, out, sp->len, flags);
4027
4028         if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
4029                 io_put_file(in);
4030         req->flags &= ~REQ_F_NEED_CLEANUP;
4031
4032         if (ret != sp->len)
4033                 req_set_fail(req);
4034         io_req_complete(req, ret);
4035         return 0;
4036 }
4037
4038 static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4039 {
4040         struct io_splice *sp = &req->splice;
4041
4042         sp->off_in = READ_ONCE(sqe->splice_off_in);
4043         sp->off_out = READ_ONCE(sqe->off);
4044         return __io_splice_prep(req, sqe);
4045 }
4046
4047 static int io_splice(struct io_kiocb *req, unsigned int issue_flags)
4048 {
4049         struct io_splice *sp = &req->splice;
4050         struct file *in = sp->file_in;
4051         struct file *out = sp->file_out;
4052         unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
4053         loff_t *poff_in, *poff_out;
4054         long ret = 0;
4055
4056         if (issue_flags & IO_URING_F_NONBLOCK)
4057                 return -EAGAIN;
4058
4059         poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
4060         poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
4061
4062         if (sp->len)
4063                 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
4064
4065         if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
4066                 io_put_file(in);
4067         req->flags &= ~REQ_F_NEED_CLEANUP;
4068
4069         if (ret != sp->len)
4070                 req_set_fail(req);
4071         io_req_complete(req, ret);
4072         return 0;
4073 }
4074
4075 /*
4076  * IORING_OP_NOP just posts a completion event, nothing else.
4077  */
4078 static int io_nop(struct io_kiocb *req, unsigned int issue_flags)
4079 {
4080         struct io_ring_ctx *ctx = req->ctx;
4081
4082         if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4083                 return -EINVAL;
4084
4085         __io_req_complete(req, issue_flags, 0, 0);
4086         return 0;
4087 }
4088
4089 static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4090 {
4091         struct io_ring_ctx *ctx = req->ctx;
4092
4093         if (!req->file)
4094                 return -EBADF;
4095
4096         if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4097                 return -EINVAL;
4098         if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index ||
4099                      sqe->splice_fd_in))
4100                 return -EINVAL;
4101
4102         req->sync.flags = READ_ONCE(sqe->fsync_flags);
4103         if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
4104                 return -EINVAL;
4105
4106         req->sync.off = READ_ONCE(sqe->off);
4107         req->sync.len = READ_ONCE(sqe->len);
4108         return 0;
4109 }
4110
4111 static int io_fsync(struct io_kiocb *req, unsigned int issue_flags)
4112 {
4113         loff_t end = req->sync.off + req->sync.len;
4114         int ret;
4115
4116         /* fsync always requires a blocking context */
4117         if (issue_flags & IO_URING_F_NONBLOCK)
4118                 return -EAGAIN;
4119
4120         ret = vfs_fsync_range(req->file, req->sync.off,
4121                                 end > 0 ? end : LLONG_MAX,
4122                                 req->sync.flags & IORING_FSYNC_DATASYNC);
4123         if (ret < 0)
4124                 req_set_fail(req);
4125         io_req_complete(req, ret);
4126         return 0;
4127 }
4128
4129 static int io_fallocate_prep(struct io_kiocb *req,
4130                              const struct io_uring_sqe *sqe)
4131 {
4132         if (sqe->ioprio || sqe->buf_index || sqe->rw_flags ||
4133             sqe->splice_fd_in)
4134                 return -EINVAL;
4135         if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4136                 return -EINVAL;
4137
4138         req->sync.off = READ_ONCE(sqe->off);
4139         req->sync.len = READ_ONCE(sqe->addr);
4140         req->sync.mode = READ_ONCE(sqe->len);
4141         return 0;
4142 }
4143
4144 static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags)
4145 {
4146         int ret;
4147
4148         /* fallocate always requiring blocking context */
4149         if (issue_flags & IO_URING_F_NONBLOCK)
4150                 return -EAGAIN;
4151         ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
4152                                 req->sync.len);
4153         if (ret < 0)
4154                 req_set_fail(req);
4155         io_req_complete(req, ret);
4156         return 0;
4157 }
4158
4159 static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4160 {
4161         const char __user *fname;
4162         int ret;
4163
4164         if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4165                 return -EINVAL;
4166         if (unlikely(sqe->ioprio || sqe->buf_index))
4167                 return -EINVAL;
4168         if (unlikely(req->flags & REQ_F_FIXED_FILE))
4169                 return -EBADF;
4170
4171         /* open.how should be already initialised */
4172         if (!(req->open.how.flags & O_PATH) && force_o_largefile())
4173                 req->open.how.flags |= O_LARGEFILE;
4174
4175         req->open.dfd = READ_ONCE(sqe->fd);
4176         fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
4177         req->open.filename = getname(fname);
4178         if (IS_ERR(req->open.filename)) {
4179                 ret = PTR_ERR(req->open.filename);
4180                 req->open.filename = NULL;
4181                 return ret;
4182         }
4183
4184         req->open.file_slot = READ_ONCE(sqe->file_index);
4185         if (req->open.file_slot && (req->open.how.flags & O_CLOEXEC))
4186                 return -EINVAL;
4187
4188         req->open.nofile = rlimit(RLIMIT_NOFILE);
4189         req->flags |= REQ_F_NEED_CLEANUP;
4190         return 0;
4191 }
4192
4193 static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4194 {
4195         u64 mode = READ_ONCE(sqe->len);
4196         u64 flags = READ_ONCE(sqe->open_flags);
4197
4198         req->open.how = build_open_how(flags, mode);
4199         return __io_openat_prep(req, sqe);
4200 }
4201
4202 static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4203 {
4204         struct open_how __user *how;
4205         size_t len;
4206         int ret;
4207
4208         how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4209         len = READ_ONCE(sqe->len);
4210         if (len < OPEN_HOW_SIZE_VER0)
4211                 return -EINVAL;
4212
4213         ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
4214                                         len);
4215         if (ret)
4216                 return ret;
4217
4218         return __io_openat_prep(req, sqe);
4219 }
4220
4221 static int io_openat2(struct io_kiocb *req, unsigned int issue_flags)
4222 {
4223         struct open_flags op;
4224         struct file *file;
4225         bool resolve_nonblock, nonblock_set;
4226         bool fixed = !!req->open.file_slot;
4227         int ret;
4228
4229         ret = build_open_flags(&req->open.how, &op);
4230         if (ret)
4231                 goto err;
4232         nonblock_set = op.open_flag & O_NONBLOCK;
4233         resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED;
4234         if (issue_flags & IO_URING_F_NONBLOCK) {
4235                 /*
4236                  * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open,
4237                  * it'll always -EAGAIN
4238                  */
4239                 if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE))
4240                         return -EAGAIN;
4241                 op.lookup_flags |= LOOKUP_CACHED;
4242                 op.open_flag |= O_NONBLOCK;
4243         }
4244
4245         if (!fixed) {
4246                 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
4247                 if (ret < 0)
4248                         goto err;
4249         }
4250
4251         file = do_filp_open(req->open.dfd, req->open.filename, &op);
4252         if (IS_ERR(file)) {
4253                 /*
4254                  * We could hang on to this 'fd' on retrying, but seems like
4255                  * marginal gain for something that is now known to be a slower
4256                  * path. So just put it, and we'll get a new one when we retry.
4257                  */
4258                 if (!fixed)
4259                         put_unused_fd(ret);
4260
4261                 ret = PTR_ERR(file);
4262                 /* only retry if RESOLVE_CACHED wasn't already set by application */
4263                 if (ret == -EAGAIN &&
4264                     (!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)))
4265                         return -EAGAIN;
4266                 goto err;
4267         }
4268
4269         if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set)
4270                 file->f_flags &= ~O_NONBLOCK;
4271         fsnotify_open(file);
4272
4273         if (!fixed)
4274                 fd_install(ret, file);
4275         else
4276                 ret = io_install_fixed_file(req, file, issue_flags,
4277                                             req->open.file_slot - 1);
4278 err:
4279         putname(req->open.filename);
4280         req->flags &= ~REQ_F_NEED_CLEANUP;
4281         if (ret < 0)
4282                 req_set_fail(req);
4283         __io_req_complete(req, issue_flags, ret, 0);
4284         return 0;
4285 }
4286
4287 static int io_openat(struct io_kiocb *req, unsigned int issue_flags)
4288 {
4289         return io_openat2(req, issue_flags);
4290 }
4291
4292 static int io_remove_buffers_prep(struct io_kiocb *req,
4293                                   const struct io_uring_sqe *sqe)
4294 {
4295         struct io_provide_buf *p = &req->pbuf;
4296         u64 tmp;
4297
4298         if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off ||
4299             sqe->splice_fd_in)
4300                 return -EINVAL;
4301
4302         tmp = READ_ONCE(sqe->fd);
4303         if (!tmp || tmp > USHRT_MAX)
4304                 return -EINVAL;
4305
4306         memset(p, 0, sizeof(*p));
4307         p->nbufs = tmp;
4308         p->bgid = READ_ONCE(sqe->buf_group);
4309         return 0;
4310 }
4311
4312 static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
4313                                int bgid, unsigned nbufs)
4314 {
4315         unsigned i = 0;
4316
4317         /* shouldn't happen */
4318         if (!nbufs)
4319                 return 0;
4320
4321         /* the head kbuf is the list itself */
4322         while (!list_empty(&buf->list)) {
4323                 struct io_buffer *nxt;
4324
4325                 nxt = list_first_entry(&buf->list, struct io_buffer, list);
4326                 list_del(&nxt->list);
4327                 kfree(nxt);
4328                 if (++i == nbufs)
4329                         return i;
4330         }
4331         i++;
4332         kfree(buf);
4333         xa_erase(&ctx->io_buffers, bgid);
4334
4335         return i;
4336 }
4337
4338 static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
4339 {
4340         struct io_provide_buf *p = &req->pbuf;
4341         struct io_ring_ctx *ctx = req->ctx;
4342         struct io_buffer *head;
4343         int ret = 0;
4344         bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
4345
4346         io_ring_submit_lock(ctx, needs_lock);
4347
4348         lockdep_assert_held(&ctx->uring_lock);
4349
4350         ret = -ENOENT;
4351         head = xa_load(&ctx->io_buffers, p->bgid);
4352         if (head)
4353                 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
4354         if (ret < 0)
4355                 req_set_fail(req);
4356
4357         /* complete before unlock, IOPOLL may need the lock */
4358         __io_req_complete(req, issue_flags, ret, 0);
4359         io_ring_submit_unlock(ctx, needs_lock);
4360         return 0;
4361 }
4362
4363 static int io_provide_buffers_prep(struct io_kiocb *req,
4364                                    const struct io_uring_sqe *sqe)
4365 {
4366         unsigned long size, tmp_check;
4367         struct io_provide_buf *p = &req->pbuf;
4368         u64 tmp;
4369
4370         if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in)
4371                 return -EINVAL;
4372
4373         tmp = READ_ONCE(sqe->fd);
4374         if (!tmp || tmp > USHRT_MAX)
4375                 return -E2BIG;
4376         p->nbufs = tmp;
4377         p->addr = READ_ONCE(sqe->addr);
4378         p->len = READ_ONCE(sqe->len);
4379
4380         if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs,
4381                                 &size))
4382                 return -EOVERFLOW;
4383         if (check_add_overflow((unsigned long)p->addr, size, &tmp_check))
4384                 return -EOVERFLOW;
4385
4386         size = (unsigned long)p->len * p->nbufs;
4387         if (!access_ok(u64_to_user_ptr(p->addr), size))
4388                 return -EFAULT;
4389
4390         p->bgid = READ_ONCE(sqe->buf_group);
4391         tmp = READ_ONCE(sqe->off);
4392         if (tmp > USHRT_MAX)
4393                 return -E2BIG;
4394         p->bid = tmp;
4395         return 0;
4396 }
4397
4398 static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
4399 {
4400         struct io_buffer *buf;
4401         u64 addr = pbuf->addr;
4402         int i, bid = pbuf->bid;
4403
4404         for (i = 0; i < pbuf->nbufs; i++) {
4405                 buf = kmalloc(sizeof(*buf), GFP_KERNEL_ACCOUNT);
4406                 if (!buf)
4407                         break;
4408
4409                 buf->addr = addr;
4410                 buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT);
4411                 buf->bid = bid;
4412                 addr += pbuf->len;
4413                 bid++;
4414                 if (!*head) {
4415                         INIT_LIST_HEAD(&buf->list);
4416                         *head = buf;
4417                 } else {
4418                         list_add_tail(&buf->list, &(*head)->list);
4419                 }
4420         }
4421
4422         return i ? i : -ENOMEM;
4423 }
4424
4425 static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
4426 {
4427         struct io_provide_buf *p = &req->pbuf;
4428         struct io_ring_ctx *ctx = req->ctx;
4429         struct io_buffer *head, *list;
4430         int ret = 0;
4431         bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
4432
4433         io_ring_submit_lock(ctx, needs_lock);
4434
4435         lockdep_assert_held(&ctx->uring_lock);
4436
4437         list = head = xa_load(&ctx->io_buffers, p->bgid);
4438
4439         ret = io_add_buffers(p, &head);
4440         if (ret >= 0 && !list) {
4441                 ret = xa_insert(&ctx->io_buffers, p->bgid, head, GFP_KERNEL);
4442                 if (ret < 0)
4443                         __io_remove_buffers(ctx, head, p->bgid, -1U);
4444         }
4445         if (ret < 0)
4446                 req_set_fail(req);
4447         /* complete before unlock, IOPOLL may need the lock */
4448         __io_req_complete(req, issue_flags, ret, 0);
4449         io_ring_submit_unlock(ctx, needs_lock);
4450         return 0;
4451 }
4452
4453 static int io_epoll_ctl_prep(struct io_kiocb *req,
4454                              const struct io_uring_sqe *sqe)
4455 {
4456 #if defined(CONFIG_EPOLL)
4457         if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
4458                 return -EINVAL;
4459         if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4460                 return -EINVAL;
4461
4462         req->epoll.epfd = READ_ONCE(sqe->fd);
4463         req->epoll.op = READ_ONCE(sqe->len);
4464         req->epoll.fd = READ_ONCE(sqe->off);
4465
4466         if (ep_op_has_event(req->epoll.op)) {
4467                 struct epoll_event __user *ev;
4468
4469                 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
4470                 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
4471                         return -EFAULT;
4472         }
4473
4474         return 0;
4475 #else
4476         return -EOPNOTSUPP;
4477 #endif
4478 }
4479
4480 static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags)
4481 {
4482 #if defined(CONFIG_EPOLL)
4483         struct io_epoll *ie = &req->epoll;
4484         int ret;
4485         bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
4486
4487         ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
4488         if (force_nonblock && ret == -EAGAIN)
4489                 return -EAGAIN;
4490
4491         if (ret < 0)
4492                 req_set_fail(req);
4493         __io_req_complete(req, issue_flags, ret, 0);
4494         return 0;
4495 #else
4496         return -EOPNOTSUPP;
4497 #endif
4498 }
4499
4500 static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4501 {
4502 #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4503         if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->splice_fd_in)
4504                 return -EINVAL;
4505         if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4506                 return -EINVAL;
4507
4508         req->madvise.addr = READ_ONCE(sqe->addr);
4509         req->madvise.len = READ_ONCE(sqe->len);
4510         req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
4511         return 0;
4512 #else
4513         return -EOPNOTSUPP;
4514 #endif
4515 }
4516
4517 static int io_madvise(struct io_kiocb *req, unsigned int issue_flags)
4518 {
4519 #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4520         struct io_madvise *ma = &req->madvise;
4521         int ret;
4522
4523         if (issue_flags & IO_URING_F_NONBLOCK)
4524                 return -EAGAIN;
4525
4526         ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
4527         if (ret < 0)
4528                 req_set_fail(req);
4529         io_req_complete(req, ret);
4530         return 0;
4531 #else
4532         return -EOPNOTSUPP;
4533 #endif
4534 }
4535
4536 static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4537 {
4538         if (sqe->ioprio || sqe->buf_index || sqe->addr || sqe->splice_fd_in)
4539                 return -EINVAL;
4540         if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4541                 return -EINVAL;
4542
4543         req->fadvise.offset = READ_ONCE(sqe->off);
4544         req->fadvise.len = READ_ONCE(sqe->len);
4545         req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4546         return 0;
4547 }
4548
4549 static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags)
4550 {
4551         struct io_fadvise *fa = &req->fadvise;
4552         int ret;
4553
4554         if (issue_flags & IO_URING_F_NONBLOCK) {
4555                 switch (fa->advice) {
4556                 case POSIX_FADV_NORMAL:
4557                 case POSIX_FADV_RANDOM:
4558                 case POSIX_FADV_SEQUENTIAL:
4559                         break;
4560                 default:
4561                         return -EAGAIN;
4562                 }
4563         }
4564
4565         ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4566         if (ret < 0)
4567                 req_set_fail(req);
4568         __io_req_complete(req, issue_flags, ret, 0);
4569         return 0;
4570 }
4571
4572 static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4573 {
4574         if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4575                 return -EINVAL;
4576         if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
4577                 return -EINVAL;
4578         if (req->flags & REQ_F_FIXED_FILE)
4579                 return -EBADF;
4580
4581         req->statx.dfd = READ_ONCE(sqe->fd);
4582         req->statx.mask = READ_ONCE(sqe->len);
4583         req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
4584         req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4585         req->statx.flags = READ_ONCE(sqe->statx_flags);
4586
4587         return 0;
4588 }
4589
4590 static int io_statx(struct io_kiocb *req, unsigned int issue_flags)
4591 {
4592         struct io_statx *ctx = &req->statx;
4593         int ret;
4594
4595         if (issue_flags & IO_URING_F_NONBLOCK)
4596                 return -EAGAIN;
4597
4598         ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4599                        ctx->buffer);
4600
4601         if (ret < 0)
4602                 req_set_fail(req);
4603         io_req_complete(req, ret);
4604         return 0;
4605 }
4606
4607 static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4608 {
4609         if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4610                 return -EINVAL;
4611         if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
4612             sqe->rw_flags || sqe->buf_index)
4613                 return -EINVAL;
4614         if (req->flags & REQ_F_FIXED_FILE)
4615                 return -EBADF;
4616
4617         req->close.fd = READ_ONCE(sqe->fd);
4618         req->close.file_slot = READ_ONCE(sqe->file_index);
4619         if (req->close.file_slot && req->close.fd)
4620                 return -EINVAL;
4621
4622         return 0;
4623 }
4624
4625 static int io_close(struct io_kiocb *req, unsigned int issue_flags)
4626 {
4627         struct files_struct *files = current->files;
4628         struct io_close *close = &req->close;
4629         struct fdtable *fdt;
4630         struct file *file = NULL;
4631         int ret = -EBADF;
4632
4633         if (req->close.file_slot) {
4634                 ret = io_close_fixed(req, issue_flags);
4635                 goto err;
4636         }
4637
4638         spin_lock(&files->file_lock);
4639         fdt = files_fdtable(files);
4640         if (close->fd >= fdt->max_fds) {
4641                 spin_unlock(&files->file_lock);
4642                 goto err;
4643         }
4644         file = fdt->fd[close->fd];
4645         if (!file || file->f_op == &io_uring_fops) {
4646                 spin_unlock(&files->file_lock);
4647                 file = NULL;
4648                 goto err;
4649         }
4650
4651         /* if the file has a flush method, be safe and punt to async */
4652         if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) {
4653                 spin_unlock(&files->file_lock);
4654                 return -EAGAIN;
4655         }
4656
4657         ret = __close_fd_get_file(close->fd, &file);
4658         spin_unlock(&files->file_lock);
4659         if (ret < 0) {
4660                 if (ret == -ENOENT)
4661                         ret = -EBADF;
4662                 goto err;
4663         }
4664
4665         /* No ->flush() or already async, safely close from here */
4666         ret = filp_close(file, current->files);
4667 err:
4668         if (ret < 0)
4669                 req_set_fail(req);
4670         if (file)
4671                 fput(file);
4672         __io_req_complete(req, issue_flags, ret, 0);
4673         return 0;
4674 }
4675
4676 static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4677 {
4678         struct io_ring_ctx *ctx = req->ctx;
4679
4680         if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4681                 return -EINVAL;
4682         if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index ||
4683                      sqe->splice_fd_in))
4684                 return -EINVAL;
4685
4686         req->sync.off = READ_ONCE(sqe->off);
4687         req->sync.len = READ_ONCE(sqe->len);
4688         req->sync.flags = READ_ONCE(sqe->sync_range_flags);
4689         return 0;
4690 }
4691
4692 static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags)
4693 {
4694         int ret;
4695
4696         /* sync_file_range always requires a blocking context */
4697         if (issue_flags & IO_URING_F_NONBLOCK)
4698                 return -EAGAIN;
4699
4700         ret = sync_file_range(req->file, req->sync.off, req->sync.len,
4701                                 req->sync.flags);
4702         if (ret < 0)
4703                 req_set_fail(req);
4704         io_req_complete(req, ret);
4705         return 0;
4706 }
4707
4708 #if defined(CONFIG_NET)
4709 static int io_setup_async_msg(struct io_kiocb *req,
4710                               struct io_async_msghdr *kmsg)
4711 {
4712         struct io_async_msghdr *async_msg = req->async_data;
4713
4714         if (async_msg)
4715                 return -EAGAIN;
4716         if (io_alloc_async_data(req)) {
4717                 kfree(kmsg->free_iov);
4718                 return -ENOMEM;
4719         }
4720         async_msg = req->async_data;
4721         req->flags |= REQ_F_NEED_CLEANUP;
4722         memcpy(async_msg, kmsg, sizeof(*kmsg));
4723         async_msg->msg.msg_name = &async_msg->addr;
4724         /* if were using fast_iov, set it to the new one */
4725         if (!async_msg->free_iov)
4726                 async_msg->msg.msg_iter.iov = async_msg->fast_iov;
4727
4728         return -EAGAIN;
4729 }
4730
4731 static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4732                                struct io_async_msghdr *iomsg)
4733 {
4734         iomsg->msg.msg_name = &iomsg->addr;
4735         iomsg->free_iov = iomsg->fast_iov;
4736         return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
4737                                    req->sr_msg.msg_flags, &iomsg->free_iov);
4738 }
4739
4740 static int io_sendmsg_prep_async(struct io_kiocb *req)
4741 {
4742         int ret;
4743
4744         ret = io_sendmsg_copy_hdr(req, req->async_data);
4745         if (!ret)
4746                 req->flags |= REQ_F_NEED_CLEANUP;
4747         return ret;
4748 }
4749
4750 static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4751 {
4752         struct io_sr_msg *sr = &req->sr_msg;
4753
4754         if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4755                 return -EINVAL;
4756
4757         sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
4758         sr->len = READ_ONCE(sqe->len);
4759         sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL;
4760         if (sr->msg_flags & MSG_DONTWAIT)
4761                 req->flags |= REQ_F_NOWAIT;
4762
4763 #ifdef CONFIG_COMPAT
4764         if (req->ctx->compat)
4765                 sr->msg_flags |= MSG_CMSG_COMPAT;
4766 #endif
4767         return 0;
4768 }
4769
4770 static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
4771 {
4772         struct io_async_msghdr iomsg, *kmsg;
4773         struct socket *sock;
4774         unsigned flags;
4775         int min_ret = 0;
4776         int ret;
4777
4778         sock = sock_from_file(req->file);
4779         if (unlikely(!sock))
4780                 return -ENOTSOCK;
4781
4782         if (req_has_async_data(req)) {
4783                 kmsg = req->async_data;
4784         } else {
4785                 ret = io_sendmsg_copy_hdr(req, &iomsg);
4786                 if (ret)
4787                         return ret;
4788                 kmsg = &iomsg;
4789         }
4790
4791         flags = req->sr_msg.msg_flags;
4792         if (issue_flags & IO_URING_F_NONBLOCK)
4793                 flags |= MSG_DONTWAIT;
4794         if (flags & MSG_WAITALL)
4795                 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
4796
4797         ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
4798         if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
4799                 return io_setup_async_msg(req, kmsg);
4800         if (ret == -ERESTARTSYS)
4801                 ret = -EINTR;
4802
4803         /* fast path, check for non-NULL to avoid function call */
4804         if (kmsg->free_iov)
4805                 kfree(kmsg->free_iov);
4806         req->flags &= ~REQ_F_NEED_CLEANUP;
4807         if (ret < min_ret)
4808                 req_set_fail(req);
4809         __io_req_complete(req, issue_flags, ret, 0);
4810         return 0;
4811 }
4812
4813 static int io_send(struct io_kiocb *req, unsigned int issue_flags)
4814 {
4815         struct io_sr_msg *sr = &req->sr_msg;
4816         struct msghdr msg;
4817         struct iovec iov;
4818         struct socket *sock;
4819         unsigned flags;
4820         int min_ret = 0;
4821         int ret;
4822
4823         sock = sock_from_file(req->file);
4824         if (unlikely(!sock))
4825                 return -ENOTSOCK;
4826
4827         ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4828         if (unlikely(ret))
4829                 return ret;
4830
4831         msg.msg_name = NULL;
4832         msg.msg_control = NULL;
4833         msg.msg_controllen = 0;
4834         msg.msg_namelen = 0;
4835
4836         flags = req->sr_msg.msg_flags;
4837         if (issue_flags & IO_URING_F_NONBLOCK)
4838                 flags |= MSG_DONTWAIT;
4839         if (flags & MSG_WAITALL)
4840                 min_ret = iov_iter_count(&msg.msg_iter);
4841
4842         msg.msg_flags = flags;
4843         ret = sock_sendmsg(sock, &msg);
4844         if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
4845                 return -EAGAIN;
4846         if (ret == -ERESTARTSYS)
4847                 ret = -EINTR;
4848
4849         if (ret < min_ret)
4850                 req_set_fail(req);
4851         __io_req_complete(req, issue_flags, ret, 0);
4852         return 0;
4853 }
4854
4855 static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4856                                  struct io_async_msghdr *iomsg)
4857 {
4858         struct io_sr_msg *sr = &req->sr_msg;
4859         struct iovec __user *uiov;
4860         size_t iov_len;
4861         int ret;
4862
4863         ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4864                                         &iomsg->uaddr, &uiov, &iov_len);
4865         if (ret)
4866                 return ret;
4867
4868         if (req->flags & REQ_F_BUFFER_SELECT) {
4869                 if (iov_len > 1)
4870                         return -EINVAL;
4871                 if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov)))
4872                         return -EFAULT;
4873                 sr->len = iomsg->fast_iov[0].iov_len;
4874                 iomsg->free_iov = NULL;
4875         } else {
4876                 iomsg->free_iov = iomsg->fast_iov;
4877                 ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
4878                                      &iomsg->free_iov, &iomsg->msg.msg_iter,
4879                                      false);
4880                 if (ret > 0)
4881                         ret = 0;
4882         }
4883
4884         return ret;
4885 }
4886
4887 #ifdef CONFIG_COMPAT
4888 static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
4889                                         struct io_async_msghdr *iomsg)
4890 {
4891         struct io_sr_msg *sr = &req->sr_msg;
4892         struct compat_iovec __user *uiov;
4893         compat_uptr_t ptr;
4894         compat_size_t len;
4895         int ret;
4896
4897         ret = __get_compat_msghdr(&iomsg->msg, sr->umsg_compat, &iomsg->uaddr,
4898                                   &ptr, &len);
4899         if (ret)
4900                 return ret;
4901
4902         uiov = compat_ptr(ptr);
4903         if (req->flags & REQ_F_BUFFER_SELECT) {
4904                 compat_ssize_t clen;
4905
4906                 if (len > 1)
4907                         return -EINVAL;
4908                 if (!access_ok(uiov, sizeof(*uiov)))
4909                         return -EFAULT;
4910                 if (__get_user(clen, &uiov->iov_len))
4911                         return -EFAULT;
4912                 if (clen < 0)
4913                         return -EINVAL;
4914                 sr->len = clen;
4915                 iomsg->free_iov = NULL;
4916         } else {
4917                 iomsg->free_iov = iomsg->fast_iov;
4918                 ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
4919                                    UIO_FASTIOV, &iomsg->free_iov,
4920                                    &iomsg->msg.msg_iter, true);
4921                 if (ret < 0)
4922                         return ret;
4923         }
4924
4925         return 0;
4926 }
4927 #endif
4928
4929 static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4930                                struct io_async_msghdr *iomsg)
4931 {
4932         iomsg->msg.msg_name = &iomsg->addr;
4933
4934 #ifdef CONFIG_COMPAT
4935         if (req->ctx->compat)
4936                 return __io_compat_recvmsg_copy_hdr(req, iomsg);
4937 #endif
4938
4939         return __io_recvmsg_copy_hdr(req, iomsg);
4940 }
4941
4942 static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
4943                                                unsigned int issue_flags)
4944 {
4945         struct io_sr_msg *sr = &req->sr_msg;
4946
4947         return io_buffer_select(req, &sr->len, sr->bgid, issue_flags);
4948 }
4949
4950 static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4951 {
4952         return io_put_kbuf(req, req->kbuf);
4953 }
4954
4955 static int io_recvmsg_prep_async(struct io_kiocb *req)
4956 {
4957         int ret;
4958
4959         ret = io_recvmsg_copy_hdr(req, req->async_data);
4960         if (!ret)
4961                 req->flags |= REQ_F_NEED_CLEANUP;
4962         return ret;
4963 }
4964
4965 static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4966 {
4967         struct io_sr_msg *sr = &req->sr_msg;
4968
4969         if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4970                 return -EINVAL;
4971
4972         sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
4973         sr->len = READ_ONCE(sqe->len);
4974         sr->bgid = READ_ONCE(sqe->buf_group);
4975         sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL;
4976         if (sr->msg_flags & MSG_DONTWAIT)
4977                 req->flags |= REQ_F_NOWAIT;
4978
4979 #ifdef CONFIG_COMPAT
4980         if (req->ctx->compat)
4981                 sr->msg_flags |= MSG_CMSG_COMPAT;
4982 #endif
4983         return 0;
4984 }
4985
4986 static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
4987 {
4988         struct io_async_msghdr iomsg, *kmsg;
4989         struct socket *sock;
4990         struct io_buffer *kbuf;
4991         unsigned flags;
4992         int min_ret = 0;
4993         int ret, cflags = 0;
4994         bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
4995
4996         sock = sock_from_file(req->file);
4997         if (unlikely(!sock))
4998                 return -ENOTSOCK;
4999
5000         if (req_has_async_data(req)) {
5001                 kmsg = req->async_data;
5002         } else {
5003                 ret = io_recvmsg_copy_hdr(req, &iomsg);
5004                 if (ret)
5005                         return ret;
5006                 kmsg = &iomsg;
5007         }
5008
5009         if (req->flags & REQ_F_BUFFER_SELECT) {
5010                 kbuf = io_recv_buffer_select(req, issue_flags);
5011                 if (IS_ERR(kbuf))
5012                         return PTR_ERR(kbuf);
5013                 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
5014                 kmsg->fast_iov[0].iov_len = req->sr_msg.len;
5015                 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov,
5016                                 1, req->sr_msg.len);
5017         }
5018
5019         flags = req->sr_msg.msg_flags;
5020         if (force_nonblock)
5021                 flags |= MSG_DONTWAIT;
5022         if (flags & MSG_WAITALL)
5023                 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
5024
5025         ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
5026                                         kmsg->uaddr, flags);
5027         if (force_nonblock && ret == -EAGAIN)
5028                 return io_setup_async_msg(req, kmsg);
5029         if (ret == -ERESTARTSYS)
5030                 ret = -EINTR;
5031
5032         if (req->flags & REQ_F_BUFFER_SELECTED)
5033                 cflags = io_put_recv_kbuf(req);
5034         /* fast path, check for non-NULL to avoid function call */
5035         if (kmsg->free_iov)
5036                 kfree(kmsg->free_iov);
5037         req->flags &= ~REQ_F_NEED_CLEANUP;
5038         if (ret < min_ret || ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))))
5039                 req_set_fail(req);
5040         __io_req_complete(req, issue_flags, ret, cflags);
5041         return 0;
5042 }
5043
5044 static int io_recv(struct io_kiocb *req, unsigned int issue_flags)
5045 {
5046         struct io_buffer *kbuf;
5047         struct io_sr_msg *sr = &req->sr_msg;
5048         struct msghdr msg;
5049         void __user *buf = sr->buf;
5050         struct socket *sock;
5051         struct iovec iov;
5052         unsigned flags;
5053         int min_ret = 0;
5054         int ret, cflags = 0;
5055         bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
5056
5057         sock = sock_from_file(req->file);
5058         if (unlikely(!sock))
5059                 return -ENOTSOCK;
5060
5061         if (req->flags & REQ_F_BUFFER_SELECT) {
5062                 kbuf = io_recv_buffer_select(req, issue_flags);
5063                 if (IS_ERR(kbuf))
5064                         return PTR_ERR(kbuf);
5065                 buf = u64_to_user_ptr(kbuf->addr);
5066         }
5067
5068         ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
5069         if (unlikely(ret))
5070                 goto out_free;
5071
5072         msg.msg_name = NULL;
5073         msg.msg_control = NULL;
5074         msg.msg_controllen = 0;
5075         msg.msg_namelen = 0;
5076         msg.msg_iocb = NULL;
5077         msg.msg_flags = 0;
5078
5079         flags = req->sr_msg.msg_flags;
5080         if (force_nonblock)
5081                 flags |= MSG_DONTWAIT;
5082         if (flags & MSG_WAITALL)
5083                 min_ret = iov_iter_count(&msg.msg_iter);
5084
5085         ret = sock_recvmsg(sock, &msg, flags);
5086         if (force_nonblock && ret == -EAGAIN)
5087                 return -EAGAIN;
5088         if (ret == -ERESTARTSYS)
5089                 ret = -EINTR;
5090 out_free:
5091         if (req->flags & REQ_F_BUFFER_SELECTED)
5092                 cflags = io_put_recv_kbuf(req);
5093         if (ret < min_ret || ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))))
5094                 req_set_fail(req);
5095         __io_req_complete(req, issue_flags, ret, cflags);
5096         return 0;
5097 }
5098
5099 static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5100 {
5101         struct io_accept *accept = &req->accept;
5102
5103         if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5104                 return -EINVAL;
5105         if (sqe->ioprio || sqe->len || sqe->buf_index)
5106                 return -EINVAL;
5107
5108         accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
5109         accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
5110         accept->flags = READ_ONCE(sqe->accept_flags);
5111         accept->nofile = rlimit(RLIMIT_NOFILE);
5112
5113         accept->file_slot = READ_ONCE(sqe->file_index);
5114         if (accept->file_slot && ((req->open.how.flags & O_CLOEXEC) ||
5115                                   (accept->flags & SOCK_CLOEXEC)))
5116                 return -EINVAL;
5117         if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
5118                 return -EINVAL;
5119         if (SOCK_NONBLOCK != O_NONBLOCK && (accept->flags & SOCK_NONBLOCK))
5120                 accept->flags = (accept->flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
5121         return 0;
5122 }
5123
5124 static int io_accept(struct io_kiocb *req, unsigned int issue_flags)
5125 {
5126         struct io_accept *accept = &req->accept;
5127         bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
5128         unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
5129         bool fixed = !!accept->file_slot;
5130         struct file *file;
5131         int ret, fd;
5132
5133         if (req->file->f_flags & O_NONBLOCK)
5134                 req->flags |= REQ_F_NOWAIT;
5135
5136         if (!fixed) {
5137                 fd = __get_unused_fd_flags(accept->flags, accept->nofile);
5138                 if (unlikely(fd < 0))
5139                         return fd;
5140         }
5141         file = do_accept(req->file, file_flags, accept->addr, accept->addr_len,
5142                          accept->flags);
5143         if (IS_ERR(file)) {
5144                 if (!fixed)
5145                         put_unused_fd(fd);
5146                 ret = PTR_ERR(file);
5147                 if (ret == -EAGAIN && force_nonblock)
5148                         return -EAGAIN;
5149                 if (ret == -ERESTARTSYS)
5150                         ret = -EINTR;
5151                 req_set_fail(req);
5152         } else if (!fixed) {
5153                 fd_install(fd, file);
5154                 ret = fd;
5155         } else {
5156                 ret = io_install_fixed_file(req, file, issue_flags,
5157                                             accept->file_slot - 1);
5158         }
5159         __io_req_complete(req, issue_flags, ret, 0);
5160         return 0;
5161 }
5162
5163 static int io_connect_prep_async(struct io_kiocb *req)
5164 {
5165         struct io_async_connect *io = req->async_data;
5166         struct io_connect *conn = &req->connect;
5167
5168         return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address);
5169 }
5170
5171 static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5172 {
5173         struct io_connect *conn = &req->connect;
5174
5175         if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5176                 return -EINVAL;
5177         if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags ||
5178             sqe->splice_fd_in)
5179                 return -EINVAL;
5180
5181         conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
5182         conn->addr_len =  READ_ONCE(sqe->addr2);
5183         return 0;
5184 }
5185
5186 static int io_connect(struct io_kiocb *req, unsigned int issue_flags)
5187 {
5188         struct io_async_connect __io, *io;
5189         unsigned file_flags;
5190         int ret;
5191         bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
5192
5193         if (req_has_async_data(req)) {
5194                 io = req->async_data;
5195         } else {
5196                 ret = move_addr_to_kernel(req->connect.addr,
5197                                                 req->connect.addr_len,
5198                                                 &__io.address);
5199                 if (ret)
5200                         goto out;
5201                 io = &__io;
5202         }
5203
5204         file_flags = force_nonblock ? O_NONBLOCK : 0;
5205
5206         ret = __sys_connect_file(req->file, &io->address,
5207                                         req->connect.addr_len, file_flags);
5208         if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
5209                 if (req_has_async_data(req))
5210                         return -EAGAIN;
5211                 if (io_alloc_async_data(req)) {
5212                         ret = -ENOMEM;
5213                         goto out;
5214                 }
5215                 memcpy(req->async_data, &__io, sizeof(__io));
5216                 return -EAGAIN;
5217         }
5218         if (ret == -ERESTARTSYS)
5219                 ret = -EINTR;
5220 out:
5221         if (ret < 0)
5222                 req_set_fail(req);
5223         __io_req_complete(req, issue_flags, ret, 0);
5224         return 0;
5225 }
5226 #else /* !CONFIG_NET */
5227 #define IO_NETOP_FN(op)                                                 \
5228 static int io_##op(struct io_kiocb *req, unsigned int issue_flags)      \
5229 {                                                                       \
5230         return -EOPNOTSUPP;                                             \
5231 }
5232
5233 #define IO_NETOP_PREP(op)                                               \
5234 IO_NETOP_FN(op)                                                         \
5235 static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \
5236 {                                                                       \
5237         return -EOPNOTSUPP;                                             \
5238 }                                                                       \
5239
5240 #define IO_NETOP_PREP_ASYNC(op)                                         \
5241 IO_NETOP_PREP(op)                                                       \
5242 static int io_##op##_prep_async(struct io_kiocb *req)                   \
5243 {                                                                       \
5244         return -EOPNOTSUPP;                                             \
5245 }
5246
5247 IO_NETOP_PREP_ASYNC(sendmsg);
5248 IO_NETOP_PREP_ASYNC(recvmsg);
5249 IO_NETOP_PREP_ASYNC(connect);
5250 IO_NETOP_PREP(accept);
5251 IO_NETOP_FN(send);
5252 IO_NETOP_FN(recv);
5253 #endif /* CONFIG_NET */
5254
5255 struct io_poll_table {
5256         struct poll_table_struct pt;
5257         struct io_kiocb *req;
5258         int nr_entries;
5259         int error;
5260 };
5261
5262 static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
5263                            __poll_t mask, io_req_tw_func_t func)
5264 {
5265         /* for instances that support it check for an event match first: */
5266         if (mask && !(mask & poll->events))
5267                 return 0;
5268
5269         trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
5270
5271         list_del_init(&poll->wait.entry);
5272
5273         req->result = mask;
5274         req->io_task_work.func = func;
5275
5276         /*
5277          * If this fails, then the task is exiting. When a task exits, the
5278          * work gets canceled, so just cancel this request as well instead
5279          * of executing it. We can't safely execute it anyway, as we may not
5280          * have the needed state needed for it anyway.
5281          */
5282         io_req_task_work_add(req);
5283         return 1;
5284 }
5285
5286 static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
5287         __acquires(&req->ctx->completion_lock)
5288 {
5289         struct io_ring_ctx *ctx = req->ctx;
5290
5291         /* req->task == current here, checking PF_EXITING is safe */
5292         if (unlikely(req->task->flags & PF_EXITING))
5293                 WRITE_ONCE(poll->canceled, true);
5294
5295         if (!req->result && !READ_ONCE(poll->canceled)) {
5296                 struct poll_table_struct pt = { ._key = poll->events };
5297
5298                 req->result = vfs_poll(req->file, &pt) & poll->events;
5299         }
5300
5301         spin_lock(&ctx->completion_lock);
5302         if (!req->result && !READ_ONCE(poll->canceled)) {
5303                 add_wait_queue(poll->head, &poll->wait);
5304                 return true;
5305         }
5306
5307         return false;
5308 }
5309
5310 static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
5311 {
5312         /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
5313         if (req->opcode == IORING_OP_POLL_ADD)
5314                 return req->async_data;
5315         return req->apoll->double_poll;
5316 }
5317
5318 static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
5319 {
5320         if (req->opcode == IORING_OP_POLL_ADD)
5321                 return &req->poll;
5322         return &req->apoll->poll;
5323 }
5324
5325 static void io_poll_remove_double(struct io_kiocb *req)
5326         __must_hold(&req->ctx->completion_lock)
5327 {
5328         struct io_poll_iocb *poll = io_poll_get_double(req);
5329
5330         lockdep_assert_held(&req->ctx->completion_lock);
5331
5332         if (poll && poll->head) {
5333                 struct wait_queue_head *head = poll->head;
5334
5335                 spin_lock_irq(&head->lock);
5336                 list_del_init(&poll->wait.entry);
5337                 if (poll->wait.private)
5338                         req_ref_put(req);
5339                 poll->head = NULL;
5340                 spin_unlock_irq(&head->lock);
5341         }
5342 }
5343
5344 static bool __io_poll_complete(struct io_kiocb *req, __poll_t mask)
5345         __must_hold(&req->ctx->completion_lock)
5346 {
5347         struct io_ring_ctx *ctx = req->ctx;
5348         unsigned flags = IORING_CQE_F_MORE;
5349         int error;
5350
5351         if (READ_ONCE(req->poll.canceled)) {
5352                 error = -ECANCELED;
5353                 req->poll.events |= EPOLLONESHOT;
5354         } else {
5355                 error = mangle_poll(mask);
5356         }
5357         if (req->poll.events & EPOLLONESHOT)
5358                 flags = 0;
5359         if (!io_cqring_fill_event(ctx, req->user_data, error, flags)) {
5360                 req->poll.events |= EPOLLONESHOT;
5361                 flags = 0;
5362         }
5363         if (flags & IORING_CQE_F_MORE)
5364                 ctx->cq_extra++;
5365
5366         return !(flags & IORING_CQE_F_MORE);
5367 }
5368
5369 static void io_poll_task_func(struct io_kiocb *req, bool *locked)
5370 {
5371         struct io_ring_ctx *ctx = req->ctx;
5372         struct io_kiocb *nxt;
5373
5374         if (io_poll_rewait(req, &req->poll)) {
5375                 spin_unlock(&ctx->completion_lock);
5376         } else {
5377                 bool done;
5378
5379                 if (req->poll.done) {
5380                         spin_unlock(&ctx->completion_lock);
5381                         return;
5382                 }
5383                 done = __io_poll_complete(req, req->result);
5384                 if (done) {
5385                         io_poll_remove_double(req);
5386                         hash_del(&req->hash_node);
5387                         req->poll.done = true;
5388                 } else {
5389                         req->result = 0;
5390                         add_wait_queue(req->poll.head, &req->poll.wait);
5391                 }
5392                 io_commit_cqring(ctx);
5393                 spin_unlock(&ctx->completion_lock);
5394                 io_cqring_ev_posted(ctx);
5395
5396                 if (done) {
5397                         nxt = io_put_req_find_next(req);
5398                         if (nxt)
5399                                 io_req_task_submit(nxt, locked);
5400                 }
5401         }
5402 }
5403
5404 static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
5405                                int sync, void *key)
5406 {
5407         struct io_kiocb *req = wait->private;
5408         struct io_poll_iocb *poll = io_poll_get_single(req);
5409         __poll_t mask = key_to_poll(key);
5410         unsigned long flags;
5411
5412         /* for instances that support it check for an event match first: */
5413         if (mask && !(mask & poll->events))
5414                 return 0;
5415         if (!(poll->events & EPOLLONESHOT))
5416                 return poll->wait.func(&poll->wait, mode, sync, key);
5417
5418         list_del_init(&wait->entry);
5419
5420         if (poll->head) {
5421                 bool done;
5422
5423                 spin_lock_irqsave(&poll->head->lock, flags);
5424                 done = list_empty(&poll->wait.entry);
5425                 if (!done)
5426                         list_del_init(&poll->wait.entry);
5427                 /* make sure double remove sees this as being gone */
5428                 wait->private = NULL;
5429                 spin_unlock_irqrestore(&poll->head->lock, flags);
5430                 if (!done) {
5431                         /* use wait func handler, so it matches the rq type */
5432                         poll->wait.func(&poll->wait, mode, sync, key);
5433                 }
5434         }
5435         req_ref_put(req);
5436         return 1;
5437 }
5438
5439 static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
5440                               wait_queue_func_t wake_func)
5441 {
5442         poll->head = NULL;
5443         poll->done = false;
5444         poll->canceled = false;
5445 #define IO_POLL_UNMASK  (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP)
5446         /* mask in events that we always want/need */
5447         poll->events = events | IO_POLL_UNMASK;
5448         INIT_LIST_HEAD(&poll->wait.entry);
5449         init_waitqueue_func_entry(&poll->wait, wake_func);
5450 }
5451
5452 static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
5453                             struct wait_queue_head *head,
5454                             struct io_poll_iocb **poll_ptr)
5455 {
5456         struct io_kiocb *req = pt->req;
5457
5458         /*
5459          * The file being polled uses multiple waitqueues for poll handling
5460          * (e.g. one for read, one for write). Setup a separate io_poll_iocb
5461          * if this happens.
5462          */
5463         if (unlikely(pt->nr_entries)) {
5464                 struct io_poll_iocb *poll_one = poll;
5465
5466                 /* double add on the same waitqueue head, ignore */
5467                 if (poll_one->head == head)
5468                         return;
5469                 /* already have a 2nd entry, fail a third attempt */
5470                 if (*poll_ptr) {
5471                         if ((*poll_ptr)->head == head)
5472                                 return;
5473                         pt->error = -EINVAL;
5474                         return;
5475                 }
5476                 /*
5477                  * Can't handle multishot for double wait for now, turn it
5478                  * into one-shot mode.
5479                  */
5480                 if (!(poll_one->events & EPOLLONESHOT))
5481                         poll_one->events |= EPOLLONESHOT;
5482                 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
5483                 if (!poll) {
5484                         pt->error = -ENOMEM;
5485                         return;
5486                 }
5487                 io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake);
5488                 req_ref_get(req);
5489                 poll->wait.private = req;
5490
5491                 *poll_ptr = poll;
5492                 if (req->opcode == IORING_OP_POLL_ADD)
5493                         req->flags |= REQ_F_ASYNC_DATA;
5494         }
5495
5496         pt->nr_entries++;
5497         poll->head = head;
5498
5499         if (poll->events & EPOLLEXCLUSIVE)
5500                 add_wait_queue_exclusive(head, &poll->wait);
5501         else
5502                 add_wait_queue(head, &poll->wait);
5503 }
5504
5505 static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
5506                                struct poll_table_struct *p)
5507 {
5508         struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5509         struct async_poll *apoll = pt->req->apoll;
5510
5511         __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
5512 }
5513
5514 static void io_async_task_func(struct io_kiocb *req, bool *locked)
5515 {
5516         struct async_poll *apoll = req->apoll;
5517         struct io_ring_ctx *ctx = req->ctx;
5518
5519         trace_io_uring_task_run(req->ctx, req, req->opcode, req->user_data);
5520
5521         if (io_poll_rewait(req, &apoll->poll)) {
5522                 spin_unlock(&ctx->completion_lock);
5523                 return;
5524         }
5525
5526         hash_del(&req->hash_node);
5527         io_poll_remove_double(req);
5528         apoll->poll.done = true;
5529         spin_unlock(&ctx->completion_lock);
5530
5531         if (!READ_ONCE(apoll->poll.canceled))
5532                 io_req_task_submit(req, locked);
5533         else
5534                 io_req_complete_failed(req, -ECANCELED);
5535 }
5536
5537 static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5538                         void *key)
5539 {
5540         struct io_kiocb *req = wait->private;
5541         struct io_poll_iocb *poll = &req->apoll->poll;
5542
5543         trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
5544                                         key_to_poll(key));
5545
5546         return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
5547 }
5548
5549 static void io_poll_req_insert(struct io_kiocb *req)
5550 {
5551         struct io_ring_ctx *ctx = req->ctx;
5552         struct hlist_head *list;
5553
5554         list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5555         hlist_add_head(&req->hash_node, list);
5556 }
5557
5558 static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5559                                       struct io_poll_iocb *poll,
5560                                       struct io_poll_table *ipt, __poll_t mask,
5561                                       wait_queue_func_t wake_func)
5562         __acquires(&ctx->completion_lock)
5563 {
5564         struct io_ring_ctx *ctx = req->ctx;
5565         bool cancel = false;
5566
5567         INIT_HLIST_NODE(&req->hash_node);
5568         io_init_poll_iocb(poll, mask, wake_func);
5569         poll->file = req->file;
5570         poll->wait.private = req;
5571
5572         ipt->pt._key = mask;
5573         ipt->req = req;
5574         ipt->error = 0;
5575         ipt->nr_entries = 0;
5576
5577         mask = vfs_poll(req->file, &ipt->pt) & poll->events;
5578         if (unlikely(!ipt->nr_entries) && !ipt->error)
5579                 ipt->error = -EINVAL;
5580
5581         spin_lock(&ctx->completion_lock);
5582         if (ipt->error || (mask && (poll->events & EPOLLONESHOT)))
5583                 io_poll_remove_double(req);
5584         if (likely(poll->head)) {
5585                 spin_lock_irq(&poll->head->lock);
5586                 if (unlikely(list_empty(&poll->wait.entry))) {
5587                         if (ipt->error)
5588                                 cancel = true;
5589                         ipt->error = 0;
5590                         mask = 0;
5591                 }
5592                 if ((mask && (poll->events & EPOLLONESHOT)) || ipt->error)
5593                         list_del_init(&poll->wait.entry);
5594                 else if (cancel)
5595                         WRITE_ONCE(poll->canceled, true);
5596                 else if (!poll->done) /* actually waiting for an event */
5597                         io_poll_req_insert(req);
5598                 spin_unlock_irq(&poll->head->lock);
5599         }
5600
5601         return mask;
5602 }
5603
5604 enum {
5605         IO_APOLL_OK,
5606         IO_APOLL_ABORTED,
5607         IO_APOLL_READY
5608 };
5609
5610 static int io_arm_poll_handler(struct io_kiocb *req)
5611 {
5612         const struct io_op_def *def = &io_op_defs[req->opcode];
5613         struct io_ring_ctx *ctx = req->ctx;
5614         struct async_poll *apoll;
5615         struct io_poll_table ipt;
5616         __poll_t ret, mask = EPOLLONESHOT | POLLERR | POLLPRI;
5617
5618         if (!def->pollin && !def->pollout)
5619                 return IO_APOLL_ABORTED;
5620         if (!file_can_poll(req->file) || (req->flags & REQ_F_POLLED))
5621                 return IO_APOLL_ABORTED;
5622
5623         if (def->pollin) {
5624                 mask |= POLLIN | POLLRDNORM;
5625
5626                 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5627                 if ((req->opcode == IORING_OP_RECVMSG) &&
5628                     (req->sr_msg.msg_flags & MSG_ERRQUEUE))
5629                         mask &= ~POLLIN;
5630         } else {
5631                 mask |= POLLOUT | POLLWRNORM;
5632         }
5633
5634         apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5635         if (unlikely(!apoll))
5636                 return IO_APOLL_ABORTED;
5637         apoll->double_poll = NULL;
5638         req->apoll = apoll;
5639         req->flags |= REQ_F_POLLED;
5640         ipt.pt._qproc = io_async_queue_proc;
5641         io_req_set_refcount(req);
5642
5643         ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5644                                         io_async_wake);
5645         spin_unlock(&ctx->completion_lock);
5646         if (ret || ipt.error)
5647                 return ret ? IO_APOLL_READY : IO_APOLL_ABORTED;
5648
5649         trace_io_uring_poll_arm(ctx, req, req->opcode, req->user_data,
5650                                 mask, apoll->poll.events);
5651         return IO_APOLL_OK;
5652 }
5653
5654 static bool __io_poll_remove_one(struct io_kiocb *req,
5655                                  struct io_poll_iocb *poll, bool do_cancel)
5656         __must_hold(&req->ctx->completion_lock)
5657 {
5658         bool do_complete = false;
5659
5660         if (!poll->head)
5661                 return false;
5662         spin_lock_irq(&poll->head->lock);
5663         if (do_cancel)
5664                 WRITE_ONCE(poll->canceled, true);
5665         if (!list_empty(&poll->wait.entry)) {
5666                 list_del_init(&poll->wait.entry);
5667                 do_complete = true;
5668         }
5669         spin_unlock_irq(&poll->head->lock);
5670         hash_del(&req->hash_node);
5671         return do_complete;
5672 }
5673
5674 static bool io_poll_remove_one(struct io_kiocb *req)
5675         __must_hold(&req->ctx->completion_lock)
5676 {
5677         bool do_complete;
5678
5679         io_poll_remove_double(req);
5680         do_complete = __io_poll_remove_one(req, io_poll_get_single(req), true);
5681
5682         if (do_complete) {
5683                 io_cqring_fill_event(req->ctx, req->user_data, -ECANCELED, 0);
5684                 io_commit_cqring(req->ctx);
5685                 req_set_fail(req);
5686                 io_put_req_deferred(req);
5687         }
5688         return do_complete;
5689 }
5690
5691 /*
5692  * Returns true if we found and killed one or more poll requests
5693  */
5694 static __cold bool io_poll_remove_all(struct io_ring_ctx *ctx,
5695                                       struct task_struct *tsk, bool cancel_all)
5696 {
5697         struct hlist_node *tmp;
5698         struct io_kiocb *req;
5699         int posted = 0, i;
5700
5701         spin_lock(&ctx->completion_lock);
5702         spin_lock_irq(&ctx->timeout_lock);
5703         for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5704                 struct hlist_head *list;
5705
5706                 list = &ctx->cancel_hash[i];
5707                 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
5708                         if (io_match_task(req, tsk, cancel_all))
5709                                 posted += io_poll_remove_one(req);
5710                 }
5711         }
5712         spin_unlock_irq(&ctx->timeout_lock);
5713         spin_unlock(&ctx->completion_lock);
5714
5715         if (posted)
5716                 io_cqring_ev_posted(ctx);
5717
5718         return posted != 0;
5719 }
5720
5721 static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr,
5722                                      bool poll_only)
5723         __must_hold(&ctx->completion_lock)
5724 {
5725         struct hlist_head *list;
5726         struct io_kiocb *req;
5727
5728         list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5729         hlist_for_each_entry(req, list, hash_node) {
5730                 if (sqe_addr != req->user_data)
5731                         continue;
5732                 if (poll_only && req->opcode != IORING_OP_POLL_ADD)
5733                         continue;
5734                 return req;
5735         }
5736         return NULL;
5737 }
5738
5739 static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr,
5740                           bool poll_only)
5741         __must_hold(&ctx->completion_lock)
5742 {
5743         struct io_kiocb *req;
5744
5745         req = io_poll_find(ctx, sqe_addr, poll_only);
5746         if (!req)
5747                 return -ENOENT;
5748         if (io_poll_remove_one(req))
5749                 return 0;
5750
5751         return -EALREADY;
5752 }
5753
5754 static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe,
5755                                      unsigned int flags)
5756 {
5757         u32 events;
5758
5759         events = READ_ONCE(sqe->poll32_events);
5760 #ifdef __BIG_ENDIAN
5761         events = swahw32(events);
5762 #endif
5763         if (!(flags & IORING_POLL_ADD_MULTI))
5764                 events |= EPOLLONESHOT;
5765         return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT));
5766 }
5767
5768 static int io_poll_update_prep(struct io_kiocb *req,
5769                                const struct io_uring_sqe *sqe)
5770 {
5771         struct io_poll_update *upd = &req->poll_update;
5772         u32 flags;
5773
5774         if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5775                 return -EINVAL;
5776         if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
5777                 return -EINVAL;
5778         flags = READ_ONCE(sqe->len);
5779         if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA |
5780                       IORING_POLL_ADD_MULTI))
5781                 return -EINVAL;
5782         /* meaningless without update */
5783         if (flags == IORING_POLL_ADD_MULTI)
5784                 return -EINVAL;
5785
5786         upd->old_user_data = READ_ONCE(sqe->addr);
5787         upd->update_events = flags & IORING_POLL_UPDATE_EVENTS;
5788         upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA;
5789
5790         upd->new_user_data = READ_ONCE(sqe->off);
5791         if (!upd->update_user_data && upd->new_user_data)
5792                 return -EINVAL;
5793         if (upd->update_events)
5794                 upd->events = io_poll_parse_events(sqe, flags);
5795         else if (sqe->poll32_events)
5796                 return -EINVAL;
5797
5798         return 0;
5799 }
5800
5801 static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5802                         void *key)
5803 {
5804         struct io_kiocb *req = wait->private;
5805         struct io_poll_iocb *poll = &req->poll;
5806
5807         return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
5808 }
5809
5810 static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5811                                struct poll_table_struct *p)
5812 {
5813         struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5814
5815         __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
5816 }
5817
5818 static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5819 {
5820         struct io_poll_iocb *poll = &req->poll;
5821         u32 flags;
5822
5823         if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5824                 return -EINVAL;
5825         if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->addr)
5826                 return -EINVAL;
5827         flags = READ_ONCE(sqe->len);
5828         if (flags & ~IORING_POLL_ADD_MULTI)
5829                 return -EINVAL;
5830
5831         io_req_set_refcount(req);
5832         poll->events = io_poll_parse_events(sqe, flags);
5833         return 0;
5834 }
5835
5836 static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
5837 {
5838         struct io_poll_iocb *poll = &req->poll;
5839         struct io_ring_ctx *ctx = req->ctx;
5840         struct io_poll_table ipt;
5841         __poll_t mask;
5842         bool done;
5843
5844         ipt.pt._qproc = io_poll_queue_proc;
5845
5846         mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5847                                         io_poll_wake);
5848
5849         if (mask) { /* no async, we'd stolen it */
5850                 ipt.error = 0;
5851                 done = __io_poll_complete(req, mask);
5852                 io_commit_cqring(req->ctx);
5853         }
5854         spin_unlock(&ctx->completion_lock);
5855
5856         if (mask) {
5857                 io_cqring_ev_posted(ctx);
5858                 if (done)
5859                         io_put_req(req);
5860         }
5861         return ipt.error;
5862 }
5863
5864 static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags)
5865 {
5866         struct io_ring_ctx *ctx = req->ctx;
5867         struct io_kiocb *preq;
5868         bool completing;
5869         int ret;
5870
5871         spin_lock(&ctx->completion_lock);
5872         preq = io_poll_find(ctx, req->poll_update.old_user_data, true);
5873         if (!preq) {
5874                 ret = -ENOENT;
5875                 goto err;
5876         }
5877
5878         if (!req->poll_update.update_events && !req->poll_update.update_user_data) {
5879                 completing = true;
5880                 ret = io_poll_remove_one(preq) ? 0 : -EALREADY;
5881                 goto err;
5882         }
5883
5884         /*
5885          * Don't allow racy completion with singleshot, as we cannot safely
5886          * update those. For multishot, if we're racing with completion, just
5887          * let completion re-add it.
5888          */
5889         completing = !__io_poll_remove_one(preq, &preq->poll, false);
5890         if (completing && (preq->poll.events & EPOLLONESHOT)) {
5891                 ret = -EALREADY;
5892                 goto err;
5893         }
5894         /* we now have a detached poll request. reissue. */
5895         ret = 0;
5896 err:
5897         if (ret < 0) {
5898                 spin_unlock(&ctx->completion_lock);
5899                 req_set_fail(req);
5900                 io_req_complete(req, ret);
5901                 return 0;
5902         }
5903         /* only mask one event flags, keep behavior flags */
5904         if (req->poll_update.update_events) {
5905                 preq->poll.events &= ~0xffff;
5906                 preq->poll.events |= req->poll_update.events & 0xffff;
5907                 preq->poll.events |= IO_POLL_UNMASK;
5908         }
5909         if (req->poll_update.update_user_data)
5910                 preq->user_data = req->poll_update.new_user_data;
5911         spin_unlock(&ctx->completion_lock);
5912
5913         /* complete update request, we're done with it */
5914         io_req_complete(req, ret);
5915
5916         if (!completing) {
5917                 ret = io_poll_add(preq, issue_flags);
5918                 if (ret < 0) {
5919                         req_set_fail(preq);
5920                         io_req_complete(preq, ret);
5921                 }
5922         }
5923         return 0;
5924 }
5925
5926 static void io_req_task_timeout(struct io_kiocb *req, bool *locked)
5927 {
5928         struct io_timeout_data *data = req->async_data;
5929
5930         if (!(data->flags & IORING_TIMEOUT_ETIME_SUCCESS))
5931                 req_set_fail(req);
5932         io_req_complete_post(req, -ETIME, 0);
5933 }
5934
5935 static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5936 {
5937         struct io_timeout_data *data = container_of(timer,
5938                                                 struct io_timeout_data, timer);
5939         struct io_kiocb *req = data->req;
5940         struct io_ring_ctx *ctx = req->ctx;
5941         unsigned long flags;
5942
5943         spin_lock_irqsave(&ctx->timeout_lock, flags);
5944         list_del_init(&req->timeout.list);
5945         atomic_set(&req->ctx->cq_timeouts,
5946                 atomic_read(&req->ctx->cq_timeouts) + 1);
5947         spin_unlock_irqrestore(&ctx->timeout_lock, flags);
5948
5949         req->io_task_work.func = io_req_task_timeout;
5950         io_req_task_work_add(req);
5951         return HRTIMER_NORESTART;
5952 }
5953
5954 static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
5955                                            __u64 user_data)
5956         __must_hold(&ctx->timeout_lock)
5957 {
5958         struct io_timeout_data *io;
5959         struct io_kiocb *req;
5960         bool found = false;
5961
5962         list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5963                 found = user_data == req->user_data;
5964                 if (found)
5965                         break;
5966         }
5967         if (!found)
5968                 return ERR_PTR(-ENOENT);
5969
5970         io = req->async_data;
5971         if (hrtimer_try_to_cancel(&io->timer) == -1)
5972                 return ERR_PTR(-EALREADY);
5973         list_del_init(&req->timeout.list);
5974         return req;
5975 }
5976
5977 static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5978         __must_hold(&ctx->completion_lock)
5979         __must_hold(&ctx->timeout_lock)
5980 {
5981         struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5982
5983         if (IS_ERR(req))
5984                 return PTR_ERR(req);
5985
5986         req_set_fail(req);
5987         io_cqring_fill_event(ctx, req->user_data, -ECANCELED, 0);
5988         io_put_req_deferred(req);
5989         return 0;
5990 }
5991
5992 static clockid_t io_timeout_get_clock(struct io_timeout_data *data)
5993 {
5994         switch (data->flags & IORING_TIMEOUT_CLOCK_MASK) {
5995         case IORING_TIMEOUT_BOOTTIME:
5996                 return CLOCK_BOOTTIME;
5997         case IORING_TIMEOUT_REALTIME:
5998                 return CLOCK_REALTIME;
5999         default:
6000                 /* can't happen, vetted at prep time */
6001                 WARN_ON_ONCE(1);
6002                 fallthrough;
6003         case 0:
6004                 return CLOCK_MONOTONIC;
6005         }
6006 }
6007
6008 static int io_linked_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
6009                                     struct timespec64 *ts, enum hrtimer_mode mode)
6010         __must_hold(&ctx->timeout_lock)
6011 {
6012         struct io_timeout_data *io;
6013         struct io_kiocb *req;
6014         bool found = false;
6015
6016         list_for_each_entry(req, &ctx->ltimeout_list, timeout.list) {
6017                 found = user_data == req->user_data;
6018                 if (found)
6019                         break;
6020         }
6021         if (!found)
6022                 return -ENOENT;
6023
6024         io = req->async_data;
6025         if (hrtimer_try_to_cancel(&io->timer) == -1)
6026                 return -EALREADY;
6027         hrtimer_init(&io->timer, io_timeout_get_clock(io), mode);
6028         io->timer.function = io_link_timeout_fn;
6029         hrtimer_start(&io->timer, timespec64_to_ktime(*ts), mode);
6030         return 0;
6031 }
6032
6033 static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
6034                              struct timespec64 *ts, enum hrtimer_mode mode)
6035         __must_hold(&ctx->timeout_lock)
6036 {
6037         struct io_kiocb *req = io_timeout_extract(ctx, user_data);
6038         struct io_timeout_data *data;
6039
6040         if (IS_ERR(req))
6041                 return PTR_ERR(req);
6042
6043         req->timeout.off = 0; /* noseq */
6044         data = req->async_data;
6045         list_add_tail(&req->timeout.list, &ctx->timeout_list);
6046         hrtimer_init(&data->timer, io_timeout_get_clock(data), mode);
6047         data->timer.function = io_timeout_fn;
6048         hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode);
6049         return 0;
6050 }
6051
6052 static int io_timeout_remove_prep(struct io_kiocb *req,
6053                                   const struct io_uring_sqe *sqe)
6054 {
6055         struct io_timeout_rem *tr = &req->timeout_rem;
6056
6057         if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
6058                 return -EINVAL;
6059         if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
6060                 return -EINVAL;
6061         if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->splice_fd_in)
6062                 return -EINVAL;
6063
6064         tr->ltimeout = false;
6065         tr->addr = READ_ONCE(sqe->addr);
6066         tr->flags = READ_ONCE(sqe->timeout_flags);
6067         if (tr->flags & IORING_TIMEOUT_UPDATE_MASK) {
6068                 if (hweight32(tr->flags & IORING_TIMEOUT_CLOCK_MASK) > 1)
6069                         return -EINVAL;
6070                 if (tr->flags & IORING_LINK_TIMEOUT_UPDATE)
6071                         tr->ltimeout = true;
6072                 if (tr->flags & ~(IORING_TIMEOUT_UPDATE_MASK|IORING_TIMEOUT_ABS))
6073                         return -EINVAL;
6074                 if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2)))
6075                         return -EFAULT;
6076         } else if (tr->flags) {
6077                 /* timeout removal doesn't support flags */
6078                 return -EINVAL;
6079         }
6080
6081         return 0;
6082 }
6083
6084 static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags)
6085 {
6086         return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS
6087                                             : HRTIMER_MODE_REL;
6088 }
6089
6090 /*
6091  * Remove or update an existing timeout command
6092  */
6093 static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags)
6094 {
6095         struct io_timeout_rem *tr = &req->timeout_rem;
6096         struct io_ring_ctx *ctx = req->ctx;
6097         int ret;
6098
6099         if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) {
6100                 spin_lock(&ctx->completion_lock);
6101                 spin_lock_irq(&ctx->timeout_lock);
6102                 ret = io_timeout_cancel(ctx, tr->addr);
6103                 spin_unlock_irq(&ctx->timeout_lock);
6104                 spin_unlock(&ctx->completion_lock);
6105         } else {
6106                 enum hrtimer_mode mode = io_translate_timeout_mode(tr->flags);
6107
6108                 spin_lock_irq(&ctx->timeout_lock);
6109                 if (tr->ltimeout)
6110                         ret = io_linked_timeout_update(ctx, tr->addr, &tr->ts, mode);
6111                 else
6112                         ret = io_timeout_update(ctx, tr->addr, &tr->ts, mode);
6113                 spin_unlock_irq(&ctx->timeout_lock);
6114         }
6115
6116         if (ret < 0)
6117                 req_set_fail(req);
6118         io_req_complete_post(req, ret, 0);
6119         return 0;
6120 }
6121
6122 static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6123                            bool is_timeout_link)
6124 {
6125         struct io_timeout_data *data;
6126         unsigned flags;
6127         u32 off = READ_ONCE(sqe->off);
6128
6129         if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
6130                 return -EINVAL;
6131         if (sqe->ioprio || sqe->buf_index || sqe->len != 1 ||
6132             sqe->splice_fd_in)
6133                 return -EINVAL;
6134         if (off && is_timeout_link)
6135                 return -EINVAL;
6136         flags = READ_ONCE(sqe->timeout_flags);
6137         if (flags & ~(IORING_TIMEOUT_ABS | IORING_TIMEOUT_CLOCK_MASK |
6138                       IORING_TIMEOUT_ETIME_SUCCESS))
6139                 return -EINVAL;
6140         /* more than one clock specified is invalid, obviously */
6141         if (hweight32(flags & IORING_TIMEOUT_CLOCK_MASK) > 1)
6142                 return -EINVAL;
6143
6144         INIT_LIST_HEAD(&req->timeout.list);
6145         req->timeout.off = off;
6146         if (unlikely(off && !req->ctx->off_timeout_used))
6147                 req->ctx->off_timeout_used = true;
6148
6149         if (WARN_ON_ONCE(req_has_async_data(req)))
6150                 return -EFAULT;
6151         if (io_alloc_async_data(req))
6152                 return -ENOMEM;
6153
6154         data = req->async_data;
6155         data->req = req;
6156         data->flags = flags;
6157
6158         if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
6159                 return -EFAULT;
6160
6161         data->mode = io_translate_timeout_mode(flags);
6162         hrtimer_init(&data->timer, io_timeout_get_clock(data), data->mode);
6163
6164         if (is_timeout_link) {
6165                 struct io_submit_link *link = &req->ctx->submit_state.link;
6166
6167                 if (!link->head)
6168                         return -EINVAL;
6169                 if (link->last->opcode == IORING_OP_LINK_TIMEOUT)
6170                         return -EINVAL;
6171                 req->timeout.head = link->last;
6172                 link->last->flags |= REQ_F_ARM_LTIMEOUT;
6173         }
6174         return 0;
6175 }
6176
6177 static int io_timeout(struct io_kiocb *req, unsigned int issue_flags)
6178 {
6179         struct io_ring_ctx *ctx = req->ctx;
6180         struct io_timeout_data *data = req->async_data;
6181         struct list_head *entry;
6182         u32 tail, off = req->timeout.off;
6183
6184         spin_lock_irq(&ctx->timeout_lock);
6185
6186         /*
6187          * sqe->off holds how many events that need to occur for this
6188          * timeout event to be satisfied. If it isn't set, then this is
6189          * a pure timeout request, sequence isn't used.
6190          */
6191         if (io_is_timeout_noseq(req)) {
6192                 entry = ctx->timeout_list.prev;
6193                 goto add;
6194         }
6195
6196         tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
6197         req->timeout.target_seq = tail + off;
6198
6199         /* Update the last seq here in case io_flush_timeouts() hasn't.
6200          * This is safe because ->completion_lock is held, and submissions
6201          * and completions are never mixed in the same ->completion_lock section.
6202          */
6203         ctx->cq_last_tm_flush = tail;
6204
6205         /*
6206          * Insertion sort, ensuring the first entry in the list is always
6207          * the one we need first.
6208          */
6209         list_for_each_prev(entry, &ctx->timeout_list) {
6210                 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
6211                                                   timeout.list);
6212
6213                 if (io_is_timeout_noseq(nxt))
6214                         continue;
6215                 /* nxt.seq is behind @tail, otherwise would've been completed */
6216                 if (off >= nxt->timeout.target_seq - tail)
6217                         break;
6218         }
6219 add:
6220         list_add(&req->timeout.list, entry);
6221         data->timer.function = io_timeout_fn;
6222         hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
6223         spin_unlock_irq(&ctx->timeout_lock);
6224         return 0;
6225 }
6226
6227 struct io_cancel_data {
6228         struct io_ring_ctx *ctx;
6229         u64 user_data;
6230 };
6231
6232 static bool io_cancel_cb(struct io_wq_work *work, void *data)
6233 {
6234         struct io_kiocb *req = container_of(work, struct io_kiocb, work);
6235         struct io_cancel_data *cd = data;
6236
6237         return req->ctx == cd->ctx && req->user_data == cd->user_data;
6238 }
6239
6240 static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data,
6241                                struct io_ring_ctx *ctx)
6242 {
6243         struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, };
6244         enum io_wq_cancel cancel_ret;
6245         int ret = 0;
6246
6247         if (!tctx || !tctx->io_wq)
6248                 return -ENOENT;
6249
6250         cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, &data, false);
6251         switch (cancel_ret) {
6252         case IO_WQ_CANCEL_OK:
6253                 ret = 0;
6254                 break;
6255         case IO_WQ_CANCEL_RUNNING:
6256                 ret = -EALREADY;
6257                 break;
6258         case IO_WQ_CANCEL_NOTFOUND:
6259                 ret = -ENOENT;
6260                 break;
6261         }
6262
6263         return ret;
6264 }
6265
6266 static int io_try_cancel_userdata(struct io_kiocb *req, u64 sqe_addr)
6267 {
6268         struct io_ring_ctx *ctx = req->ctx;
6269         int ret;
6270
6271         WARN_ON_ONCE(!io_wq_current_is_worker() && req->task != current);
6272
6273         ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx);
6274         if (ret != -ENOENT)
6275                 return ret;
6276
6277         spin_lock(&ctx->completion_lock);
6278         spin_lock_irq(&ctx->timeout_lock);
6279         ret = io_timeout_cancel(ctx, sqe_addr);
6280         spin_unlock_irq(&ctx->timeout_lock);
6281         if (ret != -ENOENT)
6282                 goto out;
6283         ret = io_poll_cancel(ctx, sqe_addr, false);
6284 out:
6285         spin_unlock(&ctx->completion_lock);
6286         return ret;
6287 }
6288
6289 static int io_async_cancel_prep(struct io_kiocb *req,
6290                                 const struct io_uring_sqe *sqe)
6291 {
6292         if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
6293                 return -EINVAL;
6294         if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
6295                 return -EINVAL;
6296         if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags ||
6297             sqe->splice_fd_in)
6298                 return -EINVAL;
6299
6300         req->cancel.addr = READ_ONCE(sqe->addr);
6301         return 0;
6302 }
6303
6304 static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
6305 {
6306         struct io_ring_ctx *ctx = req->ctx;
6307         u64 sqe_addr = req->cancel.addr;
6308         bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
6309         struct io_tctx_node *node;
6310         int ret;
6311
6312         ret = io_try_cancel_userdata(req, sqe_addr);
6313         if (ret != -ENOENT)
6314                 goto done;
6315
6316         /* slow path, try all io-wq's */
6317         io_ring_submit_lock(ctx, needs_lock);
6318         ret = -ENOENT;
6319         list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
6320                 struct io_uring_task *tctx = node->task->io_uring;
6321
6322                 ret = io_async_cancel_one(tctx, req->cancel.addr, ctx);
6323                 if (ret != -ENOENT)
6324                         break;
6325         }
6326         io_ring_submit_unlock(ctx, needs_lock);
6327 done:
6328         if (ret < 0)
6329                 req_set_fail(req);
6330         io_req_complete_post(req, ret, 0);
6331         return 0;
6332 }
6333
6334 static int io_rsrc_update_prep(struct io_kiocb *req,
6335                                 const struct io_uring_sqe *sqe)
6336 {
6337         if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
6338                 return -EINVAL;
6339         if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in)
6340                 return -EINVAL;
6341
6342         req->rsrc_update.offset = READ_ONCE(sqe->off);
6343         req->rsrc_update.nr_args = READ_ONCE(sqe->len);
6344         if (!req->rsrc_update.nr_args)
6345                 return -EINVAL;
6346         req->rsrc_update.arg = READ_ONCE(sqe->addr);
6347         return 0;
6348 }
6349
6350 static int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
6351 {
6352         struct io_ring_ctx *ctx = req->ctx;
6353         bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
6354         struct io_uring_rsrc_update2 up;
6355         int ret;
6356
6357         up.offset = req->rsrc_update.offset;
6358         up.data = req->rsrc_update.arg;
6359         up.nr = 0;
6360         up.tags = 0;
6361         up.resv = 0;
6362
6363         io_ring_submit_lock(ctx, needs_lock);
6364         ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE,
6365                                         &up, req->rsrc_update.nr_args);
6366         io_ring_submit_unlock(ctx, needs_lock);
6367
6368         if (ret < 0)
6369                 req_set_fail(req);
6370         __io_req_complete(req, issue_flags, ret, 0);
6371         return 0;
6372 }
6373
6374 static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
6375 {
6376         switch (req->opcode) {
6377         case IORING_OP_NOP:
6378                 return 0;
6379         case IORING_OP_READV:
6380         case IORING_OP_READ_FIXED:
6381         case IORING_OP_READ:
6382                 return io_read_prep(req, sqe);
6383         case IORING_OP_WRITEV:
6384         case IORING_OP_WRITE_FIXED:
6385         case IORING_OP_WRITE:
6386                 return io_write_prep(req, sqe);
6387         case IORING_OP_POLL_ADD:
6388                 return io_poll_add_prep(req, sqe);
6389         case IORING_OP_POLL_REMOVE:
6390                 return io_poll_update_prep(req, sqe);
6391         case IORING_OP_FSYNC:
6392                 return io_fsync_prep(req, sqe);
6393         case IORING_OP_SYNC_FILE_RANGE:
6394                 return io_sfr_prep(req, sqe);
6395         case IORING_OP_SENDMSG:
6396         case IORING_OP_SEND:
6397                 return io_sendmsg_prep(req, sqe);
6398         case IORING_OP_RECVMSG:
6399         case IORING_OP_RECV:
6400                 return io_recvmsg_prep(req, sqe);
6401         case IORING_OP_CONNECT:
6402                 return io_connect_prep(req, sqe);
6403         case IORING_OP_TIMEOUT:
6404                 return io_timeout_prep(req, sqe, false);
6405         case IORING_OP_TIMEOUT_REMOVE:
6406                 return io_timeout_remove_prep(req, sqe);
6407         case IORING_OP_ASYNC_CANCEL:
6408                 return io_async_cancel_prep(req, sqe);
6409         case IORING_OP_LINK_TIMEOUT:
6410                 return io_timeout_prep(req, sqe, true);
6411         case IORING_OP_ACCEPT:
6412                 return io_accept_prep(req, sqe);
6413         case IORING_OP_FALLOCATE:
6414                 return io_fallocate_prep(req, sqe);
6415         case IORING_OP_OPENAT:
6416                 return io_openat_prep(req, sqe);
6417         case IORING_OP_CLOSE:
6418                 return io_close_prep(req, sqe);
6419         case IORING_OP_FILES_UPDATE:
6420                 return io_rsrc_update_prep(req, sqe);
6421         case IORING_OP_STATX:
6422                 return io_statx_prep(req, sqe);
6423         case IORING_OP_FADVISE:
6424                 return io_fadvise_prep(req, sqe);
6425         case IORING_OP_MADVISE:
6426                 return io_madvise_prep(req, sqe);
6427         case IORING_OP_OPENAT2:
6428                 return io_openat2_prep(req, sqe);
6429         case IORING_OP_EPOLL_CTL:
6430                 return io_epoll_ctl_prep(req, sqe);
6431         case IORING_OP_SPLICE:
6432                 return io_splice_prep(req, sqe);
6433         case IORING_OP_PROVIDE_BUFFERS:
6434                 return io_provide_buffers_prep(req, sqe);
6435         case IORING_OP_REMOVE_BUFFERS:
6436                 return io_remove_buffers_prep(req, sqe);
6437         case IORING_OP_TEE:
6438                 return io_tee_prep(req, sqe);
6439         case IORING_OP_SHUTDOWN:
6440                 return io_shutdown_prep(req, sqe);
6441         case IORING_OP_RENAMEAT:
6442                 return io_renameat_prep(req, sqe);
6443         case IORING_OP_UNLINKAT:
6444                 return io_unlinkat_prep(req, sqe);
6445         case IORING_OP_MKDIRAT:
6446                 return io_mkdirat_prep(req, sqe);
6447         case IORING_OP_SYMLINKAT:
6448                 return io_symlinkat_prep(req, sqe);
6449         case IORING_OP_LINKAT:
6450                 return io_linkat_prep(req, sqe);
6451         }
6452
6453         printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
6454                         req->opcode);
6455         return -EINVAL;
6456 }
6457
6458 static int io_req_prep_async(struct io_kiocb *req)
6459 {
6460         if (!io_op_defs[req->opcode].needs_async_setup)
6461                 return 0;
6462         if (WARN_ON_ONCE(req_has_async_data(req)))
6463                 return -EFAULT;
6464         if (io_alloc_async_data(req))
6465                 return -EAGAIN;
6466
6467         switch (req->opcode) {
6468         case IORING_OP_READV:
6469                 return io_rw_prep_async(req, READ);
6470         case IORING_OP_WRITEV:
6471                 return io_rw_prep_async(req, WRITE);
6472         case IORING_OP_SENDMSG:
6473                 return io_sendmsg_prep_async(req);
6474         case IORING_OP_RECVMSG:
6475                 return io_recvmsg_prep_async(req);
6476         case IORING_OP_CONNECT:
6477                 return io_connect_prep_async(req);
6478         }
6479         printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n",
6480                     req->opcode);
6481         return -EFAULT;
6482 }
6483
6484 static u32 io_get_sequence(struct io_kiocb *req)
6485 {
6486         u32 seq = req->ctx->cached_sq_head;
6487
6488         /* need original cached_sq_head, but it was increased for each req */
6489         io_for_each_link(req, req)
6490                 seq--;
6491         return seq;
6492 }
6493
6494 static __cold void io_drain_req(struct io_kiocb *req)
6495 {
6496         struct io_ring_ctx *ctx = req->ctx;
6497         struct io_defer_entry *de;
6498         int ret;
6499         u32 seq = io_get_sequence(req);
6500
6501         /* Still need defer if there is pending req in defer list. */
6502         if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) {
6503 queue:
6504                 ctx->drain_active = false;
6505                 io_req_task_queue(req);
6506                 return;
6507         }
6508
6509         ret = io_req_prep_async(req);
6510         if (ret) {
6511 fail:
6512                 io_req_complete_failed(req, ret);
6513                 return;
6514         }
6515         io_prep_async_link(req);
6516         de = kmalloc(sizeof(*de), GFP_KERNEL);
6517         if (!de) {
6518                 ret = -ENOMEM;
6519                 goto fail;
6520         }
6521
6522         spin_lock(&ctx->completion_lock);
6523         if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
6524                 spin_unlock(&ctx->completion_lock);
6525                 kfree(de);
6526                 goto queue;
6527         }
6528
6529         trace_io_uring_defer(ctx, req, req->user_data);
6530         de->req = req;
6531         de->seq = seq;
6532         list_add_tail(&de->list, &ctx->defer_list);
6533         spin_unlock(&ctx->completion_lock);
6534 }
6535
6536 static void io_clean_op(struct io_kiocb *req)
6537 {
6538         if (req->flags & REQ_F_BUFFER_SELECTED) {
6539                 kfree(req->kbuf);
6540                 req->kbuf = NULL;
6541         }
6542
6543         if (req->flags & REQ_F_NEED_CLEANUP) {
6544                 switch (req->opcode) {
6545                 case IORING_OP_READV:
6546                 case IORING_OP_READ_FIXED:
6547                 case IORING_OP_READ:
6548                 case IORING_OP_WRITEV:
6549                 case IORING_OP_WRITE_FIXED:
6550                 case IORING_OP_WRITE: {
6551                         struct io_async_rw *io = req->async_data;
6552
6553                         kfree(io->free_iovec);
6554                         break;
6555                         }
6556                 case IORING_OP_RECVMSG:
6557                 case IORING_OP_SENDMSG: {
6558                         struct io_async_msghdr *io = req->async_data;
6559
6560                         kfree(io->free_iov);
6561                         break;
6562                         }
6563                 case IORING_OP_SPLICE:
6564                 case IORING_OP_TEE:
6565                         if (!(req->splice.flags & SPLICE_F_FD_IN_FIXED))
6566                                 io_put_file(req->splice.file_in);
6567                         break;
6568                 case IORING_OP_OPENAT:
6569                 case IORING_OP_OPENAT2:
6570                         if (req->open.filename)
6571                                 putname(req->open.filename);
6572                         break;
6573                 case IORING_OP_RENAMEAT:
6574                         putname(req->rename.oldpath);
6575                         putname(req->rename.newpath);
6576                         break;
6577                 case IORING_OP_UNLINKAT:
6578                         putname(req->unlink.filename);
6579                         break;
6580                 case IORING_OP_MKDIRAT:
6581                         putname(req->mkdir.filename);
6582                         break;
6583                 case IORING_OP_SYMLINKAT:
6584                         putname(req->symlink.oldpath);
6585                         putname(req->symlink.newpath);
6586                         break;
6587                 case IORING_OP_LINKAT:
6588                         putname(req->hardlink.oldpath);
6589                         putname(req->hardlink.newpath);
6590                         break;
6591                 }
6592         }
6593         if ((req->flags & REQ_F_POLLED) && req->apoll) {
6594                 kfree(req->apoll->double_poll);
6595                 kfree(req->apoll);
6596                 req->apoll = NULL;
6597         }
6598         if (req->flags & REQ_F_INFLIGHT) {
6599                 struct io_uring_task *tctx = req->task->io_uring;
6600
6601                 atomic_dec(&tctx->inflight_tracked);
6602         }
6603         if (req->flags & REQ_F_CREDS)
6604                 put_cred(req->creds);
6605         if (req->flags & REQ_F_ASYNC_DATA) {
6606                 kfree(req->async_data);
6607                 req->async_data = NULL;
6608         }
6609         req->flags &= ~IO_REQ_CLEAN_FLAGS;
6610 }
6611
6612 static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)
6613 {
6614         const struct cred *creds = NULL;
6615         int ret;
6616
6617         if (unlikely((req->flags & REQ_F_CREDS) && req->creds != current_cred()))
6618                 creds = override_creds(req->creds);
6619
6620         if (!io_op_defs[req->opcode].audit_skip)
6621                 audit_uring_entry(req->opcode);
6622
6623         switch (req->opcode) {
6624         case IORING_OP_NOP:
6625                 ret = io_nop(req, issue_flags);
6626                 break;
6627         case IORING_OP_READV:
6628         case IORING_OP_READ_FIXED:
6629         case IORING_OP_READ:
6630                 ret = io_read(req, issue_flags);
6631                 break;
6632         case IORING_OP_WRITEV:
6633         case IORING_OP_WRITE_FIXED:
6634         case IORING_OP_WRITE:
6635                 ret = io_write(req, issue_flags);
6636                 break;
6637         case IORING_OP_FSYNC:
6638                 ret = io_fsync(req, issue_flags);
6639                 break;
6640         case IORING_OP_POLL_ADD:
6641                 ret = io_poll_add(req, issue_flags);
6642                 break;
6643         case IORING_OP_POLL_REMOVE:
6644                 ret = io_poll_update(req, issue_flags);
6645                 break;
6646         case IORING_OP_SYNC_FILE_RANGE:
6647                 ret = io_sync_file_range(req, issue_flags);
6648                 break;
6649         case IORING_OP_SENDMSG:
6650                 ret = io_sendmsg(req, issue_flags);
6651                 break;
6652         case IORING_OP_SEND:
6653                 ret = io_send(req, issue_flags);
6654                 break;
6655         case IORING_OP_RECVMSG:
6656                 ret = io_recvmsg(req, issue_flags);
6657                 break;
6658         case IORING_OP_RECV:
6659                 ret = io_recv(req, issue_flags);
6660                 break;
6661         case IORING_OP_TIMEOUT:
6662                 ret = io_timeout(req, issue_flags);
6663                 break;
6664         case IORING_OP_TIMEOUT_REMOVE:
6665                 ret = io_timeout_remove(req, issue_flags);
6666                 break;
6667         case IORING_OP_ACCEPT:
6668                 ret = io_accept(req, issue_flags);
6669                 break;
6670         case IORING_OP_CONNECT:
6671                 ret = io_connect(req, issue_flags);
6672                 break;
6673         case IORING_OP_ASYNC_CANCEL:
6674                 ret = io_async_cancel(req, issue_flags);
6675                 break;
6676         case IORING_OP_FALLOCATE:
6677                 ret = io_fallocate(req, issue_flags);
6678                 break;
6679         case IORING_OP_OPENAT:
6680                 ret = io_openat(req, issue_flags);
6681                 break;
6682         case IORING_OP_CLOSE:
6683                 ret = io_close(req, issue_flags);
6684                 break;
6685         case IORING_OP_FILES_UPDATE:
6686                 ret = io_files_update(req, issue_flags);
6687                 break;
6688         case IORING_OP_STATX:
6689                 ret = io_statx(req, issue_flags);
6690                 break;
6691         case IORING_OP_FADVISE:
6692                 ret = io_fadvise(req, issue_flags);
6693                 break;
6694         case IORING_OP_MADVISE:
6695                 ret = io_madvise(req, issue_flags);
6696                 break;
6697         case IORING_OP_OPENAT2:
6698                 ret = io_openat2(req, issue_flags);
6699                 break;
6700         case IORING_OP_EPOLL_CTL:
6701                 ret = io_epoll_ctl(req, issue_flags);
6702                 break;
6703         case IORING_OP_SPLICE:
6704                 ret = io_splice(req, issue_flags);
6705                 break;
6706         case IORING_OP_PROVIDE_BUFFERS:
6707                 ret = io_provide_buffers(req, issue_flags);
6708                 break;
6709         case IORING_OP_REMOVE_BUFFERS:
6710                 ret = io_remove_buffers(req, issue_flags);
6711                 break;
6712         case IORING_OP_TEE:
6713                 ret = io_tee(req, issue_flags);
6714                 break;
6715         case IORING_OP_SHUTDOWN:
6716                 ret = io_shutdown(req, issue_flags);
6717                 break;
6718         case IORING_OP_RENAMEAT:
6719                 ret = io_renameat(req, issue_flags);
6720                 break;
6721         case IORING_OP_UNLINKAT:
6722                 ret = io_unlinkat(req, issue_flags);
6723                 break;
6724         case IORING_OP_MKDIRAT:
6725                 ret = io_mkdirat(req, issue_flags);
6726                 break;
6727         case IORING_OP_SYMLINKAT:
6728                 ret = io_symlinkat(req, issue_flags);
6729                 break;
6730         case IORING_OP_LINKAT:
6731                 ret = io_linkat(req, issue_flags);
6732                 break;
6733         default:
6734                 ret = -EINVAL;
6735                 break;
6736         }
6737
6738         if (!io_op_defs[req->opcode].audit_skip)
6739                 audit_uring_exit(!ret, ret);
6740
6741         if (creds)
6742                 revert_creds(creds);
6743         if (ret)
6744                 return ret;
6745         /* If the op doesn't have a file, we're not polling for it */
6746         if ((req->ctx->flags & IORING_SETUP_IOPOLL) && req->file)
6747                 io_iopoll_req_issued(req, issue_flags);
6748
6749         return 0;
6750 }
6751
6752 static struct io_wq_work *io_wq_free_work(struct io_wq_work *work)
6753 {
6754         struct io_kiocb *req = container_of(work, struct io_kiocb, work);
6755
6756         req = io_put_req_find_next(req);
6757         return req ? &req->work : NULL;
6758 }
6759
6760 static void io_wq_submit_work(struct io_wq_work *work)
6761 {
6762         struct io_kiocb *req = container_of(work, struct io_kiocb, work);
6763         unsigned int issue_flags = IO_URING_F_UNLOCKED;
6764         bool needs_poll = false;
6765         struct io_kiocb *timeout;
6766         int ret = 0;
6767
6768         /* one will be dropped by ->io_free_work() after returning to io-wq */
6769         if (!(req->flags & REQ_F_REFCOUNT))
6770                 __io_req_set_refcount(req, 2);
6771         else
6772                 req_ref_get(req);
6773
6774         timeout = io_prep_linked_timeout(req);
6775         if (timeout)
6776                 io_queue_linked_timeout(timeout);
6777
6778         /* either cancelled or io-wq is dying, so don't touch tctx->iowq */
6779         if (work->flags & IO_WQ_WORK_CANCEL) {
6780                 io_req_task_queue_fail(req, -ECANCELED);
6781                 return;
6782         }
6783
6784         if (req->flags & REQ_F_FORCE_ASYNC) {
6785                 const struct io_op_def *def = &io_op_defs[req->opcode];
6786                 bool opcode_poll = def->pollin || def->pollout;
6787
6788                 if (opcode_poll && file_can_poll(req->file)) {
6789                         needs_poll = true;
6790                         issue_flags |= IO_URING_F_NONBLOCK;
6791                 }
6792         }
6793
6794         do {
6795                 ret = io_issue_sqe(req, issue_flags);
6796                 if (ret != -EAGAIN)
6797                         break;
6798                 /*
6799                  * We can get EAGAIN for iopolled IO even though we're
6800                  * forcing a sync submission from here, since we can't
6801                  * wait for request slots on the block side.
6802                  */
6803                 if (!needs_poll) {
6804                         cond_resched();
6805                         continue;
6806                 }
6807
6808                 if (io_arm_poll_handler(req) == IO_APOLL_OK)
6809                         return;
6810                 /* aborted or ready, in either case retry blocking */
6811                 needs_poll = false;
6812                 issue_flags &= ~IO_URING_F_NONBLOCK;
6813         } while (1);
6814
6815         /* avoid locking problems by failing it from a clean context */
6816         if (ret)
6817                 io_req_task_queue_fail(req, ret);
6818 }
6819
6820 static inline struct io_fixed_file *io_fixed_file_slot(struct io_file_table *table,
6821                                                        unsigned i)
6822 {
6823         return &table->files[i];
6824 }
6825
6826 static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6827                                               int index)
6828 {
6829         struct io_fixed_file *slot = io_fixed_file_slot(&ctx->file_table, index);
6830
6831         return (struct file *) (slot->file_ptr & FFS_MASK);
6832 }
6833
6834 static void io_fixed_file_set(struct io_fixed_file *file_slot, struct file *file)
6835 {
6836         unsigned long file_ptr = (unsigned long) file;
6837
6838         file_ptr |= io_file_get_flags(file);
6839         file_slot->file_ptr = file_ptr;
6840 }
6841
6842 static inline struct file *io_file_get_fixed(struct io_ring_ctx *ctx,
6843                                              struct io_kiocb *req, int fd)
6844 {
6845         struct file *file;
6846         unsigned long file_ptr;
6847
6848         if (unlikely((unsigned int)fd >= ctx->nr_user_files))
6849                 return NULL;
6850         fd = array_index_nospec(fd, ctx->nr_user_files);
6851         file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr;
6852         file = (struct file *) (file_ptr & FFS_MASK);
6853         file_ptr &= ~FFS_MASK;
6854         /* mask in overlapping REQ_F and FFS bits */
6855         req->flags |= (file_ptr << REQ_F_SUPPORT_NOWAIT_BIT);
6856         io_req_set_rsrc_node(req, ctx);
6857         return file;
6858 }
6859
6860 static struct file *io_file_get_normal(struct io_ring_ctx *ctx,
6861                                        struct io_kiocb *req, int fd)
6862 {
6863         struct file *file = fget(fd);
6864
6865         trace_io_uring_file_get(ctx, fd);
6866
6867         /* we don't allow fixed io_uring files */
6868         if (file && unlikely(file->f_op == &io_uring_fops))
6869                 io_req_track_inflight(req);
6870         return file;
6871 }
6872
6873 static inline struct file *io_file_get(struct io_ring_ctx *ctx,
6874                                        struct io_kiocb *req, int fd, bool fixed)
6875 {
6876         if (fixed)
6877                 return io_file_get_fixed(ctx, req, fd);
6878         else
6879                 return io_file_get_normal(ctx, req, fd);
6880 }
6881
6882 static void io_req_task_link_timeout(struct io_kiocb *req, bool *locked)
6883 {
6884         struct io_kiocb *prev = req->timeout.prev;
6885         int ret;
6886
6887         if (prev) {
6888                 ret = io_try_cancel_userdata(req, prev->user_data);
6889                 io_req_complete_post(req, ret ?: -ETIME, 0);
6890                 io_put_req(prev);
6891         } else {
6892                 io_req_complete_post(req, -ETIME, 0);
6893         }
6894 }
6895
6896 static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6897 {
6898         struct io_timeout_data *data = container_of(timer,
6899                                                 struct io_timeout_data, timer);
6900         struct io_kiocb *prev, *req = data->req;
6901         struct io_ring_ctx *ctx = req->ctx;
6902         unsigned long flags;
6903
6904         spin_lock_irqsave(&ctx->timeout_lock, flags);
6905         prev = req->timeout.head;
6906         req->timeout.head = NULL;
6907
6908         /*
6909          * We don't expect the list to be empty, that will only happen if we
6910          * race with the completion of the linked work.
6911          */
6912         if (prev) {
6913                 io_remove_next_linked(prev);
6914                 if (!req_ref_inc_not_zero(prev))
6915                         prev = NULL;
6916         }
6917         list_del(&req->timeout.list);
6918         req->timeout.prev = prev;
6919         spin_unlock_irqrestore(&ctx->timeout_lock, flags);
6920
6921         req->io_task_work.func = io_req_task_link_timeout;
6922         io_req_task_work_add(req);
6923         return HRTIMER_NORESTART;
6924 }
6925
6926 static void io_queue_linked_timeout(struct io_kiocb *req)
6927 {
6928         struct io_ring_ctx *ctx = req->ctx;
6929
6930         spin_lock_irq(&ctx->timeout_lock);
6931         /*
6932          * If the back reference is NULL, then our linked request finished
6933          * before we got a chance to setup the timer
6934          */
6935         if (req->timeout.head) {
6936                 struct io_timeout_data *data = req->async_data;
6937
6938                 data->timer.function = io_link_timeout_fn;
6939                 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6940                                 data->mode);
6941                 list_add_tail(&req->timeout.list, &ctx->ltimeout_list);
6942         }
6943         spin_unlock_irq(&ctx->timeout_lock);
6944         /* drop submission reference */
6945         io_put_req(req);
6946 }
6947
6948 static void io_queue_sqe_arm_apoll(struct io_kiocb *req)
6949         __must_hold(&req->ctx->uring_lock)
6950 {
6951         struct io_kiocb *linked_timeout = io_prep_linked_timeout(req);
6952
6953         switch (io_arm_poll_handler(req)) {
6954         case IO_APOLL_READY:
6955                 io_req_task_queue(req);
6956                 break;
6957         case IO_APOLL_ABORTED:
6958                 /*
6959                  * Queued up for async execution, worker will release
6960                  * submit reference when the iocb is actually submitted.
6961                  */
6962                 io_queue_async_work(req, NULL);
6963                 break;
6964         }
6965
6966         if (linked_timeout)
6967                 io_queue_linked_timeout(linked_timeout);
6968 }
6969
6970 static inline void __io_queue_sqe(struct io_kiocb *req)
6971         __must_hold(&req->ctx->uring_lock)
6972 {
6973         struct io_kiocb *linked_timeout;
6974         int ret;
6975
6976         ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER);
6977
6978         if (req->flags & REQ_F_COMPLETE_INLINE) {
6979                 io_req_add_compl_list(req);
6980                 return;
6981         }
6982         /*
6983          * We async punt it if the file wasn't marked NOWAIT, or if the file
6984          * doesn't support non-blocking read/write attempts
6985          */
6986         if (likely(!ret)) {
6987                 linked_timeout = io_prep_linked_timeout(req);
6988                 if (linked_timeout)
6989                         io_queue_linked_timeout(linked_timeout);
6990         } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
6991                 io_queue_sqe_arm_apoll(req);
6992         } else {
6993                 io_req_complete_failed(req, ret);
6994         }
6995 }
6996
6997 static void io_queue_sqe_fallback(struct io_kiocb *req)
6998         __must_hold(&req->ctx->uring_lock)
6999 {
7000         if (req->flags & REQ_F_FAIL) {
7001                 io_req_complete_fail_submit(req);
7002         } else if (unlikely(req->ctx->drain_active)) {
7003                 io_drain_req(req);
7004         } else {
7005                 int ret = io_req_prep_async(req);
7006
7007                 if (unlikely(ret))
7008                         io_req_complete_failed(req, ret);
7009                 else
7010                         io_queue_async_work(req, NULL);
7011         }
7012 }
7013
7014 static inline void io_queue_sqe(struct io_kiocb *req)
7015         __must_hold(&req->ctx->uring_lock)
7016 {
7017         if (likely(!(req->flags & (REQ_F_FORCE_ASYNC | REQ_F_FAIL))))
7018                 __io_queue_sqe(req);
7019         else
7020                 io_queue_sqe_fallback(req);
7021 }
7022
7023 /*
7024  * Check SQE restrictions (opcode and flags).
7025  *
7026  * Returns 'true' if SQE is allowed, 'false' otherwise.
7027  */
7028 static inline bool io_check_restriction(struct io_ring_ctx *ctx,
7029                                         struct io_kiocb *req,
7030                                         unsigned int sqe_flags)
7031 {
7032         if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
7033                 return false;
7034
7035         if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
7036             ctx->restrictions.sqe_flags_required)
7037                 return false;
7038
7039         if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
7040                           ctx->restrictions.sqe_flags_required))
7041                 return false;
7042
7043         return true;
7044 }
7045
7046 static void io_init_req_drain(struct io_kiocb *req)
7047 {
7048         struct io_ring_ctx *ctx = req->ctx;
7049         struct io_kiocb *head = ctx->submit_state.link.head;
7050
7051         ctx->drain_active = true;
7052         if (head) {
7053                 /*
7054                  * If we need to drain a request in the middle of a link, drain
7055                  * the head request and the next request/link after the current
7056                  * link. Considering sequential execution of links,
7057                  * IOSQE_IO_DRAIN will be maintained for every request of our
7058                  * link.
7059                  */
7060                 head->flags |= IOSQE_IO_DRAIN | REQ_F_FORCE_ASYNC;
7061                 ctx->drain_next = true;
7062         }
7063 }
7064
7065 static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
7066                        const struct io_uring_sqe *sqe)
7067         __must_hold(&ctx->uring_lock)
7068 {
7069         unsigned int sqe_flags;
7070         int personality;
7071         u8 opcode;
7072
7073         /* req is partially pre-initialised, see io_preinit_req() */
7074         req->opcode = opcode = READ_ONCE(sqe->opcode);
7075         /* same numerical values with corresponding REQ_F_*, safe to copy */
7076         req->flags = sqe_flags = READ_ONCE(sqe->flags);
7077         req->user_data = READ_ONCE(sqe->user_data);
7078         req->file = NULL;
7079         req->fixed_rsrc_refs = NULL;
7080         req->task = current;
7081
7082         if (unlikely(opcode >= IORING_OP_LAST)) {
7083                 req->opcode = 0;
7084                 return -EINVAL;
7085         }
7086         if (unlikely(sqe_flags & ~SQE_COMMON_FLAGS)) {
7087                 /* enforce forwards compatibility on users */
7088                 if (sqe_flags & ~SQE_VALID_FLAGS)
7089                         return -EINVAL;
7090                 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
7091                     !io_op_defs[opcode].buffer_select)
7092                         return -EOPNOTSUPP;
7093                 if (sqe_flags & IOSQE_IO_DRAIN)
7094                         io_init_req_drain(req);
7095         }
7096         if (unlikely(ctx->restricted || ctx->drain_active || ctx->drain_next)) {
7097                 if (ctx->restricted && !io_check_restriction(ctx, req, sqe_flags))
7098                         return -EACCES;
7099                 /* knock it to the slow queue path, will be drained there */
7100                 if (ctx->drain_active)
7101                         req->flags |= REQ_F_FORCE_ASYNC;
7102                 /* if there is no link, we're at "next" request and need to drain */
7103                 if (unlikely(ctx->drain_next) && !ctx->submit_state.link.head) {
7104                         ctx->drain_next = false;
7105                         ctx->drain_active = true;
7106                         req->flags |= IOSQE_IO_DRAIN | REQ_F_FORCE_ASYNC;
7107                 }
7108         }
7109
7110         if (io_op_defs[opcode].needs_file) {
7111                 struct io_submit_state *state = &ctx->submit_state;
7112
7113                 /*
7114                  * Plug now if we have more than 2 IO left after this, and the
7115                  * target is potentially a read/write to block based storage.
7116                  */
7117                 if (state->need_plug && io_op_defs[opcode].plug) {
7118                         state->plug_started = true;
7119                         state->need_plug = false;
7120                         blk_start_plug_nr_ios(&state->plug, state->submit_nr);
7121                 }
7122
7123                 req->file = io_file_get(ctx, req, READ_ONCE(sqe->fd),
7124                                         (sqe_flags & IOSQE_FIXED_FILE));
7125                 if (unlikely(!req->file))
7126                         return -EBADF;
7127         }
7128
7129         personality = READ_ONCE(sqe->personality);
7130         if (personality) {
7131                 int ret;
7132
7133                 req->creds = xa_load(&ctx->personalities, personality);
7134                 if (!req->creds)
7135                         return -EINVAL;
7136                 get_cred(req->creds);
7137                 ret = security_uring_override_creds(req->creds);
7138                 if (ret) {
7139                         put_cred(req->creds);
7140                         return ret;
7141                 }
7142                 req->flags |= REQ_F_CREDS;
7143         }
7144
7145         return io_req_prep(req, sqe);
7146 }
7147
7148 static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
7149                          const struct io_uring_sqe *sqe)
7150         __must_hold(&ctx->uring_lock)
7151 {
7152         struct io_submit_link *link = &ctx->submit_state.link;
7153         int ret;
7154
7155         ret = io_init_req(ctx, req, sqe);
7156         if (unlikely(ret)) {
7157                 trace_io_uring_req_failed(sqe, ret);
7158
7159                 /* fail even hard links since we don't submit */
7160                 if (link->head) {
7161                         /*
7162                          * we can judge a link req is failed or cancelled by if
7163                          * REQ_F_FAIL is set, but the head is an exception since
7164                          * it may be set REQ_F_FAIL because of other req's failure
7165                          * so let's leverage req->result to distinguish if a head
7166                          * is set REQ_F_FAIL because of its failure or other req's
7167                          * failure so that we can set the correct ret code for it.
7168                          * init result here to avoid affecting the normal path.
7169                          */
7170                         if (!(link->head->flags & REQ_F_FAIL))
7171                                 req_fail_link_node(link->head, -ECANCELED);
7172                 } else if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
7173                         /*
7174                          * the current req is a normal req, we should return
7175                          * error and thus break the submittion loop.
7176                          */
7177                         io_req_complete_failed(req, ret);
7178                         return ret;
7179                 }
7180                 req_fail_link_node(req, ret);
7181         }
7182
7183         /* don't need @sqe from now on */
7184         trace_io_uring_submit_sqe(ctx, req, req->opcode, req->user_data,
7185                                   req->flags, true,
7186                                   ctx->flags & IORING_SETUP_SQPOLL);
7187
7188         /*
7189          * If we already have a head request, queue this one for async
7190          * submittal once the head completes. If we don't have a head but
7191          * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
7192          * submitted sync once the chain is complete. If none of those
7193          * conditions are true (normal request), then just queue it.
7194          */
7195         if (link->head) {
7196                 struct io_kiocb *head = link->head;
7197
7198                 if (!(req->flags & REQ_F_FAIL)) {
7199                         ret = io_req_prep_async(req);
7200                         if (unlikely(ret)) {
7201                                 req_fail_link_node(req, ret);
7202                                 if (!(head->flags & REQ_F_FAIL))
7203                                         req_fail_link_node(head, -ECANCELED);
7204                         }
7205                 }
7206                 trace_io_uring_link(ctx, req, head);
7207                 link->last->link = req;
7208                 link->last = req;
7209
7210                 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK))
7211                         return 0;
7212                 /* last request of a link, enqueue the link */
7213                 link->head = NULL;
7214                 req = head;
7215         } else if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
7216                 link->head = req;
7217                 link->last = req;
7218                 return 0;
7219         }
7220
7221         io_queue_sqe(req);
7222         return 0;
7223 }
7224
7225 /*
7226  * Batched submission is done, ensure local IO is flushed out.
7227  */
7228 static void io_submit_state_end(struct io_ring_ctx *ctx)
7229 {
7230         struct io_submit_state *state = &ctx->submit_state;
7231
7232         if (state->link.head)
7233                 io_queue_sqe(state->link.head);
7234         /* flush only after queuing links as they can generate completions */
7235         io_submit_flush_completions(ctx);
7236         if (state->plug_started)
7237                 blk_finish_plug(&state->plug);
7238 }
7239
7240 /*
7241  * Start submission side cache.
7242  */
7243 static void io_submit_state_start(struct io_submit_state *state,
7244                                   unsigned int max_ios)
7245 {
7246         state->plug_started = false;
7247         state->need_plug = max_ios > 2;
7248         state->submit_nr = max_ios;
7249         /* set only head, no need to init link_last in advance */
7250         state->link.head = NULL;
7251 }
7252
7253 static void io_commit_sqring(struct io_ring_ctx *ctx)
7254 {
7255         struct io_rings *rings = ctx->rings;
7256
7257         /*
7258          * Ensure any loads from the SQEs are done at this point,
7259          * since once we write the new head, the application could
7260          * write new data to them.
7261          */
7262         smp_store_release(&rings->sq.head, ctx->cached_sq_head);
7263 }
7264
7265 /*
7266  * Fetch an sqe, if one is available. Note this returns a pointer to memory
7267  * that is mapped by userspace. This means that care needs to be taken to
7268  * ensure that reads are stable, as we cannot rely on userspace always
7269  * being a good citizen. If members of the sqe are validated and then later
7270  * used, it's important that those reads are done through READ_ONCE() to
7271  * prevent a re-load down the line.
7272  */
7273 static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
7274 {
7275         unsigned head, mask = ctx->sq_entries - 1;
7276         unsigned sq_idx = ctx->cached_sq_head++ & mask;
7277
7278         /*
7279          * The cached sq head (or cq tail) serves two purposes:
7280          *
7281          * 1) allows us to batch the cost of updating the user visible
7282          *    head updates.
7283          * 2) allows the kernel side to track the head on its own, even
7284          *    though the application is the one updating it.
7285          */
7286         head = READ_ONCE(ctx->sq_array[sq_idx]);
7287         if (likely(head < ctx->sq_entries))
7288                 return &ctx->sq_sqes[head];
7289
7290         /* drop invalid entries */
7291         ctx->cq_extra--;
7292         WRITE_ONCE(ctx->rings->sq_dropped,
7293                    READ_ONCE(ctx->rings->sq_dropped) + 1);
7294         return NULL;
7295 }
7296
7297 static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
7298         __must_hold(&ctx->uring_lock)
7299 {
7300         unsigned int entries = io_sqring_entries(ctx);
7301         int submitted = 0;
7302
7303         if (unlikely(!entries))
7304                 return 0;
7305         /* make sure SQ entry isn't read before tail */
7306         nr = min3(nr, ctx->sq_entries, entries);
7307         io_get_task_refs(nr);
7308
7309         io_submit_state_start(&ctx->submit_state, nr);
7310         do {
7311                 const struct io_uring_sqe *sqe;
7312                 struct io_kiocb *req;
7313
7314                 if (unlikely(!io_alloc_req_refill(ctx))) {
7315                         if (!submitted)
7316                                 submitted = -EAGAIN;
7317                         break;
7318                 }
7319                 req = io_alloc_req(ctx);
7320                 sqe = io_get_sqe(ctx);
7321                 if (unlikely(!sqe)) {
7322                         wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list);
7323                         break;
7324                 }
7325                 /* will complete beyond this point, count as submitted */
7326                 submitted++;
7327                 if (io_submit_sqe(ctx, req, sqe))
7328                         break;
7329         } while (submitted < nr);
7330
7331         if (unlikely(submitted != nr)) {
7332                 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
7333                 int unused = nr - ref_used;
7334
7335                 current->io_uring->cached_refs += unused;
7336         }
7337
7338         io_submit_state_end(ctx);
7339          /* Commit SQ ring head once we've consumed and submitted all SQEs */
7340         io_commit_sqring(ctx);
7341
7342         return submitted;
7343 }
7344
7345 static inline bool io_sqd_events_pending(struct io_sq_data *sqd)
7346 {
7347         return READ_ONCE(sqd->state);
7348 }
7349
7350 static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
7351 {
7352         /* Tell userspace we may need a wakeup call */
7353         spin_lock(&ctx->completion_lock);
7354         WRITE_ONCE(ctx->rings->sq_flags,
7355                    ctx->rings->sq_flags | IORING_SQ_NEED_WAKEUP);
7356         spin_unlock(&ctx->completion_lock);
7357 }
7358
7359 static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
7360 {
7361         spin_lock(&ctx->completion_lock);
7362         WRITE_ONCE(ctx->rings->sq_flags,
7363                    ctx->rings->sq_flags & ~IORING_SQ_NEED_WAKEUP);
7364         spin_unlock(&ctx->completion_lock);
7365 }
7366
7367 static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries)
7368 {
7369         unsigned int to_submit;
7370         int ret = 0;
7371
7372         to_submit = io_sqring_entries(ctx);
7373         /* if we're handling multiple rings, cap submit size for fairness */
7374         if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE)
7375                 to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE;
7376
7377         if (!wq_list_empty(&ctx->iopoll_list) || to_submit) {
7378                 const struct cred *creds = NULL;
7379
7380                 if (ctx->sq_creds != current_cred())
7381                         creds = override_creds(ctx->sq_creds);
7382
7383                 mutex_lock(&ctx->uring_lock);
7384                 if (!wq_list_empty(&ctx->iopoll_list))
7385                         io_do_iopoll(ctx, true);
7386
7387                 /*
7388                  * Don't submit if refs are dying, good for io_uring_register(),
7389                  * but also it is relied upon by io_ring_exit_work()
7390                  */
7391                 if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) &&
7392                     !(ctx->flags & IORING_SETUP_R_DISABLED))
7393                         ret = io_submit_sqes(ctx, to_submit);
7394                 mutex_unlock(&ctx->uring_lock);
7395
7396                 if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait))
7397                         wake_up(&ctx->sqo_sq_wait);
7398                 if (creds)
7399                         revert_creds(creds);
7400         }
7401
7402         return ret;
7403 }
7404
7405 static __cold void io_sqd_update_thread_idle(struct io_sq_data *sqd)
7406 {
7407         struct io_ring_ctx *ctx;
7408         unsigned sq_thread_idle = 0;
7409
7410         list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7411                 sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle);
7412         sqd->sq_thread_idle = sq_thread_idle;
7413 }
7414
7415 static bool io_sqd_handle_event(struct io_sq_data *sqd)
7416 {
7417         bool did_sig = false;
7418         struct ksignal ksig;
7419
7420         if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) ||
7421             signal_pending(current)) {
7422                 mutex_unlock(&sqd->lock);
7423                 if (signal_pending(current))
7424                         did_sig = get_signal(&ksig);
7425                 cond_resched();
7426                 mutex_lock(&sqd->lock);
7427         }
7428         return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
7429 }
7430
7431 static int io_sq_thread(void *data)
7432 {
7433         struct io_sq_data *sqd = data;
7434         struct io_ring_ctx *ctx;
7435         unsigned long timeout = 0;
7436         char buf[TASK_COMM_LEN];
7437         DEFINE_WAIT(wait);
7438
7439         snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid);
7440         set_task_comm(current, buf);
7441
7442         if (sqd->sq_cpu != -1)
7443                 set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu));
7444         else
7445                 set_cpus_allowed_ptr(current, cpu_online_mask);
7446         current->flags |= PF_NO_SETAFFINITY;
7447
7448         audit_alloc_kernel(current);
7449
7450         mutex_lock(&sqd->lock);
7451         while (1) {
7452                 bool cap_entries, sqt_spin = false;
7453
7454                 if (io_sqd_events_pending(sqd) || signal_pending(current)) {
7455                         if (io_sqd_handle_event(sqd))
7456                                 break;
7457                         timeout = jiffies + sqd->sq_thread_idle;
7458                 }
7459
7460                 cap_entries = !list_is_singular(&sqd->ctx_list);
7461                 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
7462                         int ret = __io_sq_thread(ctx, cap_entries);
7463
7464                         if (!sqt_spin && (ret > 0 || !wq_list_empty(&ctx->iopoll_list)))
7465                                 sqt_spin = true;
7466                 }
7467                 if (io_run_task_work())
7468                         sqt_spin = true;
7469
7470                 if (sqt_spin || !time_after(jiffies, timeout)) {
7471                         cond_resched();
7472                         if (sqt_spin)
7473                                 timeout = jiffies + sqd->sq_thread_idle;
7474                         continue;
7475                 }
7476
7477                 prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE);
7478                 if (!io_sqd_events_pending(sqd) && !current->task_works) {
7479                         bool needs_sched = true;
7480
7481                         list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
7482                                 io_ring_set_wakeup_flag(ctx);
7483
7484                                 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
7485                                     !wq_list_empty(&ctx->iopoll_list)) {
7486                                         needs_sched = false;
7487                                         break;
7488                                 }
7489                                 if (io_sqring_entries(ctx)) {
7490                                         needs_sched = false;
7491                                         break;
7492                                 }
7493                         }
7494
7495                         if (needs_sched) {
7496                                 mutex_unlock(&sqd->lock);
7497                                 schedule();
7498                                 mutex_lock(&sqd->lock);
7499                         }
7500                         list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7501                                 io_ring_clear_wakeup_flag(ctx);
7502                 }
7503
7504                 finish_wait(&sqd->wait, &wait);
7505                 timeout = jiffies + sqd->sq_thread_idle;
7506         }
7507
7508         io_uring_cancel_generic(true, sqd);
7509         sqd->thread = NULL;
7510         list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7511                 io_ring_set_wakeup_flag(ctx);
7512         io_run_task_work();
7513         mutex_unlock(&sqd->lock);
7514
7515         audit_free(current);
7516
7517         complete(&sqd->exited);
7518         do_exit(0);
7519 }
7520
7521 struct io_wait_queue {
7522         struct wait_queue_entry wq;
7523         struct io_ring_ctx *ctx;
7524         unsigned cq_tail;
7525         unsigned nr_timeouts;
7526 };
7527
7528 static inline bool io_should_wake(struct io_wait_queue *iowq)
7529 {
7530         struct io_ring_ctx *ctx = iowq->ctx;
7531         int dist = ctx->cached_cq_tail - (int) iowq->cq_tail;
7532
7533         /*
7534          * Wake up if we have enough events, or if a timeout occurred since we
7535          * started waiting. For timeouts, we always want to return to userspace,
7536          * regardless of event count.
7537          */
7538         return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
7539 }
7540
7541 static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
7542                             int wake_flags, void *key)
7543 {
7544         struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
7545                                                         wq);
7546
7547         /*
7548          * Cannot safely flush overflowed CQEs from here, ensure we wake up
7549          * the task, and the next invocation will do it.
7550          */
7551         if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->check_cq_overflow))
7552                 return autoremove_wake_function(curr, mode, wake_flags, key);
7553         return -1;
7554 }
7555
7556 static int io_run_task_work_sig(void)
7557 {
7558         if (io_run_task_work())
7559                 return 1;
7560         if (!signal_pending(current))
7561                 return 0;
7562         if (test_thread_flag(TIF_NOTIFY_SIGNAL))
7563                 return -ERESTARTSYS;
7564         return -EINTR;
7565 }
7566
7567 /* when returns >0, the caller should retry */
7568 static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,
7569                                           struct io_wait_queue *iowq,
7570                                           signed long *timeout)
7571 {
7572         int ret;
7573
7574         /* make sure we run task_work before checking for signals */
7575         ret = io_run_task_work_sig();
7576         if (ret || io_should_wake(iowq))
7577                 return ret;
7578         /* let the caller flush overflows, retry */
7579         if (test_bit(0, &ctx->check_cq_overflow))
7580                 return 1;
7581
7582         *timeout = schedule_timeout(*timeout);
7583         return !*timeout ? -ETIME : 1;
7584 }
7585
7586 /*
7587  * Wait until events become available, if we don't already have some. The
7588  * application must reap them itself, as they reside on the shared cq ring.
7589  */
7590 static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
7591                           const sigset_t __user *sig, size_t sigsz,
7592                           struct __kernel_timespec __user *uts)
7593 {
7594         struct io_wait_queue iowq;
7595         struct io_rings *rings = ctx->rings;
7596         signed long timeout = MAX_SCHEDULE_TIMEOUT;
7597         int ret;
7598
7599         do {
7600                 io_cqring_overflow_flush(ctx);
7601                 if (io_cqring_events(ctx) >= min_events)
7602                         return 0;
7603                 if (!io_run_task_work())
7604                         break;
7605         } while (1);
7606
7607         if (uts) {
7608                 struct timespec64 ts;
7609
7610                 if (get_timespec64(&ts, uts))
7611                         return -EFAULT;
7612                 timeout = timespec64_to_jiffies(&ts);
7613         }
7614
7615         if (sig) {
7616 #ifdef CONFIG_COMPAT
7617                 if (in_compat_syscall())
7618                         ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
7619                                                       sigsz);
7620                 else
7621 #endif
7622                         ret = set_user_sigmask(sig, sigsz);
7623
7624                 if (ret)
7625                         return ret;
7626         }
7627
7628         init_waitqueue_func_entry(&iowq.wq, io_wake_function);
7629         iowq.wq.private = current;
7630         INIT_LIST_HEAD(&iowq.wq.entry);
7631         iowq.ctx = ctx;
7632         iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
7633         iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events;
7634
7635         trace_io_uring_cqring_wait(ctx, min_events);
7636         do {
7637                 /* if we can't even flush overflow, don't wait for more */
7638                 if (!io_cqring_overflow_flush(ctx)) {
7639                         ret = -EBUSY;
7640                         break;
7641                 }
7642                 prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq,
7643                                                 TASK_INTERRUPTIBLE);
7644                 ret = io_cqring_wait_schedule(ctx, &iowq, &timeout);
7645                 finish_wait(&ctx->cq_wait, &iowq.wq);
7646                 cond_resched();
7647         } while (ret > 0);
7648
7649         restore_saved_sigmask_unless(ret == -EINTR);
7650
7651         return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
7652 }
7653
7654 static void io_free_page_table(void **table, size_t size)
7655 {
7656         unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
7657
7658         for (i = 0; i < nr_tables; i++)
7659                 kfree(table[i]);
7660         kfree(table);
7661 }
7662
7663 static __cold void **io_alloc_page_table(size_t size)
7664 {
7665         unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
7666         size_t init_size = size;
7667         void **table;
7668
7669         table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT);
7670         if (!table)
7671                 return NULL;
7672
7673         for (i = 0; i < nr_tables; i++) {
7674                 unsigned int this_size = min_t(size_t, size, PAGE_SIZE);
7675
7676                 table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT);
7677                 if (!table[i]) {
7678                         io_free_page_table(table, init_size);
7679                         return NULL;
7680                 }
7681                 size -= this_size;
7682         }
7683         return table;
7684 }
7685
7686 static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node)
7687 {
7688         percpu_ref_exit(&ref_node->refs);
7689         kfree(ref_node);
7690 }
7691
7692 static __cold void io_rsrc_node_ref_zero(struct percpu_ref *ref)
7693 {
7694         struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs);
7695         struct io_ring_ctx *ctx = node->rsrc_data->ctx;
7696         unsigned long flags;
7697         bool first_add = false;
7698
7699         spin_lock_irqsave(&ctx->rsrc_ref_lock, flags);
7700         node->done = true;
7701
7702         while (!list_empty(&ctx->rsrc_ref_list)) {
7703                 node = list_first_entry(&ctx->rsrc_ref_list,
7704                                             struct io_rsrc_node, node);
7705                 /* recycle ref nodes in order */
7706                 if (!node->done)
7707                         break;
7708                 list_del(&node->node);
7709                 first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist);
7710         }
7711         spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags);
7712
7713         if (first_add)
7714                 mod_delayed_work(system_wq, &ctx->rsrc_put_work, HZ);
7715 }
7716
7717 static struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx)
7718 {
7719         struct io_rsrc_node *ref_node;
7720
7721         ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7722         if (!ref_node)
7723                 return NULL;
7724
7725         if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero,
7726                             0, GFP_KERNEL)) {
7727                 kfree(ref_node);
7728                 return NULL;
7729         }
7730         INIT_LIST_HEAD(&ref_node->node);
7731         INIT_LIST_HEAD(&ref_node->rsrc_list);
7732         ref_node->done = false;
7733         return ref_node;
7734 }
7735
7736 static void io_rsrc_node_switch(struct io_ring_ctx *ctx,
7737                                 struct io_rsrc_data *data_to_kill)
7738         __must_hold(&ctx->uring_lock)
7739 {
7740         WARN_ON_ONCE(!ctx->rsrc_backup_node);
7741         WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node);
7742
7743         io_rsrc_refs_drop(ctx);
7744
7745         if (data_to_kill) {
7746                 struct io_rsrc_node *rsrc_node = ctx->rsrc_node;
7747
7748                 rsrc_node->rsrc_data = data_to_kill;
7749                 spin_lock_irq(&ctx->rsrc_ref_lock);
7750                 list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list);
7751                 spin_unlock_irq(&ctx->rsrc_ref_lock);
7752
7753                 atomic_inc(&data_to_kill->refs);
7754                 percpu_ref_kill(&rsrc_node->refs);
7755                 ctx->rsrc_node = NULL;
7756         }
7757
7758         if (!ctx->rsrc_node) {
7759                 ctx->rsrc_node = ctx->rsrc_backup_node;
7760                 ctx->rsrc_backup_node = NULL;
7761         }
7762 }
7763
7764 static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx)
7765 {
7766         if (ctx->rsrc_backup_node)
7767                 return 0;
7768         ctx->rsrc_backup_node = io_rsrc_node_alloc(ctx);
7769         return ctx->rsrc_backup_node ? 0 : -ENOMEM;
7770 }
7771
7772 static __cold int io_rsrc_ref_quiesce(struct io_rsrc_data *data,
7773                                       struct io_ring_ctx *ctx)
7774 {
7775         int ret;
7776
7777         /* As we may drop ->uring_lock, other task may have started quiesce */
7778         if (data->quiesce)
7779                 return -ENXIO;
7780
7781         data->quiesce = true;
7782         do {
7783                 ret = io_rsrc_node_switch_start(ctx);
7784                 if (ret)
7785                         break;
7786                 io_rsrc_node_switch(ctx, data);
7787
7788                 /* kill initial ref, already quiesced if zero */
7789                 if (atomic_dec_and_test(&data->refs))
7790                         break;
7791                 mutex_unlock(&ctx->uring_lock);
7792                 flush_delayed_work(&ctx->rsrc_put_work);
7793                 ret = wait_for_completion_interruptible(&data->done);
7794                 if (!ret) {
7795                         mutex_lock(&ctx->uring_lock);
7796                         break;
7797                 }
7798
7799                 atomic_inc(&data->refs);
7800                 /* wait for all works potentially completing data->done */
7801                 flush_delayed_work(&ctx->rsrc_put_work);
7802                 reinit_completion(&data->done);
7803
7804                 ret = io_run_task_work_sig();
7805                 mutex_lock(&ctx->uring_lock);
7806         } while (ret >= 0);
7807         data->quiesce = false;
7808
7809         return ret;
7810 }
7811
7812 static u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx)
7813 {
7814         unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK;
7815         unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT;
7816
7817         return &data->tags[table_idx][off];
7818 }
7819
7820 static void io_rsrc_data_free(struct io_rsrc_data *data)
7821 {
7822         size_t size = data->nr * sizeof(data->tags[0][0]);
7823
7824         if (data->tags)
7825                 io_free_page_table((void **)data->tags, size);
7826         kfree(data);
7827 }
7828
7829 static __cold int io_rsrc_data_alloc(struct io_ring_ctx *ctx, rsrc_put_fn *do_put,
7830                                      u64 __user *utags, unsigned nr,
7831                                      struct io_rsrc_data **pdata)
7832 {
7833         struct io_rsrc_data *data;
7834         int ret = -ENOMEM;
7835         unsigned i;
7836
7837         data = kzalloc(sizeof(*data), GFP_KERNEL);
7838         if (!data)
7839                 return -ENOMEM;
7840         data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0]));
7841         if (!data->tags) {
7842                 kfree(data);
7843                 return -ENOMEM;
7844         }
7845
7846         data->nr = nr;
7847         data->ctx = ctx;
7848         data->do_put = do_put;
7849         if (utags) {
7850                 ret = -EFAULT;
7851                 for (i = 0; i < nr; i++) {
7852                         u64 *tag_slot = io_get_tag_slot(data, i);
7853
7854                         if (copy_from_user(tag_slot, &utags[i],
7855                                            sizeof(*tag_slot)))
7856                                 goto fail;
7857                 }
7858         }
7859
7860         atomic_set(&data->refs, 1);
7861         init_completion(&data->done);
7862         *pdata = data;
7863         return 0;
7864 fail:
7865         io_rsrc_data_free(data);
7866         return ret;
7867 }
7868
7869 static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files)
7870 {
7871         table->files = kvcalloc(nr_files, sizeof(table->files[0]),
7872                                 GFP_KERNEL_ACCOUNT);
7873         return !!table->files;
7874 }
7875
7876 static void io_free_file_tables(struct io_file_table *table)
7877 {
7878         kvfree(table->files);
7879         table->files = NULL;
7880 }
7881
7882 static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
7883 {
7884 #if defined(CONFIG_UNIX)
7885         if (ctx->ring_sock) {
7886                 struct sock *sock = ctx->ring_sock->sk;
7887                 struct sk_buff *skb;
7888
7889                 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
7890                         kfree_skb(skb);
7891         }
7892 #else
7893         int i;
7894
7895         for (i = 0; i < ctx->nr_user_files; i++) {
7896                 struct file *file;
7897
7898                 file = io_file_from_index(ctx, i);
7899                 if (file)
7900                         fput(file);
7901         }
7902 #endif
7903         io_free_file_tables(&ctx->file_table);
7904         io_rsrc_data_free(ctx->file_data);
7905         ctx->file_data = NULL;
7906         ctx->nr_user_files = 0;
7907 }
7908
7909 static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
7910 {
7911         int ret;
7912
7913         if (!ctx->file_data)
7914                 return -ENXIO;
7915         ret = io_rsrc_ref_quiesce(ctx->file_data, ctx);
7916         if (!ret)
7917                 __io_sqe_files_unregister(ctx);
7918         return ret;
7919 }
7920
7921 static void io_sq_thread_unpark(struct io_sq_data *sqd)
7922         __releases(&sqd->lock)
7923 {
7924         WARN_ON_ONCE(sqd->thread == current);
7925
7926         /*
7927          * Do the dance but not conditional clear_bit() because it'd race with
7928          * other threads incrementing park_pending and setting the bit.
7929          */
7930         clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
7931         if (atomic_dec_return(&sqd->park_pending))
7932                 set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
7933         mutex_unlock(&sqd->lock);
7934 }
7935
7936 static void io_sq_thread_park(struct io_sq_data *sqd)
7937         __acquires(&sqd->lock)
7938 {
7939         WARN_ON_ONCE(sqd->thread == current);
7940
7941         atomic_inc(&sqd->park_pending);
7942         set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
7943         mutex_lock(&sqd->lock);
7944         if (sqd->thread)
7945                 wake_up_process(sqd->thread);
7946 }
7947
7948 static void io_sq_thread_stop(struct io_sq_data *sqd)
7949 {
7950         WARN_ON_ONCE(sqd->thread == current);
7951         WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state));
7952
7953         set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
7954         mutex_lock(&sqd->lock);
7955         if (sqd->thread)
7956                 wake_up_process(sqd->thread);
7957         mutex_unlock(&sqd->lock);
7958         wait_for_completion(&sqd->exited);
7959 }
7960
7961 static void io_put_sq_data(struct io_sq_data *sqd)
7962 {
7963         if (refcount_dec_and_test(&sqd->refs)) {
7964                 WARN_ON_ONCE(atomic_read(&sqd->park_pending));
7965
7966                 io_sq_thread_stop(sqd);
7967                 kfree(sqd);
7968         }
7969 }
7970
7971 static void io_sq_thread_finish(struct io_ring_ctx *ctx)
7972 {
7973         struct io_sq_data *sqd = ctx->sq_data;
7974
7975         if (sqd) {
7976                 io_sq_thread_park(sqd);
7977                 list_del_init(&ctx->sqd_list);
7978                 io_sqd_update_thread_idle(sqd);
7979                 io_sq_thread_unpark(sqd);
7980
7981                 io_put_sq_data(sqd);
7982                 ctx->sq_data = NULL;
7983         }
7984 }
7985
7986 static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7987 {
7988         struct io_ring_ctx *ctx_attach;
7989         struct io_sq_data *sqd;
7990         struct fd f;
7991
7992         f = fdget(p->wq_fd);
7993         if (!f.file)
7994                 return ERR_PTR(-ENXIO);
7995         if (f.file->f_op != &io_uring_fops) {
7996                 fdput(f);
7997                 return ERR_PTR(-EINVAL);
7998         }
7999
8000         ctx_attach = f.file->private_data;
8001         sqd = ctx_attach->sq_data;
8002         if (!sqd) {
8003                 fdput(f);
8004                 return ERR_PTR(-EINVAL);
8005         }
8006         if (sqd->task_tgid != current->tgid) {
8007                 fdput(f);
8008                 return ERR_PTR(-EPERM);
8009         }
8010
8011         refcount_inc(&sqd->refs);
8012         fdput(f);
8013         return sqd;
8014 }
8015
8016 static struct io_sq_data *io_get_sq_data(struct io_uring_params *p,
8017                                          bool *attached)
8018 {
8019         struct io_sq_data *sqd;
8020
8021         *attached = false;
8022         if (p->flags & IORING_SETUP_ATTACH_WQ) {
8023                 sqd = io_attach_sq_data(p);
8024                 if (!IS_ERR(sqd)) {
8025                         *attached = true;
8026                         return sqd;
8027                 }
8028                 /* fall through for EPERM case, setup new sqd/task */
8029                 if (PTR_ERR(sqd) != -EPERM)
8030                         return sqd;
8031         }
8032
8033         sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
8034         if (!sqd)
8035                 return ERR_PTR(-ENOMEM);
8036
8037         atomic_set(&sqd->park_pending, 0);
8038         refcount_set(&sqd->refs, 1);
8039         INIT_LIST_HEAD(&sqd->ctx_list);
8040         mutex_init(&sqd->lock);
8041         init_waitqueue_head(&sqd->wait);
8042         init_completion(&sqd->exited);
8043         return sqd;
8044 }
8045
8046 #if defined(CONFIG_UNIX)
8047 /*
8048  * Ensure the UNIX gc is aware of our file set, so we are certain that
8049  * the io_uring can be safely unregistered on process exit, even if we have
8050  * loops in the file referencing.
8051  */
8052 static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
8053 {
8054         struct sock *sk = ctx->ring_sock->sk;
8055         struct scm_fp_list *fpl;
8056         struct sk_buff *skb;
8057         int i, nr_files;
8058
8059         fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
8060         if (!fpl)
8061                 return -ENOMEM;
8062
8063         skb = alloc_skb(0, GFP_KERNEL);
8064         if (!skb) {
8065                 kfree(fpl);
8066                 return -ENOMEM;
8067         }
8068
8069         skb->sk = sk;
8070
8071         nr_files = 0;
8072         fpl->user = get_uid(current_user());
8073         for (i = 0; i < nr; i++) {
8074                 struct file *file = io_file_from_index(ctx, i + offset);
8075
8076                 if (!file)
8077                         continue;
8078                 fpl->fp[nr_files] = get_file(file);
8079                 unix_inflight(fpl->user, fpl->fp[nr_files]);
8080                 nr_files++;
8081         }
8082
8083         if (nr_files) {
8084                 fpl->max = SCM_MAX_FD;
8085                 fpl->count = nr_files;
8086                 UNIXCB(skb).fp = fpl;
8087                 skb->destructor = unix_destruct_scm;
8088                 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
8089                 skb_queue_head(&sk->sk_receive_queue, skb);
8090
8091                 for (i = 0; i < nr_files; i++)
8092                         fput(fpl->fp[i]);
8093         } else {
8094                 kfree_skb(skb);
8095                 kfree(fpl);
8096         }
8097
8098         return 0;
8099 }
8100
8101 /*
8102  * If UNIX sockets are enabled, fd passing can cause a reference cycle which
8103  * causes regular reference counting to break down. We rely on the UNIX
8104  * garbage collection to take care of this problem for us.
8105  */
8106 static int io_sqe_files_scm(struct io_ring_ctx *ctx)
8107 {
8108         unsigned left, total;
8109         int ret = 0;
8110
8111         total = 0;
8112         left = ctx->nr_user_files;
8113         while (left) {
8114                 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
8115
8116                 ret = __io_sqe_files_scm(ctx, this_files, total);
8117                 if (ret)
8118                         break;
8119                 left -= this_files;
8120                 total += this_files;
8121         }
8122
8123         if (!ret)
8124                 return 0;
8125
8126         while (total < ctx->nr_user_files) {
8127                 struct file *file = io_file_from_index(ctx, total);
8128
8129                 if (file)
8130                         fput(file);
8131                 total++;
8132         }
8133
8134         return ret;
8135 }
8136 #else
8137 static int io_sqe_files_scm(struct io_ring_ctx *ctx)
8138 {
8139         return 0;
8140 }
8141 #endif
8142
8143 static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
8144 {
8145         struct file *file = prsrc->file;
8146 #if defined(CONFIG_UNIX)
8147         struct sock *sock = ctx->ring_sock->sk;
8148         struct sk_buff_head list, *head = &sock->sk_receive_queue;
8149         struct sk_buff *skb;
8150         int i;
8151
8152         __skb_queue_head_init(&list);
8153
8154         /*
8155          * Find the skb that holds this file in its SCM_RIGHTS. When found,
8156          * remove this entry and rearrange the file array.
8157          */
8158         skb = skb_dequeue(head);
8159         while (skb) {
8160                 struct scm_fp_list *fp;
8161
8162                 fp = UNIXCB(skb).fp;
8163                 for (i = 0; i < fp->count; i++) {
8164                         int left;
8165
8166                         if (fp->fp[i] != file)
8167                                 continue;
8168
8169                         unix_notinflight(fp->user, fp->fp[i]);
8170                         left = fp->count - 1 - i;
8171                         if (left) {
8172                                 memmove(&fp->fp[i], &fp->fp[i + 1],
8173                                                 left * sizeof(struct file *));
8174                         }
8175                         fp->count--;
8176                         if (!fp->count) {
8177                                 kfree_skb(skb);
8178                                 skb = NULL;
8179                         } else {
8180                                 __skb_queue_tail(&list, skb);
8181                         }
8182                         fput(file);
8183                         file = NULL;
8184                         break;
8185                 }
8186
8187                 if (!file)
8188                         break;
8189
8190                 __skb_queue_tail(&list, skb);
8191
8192                 skb = skb_dequeue(head);
8193         }
8194
8195         if (skb_peek(&list)) {
8196                 spin_lock_irq(&head->lock);
8197                 while ((skb = __skb_dequeue(&list)) != NULL)
8198                         __skb_queue_tail(head, skb);
8199                 spin_unlock_irq(&head->lock);
8200         }
8201 #else
8202         fput(file);
8203 #endif
8204 }
8205
8206 static void __io_rsrc_put_work(struct io_rsrc_node *ref_node)
8207 {
8208         struct io_rsrc_data *rsrc_data = ref_node->rsrc_data;
8209         struct io_ring_ctx *ctx = rsrc_data->ctx;
8210         struct io_rsrc_put *prsrc, *tmp;
8211
8212         list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) {
8213                 list_del(&prsrc->list);
8214
8215                 if (prsrc->tag) {
8216                         bool lock_ring = ctx->flags & IORING_SETUP_IOPOLL;
8217
8218                         io_ring_submit_lock(ctx, lock_ring);
8219                         spin_lock(&ctx->completion_lock);
8220                         io_cqring_fill_event(ctx, prsrc->tag, 0, 0);
8221                         ctx->cq_extra++;
8222                         io_commit_cqring(ctx);
8223                         spin_unlock(&ctx->completion_lock);
8224                         io_cqring_ev_posted(ctx);
8225                         io_ring_submit_unlock(ctx, lock_ring);
8226                 }
8227
8228                 rsrc_data->do_put(ctx, prsrc);
8229                 kfree(prsrc);
8230         }
8231
8232         io_rsrc_node_destroy(ref_node);
8233         if (atomic_dec_and_test(&rsrc_data->refs))
8234                 complete(&rsrc_data->done);
8235 }
8236
8237 static void io_rsrc_put_work(struct work_struct *work)
8238 {
8239         struct io_ring_ctx *ctx;
8240         struct llist_node *node;
8241
8242         ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work);
8243         node = llist_del_all(&ctx->rsrc_put_llist);
8244
8245         while (node) {
8246                 struct io_rsrc_node *ref_node;
8247                 struct llist_node *next = node->next;
8248
8249                 ref_node = llist_entry(node, struct io_rsrc_node, llist);
8250                 __io_rsrc_put_work(ref_node);
8251                 node = next;
8252         }
8253 }
8254
8255 static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
8256                                  unsigned nr_args, u64 __user *tags)
8257 {
8258         __s32 __user *fds = (__s32 __user *) arg;
8259         struct file *file;
8260         int fd, ret;
8261         unsigned i;
8262
8263         if (ctx->file_data)
8264                 return -EBUSY;
8265         if (!nr_args)
8266                 return -EINVAL;
8267         if (nr_args > IORING_MAX_FIXED_FILES)
8268                 return -EMFILE;
8269         if (nr_args > rlimit(RLIMIT_NOFILE))
8270                 return -EMFILE;
8271         ret = io_rsrc_node_switch_start(ctx);
8272         if (ret)
8273                 return ret;
8274         ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args,
8275                                  &ctx->file_data);
8276         if (ret)
8277                 return ret;
8278
8279         ret = -ENOMEM;
8280         if (!io_alloc_file_tables(&ctx->file_table, nr_args))
8281                 goto out_free;
8282
8283         for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
8284                 if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
8285                         ret = -EFAULT;
8286                         goto out_fput;
8287                 }
8288                 /* allow sparse sets */
8289                 if (fd == -1) {
8290                         ret = -EINVAL;
8291                         if (unlikely(*io_get_tag_slot(ctx->file_data, i)))
8292                                 goto out_fput;
8293                         continue;
8294                 }
8295
8296                 file = fget(fd);
8297                 ret = -EBADF;
8298                 if (unlikely(!file))
8299                         goto out_fput;
8300
8301                 /*
8302                  * Don't allow io_uring instances to be registered. If UNIX
8303                  * isn't enabled, then this causes a reference cycle and this
8304                  * instance can never get freed. If UNIX is enabled we'll
8305                  * handle it just fine, but there's still no point in allowing
8306                  * a ring fd as it doesn't support regular read/write anyway.
8307                  */
8308                 if (file->f_op == &io_uring_fops) {
8309                         fput(file);
8310                         goto out_fput;
8311                 }
8312                 io_fixed_file_set(io_fixed_file_slot(&ctx->file_table, i), file);
8313         }
8314
8315         ret = io_sqe_files_scm(ctx);
8316         if (ret) {
8317                 __io_sqe_files_unregister(ctx);
8318                 return ret;
8319         }
8320
8321         io_rsrc_node_switch(ctx, NULL);
8322         return ret;
8323 out_fput:
8324         for (i = 0; i < ctx->nr_user_files; i++) {
8325                 file = io_file_from_index(ctx, i);
8326                 if (file)
8327                         fput(file);
8328         }
8329         io_free_file_tables(&ctx->file_table);
8330         ctx->nr_user_files = 0;
8331 out_free:
8332         io_rsrc_data_free(ctx->file_data);
8333         ctx->file_data = NULL;
8334         return ret;
8335 }
8336
8337 static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
8338                                 int index)
8339 {
8340 #if defined(CONFIG_UNIX)
8341         struct sock *sock = ctx->ring_sock->sk;
8342         struct sk_buff_head *head = &sock->sk_receive_queue;
8343         struct sk_buff *skb;
8344
8345         /*
8346          * See if we can merge this file into an existing skb SCM_RIGHTS
8347          * file set. If there's no room, fall back to allocating a new skb
8348          * and filling it in.
8349          */
8350         spin_lock_irq(&head->lock);
8351         skb = skb_peek(head);
8352         if (skb) {
8353                 struct scm_fp_list *fpl = UNIXCB(skb).fp;
8354
8355                 if (fpl->count < SCM_MAX_FD) {
8356                         __skb_unlink(skb, head);
8357                         spin_unlock_irq(&head->lock);
8358                         fpl->fp[fpl->count] = get_file(file);
8359                         unix_inflight(fpl->user, fpl->fp[fpl->count]);
8360                         fpl->count++;
8361                         spin_lock_irq(&head->lock);
8362                         __skb_queue_head(head, skb);
8363                 } else {
8364                         skb = NULL;
8365                 }
8366         }
8367         spin_unlock_irq(&head->lock);
8368
8369         if (skb) {
8370                 fput(file);
8371                 return 0;
8372         }
8373
8374         return __io_sqe_files_scm(ctx, 1, index);
8375 #else
8376         return 0;
8377 #endif
8378 }
8379
8380 static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx,
8381                                  struct io_rsrc_node *node, void *rsrc)
8382 {
8383         struct io_rsrc_put *prsrc;
8384
8385         prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL);
8386         if (!prsrc)
8387                 return -ENOMEM;
8388
8389         prsrc->tag = *io_get_tag_slot(data, idx);
8390         prsrc->rsrc = rsrc;
8391         list_add(&prsrc->list, &node->rsrc_list);
8392         return 0;
8393 }
8394
8395 static int io_install_fixed_file(struct io_kiocb *req, struct file *file,
8396                                  unsigned int issue_flags, u32 slot_index)
8397 {
8398         struct io_ring_ctx *ctx = req->ctx;
8399         bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
8400         bool needs_switch = false;
8401         struct io_fixed_file *file_slot;
8402         int ret = -EBADF;
8403
8404         io_ring_submit_lock(ctx, needs_lock);
8405         if (file->f_op == &io_uring_fops)
8406                 goto err;
8407         ret = -ENXIO;
8408         if (!ctx->file_data)
8409                 goto err;
8410         ret = -EINVAL;
8411         if (slot_index >= ctx->nr_user_files)
8412                 goto err;
8413
8414         slot_index = array_index_nospec(slot_index, ctx->nr_user_files);
8415         file_slot = io_fixed_file_slot(&ctx->file_table, slot_index);
8416
8417         if (file_slot->file_ptr) {
8418                 struct file *old_file;
8419
8420                 ret = io_rsrc_node_switch_start(ctx);
8421                 if (ret)
8422                         goto err;
8423
8424                 old_file = (struct file *)(file_slot->file_ptr & FFS_MASK);
8425                 ret = io_queue_rsrc_removal(ctx->file_data, slot_index,
8426                                             ctx->rsrc_node, old_file);
8427                 if (ret)
8428                         goto err;
8429                 file_slot->file_ptr = 0;
8430                 needs_switch = true;
8431         }
8432
8433         *io_get_tag_slot(ctx->file_data, slot_index) = 0;
8434         io_fixed_file_set(file_slot, file);
8435         ret = io_sqe_file_register(ctx, file, slot_index);
8436         if (ret) {
8437                 file_slot->file_ptr = 0;
8438                 goto err;
8439         }
8440
8441         ret = 0;
8442 err:
8443         if (needs_switch)
8444                 io_rsrc_node_switch(ctx, ctx->file_data);
8445         io_ring_submit_unlock(ctx, needs_lock);
8446         if (ret)
8447                 fput(file);
8448         return ret;
8449 }
8450
8451 static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags)
8452 {
8453         unsigned int offset = req->close.file_slot - 1;
8454         struct io_ring_ctx *ctx = req->ctx;
8455         bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
8456         struct io_fixed_file *file_slot;
8457         struct file *file;
8458         int ret, i;
8459
8460         io_ring_submit_lock(ctx, needs_lock);
8461         ret = -ENXIO;
8462         if (unlikely(!ctx->file_data))
8463                 goto out;
8464         ret = -EINVAL;
8465         if (offset >= ctx->nr_user_files)
8466                 goto out;
8467         ret = io_rsrc_node_switch_start(ctx);
8468         if (ret)
8469                 goto out;
8470
8471         i = array_index_nospec(offset, ctx->nr_user_files);
8472         file_slot = io_fixed_file_slot(&ctx->file_table, i);
8473         ret = -EBADF;
8474         if (!file_slot->file_ptr)
8475                 goto out;
8476
8477         file = (struct file *)(file_slot->file_ptr & FFS_MASK);
8478         ret = io_queue_rsrc_removal(ctx->file_data, offset, ctx->rsrc_node, file);
8479         if (ret)
8480                 goto out;
8481
8482         file_slot->file_ptr = 0;
8483         io_rsrc_node_switch(ctx, ctx->file_data);
8484         ret = 0;
8485 out:
8486         io_ring_submit_unlock(ctx, needs_lock);
8487         return ret;
8488 }
8489
8490 static int __io_sqe_files_update(struct io_ring_ctx *ctx,
8491                                  struct io_uring_rsrc_update2 *up,
8492                                  unsigned nr_args)
8493 {
8494         u64 __user *tags = u64_to_user_ptr(up->tags);
8495         __s32 __user *fds = u64_to_user_ptr(up->data);
8496         struct io_rsrc_data *data = ctx->file_data;
8497         struct io_fixed_file *file_slot;
8498         struct file *file;
8499         int fd, i, err = 0;
8500         unsigned int done;
8501         bool needs_switch = false;
8502
8503         if (!ctx->file_data)
8504                 return -ENXIO;
8505         if (up->offset + nr_args > ctx->nr_user_files)
8506                 return -EINVAL;
8507
8508         for (done = 0; done < nr_args; done++) {
8509                 u64 tag = 0;
8510
8511                 if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) ||
8512                     copy_from_user(&fd, &fds[done], sizeof(fd))) {
8513                         err = -EFAULT;
8514                         break;
8515                 }
8516                 if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) {
8517                         err = -EINVAL;
8518                         break;
8519                 }
8520                 if (fd == IORING_REGISTER_FILES_SKIP)
8521                         continue;
8522
8523                 i = array_index_nospec(up->offset + done, ctx->nr_user_files);
8524                 file_slot = io_fixed_file_slot(&ctx->file_table, i);
8525
8526                 if (file_slot->file_ptr) {
8527                         file = (struct file *)(file_slot->file_ptr & FFS_MASK);
8528                         err = io_queue_rsrc_removal(data, up->offset + done,
8529                                                     ctx->rsrc_node, file);
8530                         if (err)
8531                                 break;
8532                         file_slot->file_ptr = 0;
8533                         needs_switch = true;
8534                 }
8535                 if (fd != -1) {
8536                         file = fget(fd);
8537                         if (!file) {
8538                                 err = -EBADF;
8539                                 break;
8540                         }
8541                         /*
8542                          * Don't allow io_uring instances to be registered. If
8543                          * UNIX isn't enabled, then this causes a reference
8544                          * cycle and this instance can never get freed. If UNIX
8545                          * is enabled we'll handle it just fine, but there's
8546                          * still no point in allowing a ring fd as it doesn't
8547                          * support regular read/write anyway.
8548                          */
8549                         if (file->f_op == &io_uring_fops) {
8550                                 fput(file);
8551                                 err = -EBADF;
8552                                 break;
8553                         }
8554                         *io_get_tag_slot(data, up->offset + done) = tag;
8555                         io_fixed_file_set(file_slot, file);
8556                         err = io_sqe_file_register(ctx, file, i);
8557                         if (err) {
8558                                 file_slot->file_ptr = 0;
8559                                 fput(file);
8560                                 break;
8561                         }
8562                 }
8563         }
8564
8565         if (needs_switch)
8566                 io_rsrc_node_switch(ctx, data);
8567         return done ? done : err;
8568 }
8569
8570 static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx,
8571                                         struct task_struct *task)
8572 {
8573         struct io_wq_hash *hash;
8574         struct io_wq_data data;
8575         unsigned int concurrency;
8576
8577         mutex_lock(&ctx->uring_lock);
8578         hash = ctx->hash_map;
8579         if (!hash) {
8580                 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
8581                 if (!hash) {
8582                         mutex_unlock(&ctx->uring_lock);
8583                         return ERR_PTR(-ENOMEM);
8584                 }
8585                 refcount_set(&hash->refs, 1);
8586                 init_waitqueue_head(&hash->wait);
8587                 ctx->hash_map = hash;
8588         }
8589         mutex_unlock(&ctx->uring_lock);
8590
8591         data.hash = hash;
8592         data.task = task;
8593         data.free_work = io_wq_free_work;
8594         data.do_work = io_wq_submit_work;
8595
8596         /* Do QD, or 4 * CPUS, whatever is smallest */
8597         concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
8598
8599         return io_wq_create(concurrency, &data);
8600 }
8601
8602 static __cold int io_uring_alloc_task_context(struct task_struct *task,
8603                                               struct io_ring_ctx *ctx)
8604 {
8605         struct io_uring_task *tctx;
8606         int ret;
8607
8608         tctx = kzalloc(sizeof(*tctx), GFP_KERNEL);
8609         if (unlikely(!tctx))
8610                 return -ENOMEM;
8611
8612         ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
8613         if (unlikely(ret)) {
8614                 kfree(tctx);
8615                 return ret;
8616         }
8617
8618         tctx->io_wq = io_init_wq_offload(ctx, task);
8619         if (IS_ERR(tctx->io_wq)) {
8620                 ret = PTR_ERR(tctx->io_wq);
8621                 percpu_counter_destroy(&tctx->inflight);
8622                 kfree(tctx);
8623                 return ret;
8624         }
8625
8626         xa_init(&tctx->xa);
8627         init_waitqueue_head(&tctx->wait);
8628         atomic_set(&tctx->in_idle, 0);
8629         atomic_set(&tctx->inflight_tracked, 0);
8630         task->io_uring = tctx;
8631         spin_lock_init(&tctx->task_lock);
8632         INIT_WQ_LIST(&tctx->task_list);
8633         init_task_work(&tctx->task_work, tctx_task_work);
8634         return 0;
8635 }
8636
8637 void __io_uring_free(struct task_struct *tsk)
8638 {
8639         struct io_uring_task *tctx = tsk->io_uring;
8640
8641         WARN_ON_ONCE(!xa_empty(&tctx->xa));
8642         WARN_ON_ONCE(tctx->io_wq);
8643         WARN_ON_ONCE(tctx->cached_refs);
8644
8645         percpu_counter_destroy(&tctx->inflight);
8646         kfree(tctx);
8647         tsk->io_uring = NULL;
8648 }
8649
8650 static __cold int io_sq_offload_create(struct io_ring_ctx *ctx,
8651                                        struct io_uring_params *p)
8652 {
8653         int ret;
8654
8655         /* Retain compatibility with failing for an invalid attach attempt */
8656         if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) ==
8657                                 IORING_SETUP_ATTACH_WQ) {
8658                 struct fd f;
8659
8660                 f = fdget(p->wq_fd);
8661                 if (!f.file)
8662                         return -ENXIO;
8663                 if (f.file->f_op != &io_uring_fops) {
8664                         fdput(f);
8665                         return -EINVAL;
8666                 }
8667                 fdput(f);
8668         }
8669         if (ctx->flags & IORING_SETUP_SQPOLL) {
8670                 struct task_struct *tsk;
8671                 struct io_sq_data *sqd;
8672                 bool attached;
8673
8674                 ret = security_uring_sqpoll();
8675                 if (ret)
8676                         return ret;
8677
8678                 sqd = io_get_sq_data(p, &attached);
8679                 if (IS_ERR(sqd)) {
8680                         ret = PTR_ERR(sqd);
8681                         goto err;
8682                 }
8683
8684                 ctx->sq_creds = get_current_cred();
8685                 ctx->sq_data = sqd;
8686                 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
8687                 if (!ctx->sq_thread_idle)
8688                         ctx->sq_thread_idle = HZ;
8689
8690                 io_sq_thread_park(sqd);
8691                 list_add(&ctx->sqd_list, &sqd->ctx_list);
8692                 io_sqd_update_thread_idle(sqd);
8693                 /* don't attach to a dying SQPOLL thread, would be racy */
8694                 ret = (attached && !sqd->thread) ? -ENXIO : 0;
8695                 io_sq_thread_unpark(sqd);
8696
8697                 if (ret < 0)
8698                         goto err;
8699                 if (attached)
8700                         return 0;
8701
8702                 if (p->flags & IORING_SETUP_SQ_AFF) {
8703                         int cpu = p->sq_thread_cpu;
8704
8705                         ret = -EINVAL;
8706                         if (cpu >= nr_cpu_ids || !cpu_online(cpu))
8707                                 goto err_sqpoll;
8708                         sqd->sq_cpu = cpu;
8709                 } else {
8710                         sqd->sq_cpu = -1;
8711                 }
8712
8713                 sqd->task_pid = current->pid;
8714                 sqd->task_tgid = current->tgid;
8715                 tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE);
8716                 if (IS_ERR(tsk)) {
8717                         ret = PTR_ERR(tsk);
8718                         goto err_sqpoll;
8719                 }
8720
8721                 sqd->thread = tsk;
8722                 ret = io_uring_alloc_task_context(tsk, ctx);
8723                 wake_up_new_task(tsk);
8724                 if (ret)
8725                         goto err;
8726         } else if (p->flags & IORING_SETUP_SQ_AFF) {
8727                 /* Can't have SQ_AFF without SQPOLL */
8728                 ret = -EINVAL;
8729                 goto err;
8730         }
8731
8732         return 0;
8733 err_sqpoll:
8734         complete(&ctx->sq_data->exited);
8735 err:
8736         io_sq_thread_finish(ctx);
8737         return ret;
8738 }
8739
8740 static inline void __io_unaccount_mem(struct user_struct *user,
8741                                       unsigned long nr_pages)
8742 {
8743         atomic_long_sub(nr_pages, &user->locked_vm);
8744 }
8745
8746 static inline int __io_account_mem(struct user_struct *user,
8747                                    unsigned long nr_pages)
8748 {
8749         unsigned long page_limit, cur_pages, new_pages;
8750
8751         /* Don't allow more pages than we can safely lock */
8752         page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
8753
8754         do {
8755                 cur_pages = atomic_long_read(&user->locked_vm);
8756                 new_pages = cur_pages + nr_pages;
8757                 if (new_pages > page_limit)
8758                         return -ENOMEM;
8759         } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
8760                                         new_pages) != cur_pages);
8761
8762         return 0;
8763 }
8764
8765 static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
8766 {
8767         if (ctx->user)
8768                 __io_unaccount_mem(ctx->user, nr_pages);
8769
8770         if (ctx->mm_account)
8771                 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
8772 }
8773
8774 static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
8775 {
8776         int ret;
8777
8778         if (ctx->user) {
8779                 ret = __io_account_mem(ctx->user, nr_pages);
8780                 if (ret)
8781                         return ret;
8782         }
8783
8784         if (ctx->mm_account)
8785                 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
8786
8787         return 0;
8788 }
8789
8790 static void io_mem_free(void *ptr)
8791 {
8792         struct page *page;
8793
8794         if (!ptr)
8795                 return;
8796
8797         page = virt_to_head_page(ptr);
8798         if (put_page_testzero(page))
8799                 free_compound_page(page);
8800 }
8801
8802 static void *io_mem_alloc(size_t size)
8803 {
8804         gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
8805                                 __GFP_NORETRY | __GFP_ACCOUNT;
8806
8807         return (void *) __get_free_pages(gfp_flags, get_order(size));
8808 }
8809
8810 static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
8811                                 size_t *sq_offset)
8812 {
8813         struct io_rings *rings;
8814         size_t off, sq_array_size;
8815
8816         off = struct_size(rings, cqes, cq_entries);
8817         if (off == SIZE_MAX)
8818                 return SIZE_MAX;
8819
8820 #ifdef CONFIG_SMP
8821         off = ALIGN(off, SMP_CACHE_BYTES);
8822         if (off == 0)
8823                 return SIZE_MAX;
8824 #endif
8825
8826         if (sq_offset)
8827                 *sq_offset = off;
8828
8829         sq_array_size = array_size(sizeof(u32), sq_entries);
8830         if (sq_array_size == SIZE_MAX)
8831                 return SIZE_MAX;
8832
8833         if (check_add_overflow(off, sq_array_size, &off))
8834                 return SIZE_MAX;
8835
8836         return off;
8837 }
8838
8839 static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot)
8840 {
8841         struct io_mapped_ubuf *imu = *slot;
8842         unsigned int i;
8843
8844         if (imu != ctx->dummy_ubuf) {
8845                 for (i = 0; i < imu->nr_bvecs; i++)
8846                         unpin_user_page(imu->bvec[i].bv_page);
8847                 if (imu->acct_pages)
8848                         io_unaccount_mem(ctx, imu->acct_pages);
8849                 kvfree(imu);
8850         }
8851         *slot = NULL;
8852 }
8853
8854 static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
8855 {
8856         io_buffer_unmap(ctx, &prsrc->buf);
8857         prsrc->buf = NULL;
8858 }
8859
8860 static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
8861 {
8862         unsigned int i;
8863
8864         for (i = 0; i < ctx->nr_user_bufs; i++)
8865                 io_buffer_unmap(ctx, &ctx->user_bufs[i]);
8866         kfree(ctx->user_bufs);
8867         io_rsrc_data_free(ctx->buf_data);
8868         ctx->user_bufs = NULL;
8869         ctx->buf_data = NULL;
8870         ctx->nr_user_bufs = 0;
8871 }
8872
8873 static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
8874 {
8875         int ret;
8876
8877         if (!ctx->buf_data)
8878                 return -ENXIO;
8879
8880         ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx);
8881         if (!ret)
8882                 __io_sqe_buffers_unregister(ctx);
8883         return ret;
8884 }
8885
8886 static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
8887                        void __user *arg, unsigned index)
8888 {
8889         struct iovec __user *src;
8890
8891 #ifdef CONFIG_COMPAT
8892         if (ctx->compat) {
8893                 struct compat_iovec __user *ciovs;
8894                 struct compat_iovec ciov;
8895
8896                 ciovs = (struct compat_iovec __user *) arg;
8897                 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8898                         return -EFAULT;
8899
8900                 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
8901                 dst->iov_len = ciov.iov_len;
8902                 return 0;
8903         }
8904 #endif
8905         src = (struct iovec __user *) arg;
8906         if (copy_from_user(dst, &src[index], sizeof(*dst)))
8907                 return -EFAULT;
8908         return 0;
8909 }
8910
8911 /*
8912  * Not super efficient, but this is just a registration time. And we do cache
8913  * the last compound head, so generally we'll only do a full search if we don't
8914  * match that one.
8915  *
8916  * We check if the given compound head page has already been accounted, to
8917  * avoid double accounting it. This allows us to account the full size of the
8918  * page, not just the constituent pages of a huge page.
8919  */
8920 static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
8921                                   int nr_pages, struct page *hpage)
8922 {
8923         int i, j;
8924
8925         /* check current page array */
8926         for (i = 0; i < nr_pages; i++) {
8927                 if (!PageCompound(pages[i]))
8928                         continue;
8929                 if (compound_head(pages[i]) == hpage)
8930                         return true;
8931         }
8932
8933         /* check previously registered pages */
8934         for (i = 0; i < ctx->nr_user_bufs; i++) {
8935                 struct io_mapped_ubuf *imu = ctx->user_bufs[i];
8936
8937                 for (j = 0; j < imu->nr_bvecs; j++) {
8938                         if (!PageCompound(imu->bvec[j].bv_page))
8939                                 continue;
8940                         if (compound_head(imu->bvec[j].bv_page) == hpage)
8941                                 return true;
8942                 }
8943         }
8944
8945         return false;
8946 }
8947
8948 static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
8949                                  int nr_pages, struct io_mapped_ubuf *imu,
8950                                  struct page **last_hpage)
8951 {
8952         int i, ret;
8953
8954         imu->acct_pages = 0;
8955         for (i = 0; i < nr_pages; i++) {
8956                 if (!PageCompound(pages[i])) {
8957                         imu->acct_pages++;
8958                 } else {
8959                         struct page *hpage;
8960
8961                         hpage = compound_head(pages[i]);
8962                         if (hpage == *last_hpage)
8963                                 continue;
8964                         *last_hpage = hpage;
8965                         if (headpage_already_acct(ctx, pages, i, hpage))
8966                                 continue;
8967                         imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
8968                 }
8969         }
8970
8971         if (!imu->acct_pages)
8972                 return 0;
8973
8974         ret = io_account_mem(ctx, imu->acct_pages);
8975         if (ret)
8976                 imu->acct_pages = 0;
8977         return ret;
8978 }
8979
8980 static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
8981                                   struct io_mapped_ubuf **pimu,
8982                                   struct page **last_hpage)
8983 {
8984         struct io_mapped_ubuf *imu = NULL;
8985         struct vm_area_struct **vmas = NULL;
8986         struct page **pages = NULL;
8987         unsigned long off, start, end, ubuf;
8988         size_t size;
8989         int ret, pret, nr_pages, i;
8990
8991         if (!iov->iov_base) {
8992                 *pimu = ctx->dummy_ubuf;
8993                 return 0;
8994         }
8995
8996         ubuf = (unsigned long) iov->iov_base;
8997         end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8998         start = ubuf >> PAGE_SHIFT;
8999         nr_pages = end - start;
9000
9001         *pimu = NULL;
9002         ret = -ENOMEM;
9003
9004         pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
9005         if (!pages)
9006                 goto done;
9007
9008         vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *),
9009                               GFP_KERNEL);
9010         if (!vmas)
9011                 goto done;
9012
9013         imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL);
9014         if (!imu)
9015                 goto done;
9016
9017         ret = 0;
9018         mmap_read_lock(current->mm);
9019         pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
9020                               pages, vmas);
9021         if (pret == nr_pages) {
9022                 /* don't support file backed memory */
9023                 for (i = 0; i < nr_pages; i++) {
9024                         struct vm_area_struct *vma = vmas[i];
9025
9026                         if (vma_is_shmem(vma))
9027                                 continue;
9028                         if (vma->vm_file &&
9029                             !is_file_hugepages(vma->vm_file)) {
9030                                 ret = -EOPNOTSUPP;
9031                                 break;
9032                         }
9033                 }
9034         } else {
9035                 ret = pret < 0 ? pret : -EFAULT;
9036         }
9037         mmap_read_unlock(current->mm);
9038         if (ret) {
9039                 /*
9040                  * if we did partial map, or found file backed vmas,
9041                  * release any pages we did get
9042                  */
9043                 if (pret > 0)
9044                         unpin_user_pages(pages, pret);
9045                 goto done;
9046         }
9047
9048         ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage);
9049         if (ret) {
9050                 unpin_user_pages(pages, pret);
9051                 goto done;
9052         }
9053
9054         off = ubuf & ~PAGE_MASK;
9055         size = iov->iov_len;
9056         for (i = 0; i < nr_pages; i++) {
9057                 size_t vec_len;
9058
9059                 vec_len = min_t(size_t, size, PAGE_SIZE - off);
9060                 imu->bvec[i].bv_page = pages[i];
9061                 imu->bvec[i].bv_len = vec_len;
9062                 imu->bvec[i].bv_offset = off;
9063                 off = 0;
9064                 size -= vec_len;
9065         }
9066         /* store original address for later verification */
9067         imu->ubuf = ubuf;
9068         imu->ubuf_end = ubuf + iov->iov_len;
9069         imu->nr_bvecs = nr_pages;
9070         *pimu = imu;
9071         ret = 0;
9072 done:
9073         if (ret)
9074                 kvfree(imu);
9075         kvfree(pages);
9076         kvfree(vmas);
9077         return ret;
9078 }
9079
9080 static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args)
9081 {
9082         ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL);
9083         return ctx->user_bufs ? 0 : -ENOMEM;
9084 }
9085
9086 static int io_buffer_validate(struct iovec *iov)
9087 {
9088         unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1);
9089
9090         /*
9091          * Don't impose further limits on the size and buffer
9092          * constraints here, we'll -EINVAL later when IO is
9093          * submitted if they are wrong.
9094          */
9095         if (!iov->iov_base)
9096                 return iov->iov_len ? -EFAULT : 0;
9097         if (!iov->iov_len)
9098                 return -EFAULT;
9099
9100         /* arbitrary limit, but we need something */
9101         if (iov->iov_len > SZ_1G)
9102                 return -EFAULT;
9103
9104         if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp))
9105                 return -EOVERFLOW;
9106
9107         return 0;
9108 }
9109
9110 static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
9111                                    unsigned int nr_args, u64 __user *tags)
9112 {
9113         struct page *last_hpage = NULL;
9114         struct io_rsrc_data *data;
9115         int i, ret;
9116         struct iovec iov;
9117
9118         if (ctx->user_bufs)
9119                 return -EBUSY;
9120         if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS)
9121                 return -EINVAL;
9122         ret = io_rsrc_node_switch_start(ctx);
9123         if (ret)
9124                 return ret;
9125         ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data);
9126         if (ret)
9127                 return ret;
9128         ret = io_buffers_map_alloc(ctx, nr_args);
9129         if (ret) {
9130                 io_rsrc_data_free(data);
9131                 return ret;
9132         }
9133
9134         for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) {
9135                 ret = io_copy_iov(ctx, &iov, arg, i);
9136                 if (ret)
9137                         break;
9138                 ret = io_buffer_validate(&iov);
9139                 if (ret)
9140                         break;
9141                 if (!iov.iov_base && *io_get_tag_slot(data, i)) {
9142                         ret = -EINVAL;
9143                         break;
9144                 }
9145
9146                 ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i],
9147                                              &last_hpage);
9148                 if (ret)
9149                         break;
9150         }
9151
9152         WARN_ON_ONCE(ctx->buf_data);
9153
9154         ctx->buf_data = data;
9155         if (ret)
9156                 __io_sqe_buffers_unregister(ctx);
9157         else
9158                 io_rsrc_node_switch(ctx, NULL);
9159         return ret;
9160 }
9161
9162 static int __io_sqe_buffers_update(struct io_ring_ctx *ctx,
9163                                    struct io_uring_rsrc_update2 *up,
9164                                    unsigned int nr_args)
9165 {
9166         u64 __user *tags = u64_to_user_ptr(up->tags);
9167         struct iovec iov, __user *iovs = u64_to_user_ptr(up->data);
9168         struct page *last_hpage = NULL;
9169         bool needs_switch = false;
9170         __u32 done;
9171         int i, err;
9172
9173         if (!ctx->buf_data)
9174                 return -ENXIO;
9175         if (up->offset + nr_args > ctx->nr_user_bufs)
9176                 return -EINVAL;
9177
9178         for (done = 0; done < nr_args; done++) {
9179                 struct io_mapped_ubuf *imu;
9180                 int offset = up->offset + done;
9181                 u64 tag = 0;
9182
9183                 err = io_copy_iov(ctx, &iov, iovs, done);
9184                 if (err)
9185                         break;
9186                 if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) {
9187                         err = -EFAULT;
9188                         break;
9189                 }
9190                 err = io_buffer_validate(&iov);
9191                 if (err)
9192                         break;
9193                 if (!iov.iov_base && tag) {
9194                         err = -EINVAL;
9195                         break;
9196                 }
9197                 err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage);
9198                 if (err)
9199                         break;
9200
9201                 i = array_index_nospec(offset, ctx->nr_user_bufs);
9202                 if (ctx->user_bufs[i] != ctx->dummy_ubuf) {
9203                         err = io_queue_rsrc_removal(ctx->buf_data, offset,
9204                                                     ctx->rsrc_node, ctx->user_bufs[i]);
9205                         if (unlikely(err)) {
9206                                 io_buffer_unmap(ctx, &imu);
9207                                 break;
9208                         }
9209                         ctx->user_bufs[i] = NULL;
9210                         needs_switch = true;
9211                 }
9212
9213                 ctx->user_bufs[i] = imu;
9214                 *io_get_tag_slot(ctx->buf_data, offset) = tag;
9215         }
9216
9217         if (needs_switch)
9218                 io_rsrc_node_switch(ctx, ctx->buf_data);
9219         return done ? done : err;
9220 }
9221
9222 static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
9223 {
9224         __s32 __user *fds = arg;
9225         int fd;
9226
9227         if (ctx->cq_ev_fd)
9228                 return -EBUSY;
9229
9230         if (copy_from_user(&fd, fds, sizeof(*fds)))
9231                 return -EFAULT;
9232
9233         ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
9234         if (IS_ERR(ctx->cq_ev_fd)) {
9235                 int ret = PTR_ERR(ctx->cq_ev_fd);
9236
9237                 ctx->cq_ev_fd = NULL;
9238                 return ret;
9239         }
9240
9241         return 0;
9242 }
9243
9244 static int io_eventfd_unregister(struct io_ring_ctx *ctx)
9245 {
9246         if (ctx->cq_ev_fd) {
9247                 eventfd_ctx_put(ctx->cq_ev_fd);
9248                 ctx->cq_ev_fd = NULL;
9249                 return 0;
9250         }
9251
9252         return -ENXIO;
9253 }
9254
9255 static void io_destroy_buffers(struct io_ring_ctx *ctx)
9256 {
9257         struct io_buffer *buf;
9258         unsigned long index;
9259
9260         xa_for_each(&ctx->io_buffers, index, buf) {
9261                 __io_remove_buffers(ctx, buf, index, -1U);
9262                 cond_resched();
9263         }
9264 }
9265
9266 static void io_req_caches_free(struct io_ring_ctx *ctx)
9267 {
9268         struct io_submit_state *state = &ctx->submit_state;
9269         int nr = 0;
9270
9271         mutex_lock(&ctx->uring_lock);
9272         io_flush_cached_locked_reqs(ctx, state);
9273
9274         while (state->free_list.next) {
9275                 struct io_wq_work_node *node;
9276                 struct io_kiocb *req;
9277
9278                 node = wq_stack_extract(&state->free_list);
9279                 req = container_of(node, struct io_kiocb, comp_list);
9280                 kmem_cache_free(req_cachep, req);
9281                 nr++;
9282         }
9283         if (nr)
9284                 percpu_ref_put_many(&ctx->refs, nr);
9285         mutex_unlock(&ctx->uring_lock);
9286 }
9287
9288 static void io_wait_rsrc_data(struct io_rsrc_data *data)
9289 {
9290         if (data && !atomic_dec_and_test(&data->refs))
9291                 wait_for_completion(&data->done);
9292 }
9293
9294 static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx)
9295 {
9296         io_sq_thread_finish(ctx);
9297
9298         if (ctx->mm_account) {
9299                 mmdrop(ctx->mm_account);
9300                 ctx->mm_account = NULL;
9301         }
9302
9303         io_rsrc_refs_drop(ctx);
9304         /* __io_rsrc_put_work() may need uring_lock to progress, wait w/o it */
9305         io_wait_rsrc_data(ctx->buf_data);
9306         io_wait_rsrc_data(ctx->file_data);
9307
9308         mutex_lock(&ctx->uring_lock);
9309         if (ctx->buf_data)
9310                 __io_sqe_buffers_unregister(ctx);
9311         if (ctx->file_data)
9312                 __io_sqe_files_unregister(ctx);
9313         if (ctx->rings)
9314                 __io_cqring_overflow_flush(ctx, true);
9315         mutex_unlock(&ctx->uring_lock);
9316         io_eventfd_unregister(ctx);
9317         io_destroy_buffers(ctx);
9318         if (ctx->sq_creds)
9319                 put_cred(ctx->sq_creds);
9320
9321         /* there are no registered resources left, nobody uses it */
9322         if (ctx->rsrc_node)
9323                 io_rsrc_node_destroy(ctx->rsrc_node);
9324         if (ctx->rsrc_backup_node)
9325                 io_rsrc_node_destroy(ctx->rsrc_backup_node);
9326         flush_delayed_work(&ctx->rsrc_put_work);
9327         flush_delayed_work(&ctx->fallback_work);
9328
9329         WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list));
9330         WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist));
9331
9332 #if defined(CONFIG_UNIX)
9333         if (ctx->ring_sock) {
9334                 ctx->ring_sock->file = NULL; /* so that iput() is called */
9335                 sock_release(ctx->ring_sock);
9336         }
9337 #endif
9338         WARN_ON_ONCE(!list_empty(&ctx->ltimeout_list));
9339
9340         io_mem_free(ctx->rings);
9341         io_mem_free(ctx->sq_sqes);
9342
9343         percpu_ref_exit(&ctx->refs);
9344         free_uid(ctx->user);
9345         io_req_caches_free(ctx);
9346         if (ctx->hash_map)
9347                 io_wq_put_hash(ctx->hash_map);
9348         kfree(ctx->cancel_hash);
9349         kfree(ctx->dummy_ubuf);
9350         kfree(ctx);
9351 }
9352
9353 static __poll_t io_uring_poll(struct file *file, poll_table *wait)
9354 {
9355         struct io_ring_ctx *ctx = file->private_data;
9356         __poll_t mask = 0;
9357
9358         poll_wait(file, &ctx->cq_wait, wait);
9359         /*
9360          * synchronizes with barrier from wq_has_sleeper call in
9361          * io_commit_cqring
9362          */
9363         smp_rmb();
9364         if (!io_sqring_full(ctx))
9365                 mask |= EPOLLOUT | EPOLLWRNORM;
9366
9367         /*
9368          * Don't flush cqring overflow list here, just do a simple check.
9369          * Otherwise there could possible be ABBA deadlock:
9370          *      CPU0                    CPU1
9371          *      ----                    ----
9372          * lock(&ctx->uring_lock);
9373          *                              lock(&ep->mtx);
9374          *                              lock(&ctx->uring_lock);
9375          * lock(&ep->mtx);
9376          *
9377          * Users may get EPOLLIN meanwhile seeing nothing in cqring, this
9378          * pushs them to do the flush.
9379          */
9380         if (io_cqring_events(ctx) || test_bit(0, &ctx->check_cq_overflow))
9381                 mask |= EPOLLIN | EPOLLRDNORM;
9382
9383         return mask;
9384 }
9385
9386 static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
9387 {
9388         const struct cred *creds;
9389
9390         creds = xa_erase(&ctx->personalities, id);
9391         if (creds) {
9392                 put_cred(creds);
9393                 return 0;
9394         }
9395
9396         return -EINVAL;
9397 }
9398
9399 struct io_tctx_exit {
9400         struct callback_head            task_work;
9401         struct completion               completion;
9402         struct io_ring_ctx              *ctx;
9403 };
9404
9405 static __cold void io_tctx_exit_cb(struct callback_head *cb)
9406 {
9407         struct io_uring_task *tctx = current->io_uring;
9408         struct io_tctx_exit *work;
9409
9410         work = container_of(cb, struct io_tctx_exit, task_work);
9411         /*
9412          * When @in_idle, we're in cancellation and it's racy to remove the
9413          * node. It'll be removed by the end of cancellation, just ignore it.
9414          */
9415         if (!atomic_read(&tctx->in_idle))
9416                 io_uring_del_tctx_node((unsigned long)work->ctx);
9417         complete(&work->completion);
9418 }
9419
9420 static __cold bool io_cancel_ctx_cb(struct io_wq_work *work, void *data)
9421 {
9422         struct io_kiocb *req = container_of(work, struct io_kiocb, work);
9423
9424         return req->ctx == data;
9425 }
9426
9427 static __cold void io_ring_exit_work(struct work_struct *work)
9428 {
9429         struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work);
9430         unsigned long timeout = jiffies + HZ * 60 * 5;
9431         unsigned long interval = HZ / 20;
9432         struct io_tctx_exit exit;
9433         struct io_tctx_node *node;
9434         int ret;
9435
9436         /*
9437          * If we're doing polled IO and end up having requests being
9438          * submitted async (out-of-line), then completions can come in while
9439          * we're waiting for refs to drop. We need to reap these manually,
9440          * as nobody else will be looking for them.
9441          */
9442         do {
9443                 io_uring_try_cancel_requests(ctx, NULL, true);
9444                 if (ctx->sq_data) {
9445                         struct io_sq_data *sqd = ctx->sq_data;
9446                         struct task_struct *tsk;
9447
9448                         io_sq_thread_park(sqd);
9449                         tsk = sqd->thread;
9450                         if (tsk && tsk->io_uring && tsk->io_uring->io_wq)
9451                                 io_wq_cancel_cb(tsk->io_uring->io_wq,
9452                                                 io_cancel_ctx_cb, ctx, true);
9453                         io_sq_thread_unpark(sqd);
9454                 }
9455
9456                 io_req_caches_free(ctx);
9457
9458                 if (WARN_ON_ONCE(time_after(jiffies, timeout))) {
9459                         /* there is little hope left, don't run it too often */
9460                         interval = HZ * 60;
9461                 }
9462         } while (!wait_for_completion_timeout(&ctx->ref_comp, interval));
9463
9464         init_completion(&exit.completion);
9465         init_task_work(&exit.task_work, io_tctx_exit_cb);
9466         exit.ctx = ctx;
9467         /*
9468          * Some may use context even when all refs and requests have been put,
9469          * and they are free to do so while still holding uring_lock or
9470          * completion_lock, see io_req_task_submit(). Apart from other work,
9471          * this lock/unlock section also waits them to finish.
9472          */
9473         mutex_lock(&ctx->uring_lock);
9474         while (!list_empty(&ctx->tctx_list)) {
9475                 WARN_ON_ONCE(time_after(jiffies, timeout));
9476
9477                 node = list_first_entry(&ctx->tctx_list, struct io_tctx_node,
9478                                         ctx_node);
9479                 /* don't spin on a single task if cancellation failed */
9480                 list_rotate_left(&ctx->tctx_list);
9481                 ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL);
9482                 if (WARN_ON_ONCE(ret))
9483                         continue;
9484
9485                 mutex_unlock(&ctx->uring_lock);
9486                 wait_for_completion(&exit.completion);
9487                 mutex_lock(&ctx->uring_lock);
9488         }
9489         mutex_unlock(&ctx->uring_lock);
9490         spin_lock(&ctx->completion_lock);
9491         spin_unlock(&ctx->completion_lock);
9492
9493         io_ring_ctx_free(ctx);
9494 }
9495
9496 /* Returns true if we found and killed one or more timeouts */
9497 static __cold bool io_kill_timeouts(struct io_ring_ctx *ctx,
9498                                     struct task_struct *tsk, bool cancel_all)
9499 {
9500         struct io_kiocb *req, *tmp;
9501         int canceled = 0;
9502
9503         spin_lock(&ctx->completion_lock);
9504         spin_lock_irq(&ctx->timeout_lock);
9505         list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
9506                 if (io_match_task(req, tsk, cancel_all)) {
9507                         io_kill_timeout(req, -ECANCELED);
9508                         canceled++;
9509                 }
9510         }
9511         spin_unlock_irq(&ctx->timeout_lock);
9512         if (canceled != 0)
9513                 io_commit_cqring(ctx);
9514         spin_unlock(&ctx->completion_lock);
9515         if (canceled != 0)
9516                 io_cqring_ev_posted(ctx);
9517         return canceled != 0;
9518 }
9519
9520 static __cold void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
9521 {
9522         unsigned long index;
9523         struct creds *creds;
9524
9525         mutex_lock(&ctx->uring_lock);
9526         percpu_ref_kill(&ctx->refs);
9527         if (ctx->rings)
9528                 __io_cqring_overflow_flush(ctx, true);
9529         xa_for_each(&ctx->personalities, index, creds)
9530                 io_unregister_personality(ctx, index);
9531         mutex_unlock(&ctx->uring_lock);
9532
9533         io_kill_timeouts(ctx, NULL, true);
9534         io_poll_remove_all(ctx, NULL, true);
9535
9536         /* if we failed setting up the ctx, we might not have any rings */
9537         io_iopoll_try_reap_events(ctx);
9538
9539         INIT_WORK(&ctx->exit_work, io_ring_exit_work);
9540         /*
9541          * Use system_unbound_wq to avoid spawning tons of event kworkers
9542          * if we're exiting a ton of rings at the same time. It just adds
9543          * noise and overhead, there's no discernable change in runtime
9544          * over using system_wq.
9545          */
9546         queue_work(system_unbound_wq, &ctx->exit_work);
9547 }
9548
9549 static int io_uring_release(struct inode *inode, struct file *file)
9550 {
9551         struct io_ring_ctx *ctx = file->private_data;
9552
9553         file->private_data = NULL;
9554         io_ring_ctx_wait_and_kill(ctx);
9555         return 0;
9556 }
9557
9558 struct io_task_cancel {
9559         struct task_struct *task;
9560         bool all;
9561 };
9562
9563 static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
9564 {
9565         struct io_kiocb *req = container_of(work, struct io_kiocb, work);
9566         struct io_task_cancel *cancel = data;
9567         bool ret;
9568
9569         if (!cancel->all && (req->flags & REQ_F_LINK_TIMEOUT)) {
9570                 struct io_ring_ctx *ctx = req->ctx;
9571
9572                 /* protect against races with linked timeouts */
9573                 spin_lock_irq(&ctx->timeout_lock);
9574                 ret = io_match_task(req, cancel->task, cancel->all);
9575                 spin_unlock_irq(&ctx->timeout_lock);
9576         } else {
9577                 ret = io_match_task(req, cancel->task, cancel->all);
9578         }
9579         return ret;
9580 }
9581
9582 static __cold bool io_cancel_defer_files(struct io_ring_ctx *ctx,
9583                                          struct task_struct *task,
9584                                          bool cancel_all)
9585 {
9586         struct io_defer_entry *de;
9587         LIST_HEAD(list);
9588
9589         spin_lock(&ctx->completion_lock);
9590         spin_lock_irq(&ctx->timeout_lock);
9591         list_for_each_entry_reverse(de, &ctx->defer_list, list) {
9592                 if (io_match_task(de->req, task, cancel_all)) {
9593                         list_cut_position(&list, &ctx->defer_list, &de->list);
9594                         break;
9595                 }
9596         }
9597         spin_unlock_irq(&ctx->timeout_lock);
9598         spin_unlock(&ctx->completion_lock);
9599         if (list_empty(&list))
9600                 return false;
9601
9602         while (!list_empty(&list)) {
9603                 de = list_first_entry(&list, struct io_defer_entry, list);
9604                 list_del_init(&de->list);
9605                 io_req_complete_failed(de->req, -ECANCELED);
9606                 kfree(de);
9607         }
9608         return true;
9609 }
9610
9611 static __cold bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx)
9612 {
9613         struct io_tctx_node *node;
9614         enum io_wq_cancel cret;
9615         bool ret = false;
9616
9617         mutex_lock(&ctx->uring_lock);
9618         list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
9619                 struct io_uring_task *tctx = node->task->io_uring;
9620
9621                 /*
9622                  * io_wq will stay alive while we hold uring_lock, because it's
9623                  * killed after ctx nodes, which requires to take the lock.
9624                  */
9625                 if (!tctx || !tctx->io_wq)
9626                         continue;
9627                 cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true);
9628                 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
9629         }
9630         mutex_unlock(&ctx->uring_lock);
9631
9632         return ret;
9633 }
9634
9635 static __cold void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
9636                                                 struct task_struct *task,
9637                                                 bool cancel_all)
9638 {
9639         struct io_task_cancel cancel = { .task = task, .all = cancel_all, };
9640         struct io_uring_task *tctx = task ? task->io_uring : NULL;
9641
9642         while (1) {
9643                 enum io_wq_cancel cret;
9644                 bool ret = false;
9645
9646                 if (!task) {
9647                         ret |= io_uring_try_cancel_iowq(ctx);
9648                 } else if (tctx && tctx->io_wq) {
9649                         /*
9650                          * Cancels requests of all rings, not only @ctx, but
9651                          * it's fine as the task is in exit/exec.
9652                          */
9653                         cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb,
9654                                                &cancel, true);
9655                         ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
9656                 }
9657
9658                 /* SQPOLL thread does its own polling */
9659                 if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) ||
9660                     (ctx->sq_data && ctx->sq_data->thread == current)) {
9661                         while (!wq_list_empty(&ctx->iopoll_list)) {
9662                                 io_iopoll_try_reap_events(ctx);
9663                                 ret = true;
9664                         }
9665                 }
9666
9667                 ret |= io_cancel_defer_files(ctx, task, cancel_all);
9668                 ret |= io_poll_remove_all(ctx, task, cancel_all);
9669                 ret |= io_kill_timeouts(ctx, task, cancel_all);
9670                 if (task)
9671                         ret |= io_run_task_work();
9672                 if (!ret)
9673                         break;
9674                 cond_resched();
9675         }
9676 }
9677
9678 static int __io_uring_add_tctx_node(struct io_ring_ctx *ctx)
9679 {
9680         struct io_uring_task *tctx = current->io_uring;
9681         struct io_tctx_node *node;
9682         int ret;
9683
9684         if (unlikely(!tctx)) {
9685                 ret = io_uring_alloc_task_context(current, ctx);
9686                 if (unlikely(ret))
9687                         return ret;
9688
9689                 tctx = current->io_uring;
9690                 if (ctx->iowq_limits_set) {
9691                         unsigned int limits[2] = { ctx->iowq_limits[0],
9692                                                    ctx->iowq_limits[1], };
9693
9694                         ret = io_wq_max_workers(tctx->io_wq, limits);
9695                         if (ret)
9696                                 return ret;
9697                 }
9698         }
9699         if (!xa_load(&tctx->xa, (unsigned long)ctx)) {
9700                 node = kmalloc(sizeof(*node), GFP_KERNEL);
9701                 if (!node)
9702                         return -ENOMEM;
9703                 node->ctx = ctx;
9704                 node->task = current;
9705
9706                 ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx,
9707                                         node, GFP_KERNEL));
9708                 if (ret) {
9709                         kfree(node);
9710                         return ret;
9711                 }
9712
9713                 mutex_lock(&ctx->uring_lock);
9714                 list_add(&node->ctx_node, &ctx->tctx_list);
9715                 mutex_unlock(&ctx->uring_lock);
9716         }
9717         tctx->last = ctx;
9718         return 0;
9719 }
9720
9721 /*
9722  * Note that this task has used io_uring. We use it for cancelation purposes.
9723  */
9724 static inline int io_uring_add_tctx_node(struct io_ring_ctx *ctx)
9725 {
9726         struct io_uring_task *tctx = current->io_uring;
9727
9728         if (likely(tctx && tctx->last == ctx))
9729                 return 0;
9730         return __io_uring_add_tctx_node(ctx);
9731 }
9732
9733 /*
9734  * Remove this io_uring_file -> task mapping.
9735  */
9736 static __cold void io_uring_del_tctx_node(unsigned long index)
9737 {
9738         struct io_uring_task *tctx = current->io_uring;
9739         struct io_tctx_node *node;
9740
9741         if (!tctx)
9742                 return;
9743         node = xa_erase(&tctx->xa, index);
9744         if (!node)
9745                 return;
9746
9747         WARN_ON_ONCE(current != node->task);
9748         WARN_ON_ONCE(list_empty(&node->ctx_node));
9749
9750         mutex_lock(&node->ctx->uring_lock);
9751         list_del(&node->ctx_node);
9752         mutex_unlock(&node->ctx->uring_lock);
9753
9754         if (tctx->last == node->ctx)
9755                 tctx->last = NULL;
9756         kfree(node);
9757 }
9758
9759 static __cold void io_uring_clean_tctx(struct io_uring_task *tctx)
9760 {
9761         struct io_wq *wq = tctx->io_wq;
9762         struct io_tctx_node *node;
9763         unsigned long index;
9764
9765         xa_for_each(&tctx->xa, index, node) {
9766                 io_uring_del_tctx_node(index);
9767                 cond_resched();
9768         }
9769         if (wq) {
9770                 /*
9771                  * Must be after io_uring_del_tctx_node() (removes nodes under
9772                  * uring_lock) to avoid race with io_uring_try_cancel_iowq().
9773                  */
9774                 io_wq_put_and_exit(wq);
9775                 tctx->io_wq = NULL;
9776         }
9777 }
9778
9779 static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked)
9780 {
9781         if (tracked)
9782                 return atomic_read(&tctx->inflight_tracked);
9783         return percpu_counter_sum(&tctx->inflight);
9784 }
9785
9786 static __cold void io_uring_drop_tctx_refs(struct task_struct *task)
9787 {
9788         struct io_uring_task *tctx = task->io_uring;
9789         unsigned int refs = tctx->cached_refs;
9790
9791         if (refs) {
9792                 tctx->cached_refs = 0;
9793                 percpu_counter_sub(&tctx->inflight, refs);
9794                 put_task_struct_many(task, refs);
9795         }
9796 }
9797
9798 /*
9799  * Find any io_uring ctx that this task has registered or done IO on, and cancel
9800  * requests. @sqd should be not-null IIF it's an SQPOLL thread cancellation.
9801  */
9802 static __cold void io_uring_cancel_generic(bool cancel_all,
9803                                            struct io_sq_data *sqd)
9804 {
9805         struct io_uring_task *tctx = current->io_uring;
9806         struct io_ring_ctx *ctx;
9807         s64 inflight;
9808         DEFINE_WAIT(wait);
9809
9810         WARN_ON_ONCE(sqd && sqd->thread != current);
9811
9812         if (!current->io_uring)
9813                 return;
9814         if (tctx->io_wq)
9815                 io_wq_exit_start(tctx->io_wq);
9816
9817         atomic_inc(&tctx->in_idle);
9818         do {
9819                 io_uring_drop_tctx_refs(current);
9820                 /* read completions before cancelations */
9821                 inflight = tctx_inflight(tctx, !cancel_all);
9822                 if (!inflight)
9823                         break;
9824
9825                 if (!sqd) {
9826                         struct io_tctx_node *node;
9827                         unsigned long index;
9828
9829                         xa_for_each(&tctx->xa, index, node) {
9830                                 /* sqpoll task will cancel all its requests */
9831                                 if (node->ctx->sq_data)
9832                                         continue;
9833                                 io_uring_try_cancel_requests(node->ctx, current,
9834                                                              cancel_all);
9835                         }
9836                 } else {
9837                         list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
9838                                 io_uring_try_cancel_requests(ctx, current,
9839                                                              cancel_all);
9840                 }
9841
9842                 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
9843                 io_uring_drop_tctx_refs(current);
9844                 /*
9845                  * If we've seen completions, retry without waiting. This
9846                  * avoids a race where a completion comes in before we did
9847                  * prepare_to_wait().
9848                  */
9849                 if (inflight == tctx_inflight(tctx, !cancel_all))
9850                         schedule();
9851                 finish_wait(&tctx->wait, &wait);
9852         } while (1);
9853         atomic_dec(&tctx->in_idle);
9854
9855         io_uring_clean_tctx(tctx);
9856         if (cancel_all) {
9857                 /* for exec all current's requests should be gone, kill tctx */
9858                 __io_uring_free(current);
9859         }
9860 }
9861
9862 void __io_uring_cancel(bool cancel_all)
9863 {
9864         io_uring_cancel_generic(cancel_all, NULL);
9865 }
9866
9867 static void *io_uring_validate_mmap_request(struct file *file,
9868                                             loff_t pgoff, size_t sz)
9869 {
9870         struct io_ring_ctx *ctx = file->private_data;
9871         loff_t offset = pgoff << PAGE_SHIFT;
9872         struct page *page;
9873         void *ptr;
9874
9875         switch (offset) {
9876         case IORING_OFF_SQ_RING:
9877         case IORING_OFF_CQ_RING:
9878                 ptr = ctx->rings;
9879                 break;
9880         case IORING_OFF_SQES:
9881                 ptr = ctx->sq_sqes;
9882                 break;
9883         default:
9884                 return ERR_PTR(-EINVAL);
9885         }
9886
9887         page = virt_to_head_page(ptr);
9888         if (sz > page_size(page))
9889                 return ERR_PTR(-EINVAL);
9890
9891         return ptr;
9892 }
9893
9894 #ifdef CONFIG_MMU
9895
9896 static __cold int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9897 {
9898         size_t sz = vma->vm_end - vma->vm_start;
9899         unsigned long pfn;
9900         void *ptr;
9901
9902         ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
9903         if (IS_ERR(ptr))
9904                 return PTR_ERR(ptr);
9905
9906         pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
9907         return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
9908 }
9909
9910 #else /* !CONFIG_MMU */
9911
9912 static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9913 {
9914         return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
9915 }
9916
9917 static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
9918 {
9919         return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
9920 }
9921
9922 static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
9923         unsigned long addr, unsigned long len,
9924         unsigned long pgoff, unsigned long flags)
9925 {
9926         void *ptr;
9927
9928         ptr = io_uring_validate_mmap_request(file, pgoff, len);
9929         if (IS_ERR(ptr))
9930                 return PTR_ERR(ptr);
9931
9932         return (unsigned long) ptr;
9933 }
9934
9935 #endif /* !CONFIG_MMU */
9936
9937 static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
9938 {
9939         DEFINE_WAIT(wait);
9940
9941         do {
9942                 if (!io_sqring_full(ctx))
9943                         break;
9944                 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
9945
9946                 if (!io_sqring_full(ctx))
9947                         break;
9948                 schedule();
9949         } while (!signal_pending(current));
9950
9951         finish_wait(&ctx->sqo_sq_wait, &wait);
9952         return 0;
9953 }
9954
9955 static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz,
9956                           struct __kernel_timespec __user **ts,
9957                           const sigset_t __user **sig)
9958 {
9959         struct io_uring_getevents_arg arg;
9960
9961         /*
9962          * If EXT_ARG isn't set, then we have no timespec and the argp pointer
9963          * is just a pointer to the sigset_t.
9964          */
9965         if (!(flags & IORING_ENTER_EXT_ARG)) {
9966                 *sig = (const sigset_t __user *) argp;
9967                 *ts = NULL;
9968                 return 0;
9969         }
9970
9971         /*
9972          * EXT_ARG is set - ensure we agree on the size of it and copy in our
9973          * timespec and sigset_t pointers if good.
9974          */
9975         if (*argsz != sizeof(arg))
9976                 return -EINVAL;
9977         if (copy_from_user(&arg, argp, sizeof(arg)))
9978                 return -EFAULT;
9979         *sig = u64_to_user_ptr(arg.sigmask);
9980         *argsz = arg.sigmask_sz;
9981         *ts = u64_to_user_ptr(arg.ts);
9982         return 0;
9983 }
9984
9985 SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
9986                 u32, min_complete, u32, flags, const void __user *, argp,
9987                 size_t, argsz)
9988 {
9989         struct io_ring_ctx *ctx;
9990         int submitted = 0;
9991         struct fd f;
9992         long ret;
9993
9994         io_run_task_work();
9995
9996         if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
9997                                IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG)))
9998                 return -EINVAL;
9999
10000         f = fdget(fd);
10001         if (unlikely(!f.file))
10002                 return -EBADF;
10003
10004         ret = -EOPNOTSUPP;
10005         if (unlikely(f.file->f_op != &io_uring_fops))
10006                 goto out_fput;
10007
10008         ret = -ENXIO;
10009         ctx = f.file->private_data;
10010         if (unlikely(!percpu_ref_tryget(&ctx->refs)))
10011                 goto out_fput;
10012
10013         ret = -EBADFD;
10014         if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED))
10015                 goto out;
10016
10017         /*
10018          * For SQ polling, the thread will do all submissions and completions.
10019          * Just return the requested submit count, and wake the thread if
10020          * we were asked to.
10021          */
10022         ret = 0;
10023         if (ctx->flags & IORING_SETUP_SQPOLL) {
10024                 io_cqring_overflow_flush(ctx);
10025
10026                 if (unlikely(ctx->sq_data->thread == NULL)) {
10027                         ret = -EOWNERDEAD;
10028                         goto out;
10029                 }
10030                 if (flags & IORING_ENTER_SQ_WAKEUP)
10031                         wake_up(&ctx->sq_data->wait);
10032                 if (flags & IORING_ENTER_SQ_WAIT) {
10033                         ret = io_sqpoll_wait_sq(ctx);
10034                         if (ret)
10035                                 goto out;
10036                 }
10037                 submitted = to_submit;
10038         } else if (to_submit) {
10039                 ret = io_uring_add_tctx_node(ctx);
10040                 if (unlikely(ret))
10041                         goto out;
10042                 mutex_lock(&ctx->uring_lock);
10043                 submitted = io_submit_sqes(ctx, to_submit);
10044                 mutex_unlock(&ctx->uring_lock);
10045
10046                 if (submitted != to_submit)
10047                         goto out;
10048         }
10049         if (flags & IORING_ENTER_GETEVENTS) {
10050                 const sigset_t __user *sig;
10051                 struct __kernel_timespec __user *ts;
10052
10053                 ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig);
10054                 if (unlikely(ret))
10055                         goto out;
10056
10057                 min_complete = min(min_complete, ctx->cq_entries);
10058
10059                 /*
10060                  * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
10061                  * space applications don't need to do io completion events
10062                  * polling again, they can rely on io_sq_thread to do polling
10063                  * work, which can reduce cpu usage and uring_lock contention.
10064                  */
10065                 if (ctx->flags & IORING_SETUP_IOPOLL &&
10066                     !(ctx->flags & IORING_SETUP_SQPOLL)) {
10067                         ret = io_iopoll_check(ctx, min_complete);
10068                 } else {
10069                         ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts);
10070                 }
10071         }
10072
10073 out:
10074         percpu_ref_put(&ctx->refs);
10075 out_fput:
10076         fdput(f);
10077         return submitted ? submitted : ret;
10078 }
10079
10080 #ifdef CONFIG_PROC_FS
10081 static __cold int io_uring_show_cred(struct seq_file *m, unsigned int id,
10082                 const struct cred *cred)
10083 {
10084         struct user_namespace *uns = seq_user_ns(m);
10085         struct group_info *gi;
10086         kernel_cap_t cap;
10087         unsigned __capi;
10088         int g;
10089
10090         seq_printf(m, "%5d\n", id);
10091         seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
10092         seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
10093         seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
10094         seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
10095         seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
10096         seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
10097         seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
10098         seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
10099         seq_puts(m, "\n\tGroups:\t");
10100         gi = cred->group_info;
10101         for (g = 0; g < gi->ngroups; g++) {
10102                 seq_put_decimal_ull(m, g ? " " : "",
10103                                         from_kgid_munged(uns, gi->gid[g]));
10104         }
10105         seq_puts(m, "\n\tCapEff:\t");
10106         cap = cred->cap_effective;
10107         CAP_FOR_EACH_U32(__capi)
10108                 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
10109         seq_putc(m, '\n');
10110         return 0;
10111 }
10112
10113 static __cold void __io_uring_show_fdinfo(struct io_ring_ctx *ctx,
10114                                           struct seq_file *m)
10115 {
10116         struct io_sq_data *sq = NULL;
10117         struct io_overflow_cqe *ocqe;
10118         struct io_rings *r = ctx->rings;
10119         unsigned int sq_mask = ctx->sq_entries - 1, cq_mask = ctx->cq_entries - 1;
10120         unsigned int sq_head = READ_ONCE(r->sq.head);
10121         unsigned int sq_tail = READ_ONCE(r->sq.tail);
10122         unsigned int cq_head = READ_ONCE(r->cq.head);
10123         unsigned int cq_tail = READ_ONCE(r->cq.tail);
10124         unsigned int sq_entries, cq_entries;
10125         bool has_lock;
10126         unsigned int i;
10127
10128         /*
10129          * we may get imprecise sqe and cqe info if uring is actively running
10130          * since we get cached_sq_head and cached_cq_tail without uring_lock
10131          * and sq_tail and cq_head are changed by userspace. But it's ok since
10132          * we usually use these info when it is stuck.
10133          */
10134         seq_printf(m, "SqMask:\t\t0x%x\n", sq_mask);
10135         seq_printf(m, "SqHead:\t%u\n", sq_head);
10136         seq_printf(m, "SqTail:\t%u\n", sq_tail);
10137         seq_printf(m, "CachedSqHead:\t%u\n", ctx->cached_sq_head);
10138         seq_printf(m, "CqMask:\t0x%x\n", cq_mask);
10139         seq_printf(m, "CqHead:\t%u\n", cq_head);
10140         seq_printf(m, "CqTail:\t%u\n", cq_tail);
10141         seq_printf(m, "CachedCqTail:\t%u\n", ctx->cached_cq_tail);
10142         seq_printf(m, "SQEs:\t%u\n", sq_tail - ctx->cached_sq_head);
10143         sq_entries = min(sq_tail - sq_head, ctx->sq_entries);
10144         for (i = 0; i < sq_entries; i++) {
10145                 unsigned int entry = i + sq_head;
10146                 unsigned int sq_idx = READ_ONCE(ctx->sq_array[entry & sq_mask]);
10147                 struct io_uring_sqe *sqe;
10148
10149                 if (sq_idx > sq_mask)
10150                         continue;
10151                 sqe = &ctx->sq_sqes[sq_idx];
10152                 seq_printf(m, "%5u: opcode:%d, fd:%d, flags:%x, user_data:%llu\n",
10153                            sq_idx, sqe->opcode, sqe->fd, sqe->flags,
10154                            sqe->user_data);
10155         }
10156         seq_printf(m, "CQEs:\t%u\n", cq_tail - cq_head);
10157         cq_entries = min(cq_tail - cq_head, ctx->cq_entries);
10158         for (i = 0; i < cq_entries; i++) {
10159                 unsigned int entry = i + cq_head;
10160                 struct io_uring_cqe *cqe = &r->cqes[entry & cq_mask];
10161
10162                 seq_printf(m, "%5u: user_data:%llu, res:%d, flag:%x\n",
10163                            entry & cq_mask, cqe->user_data, cqe->res,
10164                            cqe->flags);
10165         }
10166
10167         /*
10168          * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
10169          * since fdinfo case grabs it in the opposite direction of normal use
10170          * cases. If we fail to get the lock, we just don't iterate any
10171          * structures that could be going away outside the io_uring mutex.
10172          */
10173         has_lock = mutex_trylock(&ctx->uring_lock);
10174
10175         if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) {
10176                 sq = ctx->sq_data;
10177                 if (!sq->thread)
10178                         sq = NULL;
10179         }
10180
10181         seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
10182         seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
10183         seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
10184         for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
10185                 struct file *f = io_file_from_index(ctx, i);
10186
10187                 if (f)
10188                         seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
10189                 else
10190                         seq_printf(m, "%5u: <none>\n", i);
10191         }
10192         seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
10193         for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
10194                 struct io_mapped_ubuf *buf = ctx->user_bufs[i];
10195                 unsigned int len = buf->ubuf_end - buf->ubuf;
10196
10197                 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len);
10198         }
10199         if (has_lock && !xa_empty(&ctx->personalities)) {
10200                 unsigned long index;
10201                 const struct cred *cred;
10202
10203                 seq_printf(m, "Personalities:\n");
10204                 xa_for_each(&ctx->personalities, index, cred)
10205                         io_uring_show_cred(m, index, cred);
10206         }
10207         if (has_lock)
10208                 mutex_unlock(&ctx->uring_lock);
10209
10210         seq_puts(m, "PollList:\n");
10211         spin_lock(&ctx->completion_lock);
10212         for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
10213                 struct hlist_head *list = &ctx->cancel_hash[i];
10214                 struct io_kiocb *req;
10215
10216                 hlist_for_each_entry(req, list, hash_node)
10217                         seq_printf(m, "  op=%d, task_works=%d\n", req->opcode,
10218                                         req->task->task_works != NULL);
10219         }
10220
10221         seq_puts(m, "CqOverflowList:\n");
10222         list_for_each_entry(ocqe, &ctx->cq_overflow_list, list) {
10223                 struct io_uring_cqe *cqe = &ocqe->cqe;
10224
10225                 seq_printf(m, "  user_data=%llu, res=%d, flags=%x\n",
10226                            cqe->user_data, cqe->res, cqe->flags);
10227
10228         }
10229
10230         spin_unlock(&ctx->completion_lock);
10231 }
10232
10233 static __cold void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
10234 {
10235         struct io_ring_ctx *ctx = f->private_data;
10236
10237         if (percpu_ref_tryget(&ctx->refs)) {
10238                 __io_uring_show_fdinfo(ctx, m);
10239                 percpu_ref_put(&ctx->refs);
10240         }
10241 }
10242 #endif
10243
10244 static const struct file_operations io_uring_fops = {
10245         .release        = io_uring_release,
10246         .mmap           = io_uring_mmap,
10247 #ifndef CONFIG_MMU
10248         .get_unmapped_area = io_uring_nommu_get_unmapped_area,
10249         .mmap_capabilities = io_uring_nommu_mmap_capabilities,
10250 #endif
10251         .poll           = io_uring_poll,
10252 #ifdef CONFIG_PROC_FS
10253         .show_fdinfo    = io_uring_show_fdinfo,
10254 #endif
10255 };
10256
10257 static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx,
10258                                          struct io_uring_params *p)
10259 {
10260         struct io_rings *rings;
10261         size_t size, sq_array_offset;
10262
10263         /* make sure these are sane, as we already accounted them */
10264         ctx->sq_entries = p->sq_entries;
10265         ctx->cq_entries = p->cq_entries;
10266
10267         size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
10268         if (size == SIZE_MAX)
10269                 return -EOVERFLOW;
10270
10271         rings = io_mem_alloc(size);
10272         if (!rings)
10273                 return -ENOMEM;
10274
10275         ctx->rings = rings;
10276         ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
10277         rings->sq_ring_mask = p->sq_entries - 1;
10278         rings->cq_ring_mask = p->cq_entries - 1;
10279         rings->sq_ring_entries = p->sq_entries;
10280         rings->cq_ring_entries = p->cq_entries;
10281
10282         size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
10283         if (size == SIZE_MAX) {
10284                 io_mem_free(ctx->rings);
10285                 ctx->rings = NULL;
10286                 return -EOVERFLOW;
10287         }
10288
10289         ctx->sq_sqes = io_mem_alloc(size);
10290         if (!ctx->sq_sqes) {
10291                 io_mem_free(ctx->rings);
10292                 ctx->rings = NULL;
10293                 return -ENOMEM;
10294         }
10295
10296         return 0;
10297 }
10298
10299 static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file)
10300 {
10301         int ret, fd;
10302
10303         fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
10304         if (fd < 0)
10305                 return fd;
10306
10307         ret = io_uring_add_tctx_node(ctx);
10308         if (ret) {
10309                 put_unused_fd(fd);
10310                 return ret;
10311         }
10312         fd_install(fd, file);
10313         return fd;
10314 }
10315
10316 /*
10317  * Allocate an anonymous fd, this is what constitutes the application
10318  * visible backing of an io_uring instance. The application mmaps this
10319  * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
10320  * we have to tie this fd to a socket for file garbage collection purposes.
10321  */
10322 static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
10323 {
10324         struct file *file;
10325 #if defined(CONFIG_UNIX)
10326         int ret;
10327
10328         ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
10329                                 &ctx->ring_sock);
10330         if (ret)
10331                 return ERR_PTR(ret);
10332 #endif
10333
10334         file = anon_inode_getfile_secure("[io_uring]", &io_uring_fops, ctx,
10335                                          O_RDWR | O_CLOEXEC, NULL);
10336 #if defined(CONFIG_UNIX)
10337         if (IS_ERR(file)) {
10338                 sock_release(ctx->ring_sock);
10339                 ctx->ring_sock = NULL;
10340         } else {
10341                 ctx->ring_sock->file = file;
10342         }
10343 #endif
10344         return file;
10345 }
10346
10347 static __cold int io_uring_create(unsigned entries, struct io_uring_params *p,
10348                                   struct io_uring_params __user *params)
10349 {
10350         struct io_ring_ctx *ctx;
10351         struct file *file;
10352         int ret;
10353
10354         if (!entries)
10355                 return -EINVAL;
10356         if (entries > IORING_MAX_ENTRIES) {
10357                 if (!(p->flags & IORING_SETUP_CLAMP))
10358                         return -EINVAL;
10359                 entries = IORING_MAX_ENTRIES;
10360         }
10361
10362         /*
10363          * Use twice as many entries for the CQ ring. It's possible for the
10364          * application to drive a higher depth than the size of the SQ ring,
10365          * since the sqes are only used at submission time. This allows for
10366          * some flexibility in overcommitting a bit. If the application has
10367          * set IORING_SETUP_CQSIZE, it will have passed in the desired number
10368          * of CQ ring entries manually.
10369          */
10370         p->sq_entries = roundup_pow_of_two(entries);
10371         if (p->flags & IORING_SETUP_CQSIZE) {
10372                 /*
10373                  * If IORING_SETUP_CQSIZE is set, we do the same roundup
10374                  * to a power-of-two, if it isn't already. We do NOT impose
10375                  * any cq vs sq ring sizing.
10376                  */
10377                 if (!p->cq_entries)
10378                         return -EINVAL;
10379                 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
10380                         if (!(p->flags & IORING_SETUP_CLAMP))
10381                                 return -EINVAL;
10382                         p->cq_entries = IORING_MAX_CQ_ENTRIES;
10383                 }
10384                 p->cq_entries = roundup_pow_of_two(p->cq_entries);
10385                 if (p->cq_entries < p->sq_entries)
10386                         return -EINVAL;
10387         } else {
10388                 p->cq_entries = 2 * p->sq_entries;
10389         }
10390
10391         ctx = io_ring_ctx_alloc(p);
10392         if (!ctx)
10393                 return -ENOMEM;
10394         ctx->compat = in_compat_syscall();
10395         if (!capable(CAP_IPC_LOCK))
10396                 ctx->user = get_uid(current_user());
10397
10398         /*
10399          * This is just grabbed for accounting purposes. When a process exits,
10400          * the mm is exited and dropped before the files, hence we need to hang
10401          * on to this mm purely for the purposes of being able to unaccount
10402          * memory (locked/pinned vm). It's not used for anything else.
10403          */
10404         mmgrab(current->mm);
10405         ctx->mm_account = current->mm;
10406
10407         ret = io_allocate_scq_urings(ctx, p);
10408         if (ret)
10409                 goto err;
10410
10411         ret = io_sq_offload_create(ctx, p);
10412         if (ret)
10413                 goto err;
10414         /* always set a rsrc node */
10415         ret = io_rsrc_node_switch_start(ctx);
10416         if (ret)
10417                 goto err;
10418         io_rsrc_node_switch(ctx, NULL);
10419
10420         memset(&p->sq_off, 0, sizeof(p->sq_off));
10421         p->sq_off.head = offsetof(struct io_rings, sq.head);
10422         p->sq_off.tail = offsetof(struct io_rings, sq.tail);
10423         p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
10424         p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
10425         p->sq_off.flags = offsetof(struct io_rings, sq_flags);
10426         p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
10427         p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
10428
10429         memset(&p->cq_off, 0, sizeof(p->cq_off));
10430         p->cq_off.head = offsetof(struct io_rings, cq.head);
10431         p->cq_off.tail = offsetof(struct io_rings, cq.tail);
10432         p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
10433         p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
10434         p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
10435         p->cq_off.cqes = offsetof(struct io_rings, cqes);
10436         p->cq_off.flags = offsetof(struct io_rings, cq_flags);
10437
10438         p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
10439                         IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
10440                         IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
10441                         IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED |
10442                         IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS |
10443                         IORING_FEAT_RSRC_TAGS;
10444
10445         if (copy_to_user(params, p, sizeof(*p))) {
10446                 ret = -EFAULT;
10447                 goto err;
10448         }
10449
10450         file = io_uring_get_file(ctx);
10451         if (IS_ERR(file)) {
10452                 ret = PTR_ERR(file);
10453                 goto err;
10454         }
10455
10456         /*
10457          * Install ring fd as the very last thing, so we don't risk someone
10458          * having closed it before we finish setup
10459          */
10460         ret = io_uring_install_fd(ctx, file);
10461         if (ret < 0) {
10462                 /* fput will clean it up */
10463                 fput(file);
10464                 return ret;
10465         }
10466
10467         trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
10468         return ret;
10469 err:
10470         io_ring_ctx_wait_and_kill(ctx);
10471         return ret;
10472 }
10473
10474 /*
10475  * Sets up an aio uring context, and returns the fd. Applications asks for a
10476  * ring size, we return the actual sq/cq ring sizes (among other things) in the
10477  * params structure passed in.
10478  */
10479 static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
10480 {
10481         struct io_uring_params p;
10482         int i;
10483
10484         if (copy_from_user(&p, params, sizeof(p)))
10485                 return -EFAULT;
10486         for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
10487                 if (p.resv[i])
10488                         return -EINVAL;
10489         }
10490
10491         if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
10492                         IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
10493                         IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
10494                         IORING_SETUP_R_DISABLED))
10495                 return -EINVAL;
10496
10497         return  io_uring_create(entries, &p, params);
10498 }
10499
10500 SYSCALL_DEFINE2(io_uring_setup, u32, entries,
10501                 struct io_uring_params __user *, params)
10502 {
10503         return io_uring_setup(entries, params);
10504 }
10505
10506 static __cold int io_probe(struct io_ring_ctx *ctx, void __user *arg,
10507                            unsigned nr_args)
10508 {
10509         struct io_uring_probe *p;
10510         size_t size;
10511         int i, ret;
10512
10513         size = struct_size(p, ops, nr_args);
10514         if (size == SIZE_MAX)
10515                 return -EOVERFLOW;
10516         p = kzalloc(size, GFP_KERNEL);
10517         if (!p)
10518                 return -ENOMEM;
10519
10520         ret = -EFAULT;
10521         if (copy_from_user(p, arg, size))
10522                 goto out;
10523         ret = -EINVAL;
10524         if (memchr_inv(p, 0, size))
10525                 goto out;
10526
10527         p->last_op = IORING_OP_LAST - 1;
10528         if (nr_args > IORING_OP_LAST)
10529                 nr_args = IORING_OP_LAST;
10530
10531         for (i = 0; i < nr_args; i++) {
10532                 p->ops[i].op = i;
10533                 if (!io_op_defs[i].not_supported)
10534                         p->ops[i].flags = IO_URING_OP_SUPPORTED;
10535         }
10536         p->ops_len = i;
10537
10538         ret = 0;
10539         if (copy_to_user(arg, p, size))
10540                 ret = -EFAULT;
10541 out:
10542         kfree(p);
10543         return ret;
10544 }
10545
10546 static int io_register_personality(struct io_ring_ctx *ctx)
10547 {
10548         const struct cred *creds;
10549         u32 id;
10550         int ret;
10551
10552         creds = get_current_cred();
10553
10554         ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds,
10555                         XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL);
10556         if (ret < 0) {
10557                 put_cred(creds);
10558                 return ret;
10559         }
10560         return id;
10561 }
10562
10563 static __cold int io_register_restrictions(struct io_ring_ctx *ctx,
10564                                            void __user *arg, unsigned int nr_args)
10565 {
10566         struct io_uring_restriction *res;
10567         size_t size;
10568         int i, ret;
10569
10570         /* Restrictions allowed only if rings started disabled */
10571         if (!(ctx->flags & IORING_SETUP_R_DISABLED))
10572                 return -EBADFD;
10573
10574         /* We allow only a single restrictions registration */
10575         if (ctx->restrictions.registered)
10576                 return -EBUSY;
10577
10578         if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
10579                 return -EINVAL;
10580
10581         size = array_size(nr_args, sizeof(*res));
10582         if (size == SIZE_MAX)
10583                 return -EOVERFLOW;
10584
10585         res = memdup_user(arg, size);
10586         if (IS_ERR(res))
10587                 return PTR_ERR(res);
10588
10589         ret = 0;
10590
10591         for (i = 0; i < nr_args; i++) {
10592                 switch (res[i].opcode) {
10593                 case IORING_RESTRICTION_REGISTER_OP:
10594                         if (res[i].register_op >= IORING_REGISTER_LAST) {
10595                                 ret = -EINVAL;
10596                                 goto out;
10597                         }
10598
10599                         __set_bit(res[i].register_op,
10600                                   ctx->restrictions.register_op);
10601                         break;
10602                 case IORING_RESTRICTION_SQE_OP:
10603                         if (res[i].sqe_op >= IORING_OP_LAST) {
10604                                 ret = -EINVAL;
10605                                 goto out;
10606                         }
10607
10608                         __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
10609                         break;
10610                 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
10611                         ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
10612                         break;
10613                 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
10614                         ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
10615                         break;
10616                 default:
10617                         ret = -EINVAL;
10618                         goto out;
10619                 }
10620         }
10621
10622 out:
10623         /* Reset all restrictions if an error happened */
10624         if (ret != 0)
10625                 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
10626         else
10627                 ctx->restrictions.registered = true;
10628
10629         kfree(res);
10630         return ret;
10631 }
10632
10633 static int io_register_enable_rings(struct io_ring_ctx *ctx)
10634 {
10635         if (!(ctx->flags & IORING_SETUP_R_DISABLED))
10636                 return -EBADFD;
10637
10638         if (ctx->restrictions.registered)
10639                 ctx->restricted = 1;
10640
10641         ctx->flags &= ~IORING_SETUP_R_DISABLED;
10642         if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait))
10643                 wake_up(&ctx->sq_data->wait);
10644         return 0;
10645 }
10646
10647 static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type,
10648                                      struct io_uring_rsrc_update2 *up,
10649                                      unsigned nr_args)
10650 {
10651         __u32 tmp;
10652         int err;
10653
10654         if (up->resv)
10655                 return -EINVAL;
10656         if (check_add_overflow(up->offset, nr_args, &tmp))
10657                 return -EOVERFLOW;
10658         err = io_rsrc_node_switch_start(ctx);
10659         if (err)
10660                 return err;
10661
10662         switch (type) {
10663         case IORING_RSRC_FILE:
10664                 return __io_sqe_files_update(ctx, up, nr_args);
10665         case IORING_RSRC_BUFFER:
10666                 return __io_sqe_buffers_update(ctx, up, nr_args);
10667         }
10668         return -EINVAL;
10669 }
10670
10671 static int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg,
10672                                     unsigned nr_args)
10673 {
10674         struct io_uring_rsrc_update2 up;
10675
10676         if (!nr_args)
10677                 return -EINVAL;
10678         memset(&up, 0, sizeof(up));
10679         if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update)))
10680                 return -EFAULT;
10681         return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args);
10682 }
10683
10684 static int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg,
10685                                    unsigned size, unsigned type)
10686 {
10687         struct io_uring_rsrc_update2 up;
10688
10689         if (size != sizeof(up))
10690                 return -EINVAL;
10691         if (copy_from_user(&up, arg, sizeof(up)))
10692                 return -EFAULT;
10693         if (!up.nr || up.resv)
10694                 return -EINVAL;
10695         return __io_register_rsrc_update(ctx, type, &up, up.nr);
10696 }
10697
10698 static __cold int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg,
10699                             unsigned int size, unsigned int type)
10700 {
10701         struct io_uring_rsrc_register rr;
10702
10703         /* keep it extendible */
10704         if (size != sizeof(rr))
10705                 return -EINVAL;
10706
10707         memset(&rr, 0, sizeof(rr));
10708         if (copy_from_user(&rr, arg, size))
10709                 return -EFAULT;
10710         if (!rr.nr || rr.resv || rr.resv2)
10711                 return -EINVAL;
10712
10713         switch (type) {
10714         case IORING_RSRC_FILE:
10715                 return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data),
10716                                              rr.nr, u64_to_user_ptr(rr.tags));
10717         case IORING_RSRC_BUFFER:
10718                 return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data),
10719                                                rr.nr, u64_to_user_ptr(rr.tags));
10720         }
10721         return -EINVAL;
10722 }
10723
10724 static __cold int io_register_iowq_aff(struct io_ring_ctx *ctx,
10725                                        void __user *arg, unsigned len)
10726 {
10727         struct io_uring_task *tctx = current->io_uring;
10728         cpumask_var_t new_mask;
10729         int ret;
10730
10731         if (!tctx || !tctx->io_wq)
10732                 return -EINVAL;
10733
10734         if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
10735                 return -ENOMEM;
10736
10737         cpumask_clear(new_mask);
10738         if (len > cpumask_size())
10739                 len = cpumask_size();
10740
10741         if (copy_from_user(new_mask, arg, len)) {
10742                 free_cpumask_var(new_mask);
10743                 return -EFAULT;
10744         }
10745
10746         ret = io_wq_cpu_affinity(tctx->io_wq, new_mask);
10747         free_cpumask_var(new_mask);
10748         return ret;
10749 }
10750
10751 static __cold int io_unregister_iowq_aff(struct io_ring_ctx *ctx)
10752 {
10753         struct io_uring_task *tctx = current->io_uring;
10754
10755         if (!tctx || !tctx->io_wq)
10756                 return -EINVAL;
10757
10758         return io_wq_cpu_affinity(tctx->io_wq, NULL);
10759 }
10760
10761 static __cold int io_register_iowq_max_workers(struct io_ring_ctx *ctx,
10762                                                void __user *arg)
10763         __must_hold(&ctx->uring_lock)
10764 {
10765         struct io_tctx_node *node;
10766         struct io_uring_task *tctx = NULL;
10767         struct io_sq_data *sqd = NULL;
10768         __u32 new_count[2];
10769         int i, ret;
10770
10771         if (copy_from_user(new_count, arg, sizeof(new_count)))
10772                 return -EFAULT;
10773         for (i = 0; i < ARRAY_SIZE(new_count); i++)
10774                 if (new_count[i] > INT_MAX)
10775                         return -EINVAL;
10776
10777         if (ctx->flags & IORING_SETUP_SQPOLL) {
10778                 sqd = ctx->sq_data;
10779                 if (sqd) {
10780                         /*
10781                          * Observe the correct sqd->lock -> ctx->uring_lock
10782                          * ordering. Fine to drop uring_lock here, we hold
10783                          * a ref to the ctx.
10784                          */
10785                         refcount_inc(&sqd->refs);
10786                         mutex_unlock(&ctx->uring_lock);
10787                         mutex_lock(&sqd->lock);
10788                         mutex_lock(&ctx->uring_lock);
10789                         if (sqd->thread)
10790                                 tctx = sqd->thread->io_uring;
10791                 }
10792         } else {
10793                 tctx = current->io_uring;
10794         }
10795
10796         BUILD_BUG_ON(sizeof(new_count) != sizeof(ctx->iowq_limits));
10797
10798         for (i = 0; i < ARRAY_SIZE(new_count); i++)
10799                 if (new_count[i])
10800                         ctx->iowq_limits[i] = new_count[i];
10801         ctx->iowq_limits_set = true;
10802
10803         if (tctx && tctx->io_wq) {
10804                 ret = io_wq_max_workers(tctx->io_wq, new_count);
10805                 if (ret)
10806                         goto err;
10807         } else {
10808                 memset(new_count, 0, sizeof(new_count));
10809         }
10810
10811         if (sqd) {
10812                 mutex_unlock(&sqd->lock);
10813                 io_put_sq_data(sqd);
10814         }
10815
10816         if (copy_to_user(arg, new_count, sizeof(new_count)))
10817                 return -EFAULT;
10818
10819         /* that's it for SQPOLL, only the SQPOLL task creates requests */
10820         if (sqd)
10821                 return 0;
10822
10823         /* now propagate the restriction to all registered users */
10824         list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
10825                 struct io_uring_task *tctx = node->task->io_uring;
10826
10827                 if (WARN_ON_ONCE(!tctx->io_wq))
10828                         continue;
10829
10830                 for (i = 0; i < ARRAY_SIZE(new_count); i++)
10831                         new_count[i] = ctx->iowq_limits[i];
10832                 /* ignore errors, it always returns zero anyway */
10833                 (void)io_wq_max_workers(tctx->io_wq, new_count);
10834         }
10835         return 0;
10836 err:
10837         if (sqd) {
10838                 mutex_unlock(&sqd->lock);
10839                 io_put_sq_data(sqd);
10840         }
10841         return ret;
10842 }
10843
10844 static bool io_register_op_must_quiesce(int op)
10845 {
10846         switch (op) {
10847         case IORING_REGISTER_BUFFERS:
10848         case IORING_UNREGISTER_BUFFERS:
10849         case IORING_REGISTER_FILES:
10850         case IORING_UNREGISTER_FILES:
10851         case IORING_REGISTER_FILES_UPDATE:
10852         case IORING_REGISTER_PROBE:
10853         case IORING_REGISTER_PERSONALITY:
10854         case IORING_UNREGISTER_PERSONALITY:
10855         case IORING_REGISTER_FILES2:
10856         case IORING_REGISTER_FILES_UPDATE2:
10857         case IORING_REGISTER_BUFFERS2:
10858         case IORING_REGISTER_BUFFERS_UPDATE:
10859         case IORING_REGISTER_IOWQ_AFF:
10860         case IORING_UNREGISTER_IOWQ_AFF:
10861         case IORING_REGISTER_IOWQ_MAX_WORKERS:
10862                 return false;
10863         default:
10864                 return true;
10865         }
10866 }
10867
10868 static __cold int io_ctx_quiesce(struct io_ring_ctx *ctx)
10869 {
10870         long ret;
10871
10872         percpu_ref_kill(&ctx->refs);
10873
10874         /*
10875          * Drop uring mutex before waiting for references to exit. If another
10876          * thread is currently inside io_uring_enter() it might need to grab the
10877          * uring_lock to make progress. If we hold it here across the drain
10878          * wait, then we can deadlock. It's safe to drop the mutex here, since
10879          * no new references will come in after we've killed the percpu ref.
10880          */
10881         mutex_unlock(&ctx->uring_lock);
10882         do {
10883                 ret = wait_for_completion_interruptible_timeout(&ctx->ref_comp, HZ);
10884                 if (ret) {
10885                         ret = min(0L, ret);
10886                         break;
10887                 }
10888
10889                 ret = io_run_task_work_sig();
10890                 io_req_caches_free(ctx);
10891         } while (ret >= 0);
10892         mutex_lock(&ctx->uring_lock);
10893
10894         if (ret)
10895                 io_refs_resurrect(&ctx->refs, &ctx->ref_comp);
10896         return ret;
10897 }
10898
10899 static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
10900                                void __user *arg, unsigned nr_args)
10901         __releases(ctx->uring_lock)
10902         __acquires(ctx->uring_lock)
10903 {
10904         int ret;
10905
10906         /*
10907          * We're inside the ring mutex, if the ref is already dying, then
10908          * someone else killed the ctx or is already going through
10909          * io_uring_register().
10910          */
10911         if (percpu_ref_is_dying(&ctx->refs))
10912                 return -ENXIO;
10913
10914         if (ctx->restricted) {
10915                 if (opcode >= IORING_REGISTER_LAST)
10916                         return -EINVAL;
10917                 opcode = array_index_nospec(opcode, IORING_REGISTER_LAST);
10918                 if (!test_bit(opcode, ctx->restrictions.register_op))
10919                         return -EACCES;
10920         }
10921
10922         if (io_register_op_must_quiesce(opcode)) {
10923                 ret = io_ctx_quiesce(ctx);
10924                 if (ret)
10925                         return ret;
10926         }
10927
10928         switch (opcode) {
10929         case IORING_REGISTER_BUFFERS:
10930                 ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL);
10931                 break;
10932         case IORING_UNREGISTER_BUFFERS:
10933                 ret = -EINVAL;
10934                 if (arg || nr_args)
10935                         break;
10936                 ret = io_sqe_buffers_unregister(ctx);
10937                 break;
10938         case IORING_REGISTER_FILES:
10939                 ret = io_sqe_files_register(ctx, arg, nr_args, NULL);
10940                 break;
10941         case IORING_UNREGISTER_FILES:
10942                 ret = -EINVAL;
10943                 if (arg || nr_args)
10944                         break;
10945                 ret = io_sqe_files_unregister(ctx);
10946                 break;
10947         case IORING_REGISTER_FILES_UPDATE:
10948                 ret = io_register_files_update(ctx, arg, nr_args);
10949                 break;
10950         case IORING_REGISTER_EVENTFD:
10951         case IORING_REGISTER_EVENTFD_ASYNC:
10952                 ret = -EINVAL;
10953                 if (nr_args != 1)
10954                         break;
10955                 ret = io_eventfd_register(ctx, arg);
10956                 if (ret)
10957                         break;
10958                 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
10959                         ctx->eventfd_async = 1;
10960                 else
10961                         ctx->eventfd_async = 0;
10962                 break;
10963         case IORING_UNREGISTER_EVENTFD:
10964                 ret = -EINVAL;
10965                 if (arg || nr_args)
10966                         break;
10967                 ret = io_eventfd_unregister(ctx);
10968                 break;
10969         case IORING_REGISTER_PROBE:
10970                 ret = -EINVAL;
10971                 if (!arg || nr_args > 256)
10972                         break;
10973                 ret = io_probe(ctx, arg, nr_args);
10974                 break;
10975         case IORING_REGISTER_PERSONALITY:
10976                 ret = -EINVAL;
10977                 if (arg || nr_args)
10978                         break;
10979                 ret = io_register_personality(ctx);
10980                 break;
10981         case IORING_UNREGISTER_PERSONALITY:
10982                 ret = -EINVAL;
10983                 if (arg)
10984                         break;
10985                 ret = io_unregister_personality(ctx, nr_args);
10986                 break;
10987         case IORING_REGISTER_ENABLE_RINGS:
10988                 ret = -EINVAL;
10989                 if (arg || nr_args)
10990                         break;
10991                 ret = io_register_enable_rings(ctx);
10992                 break;
10993         case IORING_REGISTER_RESTRICTIONS:
10994                 ret = io_register_restrictions(ctx, arg, nr_args);
10995                 break;
10996         case IORING_REGISTER_FILES2:
10997                 ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_FILE);
10998                 break;
10999         case IORING_REGISTER_FILES_UPDATE2:
11000                 ret = io_register_rsrc_update(ctx, arg, nr_args,
11001                                               IORING_RSRC_FILE);
11002                 break;
11003         case IORING_REGISTER_BUFFERS2:
11004                 ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_BUFFER);
11005                 break;
11006         case IORING_REGISTER_BUFFERS_UPDATE:
11007                 ret = io_register_rsrc_update(ctx, arg, nr_args,
11008                                               IORING_RSRC_BUFFER);
11009                 break;
11010         case IORING_REGISTER_IOWQ_AFF:
11011                 ret = -EINVAL;
11012                 if (!arg || !nr_args)
11013                         break;
11014                 ret = io_register_iowq_aff(ctx, arg, nr_args);
11015                 break;
11016         case IORING_UNREGISTER_IOWQ_AFF:
11017                 ret = -EINVAL;
11018                 if (arg || nr_args)
11019                         break;
11020                 ret = io_unregister_iowq_aff(ctx);
11021                 break;
11022         case IORING_REGISTER_IOWQ_MAX_WORKERS:
11023                 ret = -EINVAL;
11024                 if (!arg || nr_args != 2)
11025                         break;
11026                 ret = io_register_iowq_max_workers(ctx, arg);
11027                 break;
11028         default:
11029                 ret = -EINVAL;
11030                 break;
11031         }
11032
11033         if (io_register_op_must_quiesce(opcode)) {
11034                 /* bring the ctx back to life */
11035                 percpu_ref_reinit(&ctx->refs);
11036                 reinit_completion(&ctx->ref_comp);
11037         }
11038         return ret;
11039 }
11040
11041 SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
11042                 void __user *, arg, unsigned int, nr_args)
11043 {
11044         struct io_ring_ctx *ctx;
11045         long ret = -EBADF;
11046         struct fd f;
11047
11048         f = fdget(fd);
11049         if (!f.file)
11050                 return -EBADF;
11051
11052         ret = -EOPNOTSUPP;
11053         if (f.file->f_op != &io_uring_fops)
11054                 goto out_fput;
11055
11056         ctx = f.file->private_data;
11057
11058         io_run_task_work();
11059
11060         mutex_lock(&ctx->uring_lock);
11061         ret = __io_uring_register(ctx, opcode, arg, nr_args);
11062         mutex_unlock(&ctx->uring_lock);
11063         trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
11064                                                         ctx->cq_ev_fd != NULL, ret);
11065 out_fput:
11066         fdput(f);
11067         return ret;
11068 }
11069
11070 static int __init io_uring_init(void)
11071 {
11072 #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
11073         BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
11074         BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
11075 } while (0)
11076
11077 #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
11078         __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
11079         BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
11080         BUILD_BUG_SQE_ELEM(0,  __u8,   opcode);
11081         BUILD_BUG_SQE_ELEM(1,  __u8,   flags);
11082         BUILD_BUG_SQE_ELEM(2,  __u16,  ioprio);
11083         BUILD_BUG_SQE_ELEM(4,  __s32,  fd);
11084         BUILD_BUG_SQE_ELEM(8,  __u64,  off);
11085         BUILD_BUG_SQE_ELEM(8,  __u64,  addr2);
11086         BUILD_BUG_SQE_ELEM(16, __u64,  addr);
11087         BUILD_BUG_SQE_ELEM(16, __u64,  splice_off_in);
11088         BUILD_BUG_SQE_ELEM(24, __u32,  len);
11089         BUILD_BUG_SQE_ELEM(28,     __kernel_rwf_t, rw_flags);
11090         BUILD_BUG_SQE_ELEM(28, /* compat */   int, rw_flags);
11091         BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
11092         BUILD_BUG_SQE_ELEM(28, __u32,  fsync_flags);
11093         BUILD_BUG_SQE_ELEM(28, /* compat */ __u16,  poll_events);
11094         BUILD_BUG_SQE_ELEM(28, __u32,  poll32_events);
11095         BUILD_BUG_SQE_ELEM(28, __u32,  sync_range_flags);
11096         BUILD_BUG_SQE_ELEM(28, __u32,  msg_flags);
11097         BUILD_BUG_SQE_ELEM(28, __u32,  timeout_flags);
11098         BUILD_BUG_SQE_ELEM(28, __u32,  accept_flags);
11099         BUILD_BUG_SQE_ELEM(28, __u32,  cancel_flags);
11100         BUILD_BUG_SQE_ELEM(28, __u32,  open_flags);
11101         BUILD_BUG_SQE_ELEM(28, __u32,  statx_flags);
11102         BUILD_BUG_SQE_ELEM(28, __u32,  fadvise_advice);
11103         BUILD_BUG_SQE_ELEM(28, __u32,  splice_flags);
11104         BUILD_BUG_SQE_ELEM(32, __u64,  user_data);
11105         BUILD_BUG_SQE_ELEM(40, __u16,  buf_index);
11106         BUILD_BUG_SQE_ELEM(40, __u16,  buf_group);
11107         BUILD_BUG_SQE_ELEM(42, __u16,  personality);
11108         BUILD_BUG_SQE_ELEM(44, __s32,  splice_fd_in);
11109         BUILD_BUG_SQE_ELEM(44, __u32,  file_index);
11110
11111         BUILD_BUG_ON(sizeof(struct io_uring_files_update) !=
11112                      sizeof(struct io_uring_rsrc_update));
11113         BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) >
11114                      sizeof(struct io_uring_rsrc_update2));
11115
11116         /* ->buf_index is u16 */
11117         BUILD_BUG_ON(IORING_MAX_REG_BUFFERS >= (1u << 16));
11118
11119         /* should fit into one byte */
11120         BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8));
11121         BUILD_BUG_ON(SQE_COMMON_FLAGS >= (1 << 8));
11122         BUILD_BUG_ON((SQE_VALID_FLAGS | SQE_COMMON_FLAGS) != SQE_VALID_FLAGS);
11123
11124         BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
11125         BUILD_BUG_ON(__REQ_F_LAST_BIT > 8 * sizeof(int));
11126
11127         req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC |
11128                                 SLAB_ACCOUNT);
11129         return 0;
11130 };
11131 __initcall(io_uring_init);