From: Damien Le Moal <dlemoal@kernel.org>
To: Johannes Thumshirn <johannes.thumshirn@wdc.com>,
linux-btrfs@vger.kernel.org
Cc: Christoph Hellwig <hch@lst.de>,
Hans Holmberg <Hans.Holmberg@wdc.com>,
Naohiro Aota <naohiro.aota@wdc.com>,
Josef Bacik <josef@toxicpanda.com>,
Filipe Manana <fdmanana@suse.com>,
David Sterba <dsterba@suse.com>, Boris Burkov <boris@bur.io>,
Qu Wenruo <wqu@suse.com>,
Wilfred Mallawa <wilfred.opensource@gmail.com>
Subject: Re: [PATCH 3/5] btrfs: zoned: allocate a new block-group per file
Date: Fri, 27 Nov 2026 06:20:54 +0900 [thread overview]
Message-ID: <c736ab6c-eb70-483a-ad3b-e0e3bc31da29@kernel.org> (raw)
In-Reply-To: <20261126132820.570379-4-johannes.thumshirn@wdc.com>
On 11/26/26 22:28, Johannes Thumshirn wrote:
> On zoned filesystems all data writers share the first active block-group
> with free space, so extents of parallel writers interleave. This is
> causing high fragmentation in the filesystem, that also potentially
> increases the need for garbage collection.
>
> If the allocation hint doesn't hit an active block-group with enough
> space, e.g. for a new file or after its block-group filled up, and an
> active zone can be spared, allocate from an empty block-group instead.
> If no zone can be spared, spread allocations round-robin over the active
> block-groups. Never finish a zone for this.
>
> On an expample null_blk device with 64M zones, max 12 active zones this
> results in:
>
> Test files/zone extents/file
> 5 parallel O_DIRECT writers 5.0 -> 1.09 966 -> 74
> 4 files appended round-robin 4.0 -> 1.0 192 -> 3
>
> Buffered parallel writers improve less (4.45 -> 3.09 files per zone):
> block-groups with a leftover smaller than the next allocation keep their
> active zone.
>
> Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
> ---
> fs/btrfs/extent-tree.c | 36 ++++++++++++++++++++++++++++++------
> fs/btrfs/extent-tree.h | 3 +++
> fs/btrfs/zoned.c | 31 +++++++++++++++++++++++++++++++
> fs/btrfs/zoned.h | 7 +++++++
> include/trace/events/btrfs.h | 12 ++++++++----
> 5 files changed, 79 insertions(+), 10 deletions(-)
>
> diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
> index 0541b38129e5..7169f296a523 100644
> --- a/fs/btrfs/extent-tree.c
> +++ b/fs/btrfs/extent-tree.c
> @@ -3766,6 +3766,11 @@ enum btrfs_loop_type {
> LOOP_NO_EMPTY_SIZE,
> };
>
> +static inline bool want_fresh(const struct find_free_extent_ctl *ffe_ctl)
This is a really bad name... I want fresh beer, but I am guessing that is not
what this is reporting :)
> +{
> + return ffe_ctl->want_fresh && ffe_ctl->loop <= LOOP_ALLOC_CHUNK;
> +}
> +
> static inline void
> btrfs_lock_block_group(struct btrfs_block_group *cache, bool delalloc)
> {
> @@ -4068,7 +4073,8 @@ static int do_allocation_zoned(struct btrfs_block_group *block_group,
>
> /* Check RO and no space case before trying to activate it */
> spin_lock(&block_group->lock);
> - if (block_group->ro || btrfs_zoned_bg_is_full(block_group)) {
> + if (block_group->ro || btrfs_zoned_bg_is_full(block_group) ||
> + (want_fresh(ffe_ctl) && block_group->alloc_offset)) {
> ret = 1;
> /*
> * May need to clear fs_info->{treelog,data_reloc}_bg.
> @@ -4102,6 +4108,7 @@ static int do_allocation_zoned(struct btrfs_block_group *block_group,
> fs_info->data_reloc_bg == 0);
>
> if (block_group->ro ||
> + (want_fresh(ffe_ctl) && block_group->alloc_offset) ||
> (!ffe_ctl->for_data_reloc &&
> test_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC, &block_group->runtime_flags))) {
> ret = 1;
> @@ -4259,6 +4266,13 @@ static int can_allocate_chunk_zoned(struct btrfs_fs_info *fs_info,
> if (!(ffe_ctl->flags & BTRFS_BLOCK_GROUP_DATA))
> return 0;
>
> + if (ffe_ctl->want_fresh) {
> + if (btrfs_can_spare_active_zone(fs_info, ffe_ctl->flags))
> + return 0;
> + else
> + return 1;
return !btrfs_can_spare_active_zone(fs_info, ffe_ctl->flags);
?
At the very least, drop the else please.
Also, how is btrfs_can_spare_active_zone() and btrfs_can_activate_zone() differ?
The function names kind of suggest they are doing the same thing. So cannot we
get just one function being defined to test if we can activate a zone that is
not already active, that is, a free empty zone?
Otherwise, this makes everything a lot more complicated here and hard to follow.
> + }
> +
> /* 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;
> @@ -4530,13 +4544,22 @@ static int prepare_allocation_zoned(struct btrfs_fs_info *fs_info,
> }
>
> if (list_entry_is_head(block_group, &fs_info->zone_active_bgs,
> - active_bg_list))
> - block_group = fallback;
> + active_bg_list)) {
> + if (btrfs_can_spare_active_zone(fs_info, ffe_ctl->flags)) {
> + ffe_ctl->want_fresh = true;
> + block_group = NULL;
> + } else {
> + block_group = fallback;
> + if (block_group)
> + list_move_tail(&block_group->active_bg_list,
> + &fs_info->zone_active_bgs);
> + }
> + }
>
> - if (block_group)
> - ffe_ctl->hint_byte = block_group->start;
> + ffe_ctl->hint_byte = block_group ? block_group->start : 0;
>
> - trace_prepare_allocation_zoned(fs_info, hint, block_group);
> + trace_prepare_allocation_zoned(fs_info, hint, block_group,
> + ffe_ctl->want_fresh);
> spin_unlock(&fs_info->zone_active_bgs_lock);
>
> return 0;
> @@ -4609,6 +4632,7 @@ static noinline int find_free_extent(struct btrfs_root *root,
> ffe_ctl->cached = 0;
> ffe_ctl->max_extent_size = 0;
> ffe_ctl->total_free_space = 0;
> + ffe_ctl->want_fresh = false;
> ffe_ctl->found_offset = 0;
> ffe_ctl->policy = BTRFS_EXTENT_ALLOC_CLUSTERED;
> ffe_ctl->size_class = btrfs_calc_block_group_size_class(ffe_ctl->num_bytes);
> diff --git a/fs/btrfs/extent-tree.h b/fs/btrfs/extent-tree.h
> index ff330d4896d6..50e0a16d2d8d 100644
> --- a/fs/btrfs/extent-tree.h
> +++ b/fs/btrfs/extent-tree.h
> @@ -87,6 +87,9 @@ struct find_free_extent_ctl {
>
> /* Size class of block groups to prefer in early loops */
> enum btrfs_block_group_size_class size_class;
> +
> + /* Allocate a fresh block-group */
> + bool want_fresh;
> };
>
> enum btrfs_inline_ref_type {
> diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
> index 216ca214d26a..4670db6a4fc8 100644
> --- a/fs/btrfs/zoned.c
> +++ b/fs/btrfs/zoned.c
> @@ -67,6 +67,8 @@
>
> #define SUPER_INFO_SECTORS ((u64)BTRFS_SUPER_INFO_SIZE >> SECTOR_SHIFT)
>
> +#define BTRFS_ZONED_GC_SPARE_ZONES 1 /* XXX: This needs a bump when zgc-final is merged */
> +
> 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);
>
> @@ -2721,6 +2723,35 @@ void btrfs_zone_finish_relocated(struct btrfs_block_group *block_group)
> block_group->start, ret);
> }
>
> +bool btrfs_can_spare_active_zone(struct btrfs_fs_info *fs_info, u64 flags)
> +{
> + struct btrfs_device *device;
> + const int needed = (flags & BTRFS_BLOCK_GROUP_DUP) ? 2 : 1;
> + bool ret = false;
> +
> + if (test_bit(BTRFS_FS_NEED_ZONE_FINISH, &fs_info->flags))
> + return false;
> +
> + rcu_read_lock();
> + list_for_each_entry_rcu(device, &fs_info->fs_devices->devices, dev_list) {
> + struct btrfs_zoned_device_info *zinfo = device->zone_info;
> +
> + if (!device->bdev || !zinfo || !zinfo->max_active_zones ||
> + !test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
> + continue;
> +
> + if (atomic_read(&zinfo->active_zones_left) >=
> + READ_ONCE(zinfo->reserved_active_zones) + needed +
> + BTRFS_ZONED_GC_SPARE_ZONES) {
> + ret = true;
> + break;
> + }
> + }
> + rcu_read_unlock();
> +
> + return ret;
> +}
> +
> bool btrfs_can_activate_zone(struct btrfs_fs_devices *fs_devices, u64 flags)
> {
> struct btrfs_fs_info *fs_info = fs_devices->fs_info;
> diff --git a/fs/btrfs/zoned.h b/fs/btrfs/zoned.h
> index 2225090839f3..2c17c31437ca 100644
> --- a/fs/btrfs/zoned.h
> +++ b/fs/btrfs/zoned.h
> @@ -103,6 +103,7 @@ int btrfs_zoned_activate_one_bg(struct btrfs_space_info *space_info, bool do_fin
> void btrfs_check_active_zone_reservation(struct btrfs_fs_info *fs_info);
> int btrfs_reset_unused_block_groups(struct btrfs_space_info *space_info, u64 num_bytes);
> void btrfs_show_zoned_stats(struct btrfs_fs_info *fs_info, struct seq_file *seq);
> +bool btrfs_can_spare_active_zone(struct btrfs_fs_info *fs_info, u64 flags);
>
> #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
> struct zone_info;
> @@ -299,6 +300,12 @@ static inline int btrfs_show_zoned_stats(struct btrfs_fs_info *fs_info, struct s
> return 0;
> }
>
> +static inline bool btrfs_can_spare_active_zone(struct btrfs_fs_info *fs_info,
> + u64 flags)
> +{
> + return false;
> +}
> +
> #endif
>
> static inline bool btrfs_dev_is_sequential(struct btrfs_device *device, u64 pos)
> diff --git a/include/trace/events/btrfs.h b/include/trace/events/btrfs.h
> index 1378e697a46f..830927b185a4 100644
> --- a/include/trace/events/btrfs.h
> +++ b/include/trace/events/btrfs.h
> @@ -2244,15 +2244,17 @@ TRACE_EVENT(btrfs_find_free_extent_have_block_group,
> TRACE_EVENT(prepare_allocation_zoned,
>
> TP_PROTO(const struct btrfs_fs_info *fs_info,
> - u64 hint_byte, const struct btrfs_block_group *block_group),
> + u64 hint_byte, const struct btrfs_block_group *block_group,
> + bool want_fresh),
>
> - TP_ARGS(fs_info, hint_byte, block_group),
> + TP_ARGS(fs_info, hint_byte, block_group, want_fresh),
>
> TP_STRUCT__entry_btrfs(
> __field( u64, hint_byte )
> __field( u64, bg_start )
> __field( u64, bg_flags )
> __field( bool, hit )
> + __field( bool, want_fresh )
> ),
>
> TP_fast_assign_btrfs(fs_info,
> @@ -2262,12 +2264,14 @@ TRACE_EVENT(prepare_allocation_zoned,
> __entry->hit = block_group && in_range(hint_byte,
> block_group->start,
> block_group->length);
> + __entry->want_fresh = want_fresh;
> ),
>
> - TP_printk_btrfs("hint=%llu block_group=%llu bg_flags=%llu(%s) hit=%d",
> + TP_printk_btrfs("hint=%llu block_group=%llu bg_flags=%llu(%s) hit=%d want_fresh=%d",
> __entry->hint_byte, __entry->bg_start, __entry->bg_flags,
> __print_flags((unsigned long)__entry->bg_flags, "|",
> - BTRFS_GROUP_FLAGS), __entry->hit)
> + BTRFS_GROUP_FLAGS), __entry->hit,
> + __entry->want_fresh)
> );
>
> DECLARE_EVENT_CLASS(btrfs__reserve_extent,
--
Damien Le Moal
Western Digital Research
next prev parent reply other threads:[~2026-11-26 21:20 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-26 13:28 [PATCH 0/5] btrfs: rework data placement on zoned devices Johannes Thumshirn
2026-11-26 13:28 ` [PATCH 1/5] btrfs: zoned: search for hinted block-group in prepare_allocation_zoned Johannes Thumshirn
2026-11-26 21:09 ` Damien Le Moal
2026-11-27 9:57 ` Johannes Thumshirn
2026-11-26 13:28 ` [PATCH 2/5] btrfs: zoned: save last allocation as placement hint Johannes Thumshirn
2026-11-26 21:11 ` Damien Le Moal
2026-11-26 13:28 ` [PATCH 3/5] btrfs: zoned: allocate a new block-group per file Johannes Thumshirn
2026-11-26 21:20 ` Damien Le Moal [this message]
2026-11-26 13:28 ` [PATCH 4/5] btrfs: zoned: try to fill hinted block-groups Johannes Thumshirn
2026-11-26 21:25 ` Damien Le Moal
2026-11-27 10:15 ` Johannes Thumshirn
2026-11-26 13:28 ` [PATCH 5/5] btrfs: zoned: break concurrent chunk allocation race Johannes Thumshirn
2026-11-26 21:30 ` Damien Le Moal
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=c736ab6c-eb70-483a-ad3b-e0e3bc31da29@kernel.org \
--to=dlemoal@kernel.org \
--cc=Hans.Holmberg@wdc.com \
--cc=boris@bur.io \
--cc=dsterba@suse.com \
--cc=fdmanana@suse.com \
--cc=hch@lst.de \
--cc=johannes.thumshirn@wdc.com \
--cc=josef@toxicpanda.com \
--cc=linux-btrfs@vger.kernel.org \
--cc=naohiro.aota@wdc.com \
--cc=wilfred.opensource@gmail.com \
--cc=wqu@suse.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.