Skip to content

Commit 86947bd

Browse files
Ming Leiaxboe
authored andcommitted
loop: move vfs_fsync() out of loop_update_dio()
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 <[email protected]> Reported-by: Kun Hu <[email protected]> Reported-by: Jiaji Qin <[email protected]> Closes: https://lore.kernel.org/linux-block/[email protected]/T/#u Signed-off-by: Ming Lei <[email protected]> Reviewed-by: Christoph Hellwig <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent ffa1e7a commit 86947bd

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

drivers/block/loop.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,18 +189,12 @@ static bool lo_can_use_dio(struct loop_device *lo)
189189
*/
190190
static inline void loop_update_dio(struct loop_device *lo)
191191
{
192-
bool dio_in_use = lo->lo_flags & LO_FLAGS_DIRECT_IO;
193-
194192
lockdep_assert_held(&lo->lo_mutex);
195193
WARN_ON_ONCE(lo->lo_state == Lo_bound &&
196194
lo->lo_queue->mq_freeze_depth == 0);
197195

198196
if ((lo->lo_flags & LO_FLAGS_DIRECT_IO) && !lo_can_use_dio(lo))
199197
lo->lo_flags &= ~LO_FLAGS_DIRECT_IO;
200-
201-
/* flush dirty pages before starting to issue direct I/O */
202-
if ((lo->lo_flags & LO_FLAGS_DIRECT_IO) && !dio_in_use)
203-
vfs_fsync(lo->lo_backing_file, 0);
204198
}
205199

206200
/**
@@ -637,6 +631,13 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
637631
if (get_loop_size(lo, file) != get_loop_size(lo, old_file))
638632
goto out_err;
639633

634+
/*
635+
* We might switch to direct I/O mode for the loop device, write back
636+
* all dirty data the page cache now that so that the individual I/O
637+
* operations don't have to do that.
638+
*/
639+
vfs_fsync(file, 0);
640+
640641
/* and ... switch */
641642
disk_force_media_change(lo->lo_disk);
642643
memflags = blk_mq_freeze_queue(lo->lo_queue);
@@ -1105,6 +1106,13 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode,
11051106
if (error)
11061107
goto out_unlock;
11071108

1109+
/*
1110+
* We might switch to direct I/O mode for the loop device, write back
1111+
* all dirty data the page cache now that so that the individual I/O
1112+
* operations don't have to do that.
1113+
*/
1114+
vfs_fsync(file, 0);
1115+
11081116
loop_update_dio(lo);
11091117
loop_sysfs_init(lo);
11101118

0 commit comments

Comments
 (0)