io_uring: fix io_try_cancel_userdata race for iowq
[linux-2.6-microblaze.git] / fs / io_uring.c
index 097ba59..6859438 100644 (file)
 #define IORING_MAX_CQ_ENTRIES  (2 * IORING_MAX_ENTRIES)
 #define IORING_SQPOLL_CAP_ENTRIES_VALUE 8
 
-/* 512 entries per page on 64-bit archs, 64 pages max */
+/* only define max */
 #define IORING_MAX_FIXED_FILES (1U << 15)
 #define IORING_MAX_RESTRICTIONS        (IORING_RESTRICTION_LAST + \
                                 IORING_REGISTER_LAST + IORING_OP_LAST)
 
-#define IO_RSRC_TAG_TABLE_SHIFT        9
+#define IO_RSRC_TAG_TABLE_SHIFT        (PAGE_SHIFT - 3)
 #define IO_RSRC_TAG_TABLE_MAX  (1U << IO_RSRC_TAG_TABLE_SHIFT)
 #define IO_RSRC_TAG_TABLE_MASK (IO_RSRC_TAG_TABLE_MAX - 1)
 
@@ -775,7 +775,7 @@ struct async_poll {
        struct io_poll_iocb     *double_poll;
 };
 
-typedef void (*io_req_tw_func_t)(struct io_kiocb *req);
+typedef void (*io_req_tw_func_t)(struct io_kiocb *req, bool *locked);
 
 struct io_task_work {
        union {
@@ -1034,6 +1034,9 @@ static const struct io_op_def io_op_defs[] = {
        [IORING_OP_UNLINKAT] = {},
 };
 
+/* requests with any of those set should undergo io_disarm_next() */
+#define IO_DISARM_MASK (REQ_F_ARM_LTIMEOUT | REQ_F_LINK_TIMEOUT | REQ_F_FAIL)
+
 static bool io_disarm_next(struct io_kiocb *req);
 static void io_uring_del_tctx_node(unsigned long index);
 static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
@@ -1077,6 +1080,14 @@ struct sock *io_uring_get_socket(struct file *file)
 }
 EXPORT_SYMBOL(io_uring_get_socket);
 
+static inline void io_tw_lock(struct io_ring_ctx *ctx, bool *locked)
+{
+       if (!*locked) {
+               mutex_lock(&ctx->uring_lock);
+               *locked = true;
+       }
+}
+
 #define io_for_each_link(pos, head) \
        for (pos = (head); pos; pos = pos->link)
 
@@ -1190,11 +1201,19 @@ static void io_fallback_req_func(struct work_struct *work)
                                                fallback_work.work);
        struct llist_node *node = llist_del_all(&ctx->fallback_llist);
        struct io_kiocb *req, *tmp;
+       bool locked = false;
 
        percpu_ref_get(&ctx->refs);
        llist_for_each_entry_safe(req, tmp, node, io_task_work.fallback_node)
-               req->io_task_work.func(req);
+               req->io_task_work.func(req, &locked);
+
+       if (locked) {
+               if (ctx->submit_state.compl_nr)
+                       io_submit_flush_completions(ctx);
+               mutex_unlock(&ctx->uring_lock);
+       }
        percpu_ref_put(&ctx->refs);
+
 }
 
 static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
@@ -1303,8 +1322,16 @@ static void io_req_track_inflight(struct io_kiocb *req)
        }
 }
 
+static inline void io_unprep_linked_timeout(struct io_kiocb *req)
+{
+       req->flags &= ~REQ_F_LINK_TIMEOUT;
+}
+
 static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req)
 {
+       if (WARN_ON_ONCE(!req->link))
+               return NULL;
+
        req->flags &= ~REQ_F_ARM_LTIMEOUT;
        req->flags |= REQ_F_LINK_TIMEOUT;
 
@@ -1370,12 +1397,15 @@ static void io_prep_async_link(struct io_kiocb *req)
        }
 }
 
-static void io_queue_async_work(struct io_kiocb *req)
+static void io_queue_async_work(struct io_kiocb *req, bool *locked)
 {
        struct io_ring_ctx *ctx = req->ctx;
        struct io_kiocb *link = io_prep_linked_timeout(req);
        struct io_uring_task *tctx = req->task->io_uring;
 
+       /* must not take the lock, NULL it as a precaution */
+       locked = NULL;
+
        BUG_ON(!tctx);
        BUG_ON(!tctx->io_wq);
 
@@ -1515,6 +1545,13 @@ static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
        return !ctx->eventfd_async || io_wq_current_is_worker();
 }
 
+/*
+ * This should only get called when at least one event has been posted.
+ * Some applications rely on the eventfd notification count only changing
+ * IFF a new CQE has been added to the CQ ring. There's no depedency on
+ * 1:1 relationship between how many times this function is called (and
+ * hence the eventfd count) and number of CQEs posted to the CQ ring.
+ */
 static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
 {
        /*
@@ -1612,10 +1649,14 @@ static inline void io_put_task(struct task_struct *task, int nr)
 {
        struct io_uring_task *tctx = task->io_uring;
 
-       percpu_counter_sub(&tctx->inflight, nr);
-       if (unlikely(atomic_read(&tctx->in_idle)))
-               wake_up(&tctx->wait);
-       put_task_struct_many(task, nr);
+       if (likely(task == current)) {
+               tctx->cached_refs += nr;
+       } else {
+               percpu_counter_sub(&tctx->inflight, nr);
+               if (unlikely(atomic_read(&tctx->in_idle)))
+                       wake_up(&tctx->wait);
+               put_task_struct_many(task, nr);
+       }
 }
 
 static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data,
@@ -1688,7 +1729,7 @@ static void io_req_complete_post(struct io_kiocb *req, long res,
         */
        if (req_ref_put_and_test(req)) {
                if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
-                       if (req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_FAIL))
+                       if (req->flags & IO_DISARM_MASK)
                                io_disarm_next(req);
                        if (req->link) {
                                io_req_task_queue(req->link);
@@ -1926,7 +1967,18 @@ static bool io_disarm_next(struct io_kiocb *req)
 {
        bool posted = false;
 
-       if (likely(req->flags & REQ_F_LINK_TIMEOUT)) {
+       if (req->flags & REQ_F_ARM_LTIMEOUT) {
+               struct io_kiocb *link = req->link;
+
+               req->flags &= ~REQ_F_ARM_LTIMEOUT;
+               if (link && link->opcode == IORING_OP_LINK_TIMEOUT) {
+                       io_remove_next_linked(req);
+                       io_cqring_fill_event(link->ctx, link->user_data,
+                                            -ECANCELED, 0);
+                       io_put_req_deferred(link);
+                       posted = true;
+               }
+       } else if (req->flags & REQ_F_LINK_TIMEOUT) {
                struct io_ring_ctx *ctx = req->ctx;
 
                spin_lock_irq(&ctx->timeout_lock);
@@ -1951,7 +2003,7 @@ static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
         * dependencies to the next request. In case of failure, fail the rest
         * of the chain.
         */
-       if (req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_FAIL)) {
+       if (req->flags & IO_DISARM_MASK) {
                struct io_ring_ctx *ctx = req->ctx;
                bool posted;
 
@@ -1975,20 +2027,22 @@ static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req)
        return __io_req_find_next(req);
 }
 
-static void ctx_flush_and_put(struct io_ring_ctx *ctx)
+static void ctx_flush_and_put(struct io_ring_ctx *ctx, bool *locked)
 {
        if (!ctx)
                return;
-       if (ctx->submit_state.compl_nr) {
-               mutex_lock(&ctx->uring_lock);
-               io_submit_flush_completions(ctx);
+       if (*locked) {
+               if (ctx->submit_state.compl_nr)
+                       io_submit_flush_completions(ctx);
                mutex_unlock(&ctx->uring_lock);
+               *locked = false;
        }
        percpu_ref_put(&ctx->refs);
 }
 
 static void tctx_task_work(struct callback_head *cb)
 {
+       bool locked = false;
        struct io_ring_ctx *ctx = NULL;
        struct io_uring_task *tctx = container_of(cb, struct io_uring_task,
                                                  task_work);
@@ -2011,18 +2065,20 @@ static void tctx_task_work(struct callback_head *cb)
                                                            io_task_work.node);
 
                        if (req->ctx != ctx) {
-                               ctx_flush_and_put(ctx);
+                               ctx_flush_and_put(ctx, &locked);
                                ctx = req->ctx;
+                               /* if not contended, grab and improve batching */
+                               locked = mutex_trylock(&ctx->uring_lock);
                                percpu_ref_get(&ctx->refs);
                        }
-                       req->io_task_work.func(req);
+                       req->io_task_work.func(req, &locked);
                        node = next;
                } while (node);
 
                cond_resched();
        }
 
-       ctx_flush_and_put(ctx);
+       ctx_flush_and_put(ctx, &locked);
 }
 
 static void io_req_task_work_add(struct io_kiocb *req)
@@ -2074,27 +2130,26 @@ static void io_req_task_work_add(struct io_kiocb *req)
        }
 }
 
-static void io_req_task_cancel(struct io_kiocb *req)
+static void io_req_task_cancel(struct io_kiocb *req, bool *locked)
 {
        struct io_ring_ctx *ctx = req->ctx;
 
        /* ctx is guaranteed to stay alive while we hold uring_lock */
-       mutex_lock(&ctx->uring_lock);
+       io_tw_lock(ctx, locked);
        io_req_complete_failed(req, req->result);
-       mutex_unlock(&ctx->uring_lock);
 }
 
-static void io_req_task_submit(struct io_kiocb *req)
+static void io_req_task_submit(struct io_kiocb *req, bool *locked)
 {
        struct io_ring_ctx *ctx = req->ctx;
 
        /* ctx stays valid until unlock, even if we drop all ours ctx->refs */
-       mutex_lock(&ctx->uring_lock);
+       io_tw_lock(ctx, locked);
+       /* req->task == current here, checking PF_EXITING is safe */
        if (likely(!(req->task->flags & PF_EXITING)))
                __io_queue_sqe(req);
        else
                io_req_complete_failed(req, -EFAULT);
-       mutex_unlock(&ctx->uring_lock);
 }
 
 static void io_req_task_queue_fail(struct io_kiocb *req, int ret)
@@ -2130,6 +2185,11 @@ static void io_free_req(struct io_kiocb *req)
        __io_free_req(req);
 }
 
+static void io_free_req_work(struct io_kiocb *req, bool *locked)
+{
+       io_free_req(req);
+}
+
 struct req_batch {
        struct task_struct      *task;
        int                     task_refs;
@@ -2148,9 +2208,7 @@ static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
 {
        if (rb->ctx_refs)
                percpu_ref_put_many(&ctx->refs, rb->ctx_refs);
-       if (rb->task == current)
-               current->io_uring->cached_refs += rb->task_refs;
-       else if (rb->task)
+       if (rb->task)
                io_put_task(rb->task, rb->task_refs);
 }
 
@@ -2229,7 +2287,7 @@ static inline void io_put_req(struct io_kiocb *req)
 static inline void io_put_req_deferred(struct io_kiocb *req)
 {
        if (req_ref_put_and_test(req)) {
-               req->io_task_work.func = io_free_req;
+               req->io_task_work.func = io_free_req_work;
                io_req_task_work_add(req);
        }
 }
@@ -2264,6 +2322,8 @@ static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
 {
        struct io_buffer *kbuf;
 
+       if (likely(!(req->flags & REQ_F_BUFFER_SELECTED)))
+               return 0;
        kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
        return io_put_kbuf(req, kbuf);
 }
@@ -2293,8 +2353,6 @@ static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
 
        io_init_req_batch(&rb);
        while (!list_empty(done)) {
-               int cflags = 0;
-
                req = list_first_entry(done, struct io_kiocb, inflight_entry);
                list_del(&req->inflight_entry);
 
@@ -2305,10 +2363,8 @@ static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
                        continue;
                }
 
-               if (req->flags & REQ_F_BUFFER_SELECTED)
-                       cflags = io_put_rw_kbuf(req);
-
-               __io_cqring_fill_event(ctx, req->user_data, req->result, cflags);
+               __io_cqring_fill_event(ctx, req->user_data, req->result,
+                                       io_put_rw_kbuf(req));
                (*nr_events)++;
 
                if (req_ref_put_and_test(req))
@@ -2526,13 +2582,22 @@ static bool __io_complete_rw_common(struct io_kiocb *req, long res)
        return false;
 }
 
-static void io_req_task_complete(struct io_kiocb *req)
+static void io_req_task_complete(struct io_kiocb *req, bool *locked)
 {
-       int cflags = 0;
+       unsigned int cflags = io_put_rw_kbuf(req);
+       long res = req->result;
 
-       if (req->flags & REQ_F_BUFFER_SELECTED)
-               cflags = io_put_rw_kbuf(req);
-       __io_req_complete(req, 0, req->result, cflags);
+       if (*locked) {
+               struct io_ring_ctx *ctx = req->ctx;
+               struct io_submit_state *state = &ctx->submit_state;
+
+               io_req_complete_state(req, res, cflags);
+               state->compl_reqs[state->compl_nr++] = req;
+               if (state->compl_nr == ARRAY_SIZE(state->compl_reqs))
+                       io_submit_flush_completions(ctx);
+       } else {
+               io_req_complete_post(req, res, cflags);
+       }
 }
 
 static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
@@ -2540,7 +2605,7 @@ static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
 {
        if (__io_complete_rw_common(req, res))
                return;
-       io_req_task_complete(req);
+       __io_req_complete(req, 0, req->result, io_put_rw_kbuf(req));
 }
 
 static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
@@ -2800,12 +2865,9 @@ static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
                if (io_resubmit_prep(req)) {
                        io_req_task_queue_reissue(req);
                } else {
-                       int cflags = 0;
-
                        req_set_fail(req);
-                       if (req->flags & REQ_F_BUFFER_SELECTED)
-                               cflags = io_put_rw_kbuf(req);
-                       __io_req_complete(req, issue_flags, ret, cflags);
+                       __io_req_complete(req, issue_flags, ret,
+                                         io_put_rw_kbuf(req));
                }
        }
 }
@@ -3487,7 +3549,7 @@ static int io_renameat_prep(struct io_kiocb *req,
 
        if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
                return -EINVAL;
-       if (sqe->ioprio || sqe->buf_index)
+       if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
                return -EINVAL;
        if (unlikely(req->flags & REQ_F_FIXED_FILE))
                return -EBADF;
@@ -3538,7 +3600,8 @@ static int io_unlinkat_prep(struct io_kiocb *req,
 
        if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
                return -EINVAL;
-       if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
+       if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
+           sqe->splice_fd_in)
                return -EINVAL;
        if (unlikely(req->flags & REQ_F_FIXED_FILE))
                return -EBADF;
@@ -3584,8 +3647,8 @@ static int io_shutdown_prep(struct io_kiocb *req,
 #if defined(CONFIG_NET)
        if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
                return -EINVAL;
-       if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags ||
-           sqe->buf_index)
+       if (unlikely(sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags ||
+                    sqe->buf_index || sqe->splice_fd_in))
                return -EINVAL;
 
        req->shutdown.how = READ_ONCE(sqe->len);
@@ -3733,7 +3796,8 @@ static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
 
        if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
                return -EINVAL;
-       if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
+       if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index ||
+                    sqe->splice_fd_in))
                return -EINVAL;
 
        req->sync.flags = READ_ONCE(sqe->fsync_flags);
@@ -3766,7 +3830,8 @@ static int io_fsync(struct io_kiocb *req, unsigned int issue_flags)
 static int io_fallocate_prep(struct io_kiocb *req,
                             const struct io_uring_sqe *sqe)
 {
-       if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
+       if (sqe->ioprio || sqe->buf_index || sqe->rw_flags ||
+           sqe->splice_fd_in)
                return -EINVAL;
        if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
                return -EINVAL;
@@ -3799,7 +3864,7 @@ static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe
 
        if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
                return -EINVAL;
-       if (unlikely(sqe->ioprio || sqe->buf_index))
+       if (unlikely(sqe->ioprio || sqe->buf_index || sqe->splice_fd_in))
                return -EINVAL;
        if (unlikely(req->flags & REQ_F_FIXED_FILE))
                return -EBADF;
@@ -3918,7 +3983,8 @@ static int io_remove_buffers_prep(struct io_kiocb *req,
        struct io_provide_buf *p = &req->pbuf;
        u64 tmp;
 
-       if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
+       if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off ||
+           sqe->splice_fd_in)
                return -EINVAL;
 
        tmp = READ_ONCE(sqe->fd);
@@ -3989,7 +4055,7 @@ static int io_provide_buffers_prep(struct io_kiocb *req,
        struct io_provide_buf *p = &req->pbuf;
        u64 tmp;
 
-       if (sqe->ioprio || sqe->rw_flags)
+       if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in)
                return -EINVAL;
 
        tmp = READ_ONCE(sqe->fd);
@@ -4076,7 +4142,7 @@ static int io_epoll_ctl_prep(struct io_kiocb *req,
                             const struct io_uring_sqe *sqe)
 {
 #if defined(CONFIG_EPOLL)
-       if (sqe->ioprio || sqe->buf_index)
+       if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
                return -EINVAL;
        if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
                return -EINVAL;
@@ -4122,7 +4188,7 @@ static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags)
 static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
 {
 #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
-       if (sqe->ioprio || sqe->buf_index || sqe->off)
+       if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->splice_fd_in)
                return -EINVAL;
        if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
                return -EINVAL;
@@ -4157,7 +4223,7 @@ static int io_madvise(struct io_kiocb *req, unsigned int issue_flags)
 
 static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
 {
-       if (sqe->ioprio || sqe->buf_index || sqe->addr)
+       if (sqe->ioprio || sqe->buf_index || sqe->addr || sqe->splice_fd_in)
                return -EINVAL;
        if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
                return -EINVAL;
@@ -4195,7 +4261,7 @@ static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
 {
        if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
                return -EINVAL;
-       if (sqe->ioprio || sqe->buf_index)
+       if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
                return -EINVAL;
        if (req->flags & REQ_F_FIXED_FILE)
                return -EBADF;
@@ -4231,7 +4297,7 @@ static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
        if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
                return -EINVAL;
        if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
-           sqe->rw_flags || sqe->buf_index)
+           sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in)
                return -EINVAL;
        if (req->flags & REQ_F_FIXED_FILE)
                return -EBADF;
@@ -4292,7 +4358,8 @@ static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
 
        if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
                return -EINVAL;
-       if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
+       if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index ||
+                    sqe->splice_fd_in))
                return -EINVAL;
 
        req->sync.off = READ_ONCE(sqe->off);
@@ -4719,7 +4786,7 @@ static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
 
        if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
                return -EINVAL;
-       if (sqe->ioprio || sqe->len || sqe->buf_index)
+       if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->splice_fd_in)
                return -EINVAL;
 
        accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
@@ -4767,7 +4834,8 @@ static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
 
        if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
                return -EINVAL;
-       if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
+       if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags ||
+           sqe->splice_fd_in)
                return -EINVAL;
 
        conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
@@ -4880,6 +4948,7 @@ static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
 {
        struct io_ring_ctx *ctx = req->ctx;
 
+       /* req->task == current here, checking PF_EXITING is safe */
        if (unlikely(req->task->flags & PF_EXITING))
                WRITE_ONCE(poll->canceled, true);
 
@@ -4958,7 +5027,7 @@ static bool io_poll_complete(struct io_kiocb *req, __poll_t mask)
        return !(flags & IORING_CQE_F_MORE);
 }
 
-static void io_poll_task_func(struct io_kiocb *req)
+static void io_poll_task_func(struct io_kiocb *req, bool *locked)
 {
        struct io_ring_ctx *ctx = req->ctx;
        struct io_kiocb *nxt;
@@ -4982,7 +5051,7 @@ static void io_poll_task_func(struct io_kiocb *req)
                if (done) {
                        nxt = io_put_req_find_next(req);
                        if (nxt)
-                               io_req_task_submit(nxt);
+                               io_req_task_submit(nxt, locked);
                }
        }
 }
@@ -5049,8 +5118,13 @@ static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
        if (unlikely(pt->nr_entries)) {
                struct io_poll_iocb *poll_one = poll;
 
+               /* double add on the same waitqueue head, ignore */
+               if (poll_one->head == head)
+                       return;
                /* already have a 2nd entry, fail a third attempt */
                if (*poll_ptr) {
+                       if ((*poll_ptr)->head == head)
+                               return;
                        pt->error = -EINVAL;
                        return;
                }
@@ -5060,9 +5134,6 @@ static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
                 */
                if (!(poll_one->events & EPOLLONESHOT))
                        poll_one->events |= EPOLLONESHOT;
-               /* double add on the same waitqueue head, ignore */
-               if (poll_one->head == head)
-                       return;
                poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
                if (!poll) {
                        pt->error = -ENOMEM;
@@ -5092,7 +5163,7 @@ static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
        __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
 }
 
-static void io_async_task_func(struct io_kiocb *req)
+static void io_async_task_func(struct io_kiocb *req, bool *locked)
 {
        struct async_poll *apoll = req->apoll;
        struct io_ring_ctx *ctx = req->ctx;
@@ -5109,7 +5180,7 @@ static void io_async_task_func(struct io_kiocb *req)
        spin_unlock(&ctx->completion_lock);
 
        if (!READ_ONCE(apoll->poll.canceled))
-               io_req_task_submit(req);
+               io_req_task_submit(req, locked);
        else
                io_req_complete_failed(req, -ECANCELED);
 }
@@ -5360,7 +5431,7 @@ static int io_poll_update_prep(struct io_kiocb *req,
 
        if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
                return -EINVAL;
-       if (sqe->ioprio || sqe->buf_index)
+       if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
                return -EINVAL;
        flags = READ_ONCE(sqe->len);
        if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA |
@@ -5508,18 +5579,10 @@ err:
        return 0;
 }
 
-static void io_req_task_timeout(struct io_kiocb *req)
+static void io_req_task_timeout(struct io_kiocb *req, bool *locked)
 {
-       struct io_ring_ctx *ctx = req->ctx;
-
-       spin_lock(&ctx->completion_lock);
-       io_cqring_fill_event(ctx, req->user_data, -ETIME, 0);
-       io_commit_cqring(ctx);
-       spin_unlock(&ctx->completion_lock);
-
-       io_cqring_ev_posted(ctx);
        req_set_fail(req);
-       io_put_req(req);
+       io_req_complete_post(req, -ETIME, 0);
 }
 
 static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
@@ -5565,6 +5628,7 @@ static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
 }
 
 static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
+       __must_hold(&ctx->completion_lock)
        __must_hold(&ctx->timeout_lock)
 {
        struct io_kiocb *req = io_timeout_extract(ctx, user_data);
@@ -5606,7 +5670,7 @@ 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)
+       if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->splice_fd_in)
                return -EINVAL;
 
        tr->addr = READ_ONCE(sqe->addr);
@@ -5639,22 +5703,22 @@ static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags)
        struct io_ring_ctx *ctx = req->ctx;
        int ret;
 
-       spin_lock_irq(&ctx->timeout_lock);
-       if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE))
+       if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) {
+               spin_lock(&ctx->completion_lock);
+               spin_lock_irq(&ctx->timeout_lock);
                ret = io_timeout_cancel(ctx, tr->addr);
-       else
+               spin_unlock_irq(&ctx->timeout_lock);
+               spin_unlock(&ctx->completion_lock);
+       } else {
+               spin_lock_irq(&ctx->timeout_lock);
                ret = io_timeout_update(ctx, tr->addr, &tr->ts,
                                        io_translate_timeout_mode(tr->flags));
-       spin_unlock_irq(&ctx->timeout_lock);
+               spin_unlock_irq(&ctx->timeout_lock);
+       }
 
-       spin_lock(&ctx->completion_lock);
-       io_cqring_fill_event(ctx, req->user_data, ret, 0);
-       io_commit_cqring(ctx);
-       spin_unlock(&ctx->completion_lock);
-       io_cqring_ev_posted(ctx);
        if (ret < 0)
                req_set_fail(req);
-       io_put_req(req);
+       io_req_complete_post(req, ret, 0);
        return 0;
 }
 
@@ -5667,7 +5731,8 @@ static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
 
        if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
                return -EINVAL;
-       if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
+       if (sqe->ioprio || sqe->buf_index || sqe->len != 1 ||
+           sqe->splice_fd_in)
                return -EINVAL;
        if (off && is_timeout_link)
                return -EINVAL;
@@ -5794,23 +5859,26 @@ static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data,
 }
 
 static int io_try_cancel_userdata(struct io_kiocb *req, u64 sqe_addr)
-       __acquires(&req->ctx->completion_lock)
 {
        struct io_ring_ctx *ctx = req->ctx;
        int ret;
 
-       WARN_ON_ONCE(req->task != current);
+       WARN_ON_ONCE(!io_wq_current_is_worker() && req->task != current);
 
        ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx);
-       spin_lock(&ctx->completion_lock);
        if (ret != -ENOENT)
                return ret;
+
+       spin_lock(&ctx->completion_lock);
        spin_lock_irq(&ctx->timeout_lock);
        ret = io_timeout_cancel(ctx, sqe_addr);
        spin_unlock_irq(&ctx->timeout_lock);
        if (ret != -ENOENT)
-               return ret;
-       return io_poll_cancel(ctx, sqe_addr, false);
+               goto out;
+       ret = io_poll_cancel(ctx, sqe_addr, false);
+out:
+       spin_unlock(&ctx->completion_lock);
+       return ret;
 }
 
 static int io_async_cancel_prep(struct io_kiocb *req,
@@ -5820,7 +5888,8 @@ static int io_async_cancel_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->off || sqe->len || sqe->cancel_flags)
+       if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags ||
+           sqe->splice_fd_in)
                return -EINVAL;
 
        req->cancel.addr = READ_ONCE(sqe->addr);
@@ -5837,7 +5906,6 @@ static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
        ret = io_try_cancel_userdata(req, sqe_addr);
        if (ret != -ENOENT)
                goto done;
-       spin_unlock(&ctx->completion_lock);
 
        /* slow path, try all io-wq's */
        io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK));
@@ -5850,17 +5918,10 @@ static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
                        break;
        }
        io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK));
-
-       spin_lock(&ctx->completion_lock);
 done:
-       io_cqring_fill_event(ctx, req->user_data, ret, 0);
-       io_commit_cqring(ctx);
-       spin_unlock(&ctx->completion_lock);
-       io_cqring_ev_posted(ctx);
-
        if (ret < 0)
                req_set_fail(req);
-       io_put_req(req);
+       io_req_complete_post(req, ret, 0);
        return 0;
 }
 
@@ -5869,7 +5930,7 @@ static int io_rsrc_update_prep(struct io_kiocb *req,
 {
        if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
                return -EINVAL;
-       if (sqe->ioprio || sqe->rw_flags)
+       if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in)
                return -EINVAL;
 
        req->rsrc_update.offset = READ_ONCE(sqe->off);
@@ -6075,7 +6136,7 @@ fail:
        if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
                spin_unlock(&ctx->completion_lock);
                kfree(de);
-               io_queue_async_work(req);
+               io_queue_async_work(req, NULL);
                return true;
        }
 
@@ -6308,6 +6369,7 @@ static void io_wq_submit_work(struct io_wq_work *work)
        if (timeout)
                io_queue_linked_timeout(timeout);
 
+       /* either cancelled or io-wq is dying, so don't touch tctx->iowq */
        if (work->flags & IO_WQ_WORK_CANCEL)
                ret = -ECANCELED;
 
@@ -6397,23 +6459,15 @@ static inline struct file *io_file_get(struct io_ring_ctx *ctx,
                return io_file_get_normal(ctx, req, fd);
 }
 
-static void io_req_task_link_timeout(struct io_kiocb *req)
+static void io_req_task_link_timeout(struct io_kiocb *req, bool *locked)
 {
        struct io_kiocb *prev = req->timeout.prev;
-       struct io_ring_ctx *ctx = req->ctx;
        int ret;
 
        if (prev) {
                ret = io_try_cancel_userdata(req, prev->user_data);
-               if (!ret)
-                       ret = -ETIME;
-               io_cqring_fill_event(ctx, req->user_data, ret, 0);
-               io_commit_cqring(ctx);
-               spin_unlock(&ctx->completion_lock);
-               io_cqring_ev_posted(ctx);
-
+               io_req_complete_post(req, ret ?: -ETIME, 0);
                io_put_req(prev);
-               io_put_req(req);
        } else {
                io_req_complete_post(req, -ETIME, 0);
        }
@@ -6472,7 +6526,7 @@ static void io_queue_linked_timeout(struct io_kiocb *req)
 static void __io_queue_sqe(struct io_kiocb *req)
        __must_hold(&req->ctx->uring_lock)
 {
-       struct io_kiocb *linked_timeout = io_prep_linked_timeout(req);
+       struct io_kiocb *linked_timeout;
        int ret;
 
 issue_sqe:
@@ -6490,24 +6544,34 @@ issue_sqe:
                        state->compl_reqs[state->compl_nr++] = req;
                        if (state->compl_nr == ARRAY_SIZE(state->compl_reqs))
                                io_submit_flush_completions(ctx);
+                       return;
                }
+
+               linked_timeout = io_prep_linked_timeout(req);
+               if (linked_timeout)
+                       io_queue_linked_timeout(linked_timeout);
        } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
+               linked_timeout = io_prep_linked_timeout(req);
+
                switch (io_arm_poll_handler(req)) {
                case IO_APOLL_READY:
+                       if (linked_timeout)
+                               io_unprep_linked_timeout(req);
                        goto issue_sqe;
                case IO_APOLL_ABORTED:
                        /*
                         * Queued up for async execution, worker will release
                         * submit reference when the iocb is actually submitted.
                         */
-                       io_queue_async_work(req);
+                       io_queue_async_work(req, NULL);
                        break;
                }
+
+               if (linked_timeout)
+                       io_queue_linked_timeout(linked_timeout);
        } else {
                io_req_complete_failed(req, ret);
        }
-       if (linked_timeout)
-               io_queue_linked_timeout(linked_timeout);
 }
 
 static inline void io_queue_sqe(struct io_kiocb *req)
@@ -6524,7 +6588,7 @@ static inline void io_queue_sqe(struct io_kiocb *req)
                if (unlikely(ret))
                        io_req_complete_failed(req, ret);
                else
-                       io_queue_async_work(req);
+                       io_queue_async_work(req, NULL);
        }
 }
 
@@ -7128,14 +7192,14 @@ static void **io_alloc_page_table(size_t size)
        size_t init_size = size;
        void **table;
 
-       table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL);
+       table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT);
        if (!table)
                return NULL;
 
        for (i = 0; i < nr_tables; i++) {
                unsigned int this_size = min_t(size_t, size, PAGE_SIZE);
 
-               table[i] = kzalloc(this_size, GFP_KERNEL);
+               table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT);
                if (!table[i]) {
                        io_free_page_table(table, init_size);
                        return NULL;
@@ -7326,7 +7390,8 @@ fail:
 
 static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files)
 {
-       table->files = kvcalloc(nr_files, sizeof(table->files[0]), GFP_KERNEL);
+       table->files = kvcalloc(nr_files, sizeof(table->files[0]),
+                               GFP_KERNEL_ACCOUNT);
        return !!table->files;
 }
 
@@ -7723,6 +7788,8 @@ static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
                return -EINVAL;
        if (nr_args > IORING_MAX_FIXED_FILES)
                return -EMFILE;
+       if (nr_args > rlimit(RLIMIT_NOFILE))
+               return -EMFILE;
        ret = io_rsrc_node_switch_start(ctx);
        if (ret)
                return ret;
@@ -9118,8 +9185,8 @@ static void io_uring_clean_tctx(struct io_uring_task *tctx)
                 * Must be after io_uring_del_task_file() (removes nodes under
                 * uring_lock) to avoid race with io_uring_try_cancel_iowq().
                 */
-               tctx->io_wq = NULL;
                io_wq_put_and_exit(wq);
+               tctx->io_wq = NULL;
        }
 }