io_uring: skip request refcounting
[linux-block.git] / fs / io_uring.c
index cc2d3de164234a55c6bc36bfdb585cac37a4721f..dae87c57694395bce90ca6338904fa1ae1679299 100644 (file)
@@ -299,13 +299,6 @@ struct io_sq_data {
 #define IO_REQ_CACHE_SIZE              32
 #define IO_REQ_ALLOC_BATCH             8
 
-struct io_comp_state {
-       struct io_kiocb         *reqs[IO_COMPL_BATCH];
-       unsigned int            nr;
-       /* inline/task_work completion list, under ->uring_lock */
-       struct list_head        free_list;
-};
-
 struct io_submit_link {
        struct io_kiocb         *head;
        struct io_kiocb         *last;
@@ -326,14 +319,11 @@ struct io_submit_state {
        /*
         * Batch completion logic
         */
-       struct io_comp_state    comp;
+       struct io_kiocb         *compl_reqs[IO_COMPL_BATCH];
+       unsigned int            compl_nr;
+       /* inline/task_work completion list, under ->uring_lock */
+       struct list_head        free_list;
 
-       /*
-        * File reference cache
-        */
-       struct file             *file;
-       unsigned int            fd;
-       unsigned int            file_refs;
        unsigned int            ios_left;
 };
 
@@ -419,6 +409,8 @@ struct io_ring_ctx {
        struct {
                spinlock_t              completion_lock;
 
+               spinlock_t              timeout_lock;
+
                /*
                 * ->iopoll_list is protected by the ctx->uring_lock for
                 * io_uring instances that don't use IORING_SETUP_SQPOLL.
@@ -480,8 +472,8 @@ struct io_uring_task {
 
        spinlock_t              task_lock;
        struct io_wq_work_list  task_list;
-       unsigned long           task_state;
        struct callback_head    task_work;
+       bool                    task_running;
 };
 
 /*
@@ -546,6 +538,8 @@ struct io_timeout {
        struct list_head                list;
        /* head of the link, used by linked timeouts only */
        struct io_kiocb                 *head;
+       /* for linked completions */
+       struct io_kiocb                 *prev;
 };
 
 struct io_timeout_rem {
@@ -670,7 +664,6 @@ struct io_unlink {
 
 struct io_completion {
        struct file                     *file;
-       struct list_head                list;
        u32                             cflags;
 };
 
@@ -717,6 +710,7 @@ enum {
        REQ_F_REISSUE_BIT,
        REQ_F_DONT_REISSUE_BIT,
        REQ_F_CREDS_BIT,
+       REQ_F_REFCOUNT_BIT,
        /* keep async read/write and isreg together and in order */
        REQ_F_NOWAIT_READ_BIT,
        REQ_F_NOWAIT_WRITE_BIT,
@@ -772,6 +766,8 @@ enum {
        REQ_F_ISREG             = BIT(REQ_F_ISREG_BIT),
        /* has creds assigned */
        REQ_F_CREDS             = BIT(REQ_F_CREDS_BIT),
+       /* skip refcounting if not set */
+       REQ_F_REFCOUNT          = BIT(REQ_F_REFCOUNT_BIT),
 };
 
 struct async_poll {
@@ -1048,7 +1044,7 @@ static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd);
 static bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data,
                                 long res, unsigned int cflags);
 static void io_put_req(struct io_kiocb *req);
-static void io_put_req_deferred(struct io_kiocb *req, int nr);
+static void io_put_req_deferred(struct io_kiocb *req);
 static void io_dismantle_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);
@@ -1057,14 +1053,12 @@ static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type,
                                     unsigned nr_args);
 static void io_clean_op(struct io_kiocb *req);
 static struct file *io_file_get(struct io_ring_ctx *ctx,
-                               struct io_submit_state *state,
                                struct io_kiocb *req, int fd, bool fixed);
 static void __io_queue_sqe(struct io_kiocb *req);
 static void io_rsrc_put_work(struct work_struct *work);
 
 static void io_req_task_queue(struct io_kiocb *req);
 static void io_submit_flush_completions(struct io_ring_ctx *ctx);
-static bool io_poll_remove_waitqs(struct io_kiocb *req);
 static int io_req_prep_async(struct io_kiocb *req);
 
 static struct kmem_cache *req_cachep;
@@ -1087,6 +1081,49 @@ EXPORT_SYMBOL(io_uring_get_socket);
 #define io_for_each_link(pos, head) \
        for (pos = (head); pos; pos = pos->link)
 
+/*
+ * Shamelessly stolen from the mm implementation of page reference checking,
+ * see commit f958d7b528b1 for details.
+ */
+#define req_ref_zero_or_close_to_overflow(req) \
+       ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u)
+
+static inline bool req_ref_inc_not_zero(struct io_kiocb *req)
+{
+       WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
+       return atomic_inc_not_zero(&req->refs);
+}
+
+static inline bool req_ref_put_and_test(struct io_kiocb *req)
+{
+       if (likely(!(req->flags & REQ_F_REFCOUNT)))
+               return true;
+
+       WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req));
+       return atomic_dec_and_test(&req->refs);
+}
+
+static inline void req_ref_put(struct io_kiocb *req)
+{
+       WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
+       WARN_ON_ONCE(req_ref_put_and_test(req));
+}
+
+static inline void req_ref_get(struct io_kiocb *req)
+{
+       WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
+       WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req));
+       atomic_inc(&req->refs);
+}
+
+static inline void io_req_refcount(struct io_kiocb *req)
+{
+       if (!(req->flags & REQ_F_REFCOUNT)) {
+               req->flags |= REQ_F_REFCOUNT;
+               atomic_set(&req->refs, 1);
+       }
+}
+
 static inline void io_req_set_rsrc_node(struct io_kiocb *req)
 {
        struct io_ring_ctx *ctx = req->ctx;
@@ -1201,6 +1238,7 @@ static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
        mutex_init(&ctx->uring_lock);
        init_waitqueue_head(&ctx->cq_wait);
        spin_lock_init(&ctx->completion_lock);
+       spin_lock_init(&ctx->timeout_lock);
        INIT_LIST_HEAD(&ctx->iopoll_list);
        INIT_LIST_HEAD(&ctx->defer_list);
        INIT_LIST_HEAD(&ctx->timeout_list);
@@ -1209,7 +1247,7 @@ static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
        INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work);
        init_llist_head(&ctx->rsrc_put_llist);
        INIT_LIST_HEAD(&ctx->tctx_list);
-       INIT_LIST_HEAD(&ctx->submit_state.comp.free_list);
+       INIT_LIST_HEAD(&ctx->submit_state.free_list);
        INIT_LIST_HEAD(&ctx->locked_free_list);
        INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func);
        return ctx;
@@ -1300,10 +1338,10 @@ static void io_prep_async_link(struct io_kiocb *req)
        if (req->flags & REQ_F_LINK_TIMEOUT) {
                struct io_ring_ctx *ctx = req->ctx;
 
-               spin_lock_irq(&ctx->completion_lock);
+               spin_lock(&ctx->completion_lock);
                io_for_each_link(cur, req)
                        io_prep_async_work(cur);
-               spin_unlock_irq(&ctx->completion_lock);
+               spin_unlock(&ctx->completion_lock);
        } else {
                io_for_each_link(cur, req)
                        io_prep_async_work(cur);
@@ -1341,6 +1379,7 @@ static void io_queue_async_work(struct io_kiocb *req)
 
 static void io_kill_timeout(struct io_kiocb *req, int status)
        __must_hold(&req->ctx->completion_lock)
+       __must_hold(&req->ctx->timeout_lock)
 {
        struct io_timeout_data *io = req->async_data;
 
@@ -1349,7 +1388,7 @@ static void io_kill_timeout(struct io_kiocb *req, int status)
                        atomic_read(&req->ctx->cq_timeouts) + 1);
                list_del_init(&req->timeout.list);
                io_cqring_fill_event(req->ctx, req->user_data, status, 0);
-               io_put_req_deferred(req, 1);
+               io_put_req_deferred(req);
        }
 }
 
@@ -1368,9 +1407,11 @@ static void io_queue_deferred(struct io_ring_ctx *ctx)
 }
 
 static void io_flush_timeouts(struct io_ring_ctx *ctx)
+       __must_hold(&ctx->completion_lock)
 {
        u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
 
+       spin_lock_irq(&ctx->timeout_lock);
        while (!list_empty(&ctx->timeout_list)) {
                u32 events_needed, events_got;
                struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
@@ -1395,6 +1436,7 @@ static void io_flush_timeouts(struct io_ring_ctx *ctx)
                io_kill_timeout(req, 0);
        }
        ctx->cq_last_tm_flush = seq;
+       spin_unlock_irq(&ctx->timeout_lock);
 }
 
 static void __io_commit_cqring_flush(struct io_ring_ctx *ctx)
@@ -1487,14 +1529,13 @@ static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx)
 /* Returns true if there are no backlogged entries after the flush */
 static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
 {
-       unsigned long flags;
        bool all_flushed, posted;
 
        if (!force && __io_cqring_events(ctx) == ctx->cq_entries)
                return false;
 
        posted = false;
-       spin_lock_irqsave(&ctx->completion_lock, flags);
+       spin_lock(&ctx->completion_lock);
        while (!list_empty(&ctx->cq_overflow_list)) {
                struct io_uring_cqe *cqe = io_get_cqe(ctx);
                struct io_overflow_cqe *ocqe;
@@ -1522,13 +1563,13 @@ static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
 
        if (posted)
                io_commit_cqring(ctx);
-       spin_unlock_irqrestore(&ctx->completion_lock, flags);
+       spin_unlock(&ctx->completion_lock);
        if (posted)
                io_cqring_ev_posted(ctx);
        return all_flushed;
 }
 
-static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
+static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx)
 {
        bool ret = true;
 
@@ -1536,7 +1577,7 @@ static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
                /* iopoll syncs against uring_lock, not completion_lock */
                if (ctx->flags & IORING_SETUP_IOPOLL)
                        mutex_lock(&ctx->uring_lock);
-               ret = __io_cqring_overflow_flush(ctx, force);
+               ret = __io_cqring_overflow_flush(ctx, false);
                if (ctx->flags & IORING_SETUP_IOPOLL)
                        mutex_unlock(&ctx->uring_lock);
        }
@@ -1544,41 +1585,6 @@ static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
        return ret;
 }
 
-/*
- * Shamelessly stolen from the mm implementation of page reference checking,
- * see commit f958d7b528b1 for details.
- */
-#define req_ref_zero_or_close_to_overflow(req) \
-       ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u)
-
-static inline bool req_ref_inc_not_zero(struct io_kiocb *req)
-{
-       return atomic_inc_not_zero(&req->refs);
-}
-
-static inline bool req_ref_sub_and_test(struct io_kiocb *req, int refs)
-{
-       WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req));
-       return atomic_sub_and_test(refs, &req->refs);
-}
-
-static inline bool req_ref_put_and_test(struct io_kiocb *req)
-{
-       WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req));
-       return atomic_dec_and_test(&req->refs);
-}
-
-static inline void req_ref_put(struct io_kiocb *req)
-{
-       WARN_ON_ONCE(req_ref_put_and_test(req));
-}
-
-static inline void req_ref_get(struct io_kiocb *req)
-{
-       WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req));
-       atomic_inc(&req->refs);
-}
-
 /* must to be called somewhat shortly after putting a request */
 static inline void io_put_task(struct task_struct *task, int nr)
 {
@@ -1651,9 +1657,8 @@ static void io_req_complete_post(struct io_kiocb *req, long res,
                                 unsigned int cflags)
 {
        struct io_ring_ctx *ctx = req->ctx;
-       unsigned long flags;
 
-       spin_lock_irqsave(&ctx->completion_lock, flags);
+       spin_lock(&ctx->completion_lock);
        __io_cqring_fill_event(ctx, req->user_data, res, cflags);
        /*
         * If we're the last reference to this request, add to our locked
@@ -1670,14 +1675,14 @@ static void io_req_complete_post(struct io_kiocb *req, long res,
                }
                io_dismantle_req(req);
                io_put_task(req->task, 1);
-               list_add(&req->compl.list, &ctx->locked_free_list);
+               list_add(&req->inflight_entry, &ctx->locked_free_list);
                ctx->locked_free_nr++;
        } else {
                if (!percpu_ref_tryget(&ctx->refs))
                        req = NULL;
        }
        io_commit_cqring(ctx);
-       spin_unlock_irqrestore(&ctx->completion_lock, flags);
+       spin_unlock(&ctx->completion_lock);
 
        if (req) {
                io_cqring_ev_posted(ctx);
@@ -1717,7 +1722,6 @@ static inline void io_req_complete(struct io_kiocb *req, long res)
 static void io_req_complete_failed(struct io_kiocb *req, long res)
 {
        req_set_fail(req);
-       io_put_req(req);
        io_req_complete_post(req, res, 0);
 }
 
@@ -1735,19 +1739,18 @@ static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx)
 }
 
 static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx,
-                                       struct io_comp_state *cs)
+                                       struct io_submit_state *state)
 {
-       spin_lock_irq(&ctx->completion_lock);
-       list_splice_init(&ctx->locked_free_list, &cs->free_list);
+       spin_lock(&ctx->completion_lock);
+       list_splice_init(&ctx->locked_free_list, &state->free_list);
        ctx->locked_free_nr = 0;
-       spin_unlock_irq(&ctx->completion_lock);
+       spin_unlock(&ctx->completion_lock);
 }
 
 /* Returns true IFF there are requests in the cache */
 static bool io_flush_cached_reqs(struct io_ring_ctx *ctx)
 {
        struct io_submit_state *state = &ctx->submit_state;
-       struct io_comp_state *cs = &state->comp;
        int nr;
 
        /*
@@ -1756,14 +1759,14 @@ static bool io_flush_cached_reqs(struct io_ring_ctx *ctx)
         * side cache.
         */
        if (READ_ONCE(ctx->locked_free_nr) > IO_COMPL_BATCH)
-               io_flush_cached_locked_reqs(ctx, cs);
+               io_flush_cached_locked_reqs(ctx, state);
 
        nr = state->free_reqs;
-       while (!list_empty(&cs->free_list)) {
-               struct io_kiocb *req = list_first_entry(&cs->free_list,
-                                               struct io_kiocb, compl.list);
+       while (!list_empty(&state->free_list)) {
+               struct io_kiocb *req = list_first_entry(&state->free_list,
+                                       struct io_kiocb, inflight_entry);
 
-               list_del(&req->compl.list);
+               list_del(&req->inflight_entry);
                state->reqs[nr++] = req;
                if (nr == ARRAY_SIZE(state->reqs))
                        break;
@@ -1773,7 +1776,14 @@ static bool io_flush_cached_reqs(struct io_ring_ctx *ctx)
        return nr != 0;
 }
 
+/*
+ * A request might get retired back into the request caches even before opcode
+ * handlers and io_issue_sqe() are done with it, e.g. inline completion path.
+ * Because of that, io_alloc_req() should be called only under ->uring_lock
+ * and with extra caution to not get a request that is still worked on.
+ */
 static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx)
+       __must_hold(&ctx->uring_lock)
 {
        struct io_submit_state *state = &ctx->submit_state;
        gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
@@ -1831,15 +1841,14 @@ static void io_dismantle_req(struct io_kiocb *req)
 static void __io_free_req(struct io_kiocb *req)
 {
        struct io_ring_ctx *ctx = req->ctx;
-       unsigned long flags;
 
        io_dismantle_req(req);
        io_put_task(req->task, 1);
 
-       spin_lock_irqsave(&ctx->completion_lock, flags);
-       list_add(&req->compl.list, &ctx->locked_free_list);
+       spin_lock(&ctx->completion_lock);
+       list_add(&req->inflight_entry, &ctx->locked_free_list);
        ctx->locked_free_nr++;
-       spin_unlock_irqrestore(&ctx->completion_lock, flags);
+       spin_unlock(&ctx->completion_lock);
 
        percpu_ref_put(&ctx->refs);
 }
@@ -1854,6 +1863,7 @@ static inline void io_remove_next_linked(struct io_kiocb *req)
 
 static bool io_kill_linked_timeout(struct io_kiocb *req)
        __must_hold(&req->ctx->completion_lock)
+       __must_hold(&req->ctx->timeout_lock)
 {
        struct io_kiocb *link = req->link;
 
@@ -1869,7 +1879,7 @@ static bool io_kill_linked_timeout(struct io_kiocb *req)
                if (hrtimer_try_to_cancel(&io->timer) != -1) {
                        io_cqring_fill_event(link->ctx, link->user_data,
                                             -ECANCELED, 0);
-                       io_put_req_deferred(link, 1);
+                       io_put_req_deferred(link);
                        return true;
                }
        }
@@ -1888,7 +1898,7 @@ static void io_fail_links(struct io_kiocb *req)
 
                trace_io_uring_fail_link(req, link);
                io_cqring_fill_event(link->ctx, link->user_data, -ECANCELED, 0);
-               io_put_req_deferred(link, 2);
+               io_put_req_deferred(link);
                link = nxt;
        }
 }
@@ -1898,8 +1908,13 @@ static bool io_disarm_next(struct io_kiocb *req)
 {
        bool posted = false;
 
-       if (likely(req->flags & REQ_F_LINK_TIMEOUT))
+       if (likely(req->flags & REQ_F_LINK_TIMEOUT)) {
+               struct io_ring_ctx *ctx = req->ctx;
+
+               spin_lock_irq(&ctx->timeout_lock);
                posted = io_kill_linked_timeout(req);
+               spin_unlock_irq(&ctx->timeout_lock);
+       }
        if (unlikely((req->flags & REQ_F_FAIL) &&
                     !(req->flags & REQ_F_HARDLINK))) {
                posted |= (req->link != NULL);
@@ -1920,14 +1935,13 @@ static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
         */
        if (req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_FAIL)) {
                struct io_ring_ctx *ctx = req->ctx;
-               unsigned long flags;
                bool posted;
 
-               spin_lock_irqsave(&ctx->completion_lock, flags);
+               spin_lock(&ctx->completion_lock);
                posted = io_disarm_next(req);
                if (posted)
                        io_commit_cqring(req->ctx);
-               spin_unlock_irqrestore(&ctx->completion_lock, flags);
+               spin_unlock(&ctx->completion_lock);
                if (posted)
                        io_cqring_ev_posted(ctx);
        }
@@ -1947,7 +1961,7 @@ static void ctx_flush_and_put(struct io_ring_ctx *ctx)
 {
        if (!ctx)
                return;
-       if (ctx->submit_state.comp.nr) {
+       if (ctx->submit_state.compl_nr) {
                mutex_lock(&ctx->uring_lock);
                io_submit_flush_completions(ctx);
                mutex_unlock(&ctx->uring_lock);
@@ -1967,9 +1981,13 @@ static void tctx_task_work(struct callback_head *cb)
                spin_lock_irq(&tctx->task_lock);
                node = tctx->task_list.first;
                INIT_WQ_LIST(&tctx->task_list);
+               if (!node)
+                       tctx->task_running = false;
                spin_unlock_irq(&tctx->task_lock);
+               if (!node)
+                       break;
 
-               while (node) {
+               do {
                        struct io_wq_work_node *next = node->next;
                        struct io_kiocb *req = container_of(node, struct io_kiocb,
                                                            io_task_work.node);
@@ -1981,19 +1999,8 @@ static void tctx_task_work(struct callback_head *cb)
                        }
                        req->io_task_work.func(req);
                        node = next;
-               }
-               if (wq_list_empty(&tctx->task_list)) {
-                       spin_lock_irq(&tctx->task_lock);
-                       clear_bit(0, &tctx->task_state);
-                       if (wq_list_empty(&tctx->task_list)) {
-                               spin_unlock_irq(&tctx->task_lock);
-                               break;
-                       }
-                       spin_unlock_irq(&tctx->task_lock);
-                       /* another tctx_task_work() is enqueued, yield */
-                       if (test_and_set_bit(0, &tctx->task_state))
-                               break;
-               }
+               } while (node);
+
                cond_resched();
        }
 
@@ -2007,16 +2014,19 @@ static void io_req_task_work_add(struct io_kiocb *req)
        enum task_work_notify_mode notify;
        struct io_wq_work_node *node;
        unsigned long flags;
+       bool running;
 
        WARN_ON_ONCE(!tctx);
 
        spin_lock_irqsave(&tctx->task_lock, flags);
        wq_list_add_tail(&req->io_task_work.node, &tctx->task_list);
+       running = tctx->task_running;
+       if (!running)
+               tctx->task_running = true;
        spin_unlock_irqrestore(&tctx->task_lock, flags);
 
        /* task_work already pending, we're done */
-       if (test_bit(0, &tctx->task_state) ||
-           test_and_set_bit(0, &tctx->task_state))
+       if (running)
                return;
 
        /*
@@ -2031,8 +2041,8 @@ static void io_req_task_work_add(struct io_kiocb *req)
                return;
        }
 
-       clear_bit(0, &tctx->task_state);
        spin_lock_irqsave(&tctx->task_lock, flags);
+       tctx->task_running = false;
        node = tctx->task_list.first;
        INIT_WQ_LIST(&tctx->task_list);
        spin_unlock_irqrestore(&tctx->task_lock, flags);
@@ -2144,38 +2154,37 @@ static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req,
        if (state->free_reqs != ARRAY_SIZE(state->reqs))
                state->reqs[state->free_reqs++] = req;
        else
-               list_add(&req->compl.list, &state->comp.free_list);
+               list_add(&req->inflight_entry, &state->free_list);
 }
 
 static void io_submit_flush_completions(struct io_ring_ctx *ctx)
        __must_hold(&req->ctx->uring_lock)
 {
-       struct io_comp_state *cs = &ctx->submit_state.comp;
-       int i, nr = cs->nr;
+       struct io_submit_state *state = &ctx->submit_state;
+       int i, nr = state->compl_nr;
        struct req_batch rb;
 
-       spin_lock_irq(&ctx->completion_lock);
+       spin_lock(&ctx->completion_lock);
        for (i = 0; i < nr; i++) {
-               struct io_kiocb *req = cs->reqs[i];
+               struct io_kiocb *req = state->compl_reqs[i];
 
                __io_cqring_fill_event(ctx, req->user_data, req->result,
                                        req->compl.cflags);
        }
        io_commit_cqring(ctx);
-       spin_unlock_irq(&ctx->completion_lock);
+       spin_unlock(&ctx->completion_lock);
        io_cqring_ev_posted(ctx);
 
        io_init_req_batch(&rb);
        for (i = 0; i < nr; i++) {
-               struct io_kiocb *req = cs->reqs[i];
+               struct io_kiocb *req = state->compl_reqs[i];
 
-               /* submission and completion refs */
-               if (req_ref_sub_and_test(req, 2))
+               if (req_ref_put_and_test(req))
                        io_req_free_batch(&rb, req, &ctx->submit_state);
        }
 
        io_req_free_batch_finish(ctx, &rb);
-       cs->nr = 0;
+       state->compl_nr = 0;
 }
 
 /*
@@ -2199,9 +2208,9 @@ static inline void io_put_req(struct io_kiocb *req)
                io_free_req(req);
 }
 
-static inline void io_put_req_deferred(struct io_kiocb *req, int refs)
+static inline void io_put_req_deferred(struct io_kiocb *req)
 {
-       if (req_ref_sub_and_test(req, refs)) {
+       if (req_ref_put_and_test(req)) {
                req->io_task_work.func = io_free_req;
                io_req_task_work_add(req);
        }
@@ -2274,7 +2283,6 @@ static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
                if (READ_ONCE(req->result) == -EAGAIN && resubmit &&
                    !(req->flags & REQ_F_DONT_REISSUE)) {
                        req->iopoll_completed = 0;
-                       req_ref_get(req);
                        io_req_task_queue_reissue(req);
                        continue;
                }
@@ -2484,31 +2492,48 @@ static bool io_rw_should_reissue(struct io_kiocb *req)
 }
 #endif
 
-static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
-                            unsigned int issue_flags)
+static bool __io_complete_rw_common(struct io_kiocb *req, long res)
 {
-       int cflags = 0;
-
        if (req->rw.kiocb.ki_flags & IOCB_WRITE)
                kiocb_end_write(req);
        if (res != req->result) {
                if ((res == -EAGAIN || res == -EOPNOTSUPP) &&
                    io_rw_should_reissue(req)) {
                        req->flags |= REQ_F_REISSUE;
-                       return;
+                       return true;
                }
                req_set_fail(req);
+               req->result = res;
        }
+       return false;
+}
+
+static void io_req_task_complete(struct io_kiocb *req)
+{
+       int cflags = 0;
+
        if (req->flags & REQ_F_BUFFER_SELECTED)
                cflags = io_put_rw_kbuf(req);
-       __io_req_complete(req, issue_flags, res, cflags);
+       __io_req_complete(req, 0, req->result, cflags);
+}
+
+static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
+                            unsigned int issue_flags)
+{
+       if (__io_complete_rw_common(req, res))
+               return;
+       io_req_task_complete(req);
 }
 
 static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
 {
        struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
 
-       __io_complete_rw(req, res, res2, 0);
+       if (__io_complete_rw_common(req, res))
+               return;
+       req->result = res;
+       req->io_task_work.func = io_req_task_complete;
+       io_req_task_work_add(req);
 }
 
 static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
@@ -2594,40 +2619,6 @@ static void io_iopoll_req_issued(struct io_kiocb *req)
        }
 }
 
-static inline void io_state_file_put(struct io_submit_state *state)
-{
-       if (state->file_refs) {
-               fput_many(state->file, state->file_refs);
-               state->file_refs = 0;
-       }
-}
-
-/*
- * Get as many references to a file as we have IOs left in this submission,
- * assuming most submissions are for one file, or at least that each file
- * has more than one submission.
- */
-static struct file *__io_file_get(struct io_submit_state *state, int fd)
-{
-       if (!state)
-               return fget(fd);
-
-       if (state->file_refs) {
-               if (state->fd == fd) {
-                       state->file_refs--;
-                       return state->file;
-               }
-               io_state_file_put(state);
-       }
-       state->file = fget_many(fd, state->ios_left);
-       if (unlikely(!state->file))
-               return NULL;
-
-       state->fd = fd;
-       state->file_refs = state->ios_left - 1;
-       return state->file;
-}
-
 static bool io_bdev_nowait(struct block_device *bdev)
 {
        return !bdev || blk_queue_nowait(bdev_get_queue(bdev));
@@ -2789,7 +2780,6 @@ static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
        if (check_reissue && (req->flags & REQ_F_REISSUE)) {
                req->flags &= ~REQ_F_REISSUE;
                if (io_resubmit_prep(req)) {
-                       req_ref_get(req);
                        io_req_task_queue_reissue(req);
                } else {
                        int cflags = 0;
@@ -3215,9 +3205,6 @@ static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
 
        req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
        list_del_init(&wait->entry);
-
-       /* submit ref gets dropped, acquire a new one */
-       req_ref_get(req);
        io_req_task_queue(req);
        return 1;
 }
@@ -3629,8 +3616,7 @@ static int __io_splice_prep(struct io_kiocb *req,
        if (unlikely(sp->flags & ~valid_flags))
                return -EINVAL;
 
-       sp->file_in = io_file_get(req->ctx, NULL, req,
-                                 READ_ONCE(sqe->splice_fd_in),
+       sp->file_in = io_file_get(req->ctx, req, READ_ONCE(sqe->splice_fd_in),
                                  (sp->flags & SPLICE_F_FD_IN_FIXED));
        if (!sp->file_in)
                return -EBADF;
@@ -4885,7 +4871,7 @@ static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
                req->result = vfs_poll(req->file, &pt) & poll->events;
        }
 
-       spin_lock_irq(&ctx->completion_lock);
+       spin_lock(&ctx->completion_lock);
        if (!req->result && !READ_ONCE(poll->canceled)) {
                add_wait_queue(poll->head, &poll->wait);
                return true;
@@ -4919,12 +4905,12 @@ static void io_poll_remove_double(struct io_kiocb *req)
        if (poll && poll->head) {
                struct wait_queue_head *head = poll->head;
 
-               spin_lock(&head->lock);
+               spin_lock_irq(&head->lock);
                list_del_init(&poll->wait.entry);
                if (poll->wait.private)
                        req_ref_put(req);
                poll->head = NULL;
-               spin_unlock(&head->lock);
+               spin_unlock_irq(&head->lock);
        }
 }
 
@@ -4960,7 +4946,7 @@ static void io_poll_task_func(struct io_kiocb *req)
        struct io_kiocb *nxt;
 
        if (io_poll_rewait(req, &req->poll)) {
-               spin_unlock_irq(&ctx->completion_lock);
+               spin_unlock(&ctx->completion_lock);
        } else {
                bool done;
 
@@ -4972,7 +4958,7 @@ static void io_poll_task_func(struct io_kiocb *req)
                        req->result = 0;
                        add_wait_queue(req->poll.head, &req->poll.wait);
                }
-               spin_unlock_irq(&ctx->completion_lock);
+               spin_unlock(&ctx->completion_lock);
                io_cqring_ev_posted(ctx);
 
                if (done) {
@@ -4989,6 +4975,7 @@ static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
        struct io_kiocb *req = wait->private;
        struct io_poll_iocb *poll = io_poll_get_single(req);
        __poll_t mask = key_to_poll(key);
+       unsigned long flags;
 
        /* for instances that support it check for an event match first: */
        if (mask && !(mask & poll->events))
@@ -5001,13 +4988,13 @@ static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
        if (poll->head) {
                bool done;
 
-               spin_lock(&poll->head->lock);
+               spin_lock_irqsave(&poll->head->lock, flags);
                done = list_empty(&poll->wait.entry);
                if (!done)
                        list_del_init(&poll->wait.entry);
                /* make sure double remove sees this as being gone */
                wait->private = NULL;
-               spin_unlock(&poll->head->lock);
+               spin_unlock_irqrestore(&poll->head->lock, flags);
                if (!done) {
                        /* use wait func handler, so it matches the rq type */
                        poll->wait.func(&poll->wait, mode, sync, key);
@@ -5095,13 +5082,13 @@ static void io_async_task_func(struct io_kiocb *req)
        trace_io_uring_task_run(req->ctx, req, req->opcode, req->user_data);
 
        if (io_poll_rewait(req, &apoll->poll)) {
-               spin_unlock_irq(&ctx->completion_lock);
+               spin_unlock(&ctx->completion_lock);
                return;
        }
 
        hash_del(&req->hash_node);
        io_poll_remove_double(req);
-       spin_unlock_irq(&ctx->completion_lock);
+       spin_unlock(&ctx->completion_lock);
 
        if (!READ_ONCE(apoll->poll.canceled))
                io_req_task_submit(req);
@@ -5153,11 +5140,11 @@ static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
        if (unlikely(!ipt->nr_entries) && !ipt->error)
                ipt->error = -EINVAL;
 
-       spin_lock_irq(&ctx->completion_lock);
+       spin_lock(&ctx->completion_lock);
        if (ipt->error || (mask && (poll->events & EPOLLONESHOT)))
                io_poll_remove_double(req);
        if (likely(poll->head)) {
-               spin_lock(&poll->head->lock);
+               spin_lock_irq(&poll->head->lock);
                if (unlikely(list_empty(&poll->wait.entry))) {
                        if (ipt->error)
                                cancel = true;
@@ -5170,7 +5157,7 @@ static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
                        WRITE_ONCE(poll->canceled, true);
                else if (!poll->done) /* actually waiting for an event */
                        io_poll_req_insert(req);
-               spin_unlock(&poll->head->lock);
+               spin_unlock_irq(&poll->head->lock);
        }
 
        return mask;
@@ -5222,16 +5209,17 @@ static int io_arm_poll_handler(struct io_kiocb *req)
        req->apoll = apoll;
        req->flags |= REQ_F_POLLED;
        ipt.pt._qproc = io_async_queue_proc;
+       io_req_refcount(req);
 
        ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
                                        io_async_wake);
        if (ret || ipt.error) {
-               spin_unlock_irq(&ctx->completion_lock);
+               spin_unlock(&ctx->completion_lock);
                if (ret)
                        return IO_APOLL_READY;
                return IO_APOLL_ABORTED;
        }
-       spin_unlock_irq(&ctx->completion_lock);
+       spin_unlock(&ctx->completion_lock);
        trace_io_uring_poll_arm(ctx, req, req->opcode, req->user_data,
                                mask, apoll->poll.events);
        return IO_APOLL_OK;
@@ -5245,19 +5233,19 @@ static bool __io_poll_remove_one(struct io_kiocb *req,
 
        if (!poll->head)
                return false;
-       spin_lock(&poll->head->lock);
+       spin_lock_irq(&poll->head->lock);
        if (do_cancel)
                WRITE_ONCE(poll->canceled, true);
        if (!list_empty(&poll->wait.entry)) {
                list_del_init(&poll->wait.entry);
                do_complete = true;
        }
-       spin_unlock(&poll->head->lock);
+       spin_unlock_irq(&poll->head->lock);
        hash_del(&req->hash_node);
        return do_complete;
 }
 
-static bool io_poll_remove_waitqs(struct io_kiocb *req)
+static bool io_poll_remove_one(struct io_kiocb *req)
        __must_hold(&req->ctx->completion_lock)
 {
        bool do_complete;
@@ -5265,26 +5253,12 @@ static bool io_poll_remove_waitqs(struct io_kiocb *req)
        io_poll_remove_double(req);
        do_complete = __io_poll_remove_one(req, io_poll_get_single(req), true);
 
-       if (req->opcode != IORING_OP_POLL_ADD && do_complete) {
-               /* non-poll requests have submit ref still */
-               req_ref_put(req);
-       }
-       return do_complete;
-}
-
-static bool io_poll_remove_one(struct io_kiocb *req)
-       __must_hold(&req->ctx->completion_lock)
-{
-       bool do_complete;
-
-       do_complete = io_poll_remove_waitqs(req);
        if (do_complete) {
                io_cqring_fill_event(req->ctx, req->user_data, -ECANCELED, 0);
                io_commit_cqring(req->ctx);
                req_set_fail(req);
-               io_put_req_deferred(req, 1);
+               io_put_req_deferred(req);
        }
-
        return do_complete;
 }
 
@@ -5298,7 +5272,7 @@ static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk,
        struct io_kiocb *req;
        int posted = 0, i;
 
-       spin_lock_irq(&ctx->completion_lock);
+       spin_lock(&ctx->completion_lock);
        for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
                struct hlist_head *list;
 
@@ -5308,7 +5282,7 @@ static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk,
                                posted += io_poll_remove_one(req);
                }
        }
-       spin_unlock_irq(&ctx->completion_lock);
+       spin_unlock(&ctx->completion_lock);
 
        if (posted)
                io_cqring_ev_posted(ctx);
@@ -5426,6 +5400,7 @@ static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe
        if (flags & ~IORING_POLL_ADD_MULTI)
                return -EINVAL;
 
+       io_req_refcount(req);
        poll->events = io_poll_parse_events(sqe, flags);
        return 0;
 }
@@ -5446,7 +5421,7 @@ static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
                ipt.error = 0;
                io_poll_complete(req, mask);
        }
-       spin_unlock_irq(&ctx->completion_lock);
+       spin_unlock(&ctx->completion_lock);
 
        if (mask) {
                io_cqring_ev_posted(ctx);
@@ -5463,7 +5438,7 @@ static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags)
        bool completing;
        int ret;
 
-       spin_lock_irq(&ctx->completion_lock);
+       spin_lock(&ctx->completion_lock);
        preq = io_poll_find(ctx, req->poll_update.old_user_data, true);
        if (!preq) {
                ret = -ENOENT;
@@ -5490,7 +5465,7 @@ static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags)
        ret = 0;
 err:
        if (ret < 0) {
-               spin_unlock_irq(&ctx->completion_lock);
+               spin_unlock(&ctx->completion_lock);
                req_set_fail(req);
                io_req_complete(req, ret);
                return 0;
@@ -5503,7 +5478,7 @@ err:
        }
        if (req->poll_update.update_user_data)
                preq->user_data = req->poll_update.new_user_data;
-       spin_unlock_irq(&ctx->completion_lock);
+       spin_unlock(&ctx->completion_lock);
 
        /* complete update request, we're done with it */
        io_req_complete(req, ret);
@@ -5518,6 +5493,20 @@ err:
        return 0;
 }
 
+static void io_req_task_timeout(struct io_kiocb *req)
+{
+       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);
+}
+
 static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
 {
        struct io_timeout_data *data = container_of(timer,
@@ -5526,24 +5515,20 @@ static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
        struct io_ring_ctx *ctx = req->ctx;
        unsigned long flags;
 
-       spin_lock_irqsave(&ctx->completion_lock, flags);
+       spin_lock_irqsave(&ctx->timeout_lock, flags);
        list_del_init(&req->timeout.list);
        atomic_set(&req->ctx->cq_timeouts,
                atomic_read(&req->ctx->cq_timeouts) + 1);
+       spin_unlock_irqrestore(&ctx->timeout_lock, flags);
 
-       io_cqring_fill_event(ctx, req->user_data, -ETIME, 0);
-       io_commit_cqring(ctx);
-       spin_unlock_irqrestore(&ctx->completion_lock, flags);
-
-       io_cqring_ev_posted(ctx);
-       req_set_fail(req);
-       io_put_req(req);
+       req->io_task_work.func = io_req_task_timeout;
+       io_req_task_work_add(req);
        return HRTIMER_NORESTART;
 }
 
 static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
                                           __u64 user_data)
-       __must_hold(&ctx->completion_lock)
+       __must_hold(&ctx->timeout_lock)
 {
        struct io_timeout_data *io;
        struct io_kiocb *req;
@@ -5565,7 +5550,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);
 
@@ -5574,13 +5559,13 @@ static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
 
        req_set_fail(req);
        io_cqring_fill_event(ctx, req->user_data, -ECANCELED, 0);
-       io_put_req_deferred(req, 1);
+       io_put_req_deferred(req);
        return 0;
 }
 
 static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
                             struct timespec64 *ts, enum hrtimer_mode mode)
-       __must_hold(&ctx->completion_lock)
+       __must_hold(&ctx->timeout_lock)
 {
        struct io_kiocb *req = io_timeout_extract(ctx, user_data);
        struct io_timeout_data *data;
@@ -5639,16 +5624,18 @@ 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->completion_lock);
+       spin_lock_irq(&ctx->timeout_lock);
        if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE))
                ret = io_timeout_cancel(ctx, tr->addr);
        else
                ret = io_timeout_update(ctx, tr->addr, &tr->ts,
                                        io_translate_timeout_mode(tr->flags));
+       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_irq(&ctx->completion_lock);
+       spin_unlock(&ctx->completion_lock);
        io_cqring_ev_posted(ctx);
        if (ret < 0)
                req_set_fail(req);
@@ -5700,7 +5687,7 @@ static int io_timeout(struct io_kiocb *req, unsigned int issue_flags)
        struct list_head *entry;
        u32 tail, off = req->timeout.off;
 
-       spin_lock_irq(&ctx->completion_lock);
+       spin_lock_irq(&ctx->timeout_lock);
 
        /*
         * sqe->off holds how many events that need to occur for this
@@ -5739,7 +5726,7 @@ add:
        list_add(&req->timeout.list, entry);
        data->timer.function = io_timeout_fn;
        hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
-       spin_unlock_irq(&ctx->completion_lock);
+       spin_unlock_irq(&ctx->timeout_lock);
        return 0;
 }
 
@@ -5786,14 +5773,15 @@ static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
                                     struct io_kiocb *req, __u64 sqe_addr,
                                     int success_ret)
 {
-       unsigned long flags;
        int ret;
 
        ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx);
-       spin_lock_irqsave(&ctx->completion_lock, flags);
+       spin_lock(&ctx->completion_lock);
        if (ret != -ENOENT)
                goto done;
+       spin_lock_irq(&ctx->timeout_lock);
        ret = io_timeout_cancel(ctx, sqe_addr);
+       spin_unlock_irq(&ctx->timeout_lock);
        if (ret != -ENOENT)
                goto done;
        ret = io_poll_cancel(ctx, sqe_addr, false);
@@ -5802,7 +5790,7 @@ done:
                ret = success_ret;
        io_cqring_fill_event(ctx, req->user_data, ret, 0);
        io_commit_cqring(ctx);
-       spin_unlock_irqrestore(&ctx->completion_lock, flags);
+       spin_unlock(&ctx->completion_lock);
        io_cqring_ev_posted(ctx);
 
        if (ret < 0)
@@ -5832,16 +5820,18 @@ static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
 
        /* tasks should wait for their io-wq threads, so safe w/o sync */
        ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx);
-       spin_lock_irq(&ctx->completion_lock);
+       spin_lock(&ctx->completion_lock);
        if (ret != -ENOENT)
                goto done;
+       spin_lock_irq(&ctx->timeout_lock);
        ret = io_timeout_cancel(ctx, sqe_addr);
+       spin_unlock_irq(&ctx->timeout_lock);
        if (ret != -ENOENT)
                goto done;
        ret = io_poll_cancel(ctx, sqe_addr, false);
        if (ret != -ENOENT)
                goto done;
-       spin_unlock_irq(&ctx->completion_lock);
+       spin_unlock(&ctx->completion_lock);
 
        /* slow path, try all io-wq's */
        io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK));
@@ -5855,11 +5845,11 @@ static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
        }
        io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK));
 
-       spin_lock_irq(&ctx->completion_lock);
+       spin_lock(&ctx->completion_lock);
 done:
        io_cqring_fill_event(ctx, req->user_data, ret, 0);
        io_commit_cqring(ctx);
-       spin_unlock_irq(&ctx->completion_lock);
+       spin_unlock(&ctx->completion_lock);
        io_cqring_ev_posted(ctx);
 
        if (ret < 0)
@@ -6075,9 +6065,9 @@ fail:
                return true;
        }
 
-       spin_lock_irq(&ctx->completion_lock);
+       spin_lock(&ctx->completion_lock);
        if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
-               spin_unlock_irq(&ctx->completion_lock);
+               spin_unlock(&ctx->completion_lock);
                kfree(de);
                io_queue_async_work(req);
                return true;
@@ -6087,7 +6077,7 @@ fail:
        de->req = req;
        de->seq = seq;
        list_add_tail(&de->list, &ctx->defer_list);
-       spin_unlock_irq(&ctx->completion_lock);
+       spin_unlock(&ctx->completion_lock);
        return true;
 }
 
@@ -6302,6 +6292,10 @@ static void io_wq_submit_work(struct io_wq_work *work)
        struct io_kiocb *timeout;
        int ret = 0;
 
+       io_req_refcount(req);
+       /* will be dropped by ->io_free_work() after returning to io-wq */
+       req_ref_get(req);
+
        timeout = io_prep_linked_timeout(req);
        if (timeout)
                io_queue_linked_timeout(timeout);
@@ -6324,11 +6318,8 @@ static void io_wq_submit_work(struct io_wq_work *work)
        }
 
        /* avoid locking problems by failing it from a clean context */
-       if (ret) {
-               /* io-wq is going to take one down */
-               req_ref_get(req);
+       if (ret)
                io_req_task_queue_fail(req, ret);
-       }
 }
 
 static inline struct io_fixed_file *io_fixed_file_slot(struct io_file_table *table,
@@ -6377,10 +6368,9 @@ static inline struct file *io_file_get_fixed(struct io_ring_ctx *ctx,
 }
 
 static struct file *io_file_get_normal(struct io_ring_ctx *ctx,
-                                      struct io_submit_state *state,
                                       struct io_kiocb *req, int fd)
 {
-       struct file *file = __io_file_get(state, fd);
+       struct file *file = fget(fd);
 
        trace_io_uring_file_get(ctx, fd);
 
@@ -6391,13 +6381,26 @@ static struct file *io_file_get_normal(struct io_ring_ctx *ctx,
 }
 
 static inline struct file *io_file_get(struct io_ring_ctx *ctx,
-                                      struct io_submit_state *state,
                                       struct io_kiocb *req, int fd, bool fixed)
 {
        if (fixed)
                return io_file_get_fixed(ctx, req, fd);
        else
-               return io_file_get_normal(ctx, state, req, fd);
+               return io_file_get_normal(ctx, req, fd);
+}
+
+static void io_req_task_link_timeout(struct io_kiocb *req)
+{
+       struct io_kiocb *prev = req->timeout.prev;
+       struct io_ring_ctx *ctx = req->ctx;
+
+       if (prev) {
+               io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
+               io_put_req(prev);
+               io_put_req(req);
+       } else {
+               io_req_complete_post(req, -ETIME, 0);
+       }
 }
 
 static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
@@ -6408,7 +6411,7 @@ static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
        struct io_ring_ctx *ctx = req->ctx;
        unsigned long flags;
 
-       spin_lock_irqsave(&ctx->completion_lock, flags);
+       spin_lock_irqsave(&ctx->timeout_lock, flags);
        prev = req->timeout.head;
        req->timeout.head = NULL;
 
@@ -6421,15 +6424,11 @@ static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
                if (!req_ref_inc_not_zero(prev))
                        prev = NULL;
        }
-       spin_unlock_irqrestore(&ctx->completion_lock, flags);
+       req->timeout.prev = prev;
+       spin_unlock_irqrestore(&ctx->timeout_lock, flags);
 
-       if (prev) {
-               io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
-               io_put_req_deferred(prev, 1);
-               io_put_req_deferred(req, 1);
-       } else {
-               io_req_complete_post(req, -ETIME, 0);
-       }
+       req->io_task_work.func = io_req_task_link_timeout;
+       io_req_task_work_add(req);
        return HRTIMER_NORESTART;
 }
 
@@ -6437,7 +6436,7 @@ static void io_queue_linked_timeout(struct io_kiocb *req)
 {
        struct io_ring_ctx *ctx = req->ctx;
 
-       spin_lock_irq(&ctx->completion_lock);
+       spin_lock_irq(&ctx->timeout_lock);
        /*
         * If the back reference is NULL, then our linked request finished
         * before we got a chance to setup the timer
@@ -6449,7 +6448,7 @@ static void io_queue_linked_timeout(struct io_kiocb *req)
                hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
                                data->mode);
        }
-       spin_unlock_irq(&ctx->completion_lock);
+       spin_unlock_irq(&ctx->timeout_lock);
        /* drop submission reference */
        io_put_req(req);
 }
@@ -6462,6 +6461,11 @@ static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
            nxt->opcode != IORING_OP_LINK_TIMEOUT)
                return NULL;
 
+       /* linked timeouts should have two refs once prep'ed */
+       io_req_refcount(req);
+       io_req_refcount(nxt);
+       req_ref_get(nxt);
+
        nxt->timeout.head = req;
        nxt->flags |= REQ_F_LTIMEOUT_ACTIVE;
        req->flags |= REQ_F_LINK_TIMEOUT;
@@ -6482,16 +6486,13 @@ issue_sqe:
         * doesn't support non-blocking read/write attempts
         */
        if (likely(!ret)) {
-               /* drop submission reference */
                if (req->flags & REQ_F_COMPLETE_INLINE) {
                        struct io_ring_ctx *ctx = req->ctx;
-                       struct io_comp_state *cs = &ctx->submit_state.comp;
+                       struct io_submit_state *state = &ctx->submit_state;
 
-                       cs->reqs[cs->nr++] = req;
-                       if (cs->nr == ARRAY_SIZE(cs->reqs))
+                       state->compl_reqs[state->compl_nr++] = req;
+                       if (state->compl_nr == ARRAY_SIZE(state->compl_reqs))
                                io_submit_flush_completions(ctx);
-               } else {
-                       io_put_req(req);
                }
        } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
                switch (io_arm_poll_handler(req)) {
@@ -6571,8 +6572,6 @@ static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
        req->user_data = READ_ONCE(sqe->user_data);
        req->file = NULL;
        req->fixed_rsrc_refs = NULL;
-       /* one is dropped after submission, the other at completion */
-       atomic_set(&req->refs, 2);
        req->task = current;
 
        /* enforce forwards compatibility on users */
@@ -6610,7 +6609,7 @@ static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
        }
 
        if (io_op_defs[req->opcode].needs_file) {
-               req->file = io_file_get(ctx, state, req, READ_ONCE(sqe->fd),
+               req->file = io_file_get(ctx, req, READ_ONCE(sqe->fd),
                                        (sqe_flags & IOSQE_FIXED_FILE));
                if (unlikely(!req->file))
                        ret = -EBADF;
@@ -6691,11 +6690,10 @@ static void io_submit_state_end(struct io_submit_state *state,
 {
        if (state->link.head)
                io_queue_sqe(state->link.head);
-       if (state->comp.nr)
+       if (state->compl_nr)
                io_submit_flush_completions(ctx);
        if (state->plug_started)
                blk_finish_plug(&state->plug);
-       io_state_file_put(state);
 }
 
 /*
@@ -6820,18 +6818,18 @@ static inline bool io_sqd_events_pending(struct io_sq_data *sqd)
 static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
 {
        /* Tell userspace we may need a wakeup call */
-       spin_lock_irq(&ctx->completion_lock);
+       spin_lock(&ctx->completion_lock);
        WRITE_ONCE(ctx->rings->sq_flags,
                   ctx->rings->sq_flags | IORING_SQ_NEED_WAKEUP);
-       spin_unlock_irq(&ctx->completion_lock);
+       spin_unlock(&ctx->completion_lock);
 }
 
 static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
 {
-       spin_lock_irq(&ctx->completion_lock);
+       spin_lock(&ctx->completion_lock);
        WRITE_ONCE(ctx->rings->sq_flags,
                   ctx->rings->sq_flags & ~IORING_SQ_NEED_WAKEUP);
-       spin_unlock_irq(&ctx->completion_lock);
+       spin_unlock(&ctx->completion_lock);
 }
 
 static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries)
@@ -7064,7 +7062,7 @@ static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
        int ret;
 
        do {
-               io_cqring_overflow_flush(ctx, false);
+               io_cqring_overflow_flush(ctx);
                if (io_cqring_events(ctx) >= min_events)
                        return 0;
                if (!io_run_task_work())
@@ -7102,7 +7100,7 @@ static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
        trace_io_uring_cqring_wait(ctx, min_events);
        do {
                /* if we can't even flush overflow, don't wait for more */
-               if (!io_cqring_overflow_flush(ctx, false)) {
+               if (!io_cqring_overflow_flush(ctx)) {
                        ret = -EBUSY;
                        break;
                }
@@ -7678,11 +7676,11 @@ static void __io_rsrc_put_work(struct io_rsrc_node *ref_node)
                        bool lock_ring = ctx->flags & IORING_SETUP_IOPOLL;
 
                        io_ring_submit_lock(ctx, lock_ring);
-                       spin_lock_irq(&ctx->completion_lock);
+                       spin_lock(&ctx->completion_lock);
                        io_cqring_fill_event(ctx, prsrc->tag, 0, 0);
                        ctx->cq_extra++;
                        io_commit_cqring(ctx);
-                       spin_unlock_irq(&ctx->completion_lock);
+                       spin_unlock(&ctx->completion_lock);
                        io_cqring_ev_posted(ctx);
                        io_ring_submit_unlock(ctx, lock_ring);
                }
@@ -8622,33 +8620,29 @@ static void io_destroy_buffers(struct io_ring_ctx *ctx)
                __io_remove_buffers(ctx, buf, index, -1U);
 }
 
-static void io_req_cache_free(struct list_head *list, struct task_struct *tsk)
+static void io_req_cache_free(struct list_head *list)
 {
        struct io_kiocb *req, *nxt;
 
-       list_for_each_entry_safe(req, nxt, list, compl.list) {
-               if (tsk && req->task != tsk)
-                       continue;
-               list_del(&req->compl.list);
+       list_for_each_entry_safe(req, nxt, list, inflight_entry) {
+               list_del(&req->inflight_entry);
                kmem_cache_free(req_cachep, req);
        }
 }
 
 static void io_req_caches_free(struct io_ring_ctx *ctx)
 {
-       struct io_submit_state *submit_state = &ctx->submit_state;
-       struct io_comp_state *cs = &ctx->submit_state.comp;
+       struct io_submit_state *state = &ctx->submit_state;
 
        mutex_lock(&ctx->uring_lock);
 
-       if (submit_state->free_reqs) {
-               kmem_cache_free_bulk(req_cachep, submit_state->free_reqs,
-                                    submit_state->reqs);
-               submit_state->free_reqs = 0;
+       if (state->free_reqs) {
+               kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
+               state->free_reqs = 0;
        }
 
-       io_flush_cached_locked_reqs(ctx, cs);
-       io_req_cache_free(&cs->free_list, NULL);
+       io_flush_cached_locked_reqs(ctx, state);
+       io_req_cache_free(&state->free_list);
        mutex_unlock(&ctx->uring_lock);
 }
 
@@ -8857,8 +8851,8 @@ static void io_ring_exit_work(struct work_struct *work)
                mutex_lock(&ctx->uring_lock);
        }
        mutex_unlock(&ctx->uring_lock);
-       spin_lock_irq(&ctx->completion_lock);
-       spin_unlock_irq(&ctx->completion_lock);
+       spin_lock(&ctx->completion_lock);
+       spin_unlock(&ctx->completion_lock);
 
        io_ring_ctx_free(ctx);
 }
@@ -8870,16 +8864,18 @@ static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk,
        struct io_kiocb *req, *tmp;
        int canceled = 0;
 
-       spin_lock_irq(&ctx->completion_lock);
+       spin_lock(&ctx->completion_lock);
+       spin_lock_irq(&ctx->timeout_lock);
        list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
                if (io_match_task(req, tsk, cancel_all)) {
                        io_kill_timeout(req, -ECANCELED);
                        canceled++;
                }
        }
+       spin_unlock_irq(&ctx->timeout_lock);
        if (canceled != 0)
                io_commit_cqring(ctx);
-       spin_unlock_irq(&ctx->completion_lock);
+       spin_unlock(&ctx->completion_lock);
        if (canceled != 0)
                io_cqring_ev_posted(ctx);
        return canceled != 0;
@@ -8935,13 +8931,12 @@ static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
        bool ret;
 
        if (!cancel->all && (req->flags & REQ_F_LINK_TIMEOUT)) {
-               unsigned long flags;
                struct io_ring_ctx *ctx = req->ctx;
 
                /* protect against races with linked timeouts */
-               spin_lock_irqsave(&ctx->completion_lock, flags);
+               spin_lock(&ctx->completion_lock);
                ret = io_match_task(req, cancel->task, cancel->all);
-               spin_unlock_irqrestore(&ctx->completion_lock, flags);
+               spin_unlock(&ctx->completion_lock);
        } else {
                ret = io_match_task(req, cancel->task, cancel->all);
        }
@@ -8954,14 +8949,14 @@ static bool io_cancel_defer_files(struct io_ring_ctx *ctx,
        struct io_defer_entry *de;
        LIST_HEAD(list);
 
-       spin_lock_irq(&ctx->completion_lock);
+       spin_lock(&ctx->completion_lock);
        list_for_each_entry_reverse(de, &ctx->defer_list, list) {
                if (io_match_task(de->req, task, cancel_all)) {
                        list_cut_position(&list, &ctx->defer_list, &de->list);
                        break;
                }
        }
-       spin_unlock_irq(&ctx->completion_lock);
+       spin_unlock(&ctx->completion_lock);
        if (list_empty(&list))
                return false;
 
@@ -9375,7 +9370,7 @@ SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
         */
        ret = 0;
        if (ctx->flags & IORING_SETUP_SQPOLL) {
-               io_cqring_overflow_flush(ctx, false);
+               io_cqring_overflow_flush(ctx);
 
                if (unlikely(ctx->sq_data->thread == NULL)) {
                        ret = -EOWNERDEAD;
@@ -9511,7 +9506,7 @@ static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
                        io_uring_show_cred(m, index, cred);
        }
        seq_printf(m, "PollList:\n");
-       spin_lock_irq(&ctx->completion_lock);
+       spin_lock(&ctx->completion_lock);
        for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
                struct hlist_head *list = &ctx->cancel_hash[i];
                struct io_kiocb *req;
@@ -9520,7 +9515,7 @@ static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
                        seq_printf(m, "  op=%d, task_works=%d\n", req->opcode,
                                        req->task->task_works != NULL);
        }
-       spin_unlock_irq(&ctx->completion_lock);
+       spin_unlock(&ctx->completion_lock);
        if (has_lock)
                mutex_unlock(&ctx->uring_lock);
 }