diff mbox series

[5.10] io_uring: fix link lookup racing with link timeout

Message ID c51ca0ebb562612484b248038142184572ecf2dc.1604614925.git.asml.silence@gmail.com
State Accepted
Commit 9a472ef7a3690ac0b77ebfb04c88fa795de2adea
Headers show
Series [5.10] io_uring: fix link lookup racing with link timeout | expand

Commit Message

Pavel Begunkov Nov. 5, 2020, 10:31 p.m. UTC
We can't just go over linked requests because it may race with linked
timeouts. Take ctx->completion_lock in that case.

Cc: stable@vger.kernel.org # v5.7+
Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
---
 fs/io_uring.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

Comments

Jens Axboe Nov. 5, 2020, 11 p.m. UTC | #1
On 11/5/20 3:31 PM, Pavel Begunkov wrote:
> We can't just go over linked requests because it may race with linked

> timeouts. Take ctx->completion_lock in that case.


Nice catch - applied, thanks.

-- 
Jens Axboe
diff mbox series

Patch

diff --git a/fs/io_uring.c b/fs/io_uring.c
index 29f1417690d5..8018c7076b25 100644
--- a/fs/io_uring.c
+++ b/fs/io_uring.c
@@ -8470,7 +8470,21 @@  static bool io_timeout_remove_link(struct io_ring_ctx *ctx,
 
 static bool io_cancel_link_cb(struct io_wq_work *work, void *data)
 {
-	return io_match_link(container_of(work, struct io_kiocb, work), data);
+	struct io_kiocb *req = container_of(work, struct io_kiocb, work);
+	bool ret;
+
+	if (req->flags & REQ_F_LINK_TIMEOUT) {
+		unsigned long flags;
+		struct io_ring_ctx *ctx = req->ctx;
+
+		/* protect against races with linked timeouts */
+		spin_lock_irqsave(&ctx->completion_lock, flags);
+		ret = io_match_link(req, data);
+		spin_unlock_irqrestore(&ctx->completion_lock, flags);
+	} else {
+		ret = io_match_link(req, data);
+	}
+	return ret;
 }
 
 static void io_attempt_cancel(struct io_ring_ctx *ctx, struct io_kiocb *req)