btrfs: zoned: limit active zones to max_open_zones
v3 cover btrfs: zoned: limit active zones to max_open_zones
When there is no active zone limit, we can technically write into any number of zones at the same time. However, exceeding the max open zones can degrade performance. To prevent this, this series set the max_active_zones to bdev_max_open_zones() if there is no active zone limit.
Using max_open_zones as an active zone limit enables the active zone tracking and enforcement on SMR too. The revealed some bugs on the active zone tracking. This series also includes the fixes for them.
Changes:
- v3:
- Added patch 2 to address WARN added by patch 4
- v2: https://lore.kernel.org/linux-btrfs/[email protected]/T/
- Refine commit message and add code comment to descrive the WARN condition.
- Introduce and use BTRFS_DEFAULT_MAX_ACTIVE_ZONES (= 128).
- v1: https://lore.kernel.org/linux-btrfs/[email protected]/T/
Naohiro Aota (4):
btrfs: zoned: do not select metadata BG as finish target
btrfs: zoned: fix data relocation block group reservation
btrfs: zoned: fix write time activation failure for metadata block
group
btrfs: zoned: limit active zones to max_open_zones
fs/btrfs/zoned.c | 78 +++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 64 insertions(+), 14 deletions(-)
Signature · 1 line
2.50.1
PATCH v3 1/4 btrfs: zoned: do not select metadata BG as finish target
We call btrfs_zone_finish_one_bg() to zone finish one block group and make room to activate another block group. Currently, we can choose a metadata block group as a target. But, as we reserve an active metadata block group, we no longer want to select a metadata block group. So, skip it in the loop.
--- fs/btrfs/zoned.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/zoned.c +1 -1
@@ -2650,7 +2650,7 @@ int btrfs_zone_finish_one_bg(struct btrfs_fs_info *fs_info)spin_lock (&block_group->lock);if (block_group->reserved || block_group->alloc_offset ==0 || - (block_group->flags &BTRFS_BLOCK_GROUP_SYSTEM ) || + !(block_group->flags &BTRFS_BLOCK_GROUP_DATA ) ||test_bit (BLOCK_GROUP_FLAG_ZONED_DATA_RELOC , &block_group->runtime_flags)) {spin_unlock (&block_group->lock);continue ;
Signature · 1 line
2.50.1
PATCH v3 2/4 btrfs: zoned: fix data relocation block group reservation
In reply to Naohiro Aotabtrfs_zoned_reserve_data_reloc_bg() is called on mount and at that point, all data block groups belong to the primary data space_info. So, we don't find anything in the data relocation space_info.
Also, the condition "bg->used > 0" can select a block group with full of zone_unusable bytes for the candidate. As we cannot allocate from the block group, it is useless to reserve it as the data relocation block group.
Furthermore, because of the space_info separation, we need to migrate the selected block group to the data relocation space_info. If not, the extent allocator cannot use the block group to do the allocation.
This commit fixes these three issues.
--- fs/btrfs/zoned.c | 55 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 47 insertions(+), 8 deletions(-)
fs/btrfs/zoned.c +47 -8
@@ -17,6 +17,7 @@#include "accessors.h" #include "bio.h" #include "transaction.h" +#include "sysfs.h" /* Maximum number of zones to report per blkdev_report_zones() call */ #define BTRFS_REPORT_NR_ZONES 4096
@@ -2504,12 +2505,12 @@ void btrfs_clear_data_reloc_bg(struct btrfs_block_group *bg)void btrfs_zoned_reserve_data_reloc_bg (struct btrfs_fs_info *fs_info) {struct btrfs_space_info *data_sinfo = fs_info->data_sinfo; -struct btrfs_space_info *space_info = data_sinfo->sub_group[0 ]; +struct btrfs_space_info *space_info = data_sinfo;struct btrfs_trans_handle *trans;struct btrfs_block_group *bg;struct list_head *bg_list;u64 alloc_flags; -bool initial = false; +bool first = true;bool did_chunk_alloc = false;int index;int ret;
@@ -2523,21 +2524,52 @@ void btrfs_zoned_reserve_data_reloc_bg(struct btrfs_fs_info *fs_info)if (sb_rdonly (fs_info->sb))return ; -ASSERT (space_info->subgroup_id ==BTRFS_SUB_GROUP_DATA_RELOC ); alloc_flags =btrfs_get_alloc_profile (fs_info, space_info->flags); index =btrfs_bg_flags_to_raid_index (alloc_flags); - bg_list = &data_sinfo->block_groups[index]; +/* Scan the data space_info to find empty block groups. Take the second one. */ again: + bg_list = &space_info->block_groups[index];list_for_each_entry (bg, bg_list, list) { -if (bg->used >0 ) +if (bg->alloc_offset !=0 )continue ; -if (!initial) { - initial = true; +if (first) { + first = false;continue ; } +if (space_info == data_sinfo) { +/* Migrate the block group to the data relocation space_info. */ +struct btrfs_space_info *reloc_sinfo = data_sinfo->sub_group[0 ]; +int factor; + +ASSERT (reloc_sinfo->subgroup_id ==BTRFS_SUB_GROUP_DATA_RELOC ); + factor =btrfs_bg_type_to_factor (bg->flags); + +down_write (&space_info->groups_sem); +list_del_init (&bg->list); +/* We can assume this as we choose the second empty one. */ +ASSERT (!list_empty (&space_info->block_groups[index])); +up_write (&space_info->groups_sem); + +spin_lock (&space_info->lock); + space_info->total_bytes -= bg->length; + space_info->disk_total -= bg->length * factor; +/* There is no allocation ever happened. */ +ASSERT (bg->used ==0 ); +ASSERT (bg->zone_unusable ==0 ); +/* No super block in a block group on the zoned setup. */ +ASSERT (bg->bytes_super ==0 ); +spin_unlock (&space_info->lock); + + bg->space_info = reloc_sinfo; +if (reloc_sinfo->block_group_kobjs[index] ==NULL ) +btrfs_sysfs_add_block_group_type (bg); + +btrfs_add_bg_to_space_info (fs_info, bg); + } + fs_info->data_reloc_bg = bg->start;set_bit (BLOCK_GROUP_FLAG_ZONED_DATA_RELOC , &bg->runtime_flags);btrfs_zone_activate (bg);
@@ -2552,11 +2584,18 @@ void btrfs_zoned_reserve_data_reloc_bg(struct btrfs_fs_info *fs_info)if (IS_ERR (trans))return ; +/* Allocate new BG in the data relocation space_info. */ + space_info = data_sinfo->sub_group[0 ]; +ASSERT (space_info->subgroup_id ==BTRFS_SUB_GROUP_DATA_RELOC ); ret =btrfs_chunk_alloc (trans, space_info, alloc_flags,CHUNK_ALLOC_FORCE );btrfs_end_transaction (trans);if (ret ==1 ) { +/* +* We allocated a new block group in the data relocation space_info. We +* can take that one. +*/ + first = false; did_chunk_alloc = true; - bg_list = &space_info->block_groups[index]; goto again; } }
Signature · 1 line
2.50.1
Re: [PATCH v3 2/4] btrfs: zoned: fix data relocation block group reservation
Looks good,
PATCH v3 3/4 btrfs: zoned: fix write time activation failure for metadata block group
In reply to Naohiro AotaSince commit 13bb483d32ab ("btrfs: zoned: activate metadata block group on write time"), we activate a metadata block group at the write time. If the zone capacity is small enough, we can allocate the entire region before the first write. Then, we hit the btrfs_zoned_bg_is_full() in btrfs_zone_activate() and the activation fails.
For a data block group, we activate it at the allocation time and we should check the fullness condition in the caller side. Add, a WARN to check the fullness condition.
For a metadata block group, we don't need the fullness check because we activate it at the write time. Instead, activating it once it is written should be invalid. Catch that with a WARN too.
--- fs/btrfs/zoned.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
fs/btrfs/zoned.c +9 -4
@@ -2169,10 +2169,15 @@ bool btrfs_zone_activate(struct btrfs_block_group *block_group) goto out_unlock; } -/* No space left */ -if (btrfs_zoned_bg_is_full (block_group)) { - ret = false; - goto out_unlock; +if (block_group->flags &BTRFS_BLOCK_GROUP_DATA ) { +/* The caller should check if the block group is full. */ +if (WARN_ON_ONCE (btrfs_zoned_bg_is_full (block_group))) { + ret = false; + goto out_unlock; + } + }else { +/* Since it is already written, it should have been active. */ +WARN_ON_ONCE (block_group->meta_write_pointer != block_group->start); }for (i =0 ; i < map->num_stripes; i++) {
Signature · 1 line
2.50.1
PATCH v3 4/4 btrfs: zoned: limit active zones to max_open_zones
In reply to Naohiro AotaWhen there is no active zone limit, we can technically write into any number of zones at the same time. However, exceeding the max open zones can degrade performance. To prevent this, set the max_active_zones to bdev_max_open_zones() if there is no active zone limit.
--- fs/btrfs/zoned.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
fs/btrfs/zoned.c +7 -1
@@ -43,6 +43,9 @@/* Number of superblock log zones */ #define BTRFS_NR_SB_LOG_ZONES 2 +/* Default number of max active zones when the device has no limits. */ +#define BTRFS_DEFAULT_MAX_ACTIVE_ZONES 128 +/* * Minimum of active zones we need: *
@@ -417,7 +420,10 @@ int btrfs_get_dev_zone_info(struct btrfs_device *device, bool populate_cache)if (!IS_ALIGNED (nr_sectors, zone_sectors)) zone_info->nr_zones++; - max_active_zones =bdev_max_active_zones (bdev); + max_active_zones =min_not_zero (bdev_max_active_zones (bdev), +bdev_max_open_zones (bdev)); +if (!max_active_zones && zone_info->nr_zones >BTRFS_DEFAULT_MAX_ACTIVE_ZONES ) + max_active_zones =BTRFS_DEFAULT_MAX_ACTIVE_ZONES ;if (max_active_zones && max_active_zones <BTRFS_MIN_ACTIVE_ZONES ) { btrfs_err(fs_info, "zoned: %s: max active zones %u is too small, need at least %u active zones",
Signature · 1 line
2.50.1