Skip to content

Commit 5f0addf

Browse files
naotakdave
authored andcommitted
btrfs: zoned: use dedicated lock for data relocation
Currently, we use btrfs_inode_{lock,unlock}() to grant an exclusive writeback of the relocation data inode in btrfs_zoned_data_reloc_{lock,unlock}(). However, that can cause a deadlock in the following path. Thread A takes btrfs_inode_lock() and waits for metadata reservation by e.g, waiting for writeback: prealloc_file_extent_cluster() - btrfs_inode_lock(&inode->vfs_inode, 0); - btrfs_prealloc_file_range() ... - btrfs_replace_file_extents() - btrfs_start_transaction ... - btrfs_reserve_metadata_bytes() Thread B (e.g, doing a writeback work) needs to wait for the inode lock to continue writeback process: do_writepages - btrfs_writepages - extent_writpages - btrfs_zoned_data_reloc_lock(BTRFS_I(inode)); - btrfs_inode_lock() The deadlock is caused by relying on the vfs_inode's lock. By using it, we introduced unnecessary exclusion of writeback and btrfs_prealloc_file_range(). Also, the lock at this point is useless as we don't have any dirty pages in the inode yet. Introduce fs_info->zoned_data_reloc_io_lock and use it for the exclusive writeback. Fixes: 35156d8 ("btrfs: zoned: only allow one process to add pages to a relocation inode") CC: stable@vger.kernel.org # 5.16.x: 869f4cd: btrfs: zoned: encapsulate inode locking for zoned relocation CC: stable@vger.kernel.org # 5.16.x CC: stable@vger.kernel.org # 5.17 Cc: Johannes Thumshirn <johannes.thumshirn@wdc.com> Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com> Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent a692e13 commit 5f0addf

3 files changed

Lines changed: 4 additions & 2 deletions

File tree

fs/btrfs/ctree.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,7 @@ struct btrfs_fs_info {
10601060
*/
10611061
spinlock_t relocation_bg_lock;
10621062
u64 data_reloc_bg;
1063+
struct mutex zoned_data_reloc_io_lock;
10631064

10641065
u64 nr_global_roots;
10651066

fs/btrfs/disk-io.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3156,6 +3156,7 @@ void btrfs_init_fs_info(struct btrfs_fs_info *fs_info)
31563156
mutex_init(&fs_info->reloc_mutex);
31573157
mutex_init(&fs_info->delalloc_root_mutex);
31583158
mutex_init(&fs_info->zoned_meta_io_lock);
3159+
mutex_init(&fs_info->zoned_data_reloc_io_lock);
31593160
seqlock_init(&fs_info->profiles_lock);
31603161

31613162
INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);

fs/btrfs/zoned.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,15 +359,15 @@ static inline void btrfs_zoned_data_reloc_lock(struct btrfs_inode *inode)
359359
struct btrfs_root *root = inode->root;
360360

361361
if (btrfs_is_data_reloc_root(root) && btrfs_is_zoned(root->fs_info))
362-
btrfs_inode_lock(&inode->vfs_inode, 0);
362+
mutex_lock(&root->fs_info->zoned_data_reloc_io_lock);
363363
}
364364

365365
static inline void btrfs_zoned_data_reloc_unlock(struct btrfs_inode *inode)
366366
{
367367
struct btrfs_root *root = inode->root;
368368

369369
if (btrfs_is_data_reloc_root(root) && btrfs_is_zoned(root->fs_info))
370-
btrfs_inode_unlock(&inode->vfs_inode, 0);
370+
mutex_unlock(&root->fs_info->zoned_data_reloc_io_lock);
371371
}
372372

373373
#endif

0 commit comments

Comments
 (0)