drm/edid/firmware: Add built-in edid/1280x720.bin firmware
[platform/kernel/linux-starfive.git] / io_uring / io_uring.c
index 8840cf3..38bc007 100644 (file)
@@ -495,7 +495,7 @@ static void io_eventfd_ops(struct rcu_head *rcu)
        int ops = atomic_xchg(&ev_fd->ops, 0);
 
        if (ops & BIT(IO_EVENTFD_OP_SIGNAL_BIT))
-               eventfd_signal(ev_fd->cq_ev_fd, 1);
+               eventfd_signal_mask(ev_fd->cq_ev_fd, 1, EPOLL_URING_WAKE);
 
        /* IO_EVENTFD_OP_FREE_BIT may not be set here depending on callback
         * ordering in a race but if references are 0 we know we have to free
@@ -531,7 +531,7 @@ static void io_eventfd_signal(struct io_ring_ctx *ctx)
                goto out;
 
        if (likely(eventfd_signal_allowed())) {
-               eventfd_signal(ev_fd->cq_ev_fd, 1);
+               eventfd_signal_mask(ev_fd->cq_ev_fd, 1, EPOLL_URING_WAKE);
        } else {
                atomic_inc(&ev_fd->refs);
                if (!atomic_fetch_or(BIT(IO_EVENTFD_OP_SIGNAL_BIT), &ev_fd->ops))
@@ -823,15 +823,19 @@ bool io_post_aux_cqe(struct io_ring_ctx *ctx,
        return filled;
 }
 
-static void __io_req_complete_put(struct io_kiocb *req)
+void io_req_complete_post(struct io_kiocb *req)
 {
+       struct io_ring_ctx *ctx = req->ctx;
+
+       io_cq_lock(ctx);
+       if (!(req->flags & REQ_F_CQE_SKIP))
+               __io_fill_cqe_req(ctx, req);
+
        /*
         * If we're the last reference to this request, add to our locked
         * free_list cache.
         */
        if (req_ref_put_and_test(req)) {
-               struct io_ring_ctx *ctx = req->ctx;
-
                if (req->flags & IO_REQ_LINK_FLAGS) {
                        if (req->flags & IO_DISARM_MASK)
                                io_disarm_next(req);
@@ -852,21 +856,6 @@ static void __io_req_complete_put(struct io_kiocb *req)
                wq_list_add_head(&req->comp_list, &ctx->locked_free_list);
                ctx->locked_free_nr++;
        }
-}
-
-void __io_req_complete_post(struct io_kiocb *req)
-{
-       if (!(req->flags & REQ_F_CQE_SKIP))
-               __io_fill_cqe_req(req->ctx, req);
-       __io_req_complete_put(req);
-}
-
-void io_req_complete_post(struct io_kiocb *req)
-{
-       struct io_ring_ctx *ctx = req->ctx;
-
-       io_cq_lock(ctx);
-       __io_req_complete_post(req);
        io_cq_unlock_post(ctx);
 }
 
@@ -876,9 +865,12 @@ inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags)
 }
 
 void io_req_complete_failed(struct io_kiocb *req, s32 res)
+       __must_hold(&ctx->uring_lock)
 {
        const struct io_op_def *def = &io_op_defs[req->opcode];
 
+       lockdep_assert_held(&req->ctx->uring_lock);
+
        req_set_fail(req);
        io_req_set_res(req, res, io_put_kbuf(req, IO_URING_F_UNLOCKED));
        if (def->fail)
@@ -1038,10 +1030,16 @@ static unsigned int handle_tw_list(struct llist_node *node,
                        /* if not contended, grab and improve batching */
                        *locked = mutex_trylock(&(*ctx)->uring_lock);
                        percpu_ref_get(&(*ctx)->refs);
-               }
+               } else if (!*locked)
+                       *locked = mutex_trylock(&(*ctx)->uring_lock);
                req->io_task_work.func(req, locked);
                node = next;
                count++;
+               if (unlikely(need_resched())) {
+                       ctx_flush_and_put(*ctx, locked);
+                       *ctx = NULL;
+                       cond_resched();
+               }
        }
 
        return count;
@@ -1109,13 +1107,18 @@ static void io_req_local_work_add(struct io_kiocb *req)
 {
        struct io_ring_ctx *ctx = req->ctx;
 
-       if (!llist_add(&req->io_task_work.node, &ctx->work_llist))
+       percpu_ref_get(&ctx->refs);
+
+       if (!llist_add(&req->io_task_work.node, &ctx->work_llist)) {
+               percpu_ref_put(&ctx->refs);
                return;
+       }
        /* need it for the following io_cqring_wake() */
        smp_mb__after_atomic();
 
        if (unlikely(atomic_read(&req->task->io_uring->in_idle))) {
                io_move_task_work_from_local(ctx);
+               percpu_ref_put(&ctx->refs);
                return;
        }
 
@@ -1125,9 +1128,10 @@ static void io_req_local_work_add(struct io_kiocb *req)
        if (ctx->has_evfd)
                io_eventfd_signal(ctx);
        __io_cqring_wake(ctx);
+       percpu_ref_put(&ctx->refs);
 }
 
-static inline void __io_req_task_work_add(struct io_kiocb *req, bool allow_local)
+void __io_req_task_work_add(struct io_kiocb *req, bool allow_local)
 {
        struct io_uring_task *tctx = req->task->io_uring;
        struct io_ring_ctx *ctx = req->ctx;
@@ -1159,11 +1163,6 @@ static inline void __io_req_task_work_add(struct io_kiocb *req, bool allow_local
        }
 }
 
-void io_req_task_work_add(struct io_kiocb *req)
-{
-       __io_req_task_work_add(req, true);
-}
-
 static void __cold io_move_task_work_from_local(struct io_ring_ctx *ctx)
 {
        struct llist_node *node;
@@ -1237,18 +1236,6 @@ int io_run_local_work(struct io_ring_ctx *ctx)
        return ret;
 }
 
-static void io_req_tw_post(struct io_kiocb *req, bool *locked)
-{
-       io_req_complete_post(req);
-}
-
-void io_req_tw_post_queue(struct io_kiocb *req, s32 res, u32 cflags)
-{
-       io_req_set_res(req, res, cflags);
-       req->io_task_work.func = io_req_tw_post;
-       io_req_task_work_add(req);
-}
-
 static void io_req_task_cancel(struct io_kiocb *req, bool *locked)
 {
        /* not needed for normal modes, but SQPOLL depends on it */
@@ -1610,7 +1597,7 @@ int io_req_prep_async(struct io_kiocb *req)
        const struct io_op_def *def = &io_op_defs[req->opcode];
 
        /* assign early for deferred execution for non-fixed file */
-       if (def->needs_file && !(req->flags & REQ_F_FIXED_FILE))
+       if (def->needs_file && !(req->flags & REQ_F_FIXED_FILE) && !req->file)
                req->file = io_file_get_normal(req, req->cqe.fd);
        if (!def->prep_async)
                return 0;
@@ -1635,6 +1622,7 @@ static u32 io_get_sequence(struct io_kiocb *req)
 }
 
 static __cold void io_drain_req(struct io_kiocb *req)
+       __must_hold(&ctx->uring_lock)
 {
        struct io_ring_ctx *ctx = req->ctx;
        struct io_defer_entry *de;
@@ -1652,17 +1640,12 @@ queue:
        }
        spin_unlock(&ctx->completion_lock);
 
-       ret = io_req_prep_async(req);
-       if (ret) {
-fail:
-               io_req_complete_failed(req, ret);
-               return;
-       }
        io_prep_async_link(req);
        de = kmalloc(sizeof(*de), GFP_KERNEL);
        if (!de) {
                ret = -ENOMEM;
-               goto fail;
+               io_req_complete_failed(req, ret);
+               return;
        }
 
        spin_lock(&ctx->completion_lock);
@@ -1757,7 +1740,7 @@ static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)
                return ret;
 
        /* If the op doesn't have a file, we're not polling for it */
-       if ((req->ctx->flags & IORING_SETUP_IOPOLL) && req->file)
+       if ((req->ctx->flags & IORING_SETUP_IOPOLL) && def->iopoll_queue)
                io_iopoll_req_issued(req, issue_flags);
 
        return 0;
@@ -1936,13 +1919,16 @@ static void io_queue_sqe_fallback(struct io_kiocb *req)
                req->flags &= ~REQ_F_HARDLINK;
                req->flags |= REQ_F_LINK;
                io_req_complete_failed(req, req->cqe.res);
-       } else if (unlikely(req->ctx->drain_active)) {
-               io_drain_req(req);
        } else {
                int ret = io_req_prep_async(req);
 
-               if (unlikely(ret))
+               if (unlikely(ret)) {
                        io_req_complete_failed(req, ret);
+                       return;
+               }
+
+               if (unlikely(req->ctx->drain_active))
+                       io_drain_req(req);
                else
                        io_queue_iowq(req, NULL);
        }
@@ -2358,9 +2344,9 @@ int io_run_task_work_sig(struct io_ring_ctx *ctx)
 /* when returns >0, the caller should retry */
 static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,
                                          struct io_wait_queue *iowq,
-                                         ktime_t timeout)
+                                         ktime_t *timeout)
 {
-       int ret;
+       int token, ret;
        unsigned long check_cq;
 
        /* make sure we run task_work before checking for signals */
@@ -2376,9 +2362,18 @@ static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,
                if (check_cq & BIT(IO_CHECK_CQ_DROPPED_BIT))
                        return -EBADR;
        }
-       if (!schedule_hrtimeout(&timeout, HRTIMER_MODE_ABS))
-               return -ETIME;
-       return 1;
+
+       /*
+        * Use io_schedule_prepare/finish, so cpufreq can take into account
+        * that the task is waiting for IO - turns out to be important for low
+        * QD IO.
+        */
+       token = io_schedule_prepare();
+       ret = 1;
+       if (!schedule_hrtimeout(timeout, HRTIMER_MODE_ABS))
+               ret = -ETIME;
+       io_schedule_finish(token);
+       return ret;
 }
 
 /*
@@ -2446,7 +2441,7 @@ static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
                }
                prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq,
                                                TASK_INTERRUPTIBLE);
-               ret = io_cqring_wait_schedule(ctx, &iowq, timeout);
+               ret = io_cqring_wait_schedule(ctx, &iowq, &timeout);
                cond_resched();
        } while (ret > 0);
 
@@ -2599,8 +2594,8 @@ static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx)
        io_eventfd_unregister(ctx);
        io_alloc_cache_free(&ctx->apoll_cache, io_apoll_cache_free);
        io_alloc_cache_free(&ctx->netmsg_cache, io_netmsg_cache_free);
-       mutex_unlock(&ctx->uring_lock);
        io_destroy_buffers(ctx);
+       mutex_unlock(&ctx->uring_lock);
        if (ctx->sq_creds)
                put_cred(ctx->sq_creds);
        if (ctx->submitter_task)
@@ -2673,7 +2668,7 @@ static __poll_t io_uring_poll(struct file *file, poll_table *wait)
         * pushs them to do the flush.
         */
 
-       if (io_cqring_events(ctx) || io_has_work(ctx))
+       if (__io_cqring_events_user(ctx) || io_has_work(ctx))
                mask |= EPOLLIN | EPOLLRDNORM;
 
        return mask;
@@ -2707,8 +2702,10 @@ static __cold void io_tctx_exit_cb(struct callback_head *cb)
        /*
         * When @in_idle, we're in cancellation and it's racy to remove the
         * node. It'll be removed by the end of cancellation, just ignore it.
+        * tctx can be NULL if the queueing of this task_work raced with
+        * work cancelation off the exec path.
         */
-       if (!atomic_read(&tctx->in_idle))
+       if (tctx && !atomic_read(&tctx->in_idle))
                io_uring_del_tctx_node((unsigned long)work->ctx);
        complete(&work->completion);
 }
@@ -2760,7 +2757,18 @@ static __cold void io_ring_exit_work(struct work_struct *work)
                        /* there is little hope left, don't run it too often */
                        interval = HZ * 60;
                }
-       } while (!wait_for_completion_timeout(&ctx->ref_comp, interval));
+               /*
+                * This is really an uninterruptible wait, as it has to be
+                * complete. But it's also run from a kworker, which doesn't
+                * take signals, so it's fine to make it interruptible. This
+                * avoids scenarios where we knowingly can wait much longer
+                * on completions, for example if someone does a SIGSTOP on
+                * a task that needs to finish task_work to make this loop
+                * complete. That's a synthetic situation that should not
+                * cause a stuck task backtrace, and hence a potential panic
+                * on stuck tasks if that is enabled.
+                */
+       } while (!wait_for_completion_interruptible_timeout(&ctx->ref_comp, interval));
 
        init_completion(&exit.completion);
        init_task_work(&exit.task_work, io_tctx_exit_cb);
@@ -2784,7 +2792,12 @@ static __cold void io_ring_exit_work(struct work_struct *work)
                        continue;
 
                mutex_unlock(&ctx->uring_lock);
-               wait_for_completion(&exit.completion);
+               /*
+                * See comment above for
+                * wait_for_completion_interruptible_timeout() on why this
+                * wait is marked as interruptible.
+                */
+               wait_for_completion_interruptible(&exit.completion);
                mutex_lock(&ctx->uring_lock);
        }
        mutex_unlock(&ctx->uring_lock);
@@ -2869,7 +2882,7 @@ static __cold bool io_cancel_defer_files(struct io_ring_ctx *ctx,
        while (!list_empty(&list)) {
                de = list_first_entry(&list, struct io_defer_entry, list);
                list_del_init(&de->list);
-               io_req_complete_failed(de->req, -ECANCELED);
+               io_req_task_queue_fail(de->req, -ECANCELED);
                kfree(de);
        }
        return true;
@@ -2930,6 +2943,7 @@ static __cold bool io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
                while (!wq_list_empty(&ctx->iopoll_list)) {
                        io_iopoll_try_reap_events(ctx);
                        ret = true;
+                       cond_resched();
                }
        }
 
@@ -3895,8 +3909,6 @@ static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
                return -EEXIST;
 
        if (ctx->restricted) {
-               if (opcode >= IORING_REGISTER_LAST)
-                       return -EINVAL;
                opcode = array_index_nospec(opcode, IORING_REGISTER_LAST);
                if (!test_bit(opcode, ctx->restrictions.register_op))
                        return -EACCES;
@@ -4052,6 +4064,9 @@ SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
        long ret = -EBADF;
        struct fd f;
 
+       if (opcode >= IORING_REGISTER_LAST)
+               return -EINVAL;
+
        f = fdget(fd);
        if (!f.file)
                return -EBADF;