Skip to contentSkip to conversation

->flush() fixes

PATCH In review Pavel Begunkov 2 participants 5 messages Sep 28, 2020

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

cover ->flush() fixes

It might fix flush() problems reported by syzkaller, but I haven't verified it. Jens, please tell if there was a good reason to have io_sq_thread_stop() in io_uring_flush().

Pavel Begunkov (2):
io_uring: fix use-after-free ->files
io_uring: fix unsynchronised removal of sq_data

 fs/io_uring.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
Signature · 1 line
2.24.0
#1 lore raw

PATCH 1/2 io_uring: fix use-after-free ->files

io_uring_flush() should first clear ->sqo_files and only then trying to cancel requests with io_uring_cancel_files(). Otherwise, SQ thread may wake up right after io_uring_cancel_file() and submit new requests with the going away files.

---
 fs/io_uring.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/io_uring.c +2 -2
index 7ee5e18218c2..6523500e4ae2 100644
@@ -8548,8 +8548,6 @@ static int io_uring_flush(struct file *file, void *data)
 {
 	struct io_ring_ctx *ctx = file->private_data;
 
-	io_uring_cancel_files(ctx, data);
-
 	/*
 	 * If the task is going away, cancel work it may have pending
 	 */
@@ -8570,6 +8568,8 @@ static int io_uring_flush(struct file *file, void *data)
 		io_sq_thread_unpark(sqd);
 	}
 
+	io_uring_cancel_files(ctx, data);
+
 	return 0;
 }
 
Signature · 1 line
2.24.0
#2 lore raw

PATCH 2/2 io_uring: fix unsynchronised removal of sq_data

In reply to Pavel Begunkov

Remove io_sq_thread_stop() from io_uring_flush() and just clear ->sqo_files, etc. instead. The first reason is that io_sq_thread_stop() null's ctx->sq_data without any lock held, that's racy. Also, it kills an SQPOLL thread even though there may be other processes wanting to continue using the io_uring instance.

[email protected]

---
 fs/io_uring.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
fs/io_uring.c +7 -7
index 6523500e4ae2..0ab0c3aefefd 100644
@@ -8548,13 +8548,7 @@ static int io_uring_flush(struct file *file, void *data)
 {
 	struct io_ring_ctx *ctx = file->private_data;
 
-	/*
-	 * If the task is going away, cancel work it may have pending
-	 */
-	if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
-		io_sq_thread_stop(ctx);
-		io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, current, true);
-	} else if (ctx->flags & IORING_SETUP_SQPOLL) {
+	if (ctx->flags & IORING_SETUP_SQPOLL) {
 		struct io_sq_data *sqd = ctx->sq_data;
 
 		/* Ring is being closed, mark us as neding new assignment */
@@ -8568,6 +8562,12 @@ static int io_uring_flush(struct file *file, void *data)
 		io_sq_thread_unpark(sqd);
 	}
 
+	/*
+	 * If the task is going away, cancel work it may have pending
+	 */
+	if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
+		io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, current, true);
+
 	io_uring_cancel_files(ctx, data);
 
 	return 0;
Signature · 1 line
2.24.0
#3 lore raw
In reply to Pavel Begunkov
Quoted 3 lines · Pavel Begunkov

It might fix flush() problems reported by syzkaller, but I haven't verified it. Jens, please tell if there was a good reason to have io_sq_thread_stop() in io_uring_flush().

It looks like it shouldn't be a problem removing it, because an sqpoll task sets req->task to itself, and such requests are cancelled and waited by io_uring_release() if that is required.

Quoted 8 lines

Pavel Begunkov (2):
io_uring: fix use-after-free ->files
io_uring: fix unsynchronised removal of sq_data

 fs/io_uring.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
Signature · 1 line
Pavel Begunkov
maintainer
#4 lore raw
In reply to Pavel Begunkov
Quoted 3 lines · Pavel Begunkov

It might fix flush() problems reported by syzkaller, but I haven't verified it. Jens, please tell if there was a good reason to have io_sq_thread_stop() in io_uring_flush().

No, it's not needed.

Signature · 1 line
Jens Axboe