btrfs: fix memory leaks when rejecting a non SINGLE data profile without an RST
PATCH v2 1/1 btrfs: fix memory leaks when rejecting a non SINGLE data profile without an RST
At the end of btrfs_load_block_group_zone_info() the first thing we do is to ensure that if the mapping type is not a SINGLE one and there is no RAID stripe tree, then we return early with an error.
Doing that, though, prevents the code from running the last calls from this function which are about freeing memory allocated during its run. Hence, in this case, instead of returning early, we set the ret value and fall through the rest of the cleanup code.
--- fs/btrfs/zoned.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
fs/btrfs/zoned.c +6 -1
@@ -1753,7 +1753,11 @@ int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new) !fs_info->stripe_root) {btrfs_err (fs_info,"zoned: data %s needs raid-stripe-tree" ,btrfs_bg_type_to_raid_name (map->type)); -return -EINVAL ; +/* +* Note that this might be overwritten by later if statements, +* but the error will be at least printed by the line above. +*/ + ret = -EINVAL ; }if (unlikely (cache->alloc_offset > cache->zone_capacity)) {
@@ -1785,6 +1789,7 @@ int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new)btrfs_free_chunk_map (cache->physical_map); cache->physical_map =NULL ; } +bitmap_free (active);kfree (zone_info);
Signature · 1 line
2.51.0
fs/btrfs/zoned.c int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new) · line 1759
Quoted 13 lines · Miquel Sabaté Solà
fs/btrfs/zoned.c +4
-1@@ -1753,7 +1753,11 @@ int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new) !fs_info->stripe_root) {btrfs_err (fs_info,"zoned: data %s needs raid-stripe-tree" ,btrfs_bg_type_to_raid_name (map->type)); -return -EINVAL ; +/* +* Note that this might be overwritten by later if statements, +* but the error will be at least printed by the line above. +*/
Not convinced the comment is useful.
fs/btrfs/zoned.c int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new) · line 1795
Quoted 12 lines
fs/btrfs/zoned.c +2
-0+ ret = -EINVAL ; }if (unlikely (cache->alloc_offset > cache->zone_capacity)) {@@ -1785,6 +1789,7 @@ int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new)btrfs_free_chunk_map (cache->physical_map); cache->physical_map =NULL ; } +bitmap_free (active);kfree (zone_info);
Stray newline.
Other than that,
Johannes Thumshirn @ 2025-10-08 12:04 GMT:
Quoted 34 lines · Miquel Sabaté Solà
fs/btrfs/zoned.c int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new) · line 1759
Quoted 13 lines
fs/btrfs/zoned.c +4
-1@@ -1753,7 +1753,11 @@ int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new) !fs_info->stripe_root) {btrfs_err (fs_info,"zoned: data %s needs raid-stripe-tree" ,btrfs_bg_type_to_raid_name (map->type)); -return -EINVAL ; +/* +* Note that this might be overwritten by later if statements, +* but the error will be at least printed by the line above. +*/ Not convinced the comment is useful.
fs/btrfs/zoned.c int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new) · line 1795
Quoted 12 lines
fs/btrfs/zoned.c +2
-0+ ret = -EINVAL ; }if (unlikely (cache->alloc_offset > cache->zone_capacity)) {@@ -1785,6 +1789,7 @@ int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new)btrfs_free_chunk_map (cache->physical_map); cache->physical_map =NULL ; } +bitmap_free (active);kfree (zone_info);Stray newline.
Quoted 3 lines
Other than that,
Reviewed-by Johannes Thumshirn <[email protected]>
Thanks for the review! Let me send a v3 shortly with your comments applied.