Re: [PATCH] btrfs: fix balance_ctl not free properly in btrfs_balance

From: David Sterba
Date: Fri Jul 14 2023 - 06:08:26 EST


On Fri, Jul 14, 2023 at 03:15:48AM -0400, xiaoshoukui wrote:
> Signed-off-by: xiaoshoukui <xiaoshoukui@xxxxxxxxxxxxx>
> ---
> fs/btrfs/volumes.c | 10 +---------
> 1 file changed, 1 insertion(+), 9 deletions(-)
>
> diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
> index 7823168c08a6..c1ab94d8694c 100644
> --- a/fs/btrfs/volumes.c
> +++ b/fs/btrfs/volumes.c
> @@ -4055,14 +4055,6 @@ static int alloc_profile_is_valid(u64 flags, int extended)
> return has_single_bit_set(flags);
> }
>
> -static inline int balance_need_close(struct btrfs_fs_info *fs_info)
> -{
> - /* cancel requested || normal exit path */
> - return atomic_read(&fs_info->balance_cancel_req) ||
> - (atomic_read(&fs_info->balance_pause_req) == 0 &&
> - atomic_read(&fs_info->balance_cancel_req) == 0);
> -}
> -
> /*
> * Validate target profile against allowed profiles and return true if it's OK.
> * Otherwise print the error message and return false.
> @@ -4411,7 +4403,7 @@ int btrfs_balance(struct btrfs_fs_info *fs_info,
> }
>
> if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
> - balance_need_close(fs_info)) {
> + ret == -ECANCELED || ret == 0) {
> reset_balance_state(fs_info);
> btrfs_exclop_finish(fs_info);

This is a similar patch to what Josef sent but not exactly the same,

https://lore.kernel.org/linux-btrfs/9cdf58c2f045863e98a52d7f9d5102ba12b87f07.1687496547.git.josef@xxxxxxxxxxxxxx/

Both remove balance_need_close but your version does not track the
paused state. I haven't analyzed it closer, but it looks like you're
missing some case. Josef's fix simplifies the error handling so we don't
have te enumerate the errors.

As you have a reproducer, can you please try it with this patch instead?
It's possible that there are still some unhandled states so it would be
good to check. Thanks.