Linux block layer
 help / color / mirror / Atom feed
* [PATCH] loop: use kiocb helpers to fix lockdep warning
@ 2025-07-16 11:48 Ming Lei
  2025-07-16 12:20 ` Christoph Hellwig
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Ming Lei @ 2025-07-16 11:48 UTC (permalink / raw)
  To: Jens Axboe, linux-block; +Cc: Ming Lei, Changhui Zhong

The lockdep tool can report a circular lock dependency warning in the loop
driver's AIO read/write path:

```
[ 6540.587728] kworker/u96:5/72779 is trying to acquire lock:
[ 6540.593856] ff110001b5968440 (sb_writers#9){.+.+}-{0:0}, at: loop_process_work+0x11a/0xf70 [loop]
[ 6540.603786]
[ 6540.603786] but task is already holding lock:
[ 6540.610291] ff110001b5968440 (sb_writers#9){.+.+}-{0:0}, at: loop_process_work+0x11a/0xf70 [loop]
[ 6540.620210]
[ 6540.620210] other info that might help us debug this:
[ 6540.627499]  Possible unsafe locking scenario:
[ 6540.627499]
[ 6540.634110]        CPU0
[ 6540.636841]        ----
[ 6540.639574]   lock(sb_writers#9);
[ 6540.643281]   lock(sb_writers#9);
[ 6540.646988]
[ 6540.646988]  *** DEADLOCK ***
```

This patch fixes the issue by using the AIO-specific helpers
`kiocb_start_write()` and `kiocb_end_write()`. These functions are
designed to be used with a `kiocb` and manage write sequencing
correctly for asynchronous I/O without introducing the problematic
lock dependency.

The `kiocb` is already part of the `loop_cmd` struct, so this change
also simplifies the completion function `lo_rw_aio_do_completion()` by
using the `iocb` from the `cmd` struct directly, instead of retrieving
the loop device from the request queue.

Fixes: 39d86db34e41 ("loop: add file_start_write() and file_end_write()")
Cc: Changhui Zhong <czhong@redhat.com>
Signed-off-by: Ming Lei <ming.lei@redhat.com>
---
 drivers/block/loop.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/drivers/block/loop.c b/drivers/block/loop.c
index 500840e4a74e..8d994cae3b83 100644
--- a/drivers/block/loop.c
+++ b/drivers/block/loop.c
@@ -308,14 +308,13 @@ static void lo_complete_rq(struct request *rq)
 static void lo_rw_aio_do_completion(struct loop_cmd *cmd)
 {
 	struct request *rq = blk_mq_rq_from_pdu(cmd);
-	struct loop_device *lo = rq->q->queuedata;
 
 	if (!atomic_dec_and_test(&cmd->ref))
 		return;
 	kfree(cmd->bvec);
 	cmd->bvec = NULL;
 	if (req_op(rq) == REQ_OP_WRITE)
-		file_end_write(lo->lo_backing_file);
+		kiocb_end_write(&cmd->iocb);
 	if (likely(!blk_should_fake_timeout(rq->q)))
 		blk_mq_complete_request(rq);
 }
@@ -391,7 +390,7 @@ static int lo_rw_aio(struct loop_device *lo, struct loop_cmd *cmd,
 	}
 
 	if (rw == ITER_SOURCE) {
-		file_start_write(lo->lo_backing_file);
+		kiocb_start_write(&cmd->iocb);
 		ret = file->f_op->write_iter(&cmd->iocb, &iter);
 	} else
 		ret = file->f_op->read_iter(&cmd->iocb, &iter);
-- 
2.50.1


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

* Re: [PATCH] loop: use kiocb helpers to fix lockdep warning
  2025-07-16 11:48 [PATCH] loop: use kiocb helpers to fix lockdep warning Ming Lei
@ 2025-07-16 12:20 ` Christoph Hellwig
  2025-07-16 12:31   ` Ming Lei
  2025-07-16 12:34 ` Jens Axboe
  2025-07-18  1:22 ` Changhui Zhong
  2 siblings, 1 reply; 6+ messages in thread
From: Christoph Hellwig @ 2025-07-16 12:20 UTC (permalink / raw)
  To: Ming Lei; +Cc: Jens Axboe, linux-block, Changhui Zhong

On Wed, Jul 16, 2025 at 07:48:08PM +0800, Ming Lei wrote:
> This patch fixes the issue by using the AIO-specific helpers
> `kiocb_start_write()` and `kiocb_end_write()`. These functions are
> designed to be used with a `kiocb` and manage write sequencing
> correctly for asynchronous I/O without introducing the problematic
> lock dependency.

Maybe explain what it actually does.  That is drop and reacquire the
lockdep notion of a held lock, because the process is not holding it
but instead handing it of to the one handling the completion of the
asynchronous I/O.

The change looks fine to me, but this commit log is rather confusing.


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

* Re: [PATCH] loop: use kiocb helpers to fix lockdep warning
  2025-07-16 12:20 ` Christoph Hellwig
@ 2025-07-16 12:31   ` Ming Lei
  2025-07-17  4:31     ` Christoph Hellwig
  0 siblings, 1 reply; 6+ messages in thread
From: Ming Lei @ 2025-07-16 12:31 UTC (permalink / raw)
  To: Christoph Hellwig; +Cc: Jens Axboe, linux-block, Changhui Zhong

On Wed, Jul 16, 2025 at 8:20 PM Christoph Hellwig <hch@infradead.org> wrote:
>
> On Wed, Jul 16, 2025 at 07:48:08PM +0800, Ming Lei wrote:
> > This patch fixes the issue by using the AIO-specific helpers
> > `kiocb_start_write()` and `kiocb_end_write()`. These functions are
> > designed to be used with a `kiocb` and manage write sequencing
> > correctly for asynchronous I/O without introducing the problematic
> > lock dependency.
>
> Maybe explain what it actually does.  That is drop and reacquire the
> lockdep notion of a held lock, because the process is not holding it
> but instead handing it of to the one handling the completion of the
> asynchronous I/O.
>
> The change looks fine to me, but this commit log is rather confusing.

It is actually the typical use case for aio, please see use in fs/aio.c.

Thanks,
Ming


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

* Re: [PATCH] loop: use kiocb helpers to fix lockdep warning
  2025-07-16 11:48 [PATCH] loop: use kiocb helpers to fix lockdep warning Ming Lei
  2025-07-16 12:20 ` Christoph Hellwig
@ 2025-07-16 12:34 ` Jens Axboe
  2025-07-18  1:22 ` Changhui Zhong
  2 siblings, 0 replies; 6+ messages in thread
From: Jens Axboe @ 2025-07-16 12:34 UTC (permalink / raw)
  To: linux-block, Ming Lei; +Cc: Changhui Zhong


On Wed, 16 Jul 2025 19:48:08 +0800, Ming Lei wrote:
> The lockdep tool can report a circular lock dependency warning in the loop
> driver's AIO read/write path:
> 
> ```
> [ 6540.587728] kworker/u96:5/72779 is trying to acquire lock:
> [ 6540.593856] ff110001b5968440 (sb_writers#9){.+.+}-{0:0}, at: loop_process_work+0x11a/0xf70 [loop]
> [ 6540.603786]
> [ 6540.603786] but task is already holding lock:
> [ 6540.610291] ff110001b5968440 (sb_writers#9){.+.+}-{0:0}, at: loop_process_work+0x11a/0xf70 [loop]
> [ 6540.620210]
> [ 6540.620210] other info that might help us debug this:
> [ 6540.627499]  Possible unsafe locking scenario:
> [ 6540.627499]
> [ 6540.634110]        CPU0
> [ 6540.636841]        ----
> [ 6540.639574]   lock(sb_writers#9);
> [ 6540.643281]   lock(sb_writers#9);
> [ 6540.646988]
> [ 6540.646988]  *** DEADLOCK ***
> ```
> 
> [...]

Applied, thanks!

[1/1] loop: use kiocb helpers to fix lockdep warning
      commit: c4706c5058a7bd7d7c20f3b24a8f523ecad44e83

Best regards,
-- 
Jens Axboe




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

* Re: [PATCH] loop: use kiocb helpers to fix lockdep warning
  2025-07-16 12:31   ` Ming Lei
@ 2025-07-17  4:31     ` Christoph Hellwig
  0 siblings, 0 replies; 6+ messages in thread
From: Christoph Hellwig @ 2025-07-17  4:31 UTC (permalink / raw)
  To: Ming Lei; +Cc: Christoph Hellwig, Jens Axboe, linux-block, Changhui Zhong

On Wed, Jul 16, 2025 at 08:31:56PM +0800, Ming Lei wrote:
> On Wed, Jul 16, 2025 at 8:20 PM Christoph Hellwig <hch@infradead.org> wrote:
> >
> > On Wed, Jul 16, 2025 at 07:48:08PM +0800, Ming Lei wrote:
> > > This patch fixes the issue by using the AIO-specific helpers
> > > `kiocb_start_write()` and `kiocb_end_write()`. These functions are
> > > designed to be used with a `kiocb` and manage write sequencing
> > > correctly for asynchronous I/O without introducing the problematic
> > > lock dependency.
> >
> > Maybe explain what it actually does.  That is drop and reacquire the
> > lockdep notion of a held lock, because the process is not holding it
> > but instead handing it of to the one handling the completion of the
> > asynchronous I/O.
> >
> > The change looks fine to me, but this commit log is rather confusing.
> 
> It is actually the typical use case for aio, please see use in fs/aio.c.

I know, that's not the point.


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

* Re: [PATCH] loop: use kiocb helpers to fix lockdep warning
  2025-07-16 11:48 [PATCH] loop: use kiocb helpers to fix lockdep warning Ming Lei
  2025-07-16 12:20 ` Christoph Hellwig
  2025-07-16 12:34 ` Jens Axboe
@ 2025-07-18  1:22 ` Changhui Zhong
  2 siblings, 0 replies; 6+ messages in thread
From: Changhui Zhong @ 2025-07-18  1:22 UTC (permalink / raw)
  To: Ming Lei; +Cc: Jens Axboe, linux-block

On Wed, Jul 16, 2025 at 7:48 PM Ming Lei <ming.lei@redhat.com> wrote:
>
> The lockdep tool can report a circular lock dependency warning in the loop
> driver's AIO read/write path:
>
> ```
> [ 6540.587728] kworker/u96:5/72779 is trying to acquire lock:
> [ 6540.593856] ff110001b5968440 (sb_writers#9){.+.+}-{0:0}, at: loop_process_work+0x11a/0xf70 [loop]
> [ 6540.603786]
> [ 6540.603786] but task is already holding lock:
> [ 6540.610291] ff110001b5968440 (sb_writers#9){.+.+}-{0:0}, at: loop_process_work+0x11a/0xf70 [loop]
> [ 6540.620210]
> [ 6540.620210] other info that might help us debug this:
> [ 6540.627499]  Possible unsafe locking scenario:
> [ 6540.627499]
> [ 6540.634110]        CPU0
> [ 6540.636841]        ----
> [ 6540.639574]   lock(sb_writers#9);
> [ 6540.643281]   lock(sb_writers#9);
> [ 6540.646988]
> [ 6540.646988]  *** DEADLOCK ***
> ```
>
> This patch fixes the issue by using the AIO-specific helpers
> `kiocb_start_write()` and `kiocb_end_write()`. These functions are
> designed to be used with a `kiocb` and manage write sequencing
> correctly for asynchronous I/O without introducing the problematic
> lock dependency.
>
> The `kiocb` is already part of the `loop_cmd` struct, so this change
> also simplifies the completion function `lo_rw_aio_do_completion()` by
> using the `iocb` from the `cmd` struct directly, instead of retrieving
> the loop device from the request queue.
>
> Fixes: 39d86db34e41 ("loop: add file_start_write() and file_end_write()")
> Cc: Changhui Zhong <czhong@redhat.com>
> Signed-off-by: Ming Lei <ming.lei@redhat.com>
> ---
>  drivers/block/loop.c | 5 ++---
>  1 file changed, 2 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/block/loop.c b/drivers/block/loop.c
> index 500840e4a74e..8d994cae3b83 100644
> --- a/drivers/block/loop.c
> +++ b/drivers/block/loop.c
> @@ -308,14 +308,13 @@ static void lo_complete_rq(struct request *rq)
>  static void lo_rw_aio_do_completion(struct loop_cmd *cmd)
>  {
>         struct request *rq = blk_mq_rq_from_pdu(cmd);
> -       struct loop_device *lo = rq->q->queuedata;
>
>         if (!atomic_dec_and_test(&cmd->ref))
>                 return;
>         kfree(cmd->bvec);
>         cmd->bvec = NULL;
>         if (req_op(rq) == REQ_OP_WRITE)
> -               file_end_write(lo->lo_backing_file);
> +               kiocb_end_write(&cmd->iocb);
>         if (likely(!blk_should_fake_timeout(rq->q)))
>                 blk_mq_complete_request(rq);
>  }
> @@ -391,7 +390,7 @@ static int lo_rw_aio(struct loop_device *lo, struct loop_cmd *cmd,
>         }
>
>         if (rw == ITER_SOURCE) {
> -               file_start_write(lo->lo_backing_file);
> +               kiocb_start_write(&cmd->iocb);
>                 ret = file->f_op->write_iter(&cmd->iocb, &iter);
>         } else
>                 ret = file->f_op->read_iter(&cmd->iocb, &iter);
> --
> 2.50.1
>

Thanks Ming!
Verify that after applying the patch, this warning message is no
longer triggered,


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

end of thread, other threads:[~2025-07-18  1:22 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-16 11:48 [PATCH] loop: use kiocb helpers to fix lockdep warning Ming Lei
2025-07-16 12:20 ` Christoph Hellwig
2025-07-16 12:31   ` Ming Lei
2025-07-17  4:31     ` Christoph Hellwig
2025-07-16 12:34 ` Jens Axboe
2025-07-18  1:22 ` Changhui Zhong

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox