Skip to contentSkip to conversation

btrfs: zoned: skip reporting zone for new block group

PATCH Superseded Naohiro Aota 4 participants 11 messages Mar 12, 2025 – Mar 13, 2025

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover btrfs: zoned: skip reporting zone for new block group

Newly created block group should reside on empty zones, whose write pointer should always be 0. Also, we can load the zone capacity from the block layer. So, we don't need to REPORT_ZONE to load the info.

The reporting zone on a new block group is not only unnecessary, but also can cause a deadlock. When one process do a report zones and another process freezes the block device, the report zones side cannot allocate a tag because the freeze is already started. This can thus result in new block group creation to hang forever, blocking the write path.

Naohiro Aota (2):
block: introduce zone capacity helper
btrfs: zoned: skip reporting zone for new block group

 fs/btrfs/zoned.c       | 18 ++++++++++++++++--
 include/linux/blkdev.h | 21 +++++++++++++++++++++
 2 files changed, 37 insertions(+), 2 deletions(-)
Signature · 1 line
2.48.1
#1 lore raw

PATCH 1/2 block: introduce zone capacity helper

{bdev,disk}_zone_capacity() takes block_device or gendisk and sector position and returns the zone capacity of the corresponding zone.

---
 include/linux/blkdev.h | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
include/linux/blkdev.h +21 -0
index d37751789bf5..3c860a0cf339 100644
@@ -826,6 +826,27 @@ static inline u64 sb_bdev_nr_blocks(struct super_block *sb)
 		(sb->s_blocksize_bits - SECTOR_SHIFT);
 }
 
+#ifdef CONFIG_BLK_DEV_ZONED
+static inline unsigned int disk_zone_capacity(struct gendisk *disk, sector_t pos)
+{
+	sector_t zone_sectors = disk->queue->limits.chunk_sectors;
+
+	if (pos + zone_sectors >= get_capacity(disk))
+		return disk->last_zone_capacity;
+	return disk->zone_capacity;
+}
+#else /* CONFIG_BLK_DEV_ZONED */
+static inline unsigned int disk_zone_capacity(struct gendisk *disk, sector_t pos)
+{
+	return 0;
+}
+#endif /* CONFIG_BLK_DEV_ZONED */
+
+static inline unsigned int bdev_zone_capacity(struct block_device *bdev, sector_t pos)
+{
+	return disk_zone_capacity(bdev->bd_disk, pos);
+}
+
 int bdev_disk_changed(struct gendisk *disk, bool invalidate);
 
 void put_disk(struct gendisk *disk);
Signature · 1 line
2.48.1
#2 lore raw

Re: [PATCH 1/2] block: introduce zone capacity helper

Quoted 13 lines · Naohiro Aota

{bdev,disk}_zone_capacity() takes block_device or gendisk and sector position and returns the zone capacity of the corresponding zone.

---
 include/linux/blkdev.h | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
include/linux/blkdev.h +0 -0
index d37751789bf5..3c860a0cf339 100644

include/linux/blkdev.h static inline u64 sb_bdev_nr_blocks(struct super_block *sb) · line 829

Quoted 4 lines
include/linux/blkdev.h +1 -0
@@ -826,6 +826,27 @@ static inline u64 sb_bdev_nr_blocks(struct super_block *sb)
 		(sb->s_blocksize_bits - SECTOR_SHIFT);
 }
 
+#ifdef CONFIG_BLK_DEV_ZONED

There is already an "#ifdef CONFIG_BLK_DEV_ZONED" in blkdev.h, see disk_nr_zones(). Please add this new helper under that ifdef to avoid adding a new one.

include/linux/blkdev.h static inline u64 sb_bdev_nr_blocks(struct super_block *sb) · line 839

Quoted 10 lines
include/linux/blkdev.h +10 -0
+static inline unsigned int disk_zone_capacity(struct gendisk *disk, sector_t pos)
+{
+	sector_t zone_sectors = disk->queue->limits.chunk_sectors;
+
+	if (pos + zone_sectors >= get_capacity(disk))
+		return disk->last_zone_capacity;
+	return disk->zone_capacity;
+}
+#else /* CONFIG_BLK_DEV_ZONED */
+static inline unsigned int disk_zone_capacity(struct gendisk *disk, sector_t pos)

Is this ever called for a non zoned drive ? It should not be... So do we really need this stub ?

Quoted 13 lines
include/linux/blkdev.h +10 -0
+{
+	return 0;
+}
+#endif /* CONFIG_BLK_DEV_ZONED */
+
+static inline unsigned int bdev_zone_capacity(struct block_device *bdev, sector_t pos)
+{
+	return disk_zone_capacity(bdev->bd_disk, pos);
+}
+
 int bdev_disk_changed(struct gendisk *disk, bool invalidate);
 
 void put_disk(struct gendisk *disk);
Signature · 2 lines
Damien Le Moal
Western Digital Research
#3 lore raw

Re: [PATCH 1/2] block: introduce zone capacity helper

Quoted 17 lines · Damien Le Moal
Quoted 16 lines · Naohiro Aota

{bdev,disk}_zone_capacity() takes block_device or gendisk and sector position and returns the zone capacity of the corresponding zone.

---
 include/linux/blkdev.h | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
include/linux/blkdev.h +0 -0
index d37751789bf5..3c860a0cf339 100644
@@ -826,6 +826,27 @@ static inline u64 sb_bdev_nr_blocks(struct super_block *sb)
 		(sb->s_blocksize_bits - SECTOR_SHIFT);
 }
 
Quoted 5 lines

include/linux/blkdev.h static inline u64 sb_bdev_nr_blocks(struct super_block *sb) · line 829

Quoted 1 line
include/linux/blkdev.h +1 -0
+#ifdef CONFIG_BLK_DEV_ZONED

There is already an "#ifdef CONFIG_BLK_DEV_ZONED" in blkdev.h, see disk_nr_zones(). Please add this new helper under that ifdef to avoid adding a new one.

Sure. But, since this patch uses get_capacity(), we need to move the existing ones here instead.

Quoted 14 lines

include/linux/blkdev.h static inline u64 sb_bdev_nr_blocks(struct super_block *sb) · line 839

Quoted 11 lines
include/linux/blkdev.h +10 -0
+static inline unsigned int disk_zone_capacity(struct gendisk *disk, sector_t pos)
+{
+	sector_t zone_sectors = disk->queue->limits.chunk_sectors;
+
+	if (pos + zone_sectors >= get_capacity(disk))
+		return disk->last_zone_capacity;
+	return disk->zone_capacity;
+}
+#else /* CONFIG_BLK_DEV_ZONED */
+static inline unsigned int disk_zone_capacity(struct gendisk *disk, sector_t pos)

Is this ever called for a non zoned drive ? It should not be... So do we really need this stub ?

I added that just let it compile without CONFIG_BLK_DEV_ZONED. Well, the code querying the capacity should be under CONFIG_BLK_DEV_ZONED, so maybe it is not necessary. I'll drop that.

Quoted 14 lines
Quoted 14 lines
include/linux/blkdev.h +10 -0
+{
+	return 0;
+}
+#endif /* CONFIG_BLK_DEV_ZONED */
+
+static inline unsigned int bdev_zone_capacity(struct block_device *bdev, sector_t pos)
+{
+	return disk_zone_capacity(bdev->bd_disk, pos);
+}
+
 int bdev_disk_changed(struct gendisk *disk, bool invalidate);
 
 void put_disk(struct gendisk *disk);
#4 lore raw

Re: [PATCH 1/2] block: introduce zone capacity helper

In reply to Naohiro Aota

Review

Quoted 2 lines · Naohiro Aota
(quoted diff) +2 -0
+#ifdef CONFIG_BLK_DEV_ZONED
+static inline unsigned int disk_zone_capacity(struct gendisk *disk, sector_t pos)

Overly long line.

include/linux/blkdev.h static inline u64 sb_bdev_nr_blocks(struct super_block *sb) · line 836

Quoted 6 lines
include/linux/blkdev.h +6 -0
+{
+	sector_t zone_sectors = disk->queue->limits.chunk_sectors;
+
+	if (pos + zone_sectors >= get_capacity(disk))
+		return disk->last_zone_capacity;
+	return disk->zone_capacity;

But I also don't understand how pos plays in here. Maybe add a kerneldoc comment describing what the function is supposed to do?

#5 lore raw

Re: [PATCH 1/2] block: introduce zone capacity helper

Quoted 6 lines · Christoph Hellwig

Review

Quoted 2 lines · Naohiro Aota
(quoted diff) +2 -0
+#ifdef CONFIG_BLK_DEV_ZONED
+static inline unsigned int disk_zone_capacity(struct gendisk *disk, sector_t pos)

Overly long line.

Quoted 9 lines

include/linux/blkdev.h static inline u64 sb_bdev_nr_blocks(struct super_block *sb) · line 836

Quoted 6 lines
include/linux/blkdev.h +6 -0
+{
+	sector_t zone_sectors = disk->queue->limits.chunk_sectors;
+
+	if (pos + zone_sectors >= get_capacity(disk))
+		return disk->last_zone_capacity;
+	return disk->zone_capacity;

But I also don't understand how pos plays in here. Maybe add a kerneldoc comment describing what the function is supposed to do?

The last zone can be smaller than all other zones, hence we have disk->zone_capacity and disk->last_zone_capacity. Pos is a sector used to indicate the zone for which you want the capacity.

But yes, agreed, a kernel doc would be nice to clarify that.

Signature · 2 lines
Damien Le Moal
Western Digital Research
#6 lore raw

Re: [PATCH 1/2] block: introduce zone capacity helper

Quoted 17 lines · Damien Le Moal
Quoted 6 lines · Christoph Hellwig

Review

Quoted 2 lines · Naohiro Aota
(quoted diff) +2 -0
+#ifdef CONFIG_BLK_DEV_ZONED
+static inline unsigned int disk_zone_capacity(struct gendisk *disk, sector_t pos)

Overly long line.

Quoted 9 lines

include/linux/blkdev.h static inline u64 sb_bdev_nr_blocks(struct super_block *sb) · line 836

Quoted 6 lines
include/linux/blkdev.h +6 -0
+{
+	sector_t zone_sectors = disk->queue->limits.chunk_sectors;
+
+	if (pos + zone_sectors >= get_capacity(disk))
+		return disk->last_zone_capacity;
+	return disk->zone_capacity;

But I also don't understand how pos plays in here. Maybe add a kerneldoc comment describing what the function is supposed to do?

Quoted 5 lines

The last zone can be smaller than all other zones, hence we have disk->zone_capacity and disk->last_zone_capacity. Pos is a sector used to indicate the zone for which you want the capacity.

But yes, agreed, a kernel doc would be nice to clarify that.

Should it be zoned_start then to make that obvious?

#7 lore raw

Re: [PATCH 1/2] block: introduce zone capacity helper

Quoted 18 lines · Christoph Hellwig
Quoted 17 lines · Damien Le Moal
Quoted 15 lines · Christoph Hellwig

Review

Quoted 2 lines · Naohiro Aota
(quoted diff) +2 -0
+#ifdef CONFIG_BLK_DEV_ZONED
+static inline unsigned int disk_zone_capacity(struct gendisk *disk, sector_t pos)

Overly long line.

include/linux/blkdev.h static inline u64 sb_bdev_nr_blocks(struct super_block *sb) · line 836

Quoted 6 lines
include/linux/blkdev.h +6 -0
+{
+	sector_t zone_sectors = disk->queue->limits.chunk_sectors;
+
+	if (pos + zone_sectors >= get_capacity(disk))
+		return disk->last_zone_capacity;
+	return disk->zone_capacity;

But I also don't understand how pos plays in here. Maybe add a kerneldoc comment describing what the function is supposed to do?

Quoted 7 lines
Quoted 5 lines

The last zone can be smaller than all other zones, hence we have disk->zone_capacity and disk->last_zone_capacity. Pos is a sector used to indicate the zone for which you want the capacity.

But yes, agreed, a kernel doc would be nice to clarify that.

Should it be zoned_start then to make that obvious?

Well, it does not have to be the zone start. It can be any sector contained in the zone you are interested in. With a comment, that should be clear, and also the same as helpers such as disk_zone_no() which do not require the zone start sector.

Signature · 2 lines
Damien Le Moal
Western Digital Research
#8 lore raw

PATCH 2/2 btrfs: zoned: skip reporting zone for new block group

In reply to Naohiro Aota

There is a potential deadlock if we do report zones in an IO context. When one process do a report zones and another process freezes the block device, the report zones side cannot allocate a tag because the freeze is already started. This can thus result in new block group creation to hang forever, blocking the write path.

Thankfully, a new block group should be created on empty zones. So, reporting the zones is not necessary and we can set the write pointer = 0 and load the zone capacity from the block layer using bdev_zone_capacity() helper.

---
 fs/btrfs/zoned.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
fs/btrfs/zoned.c +16 -2
index 4956baf8220a..6c730f6bce10 100644
@@ -1277,7 +1277,7 @@ struct zone_info {
 
 static int btrfs_load_zone_info(struct btrfs_fs_info *fs_info, int zone_idx,
 				struct zone_info *info, unsigned long *active,
-				struct btrfs_chunk_map *map)
+				struct btrfs_chunk_map *map, bool new)
 {
 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
 	struct btrfs_device *device;
@@ -1307,6 +1307,8 @@ static int btrfs_load_zone_info(struct btrfs_fs_info *fs_info, int zone_idx,
 		return 0;
 	}
 
+	ASSERT(!new || btrfs_dev_is_empty_zone(device, info->physical));
+
 	/* This zone will be used for allocation, so mark this zone non-empty. */
 	btrfs_dev_clear_zone_empty(device, info->physical);
 
@@ -1319,6 +1321,18 @@ static int btrfs_load_zone_info(struct btrfs_fs_info *fs_info, int zone_idx,
 	 * to determine the allocation offset within the zone.
 	 */
 	WARN_ON(!IS_ALIGNED(info->physical, fs_info->zone_size));
+
+	if (new) {
+		sector_t capacity;
+
+		capacity = bdev_zone_capacity(device->bdev, info->physical >> SECTOR_SHIFT);
+		up_read(&dev_replace->rwsem);
+		info->alloc_offset = 0;
+		info->capacity = capacity << SECTOR_SHIFT;
+
+		return 0;
+	}
+
 	nofs_flag = memalloc_nofs_save();
 	ret = btrfs_get_dev_zone(device, info->physical, &zone);
 	memalloc_nofs_restore(nofs_flag);
@@ -1588,7 +1602,7 @@ int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new)
 	}
 
 	for (i = 0; i < map->num_stripes; i++) {
-		ret = btrfs_load_zone_info(fs_info, i, &zone_info[i], active, map);
+		ret = btrfs_load_zone_info(fs_info, i, &zone_info[i], active, map, new);
 		if (ret)
 			goto out;
 
Signature · 1 line
2.48.1
Reviewed-by on v1 2/2
#9 lore raw
Quoted 5 lines · Naohiro Aota

There is a potential deadlock if we do report zones in an IO context. When one process do a report zones and another process freezes the block device, the report zones side cannot allocate a tag because the freeze is already started. This can thus result in new block group creation to hang forever, blocking the write path.

+Shin'ichiro

blktest has a failing test case due to a lockdep splat triggered by this. Would be good to add that information (with the splat) here.

Quoted 7 lines

Thankfully, a new block group should be created on empty zones. So, reporting the zones is not necessary and we can set the write pointer = 0 and load the zone capacity from the block layer using bdev_zone_capacity() helper.

With that fixed, looks good to me.

Signature · 2 lines
Damien Le Moal
Western Digital Research
#10 lore raw
Quoted 11 lines · Damien Le Moal
Quoted 5 lines · Naohiro Aota

There is a potential deadlock if we do report zones in an IO context. When one process do a report zones and another process freezes the block device, the report zones side cannot allocate a tag because the freeze is already started. This can thus result in new block group creation to hang forever, blocking the write path.

+Shin'ichiro

blktest has a failing test case due to a lockdep splat triggered by this. Would be good to add that information (with the splat) here.

I confirmed that this fix avoids the blktests zbd/009 failure I reported [1]. Thanks for the fix!

[1] https://lore.kernel.org/linux-block/uyijd3ufbrfbiyyaajvhyhdyytssubekvymzgyiqjqmkh33cmi@ksqjpewsqlvw/