OSDN Git Service

io_uring: add non-vectored read/write commands
[tomoyo/tomoyo-test1.git] / fs / io_uring.c
index 187dd94..407ba33 100644 (file)
@@ -70,6 +70,8 @@
 #include <linux/sizes.h>
 #include <linux/hugetlb.h>
 #include <linux/highmem.h>
+#include <linux/namei.h>
+#include <linux/fsnotify.h>
 
 #define CREATE_TRACE_POINTS
 #include <trace/events/io_uring.h>
@@ -177,6 +179,21 @@ struct fixed_file_table {
        struct file             **files;
 };
 
+enum {
+       FFD_F_ATOMIC,
+};
+
+struct fixed_file_data {
+       struct fixed_file_table         *table;
+       struct io_ring_ctx              *ctx;
+
+       struct percpu_ref               refs;
+       struct llist_head               put_llist;
+       unsigned long                   state;
+       struct work_struct              ref_work;
+       struct completion               done;
+};
+
 struct io_ring_ctx {
        struct {
                struct percpu_ref       refs;
@@ -207,13 +224,14 @@ struct io_ring_ctx {
                unsigned                sq_thread_idle;
                unsigned                cached_sq_dropped;
                atomic_t                cached_cq_overflow;
-               struct io_uring_sqe     *sq_sqes;
+               unsigned long           sq_check_overflow;
 
                struct list_head        defer_list;
                struct list_head        timeout_list;
                struct list_head        cq_overflow_list;
 
                wait_queue_head_t       inflight_wait;
+               struct io_uring_sqe     *sq_sqes;
        } ____cacheline_aligned_in_smp;
 
        struct io_rings *rings;
@@ -229,7 +247,7 @@ struct io_ring_ctx {
         * readers must ensure that ->refs is alive as long as the file* is
         * used. Only updated through io_uring_register(2).
         */
-       struct fixed_file_table *file_table;
+       struct fixed_file_data  *file_data;
        unsigned                nr_user_files;
 
        /* if used, fixed mapped user buffers */
@@ -255,6 +273,7 @@ struct io_ring_ctx {
                unsigned                cq_entries;
                unsigned                cq_mask;
                atomic_t                cq_timeouts;
+               unsigned long           cq_check_overflow;
                struct wait_queue_head  cq_wait;
                struct fasync_struct    *cq_fasync;
                struct eventfd_ctx      *cq_ev_fd;
@@ -267,7 +286,8 @@ struct io_ring_ctx {
 
        struct {
                spinlock_t              completion_lock;
-               bool                    poll_multi_file;
+               struct llist_head       poll_llist;
+
                /*
                 * ->poll_list is protected by the ctx->uring_lock for
                 * io_uring instances that don't use IORING_SETUP_SQPOLL.
@@ -277,6 +297,7 @@ struct io_ring_ctx {
                struct list_head        poll_list;
                struct hlist_head       *cancel_hash;
                unsigned                cancel_hash_bits;
+               bool                    poll_multi_file;
 
                spinlock_t              inflight_lock;
                struct list_head        inflight_list;
@@ -299,6 +320,12 @@ struct io_poll_iocb {
        struct wait_queue_entry         wait;
 };
 
+struct io_close {
+       struct file                     *file;
+       struct file                     *put_file;
+       int                             fd;
+};
+
 struct io_timeout_data {
        struct io_kiocb                 *req;
        struct hrtimer                  timer;
@@ -319,6 +346,7 @@ struct io_sync {
        loff_t                          len;
        loff_t                          off;
        int                             flags;
+       int                             mode;
 };
 
 struct io_cancel {
@@ -352,6 +380,26 @@ struct io_sr_msg {
        int                             msg_flags;
 };
 
+struct io_open {
+       struct file                     *file;
+       int                             dfd;
+       union {
+               umode_t                 mode;
+               unsigned                mask;
+       };
+       const char __user               *fname;
+       struct filename                 *filename;
+       struct statx __user             *buffer;
+       int                             flags;
+};
+
+struct io_files_update {
+       struct file                     *file;
+       u64                             arg;
+       u32                             nr_args;
+       u32                             offset;
+};
+
 struct io_async_connect {
        struct sockaddr_storage         address;
 };
@@ -370,12 +418,17 @@ struct io_async_rw {
        ssize_t                         size;
 };
 
+struct io_async_open {
+       struct filename                 *filename;
+};
+
 struct io_async_ctx {
        union {
                struct io_async_rw      rw;
                struct io_async_msghdr  msg;
                struct io_async_connect connect;
                struct io_timeout_data  timeout;
+               struct io_async_open    open;
        };
 };
 
@@ -396,10 +449,20 @@ struct io_kiocb {
                struct io_timeout       timeout;
                struct io_connect       connect;
                struct io_sr_msg        sr_msg;
+               struct io_open          open;
+               struct io_close         close;
+               struct io_files_update  files_update;
        };
 
        struct io_async_ctx             *io;
-       struct file                     *ring_file;
+       union {
+               /*
+                * ring_file is only used in the submission path, and
+                * llist_node is only used for poll deferred completions
+                */
+               struct file             *ring_file;
+               struct llist_node       llist_node;
+       };
        int                             ring_fd;
        bool                            has_user;
        bool                            in_async;
@@ -431,6 +494,7 @@ struct io_kiocb {
 #define REQ_F_INFLIGHT         16384   /* on inflight list */
 #define REQ_F_COMP_LOCKED      32768   /* completion under lock */
 #define REQ_F_HARDLINK         65536   /* doesn't sever on completion < 0 */
+#define REQ_F_FORCE_ASYNC      131072  /* IOSQE_ASYNC */
        u64                     user_data;
        u32                     result;
        u32                     sequence;
@@ -463,14 +527,156 @@ struct io_submit_state {
        unsigned int            ios_left;
 };
 
+struct io_op_def {
+       /* needs req->io allocated for deferral/async */
+       unsigned                async_ctx : 1;
+       /* needs current->mm setup, does mm access */
+       unsigned                needs_mm : 1;
+       /* needs req->file assigned */
+       unsigned                needs_file : 1;
+       /* needs req->file assigned IFF fd is >= 0 */
+       unsigned                fd_non_neg : 1;
+       /* hash wq insertion if file is a regular file */
+       unsigned                hash_reg_file : 1;
+       /* unbound wq insertion if file is a non-regular file */
+       unsigned                unbound_nonreg_file : 1;
+};
+
+static const struct io_op_def io_op_defs[] = {
+       {
+               /* IORING_OP_NOP */
+       },
+       {
+               /* IORING_OP_READV */
+               .async_ctx              = 1,
+               .needs_mm               = 1,
+               .needs_file             = 1,
+               .unbound_nonreg_file    = 1,
+       },
+       {
+               /* IORING_OP_WRITEV */
+               .async_ctx              = 1,
+               .needs_mm               = 1,
+               .needs_file             = 1,
+               .hash_reg_file          = 1,
+               .unbound_nonreg_file    = 1,
+       },
+       {
+               /* IORING_OP_FSYNC */
+               .needs_file             = 1,
+       },
+       {
+               /* IORING_OP_READ_FIXED */
+               .needs_file             = 1,
+               .unbound_nonreg_file    = 1,
+       },
+       {
+               /* IORING_OP_WRITE_FIXED */
+               .needs_file             = 1,
+               .hash_reg_file          = 1,
+               .unbound_nonreg_file    = 1,
+       },
+       {
+               /* IORING_OP_POLL_ADD */
+               .needs_file             = 1,
+               .unbound_nonreg_file    = 1,
+       },
+       {
+               /* IORING_OP_POLL_REMOVE */
+       },
+       {
+               /* IORING_OP_SYNC_FILE_RANGE */
+               .needs_file             = 1,
+       },
+       {
+               /* IORING_OP_SENDMSG */
+               .async_ctx              = 1,
+               .needs_mm               = 1,
+               .needs_file             = 1,
+               .unbound_nonreg_file    = 1,
+       },
+       {
+               /* IORING_OP_RECVMSG */
+               .async_ctx              = 1,
+               .needs_mm               = 1,
+               .needs_file             = 1,
+               .unbound_nonreg_file    = 1,
+       },
+       {
+               /* IORING_OP_TIMEOUT */
+               .async_ctx              = 1,
+               .needs_mm               = 1,
+       },
+       {
+               /* IORING_OP_TIMEOUT_REMOVE */
+       },
+       {
+               /* IORING_OP_ACCEPT */
+               .needs_mm               = 1,
+               .needs_file             = 1,
+               .unbound_nonreg_file    = 1,
+       },
+       {
+               /* IORING_OP_ASYNC_CANCEL */
+       },
+       {
+               /* IORING_OP_LINK_TIMEOUT */
+               .async_ctx              = 1,
+               .needs_mm               = 1,
+       },
+       {
+               /* IORING_OP_CONNECT */
+               .async_ctx              = 1,
+               .needs_mm               = 1,
+               .needs_file             = 1,
+               .unbound_nonreg_file    = 1,
+       },
+       {
+               /* IORING_OP_FALLOCATE */
+               .needs_file             = 1,
+       },
+       {
+               /* IORING_OP_OPENAT */
+               .needs_file             = 1,
+               .fd_non_neg             = 1,
+       },
+       {
+               /* IORING_OP_CLOSE */
+               .needs_file             = 1,
+       },
+       {
+               /* IORING_OP_FILES_UPDATE */
+               .needs_mm               = 1,
+       },
+       {
+               /* IORING_OP_STATX */
+               .needs_mm               = 1,
+               .needs_file             = 1,
+               .fd_non_neg             = 1,
+       },
+       {
+               /* IORING_OP_READ */
+               .needs_mm               = 1,
+               .needs_file             = 1,
+               .unbound_nonreg_file    = 1,
+       },
+       {
+               /* IORING_OP_WRITE */
+               .needs_mm               = 1,
+               .needs_file             = 1,
+               .unbound_nonreg_file    = 1,
+       },
+};
+
 static void io_wq_submit_work(struct io_wq_work **workptr);
 static void io_cqring_fill_event(struct io_kiocb *req, long res);
-static void __io_free_req(struct io_kiocb *req);
 static void io_put_req(struct io_kiocb *req);
-static void io_double_put_req(struct io_kiocb *req);
 static void __io_double_put_req(struct io_kiocb *req);
 static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
 static void io_queue_linked_timeout(struct io_kiocb *req);
+static int __io_sqe_files_update(struct io_ring_ctx *ctx,
+                                struct io_uring_files_update *ip,
+                                unsigned nr_args);
 
 static struct kmem_cache *req_cachep;
 
@@ -540,6 +746,7 @@ static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
        mutex_init(&ctx->uring_lock);
        init_waitqueue_head(&ctx->wait);
        spin_lock_init(&ctx->completion_lock);
+       init_llist_head(&ctx->poll_llist);
        INIT_LIST_HEAD(&ctx->poll_list);
        INIT_LIST_HEAD(&ctx->defer_list);
        INIT_LIST_HEAD(&ctx->timeout_list);
@@ -617,41 +824,20 @@ static void __io_commit_cqring(struct io_ring_ctx *ctx)
        }
 }
 
-static inline bool io_req_needs_user(struct io_kiocb *req)
-{
-       return !(req->opcode == IORING_OP_READ_FIXED ||
-                req->opcode == IORING_OP_WRITE_FIXED);
-}
-
 static inline bool io_prep_async_work(struct io_kiocb *req,
                                      struct io_kiocb **link)
 {
+       const struct io_op_def *def = &io_op_defs[req->opcode];
        bool do_hashed = false;
 
-       switch (req->opcode) {
-       case IORING_OP_WRITEV:
-       case IORING_OP_WRITE_FIXED:
-               /* only regular files should be hashed for writes */
-               if (req->flags & REQ_F_ISREG)
+       if (req->flags & REQ_F_ISREG) {
+               if (def->hash_reg_file)
                        do_hashed = true;
-               /* fall-through */
-       case IORING_OP_READV:
-       case IORING_OP_READ_FIXED:
-       case IORING_OP_SENDMSG:
-       case IORING_OP_RECVMSG:
-       case IORING_OP_ACCEPT:
-       case IORING_OP_POLL_ADD:
-       case IORING_OP_CONNECT:
-               /*
-                * We know REQ_F_ISREG is not set on some of these
-                * opcodes, but this enables us to keep the check in
-                * just one place.
-                */
-               if (!(req->flags & REQ_F_ISREG))
+       } else {
+               if (def->unbound_nonreg_file)
                        req->work.flags |= IO_WQ_WORK_UNBOUND;
-               break;
        }
-       if (io_req_needs_user(req))
+       if (def->needs_mm)
                req->work.flags |= IO_WQ_WORK_NEEDS_USER;
 
        *link = io_prep_linked_timeout(req);
@@ -788,6 +974,10 @@ static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
        }
 
        io_commit_cqring(ctx);
+       if (cqe) {
+               clear_bit(0, &ctx->sq_check_overflow);
+               clear_bit(0, &ctx->cq_check_overflow);
+       }
        spin_unlock_irqrestore(&ctx->completion_lock, flags);
        io_cqring_ev_posted(ctx);
 
@@ -821,6 +1011,10 @@ static void io_cqring_fill_event(struct io_kiocb *req, long res)
                WRITE_ONCE(ctx->rings->cq_overflow,
                                atomic_inc_return(&ctx->cached_cq_overflow));
        } else {
+               if (list_empty(&ctx->cq_overflow_list)) {
+                       set_bit(0, &ctx->sq_check_overflow);
+                       set_bit(0, &ctx->cq_check_overflow);
+               }
                refcount_inc(&req->refs);
                req->result = res;
                list_add_tail(&req->list, &ctx->cq_overflow_list);
@@ -920,6 +1114,7 @@ static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
        if (*nr) {
                kmem_cache_free_bulk(req_cachep, *nr, reqs);
                percpu_ref_put_many(&ctx->refs, *nr);
+               percpu_ref_put_many(&ctx->file_data->refs, *nr);
                *nr = 0;
        }
 }
@@ -930,8 +1125,12 @@ static void __io_free_req(struct io_kiocb *req)
 
        if (req->io)
                kfree(req->io);
-       if (req->file && !(req->flags & REQ_F_FIXED_FILE))
-               fput(req->file);
+       if (req->file) {
+               if (req->flags & REQ_F_FIXED_FILE)
+                       percpu_ref_put(&ctx->file_data->refs);
+               else
+                       fput(req->file);
+       }
        if (req->flags & REQ_F_INFLIGHT) {
                unsigned long flags;
 
@@ -1118,19 +1317,21 @@ static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
 {
        struct io_rings *rings = ctx->rings;
 
-       /*
-        * noflush == true is from the waitqueue handler, just ensure we wake
-        * up the task, and the next invocation will flush the entries. We
-        * cannot safely to it from here.
-        */
-       if (noflush && !list_empty(&ctx->cq_overflow_list))
-               return -1U;
+       if (test_bit(0, &ctx->cq_check_overflow)) {
+               /*
+                * noflush == true is from the waitqueue handler, just ensure
+                * we wake up the task, and the next invocation will flush the
+                * entries. We cannot safely to it from here.
+                */
+               if (noflush && !list_empty(&ctx->cq_overflow_list))
+                       return -1U;
 
-       io_cqring_overflow_flush(ctx, false);
+               io_cqring_overflow_flush(ctx, false);
+       }
 
        /* See comment at the top of this file */
        smp_rmb();
-       return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
+       return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
 }
 
 static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
@@ -1141,6 +1342,20 @@ static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
        return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
 }
 
+static inline bool io_req_multi_free(struct io_kiocb *req)
+{
+       /*
+        * If we're not using fixed files, we have to pair the completion part
+        * with the file put. Use regular completions for those, only batch
+        * free for fixed file and non-linked commands.
+        */
+       if (((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) == REQ_F_FIXED_FILE)
+           && !io_is_fallback_req(req) && !req->io)
+               return true;
+
+       return false;
+}
+
 /*
  * Find and free completed poll iocbs
  */
@@ -1160,14 +1375,7 @@ static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
                (*nr_events)++;
 
                if (refcount_dec_and_test(&req->refs)) {
-                       /* If we're not using fixed files, we have to pair the
-                        * completion part with the file put. Use regular
-                        * completions for those, only batch free for fixed
-                        * file and non-linked commands.
-                        */
-                       if (((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) ==
-                           REQ_F_FIXED_FILE) && !io_is_fallback_req(req) &&
-                           !req->io) {
+                       if (io_req_multi_free(req)) {
                                reqs[to_free++] = req;
                                if (to_free == ARRAY_SIZE(reqs))
                                        io_free_req_many(ctx, reqs, &to_free);
@@ -1671,6 +1879,13 @@ static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
        if (req->rw.kiocb.private)
                return -EINVAL;
 
+       if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
+               ssize_t ret;
+               ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
+               *iovec = NULL;
+               return ret;
+       }
+
        if (req->io) {
                struct io_async_rw *iorw = &req->io->rw;
 
@@ -1767,6 +1982,8 @@ static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
 
 static int io_alloc_async_ctx(struct io_kiocb *req)
 {
+       if (!io_op_defs[req->opcode].async_ctx)
+               return 0;
        req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
        return req->io == NULL;
 }
@@ -2101,6 +2318,271 @@ static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt,
        return 0;
 }
 
+static void io_fallocate_finish(struct io_wq_work **workptr)
+{
+       struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
+       struct io_kiocb *nxt = NULL;
+       int ret;
+
+       ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
+                               req->sync.len);
+       if (ret < 0)
+               req_set_fail_links(req);
+       io_cqring_add_event(req, ret);
+       io_put_req_find_next(req, &nxt);
+       if (nxt)
+               io_wq_assign_next(workptr, nxt);
+}
+
+static int io_fallocate_prep(struct io_kiocb *req,
+                            const struct io_uring_sqe *sqe)
+{
+       if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
+               return -EINVAL;
+
+       req->sync.off = READ_ONCE(sqe->off);
+       req->sync.len = READ_ONCE(sqe->addr);
+       req->sync.mode = READ_ONCE(sqe->len);
+       return 0;
+}
+
+static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt,
+                       bool force_nonblock)
+{
+       struct io_wq_work *work, *old_work;
+
+       /* fallocate always requiring blocking context */
+       if (force_nonblock) {
+               io_put_req(req);
+               req->work.func = io_fallocate_finish;
+               return -EAGAIN;
+       }
+
+       work = old_work = &req->work;
+       io_fallocate_finish(&work);
+       if (work && work != old_work)
+               *nxt = container_of(work, struct io_kiocb, work);
+
+       return 0;
+}
+
+static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
+{
+       int ret;
+
+       if (sqe->ioprio || sqe->buf_index)
+               return -EINVAL;
+
+       req->open.dfd = READ_ONCE(sqe->fd);
+       req->open.mode = READ_ONCE(sqe->len);
+       req->open.fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
+       req->open.flags = READ_ONCE(sqe->open_flags);
+
+       req->open.filename = getname(req->open.fname);
+       if (IS_ERR(req->open.filename)) {
+               ret = PTR_ERR(req->open.filename);
+               req->open.filename = NULL;
+               return ret;
+       }
+
+       return 0;
+}
+
+static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt,
+                    bool force_nonblock)
+{
+       struct open_flags op;
+       struct open_how how;
+       struct file *file;
+       int ret;
+
+       if (force_nonblock) {
+               req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
+               return -EAGAIN;
+       }
+
+       how = build_open_how(req->open.flags, req->open.mode);
+       ret = build_open_flags(&how, &op);
+       if (ret)
+               goto err;
+
+       ret = get_unused_fd_flags(how.flags);
+       if (ret < 0)
+               goto err;
+
+       file = do_filp_open(req->open.dfd, req->open.filename, &op);
+       if (IS_ERR(file)) {
+               put_unused_fd(ret);
+               ret = PTR_ERR(file);
+       } else {
+               fsnotify_open(file);
+               fd_install(ret, file);
+       }
+err:
+       putname(req->open.filename);
+       if (ret < 0)
+               req_set_fail_links(req);
+       io_cqring_add_event(req, ret);
+       io_put_req_find_next(req, nxt);
+       return 0;
+}
+
+static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
+{
+       unsigned lookup_flags;
+       int ret;
+
+       if (sqe->ioprio || sqe->buf_index)
+               return -EINVAL;
+
+       req->open.dfd = READ_ONCE(sqe->fd);
+       req->open.mask = READ_ONCE(sqe->len);
+       req->open.fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
+       req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
+       req->open.flags = READ_ONCE(sqe->statx_flags);
+
+       if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.flags))
+               return -EINVAL;
+
+       req->open.filename = getname_flags(req->open.fname, lookup_flags, NULL);
+       if (IS_ERR(req->open.filename)) {
+               ret = PTR_ERR(req->open.filename);
+               req->open.filename = NULL;
+               return ret;
+       }
+
+       return 0;
+}
+
+static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt,
+                   bool force_nonblock)
+{
+       struct io_open *ctx = &req->open;
+       unsigned lookup_flags;
+       struct path path;
+       struct kstat stat;
+       int ret;
+
+       if (force_nonblock)
+               return -EAGAIN;
+
+       if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->flags))
+               return -EINVAL;
+
+retry:
+       /* filename_lookup() drops it, keep a reference */
+       ctx->filename->refcnt++;
+
+       ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
+                               NULL);
+       if (ret)
+               goto err;
+
+       ret = vfs_getattr(&path, &stat, ctx->mask, ctx->flags);
+       path_put(&path);
+       if (retry_estale(ret, lookup_flags)) {
+               lookup_flags |= LOOKUP_REVAL;
+               goto retry;
+       }
+       if (!ret)
+               ret = cp_statx(&stat, ctx->buffer);
+err:
+       putname(ctx->filename);
+       if (ret < 0)
+               req_set_fail_links(req);
+       io_cqring_add_event(req, ret);
+       io_put_req_find_next(req, nxt);
+       return 0;
+}
+
+static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
+{
+       /*
+        * If we queue this for async, it must not be cancellable. That would
+        * leave the 'file' in an undeterminate state.
+        */
+       req->work.flags |= IO_WQ_WORK_NO_CANCEL;
+
+       if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
+           sqe->rw_flags || sqe->buf_index)
+               return -EINVAL;
+       if (sqe->flags & IOSQE_FIXED_FILE)
+               return -EINVAL;
+
+       req->close.fd = READ_ONCE(sqe->fd);
+       if (req->file->f_op == &io_uring_fops ||
+           req->close.fd == req->ring_fd)
+               return -EBADF;
+
+       return 0;
+}
+
+static void io_close_finish(struct io_wq_work **workptr)
+{
+       struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
+       struct io_kiocb *nxt = NULL;
+
+       /* Invoked with files, we need to do the close */
+       if (req->work.files) {
+               int ret;
+
+               ret = filp_close(req->close.put_file, req->work.files);
+               if (ret < 0) {
+                       req_set_fail_links(req);
+               }
+               io_cqring_add_event(req, ret);
+       }
+
+       fput(req->close.put_file);
+
+       /* we bypassed the re-issue, drop the submission reference */
+       io_put_req(req);
+       io_put_req_find_next(req, &nxt);
+       if (nxt)
+               io_wq_assign_next(workptr, nxt);
+}
+
+static int io_close(struct io_kiocb *req, struct io_kiocb **nxt,
+                   bool force_nonblock)
+{
+       int ret;
+
+       req->close.put_file = NULL;
+       ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
+       if (ret < 0)
+               return ret;
+
+       /* if the file has a flush method, be safe and punt to async */
+       if (req->close.put_file->f_op->flush && !io_wq_current_is_worker()) {
+               req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
+               goto eagain;
+       }
+
+       /*
+        * No ->flush(), safely close from here and just punt the
+        * fput() to async context.
+        */
+       ret = filp_close(req->close.put_file, current->files);
+
+       if (ret < 0)
+               req_set_fail_links(req);
+       io_cqring_add_event(req, ret);
+
+       if (io_wq_current_is_worker()) {
+               struct io_wq_work *old_work, *work;
+
+               old_work = work = &req->work;
+               io_close_finish(&work);
+               if (work && work != old_work)
+                       *nxt = container_of(work, struct io_kiocb, work);
+               return 0;
+       }
+
+eagain:
+       req->work.func = io_close_finish;
+       return -EAGAIN;
+}
+
 static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
 {
        struct io_ring_ctx *ctx = req->ctx;
@@ -2635,20 +3117,57 @@ static void io_poll_complete_work(struct io_wq_work **workptr)
                io_wq_assign_next(workptr, nxt);
 }
 
-static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
-                       void *key)
+static void __io_poll_flush(struct io_ring_ctx *ctx, struct llist_node *nodes)
 {
-       struct io_poll_iocb *poll = wait->private;
-       struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
-       struct io_ring_ctx *ctx = req->ctx;
-       __poll_t mask = key_to_poll(key);
-       unsigned long flags;
+       void *reqs[IO_IOPOLL_BATCH];
+       struct io_kiocb *req, *tmp;
+       int to_free = 0;
 
-       /* for instances that support it check for an event match first: */
-       if (mask && !(mask & poll->events))
-               return 0;
+       spin_lock_irq(&ctx->completion_lock);
+       llist_for_each_entry_safe(req, tmp, nodes, llist_node) {
+               hash_del(&req->hash_node);
+               io_poll_complete(req, req->result, 0);
 
-       list_del_init(&poll->wait.entry);
+               if (refcount_dec_and_test(&req->refs)) {
+                       if (io_req_multi_free(req)) {
+                               reqs[to_free++] = req;
+                               if (to_free == ARRAY_SIZE(reqs))
+                                       io_free_req_many(ctx, reqs, &to_free);
+                       } else {
+                               req->flags |= REQ_F_COMP_LOCKED;
+                               io_free_req(req);
+                       }
+               }
+       }
+       spin_unlock_irq(&ctx->completion_lock);
+
+       io_cqring_ev_posted(ctx);
+       io_free_req_many(ctx, reqs, &to_free);
+}
+
+static void io_poll_flush(struct io_wq_work **workptr)
+{
+       struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
+       struct llist_node *nodes;
+
+       nodes = llist_del_all(&req->ctx->poll_llist);
+       if (nodes)
+               __io_poll_flush(req->ctx, nodes);
+}
+
+static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
+                       void *key)
+{
+       struct io_poll_iocb *poll = wait->private;
+       struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
+       struct io_ring_ctx *ctx = req->ctx;
+       __poll_t mask = key_to_poll(key);
+
+       /* for instances that support it check for an event match first: */
+       if (mask && !(mask & poll->events))
+               return 0;
+
+       list_del_init(&poll->wait.entry);
 
        /*
         * Run completion inline if we can. We're using trylock here because
@@ -2656,17 +3175,31 @@ static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
         * If we have a link timeout we're going to need the completion_lock
         * for finalizing the request, mark us as having grabbed that already.
         */
-       if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
-               hash_del(&req->hash_node);
-               io_poll_complete(req, mask, 0);
-               req->flags |= REQ_F_COMP_LOCKED;
-               io_put_req(req);
-               spin_unlock_irqrestore(&ctx->completion_lock, flags);
+       if (mask) {
+               unsigned long flags;
 
-               io_cqring_ev_posted(ctx);
-       } else {
-               io_queue_async_work(req);
+               if (llist_empty(&ctx->poll_llist) &&
+                   spin_trylock_irqsave(&ctx->completion_lock, flags)) {
+                       hash_del(&req->hash_node);
+                       io_poll_complete(req, mask, 0);
+                       req->flags |= REQ_F_COMP_LOCKED;
+                       io_put_req(req);
+                       spin_unlock_irqrestore(&ctx->completion_lock, flags);
+
+                       io_cqring_ev_posted(ctx);
+                       req = NULL;
+               } else {
+                       req->result = mask;
+                       req->llist_node.next = NULL;
+                       /* if the list wasn't empty, we're done */
+                       if (!llist_add(&req->llist_node, &ctx->poll_llist))
+                               req = NULL;
+                       else
+                               req->work.func = io_poll_flush;
+               }
        }
+       if (req)
+               io_queue_async_work(req);
 
        return 1;
 }
@@ -3071,6 +3604,45 @@ static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt)
        return 0;
 }
 
+static int io_files_update_prep(struct io_kiocb *req,
+                               const struct io_uring_sqe *sqe)
+{
+       if (sqe->flags || sqe->ioprio || sqe->rw_flags)
+               return -EINVAL;
+
+       req->files_update.offset = READ_ONCE(sqe->off);
+       req->files_update.nr_args = READ_ONCE(sqe->len);
+       if (!req->files_update.nr_args)
+               return -EINVAL;
+       req->files_update.arg = READ_ONCE(sqe->addr);
+       return 0;
+}
+
+static int io_files_update(struct io_kiocb *req, bool force_nonblock)
+{
+       struct io_ring_ctx *ctx = req->ctx;
+       struct io_uring_files_update up;
+       int ret;
+
+       if (force_nonblock) {
+               req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
+               return -EAGAIN;
+       }
+
+       up.offset = req->files_update.offset;
+       up.fds = req->files_update.arg;
+
+       mutex_lock(&ctx->uring_lock);
+       ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
+       mutex_unlock(&ctx->uring_lock);
+
+       if (ret < 0)
+               req_set_fail_links(req);
+       io_cqring_add_event(req, ret);
+       io_put_req(req);
+       return 0;
+}
+
 static int io_req_defer_prep(struct io_kiocb *req,
                             const struct io_uring_sqe *sqe)
 {
@@ -3081,10 +3653,12 @@ static int io_req_defer_prep(struct io_kiocb *req,
                break;
        case IORING_OP_READV:
        case IORING_OP_READ_FIXED:
+       case IORING_OP_READ:
                ret = io_read_prep(req, sqe, true);
                break;
        case IORING_OP_WRITEV:
        case IORING_OP_WRITE_FIXED:
+       case IORING_OP_WRITE:
                ret = io_write_prep(req, sqe, true);
                break;
        case IORING_OP_POLL_ADD:
@@ -3123,6 +3697,21 @@ static int io_req_defer_prep(struct io_kiocb *req,
        case IORING_OP_ACCEPT:
                ret = io_accept_prep(req, sqe);
                break;
+       case IORING_OP_FALLOCATE:
+               ret = io_fallocate_prep(req, sqe);
+               break;
+       case IORING_OP_OPENAT:
+               ret = io_openat_prep(req, sqe);
+               break;
+       case IORING_OP_CLOSE:
+               ret = io_close_prep(req, sqe);
+               break;
+       case IORING_OP_FILES_UPDATE:
+               ret = io_files_update_prep(req, sqe);
+               break;
+       case IORING_OP_STATX:
+               ret = io_statx_prep(req, sqe);
+               break;
        default:
                printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
                                req->opcode);
@@ -3173,6 +3762,7 @@ static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
                break;
        case IORING_OP_READV:
        case IORING_OP_READ_FIXED:
+       case IORING_OP_READ:
                if (sqe) {
                        ret = io_read_prep(req, sqe, force_nonblock);
                        if (ret < 0)
@@ -3182,6 +3772,7 @@ static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
                break;
        case IORING_OP_WRITEV:
        case IORING_OP_WRITE_FIXED:
+       case IORING_OP_WRITE:
                if (sqe) {
                        ret = io_write_prep(req, sqe, force_nonblock);
                        if (ret < 0)
@@ -3277,6 +3868,46 @@ static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
                }
                ret = io_async_cancel(req, nxt);
                break;
+       case IORING_OP_FALLOCATE:
+               if (sqe) {
+                       ret = io_fallocate_prep(req, sqe);
+                       if (ret)
+                               break;
+               }
+               ret = io_fallocate(req, nxt, force_nonblock);
+               break;
+       case IORING_OP_OPENAT:
+               if (sqe) {
+                       ret = io_openat_prep(req, sqe);
+                       if (ret)
+                               break;
+               }
+               ret = io_openat(req, nxt, force_nonblock);
+               break;
+       case IORING_OP_CLOSE:
+               if (sqe) {
+                       ret = io_close_prep(req, sqe);
+                       if (ret)
+                               break;
+               }
+               ret = io_close(req, nxt, force_nonblock);
+               break;
+       case IORING_OP_FILES_UPDATE:
+               if (sqe) {
+                       ret = io_files_update_prep(req, sqe);
+                       if (ret)
+                               break;
+               }
+               ret = io_files_update(req, force_nonblock);
+               break;
+       case IORING_OP_STATX:
+               if (sqe) {
+                       ret = io_statx_prep(req, sqe);
+                       if (ret)
+                               break;
+               }
+               ret = io_statx(req, nxt, force_nonblock);
+               break;
        default:
                ret = -EINVAL;
                break;
@@ -3311,8 +3942,11 @@ static void io_wq_submit_work(struct io_wq_work **workptr)
        struct io_kiocb *nxt = NULL;
        int ret = 0;
 
-       if (work->flags & IO_WQ_WORK_CANCEL)
+       /* if NO_CANCEL is set, we must still run the work */
+       if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
+                               IO_WQ_WORK_CANCEL) {
                ret = -ECANCELED;
+       }
 
        if (!ret) {
                req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
@@ -3344,26 +3978,13 @@ static void io_wq_submit_work(struct io_wq_work **workptr)
                io_wq_assign_next(workptr, nxt);
 }
 
-static bool io_req_op_valid(int op)
+static int io_req_needs_file(struct io_kiocb *req, int fd)
 {
-       return op >= IORING_OP_NOP && op < IORING_OP_LAST;
-}
-
-static int io_req_needs_file(struct io_kiocb *req)
-{
-       switch (req->opcode) {
-       case IORING_OP_NOP:
-       case IORING_OP_POLL_REMOVE:
-       case IORING_OP_TIMEOUT:
-       case IORING_OP_TIMEOUT_REMOVE:
-       case IORING_OP_ASYNC_CANCEL:
-       case IORING_OP_LINK_TIMEOUT:
+       if (!io_op_defs[req->opcode].needs_file)
                return 0;
-       default:
-               if (io_req_op_valid(req->opcode))
-                       return 1;
-               return -EINVAL;
-       }
+       if (fd == -1 && io_op_defs[req->opcode].fd_non_neg)
+               return 0;
+       return 1;
 }
 
 static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
@@ -3371,8 +3992,8 @@ static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
 {
        struct fixed_file_table *table;
 
-       table = &ctx->file_table[index >> IORING_FILE_TABLE_SHIFT];
-       return table->files[index & IORING_FILE_TABLE_MASK];
+       table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
+       return table->files[index & IORING_FILE_TABLE_MASK];;
 }
 
 static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
@@ -3380,7 +4001,7 @@ static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
 {
        struct io_ring_ctx *ctx = req->ctx;
        unsigned flags;
-       int fd, ret;
+       int fd;
 
        flags = READ_ONCE(sqe->flags);
        fd = READ_ONCE(sqe->fd);
@@ -3388,12 +4009,11 @@ static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
        if (flags & IOSQE_IO_DRAIN)
                req->flags |= REQ_F_IO_DRAIN;
 
-       ret = io_req_needs_file(req);
-       if (ret <= 0)
-               return ret;
+       if (!io_req_needs_file(req, fd))
+               return 0;
 
        if (flags & IOSQE_FIXED_FILE) {
-               if (unlikely(!ctx->file_table ||
+               if (unlikely(!ctx->file_data ||
                    (unsigned) fd >= ctx->nr_user_files))
                        return -EBADF;
                fd = array_index_nospec(fd, ctx->nr_user_files);
@@ -3401,6 +4021,7 @@ static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
                if (!req->file)
                        return -EBADF;
                req->flags |= REQ_F_FIXED_FILE;
+               percpu_ref_get(&ctx->file_data->refs);
        } else {
                if (req->needs_fixed_file)
                        return -EBADF;
@@ -3418,6 +4039,9 @@ static int io_grab_files(struct io_kiocb *req)
        int ret = -EBADF;
        struct io_ring_ctx *ctx = req->ctx;
 
+       if (!req->ring_file)
+               return -EBADF;
+
        rcu_read_lock();
        spin_lock_irq(&ctx->inflight_lock);
        /*
@@ -3588,8 +4212,17 @@ static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
                        req_set_fail_links(req);
                        io_double_put_req(req);
                }
-       } else
+       } else if ((req->flags & REQ_F_FORCE_ASYNC) &&
+                  !io_wq_current_is_worker()) {
+               /*
+                * Never try inline submit of IOSQE_ASYNC is set, go straight
+                * to async execution.
+                */
+               req->work.flags |= IO_WQ_WORK_CONCURRENT;
+               io_queue_async_work(req);
+       } else {
                __io_queue_sqe(req, sqe);
+       }
 }
 
 static inline void io_queue_link_head(struct io_kiocb *req)
@@ -3602,19 +4235,24 @@ static inline void io_queue_link_head(struct io_kiocb *req)
 }
 
 #define SQE_VALID_FLAGS        (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
-                               IOSQE_IO_HARDLINK)
+                               IOSQE_IO_HARDLINK | IOSQE_ASYNC)
 
 static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
                          struct io_submit_state *state, struct io_kiocb **link)
 {
        struct io_ring_ctx *ctx = req->ctx;
+       unsigned int sqe_flags;
        int ret;
 
+       sqe_flags = READ_ONCE(sqe->flags);
+
        /* enforce forwards compatibility on users */
-       if (unlikely(sqe->flags & ~SQE_VALID_FLAGS)) {
+       if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
                ret = -EINVAL;
                goto err_req;
        }
+       if (sqe_flags & IOSQE_ASYNC)
+               req->flags |= REQ_F_FORCE_ASYNC;
 
        ret = io_req_set_file(state, req, sqe);
        if (unlikely(ret)) {
@@ -3632,12 +4270,12 @@ err_req:
         * conditions are true (normal request), then just queue it.
         */
        if (*link) {
-               struct io_kiocb *prev = *link;
+               struct io_kiocb *head = *link;
 
-               if (sqe->flags & IOSQE_IO_DRAIN)
-                       (*link)->flags |= REQ_F_DRAIN_LINK | REQ_F_IO_DRAIN;
+               if (sqe_flags & IOSQE_IO_DRAIN)
+                       head->flags |= REQ_F_DRAIN_LINK | REQ_F_IO_DRAIN;
 
-               if (sqe->flags & IOSQE_IO_HARDLINK)
+               if (sqe_flags & IOSQE_IO_HARDLINK)
                        req->flags |= REQ_F_HARDLINK;
 
                if (io_alloc_async_ctx(req)) {
@@ -3648,14 +4286,20 @@ err_req:
                ret = io_req_defer_prep(req, sqe);
                if (ret) {
                        /* fail even hard links since we don't submit */
-                       prev->flags |= REQ_F_FAIL_LINK;
+                       head->flags |= REQ_F_FAIL_LINK;
                        goto err_req;
                }
-               trace_io_uring_link(ctx, req, prev);
-               list_add_tail(&req->link_list, &prev->link_list);
-       } else if (sqe->flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
+               trace_io_uring_link(ctx, req, head);
+               list_add_tail(&req->link_list, &head->link_list);
+
+               /* last request of a link, enqueue the link */
+               if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) {
+                       io_queue_link_head(head);
+                       *link = NULL;
+               }
+       } else if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
                req->flags |= REQ_F_LINK;
-               if (sqe->flags & IOSQE_IO_HARDLINK)
+               if (sqe_flags & IOSQE_IO_HARDLINK)
                        req->flags |= REQ_F_HARDLINK;
 
                INIT_LIST_HEAD(&req->link_list);
@@ -3768,9 +4412,11 @@ static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
        bool mm_fault = false;
 
        /* if we have a backlog and couldn't flush it all, return BUSY */
-       if (!list_empty(&ctx->cq_overflow_list) &&
-           !io_cqring_overflow_flush(ctx, false))
-               return -EBUSY;
+       if (test_bit(0, &ctx->sq_check_overflow)) {
+               if (!list_empty(&ctx->cq_overflow_list) &&
+                   !io_cqring_overflow_flush(ctx, false))
+                       return -EBUSY;
+       }
 
        if (nr > IO_PLUG_THRESHOLD) {
                io_submit_state_start(&state, nr);
@@ -3780,7 +4426,6 @@ static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
        for (i = 0; i < nr; i++) {
                const struct io_uring_sqe *sqe;
                struct io_kiocb *req;
-               unsigned int sqe_flags;
 
                req = io_get_req(ctx, statep);
                if (unlikely(!req)) {
@@ -3793,7 +4438,16 @@ static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
                        break;
                }
 
-               if (io_req_needs_user(req) && !*mm) {
+               /* will complete beyond this point, count as submitted */
+               submitted++;
+
+               if (unlikely(req->opcode >= IORING_OP_LAST)) {
+                       io_cqring_add_event(req, -EINVAL);
+                       io_double_put_req(req);
+                       break;
+               }
+
+               if (io_op_defs[req->opcode].needs_mm && !*mm) {
                        mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
                        if (!mm_fault) {
                                use_mm(ctx->sqo_mm);
@@ -3801,9 +4455,6 @@ static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
                        }
                }
 
-               submitted++;
-               sqe_flags = sqe->flags;
-
                req->ring_file = ring_file;
                req->ring_fd = ring_fd;
                req->has_user = *mm != NULL;
@@ -3812,14 +4463,6 @@ static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
                trace_io_uring_submit_sqe(ctx, req->user_data, true, async);
                if (!io_submit_sqe(req, sqe, statep, &link))
                        break;
-               /*
-                * If previous wasn't linked and we have a linked command,
-                * that's the end of the chain. Submit the previous link.
-                */
-               if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) && link) {
-                       io_queue_link_head(link);
-                       link = NULL;
-               }
        }
 
        if (link)
@@ -4075,19 +4718,37 @@ static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
 #endif
 }
 
+static void io_file_ref_kill(struct percpu_ref *ref)
+{
+       struct fixed_file_data *data;
+
+       data = container_of(ref, struct fixed_file_data, refs);
+       complete(&data->done);
+}
+
 static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
 {
+       struct fixed_file_data *data = ctx->file_data;
        unsigned nr_tables, i;
 
-       if (!ctx->file_table)
+       if (!data)
                return -ENXIO;
 
+       /* protect against inflight atomic switch, which drops the ref */
+       flush_work(&data->ref_work);
+       percpu_ref_get(&data->refs);
+       percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
+       wait_for_completion(&data->done);
+       percpu_ref_put(&data->refs);
+       percpu_ref_exit(&data->refs);
+
        __io_sqe_files_unregister(ctx);
        nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
        for (i = 0; i < nr_tables; i++)
-               kfree(ctx->file_table[i].files);
-       kfree(ctx->file_table);
-       ctx->file_table = NULL;
+               kfree(data->table[i].files);
+       kfree(data->table);
+       kfree(data);
+       ctx->file_data = NULL;
        ctx->nr_user_files = 0;
        return 0;
 }
@@ -4118,16 +4779,6 @@ static void io_finish_async(struct io_ring_ctx *ctx)
 }
 
 #if defined(CONFIG_UNIX)
-static void io_destruct_skb(struct sk_buff *skb)
-{
-       struct io_ring_ctx *ctx = skb->sk->sk_user_data;
-
-       if (ctx->io_wq)
-               io_wq_flush(ctx->io_wq);
-
-       unix_destruct_scm(skb);
-}
-
 /*
  * Ensure the UNIX gc is aware of our file set, so we are certain that
  * the io_uring can be safely unregistered on process exit, even if we have
@@ -4175,7 +4826,7 @@ static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
                fpl->max = SCM_MAX_FD;
                fpl->count = nr_files;
                UNIXCB(skb).fp = fpl;
-               skb->destructor = io_destruct_skb;
+               skb->destructor = unix_destruct_scm;
                refcount_add(skb->truesize, &sk->sk_wmem_alloc);
                skb_queue_head(&sk->sk_receive_queue, skb);
 
@@ -4237,7 +4888,7 @@ static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
        int i;
 
        for (i = 0; i < nr_tables; i++) {
-               struct fixed_file_table *table = &ctx->file_table[i];
+               struct fixed_file_table *table = &ctx->file_data->table[i];
                unsigned this_files;
 
                this_files = min(nr_files, IORING_MAX_FILES_TABLE);
@@ -4252,36 +4903,159 @@ static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
                return 0;
 
        for (i = 0; i < nr_tables; i++) {
-               struct fixed_file_table *table = &ctx->file_table[i];
+               struct fixed_file_table *table = &ctx->file_data->table[i];
                kfree(table->files);
        }
        return 1;
 }
 
+static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
+{
+#if defined(CONFIG_UNIX)
+       struct sock *sock = ctx->ring_sock->sk;
+       struct sk_buff_head list, *head = &sock->sk_receive_queue;
+       struct sk_buff *skb;
+       int i;
+
+       __skb_queue_head_init(&list);
+
+       /*
+        * Find the skb that holds this file in its SCM_RIGHTS. When found,
+        * remove this entry and rearrange the file array.
+        */
+       skb = skb_dequeue(head);
+       while (skb) {
+               struct scm_fp_list *fp;
+
+               fp = UNIXCB(skb).fp;
+               for (i = 0; i < fp->count; i++) {
+                       int left;
+
+                       if (fp->fp[i] != file)
+                               continue;
+
+                       unix_notinflight(fp->user, fp->fp[i]);
+                       left = fp->count - 1 - i;
+                       if (left) {
+                               memmove(&fp->fp[i], &fp->fp[i + 1],
+                                               left * sizeof(struct file *));
+                       }
+                       fp->count--;
+                       if (!fp->count) {
+                               kfree_skb(skb);
+                               skb = NULL;
+                       } else {
+                               __skb_queue_tail(&list, skb);
+                       }
+                       fput(file);
+                       file = NULL;
+                       break;
+               }
+
+               if (!file)
+                       break;
+
+               __skb_queue_tail(&list, skb);
+
+               skb = skb_dequeue(head);
+       }
+
+       if (skb_peek(&list)) {
+               spin_lock_irq(&head->lock);
+               while ((skb = __skb_dequeue(&list)) != NULL)
+                       __skb_queue_tail(head, skb);
+               spin_unlock_irq(&head->lock);
+       }
+#else
+       fput(file);
+#endif
+}
+
+struct io_file_put {
+       struct llist_node llist;
+       struct file *file;
+       struct completion *done;
+};
+
+static void io_ring_file_ref_switch(struct work_struct *work)
+{
+       struct io_file_put *pfile, *tmp;
+       struct fixed_file_data *data;
+       struct llist_node *node;
+
+       data = container_of(work, struct fixed_file_data, ref_work);
+
+       while ((node = llist_del_all(&data->put_llist)) != NULL) {
+               llist_for_each_entry_safe(pfile, tmp, node, llist) {
+                       io_ring_file_put(data->ctx, pfile->file);
+                       if (pfile->done)
+                               complete(pfile->done);
+                       else
+                               kfree(pfile);
+               }
+       }
+
+       percpu_ref_get(&data->refs);
+       percpu_ref_switch_to_percpu(&data->refs);
+}
+
+static void io_file_data_ref_zero(struct percpu_ref *ref)
+{
+       struct fixed_file_data *data;
+
+       data = container_of(ref, struct fixed_file_data, refs);
+
+       /* we can't safely switch from inside this context, punt to wq */
+       queue_work(system_wq, &data->ref_work);
+}
+
 static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
                                 unsigned nr_args)
 {
        __s32 __user *fds = (__s32 __user *) arg;
        unsigned nr_tables;
+       struct file *file;
        int fd, ret = 0;
        unsigned i;
 
-       if (ctx->file_table)
+       if (ctx->file_data)
                return -EBUSY;
        if (!nr_args)
                return -EINVAL;
        if (nr_args > IORING_MAX_FIXED_FILES)
                return -EMFILE;
 
+       ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
+       if (!ctx->file_data)
+               return -ENOMEM;
+       ctx->file_data->ctx = ctx;
+       init_completion(&ctx->file_data->done);
+
        nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
-       ctx->file_table = kcalloc(nr_tables, sizeof(struct fixed_file_table),
+       ctx->file_data->table = kcalloc(nr_tables,
+                                       sizeof(struct fixed_file_table),
                                        GFP_KERNEL);
-       if (!ctx->file_table)
+       if (!ctx->file_data->table) {
+               kfree(ctx->file_data);
+               ctx->file_data = NULL;
                return -ENOMEM;
+       }
+
+       if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
+                               PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
+               kfree(ctx->file_data->table);
+               kfree(ctx->file_data);
+               ctx->file_data = NULL;
+               return -ENOMEM;
+       }
+       ctx->file_data->put_llist.first = NULL;
+       INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
 
        if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
-               kfree(ctx->file_table);
-               ctx->file_table = NULL;
+               percpu_ref_exit(&ctx->file_data->refs);
+               kfree(ctx->file_data->table);
+               kfree(ctx->file_data);
+               ctx->file_data = NULL;
                return -ENOMEM;
        }
 
@@ -4298,13 +5072,14 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
                        continue;
                }
 
-               table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
+               table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
                index = i & IORING_FILE_TABLE_MASK;
-               table->files[index] = fget(fd);
+               file = fget(fd);
 
                ret = -EBADF;
-               if (!table->files[index])
+               if (!file)
                        break;
+
                /*
                 * Don't allow io_uring instances to be registered. If UNIX
                 * isn't enabled, then this causes a reference cycle and this
@@ -4312,26 +5087,26 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
                 * handle it just fine, but there's still no point in allowing
                 * a ring fd as it doesn't support regular read/write anyway.
                 */
-               if (table->files[index]->f_op == &io_uring_fops) {
-                       fput(table->files[index]);
+               if (file->f_op == &io_uring_fops) {
+                       fput(file);
                        break;
                }
                ret = 0;
+               table->files[index] = file;
        }
 
        if (ret) {
                for (i = 0; i < ctx->nr_user_files; i++) {
-                       struct file *file;
-
                        file = io_file_from_index(ctx, i);
                        if (file)
                                fput(file);
                }
                for (i = 0; i < nr_tables; i++)
-                       kfree(ctx->file_table[i].files);
+                       kfree(ctx->file_data->table[i].files);
 
-               kfree(ctx->file_table);
-               ctx->file_table = NULL;
+               kfree(ctx->file_data->table);
+               kfree(ctx->file_data);
+               ctx->file_data = NULL;
                ctx->nr_user_files = 0;
                return ret;
        }
@@ -4343,69 +5118,6 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
        return ret;
 }
 
-static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index)
-{
-#if defined(CONFIG_UNIX)
-       struct file *file = io_file_from_index(ctx, index);
-       struct sock *sock = ctx->ring_sock->sk;
-       struct sk_buff_head list, *head = &sock->sk_receive_queue;
-       struct sk_buff *skb;
-       int i;
-
-       __skb_queue_head_init(&list);
-
-       /*
-        * Find the skb that holds this file in its SCM_RIGHTS. When found,
-        * remove this entry and rearrange the file array.
-        */
-       skb = skb_dequeue(head);
-       while (skb) {
-               struct scm_fp_list *fp;
-
-               fp = UNIXCB(skb).fp;
-               for (i = 0; i < fp->count; i++) {
-                       int left;
-
-                       if (fp->fp[i] != file)
-                               continue;
-
-                       unix_notinflight(fp->user, fp->fp[i]);
-                       left = fp->count - 1 - i;
-                       if (left) {
-                               memmove(&fp->fp[i], &fp->fp[i + 1],
-                                               left * sizeof(struct file *));
-                       }
-                       fp->count--;
-                       if (!fp->count) {
-                               kfree_skb(skb);
-                               skb = NULL;
-                       } else {
-                               __skb_queue_tail(&list, skb);
-                       }
-                       fput(file);
-                       file = NULL;
-                       break;
-               }
-
-               if (!file)
-                       break;
-
-               __skb_queue_tail(&list, skb);
-
-               skb = skb_dequeue(head);
-       }
-
-       if (skb_peek(&list)) {
-               spin_lock_irq(&head->lock);
-               while ((skb = __skb_dequeue(&list)) != NULL)
-                       __skb_queue_tail(head, skb);
-               spin_unlock_irq(&head->lock);
-       }
-#else
-       fput(io_file_from_index(ctx, index));
-#endif
-}
-
 static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
                                int index)
 {
@@ -4449,27 +5161,65 @@ static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
 #endif
 }
 
-static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
-                              unsigned nr_args)
+static void io_atomic_switch(struct percpu_ref *ref)
 {
-       struct io_uring_files_update up;
+       struct fixed_file_data *data;
+
+       data = container_of(ref, struct fixed_file_data, refs);
+       clear_bit(FFD_F_ATOMIC, &data->state);
+}
+
+static bool io_queue_file_removal(struct fixed_file_data *data,
+                                 struct file *file)
+{
+       struct io_file_put *pfile, pfile_stack;
+       DECLARE_COMPLETION_ONSTACK(done);
+
+       /*
+        * If we fail allocating the struct we need for doing async reomval
+        * of this file, just punt to sync and wait for it.
+        */
+       pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
+       if (!pfile) {
+               pfile = &pfile_stack;
+               pfile->done = &done;
+       }
+
+       pfile->file = file;
+       llist_add(&pfile->llist, &data->put_llist);
+
+       if (pfile == &pfile_stack) {
+               if (!test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
+                       percpu_ref_put(&data->refs);
+                       percpu_ref_switch_to_atomic(&data->refs,
+                                                       io_atomic_switch);
+               }
+               wait_for_completion(&done);
+               flush_work(&data->ref_work);
+               return false;
+       }
+
+       return true;
+}
+
+static int __io_sqe_files_update(struct io_ring_ctx *ctx,
+                                struct io_uring_files_update *up,
+                                unsigned nr_args)
+{
+       struct fixed_file_data *data = ctx->file_data;
+       bool ref_switch = false;
+       struct file *file;
        __s32 __user *fds;
        int fd, i, err;
        __u32 done;
 
-       if (!ctx->file_table)
-               return -ENXIO;
-       if (!nr_args)
-               return -EINVAL;
-       if (copy_from_user(&up, arg, sizeof(up)))
-               return -EFAULT;
-       if (check_add_overflow(up.offset, nr_args, &done))
+       if (check_add_overflow(up->offset, nr_args, &done))
                return -EOVERFLOW;
        if (done > ctx->nr_user_files)
                return -EINVAL;
 
        done = 0;
-       fds = (__s32 __user *) up.fds;
+       fds = u64_to_user_ptr(up->fds);
        while (nr_args) {
                struct fixed_file_table *table;
                unsigned index;
@@ -4479,16 +5229,16 @@ static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
                        err = -EFAULT;
                        break;
                }
-               i = array_index_nospec(up.offset, ctx->nr_user_files);
-               table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
+               i = array_index_nospec(up->offset, ctx->nr_user_files);
+               table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
                index = i & IORING_FILE_TABLE_MASK;
                if (table->files[index]) {
-                       io_sqe_file_unregister(ctx, i);
+                       file = io_file_from_index(ctx, index);
                        table->files[index] = NULL;
+                       if (io_queue_file_removal(data, file))
+                               ref_switch = true;
                }
                if (fd != -1) {
-                       struct file *file;
-
                        file = fget(fd);
                        if (!file) {
                                err = -EBADF;
@@ -4514,11 +5264,32 @@ static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
                }
                nr_args--;
                done++;
-               up.offset++;
+               up->offset++;
+       }
+
+       if (ref_switch && !test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
+               percpu_ref_put(&data->refs);
+               percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
        }
 
        return done ? done : err;
 }
+static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
+                              unsigned nr_args)
+{
+       struct io_uring_files_update up;
+
+       if (!ctx->file_data)
+               return -ENXIO;
+       if (!nr_args)
+               return -EINVAL;
+       if (copy_from_user(&up, arg, sizeof(up)))
+               return -EFAULT;
+       if (up.resv)
+               return -EINVAL;
+
+       return __io_sqe_files_update(ctx, &up, nr_args);
+}
 
 static void io_put_work(struct io_wq_work *work)
 {
@@ -5281,7 +6052,6 @@ static int io_uring_get_fd(struct io_ring_ctx *ctx)
 
 #if defined(CONFIG_UNIX)
        ctx->ring_sock->file = file;
-       ctx->ring_sock->sk->sk_user_data = ctx;
 #endif
        fd_install(ret, file);
        return ret;
@@ -5445,18 +6215,22 @@ static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
        if (percpu_ref_is_dying(&ctx->refs))
                return -ENXIO;
 
-       percpu_ref_kill(&ctx->refs);
+       if (opcode != IORING_UNREGISTER_FILES &&
+           opcode != IORING_REGISTER_FILES_UPDATE) {
+               percpu_ref_kill(&ctx->refs);
 
-       /*
-        * Drop uring mutex before waiting for references to exit. If another
-        * thread is currently inside io_uring_enter() it might need to grab
-        * the uring_lock to make progress. If we hold it here across the drain
-        * wait, then we can deadlock. It's safe to drop the mutex here, since
-        * no new references will come in after we've killed the percpu ref.
-        */
-       mutex_unlock(&ctx->uring_lock);
-       wait_for_completion(&ctx->completions[0]);
-       mutex_lock(&ctx->uring_lock);
+               /*
+                * Drop uring mutex before waiting for references to exit. If
+                * another thread is currently inside io_uring_enter() it might
+                * need to grab the uring_lock to make progress. If we hold it
+                * here across the drain wait, then we can deadlock. It's safe
+                * to drop the mutex here, since no new references will come in
+                * after we've killed the percpu ref.
+                */
+               mutex_unlock(&ctx->uring_lock);
+               wait_for_completion(&ctx->completions[0]);
+               mutex_lock(&ctx->uring_lock);
+       }
 
        switch (opcode) {
        case IORING_REGISTER_BUFFERS:
@@ -5497,9 +6271,13 @@ static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
                break;
        }
 
-       /* bring the ctx back to life */
-       reinit_completion(&ctx->completions[0]);
-       percpu_ref_reinit(&ctx->refs);
+
+       if (opcode != IORING_UNREGISTER_FILES &&
+           opcode != IORING_REGISTER_FILES_UPDATE) {
+               /* bring the ctx back to life */
+               reinit_completion(&ctx->completions[0]);
+               percpu_ref_reinit(&ctx->refs);
+       }
        return ret;
 }
 
@@ -5532,6 +6310,7 @@ out_fput:
 
 static int __init io_uring_init(void)
 {
+       BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
        req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
        return 0;
 };