Linux Btrfs filesystem development
 help / color / mirror / Atom feed
* [PATCH 0/5] btrfs: rework data placement on zoned devices
@ 2026-11-26 13:28 Johannes Thumshirn
  2026-11-26 13:28 ` [PATCH 1/5] btrfs: zoned: search for hinted block-group in prepare_allocation_zoned Johannes Thumshirn
                   ` (4 more replies)
  0 siblings, 5 replies; 11+ messages in thread
From: Johannes Thumshirn @ 2026-11-26 13:28 UTC (permalink / raw)
  To: linux-btrfs
  Cc: Damien Le Moal, Christoph Hellwig, Hans Holmberg, Naohiro Aota,
	Josef Bacik, Filipe Manana, David Sterba, Boris Burkov, Qu Wenruo,
	Wilfred Mallawa, Johannes Thumshirn

Rework data placement on zoned devices. Currently zoned BTRFS ignores
the write hint passed to the extent allocator and always picks the first
block-group in the active_bgs list as extent allocation target.

This causes high fragmentation, which then in turn causes high demands
for garbage collection due to the append only nature of zoned block
devices. As the fragmentation is high garbage collection cannot reset
a full zone when one hughe file is overwritten, but has to evacuate data
that is still referenced by other files first, before performing a zone
reset.

Mimic what XFS on zoned realtime devices does for this setup: As long as
we can still allocate new zones from the underlying devices, allocate a
new block-group and place, the new file there. For extent-allocations of
already existing files, remember the last allocation of this inode and
use it as a hint for the extent allocator. If two allocations race for
the same block-group, retry to see if another block-group can be chosen.

Test setup
----------

QEMU VM (virtme-ng), 8 vCPUs, 8 GiB RAM, base btrfs for-next 98768e7ccd0f
with and without the series. Each test runs 3 times per kernel on a
freshly created filesystem (mkfs.btrfs -d single -m single) on a memory
backed zoned null_blk device:

  memory_backed=1 blocksize=4096 size=4096 (MiB) zoned=1 zone_size=64 (MiB)
  zone_nr_conv=0 zone_max_active=12

btrfs uses 10 of the 12 active zones for data. Writes are done with
xfs_io pwrite in 1 MiB blocks, buffered writes are followed by fsync.

Workloads:

- Interleaved appends: 4 files are appended round-robin in 1 MiB steps,
  each step followed by fsync, until every file is 192 MiB (3 zones).
- Parallel writers: 5 writers start at the same time and each write a
  new 128 MiB file (2 zones).
- More writers than active zones: 20 writers start at the same time and
  each write a new 64 MiB file (1 zone).
- Fill to ENOSPC: 8 O_DIRECT writers write until the filesystem is full.
- GC churn: 5 writers create 6 files each (16-96 MiB, 1.5 GiB total).
  Then, in 8 rounds, every writer picks 2 of its files and either deletes
  and re-creates it with a new size, overwrites it completely, or
  overwrites 25% of it in random 1 MiB pieces. The sequence of operations
  is generated from a fixed seed, so both kernels run the same sequence
  (about 5 GiB of writes in total). There is one sync per round instead
  of fsync per write. GC is the regular zoned reclaim with default
  thresholds; the counters are read after reclaim has settled.

Metrics:

- files/zone: number of files with data in a data block-group, averaged
  over all data block-groups used (1.0 = no sharing).
- extents/file: extents per file as reported by FIEMAP.
- GC copied: bytes relocated by zoned reclaim
  (/sys/fs/btrfs/<uuid>/allocation/data/reclaim_bytes).
- zones freed without copying: data block-groups removed without being
  relocated first.

Results (mean of 3 runs)
------------------------

Workload                              files/zone      extents/file
                                      base -> series  base -> series
------------------------------------  --------------  ----------------
Interleaved appends
  buffered                             4.00 -> 1.00     192 -> 3
  O_DIRECT                             4.00 -> 1.00     192 -> 3
5 parallel writers
  buffered                             4.42 -> 1.48      41 -> 6
  O_DIRECT                             5.00 -> 1.00     964 -> 2
20 parallel writers (2x active zones)
  buffered                            16.98 -> 4.47      53 -> 44
  O_DIRECT                            19.60 -> 5.25     503 -> 244
Fill to ENOSPC, O_DIRECT               8.00 -> 2.35    3920 -> 1261

                                      GC copied        zones freed
                                      base -> series   w/o copying
------------------------------------  ---------------  --------------
GC churn, buffered                    261 -> 4 MiB     17 -> 36
GC churn, O_DIRECT                     70 -> 0 MiB     16 -> 35

Further numbers:

- 5 parallel O_DIRECT writers: every file has its own zones in all runs
  (at most 1 file per zone, base: 5).
- 20 parallel writers: above the active zone limit allocations are spread
  round-robin over the active block-groups; at most 6-10 files per zone
  instead of 20.
- Fill to ENOSPC: the filesystem fills to 92% of the device size with and
  without the series.
- GC churn per run: base copies 45-677 MiB (buffered) and 49-100 MiB
  (O_DIRECT), the series 0-12 MiB and 0 MiB. GC write amplification
  ((written + copied) / written) goes from 1.008-1.129 to 1.000-1.002.
- The series doesn't use more data block-groups for the streaming
  workloads; with more writers than active zones it uses 1-4 more
  (24-25 instead of 21-25).

Performance changes (all numbers in MB/s):

Workload                  write                parallel read          single reader
------------------------  -------------------  ---------------------  --------------------
 5 writers, O_DIRECT      1660 -> 1981 (+19%)  10335 -> 11996 (+16%)  4077 -> 5691 (+40%)
20 writers, O_DIRECT      2346 -> 2928 (+25%)  12584 -> 12920  (+3%)  4561 -> 5283 (+16%)
 5 writers, buffered       643 ->  628  (-2%)  13165 -> 13091  (-1%)  5353 -> 5168  (-3%)
20 writers, buffered       842 ->  831  (-1%)  14507 -> 14620  (+1%)  6240 -> 6370  (+2%)


Johannes Thumshirn (5):
  btrfs: zoned: search for hinted block-group in
    prepare_allocation_zoned
  btrfs: zoned: save last allocation as placement hint
  btrfs: zoned: allocate a new block-group per file
  btrfs: zoned: try to fill hinted block-groups
  btrfs: zoned: break concurrent chunk allocation race

 fs/btrfs/btrfs_inode.h       |  2 +
 fs/btrfs/extent-tree.c       | 85 +++++++++++++++++++++++++++++++++---
 fs/btrfs/extent-tree.h       |  8 ++++
 fs/btrfs/inode.c             |  5 +++
 fs/btrfs/ordered-data.c      |  7 ++-
 fs/btrfs/zoned.c             | 31 +++++++++++++
 fs/btrfs/zoned.h             |  7 +++
 include/trace/events/btrfs.h | 33 ++++++++++++++
 8 files changed, 170 insertions(+), 8 deletions(-)

-- 
2.56.1


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 1/5] btrfs: zoned: search for hinted block-group in prepare_allocation_zoned
  2026-11-26 13:28 [PATCH 0/5] btrfs: rework data placement on zoned devices Johannes Thumshirn
@ 2026-11-26 13:28 ` Johannes Thumshirn
  2026-11-26 21:09   ` Damien Le Moal
  2026-11-26 13:28 ` [PATCH 2/5] btrfs: zoned: save last allocation as placement hint Johannes Thumshirn
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 11+ messages in thread
From: Johannes Thumshirn @ 2026-11-26 13:28 UTC (permalink / raw)
  To: linux-btrfs
  Cc: Damien Le Moal, Christoph Hellwig, Hans Holmberg, Naohiro Aota,
	Josef Bacik, Filipe Manana, David Sterba, Boris Burkov, Qu Wenruo,
	Wilfred Mallawa, Johannes Thumshirn

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;
+
+	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,
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH 2/5] btrfs: zoned: save last allocation as placement hint
  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 13:28 ` 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
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 11+ messages in thread
From: Johannes Thumshirn @ 2026-11-26 13:28 UTC (permalink / raw)
  To: linux-btrfs
  Cc: Damien Le Moal, Christoph Hellwig, Hans Holmberg, Naohiro Aota,
	Josef Bacik, Filipe Manana, David Sterba, Boris Burkov, Qu Wenruo,
	Wilfred Mallawa, Johannes Thumshirn

Save the last allocation of a data extent in the inode and if set use it
as a placement hint for a subsequent extent allocation in the extent
allocator.

Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
---
 fs/btrfs/btrfs_inode.h  | 2 ++
 fs/btrfs/inode.c        | 5 +++++
 fs/btrfs/ordered-data.c | 7 +++++--
 3 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/fs/btrfs/btrfs_inode.h b/fs/btrfs/btrfs_inode.h
index 357a797c4f6c..34baefc826c2 100644
--- a/fs/btrfs/btrfs_inode.h
+++ b/fs/btrfs/btrfs_inode.h
@@ -334,6 +334,8 @@ struct btrfs_inode {
 	struct rw_semaphore i_mmap_lock;
 
 	struct inode vfs_inode;
+
+	u64 last_alloc;
 };
 
 static inline u64 btrfs_get_first_dir_index_to_log(const struct btrfs_inode *inode)
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 40dd87276cc9..402bbba0b11a 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -1142,6 +1142,10 @@ u64 btrfs_get_extent_allocation_hint(struct btrfs_inode *inode, u64 start,
 	struct extent_map_tree *em_tree = &inode->extent_tree;
 	struct extent_map *em;
 	u64 alloc_hint = 0;
+	u64 last_alloc = READ_ONCE(inode->last_alloc);
+
+	if (btrfs_is_zoned(inode->root->fs_info) && last_alloc)
+		return last_alloc;
 
 	read_lock(&em_tree->lock);
 	em = btrfs_search_extent_mapping(em_tree, start, num_bytes);
@@ -8038,6 +8042,7 @@ struct inode *btrfs_alloc_inode(struct super_block *sb)
 	ei->new_delalloc_bytes = 0;
 	ei->defrag_bytes = 0;
 	ei->disk_i_size = 0;
+	ei->last_alloc = 0;
 	ei->flags = 0;
 	ei->ro_flags = 0;
 	/*
diff --git a/fs/btrfs/ordered-data.c b/fs/btrfs/ordered-data.c
index df74c75d6c29..3b0925a13660 100644
--- a/fs/btrfs/ordered-data.c
+++ b/fs/btrfs/ordered-data.c
@@ -317,14 +317,14 @@ struct btrfs_ordered_extent *btrfs_alloc_ordered_extent(
 	 * So here we always pass 0 as offset for NOCOW/PREALLOC ordered extents,
 	 * or btrfs_split_ordered_extent() cannot handle it correctly.
 	 */
-	if (flags & ((1U << BTRFS_ORDERED_NOCOW) | (1U << BTRFS_ORDERED_PREALLOC)))
+	if (flags & ((1U << BTRFS_ORDERED_NOCOW) | (1U << BTRFS_ORDERED_PREALLOC))) {
 		entry = alloc_ordered_extent(inode, file_offset,
 					     file_extent->num_bytes,
 					     file_extent->num_bytes,
 					     file_extent->disk_bytenr + file_extent->offset,
 					     file_extent->num_bytes, 0, flags,
 					     file_extent->compression);
-	else
+	} else {
 		entry = alloc_ordered_extent(inode, file_offset,
 					     file_extent->num_bytes,
 					     file_extent->ram_bytes,
@@ -332,6 +332,9 @@ struct btrfs_ordered_extent *btrfs_alloc_ordered_extent(
 					     file_extent->disk_num_bytes,
 					     file_extent->offset, flags,
 					     file_extent->compression);
+		WRITE_ONCE(inode->last_alloc,
+			file_extent->disk_bytenr + file_extent->disk_num_bytes - 1);
+	}
 	if (!IS_ERR(entry))
 		insert_ordered_extent(entry);
 	return entry;
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH 3/5] btrfs: zoned: allocate a new block-group per file
  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 13:28 ` [PATCH 2/5] btrfs: zoned: save last allocation as placement hint Johannes Thumshirn
@ 2026-11-26 13:28 ` 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
  4 siblings, 1 reply; 11+ messages in thread
From: Johannes Thumshirn @ 2026-11-26 13:28 UTC (permalink / raw)
  To: linux-btrfs
  Cc: Damien Le Moal, Christoph Hellwig, Hans Holmberg, Naohiro Aota,
	Josef Bacik, Filipe Manana, David Sterba, Boris Burkov, Qu Wenruo,
	Wilfred Mallawa, Johannes Thumshirn

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)
+{
+	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;
+	}
+
 	/* 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,
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH 4/5] btrfs: zoned: try to fill hinted block-groups
  2026-11-26 13:28 [PATCH 0/5] btrfs: rework data placement on zoned devices Johannes Thumshirn
                   ` (2 preceding siblings ...)
  2026-11-26 13:28 ` [PATCH 3/5] btrfs: zoned: allocate a new block-group per file Johannes Thumshirn
@ 2026-11-26 13:28 ` Johannes Thumshirn
  2026-11-26 21:25   ` Damien Le Moal
  2026-11-26 13:28 ` [PATCH 5/5] btrfs: zoned: break concurrent chunk allocation race Johannes Thumshirn
  4 siblings, 1 reply; 11+ messages in thread
From: Johannes Thumshirn @ 2026-11-26 13:28 UTC (permalink / raw)
  To: linux-btrfs
  Cc: Damien Le Moal, Christoph Hellwig, Hans Holmberg, Naohiro Aota,
	Josef Bacik, Filipe Manana, David Sterba, Boris Burkov, Qu Wenruo,
	Wilfred Mallawa, Johannes Thumshirn

If the hinted block-group has less space left than the allocation, the
allocation goes to a new block-group and the old one stays active with
its leftover. With buffered writes this happens for nearly every
block-group, the leftovers use up the active zones and new files fall
back to round-robin.

Shrink the allocation to the space left in the hinted block-group
instead, as long as it is not smaller than min_alloc_size. The
block-group becomes full, gets finished and releases its active zone.

5 parallel buffered writers on null_blk, 64M zones, max 12 active zones:
files per zone 3.09 -> 1.46, extents per file 29.6 -> 5.8.

Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
---
 fs/btrfs/extent-tree.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index 7169f296a523..d8dc43655b16 100644
--- a/fs/btrfs/extent-tree.c
+++ b/fs/btrfs/extent-tree.c
@@ -4526,8 +4526,23 @@ static int prepare_allocation_zoned(struct btrfs_fs_info *fs_info,
 					&block_group->runtime_flags))
 			continue;
 
-		if (avail < ffe_ctl->num_bytes)
+		/*
+		 * If we have less space available in the block-group but can
+		 * fit the allocation into the hinted block-group if we shrink
+		 * it, fill up the block-group.
+		 */
+		if (avail < ffe_ctl->num_bytes) {
+			avail = round_down(avail, fs_info->sectorsize);
+			if (avail >= ffe_ctl->min_alloc_size &&
+			    in_range(ffe_ctl->hint_byte, block_group->start,
+				     block_group->length)) {
+				if (ffe_ctl->ram_bytes == ffe_ctl->num_bytes)
+					ffe_ctl->ram_bytes = avail;
+				ffe_ctl->num_bytes = avail;
+				break;
+			}
 			continue;
+		}
 
 		/*
 		 * Remember the first block-group that fits the allocation. Then
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH 5/5] btrfs: zoned: break concurrent chunk allocation race
  2026-11-26 13:28 [PATCH 0/5] btrfs: rework data placement on zoned devices Johannes Thumshirn
                   ` (3 preceding siblings ...)
  2026-11-26 13:28 ` [PATCH 4/5] btrfs: zoned: try to fill hinted block-groups Johannes Thumshirn
@ 2026-11-26 13:28 ` Johannes Thumshirn
  2026-11-26 21:30   ` Damien Le Moal
  4 siblings, 1 reply; 11+ messages in thread
From: Johannes Thumshirn @ 2026-11-26 13:28 UTC (permalink / raw)
  To: linux-btrfs
  Cc: Damien Le Moal, Christoph Hellwig, Hans Holmberg, Naohiro Aota,
	Josef Bacik, Filipe Manana, David Sterba, Boris Burkov, Qu Wenruo,
	Wilfred Mallawa, Johannes Thumshirn

btrfs_chunk_alloc() coalesces concurrent allocations, so a fresh
block-group request can end up without a chunk of its own when several
new files race.

Retry the chunk allocation a few times before giving up on a fresh
block-group.

Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
---
 fs/btrfs/extent-tree.c | 5 +++++
 fs/btrfs/extent-tree.h | 5 +++++
 2 files changed, 10 insertions(+)

diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index d8dc43655b16..d4f809af75db 100644
--- a/fs/btrfs/extent-tree.c
+++ b/fs/btrfs/extent-tree.c
@@ -4367,6 +4367,10 @@ static int find_free_extent_update_loop(struct btrfs_fs_info *fs_info,
 		return -ENOSPC;
 
 	ffe_ctl->index = 0;
+
+	if (ffe_ctl->loop == LOOP_ALLOC_CHUNK && ffe_ctl->want_fresh &&
+	    ffe_ctl->fresh_retries++ < BTRFS_ZONED_FRESH_RETRIES)
+		ffe_ctl->loop--;
 	/*
 	 * We want to skip the LOOP_CACHING_WAIT step if we don't have any
 	 * uncached bgs and we've already done a full search through.
@@ -4648,6 +4652,7 @@ static noinline int find_free_extent(struct btrfs_root *root,
 	ffe_ctl->max_extent_size = 0;
 	ffe_ctl->total_free_space = 0;
 	ffe_ctl->want_fresh = false;
+	ffe_ctl->fresh_retries = 0;
 	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 50e0a16d2d8d..c5d5fd5c0196 100644
--- a/fs/btrfs/extent-tree.h
+++ b/fs/btrfs/extent-tree.h
@@ -90,8 +90,13 @@ struct find_free_extent_ctl {
 
 	/* Allocate a fresh block-group */
 	bool want_fresh;
+
+	/* Retry counter for allocating fresh block-groups */
+	u8 fresh_retries;
 };
 
+#define BTRFS_ZONED_FRESH_RETRIES	3
+
 enum btrfs_inline_ref_type {
 	BTRFS_REF_TYPE_INVALID,
 	BTRFS_REF_TYPE_BLOCK,
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH 1/5] btrfs: zoned: search for hinted block-group in prepare_allocation_zoned
  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
  0 siblings, 0 replies; 11+ messages in thread
From: Damien Le Moal @ 2026-11-26 21:09 UTC (permalink / raw)
  To: Johannes Thumshirn, linux-btrfs
  Cc: Christoph Hellwig, Hans Holmberg, Naohiro Aota, Josef Bacik,
	Filipe Manana, David Sterba, Boris Burkov, Qu Wenruo,
	Wilfred Mallawa

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

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 2/5] btrfs: zoned: save last allocation as placement hint
  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
  0 siblings, 0 replies; 11+ messages in thread
From: Damien Le Moal @ 2026-11-26 21:11 UTC (permalink / raw)
  To: Johannes Thumshirn, linux-btrfs
  Cc: Christoph Hellwig, Hans Holmberg, Naohiro Aota, Josef Bacik,
	Filipe Manana, David Sterba, Boris Burkov, Qu Wenruo,
	Wilfred Mallawa

On 11/26/26 22:28, Johannes Thumshirn wrote:
> Save the last allocation of a data extent in the inode and if set use it
> as a placement hint for a subsequent extent allocation in the extent
> allocator.
> 
> Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>

Looks good.

Reviewed-by: Damien Le Moal <dlemoal@kernel.org>

-- 
Damien Le Moal
Western Digital Research

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 3/5] btrfs: zoned: allocate a new block-group per file
  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
  0 siblings, 0 replies; 11+ messages in thread
From: Damien Le Moal @ 2026-11-26 21:20 UTC (permalink / raw)
  To: Johannes Thumshirn, linux-btrfs
  Cc: Christoph Hellwig, Hans Holmberg, Naohiro Aota, Josef Bacik,
	Filipe Manana, David Sterba, Boris Burkov, Qu Wenruo,
	Wilfred Mallawa

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

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 4/5] btrfs: zoned: try to fill hinted block-groups
  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
  0 siblings, 0 replies; 11+ messages in thread
From: Damien Le Moal @ 2026-11-26 21:25 UTC (permalink / raw)
  To: Johannes Thumshirn, linux-btrfs
  Cc: Christoph Hellwig, Hans Holmberg, Naohiro Aota, Josef Bacik,
	Filipe Manana, David Sterba, Boris Burkov, Qu Wenruo,
	Wilfred Mallawa

On 11/26/26 22:28, Johannes Thumshirn wrote:
> If the hinted block-group has less space left than the allocation, the
> allocation goes to a new block-group and the old one stays active with
> its leftover. With buffered writes this happens for nearly every
> block-group, the leftovers use up the active zones and new files fall
> back to round-robin.
> 
> Shrink the allocation to the space left in the hinted block-group
> instead, as long as it is not smaller than min_alloc_size. The
> block-group becomes full, gets finished and releases its active zone.
> 
> 5 parallel buffered writers on null_blk, 64M zones, max 12 active zones:
> files per zone 3.09 -> 1.46, extents per file 29.6 -> 5.8.
> 
> Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>

One nit below. Otherwise looks good to me.

Reviewed-by: Damien Le Moal <dlemoal@kernel.org>

> ---
>  fs/btrfs/extent-tree.c | 17 ++++++++++++++++-
>  1 file changed, 16 insertions(+), 1 deletion(-)
> 
> diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
> index 7169f296a523..d8dc43655b16 100644
> --- a/fs/btrfs/extent-tree.c
> +++ b/fs/btrfs/extent-tree.c
> @@ -4526,8 +4526,23 @@ static int prepare_allocation_zoned(struct btrfs_fs_info *fs_info,
>  					&block_group->runtime_flags))
>  			continue;
>  
> -		if (avail < ffe_ctl->num_bytes)
> +		/*
> +		 * If we have less space available in the block-group but can
> +		 * fit the allocation into the hinted block-group if we shrink
> +		 * it, fill up the block-group.
> +		 */
> +		if (avail < ffe_ctl->num_bytes) {
> +			avail = round_down(avail, fs_info->sectorsize);
> +			if (avail >= ffe_ctl->min_alloc_size &&
> +			    in_range(ffe_ctl->hint_byte, block_group->start,
> +				     block_group->length)) {

This in_range() call is being repeated. It feels like an inline wrapper for it
would be nice. E.g. something like:

	bg_contains_byte(block_group, ffe_ctl->hint_byte)


> +				if (ffe_ctl->ram_bytes == ffe_ctl->num_bytes)
> +					ffe_ctl->ram_bytes = avail;
> +				ffe_ctl->num_bytes = avail;
> +				break;
> +			}
>  			continue;
> +		}
>  
>  		/*
>  		 * Remember the first block-group that fits the allocation. Then


-- 
Damien Le Moal
Western Digital Research

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 5/5] btrfs: zoned: break concurrent chunk allocation race
  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
  0 siblings, 0 replies; 11+ messages in thread
From: Damien Le Moal @ 2026-11-26 21:30 UTC (permalink / raw)
  To: Johannes Thumshirn, linux-btrfs
  Cc: Christoph Hellwig, Hans Holmberg, Naohiro Aota, Josef Bacik,
	Filipe Manana, David Sterba, Boris Burkov, Qu Wenruo,
	Wilfred Mallawa

On 11/26/26 22:28, Johannes Thumshirn wrote:
> btrfs_chunk_alloc() coalesces concurrent allocations, so a fresh
> block-group request can end up without a chunk of its own when several
> new files race.
> 
> Retry the chunk allocation a few times before giving up on a fresh
> block-group.
> 
> Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>

Modulo the already commented fact that "fresh" is not a super nice name, looks
good to me.

Reviewed-by: Damien Le Moal <dlemoal@kernel.org>

But I strongly suggest to be more precise with the naming and replace "fresh"
with "free" or "empty" and always have bg or block_group as the qualifying name.
E.g. ffe_ctl->want_empty_bg

That would make the code a lot easier to follow and avoid confusions with other
things that can be fresh, like beer :)


-- 
Damien Le Moal
Western Digital Research

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2026-11-26 21:30 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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-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 21:25   ` Damien Le Moal
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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox