Re: [PATCH v2 1/3] btrfs: balance: fix null-ptr-deref in chunk_usage_filter

From: ZhengYuan Huang

Date: Mon Mar 23 2026 - 22:59:52 EST


On Tue, Mar 24, 2026 at 1:40 AM David Sterba <dsterba@xxxxxxx> wrote:
> So, for example you let a filesystem create some structures, let it
> continue, damage/destroy the structures and then let it access again?
>
> If this is supposed to emulate a corruption, either on media or in the
> IO path then OK.

Yes, this is one of the fuzzing strategies we use, where metadata is
intentionally corrupted at runtime to emulate possible media corruption
or I/O errors.

> > diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
> > index 2bec544d8ba3..7c21ac249383 100644
> > --- a/fs/btrfs/volumes.c
> > +++ b/fs/btrfs/volumes.c
> > @@ -3863,14 +3863,20 @@ static bool chunk_usage_range_filter(struct btrfs_fs_info *fs_info, u64 chunk_of
> > return ret;
> > }
> >
> > -static bool chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
> > - struct btrfs_balance_args *bargs)
> > +static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
> > + struct btrfs_balance_args *bargs)
> > {
> > struct btrfs_block_group *cache;
> > u64 chunk_used, user_thresh;
> > bool ret = true;
>
> As this is bool it does not match the changed return type anymore
>
> >
> > cache = btrfs_lookup_block_group(fs_info, chunk_offset);
> > + if (!cache) {
> > + btrfs_err(fs_info,
> > + "balance: chunk at bytenr %llu has no corresponding block group",
> > + chunk_offset);
> > + return -EUCLEAN;
> > + }
> > chunk_used = cache->used;
> >
> > if (bargs->usage_min == 0)
> > @@ -3986,8 +3992,8 @@ static bool chunk_soft_convert_filter(u64 chunk_type, struct btrfs_balance_args
> > return false;
> > }
> >
> > -static bool should_balance_chunk(struct extent_buffer *leaf, struct btrfs_chunk *chunk,
> > - u64 chunk_offset)
> > +static int should_balance_chunk(struct extent_buffer *leaf, struct btrfs_chunk *chunk,
> > + u64 chunk_offset)
> > {
> > struct btrfs_fs_info *fs_info = leaf->fs_info;
> > struct btrfs_balance_control *bctl = fs_info->balance_ctl;
> > @@ -4014,9 +4020,13 @@ static bool should_balance_chunk(struct extent_buffer *leaf, struct btrfs_chunk
> > }
> >
> > /* usage filter */
> > - if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
> > - chunk_usage_filter(fs_info, chunk_offset, bargs)) {
> > - return false;
> > + if (bargs->flags & BTRFS_BALANCE_ARGS_USAGE) {
> > + int filter_ret = chunk_usage_filter(fs_info, chunk_offset, bargs);
>
> Same problem here. Also please use ret2 for nested return values.

Thanks for the note, I’ll fix the return type issue and send a v3.

Thanks,
ZhengYuan Huang