public inbox for [email protected]
 help / color / mirror / Atom feed
* [PATCH for-5.9 0/2] fix deferred ->files cancel
@ 2020-09-05 21:45 Pavel Begunkov
  2020-09-05 21:45 ` [PATCH 1/2] io_uring: fix cancel of deferred reqs with ->files Pavel Begunkov
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Pavel Begunkov @ 2020-09-05 21:45 UTC (permalink / raw)
  To: Jens Axboe, io-uring

io_uring_cancel_files() may hangs because it can't find requests with
 ->files in defer_list.

Pavel Begunkov (2):
  io_uring: fix cancel of deferred reqs with ->files
  io_uring: fix linked deferred ->files cancellation

 fs/io_uring.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)

-- 
2.24.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/2] io_uring: fix cancel of deferred reqs with ->files
  2020-09-05 21:45 [PATCH for-5.9 0/2] fix deferred ->files cancel Pavel Begunkov
@ 2020-09-05 21:45 ` Pavel Begunkov
  2020-09-05 21:45 ` [PATCH 2/2] io_uring: fix linked deferred ->files cancellation Pavel Begunkov
  2020-09-05 22:03 ` [PATCH for-5.9 0/2] fix deferred ->files cancel Jens Axboe
  2 siblings, 0 replies; 4+ messages in thread
From: Pavel Begunkov @ 2020-09-05 21:45 UTC (permalink / raw)
  To: Jens Axboe, io-uring

While trying to cancel requests with ->files, it also should look for 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.

Signed-off-by: Pavel Begunkov <[email protected]>
---
 fs/io_uring.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/fs/io_uring.c b/fs/io_uring.c
index 2e3adf9d17dd..20b647afe206 100644
--- a/fs/io_uring.c
+++ b/fs/io_uring.c
@@ -8092,12 +8092,38 @@ static void io_attempt_cancel(struct io_ring_ctx *ctx, struct io_kiocb *req)
 	io_timeout_remove_link(ctx, req);
 }
 
+static void io_cancel_defer_files(struct io_ring_ctx *ctx,
+				  struct files_struct *files)
+{
+	struct io_defer_entry *de = NULL;
+	LIST_HEAD(list);
+
+	spin_lock_irq(&ctx->completion_lock);
+	list_for_each_entry_reverse(de, &ctx->defer_list, list)
+		if ((de->req->flags & REQ_F_WORK_INITIALIZED)
+			&& de->req->work.files == files) {
+			list_cut_position(&list, &ctx->defer_list, &de->list);
+			break;
+		}
+	spin_unlock_irq(&ctx->completion_lock);
+
+	while (!list_empty(&list)) {
+		de = list_first_entry(&list, struct io_defer_entry, list);
+		list_del_init(&de->list);
+		req_set_fail_links(de->req);
+		io_put_req(de->req);
+		io_req_complete(de->req, -ECANCELED);
+		kfree(de);
+	}
+}
+
 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);
 
-- 
2.24.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] io_uring: fix linked deferred ->files cancellation
  2020-09-05 21:45 [PATCH for-5.9 0/2] fix deferred ->files cancel Pavel Begunkov
  2020-09-05 21:45 ` [PATCH 1/2] io_uring: fix cancel of deferred reqs with ->files Pavel Begunkov
@ 2020-09-05 21:45 ` Pavel Begunkov
  2020-09-05 22:03 ` [PATCH for-5.9 0/2] fix deferred ->files cancel Jens Axboe
  2 siblings, 0 replies; 4+ messages in thread
From: Pavel Begunkov @ 2020-09-05 21:45 UTC (permalink / raw)
  To: Jens Axboe, io-uring

While looking for ->files in ->defer_list, consider that requests there
may actually be links.

Signed-off-by: Pavel Begunkov <[email protected]>
---
 fs/io_uring.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)

diff --git a/fs/io_uring.c b/fs/io_uring.c
index 20b647afe206..f56e2cad97cc 100644
--- a/fs/io_uring.c
+++ b/fs/io_uring.c
@@ -8018,6 +8018,26 @@ static bool io_match_link(struct io_kiocb *preq, struct io_kiocb *req)
 	return false;
 }
 
+static inline bool io_match_files(struct io_kiocb *req,
+				       struct files_struct *file)
+{
+	return (req->flags & REQ_F_WORK_INITIALIZED) && req->work.files == file;
+}
+
+static bool io_match_link_files(struct io_kiocb *req,
+				struct files_struct *files)
+{
+	struct io_kiocb *link;
+
+	if (io_match_files(req, files))
+		return true;
+	if (req->flags & REQ_F_LINK_HEAD)
+		list_for_each_entry(link, &req->link_list, link_list)
+			if (io_match_files(link, files))
+				return true;
+	return false;
+}
+
 /*
  * We're looking to cancel 'req' because it's holding on to our files, but
  * 'req' could be a link to another request. See if it is, and cancel that
@@ -8100,8 +8120,7 @@ static void io_cancel_defer_files(struct io_ring_ctx *ctx,
 
 	spin_lock_irq(&ctx->completion_lock);
 	list_for_each_entry_reverse(de, &ctx->defer_list, list)
-		if ((de->req->flags & REQ_F_WORK_INITIALIZED)
-			&& de->req->work.files == files) {
+		if (io_match_link_files(de->req, files)) {
 			list_cut_position(&list, &ctx->defer_list, &de->list);
 			break;
 		}
-- 
2.24.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH for-5.9 0/2] fix deferred ->files cancel
  2020-09-05 21:45 [PATCH for-5.9 0/2] fix deferred ->files cancel Pavel Begunkov
  2020-09-05 21:45 ` [PATCH 1/2] io_uring: fix cancel of deferred reqs with ->files Pavel Begunkov
  2020-09-05 21:45 ` [PATCH 2/2] io_uring: fix linked deferred ->files cancellation Pavel Begunkov
@ 2020-09-05 22:03 ` Jens Axboe
  2 siblings, 0 replies; 4+ messages in thread
From: Jens Axboe @ 2020-09-05 22:03 UTC (permalink / raw)
  To: Pavel Begunkov, io-uring

On 9/5/20 3:45 PM, Pavel Begunkov wrote:
> io_uring_cancel_files() may hangs because it can't find requests with
>  ->files in defer_list.

Thanks! Applied with a few style changes.

-- 
Jens Axboe


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-09-05 22:03 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-09-05 21:45 [PATCH for-5.9 0/2] fix deferred ->files cancel Pavel Begunkov
2020-09-05 21:45 ` [PATCH 1/2] io_uring: fix cancel of deferred reqs with ->files Pavel Begunkov
2020-09-05 21:45 ` [PATCH 2/2] io_uring: fix linked deferred ->files cancellation Pavel Begunkov
2020-09-05 22:03 ` [PATCH for-5.9 0/2] fix deferred ->files cancel Jens Axboe

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox