* [PATCH] loop: move vfs_fsync() out of loop_update_dio()
@ 2025-01-13 12:06 Ming Lei
2025-01-13 13:50 ` Christoph Hellwig
2025-03-17 8:52 ` 胡焜
0 siblings, 2 replies; 7+ messages in thread
From: Ming Lei @ 2025-01-13 12:06 UTC (permalink / raw)
To: Jens Axboe, linux-block; +Cc: Ming Lei, Christoph Hellwig, Kun Hu, Jiaji Qin
If vfs_flush() is called with queue frozen, the queue freeze lock may be
connected with FS internal lock, and lockdep warning can be triggered
because the queue freeze lock is connected with too many global or
sub-system locks.
Fix the warning by moving vfs_fsync() out of loop_update_dio():
- vfs_fsync() is only needed when switching to dio
- only loop_change_fd() and loop_configure() may switch from buffered
IO to direct IO, so call vfs_fsync() directly here. This way is safe
because either loop is in unbound, or new file isn't attached
- for the other two cases of set_status and set_block_size, direct IO
can only become off, so no need to call vfs_fsync()
Cc: Christoph Hellwig <hch@infradead.org>
Reported-by: Kun Hu <huk23@m.fudan.edu.cn>
Reported-by: Jiaji Qin <jjtan24@m.fudan.edu.cn>
Closes: https://lore.kernel.org/linux-block/359BC288-B0B1-4815-9F01-3A349B12E816@m.fudan.edu.cn/T/#u
Signed-off-by: Ming Lei <ming.lei@redhat.com>
---
drivers/block/loop.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/drivers/block/loop.c b/drivers/block/loop.c
index 1ec7417c7f00..be7e20064427 100644
--- a/drivers/block/loop.c
+++ b/drivers/block/loop.c
@@ -205,8 +205,6 @@ static bool lo_can_use_dio(struct loop_device *lo)
*/
static inline void loop_update_dio(struct loop_device *lo)
{
- bool dio_in_use = lo->lo_flags & LO_FLAGS_DIRECT_IO;
-
lockdep_assert_held(&lo->lo_mutex);
WARN_ON_ONCE(lo->lo_state == Lo_bound &&
lo->lo_queue->mq_freeze_depth == 0);
@@ -215,10 +213,6 @@ static inline void loop_update_dio(struct loop_device *lo)
lo->lo_flags |= LO_FLAGS_DIRECT_IO;
if ((lo->lo_flags & LO_FLAGS_DIRECT_IO) && !lo_can_use_dio(lo))
lo->lo_flags &= ~LO_FLAGS_DIRECT_IO;
-
- /* flush dirty pages before starting to issue direct I/O */
- if ((lo->lo_flags & LO_FLAGS_DIRECT_IO) && !dio_in_use)
- vfs_fsync(lo->lo_backing_file, 0);
}
/**
@@ -621,6 +615,9 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
if (get_loop_size(lo, file) != get_loop_size(lo, old_file))
goto out_err;
+ /* may work in dio, so flush page cache for avoiding race */
+ vfs_fsync(file, 0);
+
/* and ... switch */
disk_force_media_change(lo->lo_disk);
blk_mq_freeze_queue(lo->lo_queue);
@@ -1098,6 +1095,9 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode,
if (error)
goto out_unlock;
+ /* may work in dio, so flush page cache for avoiding race */
+ vfs_fsync(file, 0);
+
loop_update_dio(lo);
loop_sysfs_init(lo);
--
2.44.0
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH] loop: move vfs_fsync() out of loop_update_dio()
2025-01-13 12:06 [PATCH] loop: move vfs_fsync() out of loop_update_dio() Ming Lei
@ 2025-01-13 13:50 ` Christoph Hellwig
2025-01-13 15:04 ` Ming Lei
2025-03-17 8:52 ` 胡焜
1 sibling, 1 reply; 7+ messages in thread
From: Christoph Hellwig @ 2025-01-13 13:50 UTC (permalink / raw)
To: Ming Lei; +Cc: Jens Axboe, linux-block, Christoph Hellwig, Kun Hu, Jiaji Qin
NAK for the same reason as v1. It's not needed because there is no
deadlock, just a false positive due to missing annotations of lock
instances, and we need to fsync with a frozen queue to ensure there
is no outstanding I/O.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] loop: move vfs_fsync() out of loop_update_dio()
2025-01-13 13:50 ` Christoph Hellwig
@ 2025-01-13 15:04 ` Ming Lei
2025-01-15 6:59 ` Christoph Hellwig
0 siblings, 1 reply; 7+ messages in thread
From: Ming Lei @ 2025-01-13 15:04 UTC (permalink / raw)
To: Christoph Hellwig; +Cc: Jens Axboe, linux-block, Kun Hu, Jiaji Qin
On Mon, Jan 13, 2025 at 05:50:34AM -0800, Christoph Hellwig wrote:
> NAK for the same reason as v1. It's not needed because there is no
> deadlock, just a false positive due to missing annotations of lock
Yes, but annotation should be the last straw.
> instances, and we need to fsync with a frozen queue to ensure there
> is no outstanding I/O.
loop_configure() is on unbound loop, so there isn't outstanding I/O.
loop_change_fd() is switching to this new file, so no outstanding I/O
on this new file before unfreeze.
The other two can only switch to buffered IO, which needn't the fsync.
So can you point out anything is wrong?
And this way is sort of simplification.
Thanks,
Ming
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] loop: move vfs_fsync() out of loop_update_dio()
2025-01-13 15:04 ` Ming Lei
@ 2025-01-15 6:59 ` Christoph Hellwig
0 siblings, 0 replies; 7+ messages in thread
From: Christoph Hellwig @ 2025-01-15 6:59 UTC (permalink / raw)
To: Ming Lei; +Cc: Christoph Hellwig, Jens Axboe, linux-block, Kun Hu, Jiaji Qin
On Mon, Jan 13, 2025 at 11:04:51PM +0800, Ming Lei wrote:
> > instances, and we need to fsync with a frozen queue to ensure there
> > is no outstanding I/O.
>
> loop_configure() is on unbound loop, so there isn't outstanding I/O.
Yeah.
> loop_change_fd() is switching to this new file, so no outstanding I/O
> on this new file before unfreeze.
True.
> The other two can only switch to buffered IO, which needn't the fsync.
>
> So can you point out anything is wrong?
>
> And this way is sort of simplification.
Yeah, I think this is fine in this version. But please update the
comment away from the avoiding races to spell out that this is
writing out the page cache before starting to use direct I/O.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re:[PATCH] loop: move vfs_fsync() out of loop_update_dio()
2025-01-13 12:06 [PATCH] loop: move vfs_fsync() out of loop_update_dio() Ming Lei
2025-01-13 13:50 ` Christoph Hellwig
@ 2025-03-17 8:52 ` 胡焜
2025-03-18 1:07 ` [PATCH] " Ming Lei
1 sibling, 1 reply; 7+ messages in thread
From: 胡焜 @ 2025-03-17 8:52 UTC (permalink / raw)
To: Ming Lei, linux-block
Cc: Ming Lei, Christoph Hellwig, 覃佳基
> drivers/block/loop.c | 12 ++++++------
> 1 file changed, 6 insertions(+), 6 deletions(-)
> diff --git a/drivers/block/loop.c b/drivers/block/loop.c
> index 1ec7417c7f00..be7e20064427 100644
> --- a/drivers/block/loop.c
> +++ b/drivers/block/loop.c
> @@ -205,8 +205,6 @@ static bool lo_can_use_dio(struct loop_device *lo)
> */
> static inline void loop_update_dio(struct loop_device *lo)
> {
> - bool dio_in_use = lo->lo_flags & LO_FLAGS_DIRECT_IO;
> -
> lockdep_assert_held(&lo->lo_mutex);
> WARN_ON_ONCE(lo->lo_state == Lo_bound &&
> lo->lo_queue->mq_freeze_depth == 0);
> @@ -215,10 +213,6 @@ static inline void loop_update_dio(struct loop_device *lo)
> lo->lo_flags |= LO_FLAGS_DIRECT_IO;
> if ((lo->lo_flags & LO_FLAGS_DIRECT_IO) && !lo_can_use_dio(lo))
> lo->lo_flags &= ~LO_FLAGS_DIRECT_IO;
> -
> - /* flush dirty pages before starting to issue direct I/O */
> - if ((lo->lo_flags & LO_FLAGS_DIRECT_IO) && !dio_in_use)
> - vfs_fsync(lo->lo_backing_file, 0);
> }
>
> /**
> @@ -621,6 +615,9 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
> if (get_loop_size(lo, file) != get_loop_size(lo, old_file))
> goto out_err;
> + /* may work in dio, so flush page cache for avoiding race */
> + vfs_fsync(file, 0);
> +
> /* and ... switch */
> disk_force_media_change(lo->lo_disk);
> blk_mq_freeze_queue(lo->lo_queue);
> @@ -1098,6 +1095,9 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode,
> if (error)
> goto out_unlock;
> + /* may work in dio, so flush page cache for avoiding race */
> + vfs_fsync(file, 0);
> +
> loop_update_dio(lo);
> loop_sysfs_init(lo);
>
> --
> 2.44.0
Hello Ming,
I would like to double check that this fix doesn't seem to have been merged into the main thread, will this version still be merged into mainline kernel tree?
Thanks,
Kun
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] loop: move vfs_fsync() out of loop_update_dio()
2025-03-17 8:52 ` 胡焜
@ 2025-03-18 1:07 ` Ming Lei
2025-04-09 5:49 ` 胡焜
0 siblings, 1 reply; 7+ messages in thread
From: Ming Lei @ 2025-03-18 1:07 UTC (permalink / raw)
To: 胡焜; +Cc: linux-block, Christoph Hellwig, 覃佳基
On Mon, Mar 17, 2025 at 04:52:16PM +0800, 胡焜 wrote:
> > drivers/block/loop.c | 12 ++++++------
> > 1 file changed, 6 insertions(+), 6 deletions(-)
>
> > diff --git a/drivers/block/loop.c b/drivers/block/loop.c
> > index 1ec7417c7f00..be7e20064427 100644
> > --- a/drivers/block/loop.c
> > +++ b/drivers/block/loop.c
> > @@ -205,8 +205,6 @@ static bool lo_can_use_dio(struct loop_device *lo)
> > */
> > static inline void loop_update_dio(struct loop_device *lo)
> > {
> > - bool dio_in_use = lo->lo_flags & LO_FLAGS_DIRECT_IO;
> > -
> > lockdep_assert_held(&lo->lo_mutex);
> > WARN_ON_ONCE(lo->lo_state == Lo_bound &&
> > lo->lo_queue->mq_freeze_depth == 0);
> > @@ -215,10 +213,6 @@ static inline void loop_update_dio(struct loop_device *lo)
> > lo->lo_flags |= LO_FLAGS_DIRECT_IO;
> > if ((lo->lo_flags & LO_FLAGS_DIRECT_IO) && !lo_can_use_dio(lo))
> > lo->lo_flags &= ~LO_FLAGS_DIRECT_IO;
> > -
> > - /* flush dirty pages before starting to issue direct I/O */
> > - if ((lo->lo_flags & LO_FLAGS_DIRECT_IO) && !dio_in_use)
> > - vfs_fsync(lo->lo_backing_file, 0);
> > }
> >
> > /**
> > @@ -621,6 +615,9 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
> > if (get_loop_size(lo, file) != get_loop_size(lo, old_file))
> > goto out_err;
>
> > + /* may work in dio, so flush page cache for avoiding race */
> > + vfs_fsync(file, 0);
> > +
> > /* and ... switch */
> > disk_force_media_change(lo->lo_disk);
> > blk_mq_freeze_queue(lo->lo_queue);
> > @@ -1098,6 +1095,9 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode,
> > if (error)
> > goto out_unlock;
>
> > + /* may work in dio, so flush page cache for avoiding race */
> > + vfs_fsync(file, 0);
> > +
> > loop_update_dio(lo);
> > loop_sysfs_init(lo);
> >
> > --
> > 2.44.0
>
>
> Hello Ming,
>
> I would like to double check that this fix doesn't seem to have been merged into the main thread, will this version still be merged into mainline kernel tree?
The V2 has been sent out after updating comment, please verify if it fixes your issue:
https://lore.kernel.org/linux-block/20250318010318.3861682-1-ming.lei@redhat.com/
If yes, feel free to provide one tested-by for moving on.
Thanks,
Ming
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] loop: move vfs_fsync() out of loop_update_dio()
2025-03-18 1:07 ` [PATCH] " Ming Lei
@ 2025-04-09 5:49 ` 胡焜
0 siblings, 0 replies; 7+ messages in thread
From: 胡焜 @ 2025-04-09 5:49 UTC (permalink / raw)
To: Ming Lei; +Cc: linux-block, Christoph Hellwig, jjtan24@m.fudan.edu.cn
> 2025年3月18日 09:07,Ming Lei <ming.lei@redhat.com> 写道:
>
> On Mon, Mar 17, 2025 at 04:52:16PM +0800, 胡焜 wrote:
>>> drivers/block/loop.c | 12 ++++++------
>>> 1 file changed, 6 insertions(+), 6 deletions(-)
>>
>>> diff --git a/drivers/block/loop.c b/drivers/block/loop.c
>>> index 1ec7417c7f00..be7e20064427 100644
>>> --- a/drivers/block/loop.c
>>> +++ b/drivers/block/loop.c
>>> @@ -205,8 +205,6 @@ static bool lo_can_use_dio(struct loop_device *lo)
>>> */
>>> static inline void loop_update_dio(struct loop_device *lo)
>>> {
>>> - bool dio_in_use = lo->lo_flags & LO_FLAGS_DIRECT_IO;
>>> -
>>> lockdep_assert_held(&lo->lo_mutex);
>>> WARN_ON_ONCE(lo->lo_state == Lo_bound &&
>>> lo->lo_queue->mq_freeze_depth == 0);
>>> @@ -215,10 +213,6 @@ static inline void loop_update_dio(struct loop_device *lo)
>>> lo->lo_flags |= LO_FLAGS_DIRECT_IO;
>>> if ((lo->lo_flags & LO_FLAGS_DIRECT_IO) && !lo_can_use_dio(lo))
>>> lo->lo_flags &= ~LO_FLAGS_DIRECT_IO;
>>> -
>>> - /* flush dirty pages before starting to issue direct I/O */
>>> - if ((lo->lo_flags & LO_FLAGS_DIRECT_IO) && !dio_in_use)
>>> - vfs_fsync(lo->lo_backing_file, 0);
>>> }
>>>
>>> /**
>>> @@ -621,6 +615,9 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
>>> if (get_loop_size(lo, file) != get_loop_size(lo, old_file))
>>> goto out_err;
>>
>>> + /* may work in dio, so flush page cache for avoiding race */
>>> + vfs_fsync(file, 0);
>>> +
>>> /* and ... switch */
>>> disk_force_media_change(lo->lo_disk);
>>> blk_mq_freeze_queue(lo->lo_queue);
>>> @@ -1098,6 +1095,9 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode,
>>> if (error)
>>> goto out_unlock;
>>
>>> + /* may work in dio, so flush page cache for avoiding race */
>>> + vfs_fsync(file, 0);
>>> +
>>> loop_update_dio(lo);
>>> loop_sysfs_init(lo);
>>>
>>> --
>>> 2.44.0
>>
>>
>> Hello Ming,
>>
>> I would like to double check that this fix doesn't seem to have been merged into the main thread, will this version still be merged into mainline kernel tree?
>
> The V2 has been sent out after updating comment, please verify if it fixes your issue:
>
> https://lore.kernel.org/linux-block/20250318010318.3861682-1-ming.lei@redhat.com/
>
> If yes, feel free to provide one tested-by for moving on.
>
>
> Thanks,
> Ming
Hi Ming,
I have tested the V2 patch you sent, and it has successfully fixed the issue. Thank you for the update!
Tested-by: Kun Hu <huk23@m.fudan.edu.cn>, Jiaji Qin <jjtan24@m.fudan.edu.cn>.
—————
Thanks,
Kun
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2025-04-09 5:49 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-01-13 12:06 [PATCH] loop: move vfs_fsync() out of loop_update_dio() Ming Lei
2025-01-13 13:50 ` Christoph Hellwig
2025-01-13 15:04 ` Ming Lei
2025-01-15 6:59 ` Christoph Hellwig
2025-03-17 8:52 ` 胡焜
2025-03-18 1:07 ` [PATCH] " Ming Lei
2025-04-09 5:49 ` 胡焜
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).