Skip to contentSkip to conversation

fix deferred ->files cancel

PATCH Applied Pavel Begunkov 2 participants 4 messages Sep 5, 2020

Subsystems (2)
lore
Share & tools
lore
#1 lore raw

PATCH 1/2 io_uring: fix cancel of deferred reqs with ->files

In reply to Pavel Begunkov

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.

---
 fs/io_uring.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
fs/io_uring.c +26 -0
index 2e3adf9d17dd..20b647afe206 100644
@@ -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);
 
Signature · 1 line
2.24.0
#2 lore raw

PATCH 2/2 io_uring: fix linked deferred ->files cancellation

In reply to Pavel Begunkov

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

---
 fs/io_uring.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)
fs/io_uring.c +21 -2
index 20b647afe206..f56e2cad97cc 100644
@@ -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;
 		}
Signature · 1 line
2.24.0
#0 lore raw

cover fix deferred ->files cancel

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(+)
Signature · 1 line
2.24.0
maintainer Applied
#3 lore raw
Quoted 2 lines · Pavel Begunkov

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

Thanks! Applied with a few style changes.

Signature · 1 line
Jens Axboe