Skip to content

Commit 976f370

Browse files
author
Ming Lei
committed
loop: move vfs_fsync() out of loop_update_dio()
JIRA: https://issues.redhat.com/browse/RHEL-112997 commit 86947bd Author: Ming Lei <ming.lei@redhat.com> Date: Tue Mar 18 15:29:55 2025 +0800 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 <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> Reviewed-by: Christoph Hellwig <hch@lst.de> Link: https://lore.kernel.org/r/20250318072955.3893805-1-ming.lei@redhat.com Signed-off-by: Jens Axboe <axboe@kernel.dk> Signed-off-by: Ming Lei <ming.lei@redhat.com>
1 parent 195dd2a commit 976f370

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
/**
@@ -578,6 +572,13 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
578572
if (get_loop_size(lo, file) != get_loop_size(lo, old_file))
579573
goto out_err;
580574

575+
/*
576+
* We might switch to direct I/O mode for the loop device, write back
577+
* all dirty data the page cache now that so that the individual I/O
578+
* operations don't have to do that.
579+
*/
580+
vfs_fsync(file, 0);
581+
581582
/* and ... switch */
582583
disk_force_media_change(lo->lo_disk);
583584
memflags = blk_mq_freeze_queue(lo->lo_queue);
@@ -1051,6 +1052,13 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode,
10511052
if (error)
10521053
goto out_unlock;
10531054

1055+
/*
1056+
* We might switch to direct I/O mode for the loop device, write back
1057+
* all dirty data the page cache now that so that the individual I/O
1058+
* operations don't have to do that.
1059+
*/
1060+
vfs_fsync(file, 0);
1061+
10541062
loop_update_dio(lo);
10551063
loop_sysfs_init(lo);
10561064

0 commit comments

Comments
 (0)