All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] ublk: fix AB-BA lockdep warning
@ 2023-05-17 13:34 Ming Lei
  2023-05-18  2:01 ` Shinichiro Kawasaki
  2023-05-18 13:59 ` Jens Axboe
  0 siblings, 2 replies; 4+ messages in thread
From: Ming Lei @ 2023-05-17 13:34 UTC (permalink / raw)
  To: Jens Axboe; +Cc: linux-block, Ming Lei, Shinichiro Kawasaki, Ziyang Zhang

When handling UBLK_IO_FETCH_REQ, ctx->uring_lock is grabbed first, then
ub->mutex is acquired.

When handling UBLK_CMD_STOP_DEV or UBLK_CMD_DEL_DEV, ub->mutex is
grabbed first, then calling io_uring_cmd_done() for canceling uring
command, in which ctx->uring_lock may be required.

Real deadlock only happens when all the above commands are issued from
same uring context, and in reality different uring contexts are often used
for handing control command and IO command.

Fix the issue by using io_uring_cmd_complete_in_task() to cancel command
in ublk_cancel_dev(ublk_cancel_queue).

Reported-by: Shinichiro Kawasaki <shinichiro.kawasaki@wdc.com>
Closes: https://lore.kernel.org/linux-block/becol2g7sawl4rsjq2dztsbc7mqypfqko6wzsyoyazqydoasml@rcxarzwidrhk
Cc: Ziyang Zhang <ZiyangZhang@linux.alibaba.com>
Signed-off-by: Ming Lei <ming.lei@redhat.com>
---
 drivers/block/ublk_drv.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/drivers/block/ublk_drv.c b/drivers/block/ublk_drv.c
index c7ed5d69e9ee..33d3298a0da1 100644
--- a/drivers/block/ublk_drv.c
+++ b/drivers/block/ublk_drv.c
@@ -1120,6 +1120,11 @@ static inline bool ublk_queue_ready(struct ublk_queue *ubq)
 	return ubq->nr_io_ready == ubq->q_depth;
 }
 
+static void ublk_cmd_cancel_cb(struct io_uring_cmd *cmd, unsigned issue_flags)
+{
+	io_uring_cmd_done(cmd, UBLK_IO_RES_ABORT, 0, issue_flags);
+}
+
 static void ublk_cancel_queue(struct ublk_queue *ubq)
 {
 	int i;
@@ -1131,8 +1136,8 @@ static void ublk_cancel_queue(struct ublk_queue *ubq)
 		struct ublk_io *io = &ubq->ios[i];
 
 		if (io->flags & UBLK_IO_FLAG_ACTIVE)
-			io_uring_cmd_done(io->cmd, UBLK_IO_RES_ABORT, 0,
-						IO_URING_F_UNLOCKED);
+			io_uring_cmd_complete_in_task(io->cmd,
+						      ublk_cmd_cancel_cb);
 	}
 
 	/* all io commands are canceled */
-- 
2.38.1


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

* Re: [PATCH] ublk: fix AB-BA lockdep warning
  2023-05-17 13:34 [PATCH] ublk: fix AB-BA lockdep warning Ming Lei
@ 2023-05-18  2:01 ` Shinichiro Kawasaki
  2023-05-18  2:59   ` Ming Lei
  2023-05-18 13:59 ` Jens Axboe
  1 sibling, 1 reply; 4+ messages in thread
From: Shinichiro Kawasaki @ 2023-05-18  2:01 UTC (permalink / raw)
  To: Ming Lei; +Cc: Jens Axboe, linux-block, Ziyang Zhang

On May 17, 2023 / 21:34, Ming Lei wrote:
> When handling UBLK_IO_FETCH_REQ, ctx->uring_lock is grabbed first, then
> ub->mutex is acquired.
> 
> When handling UBLK_CMD_STOP_DEV or UBLK_CMD_DEL_DEV, ub->mutex is
> grabbed first, then calling io_uring_cmd_done() for canceling uring
> command, in which ctx->uring_lock may be required.
> 
> Real deadlock only happens when all the above commands are issued from
> same uring context, and in reality different uring contexts are often used
> for handing control command and IO command.
> 
> Fix the issue by using io_uring_cmd_complete_in_task() to cancel command
> in ublk_cancel_dev(ublk_cancel_queue).
> 
> Reported-by: Shinichiro Kawasaki <shinichiro.kawasaki@wdc.com>
> Closes: https://lore.kernel.org/linux-block/becol2g7sawl4rsjq2dztsbc7mqypfqko6wzsyoyazqydoasml@rcxarzwidrhk
> Cc: Ziyang Zhang <ZiyangZhang@linux.alibaba.com>
> Signed-off-by: Ming Lei <ming.lei@redhat.com>

Using Ziyang's new blktests test cases, I confirmed this patch avoids the
failure I reported. Thanks.

Tested-by: Shinichiro Kawasaki <shinichiro.kawasaki@wdc.com>

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

* Re: [PATCH] ublk: fix AB-BA lockdep warning
  2023-05-18  2:01 ` Shinichiro Kawasaki
@ 2023-05-18  2:59   ` Ming Lei
  0 siblings, 0 replies; 4+ messages in thread
From: Ming Lei @ 2023-05-18  2:59 UTC (permalink / raw)
  To: Shinichiro Kawasaki; +Cc: Jens Axboe, linux-block, Ziyang Zhang

On Thu, May 18, 2023 at 02:01:17AM +0000, Shinichiro Kawasaki wrote:
> On May 17, 2023 / 21:34, Ming Lei wrote:
> > When handling UBLK_IO_FETCH_REQ, ctx->uring_lock is grabbed first, then
> > ub->mutex is acquired.
> > 
> > When handling UBLK_CMD_STOP_DEV or UBLK_CMD_DEL_DEV, ub->mutex is
> > grabbed first, then calling io_uring_cmd_done() for canceling uring
> > command, in which ctx->uring_lock may be required.
> > 
> > Real deadlock only happens when all the above commands are issued from
> > same uring context, and in reality different uring contexts are often used
> > for handing control command and IO command.
> > 
> > Fix the issue by using io_uring_cmd_complete_in_task() to cancel command
> > in ublk_cancel_dev(ublk_cancel_queue).
> > 
> > Reported-by: Shinichiro Kawasaki <shinichiro.kawasaki@wdc.com>
> > Closes: https://lore.kernel.org/linux-block/becol2g7sawl4rsjq2dztsbc7mqypfqko6wzsyoyazqydoasml@rcxarzwidrhk
> > Cc: Ziyang Zhang <ZiyangZhang@linux.alibaba.com>
> > Signed-off-by: Ming Lei <ming.lei@redhat.com>
> 
> Using Ziyang's new blktests test cases, I confirmed this patch avoids the
> failure I reported. Thanks.
> 
> Tested-by: Shinichiro Kawasaki <shinichiro.kawasaki@wdc.com>

Shinichiro, Thanks for the test!

-- 
Ming


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

* Re: [PATCH] ublk: fix AB-BA lockdep warning
  2023-05-17 13:34 [PATCH] ublk: fix AB-BA lockdep warning Ming Lei
  2023-05-18  2:01 ` Shinichiro Kawasaki
@ 2023-05-18 13:59 ` Jens Axboe
  1 sibling, 0 replies; 4+ messages in thread
From: Jens Axboe @ 2023-05-18 13:59 UTC (permalink / raw)
  To: Ming Lei; +Cc: linux-block, Shinichiro Kawasaki, Ziyang Zhang


On Wed, 17 May 2023 21:34:08 +0800, Ming Lei wrote:
> When handling UBLK_IO_FETCH_REQ, ctx->uring_lock is grabbed first, then
> ub->mutex is acquired.
> 
> When handling UBLK_CMD_STOP_DEV or UBLK_CMD_DEL_DEV, ub->mutex is
> grabbed first, then calling io_uring_cmd_done() for canceling uring
> command, in which ctx->uring_lock may be required.
> 
> [...]

Applied, thanks!

[1/1] ublk: fix AB-BA lockdep warning
      commit: ac5902f84bb546c64aea02c439c2579cbf40318f

Best regards,
-- 
Jens Axboe




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

end of thread, other threads:[~2023-05-18 13:59 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-17 13:34 [PATCH] ublk: fix AB-BA lockdep warning Ming Lei
2023-05-18  2:01 ` Shinichiro Kawasaki
2023-05-18  2:59   ` Ming Lei
2023-05-18 13:59 ` Jens Axboe

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.