Linux Btrfs filesystem development
 help / color / mirror / Atom feed
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 1/5] btrfs: zoned: search for hinted block-group in prepare_allocation_zoned
Date: Fri, 27 Nov 2026 06:09:58 +0900	[thread overview]
Message-ID: <a70e0150-abae-4545-95bd-e9bf7f594458@kernel.org> (raw)
In-Reply-To: <20261126132820.570379-2-johannes.thumshirn@wdc.com>

On 11/26/26 22:28, Johannes Thumshirn wrote:
> When searching for a block-group to place to allocate data extents from,
> honor the hint passed in if the hint falls within the range of an
> already active block-group.
> 
> If the hint doesn't fall into an active block-group or the hinted
> block-group does not have enough available space left, fall back to an
> avtive block-group that has enough space left for the allocation.
> 
> Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
> ---
>  fs/btrfs/extent-tree.c       | 41 ++++++++++++++++++++++++++++++------
>  include/trace/events/btrfs.h | 29 +++++++++++++++++++++++++
>  2 files changed, 64 insertions(+), 6 deletions(-)
> 
> diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
> index d59289ce3294..0541b38129e5 100644
> --- a/fs/btrfs/extent-tree.c
> +++ b/fs/btrfs/extent-tree.c
> @@ -4472,7 +4472,8 @@ static int prepare_allocation_zoned(struct btrfs_fs_info *fs_info,
>  				    struct find_free_extent_ctl *ffe_ctl,
>  				    struct btrfs_space_info *space_info)
>  {
> -	struct btrfs_block_group *block_group;
> +	struct btrfs_block_group *block_group, *fallback = NULL;
> +	u64 hint = ffe_ctl->hint_byte;
>  
>  	if (ffe_ctl->for_treelog) {
>  		spin_lock(&fs_info->treelog_bg_lock);
> @@ -4501,13 +4502,41 @@ static int prepare_allocation_zoned(struct btrfs_fs_info *fs_info,
>  		 */
>  		u64 avail = block_group->zone_capacity - block_group->alloc_offset;
>  
> -		if (block_group_bits(block_group, ffe_ctl->flags) &&
> -		    block_group->space_info == space_info &&
> -		    avail >= ffe_ctl->num_bytes) {
> -			ffe_ctl->hint_byte = block_group->start;
> +		if (!block_group_bits(block_group, ffe_ctl->flags))
> +			continue;
> +
> +		if (block_group->space_info != space_info)
> +			continue;
> +
> +		if (test_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC,
> +					&block_group->runtime_flags))
> +			continue;
> +
> +		if (avail < ffe_ctl->num_bytes)
> +			continue;
> +
> +		/*
> +		 * Remember the first block-group that fits the allocation. Then
> +		 * check if the hint in ffe_ctl::hint_byte is in the active_bgs
> +		 * list and has enough space available. If yes pick it. If not,
> +		 * use the fallback block-group.
> +		 */
> +		if (!fallback)
> +			fallback = block_group;
> +
> +		if (in_range(ffe_ctl->hint_byte, block_group->start,
> +			     block_group->length))
>  			break;
> -		}
>  	}
> +
> +	if (list_entry_is_head(block_group, &fs_info->zone_active_bgs,
> +				active_bg_list))
> +		block_group = fallback;

It took me a while to understand what the test is... To make this easier to
understand, why not use a "bg" variable for the loop, and set block_group to bg
if the in_range() test is true? You can then simply have:

	if (!block_group)
		block_group = fallback;

That would be a lot easier to understand and nicer to read :)

Other than this, this looks all good to me.

> +
> +	if (block_group)
> +		ffe_ctl->hint_byte = block_group->start;
> +
> +	trace_prepare_allocation_zoned(fs_info, hint, block_group);
>  	spin_unlock(&fs_info->zone_active_bgs_lock);
>  
>  	return 0;
> diff --git a/include/trace/events/btrfs.h b/include/trace/events/btrfs.h
> index dd862d1f2a2c..1378e697a46f 100644
> --- a/include/trace/events/btrfs.h
> +++ b/include/trace/events/btrfs.h
> @@ -2241,6 +2241,35 @@ TRACE_EVENT(btrfs_find_free_extent_have_block_group,
>  				 BTRFS_GROUP_FLAGS))
>  );
>  
> +TRACE_EVENT(prepare_allocation_zoned,
> +
> +	TP_PROTO(const struct btrfs_fs_info *fs_info,
> +		 u64 hint_byte, const struct btrfs_block_group *block_group),
> +
> +	TP_ARGS(fs_info, hint_byte, block_group),
> +
> +	TP_STRUCT__entry_btrfs(
> +		__field(	u64,	hint_byte		)
> +		__field(	u64,	bg_start		)
> +		__field(	u64,	bg_flags		)
> +		__field(	bool,	hit			)
> +	),
> +
> +	TP_fast_assign_btrfs(fs_info,
> +		__entry->hint_byte	= hint_byte;
> +		__entry->bg_start	= block_group ? block_group->start : 0;
> +		__entry->bg_flags	= block_group ? block_group->flags : 0;
> +		__entry->hit		= block_group && in_range(hint_byte,
> +						block_group->start,
> +						block_group->length);
> +	),
> +
> +	TP_printk_btrfs("hint=%llu block_group=%llu bg_flags=%llu(%s) hit=%d",
> +			__entry->hint_byte, __entry->bg_start, __entry->bg_flags,
> +			__print_flags((unsigned long)__entry->bg_flags, "|",
> +				BTRFS_GROUP_FLAGS), __entry->hit)
> +);
> +
>  DECLARE_EVENT_CLASS(btrfs__reserve_extent,
>  
>  	TP_PROTO(const struct btrfs_block_group *block_group,


-- 
Damien Le Moal
Western Digital Research

  reply	other threads:[~2026-11-26 21:10 UTC|newest]

Thread overview: 9+ 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 [this message]
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
2026-11-26 13:28 ` [PATCH 4/5] btrfs: zoned: try to fill hinted block-groups Johannes Thumshirn
2026-11-26 13:28 ` [PATCH 5/5] btrfs: zoned: break concurrent chunk allocation race Johannes Thumshirn

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=a70e0150-abae-4545-95bd-e9bf7f594458@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox