[PATCH] io_uring: fix possible deadlock in io_register_iowq_max_workers()

Hagar Hemdan posted 1 patch 1 year, 8 months ago
io_uring/register.c | 4 ++++
1 file changed, 4 insertions(+)
[PATCH] io_uring: fix possible deadlock in io_register_iowq_max_workers()
Posted by Hagar Hemdan 1 year, 8 months ago
The io_register_iowq_max_workers() function calls io_put_sq_data(),
which acquires the sqd->lock without releasing the uring_lock.
Similar to the commit 009ad9f0c6ee ("io_uring: drop ctx->uring_lock
before acquiring sqd->lock"), this can lead to a potential deadlock
situation.

To resolve this issue, the uring_lock is released before calling
io_put_sq_data(), and then it is re-acquired after the function call.

This change ensures that the locks are acquired in the correct
order, preventing the possibility of a deadlock.

Suggested-by: Maximilian Heyne <mheyne@amazon.de>
Signed-off-by: Hagar Hemdan <hagarhem@amazon.com>
---
only compile tested.
---
 io_uring/register.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/io_uring/register.c b/io_uring/register.c
index ef8c908346a4..c0010a66a6f2 100644
--- a/io_uring/register.c
+++ b/io_uring/register.c
@@ -355,8 +355,10 @@ static __cold int io_register_iowq_max_workers(struct io_ring_ctx *ctx,
 	}
 
 	if (sqd) {
+		mutex_unlock(&ctx->uring_lock);
 		mutex_unlock(&sqd->lock);
 		io_put_sq_data(sqd);
+		mutex_lock(&ctx->uring_lock);
 	}
 
 	if (copy_to_user(arg, new_count, sizeof(new_count)))
@@ -380,8 +382,10 @@ static __cold int io_register_iowq_max_workers(struct io_ring_ctx *ctx,
 	return 0;
 err:
 	if (sqd) {
+		mutex_unlock(&ctx->uring_lock);
 		mutex_unlock(&sqd->lock);
 		io_put_sq_data(sqd);
+		mutex_lock(&ctx->uring_lock);
 	}
 	return ret;
 }
-- 
2.40.1
Re: [PATCH] io_uring: fix possible deadlock in io_register_iowq_max_workers()
Posted by Jens Axboe 1 year, 8 months ago
On Tue, 04 Jun 2024 13:05:27 +0000, Hagar Hemdan wrote:
> The io_register_iowq_max_workers() function calls io_put_sq_data(),
> which acquires the sqd->lock without releasing the uring_lock.
> Similar to the commit 009ad9f0c6ee ("io_uring: drop ctx->uring_lock
> before acquiring sqd->lock"), this can lead to a potential deadlock
> situation.
> 
> To resolve this issue, the uring_lock is released before calling
> io_put_sq_data(), and then it is re-acquired after the function call.
> 
> [...]

Applied, thanks!

[1/1] io_uring: fix possible deadlock in io_register_iowq_max_workers()
      commit: a59035de589f31f195ed1fff97007d332552a72b

Best regards,
-- 
Jens Axboe