btrfs: zoned: reduce lock contention in zoned extent allocator
v2 cover btrfs: zoned: reduce lock contention in zoned extent allocator
From: Johannes Thumshirn <[email protected]>
While investigating performance issues on zoned btrfs, I've analyzed lockstat output while running fio workloads.
Three locks that have always been on top are the space_info's ->lock, as well as fs_info's ->treelog_bg_lock and ->relocation_bg_lock.
The first one is not as trivial to get rid and most likely legit in most cases (see patch #3). The other two are only protecting a single value in fs_info and can be replaced by REAC_ONCE() and WRITE_ONCE().
The series has been run through fstests on tcmu-runner emulated SMR drives as well as Damien's fio benchmarks on real ZNS hardware and no regressions have been observed.
Changes in v2:
- Rewording of the commit message (Damien)
- No more local variables caching the values (Damien)
Johannes Thumshirn (3):
btrfs: zoned: get rid of relocation_bg_lock
btrfs: zoned: get rid of treelog_bg_lock
btrfs: zoned: don't hold space_info lock on zoned allocation
fs/btrfs/disk-io.c | 2 - fs/btrfs/extent-tree.c | 86 +++++++++++------------------------------- fs/btrfs/fs.h | 7 +--- fs/btrfs/zoned.c | 12 +++--- fs/btrfs/zoned.h | 6 +-- 5 files changed, 31 insertions(+), 82 deletions(-)
Signature · 1 line
2.49.0
PATCH v2 1/3 btrfs: zoned: get rid of relocation_bg_lock
From: Johannes Thumshirn <[email protected]>
Lockstat analysis of benchmark workloads shows a very high contention on relocation_bg_lock. But the lock only protects a single field in 'struct btrfs_fs_info', namely 'u64 data_reloc_bg'.
Use READ_ONCE()/WRITE_ONCE() to access 'btrfs_fs_info::data_reloc_bg'.
--- fs/btrfs/disk-io.c | 1 - fs/btrfs/extent-tree.c | 34 ++++++++++++---------------------- fs/btrfs/fs.h | 6 +----- fs/btrfs/zoned.c | 10 ++++------ 4 files changed, 17 insertions(+), 34 deletions(-)
fs/btrfs/disk-io.c +0 -1
@@ -2791,7 +2791,6 @@ void btrfs_init_fs_info(struct btrfs_fs_info *fs_info)spin_lock_init (&fs_info->unused_bgs_lock);spin_lock_init (&fs_info->treelog_bg_lock);spin_lock_init (&fs_info->zone_active_bgs_lock); -spin_lock_init (&fs_info->relocation_bg_lock);rwlock_init (&fs_info->tree_mod_log_lock);rwlock_init (&fs_info->global_root_lock);mutex_init (&fs_info->unused_bg_unpin_mutex);
fs/btrfs/extent-tree.c +12 -22
@@ -3842,7 +3842,6 @@ static int do_allocation_zoned(struct btrfs_block_group *block_group,u64 avail;u64 bytenr = block_group->start;u64 log_bytenr; -u64 data_reloc_bytenr;int ret =0 ;bool skip = false;
@@ -3865,14 +3864,9 @@ static int do_allocation_zoned(struct btrfs_block_group *block_group,* Do not allow non-relocation blocks in the dedicated relocation block * group, and vice versa. */ -spin_lock (&fs_info->relocation_bg_lock); - data_reloc_bytenr = fs_info->data_reloc_bg; -if (data_reloc_bytenr && - ((ffe_ctl->for_data_reloc && bytenr != data_reloc_bytenr) || - (!ffe_ctl->for_data_reloc && bytenr == data_reloc_bytenr))) - skip = true; -spin_unlock (&fs_info->relocation_bg_lock); -if (skip) +if (READ_ONCE (fs_info->data_reloc_bg) && + ((ffe_ctl->for_data_reloc && bytenr !=READ_ONCE (fs_info->data_reloc_bg)) || + (!ffe_ctl->for_data_reloc && bytenr ==READ_ONCE (fs_info->data_reloc_bg))))return 1 ;/* Check RO and no space case before trying to activate it */
@@ -3899,7 +3893,6 @@ static int do_allocation_zoned(struct btrfs_block_group *block_group,spin_lock (&space_info->lock);spin_lock (&block_group->lock);spin_lock (&fs_info->treelog_bg_lock); -spin_lock (&fs_info->relocation_bg_lock);if (ret) goto out;
@@ -3908,8 +3901,8 @@ static int do_allocation_zoned(struct btrfs_block_group *block_group, block_group->start == fs_info->treelog_bg || fs_info->treelog_bg == 0);ASSERT (!ffe_ctl->for_data_reloc || - block_group->start == fs_info->data_reloc_bg || - fs_info->data_reloc_bg ==0 ); + block_group->start ==READ_ONCE (fs_info->data_reloc_bg) || +READ_ONCE (fs_info->data_reloc_bg) ==0 ); if (block_group->ro || (!ffe_ctl->for_data_reloc &&
@@ -3932,7 +3925,7 @@ static int do_allocation_zoned(struct btrfs_block_group *block_group,* Do not allow currently used block group to be the data relocation * dedicated block group. */ -if (ffe_ctl->for_data_reloc && !fs_info->data_reloc_bg && +if (ffe_ctl->for_data_reloc &&READ_ONCE (fs_info->data_reloc_bg) ==0 && (block_group->used || block_group->reserved)) { ret =1 ; goto out;
@@ -3957,8 +3950,8 @@ static int do_allocation_zoned(struct btrfs_block_group *block_group, fs_info->treelog_bg = block_group->start;if (ffe_ctl->for_data_reloc) { -if (!fs_info->data_reloc_bg) - fs_info->data_reloc_bg = block_group->start; +if (READ_ONCE (fs_info->data_reloc_bg) ==0 ) +WRITE_ONCE (fs_info->data_reloc_bg, block_group->start);/* * Do not allow allocations from this block group, unless it is * for data relocation. Compared to increasing the ->ro, setting
@@ -3994,8 +3987,7 @@ static int do_allocation_zoned(struct btrfs_block_group *block_group,if (ret && ffe_ctl->for_treelog) fs_info->treelog_bg =0 ;if (ret && ffe_ctl->for_data_reloc) - fs_info->data_reloc_bg =0 ; -spin_unlock (&fs_info->relocation_bg_lock); +WRITE_ONCE (fs_info->data_reloc_bg,0 );spin_unlock (&fs_info->treelog_bg_lock);spin_unlock (&block_group->lock);spin_unlock (&space_info->lock);
@@ -4303,11 +4295,9 @@ static int prepare_allocation_zoned(struct btrfs_fs_info *fs_info,if (fs_info->treelog_bg) ffe_ctl->hint_byte = fs_info->treelog_bg;spin_unlock (&fs_info->treelog_bg_lock); - }else if (ffe_ctl->for_data_reloc) { -spin_lock (&fs_info->relocation_bg_lock); -if (fs_info->data_reloc_bg) - ffe_ctl->hint_byte = fs_info->data_reloc_bg; -spin_unlock (&fs_info->relocation_bg_lock); + }else if (ffe_ctl->for_data_reloc && +READ_ONCE (fs_info->data_reloc_bg)) { + ffe_ctl->hint_byte =READ_ONCE (fs_info->data_reloc_bg); }else if (ffe_ctl->flags &BTRFS_BLOCK_GROUP_DATA ) {struct btrfs_block_group *block_group;
fs/btrfs/fs.h +1 -5
@@ -849,11 +849,7 @@ struct btrfs_fs_info {spinlock_t treelog_bg_lock;u64 treelog_bg; -/* -* Start of the dedicated data relocation block group, protected by -* relocation_bg_lock. -*/ -spinlock_t relocation_bg_lock; +/* Start of the dedicated data relocation block group */ u64 data_reloc_bg;struct mutex zoned_data_reloc_io_lock;
fs/btrfs/zoned.c +4 -6
@@ -2496,10 +2496,8 @@ void btrfs_clear_data_reloc_bg(struct btrfs_block_group *bg) {struct btrfs_fs_info *fs_info = bg->fs_info; -spin_lock (&fs_info->relocation_bg_lock); -if (fs_info->data_reloc_bg == bg->start) - fs_info->data_reloc_bg =0 ; -spin_unlock (&fs_info->relocation_bg_lock); +if (READ_ONCE (fs_info->data_reloc_bg) == bg->start) +WRITE_ONCE (fs_info->data_reloc_bg,0 ); }void btrfs_zoned_reserve_data_reloc_bg (struct btrfs_fs_info *fs_info)
@@ -2518,7 +2516,7 @@ void btrfs_zoned_reserve_data_reloc_bg(struct btrfs_fs_info *fs_info)if (!btrfs_is_zoned (fs_info))return ; -if (fs_info->data_reloc_bg) +if (READ_ONCE (fs_info->data_reloc_bg))return ;if (sb_rdonly (fs_info->sb))
@@ -2539,7 +2537,7 @@ void btrfs_zoned_reserve_data_reloc_bg(struct btrfs_fs_info *fs_info)continue ; } - fs_info->data_reloc_bg = bg->start; +WRITE_ONCE (fs_info->data_reloc_bg, bg->start);set_bit (BLOCK_GROUP_FLAG_ZONED_DATA_RELOC , &bg->runtime_flags);btrfs_zone_activate (bg);
Signature · 1 line
2.49.0
Re: [PATCH v2 1/3] btrfs: zoned: get rid of relocation_bg_lock
Quoted 87 lines · Johannes Thumshirn
From: Johannes Thumshirn <[email protected]>
Lockstat analysis of benchmark workloads shows a very high contention on
relocation_bg_lock. But the lock only protects a single field in 'structbtrfs_fs_info', namely 'u64data_reloc_bg'.Use READ_ONCE()/WRITE_ONCE() to access '
btrfs_fs_info::data_reloc_bg'.Signed-off-by Johannes Thumshirn <[email protected]>--- fs/btrfs/disk-io.c | 1 - fs/btrfs/extent-tree.c | 34 ++++++++++++---------------------- fs/btrfs/fs.h | 6 +----- fs/btrfs/zoned.c | 10 ++++------ 4 files changed, 17 insertions(+), 34 deletions(-)fs/btrfs/disk-io.c +0
-1@@ -2791,7 +2791,6 @@ void btrfs_init_fs_info(struct btrfs_fs_info *fs_info)spin_lock_init (&fs_info->unused_bgs_lock);spin_lock_init (&fs_info->treelog_bg_lock);spin_lock_init (&fs_info->zone_active_bgs_lock); -spin_lock_init (&fs_info->relocation_bg_lock);rwlock_init (&fs_info->tree_mod_log_lock);rwlock_init (&fs_info->global_root_lock);mutex_init (&fs_info->unused_bg_unpin_mutex);fs/btrfs/extent-tree.c +6
-13@@ -3842,7 +3842,6 @@ static int do_allocation_zoned(struct btrfs_block_group *block_group,u64 avail;u64 bytenr = block_group->start;u64 log_bytenr; -u64 data_reloc_bytenr;int ret =0 ;bool skip = false;@@ -3865,14 +3864,9 @@ static int do_allocation_zoned(struct btrfs_block_group *block_group,* Do not allow non-relocation blocks in the dedicated relocation block * group, and vice versa. */ -spin_lock (&fs_info->relocation_bg_lock); - data_reloc_bytenr = fs_info->data_reloc_bg; -if (data_reloc_bytenr && - ((ffe_ctl->for_data_reloc && bytenr != data_reloc_bytenr) || - (!ffe_ctl->for_data_reloc && bytenr == data_reloc_bytenr))) - skip = true; -spin_unlock (&fs_info->relocation_bg_lock); -if (skip) +if (READ_ONCE (fs_info->data_reloc_bg) && + ((ffe_ctl->for_data_reloc && bytenr !=READ_ONCE (fs_info->data_reloc_bg)) || + (!ffe_ctl->for_data_reloc && bytenr ==READ_ONCE (fs_info->data_reloc_bg))))return 1 ;/* Check RO and no space case before trying to activate it */ @@ -3899,7 +3893,6 @@ static int do_allocation_zoned(struct btrfs_block_group *block_group,spin_lock (&space_info->lock);spin_lock (&block_group->lock);spin_lock (&fs_info->treelog_bg_lock); -spin_lock (&fs_info->relocation_bg_lock);if (ret) goto out;@@ -3908,8 +3901,8 @@ static int do_allocation_zoned(struct btrfs_block_group *block_group, block_group->start == fs_info->treelog_bg || fs_info->treelog_bg == 0);ASSERT (!ffe_ctl->for_data_reloc || - block_group->start == fs_info->data_reloc_bg || - fs_info->data_reloc_bg ==0 ); + block_group->start ==READ_ONCE (fs_info->data_reloc_bg) || +READ_ONCE (fs_info->data_reloc_bg) ==0 ); if (block_group->ro || (!ffe_ctl->for_data_reloc &&@@ -3932,7 +3925,7 @@ static int do_allocation_zoned(struct btrfs_block_group *block_group,* Do not allow currently used block group to be the data relocation * dedicated block group. */ -if (ffe_ctl->for_data_reloc && !fs_info->data_reloc_bg && +if (ffe_ctl->for_data_reloc &&READ_ONCE (fs_info->data_reloc_bg) ==0 && (block_group->used || block_group->reserved)) { ret =1 ; goto out;
fs/btrfs/extent-tree.c static int do_allocation_zoned(struct btrfs_block_group *block_group, · line 3954
Quoted 7 lines
fs/btrfs/extent-tree.c +2
-2@@ -3957,8 +3950,8 @@ static int do_allocation_zoned(struct btrfs_block_group *block_group, fs_info->treelog_bg = block_group->start;if (ffe_ctl->for_data_reloc) { -if (!fs_info->data_reloc_bg) - fs_info->data_reloc_bg = block_group->start; +if (READ_ONCE (fs_info->data_reloc_bg) ==0 ) +WRITE_ONCE (fs_info->data_reloc_bg, block_group->start);
What happens two threads reach here at the same time and each has a different block group locked? One of them will get an unexpected state.
Apparently, it does not happen because we have the space_info->lock around, but patch 3 will remove it...
Quoted 80 lines
fs/btrfs/extent-tree.c +4
-7/* * Do not allow allocations from this block group, unless it is * for data relocation. Compared to increasing the ->ro, setting @@ -3994,8 +3987,7 @@ static int do_allocation_zoned(struct btrfs_block_group *block_group,if (ret && ffe_ctl->for_treelog) fs_info->treelog_bg =0 ;if (ret && ffe_ctl->for_data_reloc) - fs_info->data_reloc_bg =0 ; -spin_unlock (&fs_info->relocation_bg_lock); +WRITE_ONCE (fs_info->data_reloc_bg,0 );spin_unlock (&fs_info->treelog_bg_lock);spin_unlock (&block_group->lock);spin_unlock (&space_info->lock);@@ -4303,11 +4295,9 @@ static int prepare_allocation_zoned(struct btrfs_fs_info *fs_info,if (fs_info->treelog_bg) ffe_ctl->hint_byte = fs_info->treelog_bg;spin_unlock (&fs_info->treelog_bg_lock); - }else if (ffe_ctl->for_data_reloc) { -spin_lock (&fs_info->relocation_bg_lock); -if (fs_info->data_reloc_bg) - ffe_ctl->hint_byte = fs_info->data_reloc_bg; -spin_unlock (&fs_info->relocation_bg_lock); + }else if (ffe_ctl->for_data_reloc && +READ_ONCE (fs_info->data_reloc_bg)) { + ffe_ctl->hint_byte =READ_ONCE (fs_info->data_reloc_bg); }else if (ffe_ctl->flags &BTRFS_BLOCK_GROUP_DATA ) {struct btrfs_block_group *block_group;fs/btrfs/fs.h +1
-5@@ -849,11 +849,7 @@ struct btrfs_fs_info {spinlock_t treelog_bg_lock;u64 treelog_bg; -/* -* Start of the dedicated data relocation block group, protected by -* relocation_bg_lock. -*/ -spinlock_t relocation_bg_lock; +/* Start of the dedicated data relocation block group */ u64 data_reloc_bg;struct mutex zoned_data_reloc_io_lock;fs/btrfs/zoned.c +4
-6@@ -2496,10 +2496,8 @@ void btrfs_clear_data_reloc_bg(struct btrfs_block_group *bg) {struct btrfs_fs_info *fs_info = bg->fs_info; -spin_lock (&fs_info->relocation_bg_lock); -if (fs_info->data_reloc_bg == bg->start) - fs_info->data_reloc_bg =0 ; -spin_unlock (&fs_info->relocation_bg_lock); +if (READ_ONCE (fs_info->data_reloc_bg) == bg->start) +WRITE_ONCE (fs_info->data_reloc_bg,0 ); }void btrfs_zoned_reserve_data_reloc_bg (struct btrfs_fs_info *fs_info)@@ -2518,7 +2516,7 @@ void btrfs_zoned_reserve_data_reloc_bg(struct btrfs_fs_info *fs_info)if (!btrfs_is_zoned (fs_info))return ; -if (fs_info->data_reloc_bg) +if (READ_ONCE (fs_info->data_reloc_bg))return ;if (sb_rdonly (fs_info->sb))@@ -2539,7 +2537,7 @@ void btrfs_zoned_reserve_data_reloc_bg(struct btrfs_fs_info *fs_info)continue ; } - fs_info->data_reloc_bg = bg->start; +WRITE_ONCE (fs_info->data_reloc_bg, bg->start);set_bit (BLOCK_GROUP_FLAG_ZONED_DATA_RELOC , &bg->runtime_flags);btrfs_zone_activate (bg);
Re: [PATCH v2 1/3] btrfs: zoned: get rid of relocation_bg_lock
Quoted 5 lines · Naohiro Aota
What happens two threads reach here at the same time and each has a different block group locked? One of them will get an unexpected state.
Apparently, it does not happen because we have the
space_info->lockaround, but patch 3 will remove it...
But can this really happen, now that we have dedicated space_infos for relocation and treelog?
PATCH v2 2/3 btrfs: zoned: get rid of treelog_bg_lock
In reply to Johannes ThumshirnFrom: Johannes Thumshirn <[email protected]>
Lockstat analysis of benchmark workloads shows a very high contention on treelog_bg_lock. But the lock only protects a single field in 'struct btrfs_fs_info', namely 'u64 treelog_bg'.
Use READ_ONCE()/WRITE_ONCE() to access 'btrfs_fs_info::treelog_bg'.
--- fs/btrfs/disk-io.c | 1 - fs/btrfs/extent-tree.c | 49 ++++++++++-------------------------------- fs/btrfs/fs.h | 1 - fs/btrfs/zoned.c | 2 +- fs/btrfs/zoned.h | 6 ++---- 5 files changed, 14 insertions(+), 45 deletions(-)
fs/btrfs/disk-io.c +0 -1
@@ -2789,7 +2789,6 @@ void btrfs_init_fs_info(struct btrfs_fs_info *fs_info)spin_lock_init (&fs_info->defrag_inodes_lock);spin_lock_init (&fs_info->super_lock);spin_lock_init (&fs_info->unused_bgs_lock); -spin_lock_init (&fs_info->treelog_bg_lock);spin_lock_init (&fs_info->zone_active_bgs_lock);rwlock_init (&fs_info->tree_mod_log_lock);rwlock_init (&fs_info->global_root_lock);
fs/btrfs/extent-tree.c +11 -38
105 lines not on this page · raw message
fs/btrfs/fs.h +0 -1
@@ -846,7 +846,6 @@ struct btrfs_fs_info {u64 max_zone_append_size;struct mutex zoned_meta_io_lock; -spinlock_t treelog_bg_lock;u64 treelog_bg;/* Start of the dedicated data relocation block group */
fs/btrfs/zoned.c +1 -1
@@ -1948,7 +1948,7 @@ static bool check_bg_is_active(struct btrfs_eb_write_context *ctx,if (test_bit (BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE , &block_group->runtime_flags))return true; -if (fs_info->treelog_bg == block_group->start) { +if (READ_ONCE (fs_info->treelog_bg) == block_group->start) {if (!btrfs_zone_activate (block_group)) {int ret_fin =btrfs_zone_finish_one_bg (fs_info);
fs/btrfs/zoned.h +2 -4
@@ -387,10 +387,8 @@ static inline void btrfs_clear_treelog_bg(struct btrfs_block_group *bg)if (!btrfs_is_zoned (fs_info))return ; -spin_lock (&fs_info->treelog_bg_lock); -if (fs_info->treelog_bg == bg->start) - fs_info->treelog_bg =0 ; -spin_unlock (&fs_info->treelog_bg_lock); +if (READ_ONCE (fs_info->treelog_bg) == bg->start) +WRITE_ONCE (fs_info->treelog_bg,0 ); }static inline void btrfs_zoned_data_reloc_lock (struct btrfs_inode *inode)
Signature · 1 line
2.49.0
PATCH v2 3/3 btrfs: zoned: don't hold space_info lock on zoned allocation
In reply to Johannes ThumshirnFrom: Johannes Thumshirn <[email protected]>
The zoned extent allocator holds 'struct btrfs_space_info::lock' nearly over the entirety of the allocation process, but nothing in do_allocation_zoned() is actually accessing fields of 'struct btrfs_space_info'.
Furthermore taking lock_stat snapshots in performance testing, always shows the space_info::lock as the most contented lock in the entire system.
Remove locking the space_info lock during do_allocation_zoned() to reduce lock contention.
--- fs/btrfs/extent-tree.c | 3 --- 1 file changed, 3 deletions(-)
fs/btrfs/extent-tree.c +0 -3
@@ -3819,7 +3819,6 @@ static int do_allocation_zoned(struct btrfs_block_group *block_group, struct btrfs_block_group **bg_ret) {struct btrfs_fs_info *fs_info = block_group->fs_info; -struct btrfs_space_info *space_info = block_group->space_info;struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;u64 start = block_group->start;u64 num_bytes = ffe_ctl->num_bytes;
@@ -3868,7 +3867,6 @@ static int do_allocation_zoned(struct btrfs_block_group *block_group,*/ } -spin_lock (&space_info->lock);spin_lock (&block_group->lock);if (ret)
@@ -3966,7 +3964,6 @@ static int do_allocation_zoned(struct btrfs_block_group *block_group,if (ret && ffe_ctl->for_data_reloc)WRITE_ONCE (fs_info->data_reloc_bg,0 );spin_unlock (&block_group->lock); -spin_unlock (&space_info->lock);return ret; }
Signature · 1 line
2.49.0