io_uring: clean file_data access in files_register
[linux-2.6-microblaze.git] / fs / io_uring.c
index fcb4e95..c3ca82f 100644 (file)
@@ -434,13 +434,16 @@ struct io_cancel {
 
 struct io_timeout {
        struct file                     *file;
-       u64                             addr;
-       int                             flags;
        u32                             off;
        u32                             target_seq;
        struct list_head                list;
 };
 
+struct io_timeout_rem {
+       struct file                     *file;
+       u64                             addr;
+};
+
 struct io_rw {
        /* NOTE: kiocb has the file as the first member, so don't do it here */
        struct kiocb                    kiocb;
@@ -644,6 +647,7 @@ struct io_kiocb {
                struct io_sync          sync;
                struct io_cancel        cancel;
                struct io_timeout       timeout;
+               struct io_timeout_rem   timeout_rem;
                struct io_connect       connect;
                struct io_sr_msg        sr_msg;
                struct io_open          open;
@@ -967,13 +971,10 @@ 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 int io_prep_work_files(struct io_kiocb *req);
 static void __io_clean_op(struct io_kiocb *req);
-static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
-                      int fd, struct file **out_file, bool fixed);
-static void __io_queue_sqe(struct io_kiocb *req,
-                          const struct io_uring_sqe *sqe,
-                          struct io_comp_state *cs);
+static struct file *io_file_get(struct io_submit_state *state,
+                               struct io_kiocb *req, int fd, bool fixed);
+static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs);
 static void io_file_put_work(struct work_struct *work);
 
 static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
@@ -1224,16 +1225,28 @@ static bool io_req_clean_work(struct io_kiocb *req)
 static void io_prep_async_work(struct io_kiocb *req)
 {
        const struct io_op_def *def = &io_op_defs[req->opcode];
+       struct io_ring_ctx *ctx = req->ctx;
 
        io_req_init_async(req);
 
        if (req->flags & REQ_F_ISREG) {
-               if (def->hash_reg_file || (req->ctx->flags & IORING_SETUP_IOPOLL))
+               if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL))
                        io_wq_hash_work(&req->work, file_inode(req->file));
        } else {
                if (def->unbound_nonreg_file)
                        req->work.flags |= IO_WQ_WORK_UNBOUND;
        }
+       if (!req->work.files && io_op_defs[req->opcode].file_table &&
+           !(req->flags & REQ_F_NO_FILE_TABLE)) {
+               req->work.files = get_files_struct(current);
+               get_nsproxy(current->nsproxy);
+               req->work.nsproxy = current->nsproxy;
+               req->flags |= REQ_F_INFLIGHT;
+
+               spin_lock_irq(&ctx->inflight_lock);
+               list_add(&req->inflight_entry, &ctx->inflight_list);
+               spin_unlock_irq(&ctx->inflight_lock);
+       }
        if (!req->work.mm && def->needs_mm) {
                mmgrab(current->mm);
                req->work.mm = current->mm;
@@ -1650,10 +1663,8 @@ static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
 static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
                                     struct io_submit_state *state)
 {
-       gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
-       struct io_kiocb *req;
-
        if (!state->free_reqs) {
+               gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
                size_t sz;
                int ret;
 
@@ -1670,14 +1681,11 @@ static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
                                goto fallback;
                        ret = 1;
                }
-               state->free_reqs = ret - 1;
-               req = state->reqs[ret - 1];
-       } else {
-               state->free_reqs--;
-               req = state->reqs[state->free_reqs];
+               state->free_reqs = ret;
        }
 
-       return req;
+       state->free_reqs--;
+       return state->reqs[state->free_reqs];
 fallback:
        return io_get_fallback_req(ctx);
 }
@@ -1893,8 +1901,7 @@ static struct io_kiocb *io_req_find_next(struct io_kiocb *req)
        return __io_req_find_next(req);
 }
 
-static int io_req_task_work_add(struct io_kiocb *req, struct callback_head *cb,
-                               bool twa_signal_ok)
+static int io_req_task_work_add(struct io_kiocb *req, bool twa_signal_ok)
 {
        struct task_struct *tsk = req->task;
        struct io_ring_ctx *ctx = req->ctx;
@@ -1913,7 +1920,7 @@ static int io_req_task_work_add(struct io_kiocb *req, struct callback_head *cb,
        if (!(ctx->flags & IORING_SETUP_SQPOLL) && twa_signal_ok)
                notify = TWA_SIGNAL;
 
-       ret = task_work_add(tsk, cb, notify);
+       ret = task_work_add(tsk, &req->task_work, notify);
        if (!ret)
                wake_up_process(tsk);
 
@@ -1949,7 +1956,7 @@ static void __io_req_task_submit(struct io_kiocb *req)
 
        if (!__io_sq_thread_acquire_mm(ctx)) {
                mutex_lock(&ctx->uring_lock);
-               __io_queue_sqe(req, NULL, NULL);
+               __io_queue_sqe(req, NULL);
                mutex_unlock(&ctx->uring_lock);
        } else {
                __io_req_task_cancel(req, -EFAULT);
@@ -1972,7 +1979,7 @@ static void io_req_task_queue(struct io_kiocb *req)
        init_task_work(&req->task_work, io_req_task_submit);
        percpu_ref_get(&req->ctx->refs);
 
-       ret = io_req_task_work_add(req, &req->task_work, true);
+       ret = io_req_task_work_add(req, true);
        if (unlikely(ret)) {
                struct task_struct *tsk;
 
@@ -2578,7 +2585,6 @@ static struct file *__io_file_get(struct io_submit_state *state, int fd)
        if (state->file) {
                if (state->fd == fd) {
                        state->has_refs--;
-                       state->ios_left--;
                        return state->file;
                }
                __io_state_file_put(state);
@@ -2588,8 +2594,7 @@ static struct file *__io_file_get(struct io_submit_state *state, int fd)
                return NULL;
 
        state->fd = fd;
-       state->ios_left--;
-       state->has_refs = state->ios_left;
+       state->has_refs = state->ios_left - 1;
        return state->file;
 }
 
@@ -2638,8 +2643,7 @@ static bool io_file_supports_async(struct file *file, int rw)
        return file->f_op->write_iter != NULL;
 }
 
-static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
-                     bool force_nonblock)
+static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
 {
        struct io_ring_ctx *ctx = req->ctx;
        struct kiocb *kiocb = &req->rw.kiocb;
@@ -2674,9 +2678,6 @@ static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
        if (kiocb->ki_flags & IOCB_NOWAIT)
                req->flags |= REQ_F_NOWAIT;
 
-       if (force_nonblock)
-               kiocb->ki_flags |= IOCB_NOWAIT;
-
        if (ctx->flags & IORING_SETUP_IOPOLL) {
                if (!(kiocb->ki_flags & IOCB_DIRECT) ||
                    !kiocb->ki_filp->f_op->iopoll)
@@ -3013,7 +3014,7 @@ static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
 
 static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
 {
-       return kiocb->ki_filp->f_mode & FMODE_STREAM ? NULL : &kiocb->ki_pos;
+       return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
 }
 
 /*
@@ -3131,14 +3132,13 @@ static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
        return 0;
 }
 
-static inline int io_rw_prep_async(struct io_kiocb *req, int rw,
-                                  bool force_nonblock)
+static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
 {
        struct io_async_rw *iorw = req->async_data;
        struct iovec *iov = iorw->fast_iov;
        ssize_t ret;
 
-       ret = __io_import_iovec(rw, req, &iov, &iorw->iter, !force_nonblock);
+       ret = __io_import_iovec(rw, req, &iov, &iorw->iter, false);
        if (unlikely(ret < 0))
                return ret;
 
@@ -3149,12 +3149,11 @@ static inline int io_rw_prep_async(struct io_kiocb *req, int rw,
        return 0;
 }
 
-static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
-                       bool force_nonblock)
+static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
 {
        ssize_t ret;
 
-       ret = io_prep_rw(req, sqe, force_nonblock);
+       ret = io_prep_rw(req, sqe);
        if (ret)
                return ret;
 
@@ -3162,9 +3161,9 @@ static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
                return -EBADF;
 
        /* either don't need iovec imported or already have it */
-       if (!req->async_data || req->flags & REQ_F_NEED_CLEANUP)
+       if (!req->async_data)
                return 0;
-       return io_rw_prep_async(req, READ, force_nonblock);
+       return io_rw_prep_async(req, READ);
 }
 
 /*
@@ -3198,7 +3197,7 @@ static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
 
        /* submit ref gets dropped, acquire a new one */
        refcount_inc(&req->refs);
-       ret = io_req_task_work_add(req, &req->task_work, true);
+       ret = io_req_task_work_add(req, true);
        if (unlikely(ret)) {
                struct task_struct *tsk;
 
@@ -3289,6 +3288,9 @@ static int io_read(struct io_kiocb *req, bool force_nonblock,
        /* Ensure we clear previously set non-block flag */
        if (!force_nonblock)
                kiocb->ki_flags &= ~IOCB_NOWAIT;
+       else
+               kiocb->ki_flags |= IOCB_NOWAIT;
+
 
        /* If the file doesn't support async, just async punt */
        no_async = force_nonblock && !io_file_supports_async(req->file, READ);
@@ -3373,12 +3375,11 @@ out_free:
        return ret;
 }
 
-static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
-                        bool force_nonblock)
+static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
 {
        ssize_t ret;
 
-       ret = io_prep_rw(req, sqe, force_nonblock);
+       ret = io_prep_rw(req, sqe);
        if (ret)
                return ret;
 
@@ -3386,9 +3387,9 @@ static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
                return -EBADF;
 
        /* either don't need iovec imported or already have it */
-       if (!req->async_data || req->flags & REQ_F_NEED_CLEANUP)
+       if (!req->async_data)
                return 0;
-       return io_rw_prep_async(req, WRITE, force_nonblock);
+       return io_rw_prep_async(req, WRITE);
 }
 
 static int io_write(struct io_kiocb *req, bool force_nonblock,
@@ -3413,7 +3414,9 @@ static int io_write(struct io_kiocb *req, bool force_nonblock,
 
        /* Ensure we clear previously set non-block flag */
        if (!force_nonblock)
-               req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
+               kiocb->ki_flags &= ~IOCB_NOWAIT;
+       else
+               kiocb->ki_flags |= IOCB_NOWAIT;
 
        /* If the file doesn't support async, just async punt */
        if (force_nonblock && !io_file_supports_async(req->file, WRITE))
@@ -3485,10 +3488,7 @@ static int __io_splice_prep(struct io_kiocb *req,
 {
        struct io_splice* sp = &req->splice;
        unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
-       int ret;
 
-       if (req->flags & REQ_F_NEED_CLEANUP)
-               return 0;
        if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
                return -EINVAL;
 
@@ -3499,10 +3499,10 @@ static int __io_splice_prep(struct io_kiocb *req,
        if (unlikely(sp->flags & ~valid_flags))
                return -EINVAL;
 
-       ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
-                         (sp->flags & SPLICE_F_FD_IN_FIXED));
-       if (ret)
-               return ret;
+       sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in),
+                                 (sp->flags & SPLICE_F_FD_IN_FIXED));
+       if (!sp->file_in)
+               return -EBADF;
        req->flags |= REQ_F_NEED_CLEANUP;
 
        if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
@@ -3698,8 +3698,6 @@ static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
 
        if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
                return -EINVAL;
-       if (req->flags & REQ_F_NEED_CLEANUP)
-               return 0;
        mode = READ_ONCE(sqe->len);
        flags = READ_ONCE(sqe->open_flags);
        req->open.how = build_open_how(flags, mode);
@@ -3714,8 +3712,6 @@ static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
 
        if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
                return -EINVAL;
-       if (req->flags & REQ_F_NEED_CLEANUP)
-               return 0;
        how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
        len = READ_ONCE(sqe->len);
        if (len < OPEN_HOW_SIZE_VER0)
@@ -4223,10 +4219,6 @@ static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
 
        if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
                return 0;
-       /* iovec is already imported */
-       if (req->flags & REQ_F_NEED_CLEANUP)
-               return 0;
-
        ret = io_sendmsg_copy_hdr(req, async_msg);
        if (!ret)
                req->flags |= REQ_F_NEED_CLEANUP;
@@ -4453,10 +4445,6 @@ static int io_recvmsg_prep(struct io_kiocb *req,
 
        if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
                return 0;
-       /* iovec is already imported */
-       if (req->flags & REQ_F_NEED_CLEANUP)
-               return 0;
-
        ret = io_recvmsg_copy_hdr(req, async_msg);
        if (!ret)
                req->flags |= REQ_F_NEED_CLEANUP;
@@ -4775,7 +4763,7 @@ static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
         * of executing it. We can't safely execute it anyway, as we may not
         * have the needed state needed for it anyway.
         */
-       ret = io_req_task_work_add(req, &req->task_work, twa_signal_ok);
+       ret = io_req_task_work_add(req, twa_signal_ok);
        if (unlikely(ret)) {
                struct task_struct *tsk;
 
@@ -5313,16 +5301,10 @@ static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
        unsigned long flags;
 
        spin_lock_irqsave(&ctx->completion_lock, flags);
+       list_del_init(&req->timeout.list);
        atomic_set(&req->ctx->cq_timeouts,
                atomic_read(&req->ctx->cq_timeouts) + 1);
 
-       /*
-        * We could be racing with timeout deletion. If the list is empty,
-        * then timeout lookup already found it and will be handling it.
-        */
-       if (!list_empty(&req->timeout.list))
-               list_del_init(&req->timeout.list);
-
        io_cqring_fill_event(req, -ETIME);
        io_commit_cqring(ctx);
        spin_unlock_irqrestore(&ctx->completion_lock, flags);
@@ -5338,11 +5320,10 @@ static int __io_timeout_cancel(struct io_kiocb *req)
        struct io_timeout_data *io = req->async_data;
        int ret;
 
-       list_del_init(&req->timeout.list);
-
        ret = hrtimer_try_to_cancel(&io->timer);
        if (ret == -1)
                return -EALREADY;
+       list_del_init(&req->timeout.list);
 
        req_set_fail_links(req);
        req->flags |= REQ_F_COMP_LOCKED;
@@ -5376,14 +5357,10 @@ static int io_timeout_remove_prep(struct io_kiocb *req,
                return -EINVAL;
        if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
                return -EINVAL;
-       if (sqe->ioprio || sqe->buf_index || sqe->len)
-               return -EINVAL;
-
-       req->timeout.addr = READ_ONCE(sqe->addr);
-       req->timeout.flags = READ_ONCE(sqe->timeout_flags);
-       if (req->timeout.flags)
+       if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->timeout_flags)
                return -EINVAL;
 
+       req->timeout_rem.addr = READ_ONCE(sqe->addr);
        return 0;
 }
 
@@ -5396,7 +5373,7 @@ static int io_timeout_remove(struct io_kiocb *req)
        int ret;
 
        spin_lock_irq(&ctx->completion_lock);
-       ret = io_timeout_cancel(ctx, req->timeout.addr);
+       ret = io_timeout_cancel(ctx, req->timeout_rem.addr);
 
        io_cqring_fill_event(req, ret);
        io_commit_cqring(ctx);
@@ -5611,120 +5588,86 @@ static int io_files_update(struct io_kiocb *req, bool force_nonblock,
        return 0;
 }
 
-static int io_req_defer_prep(struct io_kiocb *req,
-                            const struct io_uring_sqe *sqe)
+static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
 {
-       ssize_t ret = 0;
-
-       if (!sqe)
-               return 0;
-
-       if (io_alloc_async_data(req))
-               return -EAGAIN;
-       ret = io_prep_work_files(req);
-       if (unlikely(ret))
-               return ret;
-
-       io_prep_async_work(req);
-
        switch (req->opcode) {
        case IORING_OP_NOP:
-               break;
+               return 0;
        case IORING_OP_READV:
        case IORING_OP_READ_FIXED:
        case IORING_OP_READ:
-               ret = io_read_prep(req, sqe, true);
-               break;
+               return io_read_prep(req, sqe);
        case IORING_OP_WRITEV:
        case IORING_OP_WRITE_FIXED:
        case IORING_OP_WRITE:
-               ret = io_write_prep(req, sqe, true);
-               break;
+               return io_write_prep(req, sqe);
        case IORING_OP_POLL_ADD:
-               ret = io_poll_add_prep(req, sqe);
-               break;
+               return io_poll_add_prep(req, sqe);
        case IORING_OP_POLL_REMOVE:
-               ret = io_poll_remove_prep(req, sqe);
-               break;
+               return io_poll_remove_prep(req, sqe);
        case IORING_OP_FSYNC:
-               ret = io_prep_fsync(req, sqe);
-               break;
+               return io_prep_fsync(req, sqe);
        case IORING_OP_SYNC_FILE_RANGE:
-               ret = io_prep_sfr(req, sqe);
-               break;
+               return io_prep_sfr(req, sqe);
        case IORING_OP_SENDMSG:
        case IORING_OP_SEND:
-               ret = io_sendmsg_prep(req, sqe);
-               break;
+               return io_sendmsg_prep(req, sqe);
        case IORING_OP_RECVMSG:
        case IORING_OP_RECV:
-               ret = io_recvmsg_prep(req, sqe);
-               break;
+               return io_recvmsg_prep(req, sqe);
        case IORING_OP_CONNECT:
-               ret = io_connect_prep(req, sqe);
-               break;
+               return io_connect_prep(req, sqe);
        case IORING_OP_TIMEOUT:
-               ret = io_timeout_prep(req, sqe, false);
-               break;
+               return io_timeout_prep(req, sqe, false);
        case IORING_OP_TIMEOUT_REMOVE:
-               ret = io_timeout_remove_prep(req, sqe);
-               break;
+               return io_timeout_remove_prep(req, sqe);
        case IORING_OP_ASYNC_CANCEL:
-               ret = io_async_cancel_prep(req, sqe);
-               break;
+               return io_async_cancel_prep(req, sqe);
        case IORING_OP_LINK_TIMEOUT:
-               ret = io_timeout_prep(req, sqe, true);
-               break;
+               return io_timeout_prep(req, sqe, true);
        case IORING_OP_ACCEPT:
-               ret = io_accept_prep(req, sqe);
-               break;
+               return io_accept_prep(req, sqe);
        case IORING_OP_FALLOCATE:
-               ret = io_fallocate_prep(req, sqe);
-               break;
+               return io_fallocate_prep(req, sqe);
        case IORING_OP_OPENAT:
-               ret = io_openat_prep(req, sqe);
-               break;
+               return io_openat_prep(req, sqe);
        case IORING_OP_CLOSE:
-               ret = io_close_prep(req, sqe);
-               break;
+               return io_close_prep(req, sqe);
        case IORING_OP_FILES_UPDATE:
-               ret = io_files_update_prep(req, sqe);
-               break;
+               return io_files_update_prep(req, sqe);
        case IORING_OP_STATX:
-               ret = io_statx_prep(req, sqe);
-               break;
+               return io_statx_prep(req, sqe);
        case IORING_OP_FADVISE:
-               ret = io_fadvise_prep(req, sqe);
-               break;
+               return io_fadvise_prep(req, sqe);
        case IORING_OP_MADVISE:
-               ret = io_madvise_prep(req, sqe);
-               break;
+               return io_madvise_prep(req, sqe);
        case IORING_OP_OPENAT2:
-               ret = io_openat2_prep(req, sqe);
-               break;
+               return io_openat2_prep(req, sqe);
        case IORING_OP_EPOLL_CTL:
-               ret = io_epoll_ctl_prep(req, sqe);
-               break;
+               return io_epoll_ctl_prep(req, sqe);
        case IORING_OP_SPLICE:
-               ret = io_splice_prep(req, sqe);
-               break;
+               return io_splice_prep(req, sqe);
        case IORING_OP_PROVIDE_BUFFERS:
-               ret = io_provide_buffers_prep(req, sqe);
-               break;
+               return io_provide_buffers_prep(req, sqe);
        case IORING_OP_REMOVE_BUFFERS:
-               ret = io_remove_buffers_prep(req, sqe);
-               break;
+               return io_remove_buffers_prep(req, sqe);
        case IORING_OP_TEE:
-               ret = io_tee_prep(req, sqe);
-               break;
-       default:
-               printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
-                               req->opcode);
-               ret = -EINVAL;
-               break;
+               return io_tee_prep(req, sqe);
        }
 
-       return ret;
+       printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
+                       req->opcode);
+       return-EINVAL;
+}
+
+static int io_req_defer_prep(struct io_kiocb *req,
+                            const struct io_uring_sqe *sqe)
+{
+       if (!sqe)
+               return 0;
+       if (io_alloc_async_data(req))
+               return -EAGAIN;
+       return io_req_prep(req, sqe);
 }
 
 static u32 io_get_sequence(struct io_kiocb *req)
@@ -5855,8 +5798,8 @@ static void __io_clean_op(struct io_kiocb *req)
                io_req_drop_files(req);
 }
 
-static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
-                       bool force_nonblock, struct io_comp_state *cs)
+static int io_issue_sqe(struct io_kiocb *req, bool force_nonblock,
+                       struct io_comp_state *cs)
 {
        struct io_ring_ctx *ctx = req->ctx;
        int ret;
@@ -5868,221 +5811,89 @@ static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
        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)
-                               break;
-               }
                ret = io_read(req, force_nonblock, cs);
                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)
-                               break;
-               }
                ret = io_write(req, force_nonblock, cs);
                break;
        case IORING_OP_FSYNC:
-               if (sqe) {
-                       ret = io_prep_fsync(req, sqe);
-                       if (ret < 0)
-                               break;
-               }
                ret = io_fsync(req, force_nonblock);
                break;
        case IORING_OP_POLL_ADD:
-               if (sqe) {
-                       ret = io_poll_add_prep(req, sqe);
-                       if (ret)
-                               break;
-               }
                ret = io_poll_add(req);
                break;
        case IORING_OP_POLL_REMOVE:
-               if (sqe) {
-                       ret = io_poll_remove_prep(req, sqe);
-                       if (ret < 0)
-                               break;
-               }
                ret = io_poll_remove(req);
                break;
        case IORING_OP_SYNC_FILE_RANGE:
-               if (sqe) {
-                       ret = io_prep_sfr(req, sqe);
-                       if (ret < 0)
-                               break;
-               }
                ret = io_sync_file_range(req, force_nonblock);
                break;
        case IORING_OP_SENDMSG:
+               ret = io_sendmsg(req, force_nonblock, cs);
+               break;
        case IORING_OP_SEND:
-               if (sqe) {
-                       ret = io_sendmsg_prep(req, sqe);
-                       if (ret < 0)
-                               break;
-               }
-               if (req->opcode == IORING_OP_SENDMSG)
-                       ret = io_sendmsg(req, force_nonblock, cs);
-               else
-                       ret = io_send(req, force_nonblock, cs);
+               ret = io_send(req, force_nonblock, cs);
                break;
        case IORING_OP_RECVMSG:
+               ret = io_recvmsg(req, force_nonblock, cs);
+               break;
        case IORING_OP_RECV:
-               if (sqe) {
-                       ret = io_recvmsg_prep(req, sqe);
-                       if (ret)
-                               break;
-               }
-               if (req->opcode == IORING_OP_RECVMSG)
-                       ret = io_recvmsg(req, force_nonblock, cs);
-               else
-                       ret = io_recv(req, force_nonblock, cs);
+               ret = io_recv(req, force_nonblock, cs);
                break;
        case IORING_OP_TIMEOUT:
-               if (sqe) {
-                       ret = io_timeout_prep(req, sqe, false);
-                       if (ret)
-                               break;
-               }
                ret = io_timeout(req);
                break;
        case IORING_OP_TIMEOUT_REMOVE:
-               if (sqe) {
-                       ret = io_timeout_remove_prep(req, sqe);
-                       if (ret)
-                               break;
-               }
                ret = io_timeout_remove(req);
                break;
        case IORING_OP_ACCEPT:
-               if (sqe) {
-                       ret = io_accept_prep(req, sqe);
-                       if (ret)
-                               break;
-               }
                ret = io_accept(req, force_nonblock, cs);
                break;
        case IORING_OP_CONNECT:
-               if (sqe) {
-                       ret = io_connect_prep(req, sqe);
-                       if (ret)
-                               break;
-               }
                ret = io_connect(req, force_nonblock, cs);
                break;
        case IORING_OP_ASYNC_CANCEL:
-               if (sqe) {
-                       ret = io_async_cancel_prep(req, sqe);
-                       if (ret)
-                               break;
-               }
                ret = io_async_cancel(req);
                break;
        case IORING_OP_FALLOCATE:
-               if (sqe) {
-                       ret = io_fallocate_prep(req, sqe);
-                       if (ret)
-                               break;
-               }
                ret = io_fallocate(req, force_nonblock);
                break;
        case IORING_OP_OPENAT:
-               if (sqe) {
-                       ret = io_openat_prep(req, sqe);
-                       if (ret)
-                               break;
-               }
                ret = io_openat(req, force_nonblock);
                break;
        case IORING_OP_CLOSE:
-               if (sqe) {
-                       ret = io_close_prep(req, sqe);
-                       if (ret)
-                               break;
-               }
                ret = io_close(req, force_nonblock, cs);
                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, cs);
                break;
        case IORING_OP_STATX:
-               if (sqe) {
-                       ret = io_statx_prep(req, sqe);
-                       if (ret)
-                               break;
-               }
                ret = io_statx(req, force_nonblock);
                break;
        case IORING_OP_FADVISE:
-               if (sqe) {
-                       ret = io_fadvise_prep(req, sqe);
-                       if (ret)
-                               break;
-               }
                ret = io_fadvise(req, force_nonblock);
                break;
        case IORING_OP_MADVISE:
-               if (sqe) {
-                       ret = io_madvise_prep(req, sqe);
-                       if (ret)
-                               break;
-               }
                ret = io_madvise(req, force_nonblock);
                break;
        case IORING_OP_OPENAT2:
-               if (sqe) {
-                       ret = io_openat2_prep(req, sqe);
-                       if (ret)
-                               break;
-               }
                ret = io_openat2(req, force_nonblock);
                break;
        case IORING_OP_EPOLL_CTL:
-               if (sqe) {
-                       ret = io_epoll_ctl_prep(req, sqe);
-                       if (ret)
-                               break;
-               }
                ret = io_epoll_ctl(req, force_nonblock, cs);
                break;
        case IORING_OP_SPLICE:
-               if (sqe) {
-                       ret = io_splice_prep(req, sqe);
-                       if (ret < 0)
-                               break;
-               }
                ret = io_splice(req, force_nonblock);
                break;
        case IORING_OP_PROVIDE_BUFFERS:
-               if (sqe) {
-                       ret = io_provide_buffers_prep(req, sqe);
-                       if (ret)
-                               break;
-               }
                ret = io_provide_buffers(req, force_nonblock, cs);
                break;
        case IORING_OP_REMOVE_BUFFERS:
-               if (sqe) {
-                       ret = io_remove_buffers_prep(req, sqe);
-                       if (ret)
-                               break;
-               }
                ret = io_remove_buffers(req, force_nonblock, cs);
                break;
        case IORING_OP_TEE:
-               if (sqe) {
-                       ret = io_tee_prep(req, sqe);
-                       if (ret < 0)
-                               break;
-               }
                ret = io_tee(req, force_nonblock);
                break;
        default:
@@ -6128,7 +5939,7 @@ static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work)
 
        if (!ret) {
                do {
-                       ret = io_issue_sqe(req, NULL, false, NULL);
+                       ret = io_issue_sqe(req, false, NULL);
                        /*
                         * We can get EAGAIN for polled IO even though we're
                         * forcing a sync submission from here, since we can't
@@ -6157,16 +5968,15 @@ static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
        return table->files[index & IORING_FILE_TABLE_MASK];
 }
 
-static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
-                       int fd, struct file **out_file, bool fixed)
+static struct file *io_file_get(struct io_submit_state *state,
+                               struct io_kiocb *req, int fd, bool fixed)
 {
        struct io_ring_ctx *ctx = req->ctx;
        struct file *file;
 
        if (fixed) {
-               if (unlikely(!ctx->file_data ||
-                   (unsigned) fd >= ctx->nr_user_files))
-                       return -EBADF;
+               if (unlikely((unsigned int)fd >= ctx->nr_user_files))
+                       return NULL;
                fd = array_index_nospec(fd, ctx->nr_user_files);
                file = io_file_from_index(ctx, fd);
                if (file) {
@@ -6178,11 +5988,7 @@ static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
                file = __io_file_get(state, fd);
        }
 
-       if (file || io_op_defs[req->opcode].needs_file_no_error) {
-               *out_file = file;
-               return 0;
-       }
-       return -EBADF;
+       return file;
 }
 
 static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
@@ -6194,34 +6000,10 @@ static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
        if (unlikely(!fixed && io_async_submit(req->ctx)))
                return -EBADF;
 
-       return io_file_get(state, req, fd, &req->file, fixed);
-}
-
-static int io_grab_files(struct io_kiocb *req)
-{
-       struct io_ring_ctx *ctx = req->ctx;
-
-       io_req_init_async(req);
-
-       if (req->work.files || (req->flags & REQ_F_NO_FILE_TABLE))
-               return 0;
-
-       req->work.files = get_files_struct(current);
-       get_nsproxy(current->nsproxy);
-       req->work.nsproxy = current->nsproxy;
-       req->flags |= REQ_F_INFLIGHT;
-
-       spin_lock_irq(&ctx->inflight_lock);
-       list_add(&req->inflight_entry, &ctx->inflight_list);
-       spin_unlock_irq(&ctx->inflight_lock);
-       return 0;
-}
-
-static inline int io_prep_work_files(struct io_kiocb *req)
-{
-       if (!io_op_defs[req->opcode].file_table)
+       req->file = io_file_get(state, req, fd, fixed);
+       if (req->file || io_op_defs[req->opcode].needs_file_no_error)
                return 0;
-       return io_grab_files(req);
+       return -EBADF;
 }
 
 static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
@@ -6306,8 +6088,7 @@ static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
        return nxt;
 }
 
-static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
-                          struct io_comp_state *cs)
+static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs)
 {
        struct io_kiocb *linked_timeout;
        struct io_kiocb *nxt;
@@ -6327,7 +6108,7 @@ again:
                        old_creds = override_creds(req->work.creds);
        }
 
-       ret = io_issue_sqe(req, sqe, true, cs);
+       ret = io_issue_sqe(req, true, cs);
 
        /*
         * We async punt it if the file wasn't marked NOWAIT, or if the file
@@ -6336,9 +6117,6 @@ again:
        if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
                if (!io_arm_poll_handler(req)) {
 punt:
-                       ret = io_prep_work_files(req);
-                       if (unlikely(ret))
-                               goto err;
                        /*
                         * Queued up for async execution, worker will release
                         * submit reference when the iocb is actually submitted.
@@ -6352,7 +6130,6 @@ punt:
        }
 
        if (unlikely(ret)) {
-err:
                /* un-prep timeout, so it'll be killed as any other linked */
                req->flags &= ~REQ_F_LINK_TIMEOUT;
                req_set_fail_links(req);
@@ -6406,7 +6183,12 @@ fail_req:
                req->work.flags |= IO_WQ_WORK_CONCURRENT;
                io_queue_async_work(req);
        } else {
-               __io_queue_sqe(req, sqe, cs);
+               if (sqe) {
+                       ret = io_req_prep(req, sqe);
+                       if (unlikely(ret))
+                               goto fail_req;
+               }
+               __io_queue_sqe(req, cs);
        }
 }
 
@@ -6593,7 +6375,7 @@ static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
                       struct io_submit_state *state)
 {
        unsigned int sqe_flags;
-       int id;
+       int id, ret;
 
        req->opcode = READ_ONCE(sqe->opcode);
        req->user_data = READ_ONCE(sqe->user_data);
@@ -6604,8 +6386,6 @@ static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
        /* one is dropped after submission, the other at completion */
        refcount_set(&req->refs, 2);
        req->task = current;
-       get_task_struct(req->task);
-       atomic_long_inc(&req->task->io_uring->req_issue);
        req->result = 0;
 
        if (unlikely(req->opcode >= IORING_OP_LAST))
@@ -6641,7 +6421,9 @@ static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
        if (!io_op_defs[req->opcode].needs_file)
                return 0;
 
-       return io_req_set_file(state, req, READ_ONCE(sqe->fd));
+       ret = io_req_set_file(state, req, READ_ONCE(sqe->fd));
+       state->ios_left--;
+       return ret;
 }
 
 static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
@@ -6663,6 +6445,9 @@ static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
        if (!percpu_ref_tryget_many(&ctx->refs, nr))
                return -EAGAIN;
 
+       atomic_long_add(nr, &current->io_uring->req_issue);
+       refcount_add(nr, &current->usage);
+
        io_submit_state_start(&state, ctx, nr);
 
        for (i = 0; i < nr; i++) {
@@ -6681,12 +6466,11 @@ static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
                                submitted = -EAGAIN;
                        break;
                }
-
-               err = io_init_req(ctx, req, sqe, &state);
                io_consume_sqe(ctx);
                /* will complete beyond this point, count as submitted */
                submitted++;
 
+               err = io_init_req(ctx, req, sqe, &state);
                if (unlikely(err)) {
 fail_req:
                        io_put_req(req);
@@ -6705,6 +6489,8 @@ fail_req:
                int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
 
                percpu_ref_put_many(&ctx->refs, nr - ref_used);
+               atomic_long_sub(nr - ref_used, &current->io_uring->req_issue);
+               put_task_struct_many(current, nr - ref_used);
        }
        if (link)
                io_queue_link_head(link, &state.comp);
@@ -7313,13 +7099,13 @@ static int io_sqe_files_scm(struct io_ring_ctx *ctx)
 }
 #endif
 
-static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
-                                   unsigned nr_files)
+static int io_sqe_alloc_file_tables(struct fixed_file_data *file_data,
+                                   unsigned nr_tables, unsigned nr_files)
 {
        int i;
 
        for (i = 0; i < nr_tables; i++) {
-               struct fixed_file_table *table = &ctx->file_data->table[i];
+               struct fixed_file_table *table = &file_data->table[i];
                unsigned this_files;
 
                this_files = min(nr_files, IORING_MAX_FILES_TABLE);
@@ -7334,7 +7120,7 @@ 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_data->table[i];
+               struct fixed_file_table *table = &file_data->table[i];
                kfree(table->files);
        }
        return 1;
@@ -7501,6 +7287,7 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
        int fd, ret = 0;
        unsigned i;
        struct fixed_file_ref_node *ref_node;
+       struct fixed_file_data *file_data;
 
        if (ctx->file_data)
                return -EBUSY;
@@ -7509,37 +7296,33 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
        if (nr_args > IORING_MAX_FIXED_FILES)
                return -EMFILE;
 
-       ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
-       if (!ctx->file_data)
+       file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
+       if (!file_data)
                return -ENOMEM;
-       ctx->file_data->ctx = ctx;
-       init_completion(&ctx->file_data->done);
-       INIT_LIST_HEAD(&ctx->file_data->ref_list);
-       spin_lock_init(&ctx->file_data->lock);
+       file_data->ctx = ctx;
+       init_completion(&file_data->done);
+       INIT_LIST_HEAD(&file_data->ref_list);
+       spin_lock_init(&file_data->lock);
 
        nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
-       ctx->file_data->table = kcalloc(nr_tables,
-                                       sizeof(struct fixed_file_table),
-                                       GFP_KERNEL);
-       if (!ctx->file_data->table) {
-               kfree(ctx->file_data);
-               ctx->file_data = NULL;
+       file_data->table = kcalloc(nr_tables, sizeof(file_data->table),
+                                  GFP_KERNEL);
+       if (!file_data->table) {
+               kfree(file_data);
                return -ENOMEM;
        }
 
-       if (percpu_ref_init(&ctx->file_data->refs, io_file_ref_kill,
+       if (percpu_ref_init(&file_data->refs, io_file_ref_kill,
                                PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
-               kfree(ctx->file_data->table);
-               kfree(ctx->file_data);
-               ctx->file_data = NULL;
+               kfree(file_data->table);
+               kfree(file_data);
                return -ENOMEM;
        }
 
-       if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
-               percpu_ref_exit(&ctx->file_data->refs);
-               kfree(ctx->file_data->table);
-               kfree(ctx->file_data);
-               ctx->file_data = NULL;
+       if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args)) {
+               percpu_ref_exit(&file_data->refs);
+               kfree(file_data->table);
+               kfree(file_data);
                return -ENOMEM;
        }
 
@@ -7556,7 +7339,7 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
                        continue;
                }
 
-               table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
+               table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT];
                index = i & IORING_FILE_TABLE_MASK;
                file = fget(fd);
 
@@ -7586,16 +7369,16 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
                                fput(file);
                }
                for (i = 0; i < nr_tables; i++)
-                       kfree(ctx->file_data->table[i].files);
+                       kfree(file_data->table[i].files);
 
-               percpu_ref_exit(&ctx->file_data->refs);
-               kfree(ctx->file_data->table);
-               kfree(ctx->file_data);
-               ctx->file_data = NULL;
+               percpu_ref_exit(&file_data->refs);
+               kfree(file_data->table);
+               kfree(file_data);
                ctx->nr_user_files = 0;
                return ret;
        }
 
+       ctx->file_data = file_data;
        ret = io_sqe_files_scm(ctx);
        if (ret) {
                io_sqe_files_unregister(ctx);
@@ -7608,11 +7391,11 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
                return PTR_ERR(ref_node);
        }
 
-       ctx->file_data->cur_refs = &ref_node->refs;
-       spin_lock(&ctx->file_data->lock);
-       list_add(&ref_node->node, &ctx->file_data->ref_list);
-       spin_unlock(&ctx->file_data->lock);
-       percpu_ref_get(&ctx->file_data->refs);
+       file_data->cur_refs = &ref_node->refs;
+       spin_lock(&file_data->lock);
+       list_add(&ref_node->node, &file_data->ref_list);
+       spin_unlock(&file_data->lock);
+       percpu_ref_get(&file_data->refs);
        return ret;
 }
 
@@ -7860,7 +7643,6 @@ void __io_uring_free(struct task_struct *tsk)
        struct io_uring_task *tctx = tsk->io_uring;
 
        WARN_ON_ONCE(!xa_empty(&tctx->xa));
-       xa_destroy(&tctx->xa);
        kfree(tctx);
        tsk->io_uring = NULL;
 }
@@ -8759,27 +8541,24 @@ static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
  */
 static int io_uring_add_task_file(struct file *file)
 {
-       if (unlikely(!current->io_uring)) {
+       struct io_uring_task *tctx = current->io_uring;
+
+       if (unlikely(!tctx)) {
                int ret;
 
                ret = io_uring_alloc_task_context(current);
                if (unlikely(ret))
                        return ret;
+               tctx = current->io_uring;
        }
-       if (current->io_uring->last != file) {
-               XA_STATE(xas, &current->io_uring->xa, (unsigned long) file);
-               void *old;
+       if (tctx->last != file) {
+               void *old = xa_load(&tctx->xa, (unsigned long)file);
 
-               rcu_read_lock();
-               old = xas_load(&xas);
-               if (old != file) {
+               if (!old) {
                        get_file(file);
-                       xas_lock(&xas);
-                       xas_store(&xas, file);
-                       xas_unlock(&xas);
+                       xa_store(&tctx->xa, (unsigned long)file, file, GFP_KERNEL);
                }
-               rcu_read_unlock();
-               current->io_uring->last = file;
+               tctx->last = file;
        }
 
        return 0;
@@ -8791,27 +8570,17 @@ static int io_uring_add_task_file(struct file *file)
 static void io_uring_del_task_file(struct file *file)
 {
        struct io_uring_task *tctx = current->io_uring;
-       XA_STATE(xas, &tctx->xa, (unsigned long) file);
 
        if (tctx->last == file)
                tctx->last = NULL;
-
-       xas_lock(&xas);
-       file = xas_store(&xas, NULL);
-       xas_unlock(&xas);
-
+       file = xa_erase(&tctx->xa, (unsigned long)file);
        if (file)
                fput(file);
 }
 
 static void __io_uring_attempt_task_drop(struct file *file)
 {
-       XA_STATE(xas, &current->io_uring->xa, (unsigned long) file);
-       struct file *old;
-
-       rcu_read_lock();
-       old = xas_load(&xas);
-       rcu_read_unlock();
+       struct file *old = xa_load(&current->io_uring->xa, (unsigned long)file);
 
        if (old == file)
                io_uring_del_task_file(file);
@@ -8838,28 +8607,19 @@ static void io_uring_attempt_task_drop(struct file *file, bool exiting)
 void __io_uring_files_cancel(struct files_struct *files)
 {
        struct io_uring_task *tctx = current->io_uring;
-       XA_STATE(xas, &tctx->xa, 0);
+       struct file *file;
+       unsigned long index;
 
        /* make sure overflow events are dropped */
        tctx->in_idle = true;
 
-       do {
-               struct io_ring_ctx *ctx;
-               struct file *file;
-
-               xas_lock(&xas);
-               file = xas_next_entry(&xas, ULONG_MAX);
-               xas_unlock(&xas);
-
-               if (!file)
-                       break;
-
-               ctx = file->private_data;
+       xa_for_each(&tctx->xa, index, file) {
+               struct io_ring_ctx *ctx = file->private_data;
 
                io_uring_cancel_task_requests(ctx, files);
                if (files)
                        io_uring_del_task_file(file);
-       } while (1);
+       }
 }
 
 static inline bool io_uring_task_idle(struct io_uring_task *tctx)
@@ -9684,15 +9444,15 @@ static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
                        ret = wait_for_completion_interruptible(&ctx->ref_comp);
                        if (!ret)
                                break;
-                       if (io_run_task_work_sig() > 0)
-                               continue;
+                       ret = io_run_task_work_sig();
+                       if (ret < 0)
+                               break;
                } while (1);
 
                mutex_lock(&ctx->uring_lock);
 
                if (ret) {
                        percpu_ref_resurrect(&ctx->refs);
-                       ret = -EINTR;
                        goto out_quiesce;
                }
        }