Commit 8cb01fac authored by Pavel Begunkov's avatar Pavel Begunkov Committed by Jens Axboe

io_uring: deduplicate cancellation code

IORING_OP_ASYNC_CANCEL and IORING_OP_LINK_TIMEOUT have enough of
overlap, so extract a helper for request cancellation and use in both.
Also, removes some amount of ugliness because of success_ret.
Signed-off-by: default avatarPavel Begunkov <asml.silence@gmail.com>
Link: https://lore.kernel.org/r/900122b588e65b637e71bfec80a260726c6a54d6.1628981736.git.asml.silence@gmail.comSigned-off-by: default avatarJens Axboe <axboe@kernel.dk>
parent a8576af9
...@@ -5792,32 +5792,24 @@ static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data, ...@@ -5792,32 +5792,24 @@ static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data,
return ret; return ret;
} }
static void io_async_find_and_cancel(struct io_ring_ctx *ctx, static int io_try_cancel_userdata(struct io_kiocb *req, u64 sqe_addr)
struct io_kiocb *req, __u64 sqe_addr, __acquires(&req->ctx->completion_lock)
int success_ret)
{ {
struct io_ring_ctx *ctx = req->ctx;
int ret; int ret;
WARN_ON_ONCE(req->task != current);
ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx); ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx);
spin_lock(&ctx->completion_lock); spin_lock(&ctx->completion_lock);
if (ret != -ENOENT) if (ret != -ENOENT)
goto done; return ret;
spin_lock_irq(&ctx->timeout_lock); spin_lock_irq(&ctx->timeout_lock);
ret = io_timeout_cancel(ctx, sqe_addr); ret = io_timeout_cancel(ctx, sqe_addr);
spin_unlock_irq(&ctx->timeout_lock); spin_unlock_irq(&ctx->timeout_lock);
if (ret != -ENOENT) if (ret != -ENOENT)
goto done; return ret;
ret = io_poll_cancel(ctx, sqe_addr, false); return io_poll_cancel(ctx, sqe_addr, false);
done:
if (!ret)
ret = success_ret;
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);
} }
static int io_async_cancel_prep(struct io_kiocb *req, static int io_async_cancel_prep(struct io_kiocb *req,
...@@ -5841,17 +5833,7 @@ static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags) ...@@ -5841,17 +5833,7 @@ static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
struct io_tctx_node *node; struct io_tctx_node *node;
int ret; int ret;
/* tasks should wait for their io-wq threads, so safe w/o sync */ ret = io_try_cancel_userdata(req, sqe_addr);
ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx);
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) if (ret != -ENOENT)
goto done; goto done;
spin_unlock(&ctx->completion_lock); spin_unlock(&ctx->completion_lock);
...@@ -6418,9 +6400,17 @@ static void io_req_task_link_timeout(struct io_kiocb *req) ...@@ -6418,9 +6400,17 @@ static void io_req_task_link_timeout(struct io_kiocb *req)
{ {
struct io_kiocb *prev = req->timeout.prev; struct io_kiocb *prev = req->timeout.prev;
struct io_ring_ctx *ctx = req->ctx; struct io_ring_ctx *ctx = req->ctx;
int ret;
if (prev) { if (prev) {
io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME); 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_put_req(prev); io_put_req(prev);
io_put_req(req); io_put_req(req);
} else { } else {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment