Skip to content

Commit

Permalink
io_uring: hold uring_lock while completing failed polled io in io_wq_…
Browse files Browse the repository at this point in the history
…submit_work()

commit c07e671 upstream.

io_iopoll_complete() does not hold completion_lock to complete polled io,
so in io_wq_submit_work(), we can not call io_req_complete() directly, to
complete polled io, otherwise there maybe concurrent access to cqring,
defer_list, etc, which is not safe. Commit dad1b12 ("io_uring: always
let io_iopoll_complete() complete polled io") has fixed this issue, but
Pavel reported that IOPOLL apart from rw can do buf reg/unreg requests(
IORING_OP_PROVIDE_BUFFERS or IORING_OP_REMOVE_BUFFERS), so the fix is not
good.

Given that io_iopoll_complete() is always called under uring_lock, so here
for polled io, we can also get uring_lock to fix this issue.

Fixes: dad1b12 ("io_uring: always let io_iopoll_complete() complete polled io")
Cc: <stable@vger.kernel.org> # 5.5+
Signed-off-by: Xiaoguang Wang <xiaoguang.wang@linux.alibaba.com>
Reviewed-by: Pavel Begunkov <asml.silence@gmail.com>
[axboe: don't deref 'req' after completing it']
Signed-off-by: Jens Axboe <axboe@kernel.dk>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  • Loading branch information
Xiaoguang Wang authored and gregkh committed Dec 30, 2020
1 parent 72a016d commit 10e5fb0
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions fs/io_uring.c
Expand Up @@ -6081,19 +6081,28 @@ static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work)
}

if (ret) {
struct io_ring_ctx *lock_ctx = NULL;

if (req->ctx->flags & IORING_SETUP_IOPOLL)
lock_ctx = req->ctx;

/*
* io_iopoll_complete() does not hold completion_lock to complete
* polled io, so here for polled io, just mark it done and still let
* io_iopoll_complete() complete it.
* io_iopoll_complete() does not hold completion_lock to
* complete polled io, so here for polled io, we can not call
* io_req_complete() directly, otherwise there maybe concurrent
* access to cqring, defer_list, etc, which is not safe. Given
* that io_iopoll_complete() is always called under uring_lock,
* so here for polled io, we also get uring_lock to complete
* it.
*/
if (req->ctx->flags & IORING_SETUP_IOPOLL) {
struct kiocb *kiocb = &req->rw.kiocb;
if (lock_ctx)
mutex_lock(&lock_ctx->uring_lock);

kiocb_done(kiocb, ret, NULL);
} else {
req_set_fail_links(req);
io_req_complete(req, ret);
}
req_set_fail_links(req);
io_req_complete(req, ret);

if (lock_ctx)
mutex_unlock(&lock_ctx->uring_lock);
}

return io_steal_work(req);
Expand Down

0 comments on commit 10e5fb0

Please sign in to comment.