loop: don't call vfs_flush() with queue frozen
PATCH 1/1 loop: don't call vfs_flush() with queue frozen
If vfs_flush() is called with queue frozen, the queue freeze lock may be connected with FS internal lock, and potential deadlock could be triggered.
Fix it by moving vfs_flush() out of queue freezing.
--- drivers/block/loop.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-)
drivers/block/loop.c +15 -6
@@ -203,7 +203,7 @@ static bool lo_can_use_dio(struct loop_device *lo)* loop_get_status will always report the effective LO_FLAGS_DIRECT_IO flag and * not the originally passed in one. */ -static inline void loop_update_dio (struct loop_device *lo) +static inline bool loop_update_dio (struct loop_device *lo) {bool dio_in_use = lo->lo_flags &LO_FLAGS_DIRECT_IO ;
@@ -217,8 +217,7 @@ static inline void loop_update_dio(struct loop_device *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 ); +return (lo->lo_flags &LO_FLAGS_DIRECT_IO ) && !dio_in_use; }/**
@@ -589,6 +588,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,int error;bool partscan;bool is_loop; +bool flush;if (!file)return -EBADF ;
@@ -629,11 +629,14 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev, lo->old_gfp_mask =mapping_gfp_mask (file->f_mapping);mapping_set_gfp_mask (file->f_mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); -loop_update_dio (lo); + flush =loop_update_dio (lo);blk_mq_unfreeze_queue (lo->lo_queue); partscan = lo->lo_flags &LO_FLAGS_PARTSCAN ;loop_global_unlock (lo, is_loop); +if (flush) +vfs_fsync (lo->lo_backing_file,0 ); +/* * Flush loop_validate_file() before fput(), for l->lo_backing_file * might be pointing at old_file which might be the last reference.
@@ -1255,6 +1258,7 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info)int err;bool partscan = false;bool size_changed = false; +bool flush = false; err =mutex_lock_killable (&lo->lo_mutex);if (err)
@@ -1292,7 +1296,7 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info) }/* update the direct I/O flag if lo_offset changed */ -loop_update_dio (lo); + flush =loop_update_dio (lo); out_unfreeze:blk_mq_unfreeze_queue (lo->lo_queue);
@@ -1302,6 +1306,8 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info)mutex_unlock (&lo->lo_mutex);if (partscan)loop_reread_partitions (lo); +if (flush) +vfs_fsync (lo->lo_backing_file,0 );return err; }
@@ -1473,6 +1479,7 @@ static int loop_set_block_size(struct loop_device *lo, unsigned long arg) {struct queue_limits lim;int err =0 ; +bool flush;if (lo->lo_state != Lo_bound)return -ENXIO ;
@@ -1488,8 +1495,10 @@ static int loop_set_block_size(struct loop_device *lo, unsigned long arg)blk_mq_freeze_queue (lo->lo_queue); err =queue_limits_commit_update (lo->lo_queue, &lim); -loop_update_dio (lo); + flush =loop_update_dio (lo);blk_mq_unfreeze_queue (lo->lo_queue); +if (flush) +vfs_fsync (lo->lo_backing_file,0 );return err; }
Signature · 1 line
2.44.0
Quoted 2 lines · Ming Lei
If
vfs_flush()is called with queue frozen, the queue freeze lock may be connected with FS internal lock
What "FS internal lock" ?
Quoted 4 lines
, and potential deadlock could be
triggered.Fix it by moving
vfs_flush()out of queue freezing.
That doesn't work. The pagecache will be dirties by any command processed using buffered I/O, so we need to freeze first to ensure that there are no outstanding commands.
Quoted 5 lines · Christoph Hellwig
Quoted 2 lines · Ming Lei
If
vfs_flush()is called with queue frozen, the queue freeze lock may be connected with FS internal lockWhat "FS internal lock" ?
Please see the report:
https://lore.kernel.org/linux-block/[email protected]/T/#u
Quoted 9 lines
Quoted 5 lines
, and potential deadlock could be
triggered.Fix it by moving
vfs_flush()out of queue freezing.That doesn't work. The pagecache will be dirties by any command processed using buffered I/O, so we need to freeze first to ensure that there are no outstanding commands.
vfs_flush() is called in case of previous buffered IO for flushing dirty pages.
The call from loop_change_fd() has been broken, because ->lo_backing_file is updated to new one when calling loop_update_dio(), and I will cover this case in V2.
For others in which backing file isn't changed, the patch is just fine in case of new buffered IO mode, isn't it?
Thanks,
Ming
Quoted 10 lines · Ming Lei
Quoted 5 lines · Christoph Hellwig
Quoted 2 lines · Ming Lei
If
vfs_flush()is called with queue frozen, the queue freeze lock may be connected with FS internal lockWhat "FS internal lock" ?
Please see the report:
Please state the locks. Nothing fs internal here, that report is about i_rwsem. And a false positive because it is about ordering of i_rwsem on the upper file system sitting on the loop device vs the one on the lower file systems sitting below the block device. These obviously can't deadlock, we just need to tell lockdep about that fact.
Quoted 10 lines · Christoph Hellwig
Quoted 9 lines · Ming Lei
Quoted 5 lines · Christoph Hellwig
Quoted 2 lines · Ming Lei
If
vfs_flush()is called with queue frozen, the queue freeze lock may be connected with FS internal lockWhat "FS internal lock" ?
Please see the report:
Quoted 7 lines
Please state the locks. Nothing fs internal here, that report is about
i_rwsem. And a false positive because it is about ordering ofi_rwsemon the upper file system sitting on the loop device vs the one on the lower file systems sitting below the block device. These obviously can't deadlock, we just need to tell lockdep about that fact.
How can you guarantee that some code won't submit IO by grabbing the i_rwsem?
As I explained, it is fine to move out vfs_fsync() out of freeze queue.
Actually any lock which depends on freeze queue needs to take a careful look, because freeze queue connects too many global/sub-system locks.
Thanks,
Ming
Quoted 8 lines · Ming Lei
Quoted 5 lines
Please state the locks. Nothing fs internal here, that report is about
i_rwsem. And a false positive because it is about ordering ofi_rwsemon the upper file system sitting on the loop device vs the one on the lower file systems sitting below the block device. These obviously can't deadlock, we just need to tell lockdep about that fact.How can you guarantee that some code won't submit IO by grabbing the
i_rwsem?
? A lot of the I/O will grab i_rwsem on the underlying device. Basically all writes, and for many file systems also on reads. But that is an entirely different i_rwsem as the one held the bio submitter as that is in different file system. There is no way the top file system can lock i_rwsem on the lower file system except through the loop driver, and that always sits below the freeze protection.
Quoted 4 lines
As I explained, it is fine to move out
vfs_fsync()out of freeze queue.Actually any lock which depends on freeze queue needs to take a careful look, because freeze queue connects too many global/sub-system locks.
For block layer locks: absolutely. For file systems lock: not at all, because we're talking about different file systems instances. The only exception would be file systems taking global locks in the I/O path, but I sincerely hope no one does that.
Quoted 19 lines · Christoph Hellwig
Quoted 8 lines · Ming Lei
Quoted 5 lines
Please state the locks. Nothing fs internal here, that report is about
i_rwsem. And a false positive because it is about ordering ofi_rwsemon the upper file system sitting on the loop device vs the one on the lower file systems sitting below the block device. These obviously can't deadlock, we just need to tell lockdep about that fact.How can you guarantee that some code won't submit IO by grabbing the
i_rwsem?? A lot of the I/O will grab
i_rwsemon the underlying device. Basically all writes, and for many file systems also on reads. But that is an entirely differenti_rwsemas the one held the bio submitter as that is in different file system. There is no way the top file system can locki_rwsemon the lower file system except through the loop driver, and that always sits below the freeze protection.Quoted 2 lines
As I explained, it is fine to move out
vfs_fsync()out of freeze queue.
Quoted 7 lines
Quoted 2 lines
Actually any lock which depends on freeze queue needs to take a careful look, because freeze queue connects too many global/sub-system locks.
For block layer locks: absolutely. For file systems lock: not at all, because we're talking about different file systems instances. The only exception would be file systems taking global locks in the I/O path, but I sincerely hope no one does that.
Didn't you see the report on fs_reclaim and sysfs root lock?
https://lore.kernel.org/linux-block/[email protected]/
Thanks,
Ming
Quoted 11 lines · Ming Lei
Quoted 10 lines · Christoph Hellwig
Quoted 8 lines · Ming Lei
Quoted 5 lines
Please state the locks. Nothing fs internal here, that report is about
i_rwsem. And a false positive because it is about ordering ofi_rwsemon the upper file system sitting on the loop device vs the one on the lower file systems sitting below the block device. These obviously can't deadlock, we just need to tell lockdep about that fact.How can you guarantee that some code won't submit IO by grabbing the
i_rwsem?
Quoted 6 lines
Quoted 6 lines
? A lot of the I/O will grab
i_rwsemon the underlying device. Basically all writes, and for many file systems also on reads. But that is an entirely differenti_rwsemas the one held the bio submitter as that is in different file system. There is no way the top file system can locki_rwsemon the lower file system except through the loop driver, and that always sits below the freeze protection.
Actually some FSs may call kmalloc(GFP_KERNEL) with i_rwsem grabbed, which could call into real deadlock if IO on the loop disk is caused by the kmalloc(GFP_KERNEL).
So it is not one false positive.
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
As I explained, it is fine to move out
vfs_fsync()out of freeze queue.Actually any lock which depends on freeze queue needs to take a careful look, because freeze queue connects too many global/sub-system locks.
Quoted 8 lines
Quoted 4 lines
For block layer locks: absolutely. For file systems lock: not at all, because we're talking about different file systems instances. The only exception would be file systems taking global locks in the I/O path, but I sincerely hope no one does that.
Didn't you see the report on
fs_reclaimand sysfs root lock?
There are more, such as mm->mmap_lock[1], hfs SB 'cat_tree' lock[2]...
[1] https://lore.kernel.org/linux-block/[email protected]/
[2] https://lore.kernel.org/linux-block/[email protected]/
Thanks,
Ming
Quoted 3 lines · Ming Lei
Actually some FSs may call
kmalloc(GFP_KERNEL)withi_rwsemgrabbed, which could call into real deadlock if IO on the loop disk is caused by thekmalloc(GFP_KERNEL).
Well, loop can always deadlock when the lower fs is doing allocations, even without the freeze. I'm actually kinda surprised loop doesn't force a context noio as we'd really need that.
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
because we're talking about different file systems instances. The only exception would be file systems taking global locks in the I/O path, but I sincerely hope no one does that.
Didn't you see the report on
fs_reclaimand sysfs root lock?
Quoted 7 lines
There are more, such as mm->mmap_lock[1], hfs SB '
cat_tree' lock[2]...[1] https://lore.kernel.org/linux-block/[email protected]/
[2] https://lore.kernel.org/linux-block/[email protected]/
And all of these are caused by not breaking the dependency loop..
Quoted 8 lines · Christoph Hellwig
Quoted 3 lines · Ming Lei
Actually some FSs may call
kmalloc(GFP_KERNEL)withi_rwsemgrabbed, which could call into real deadlock if IO on the loop disk is caused by thekmalloc(GFP_KERNEL).Well, loop can always deadlock when the lower fs is doing allocations, even without the freeze. I'm actually kinda surprised loop doesn't force a context noio as we'd really need that.
Loop does call mapping_set_gfp_mask(~(__GFP_IO|__GFP_FS)).
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 4 lines
because we're talking about different file systems instances. The only exception would be file systems taking global locks in the I/O path, but I sincerely hope no one does that.
Didn't you see the report on
fs_reclaimand sysfs root lock?
Quoted 7 lines
Quoted 5 lines
There are more, such as mm->mmap_lock[1], hfs SB '
cat_tree' lock[2]...[1] https://lore.kernel.org/linux-block/[email protected]/
[2] https://lore.kernel.org/linux-block/[email protected]/And all of these are caused by not breaking the dependency loop..
Can you share how to break the dependency? Such as fs_reclaim & mm->mmap_lock.
Thanks,
Ming
Quoted 11 lines · Ming Lei
Quoted 8 lines · Christoph Hellwig
Quoted 3 lines · Ming Lei
Actually some FSs may call
kmalloc(GFP_KERNEL)withi_rwsemgrabbed, which could call into real deadlock if IO on the loop disk is caused by thekmalloc(GFP_KERNEL).Well, loop can always deadlock when the lower fs is doing allocations, even without the freeze. I'm actually kinda surprised loop doesn't force a context noio as we'd really need that.
Loop does call mapping_set_gfp_mask(~(__GFP_IO|__GFP_FS)).
But that only helps with page cache allocations, not other allocations don't in the I/O path, of which there are plenty in modern file systems.
Quoted 4 lines
Quoted 1 line
And all of these are caused by not breaking the dependency loop..
Can you share how to break the dependency? Such as
fs_reclaim&mm->mmap_lock.
fs_reclaim should not be broken. It's a fake lockdep key annotation the reclaim path. Everything under it needs to be NOFS, or in case of loop NOIO (see above). I think mmap_lock is just an indirect chain here, but I need to take a closer look. A little busty this week and only sporadically online, sorry.
Quoted 112 lines
If
vfs_flush()is called with queue frozen, the queue freeze lock may be connected with FS internal lock, and potential deadlock could be triggered.Fix it by moving
vfs_flush()out of queue freezing.Reported-by Kun Hu <[email protected]>Reported-by Jiaji Qin <[email protected]>Signed-off-by Ming Lei <[email protected]>---
drivers/block/loop.c| 21 +++++++++++++++------1 file changed, 15 insertions(+), 6 deletions(-)drivers/block/loop.c +9
-5@@ -203,7 +203,7 @@ static bool lo_can_use_dio(struct loop_device *lo)* loop_get_status will always report the effective LO_FLAGS_DIRECT_IO flag and * not the originally passed in one. */ -static inline void loop_update_dio (struct loop_device *lo) +static inline bool loop_update_dio (struct loop_device *lo) {bool dio_in_use = lo->lo_flags &LO_FLAGS_DIRECT_IO ;@@ -217,8 +217,7 @@ static inline void loop_update_dio(struct loop_device *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 ); +return (lo->lo_flags &LO_FLAGS_DIRECT_IO ) && !dio_in_use; }/** @@ -589,6 +588,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,int error;bool partscan;bool is_loop; +bool flush;if (!file)return -EBADF ;@@ -629,11 +629,14 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev, lo->old_gfp_mask =mapping_gfp_mask (file->f_mapping);mapping_set_gfp_mask (file->f_mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); -loop_update_dio (lo); + flush =loop_update_dio (lo);blk_mq_unfreeze_queue (lo->lo_queue); partscan = lo->lo_flags &LO_FLAGS_PARTSCAN ;loop_global_unlock (lo, is_loop); +if (flush) +vfs_fsync (lo->lo_backing_file,0 ); +/* * Flush loop_validate_file() before fput(), for l->lo_backing_file * might be pointing at old_file which might be the last reference. @@ -1255,6 +1258,7 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info)int err;bool partscan = false;bool size_changed = false; +bool flush = false; err =mutex_lock_killable (&lo->lo_mutex);if (err)@@ -1292,7 +1296,7 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info) }/* update the direct I/O flag if lo_offset changed */ -loop_update_dio (lo); + flush =loop_update_dio (lo);
out_unfreeze:blk_mq_unfreeze_queue(lo->lo_queue);drivers/block/loop.c +6
-1@@ -1302,6 +1306,8 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info)mutex_unlock (&lo->lo_mutex);if (partscan)loop_reread_partitions (lo); +if (flush) +vfs_fsync (lo->lo_backing_file,0 );return err; }@@ -1473,6 +1479,7 @@ static int loop_set_block_size(struct loop_device *lo, unsigned long arg) {struct queue_limits lim;int err =0 ; +bool flush;if (lo->lo_state != Lo_bound)return -ENXIO ;@@ -1488,8 +1495,10 @@ static int loop_set_block_size(struct loop_device *lo, unsigned long arg)blk_mq_freeze_queue (lo->lo_queue); err =queue_limits_commit_update (lo->lo_queue, &lim); -loop_update_dio (lo); + flush =loop_update_dio (lo);blk_mq_unfreeze_queue (lo->lo_queue); +if (flush) +vfs_fsync (lo->lo_backing_file,0 );return err; }Signature · 1 line
2.44.0
Hello Ming,
Should we test the fixed code through multiple rounds? Is there a conclusion to the ongoing discussion about the patch?
——
Thanks,
Kun Hu
Quoted 108 lines
Quoted 104 lines · 2025年1月14日 11:21,Kun Hu <[email protected]> 写道:
If
vfs_flush()is called with queue frozen, the queue freeze lock may be connected with FS internal lock, and potential deadlock could be triggered.Fix it by moving
vfs_flush()out of queue freezing.Reported-by Kun Hu <[email protected]>Reported-by Jiaji Qin <[email protected]>Signed-off-by Ming Lei <[email protected]>---
drivers/block/loop.c| 21 +++++++++++++++------1 file changed, 15 insertions(+), 6 deletions(-)drivers/block/loop.c +9
-5@@ -203,7 +203,7 @@ static bool lo_can_use_dio(struct loop_device *lo)* loop_get_status will always report the effective LO_FLAGS_DIRECT_IO flag and * not the originally passed in one. */ -static inline void loop_update_dio (struct loop_device *lo) +static inline bool loop_update_dio (struct loop_device *lo) {bool dio_in_use = lo->lo_flags &LO_FLAGS_DIRECT_IO ;@@ -217,8 +217,7 @@ static inline void loop_update_dio(struct loop_device *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 ); +return (lo->lo_flags &LO_FLAGS_DIRECT_IO ) && !dio_in_use; }/** @@ -589,6 +588,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,int error;bool partscan;bool is_loop; +bool flush;if (!file)return -EBADF ;@@ -629,11 +629,14 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev, lo->old_gfp_mask =mapping_gfp_mask (file->f_mapping);mapping_set_gfp_mask (file->f_mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); -loop_update_dio (lo); + flush =loop_update_dio (lo);blk_mq_unfreeze_queue (lo->lo_queue); partscan = lo->lo_flags &LO_FLAGS_PARTSCAN ;loop_global_unlock (lo, is_loop); +if (flush) +vfs_fsync (lo->lo_backing_file,0 ); +/* * Flush loop_validate_file() before fput(), for l->lo_backing_file * might be pointing at old_file which might be the last reference. @@ -1255,6 +1258,7 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info)int err;bool partscan = false;bool size_changed = false; +bool flush = false; err =mutex_lock_killable (&lo->lo_mutex);if (err)@@ -1292,7 +1296,7 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info) }/* update the direct I/O flag if lo_offset changed */ -loop_update_dio (lo); + flush =loop_update_dio (lo);
out_unfreeze:blk_mq_unfreeze_queue(lo->lo_queue);drivers/block/loop.c +6
-1@@ -1302,6 +1306,8 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info)mutex_unlock (&lo->lo_mutex);if (partscan)loop_reread_partitions (lo); +if (flush) +vfs_fsync (lo->lo_backing_file,0 );return err; }@@ -1473,6 +1479,7 @@ static int loop_set_block_size(struct loop_device *lo, unsigned long arg) {struct queue_limits lim;int err =0 ; +bool flush;if (lo->lo_state != Lo_bound)return -ENXIO ;@@ -1488,8 +1495,10 @@ static int loop_set_block_size(struct loop_device *lo, unsigned long arg)blk_mq_freeze_queue (lo->lo_queue); err =queue_limits_commit_update (lo->lo_queue, &lim); -loop_update_dio (lo); + flush =loop_update_dio (lo);blk_mq_unfreeze_queue (lo->lo_queue); +if (flush) +vfs_fsync (lo->lo_backing_file,0 );
Quoted 5 lines
Quoted 5 lines
drivers/block/loop.c +0
-0return err; }Signature · 1 line
2.44.0
Hi Ming,
I wanted to follow up as I haven’t yet seen the fix you provided, titled “[PATCH] loop: don't call vfs_flush() with queue frozen” and “[PATCH] loop: move vfs_fsync() out of loop_update_dio()" in the kernel tree. Could you kindly confirm if this resolves the issue we’ve been discussing? Additionally, I would greatly appreciate it if you could share any updates regarding the resolution of this matter.