btrfs: zoned: write-time activation of metadata block group
v3 cover btrfs: zoned: write-time activation of metadata block group
In the current implementation, block groups are activated at reservation time to ensure that all reserved bytes can be written to an active metadata block group. However, this approach has proven to be less efficient, as it activates block groups more frequently than necessary, putting pressure on the active zone resource and leading to potential issues such as early ENOSPC or hung_task.
Another drawback of the current method is that it hampers metadata over-commit, and necessitates additional flush operations and block group allocations, resulting in decreased overall performance.
Actually, we don't need so many active metadata block groups because there is only one sequential metadata write stream.
So, this series introduces a write-time activation of metadata and system block group. This involves reserving at least one active block group specifically for a metadata and system block group. When the write goes into a new block group, it should have allocated all the regions in the current active block group. So, we can wait for IOs to fill the space, and then switch to a new block group.
Switching to the write-time activation solves the above issue and will lead to better performance.
* Performance
There is a significant difference with a workload (buffered write without sync) because we re-enable metadata over-commit.
before the patch: 741.00 MB/sec after the patch: 1430.27 MB/sec (+ 93%)
* Organization
Patches 1-5 are preparation patches involves meta_write_pointer check.
Patches 6 and 7 are the main part of this series, implementing the write-time activation.
Patches 8-10 addresses code for reserve time activation: counting fresh block group as zone_unusable, activating a block group on allocation, and disabling metadata over-commit.
* Changes
- v3
- Rework the reservation patch to fix the over-reservation problem
https://lore.kernel.org/all/xpb5wdmxx5wops26ihulo73oluc64dt4zpxqc7cirp2wvxl3qy@hv7lsvma5hxf/
- Rename btrfs_eb_write_context's block_group to zoned_bg.
- v2
- Introduce a struct to consolidate extent buffer write context (btrfs_eb_write_context)
- Change return type of btrfs_check_meta_write_pointer to int
- Calculate the reservation count only when it sees DUP BG
- Drop unnecessary BG lock
Naohiro Aota (10):
btrfs: introduce struct to consolidate extent buffer write context
btrfs: zoned: introduce block group context to btrfs_eb_write_context
btrfs: zoned: return int from btrfs_check_meta_write_pointer
btrfs: zoned: defer advancing meta_write_pointer
btrfs: zoned: update meta_write_pointer on zone finish
btrfs: zoned: reserve zones for an active metadata/system block group
btrfs: zoned: activate metadata block group on write time
btrfs: zoned: no longer count fresh BG region as zone unusable
btrfs: zoned: don't activate non-DATA BG on allocation
btrfs: zoned: re-enable metadata over-commit for zoned mode
fs/btrfs/block-group.c | 13 +- fs/btrfs/disk-io.c | 2 + fs/btrfs/extent-tree.c | 8 +- fs/btrfs/extent_io.c | 44 +++--- fs/btrfs/extent_io.h | 7 + fs/btrfs/free-space-cache.c | 8 +- fs/btrfs/fs.h | 3 + fs/btrfs/space-info.c | 34 +---- fs/btrfs/zoned.c | 259 ++++++++++++++++++++++++++++-------- fs/btrfs/zoned.h | 29 ++-- 10 files changed, 273 insertions(+), 134 deletions(-)
Signature · 1 line
2.41.0
PATCH v3 1/10 btrfs: introduce struct to consolidate extent buffer write context
Introduce btrfs_eb_write_context to consolidate writeback_control and the exntent buffer context.
This will help adding a block group context as well.
While at it, move the eb context setting before btrfs_check_meta_write_pointer(). We can set it here because we anyway need to skip pages in the same eb if that eb is rejected by btrfs_check_meta_write_pointer().
--- fs/btrfs/extent_io.c | 14 +++++++------- fs/btrfs/extent_io.h | 5 +++++ 2 files changed, 12 insertions(+), 7 deletions(-)
fs/btrfs/extent_io.c +7 -7
@@ -1784,9 +1784,9 @@ static int submit_eb_subpage(struct page *page, struct writeback_control *wbc)* previous call. * Return <0 for fatal error. */ -static int submit_eb_page (struct page *page,struct writeback_control *wbc, -struct extent_buffer **eb_context) +static int submit_eb_page (struct page *page,struct btrfs_eb_write_context *ctx) { +struct writeback_control *wbc = ctx->wbc;struct address_space *mapping = page->mapping;struct btrfs_block_group *cache =NULL ;struct extent_buffer *eb;
@@ -1815,7 +1815,7 @@ static int submit_eb_page(struct page *page, struct writeback_control *wbc,return 0 ; } -if (eb == *eb_context) { +if (eb == ctx->eb) {spin_unlock (&mapping->private_lock);return 0 ; }
@@ -1824,6 +1824,8 @@ static int submit_eb_page(struct page *page, struct writeback_control *wbc,if (!ret)return 0 ; + ctx->eb = eb; +if (!btrfs_check_meta_write_pointer (eb->fs_info, eb, &cache)) {/* * If for_sync, this hole will be filled with
@@ -1837,8 +1839,6 @@ static int submit_eb_page(struct page *page, struct writeback_control *wbc,return ret; } - *eb_context = eb; -if (!lock_extent_buffer_for_io (eb, wbc)) {btrfs_revert_meta_write_pointer (cache, eb);if (cache)
@@ -1861,7 +1861,7 @@ static int submit_eb_page(struct page *page, struct writeback_control *wbc,int btree_write_cache_pages (struct address_space *mapping,struct writeback_control *wbc) { -struct extent_buffer *eb_context =NULL ; +struct btrfs_eb_write_context ctx = { .wbc = wbc };struct btrfs_fs_info *fs_info =BTRFS_I (mapping->host)->root->fs_info;int ret =0 ;int done =0 ;
@@ -1903,7 +1903,7 @@ int btree_write_cache_pages(struct address_space *mapping,for (i =0 ; i < nr_folios; i++) {struct folio *folio = fbatch.folios[i]; - ret =submit_eb_page (&folio->page, wbc, &eb_context); + ret =submit_eb_page (&folio->page, &ctx);if (ret ==0 )continue ;if (ret <0 ) {
fs/btrfs/extent_io.h +5 -0
@@ -93,6 +93,11 @@ struct extent_buffer {#endif }; +struct btrfs_eb_write_context { +struct writeback_control *wbc; +struct extent_buffer *eb; +}; +/* * Get the correct offset inside the page of extent buffer. *
Signature · 1 line
2.41.0
PATCH v3 2/10 btrfs: zoned: introduce block group context to btrfs_eb_write_context
In reply to Naohiro AotaFor metadata write out on the zoned mode, we call btrfs_check_meta_write_pointer() to check if an extent buffer to be written is aligned to the write pointer.
We lookup for a block group containing the extent buffer for every extent buffer, which take unnecessary effort as the writing extent buffers are mostly contiguous.
Introduce "zoned_bg" to cache the block group working on.
Also, while at it, rename "cache" to "block_group".
--- fs/btrfs/extent_io.c | 15 +++++++-------- fs/btrfs/extent_io.h | 2 ++ fs/btrfs/zoned.c | 35 ++++++++++++++++++++--------------- fs/btrfs/zoned.h | 6 ++---- 4 files changed, 31 insertions(+), 27 deletions(-)
fs/btrfs/extent_io.c +7 -8
@@ -1788,7 +1788,6 @@ static int submit_eb_page(struct page *page, struct btrfs_eb_write_context *ctx) {struct writeback_control *wbc = ctx->wbc;struct address_space *mapping = page->mapping; -struct btrfs_block_group *cache =NULL ;struct extent_buffer *eb;int ret;
@@ -1826,7 +1825,7 @@ static int submit_eb_page(struct page *page, struct btrfs_eb_write_context *ctx) ctx->eb = eb; -if (!btrfs_check_meta_write_pointer (eb->fs_info, eb, &cache)) { +if (!btrfs_check_meta_write_pointer (eb->fs_info, ctx)) {/* * If for_sync, this hole will be filled with * trasnsaction commit.
@@ -1840,18 +1839,15 @@ static int submit_eb_page(struct page *page, struct btrfs_eb_write_context *ctx) }if (!lock_extent_buffer_for_io (eb, wbc)) { -btrfs_revert_meta_write_pointer (cache, eb); -if (cache) -btrfs_put_block_group (cache); +btrfs_revert_meta_write_pointer (ctx->zoned_bg, eb);free_extent_buffer (eb);return 0 ; } -if (cache) { +if (ctx->zoned_bg) {/* * Implies write in zoned mode. Mark the last eb in a block group. */ -btrfs_schedule_zone_finish_bg (cache, eb); -btrfs_put_block_group (cache); +btrfs_schedule_zone_finish_bg (ctx->zoned_bg, eb); }write_one_eb (eb, wbc);free_extent_buffer (eb);
@@ -1964,6 +1960,9 @@ int btree_write_cache_pages(struct address_space *mapping, ret =0 ;if (!ret &&BTRFS_FS_ERROR (fs_info)) ret = -EROFS ; + +if (ctx.zoned_bg) +btrfs_put_block_group (ctx.zoned_bg);btrfs_zoned_meta_io_unlock (fs_info);return ret; }
fs/btrfs/extent_io.h +2 -0
@@ -96,6 +96,8 @@ struct extent_buffer {struct btrfs_eb_write_context {struct writeback_control *wbc;struct extent_buffer *eb; +/* Block group @eb resides in. Only used for zoned mode. */ +struct btrfs_block_group *zoned_bg; };/*
fs/btrfs/zoned.c +20 -15
@@ -1748,30 +1748,35 @@ void btrfs_finish_ordered_zoned(struct btrfs_ordered_extent *ordered) }bool btrfs_check_meta_write_pointer (struct btrfs_fs_info *fs_info, -struct extent_buffer *eb, -struct btrfs_block_group **cache_ret) +struct btrfs_eb_write_context *ctx) { -struct btrfs_block_group *cache; -bool ret = true; +const struct extent_buffer *eb = ctx->eb; +struct btrfs_block_group *block_group = ctx->zoned_bg;if (!btrfs_is_zoned (fs_info))return true; - cache =btrfs_lookup_block_group (fs_info, eb->start); -if (!cache) -return true; +if (block_group) { +if (block_group->start > eb->start || + block_group->start + block_group->length <= eb->start) { +btrfs_put_block_group (block_group); + block_group =NULL ; + ctx->zoned_bg =NULL ; + } + } -if (cache->meta_write_pointer != eb->start) { -btrfs_put_block_group (cache); - cache =NULL ; - ret = false; - }else { - cache->meta_write_pointer = eb->start + eb->len; +if (!block_group) { + block_group =btrfs_lookup_block_group (fs_info, eb->start); +if (!block_group) +return true; + ctx->zoned_bg = block_group; } - *cache_ret = cache; +if (block_group->meta_write_pointer != eb->start) +return false; + block_group->meta_write_pointer = eb->start + eb->len; -return ret; +return true; } void btrfs_revert_meta_write_pointer(struct btrfs_block_group *cache,
fs/btrfs/zoned.h +2 -4
@@ -59,8 +59,7 @@ void btrfs_redirty_list_add(struct btrfs_transaction *trans,bool btrfs_use_zone_append (struct btrfs_bio *bbio);void btrfs_record_physical_zoned (struct btrfs_bio *bbio);bool btrfs_check_meta_write_pointer (struct btrfs_fs_info *fs_info, -struct extent_buffer *eb, -struct btrfs_block_group **cache_ret); +struct btrfs_eb_write_context *ctx);void btrfs_revert_meta_write_pointer (struct btrfs_block_group *cache,struct extent_buffer *eb);int btrfs_zoned_issue_zeroout (struct btrfs_device *device,u64 physical,u64 length);
@@ -190,8 +189,7 @@ static inline void btrfs_record_physical_zoned(struct btrfs_bio *bbio) }static inline bool btrfs_check_meta_write_pointer (struct btrfs_fs_info *fs_info, -struct extent_buffer *eb, -struct btrfs_block_group **cache_ret) +struct btrfs_eb_write_context *ctx) {return true; }
Signature · 1 line
2.41.0
PATCH v3 3/10 btrfs: zoned: return int from btrfs_check_meta_write_pointer
In reply to Naohiro AotaNow that we have writeback_control passed to btrfs_check_meta_write_pointer(), we can move the wbc condition in submit_eb_page() to btrfs_check_meta_write_pointer() and return int.
--- fs/btrfs/extent_io.c | 11 +++-------- fs/btrfs/zoned.c | 30 ++++++++++++++++++++++-------- fs/btrfs/zoned.h | 10 +++++----- 3 files changed, 30 insertions(+), 21 deletions(-)
fs/btrfs/extent_io.c +3 -8
@@ -1825,14 +1825,9 @@ static int submit_eb_page(struct page *page, struct btrfs_eb_write_context *ctx) ctx->eb = eb; -if (!btrfs_check_meta_write_pointer (eb->fs_info, ctx)) { -/* -* If for_sync, this hole will be filled with -* trasnsaction commit. -*/ -if (wbc->sync_mode ==WB_SYNC_ALL && !wbc->for_sync) - ret = -EAGAIN ; -else + ret =btrfs_check_meta_write_pointer (eb->fs_info, ctx); +if (ret) { +if (ret == -EBUSY ) ret =0 ;free_extent_buffer (eb);return ret;
fs/btrfs/zoned.c +22 -8
@@ -1747,14 +1747,23 @@ void btrfs_finish_ordered_zoned(struct btrfs_ordered_extent *ordered) } } -bool btrfs_check_meta_write_pointer (struct btrfs_fs_info *fs_info, -struct btrfs_eb_write_context *ctx) +/* +* Check @ctx->eb is aligned to the write pointer +* +* Return: +* 0: @ctx->eb is at the write pointer. You can write it. +* -EAGAIN: There is a hole. The caller should handle the case. +* -EBUSY: There is a hole, but the caller can just bail out. +*/ +int btrfs_check_meta_write_pointer (struct btrfs_fs_info *fs_info, +struct btrfs_eb_write_context *ctx) { +const struct writeback_control *wbc = ctx->wbc;const struct extent_buffer *eb = ctx->eb;struct btrfs_block_group *block_group = ctx->zoned_bg;if (!btrfs_is_zoned (fs_info)) -return true; +return 0 ;if (block_group) { if (block_group->start > eb->start ||
@@ -1768,15 +1777,20 @@ bool btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info,if (!block_group) { block_group =btrfs_lookup_block_group (fs_info, eb->start);if (!block_group) -return true; +return 0 ; ctx->zoned_bg = block_group; } -if (block_group->meta_write_pointer != eb->start) -return false; - block_group->meta_write_pointer = eb->start + eb->len; +if (block_group->meta_write_pointer == eb->start) { + block_group->meta_write_pointer = eb->start + eb->len; -return true; +return 0 ; + } + +/* If for_sync, this hole will be filled with trasnsaction commit. */ +if (wbc->sync_mode ==WB_SYNC_ALL && !wbc->for_sync) +return -EAGAIN ; +return -EBUSY ; } void btrfs_revert_meta_write_pointer(struct btrfs_block_group *cache,
fs/btrfs/zoned.h +5 -5
@@ -58,8 +58,8 @@ void btrfs_redirty_list_add(struct btrfs_transaction *trans, struct extent_buffer *eb);bool btrfs_use_zone_append (struct btrfs_bio *bbio);void btrfs_record_physical_zoned (struct btrfs_bio *bbio); -bool btrfs_check_meta_write_pointer (struct btrfs_fs_info *fs_info, -struct btrfs_eb_write_context *ctx); +int btrfs_check_meta_write_pointer (struct btrfs_fs_info *fs_info, +struct btrfs_eb_write_context *ctx);void btrfs_revert_meta_write_pointer (struct btrfs_block_group *cache,struct extent_buffer *eb);int btrfs_zoned_issue_zeroout (struct btrfs_device *device,u64 physical,u64 length);
@@ -188,10 +188,10 @@ static inline void btrfs_record_physical_zoned(struct btrfs_bio *bbio) { } -static inline bool btrfs_check_meta_write_pointer (struct btrfs_fs_info *fs_info, -struct btrfs_eb_write_context *ctx) +static inline int btrfs_check_meta_write_pointer (struct btrfs_fs_info *fs_info, +struct btrfs_eb_write_context *ctx) { -return true; +return 0 ; } static inline void btrfs_revert_meta_write_pointer(
Signature · 1 line
2.41.0
PATCH v3 4/10 btrfs: zoned: defer advancing meta_write_pointer
In reply to Naohiro AotaWe currently advance the meta_write_pointer in btrfs_check_meta_write_pointer(). That make it necessary to revert to it when locking the buffer failed. Instead, we can advance it just before sending the buffer.
Also, this is necessary for the following commit. In the commit, it needs to release the zoned_meta_io_lock to allow IOs to come in and wait for them to fill the currently active block group. If we advance the meta_write_pointer before locking the extent buffer, the following extent buffer can pass the meta_write_pointer check, resuting in an unaligned write failure.
Advancing the pointer is still thread-safe as the extent buffer is locked.
--- fs/btrfs/extent_io.c | 8 ++++---- fs/btrfs/zoned.c | 15 +-------------- fs/btrfs/zoned.h | 8 -------- 3 files changed, 5 insertions(+), 26 deletions(-)
fs/btrfs/extent_io.c +4 -4
@@ -1834,15 +1834,15 @@ static int submit_eb_page(struct page *page, struct btrfs_eb_write_context *ctx) }if (!lock_extent_buffer_for_io (eb, wbc)) { -btrfs_revert_meta_write_pointer (ctx->zoned_bg, eb);free_extent_buffer (eb);return 0 ; }if (ctx->zoned_bg) { -/* -* Implies write in zoned mode. Mark the last eb in a block group. -*/ +/* Implies write in zoned mode. */ + +/* Mark the last eb in the block group. */ btrfs_schedule_zone_finish_bg (ctx->zoned_bg, eb); + ctx->zoned_bg->meta_write_pointer += eb->len; }write_one_eb (eb, wbc);free_extent_buffer (eb);
fs/btrfs/zoned.c +1 -14
@@ -1781,11 +1781,8 @@ int btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info, ctx->zoned_bg = block_group; } -if (block_group->meta_write_pointer == eb->start) { - block_group->meta_write_pointer = eb->start + eb->len; - +if (block_group->meta_write_pointer == eb->start)return 0 ; - }/* If for_sync, this hole will be filled with trasnsaction commit. */ if (wbc->sync_mode ==WB_SYNC_ALL && !wbc->for_sync)
@@ -1793,16 +1790,6 @@ int btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info,return -EBUSY ; } -void btrfs_revert_meta_write_pointer (struct btrfs_block_group *cache, -struct extent_buffer *eb) -{ -if (!btrfs_is_zoned (eb->fs_info) || !cache) -return ; - -ASSERT (cache->meta_write_pointer == eb->start + eb->len); - cache->meta_write_pointer = eb->start; -} -int btrfs_zoned_issue_zeroout (struct btrfs_device *device,u64 physical,u64 length) {if (!btrfs_dev_is_sequential (device, physical))
fs/btrfs/zoned.h +0 -8
@@ -60,8 +60,6 @@ bool btrfs_use_zone_append(struct btrfs_bio *bbio);void btrfs_record_physical_zoned (struct btrfs_bio *bbio);int btrfs_check_meta_write_pointer (struct btrfs_fs_info *fs_info,struct btrfs_eb_write_context *ctx); -void btrfs_revert_meta_write_pointer (struct btrfs_block_group *cache, -struct extent_buffer *eb);int btrfs_zoned_issue_zeroout (struct btrfs_device *device,u64 physical,u64 length);int btrfs_sync_zone_write_pointer (struct btrfs_device *tgt_dev,u64 logical,u64 physical_start,u64 physical_pos);
@@ -194,12 +192,6 @@ static inline int btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info,return 0 ; } -static inline void btrfs_revert_meta_write_pointer ( -struct btrfs_block_group *cache, -struct extent_buffer *eb) -{ -} -static inline int btrfs_zoned_issue_zeroout (struct btrfs_device *device,u64 physical,u64 length) {
Signature · 1 line
2.41.0
PATCH v3 5/10 btrfs: zoned: update meta_write_pointer on zone finish
In reply to Naohiro AotaOn finishing a zone, the meta_write_pointer should be set of the end of the zone to reflect the actual write pointer position.
--- fs/btrfs/zoned.c | 3 +++ 1 file changed, 3 insertions(+)
fs/btrfs/zoned.c +3 -0
@@ -2056,6 +2056,9 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writclear_bit (BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE , &block_group->runtime_flags); block_group->alloc_offset = block_group->zone_capacity; +if (block_group->flags & (BTRFS_BLOCK_GROUP_METADATA |BTRFS_BLOCK_GROUP_SYSTEM )) + block_group->meta_write_pointer = block_group->start + + block_group->zone_capacity; block_group->free_space_ctl->free_space =0 ;btrfs_clear_treelog_bg (block_group);btrfs_clear_data_reloc_bg (block_group);
Signature · 1 line
2.41.0
PATCH v3 6/10 btrfs: zoned: reserve zones for an active metadata/system block group
In reply to Naohiro AotaEnsure a metadata and system block group can be activated on write time, by leaving a certain number of active zones when trying to activate a data block group.
Zones for two metadata block groups (normal and tree-log) and one system block group are reserved, according to the profile type: two zones per block group on the DUP profile and one zone per block group otherwise.
The reservation must be freed once a non-data block group is allocated. If not, we over-reserve the active zones and data block group activation will suffer. For the dynamic reservation count, we need to manage the reservation count per device.
The reservation count variable is protected byfs_info->zone_active_bgs_lock.
--- fs/btrfs/disk-io.c | 2 + fs/btrfs/zoned.c | 97 +++++++++++++++++++++++++++++++++++++++++++--- fs/btrfs/zoned.h | 9 +++++ 3 files changed, 103 insertions(+), 5 deletions(-)
fs/btrfs/disk-io.c +2 -0
@@ -3456,6 +3456,8 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_devicebtrfs_free_zone_cache (fs_info); +btrfs_check_active_zone_reservation (fs_info); +if (!sb_rdonly (sb) && fs_info->fs_devices->missing_devices && !btrfs_check_rw_degradable (fs_info,NULL )) { btrfs_warn(fs_info,
fs/btrfs/zoned.c +92 -5
163 lines not on this page · raw message
fs/btrfs/zoned.h +9 -0
@@ -22,6 +22,12 @@ struct btrfs_zoned_device_info {u8 zone_size_shift;u32 nr_zones;unsigned int max_active_zones; +/* +* Reserved active zones for one metadata and one system block +* group. It can vary per-device depending of the allocation +* status. +*/ +int reserved_active_zones;atomic_t active_zones_left;unsigned long *seq_zones;unsigned long *empty_zones;
@@ -78,6 +84,7 @@ void btrfs_zoned_release_data_reloc_bg(struct btrfs_fs_info *fs_info, u64 logicaint btrfs_zone_finish_one_bg (struct btrfs_fs_info *fs_info);int btrfs_zoned_activate_one_bg (struct btrfs_fs_info *fs_info,struct btrfs_space_info *space_info,bool do_finish); +void btrfs_check_active_zone_reservation (struct btrfs_fs_info *fs_info);#else /* CONFIG_BLK_DEV_ZONED */ static inline int btrfs_get_dev_zone (struct btrfs_device *device,u64 pos,struct blk_zone *zone)
@@ -252,6 +259,8 @@ static inline int btrfs_zoned_activate_one_bg(struct btrfs_fs_info *fs_info, return 0; } +static inline void btrfs_check_active_zone_reservation (struct btrfs_fs_info *fs_info) { } +#endif static inline bool btrfs_dev_is_sequential (struct btrfs_device *device,u64 pos)
Signature · 1 line
2.41.0
PATCH v3 7/10 btrfs: zoned: activate metadata block group on write time
In reply to Naohiro AotaIn the current implementation, block groups are activated at reservation time to ensure that all reserved bytes can be written to an active metadata block group. However, this approach has proven to be less efficient, as it activates block groups more frequently than necessary, putting pressure on the active zone resource and leading to potential issues such as early ENOSPC or hung_task.
Another drawback of the current method is that it hampers metadata over-commit, and necessitates additional flush operations and block group allocations, resulting in decreased overall performance.
To address these issues, this commit introduces a write-time activation of metadata and system block group. This involves reserving at least one active block group specifically for a metadata and system block group.
Since metadata write-out is always allocated sequentially, when we need to write to a non-active block group, we can wait for the ongoing IOs to complete, activate a new block group, and then proceed with writing to the new block group.
--- fs/btrfs/block-group.c | 11 ++++++ fs/btrfs/fs.h | 3 ++ fs/btrfs/zoned.c | 83 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 95 insertions(+), 2 deletions(-)
fs/btrfs/block-group.c +11 -0
@@ -4287,6 +4287,17 @@ int btrfs_free_block_groups(struct btrfs_fs_info *info)struct btrfs_caching_control *caching_ctl;struct rb_node *n; +if (btrfs_is_zoned (info)) { +if (info->active_meta_bg) { +btrfs_put_block_group (info->active_meta_bg); + info->active_meta_bg =NULL ; + } +if (info->active_system_bg) { +btrfs_put_block_group (info->active_system_bg); + info->active_system_bg =NULL ; + } + } +write_lock (&info->block_group_cache_lock);while (!list_empty (&info->caching_block_groups)) { caching_ctl = list_entry(info->caching_block_groups.next,
fs/btrfs/fs.h +3 -0
@@ -770,6 +770,9 @@ struct btrfs_fs_info {u64 data_reloc_bg;struct mutex zoned_data_reloc_io_lock; +struct btrfs_block_group *active_meta_bg; +struct btrfs_block_group *active_system_bg; +u64 nr_global_roots;spinlock_t zone_active_bgs_lock;
fs/btrfs/zoned.c +81 -2
101 lines not on this page · raw message
Signature · 1 line
2.41.0
Re: [PATCH v3 07/10] btrfs: zoned: activate metadata block group on write time
fs/btrfs/zoned.c line 69
Quoted 8 lines · Naohiro Aota
fs/btrfs/zoned.c +2
-0@@ -65,6 +65,9 @@#define SUPER_INFO_SECTORS ((u64)BTRFS_SUPER_INFO_SIZE >> SECTOR_SHIFT) +static void wait_eb_writebacks (struct btrfs_block_group *block_group); +static int do_zone_finish (struct btrfs_block_group *block_group,bool fully_written);
Looks like the forward declarations can't be avoided due to btrfs_check_meta_write_pointer being defined earlier in the file.
PATCH v3 8/10 btrfs: zoned: no longer count fresh BG region as zone unusable
In reply to Naohiro AotaNow that we switched to write time activation, we no longer need to (and must not) count the fresh region as zone unusable. This commit is similar to revert commit fc22cf8eba79 ("btrfs: zoned: count fresh BG region as zone unusable").
--- fs/btrfs/free-space-cache.c | 8 +------- fs/btrfs/zoned.c | 26 +++----------------------- 2 files changed, 4 insertions(+), 30 deletions(-)
fs/btrfs/free-space-cache.c +1 -7
@@ -2704,13 +2704,8 @@ static int __btrfs_add_free_space_zoned(struct btrfs_block_group *block_group, bg_reclaim_threshold =READ_ONCE (sinfo->bg_reclaim_threshold);spin_lock (&ctl->tree_lock); -/* Count initial region as zone_unusable until it gets activated. */ if (!used) to_free = size; -else if (initial && -test_bit (BTRFS_FS_ACTIVE_ZONE_TRACKING , &block_group->fs_info->flags) && - (block_group->flags & (BTRFS_BLOCK_GROUP_METADATA |BTRFS_BLOCK_GROUP_SYSTEM ))) - to_free =0 ;else if (initial) to_free = block_group->zone_capacity;else if (offset >= block_group->alloc_offset)
@@ -2738,8 +2733,7 @@ static int __btrfs_add_free_space_zoned(struct btrfs_block_group *block_group, reclaimable_unusable = block_group->zone_unusable - (block_group->length - block_group->zone_capacity);/* All the region is now unusable. Mark it as unused and reclaim */ -if (block_group->zone_unusable == block_group->length && - block_group->alloc_offset) { +if (block_group->zone_unusable == block_group->length) {btrfs_mark_bg_unused (block_group); }else if (bg_reclaim_threshold && reclaimable_unusable >=
fs/btrfs/zoned.c +3 -23
@@ -1586,19 +1586,9 @@ void btrfs_calc_zone_unusable(struct btrfs_block_group *cache)return ;WARN_ON (cache->bytes_super !=0 ); - -/* Check for block groups never get activated */ -if (test_bit (BTRFS_FS_ACTIVE_ZONE_TRACKING , &cache->fs_info->flags) && - cache->flags & (BTRFS_BLOCK_GROUP_METADATA |BTRFS_BLOCK_GROUP_SYSTEM ) && - !test_bit (BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE , &cache->runtime_flags) && - cache->alloc_offset ==0 ) { - unusable = cache->length; - free =0 ; - }else { - unusable = (cache->alloc_offset - cache->used) + - (cache->length - cache->zone_capacity); - free = cache->zone_capacity - cache->alloc_offset; - } + unusable = (cache->alloc_offset - cache->used) + + (cache->length - cache->zone_capacity); + free = cache->zone_capacity - cache->alloc_offset;/* We only need ->free_space in ALLOC_SEQ block groups */ cache->cached =BTRFS_CACHE_FINISHED ;
@@ -1964,7 +1954,6 @@ int btrfs_sync_zone_write_pointer(struct btrfs_device *tgt_dev, u64 logical,bool btrfs_zone_activate (struct btrfs_block_group *block_group) {struct btrfs_fs_info *fs_info = block_group->fs_info; -struct btrfs_space_info *space_info = block_group->space_info;struct map_lookup *map;struct btrfs_device *device;u64 physical;
@@ -1977,7 +1966,6 @@ bool btrfs_zone_activate(struct btrfs_block_group *block_group) map = block_group->physical_map; -spin_lock (&space_info->lock);spin_lock (&block_group->lock);if (test_bit (BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE , &block_group->runtime_flags)) { ret = true;
@@ -2027,14 +2015,7 @@ bool btrfs_zone_activate(struct btrfs_block_group *block_group)/* Successfully activated all the zones */ set_bit (BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE , &block_group->runtime_flags); -WARN_ON (block_group->alloc_offset !=0 ); -if (block_group->zone_unusable == block_group->length) { - block_group->zone_unusable = block_group->length - block_group->zone_capacity; - space_info->bytes_zone_unusable -= block_group->zone_capacity; - }spin_unlock (&block_group->lock); -btrfs_try_granting_tickets (fs_info, space_info); -spin_unlock (&space_info->lock);/* For the active block group list */ btrfs_get_block_group (block_group);
@@ -2047,7 +2028,6 @@ bool btrfs_zone_activate(struct btrfs_block_group *block_group) out_unlock:spin_unlock (&block_group->lock); -spin_unlock (&space_info->lock);return ret; }
Signature · 1 line
2.41.0
Re: [PATCH v3 08/10] btrfs: zoned: no longer count fresh BG region as zone unusable
Quoted 4 lines · Naohiro Aota
Now that we switched to write time activation, we no longer need to (and must not) count the fresh region as zone unusable. This commit is similar to revert commit
fc22cf8eba79("btrfs: zoned: count fresh BG region as zone unusable").
The commit id should be fa2068d7e922b434eba guessing by the subject, I don't have fc22cf8eba79.
PATCH v3 9/10 btrfs: zoned: don't activate non-DATA BG on allocation
In reply to Naohiro AotaNow that, a non-DATA block group is activated at write time. Don't activate it on allocation time.
--- fs/btrfs/block-group.c | 2 +- fs/btrfs/extent-tree.c | 8 +++++++- fs/btrfs/space-info.c | 28 ---------------------------- 3 files changed, 8 insertions(+), 30 deletions(-)
fs/btrfs/block-group.c +1 -1
@@ -4089,7 +4089,7 @@ int btrfs_chunk_alloc(struct btrfs_trans_handle *trans, u64 flags,if (IS_ERR (ret_bg)) { ret =PTR_ERR (ret_bg); - }else if (from_extent_allocation) { + }else if (from_extent_allocation && (flags &BTRFS_BLOCK_GROUP_DATA )) {/* * New block group is likely to be used soon. Try to activate * it now. Failure is OK for now.
fs/btrfs/extent-tree.c +7 -1
@@ -3690,7 +3690,9 @@ static int do_allocation_zoned(struct btrfs_block_group *block_group, }spin_unlock (&block_group->lock); -if (!ret && !btrfs_zone_activate (block_group)) { +/* Metadata block group is activated on write time. */ +if (!ret && (block_group->flags &BTRFS_BLOCK_GROUP_DATA ) && + !btrfs_zone_activate (block_group)) { ret =1 ;/* * May need to clear fs_info->{treelog,data_reloc}_bg.
@@ -3870,6 +3872,10 @@ static void found_extent(struct find_free_extent_ctl *ffe_ctl,static int can_allocate_chunk_zoned (struct btrfs_fs_info *fs_info,struct find_free_extent_ctl *ffe_ctl) { +/* Block group's activeness is not a requirement for METADATA block groups. */ +if (!(ffe_ctl->flags &BTRFS_BLOCK_GROUP_DATA )) +return 0 ; +/* If we can activate new zone, just allocate a chunk and use it */ if (btrfs_can_activate_zone (fs_info->fs_devices, ffe_ctl->flags))return 0 ;
fs/btrfs/space-info.c +0 -28
@@ -761,18 +761,6 @@ static void flush_space(struct btrfs_fs_info *fs_info,break ;case ALLOC_CHUNK :case ALLOC_CHUNK_FORCE : -/* -* For metadata space on zoned filesystem, reaching here means we -* don't have enough space left in active_total_bytes. Try to -* activate a block group first, because we may have inactive -* block group already allocated. -*/ - ret =btrfs_zoned_activate_one_bg (fs_info, space_info, false); -if (ret <0 ) -break ; -else if (ret ==1 ) -break ; - trans =btrfs_join_transaction (root);if (IS_ERR (trans)) { ret =PTR_ERR (trans);
@@ -784,22 +772,6 @@ static void flush_space(struct btrfs_fs_info *fs_info, CHUNK_ALLOC_FORCE);btrfs_end_transaction (trans); -/* -* For metadata space on zoned filesystem, allocating a new chunk -* is not enough. We still need to activate the block * group. -* Active the newly allocated block group by (maybe) finishing -* a block group. -*/ -if (ret ==1 ) { - ret =btrfs_zoned_activate_one_bg (fs_info, space_info, true); -/* -* Revert to the original ret regardless we could finish -* one block group or not. -*/ -if (ret >=0 ) - ret =1 ; - } -if (ret >0 || ret == -ENOSPC ) ret =0 ;break ;
Signature · 1 line
2.41.0
PATCH v3 10/10 btrfs: zoned: re-enable metadata over-commit for zoned mode
In reply to Naohiro AotaNow that, we can re-enable metadata over-commit. As we moved the activation from the reservation time to the write time, we no longer need to ensure all the reserved bytes is properly activated.
Without the metadata over-commit, it suffers from lower performance because it needs to flush the delalloc items more often and allocate more block groups. Re-enabling metadata over-commit will solve the issue.
--- fs/btrfs/space-info.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
fs/btrfs/space-info.c +1 -5
@@ -389,11 +389,7 @@ int btrfs_can_overcommit(struct btrfs_fs_info *fs_info,return 0 ; used =btrfs_space_info_used (space_info, true); -if (test_bit (BTRFS_FS_ACTIVE_ZONE_TRACKING , &fs_info->flags) && - (space_info->flags &BTRFS_BLOCK_GROUP_METADATA )) - avail =0 ; -else - avail =calc_available_free_space (fs_info, space_info, flush); + avail =calc_available_free_space (fs_info, space_info, flush);if (used + bytes < space_info->total_bytes + avail)return 1 ;
Signature · 1 line
2.41.0
Quoted 43 lines · Naohiro Aota
In the current implementation, block groups are activated at reservation time to ensure that all reserved bytes can be written to an active metadata block group. However, this approach has proven to be less efficient, as it activates block groups more frequently than necessary, putting pressure on the active zone resource and leading to potential issues such as early ENOSPC or
hung_task.Another drawback of the current method is that it hampers metadata over-commit, and necessitates additional flush operations and block group allocations, resulting in decreased overall performance.
Actually, we don't need so many active metadata block groups because there is only one sequential metadata write stream.
So, this series introduces a write-time activation of metadata and system block group. This involves reserving at least one active block group specifically for a metadata and system block group. When the write goes into a new block group, it should have allocated all the regions in the current active block group. So, we can wait for IOs to fill the space, and then switch to a new block group.
Switching to the write-time activation solves the above issue and will lead to better performance.
* Performance
There is a significant difference with a workload (buffered write without sync) because we re-enable metadata over-commit.
before the patch: 741.00 MB/sec after the patch: 1430.27 MB/sec (+ 93%)* Organization
Patches 1-5 are preparation patches involves
meta_write_pointercheck.Patches 6 and 7 are the main part of this series, implementing the write-time activation.
Patches 8-10 addresses code for reserve time activation: counting fresh block group as
zone_unusable, activating a block group on allocation, and disabling metadata over-commit.
Quoted 6 lines
* Changes
- v3
- Rework the reservation patch to fix the over-reservation problem
https://lore.kernel.org/all/xpb5wdmxx5wops26ihulo73oluc64dt4zpxqc7cirp2wvxl3qy@hv7lsvma5hxf/
- Rename btrfs_eb_write_context'sblock_grouptozoned_bg.
Added to misc-next, thanks. We need it in order to enable zoned tests in the CI so this goes in now, any fixups or more review tags will be done in the commits.
Quoted 34 lines · Naohiro Aota
In the current implementation, block groups are activated at reservation time to ensure that all reserved bytes can be written to an active metadata block group. However, this approach has proven to be less efficient, as it activates block groups more frequently than necessary, putting pressure on the active zone resource and leading to potential issues such as early ENOSPC or
hung_task.Another drawback of the current method is that it hampers metadata over-commit, and necessitates additional flush operations and block group allocations, resulting in decreased overall performance.
Actually, we don't need so many active metadata block groups because there is only one sequential metadata write stream.
So, this series introduces a write-time activation of metadata and system block group. This involves reserving at least one active block group specifically for a metadata and system block group. When the write goes into a new block group, it should have allocated all the regions in the current active block group. So, we can wait for IOs to fill the space, and then switch to a new block group.
Switching to the write-time activation solves the above issue and will lead to better performance.
* Performance
There is a significant difference with a workload (buffered write without sync) because we re-enable metadata over-commit.
before the patch: 741.00 MB/sec after the patch: 1430.27 MB/sec (+ 93%)* Organization
Quoted 9 lines
Patches 1-5 are preparation patches involves
meta_write_pointercheck.Patches 6 and 7 are the main part of this series, implementing the write-time activation.
Patches 8-10 addresses code for reserve time activation: counting fresh block group as
zone_unusable, activating a block group on allocation, and disabling metadata over-commit.
Hey Naohiro,
This enabled me to turn on the zoned vm for the GitHub CI, we're only failing 7 tests now, so great job!
However all the !zoned vms panic immediately
https://paste.centos.org/view/54d11384
Can you fix that up? Also you can submit a PR against the 'ci' branch of our linux repo in the btrfs GitHub project to run through the CI yourself to make sure you didn't mess anything up. Thanks,
Josef
Quoted 45 lines · Josef Bacik
Quoted 34 lines · Naohiro Aota
In the current implementation, block groups are activated at reservation time to ensure that all reserved bytes can be written to an active metadata block group. However, this approach has proven to be less efficient, as it activates block groups more frequently than necessary, putting pressure on the active zone resource and leading to potential issues such as early ENOSPC or
hung_task.Another drawback of the current method is that it hampers metadata over-commit, and necessitates additional flush operations and block group allocations, resulting in decreased overall performance.
Actually, we don't need so many active metadata block groups because there is only one sequential metadata write stream.
So, this series introduces a write-time activation of metadata and system block group. This involves reserving at least one active block group specifically for a metadata and system block group. When the write goes into a new block group, it should have allocated all the regions in the current active block group. So, we can wait for IOs to fill the space, and then switch to a new block group.
Switching to the write-time activation solves the above issue and will lead to better performance.
* Performance
There is a significant difference with a workload (buffered write without sync) because we re-enable metadata over-commit.
before the patch: 741.00 MB/sec after the patch: 1430.27 MB/sec (+ 93%)* Organization
Quoted 9 lines
Patches 1-5 are preparation patches involves
meta_write_pointercheck.Patches 6 and 7 are the main part of this series, implementing the write-time activation.
Patches 8-10 addresses code for reserve time activation: counting fresh block group as
zone_unusable, activating a block group on allocation, and disabling metadata over-commit.
Quoted 4 lines
Hey Naohiro,
This enabled me to turn on the zoned vm for the GitHub CI, we're only failing 7 tests now, so great job!
Thanks! The github CI setup is really interesting. I tried to figure out how it setup the zoned devices. Are they QEmu emulated ZNS devices?
Quoted 7 lines
However all the !zoned vms panic immediately
https://paste.centos.org/view/54d11384
Can you fix that up? Also you can submit a PR against the 'ci' branch of our linux repo in the btrfs GitHub project to run through the CI yourself to make sure you didn't mess anything up. Thanks,
I sent a candidate fix as a PR. I hope it works well.
Quoted 2 lines
Josef
Quoted 36 lines · Naohiro Aota
In the current implementation, block groups are activated at reservation time to ensure that all reserved bytes can be written to an active metadata block group. However, this approach has proven to be less efficient, as it activates block groups more frequently than necessary, putting pressure on the active zone resource and leading to potential issues such as early ENOSPC or
hung_task.Another drawback of the current method is that it hampers metadata over-commit, and necessitates additional flush operations and block group allocations, resulting in decreased overall performance.
Actually, we don't need so many active metadata block groups because there is only one sequential metadata write stream.
So, this series introduces a write-time activation of metadata and system block group. This involves reserving at least one active block group specifically for a metadata and system block group. When the write goes into a new block group, it should have allocated all the regions in the current active block group. So, we can wait for IOs to fill the space, and then switch to a new block group.
Switching to the write-time activation solves the above issue and will lead to better performance.
* Performance
There is a significant difference with a workload (buffered write without sync) because we re-enable metadata over-commit.
before the patch: 741.00 MB/sec after the patch: 1430.27 MB/sec (+ 93%)* Organization
Patches 1-5 are preparation patches involves
meta_write_pointercheck.
Quoted 8 lines
Patches 6 and 7 are the main part of this series, implementing the write-time activation.
Patches 8-10 addresses code for reserve time activation: counting fresh block group as
zone_unusable, activating a block group on allocation, and disabling metadata over-commit.* Changes
Additionally you had these failures in the CI setup
btrfs/220 btrfs/237 btrfs/239 btrfs/273 btrfs/295 generic/551 generic/574
I've excluded them so we can catch regressions, but everything except btrfs/220 seem like legitimate failures. btrfs/220 needs to be updated since zoned doesn't do discard=async, but you can do that whenever, I'm less worried about that. The rest should be investigated at some point, though not as a prerequisite for merging this series. Thanks,
Josef
Quoted 48 lines · Josef Bacik
Quoted 36 lines · Naohiro Aota
In the current implementation, block groups are activated at reservation time to ensure that all reserved bytes can be written to an active metadata block group. However, this approach has proven to be less efficient, as it activates block groups more frequently than necessary, putting pressure on the active zone resource and leading to potential issues such as early ENOSPC or
hung_task.Another drawback of the current method is that it hampers metadata over-commit, and necessitates additional flush operations and block group allocations, resulting in decreased overall performance.
Actually, we don't need so many active metadata block groups because there is only one sequential metadata write stream.
So, this series introduces a write-time activation of metadata and system block group. This involves reserving at least one active block group specifically for a metadata and system block group. When the write goes into a new block group, it should have allocated all the regions in the current active block group. So, we can wait for IOs to fill the space, and then switch to a new block group.
Switching to the write-time activation solves the above issue and will lead to better performance.
* Performance
There is a significant difference with a workload (buffered write without sync) because we re-enable metadata over-commit.
before the patch: 741.00 MB/sec after the patch: 1430.27 MB/sec (+ 93%)* Organization
Patches 1-5 are preparation patches involves
meta_write_pointercheck.Quoted 8 lines
Patches 6 and 7 are the main part of this series, implementing the write-time activation.
Patches 8-10 addresses code for reserve time activation: counting fresh block group as
zone_unusable, activating a block group on allocation, and disabling metadata over-commit.* Changes
Additionally you had these failures in the CI setup
Quoted 7 lines
btrfs/220 btrfs/237 btrfs/239 btrfs/273 btrfs/295 generic/551 generic/574
I've excluded them so we can catch regressions, but everything except btrfs/220 seem like legitimate failures. btrfs/220 needs to be updated since zoned doesn't do discard=async, but you can do that whenever, I'm less worried about that. The rest should be investigated at some point, though not as a prerequisite for merging this series. Thanks,
I checked the CI log. Yes, btrfs/220 is due to discards=async.
* known to fail
- btrfs/237: we need to tweak the test for ZNS (zone capacity != zone size)
- btrfs/239: somehow, tree-log is behaving differently on zoned mode... I have no idea why it fail. But, I think it is still a valid status...
* need to modify test?
- btrfs/295: overwriting a zoned device won't work. So, this test should be skipped.
- generic/574: not sure fsverity works with zoned mode. Need to check.
So, btrfs/273 and generic/551 are suspicious. btrfs/273 prints some WARN dmesg and generic/551 killed a AIO_TEST program... Are there details available?
Quoted 2 lines
Josef
Quoted 7 lines · Naohiro Aota
Quoted 4 lines
seem like legitimate failures. btrfs/220 needs to be updated since zoned doesn't do discard=async, but you can do that whenever, I'm less worried about that. The rest should be investigated at some point, though not as a prerequisite for merging this series. Thanks,
I checked the CI log. Yes, btrfs/220 is due to discards=async.
Quoted 7 lines
* known to fail
- btrfs/237: we need to tweak the test for ZNS (zone capacity != zone size)
- btrfs/239: somehow, tree-log is behaving differently on zoned mode... I have no idea why it fail. But, I think it is still a valid status...* need to modify test?
- generic/574: not sure fsverity works with zoned mode. Need to check.
The compatibility matrix at https://btrfs.readthedocs.io/en/latest/Status.html#zoned-mode
does not mention fsverity, so somebody has to test it and add the entry.