From e90866ba51d062d6834ca5b7fc708e485a4d0bcf Mon Sep 17 00:00:00 2001 From: Pavel Begunkov Date: Sun, 6 Sep 2020 00:45:14 +0300 Subject: [PATCH 2501/2944] io_uring: fix cancel of deferred reqs with ->files fix #32041940 commit b7ddce3cbf010edbfac6c6d8cc708560a7bcd7a4 upstream. While trying to cancel requests with ->files, it also should look for requests in ->defer_list, otherwise it might end up hanging a thread. Cancel all requests in ->defer_list up to the last request there with matching ->files, that's needed to follow drain ordering semantics. [Joseph] struct io_defer_entry is introduced by commit 27dc8338e5fb ("io_uring: use non-intrusive list for defer"), and if we backport this commit, many other commits will get involved. So use io_kiocb->list instead. Signed-off-by: Pavel Begunkov Signed-off-by: Jens Axboe Signed-off-by: Joseph Qi Acked-by: Xiaoguang Wang --- fs/io_uring.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/fs/io_uring.c b/fs/io_uring.c index 7e290ba..1f0c2a1 100644 --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -8181,12 +8181,38 @@ static bool io_wq_files_match(struct io_wq_work *work, void *data) return work->files == files; } +static void io_cancel_defer_files(struct io_ring_ctx *ctx, + struct files_struct *files) +{ + struct io_kiocb *req = NULL; + LIST_HEAD(list); + + spin_lock_irq(&ctx->completion_lock); + list_for_each_entry_reverse(req, &ctx->defer_list, list) { + if ((req->flags & REQ_F_WORK_INITIALIZED) + && req->work.files == files) { + list_cut_position(&list, &ctx->defer_list, &req->list); + break; + } + } + spin_unlock_irq(&ctx->completion_lock); + + while (!list_empty(&list)) { + req = list_first_entry(&list, struct io_kiocb, list); + list_del_init(&req->list); + req_set_fail_links(req); + io_put_req(req); + io_req_complete(req, -ECANCELED); + } +} + static void io_uring_cancel_files(struct io_ring_ctx *ctx, struct files_struct *files) { if (list_empty_careful(&ctx->inflight_list)) return; + io_cancel_defer_files(ctx, files); /* cancel all at once, should be faster than doing it one by one*/ io_wq_cancel_cb(ctx->io_wq, io_wq_files_match, files, true); -- 1.8.3.1