[v2] btrfs: add missing check after link_free_space
diff mbox series

Message ID 20191209034114.16212-1-dinghao.liu@zju.edu.cn
State New
Headers show
Series
  • [v2] btrfs: add missing check after link_free_space
Related show

Commit Message

Dinghao Liu Dec. 9, 2019, 3:41 a.m. UTC
The return value of link_free_space is checked out-sync.
One branch of an if statement uses an extra check after
WARN_ON() but its peer branch does not. WARN_ON() does
not change the control flow, thus only using this check
might be insufficient.

Fix this by simply adding a check on ret.

Signed-off-by: Dinghao Liu <dinghao.liu@zju.edu.cn>
--
Changes in v2:
  - Add memory free for free space entry.
---
 fs/btrfs/free-space-cache.c | 4 ++++
 1 file changed, 4 insertions(+)

Comments

David Sterba Dec. 9, 2019, 6:25 p.m. UTC | #1
On Mon, Dec 09, 2019 at 11:41:14AM +0800, Dinghao Liu wrote:
> The return value of link_free_space is checked out-sync.
> One branch of an if statement uses an extra check after
> WARN_ON() but its peer branch does not. WARN_ON() does
> not change the control flow, thus only using this check
> might be insufficient.
> 
> Fix this by simply adding a check on ret.
> 
> Signed-off-by: Dinghao Liu <dinghao.liu@zju.edu.cn>
> --
> Changes in v2:
>   - Add memory free for free space entry.
> ---
>  fs/btrfs/free-space-cache.c | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/fs/btrfs/free-space-cache.c b/fs/btrfs/free-space-cache.c
> index 3283da419200..ba2e6cea5233 100644
> --- a/fs/btrfs/free-space-cache.c
> +++ b/fs/btrfs/free-space-cache.c
> @@ -2437,6 +2437,10 @@ int btrfs_remove_free_space(struct btrfs_block_group *block_group,
>  			if (info->bytes) {
>  				ret = link_free_space(ctl, info);
>  				WARN_ON(ret);
> +				if (ret) {
> +					kmem_cache_free(btrfs_free_space_cachep, info);
> +					goto out_lock;
> +				}
>  			} else {
>  				kmem_cache_free(btrfs_free_space_cachep, info);

There are two link_free_space followed by WARN_ON instances in the
function, please remove both.

In the above case, the branches can be merged together so there's not
repeated kmem_cache_free, like

	if (info->bytes)
		ret = link_free_space(...);
	kmem_cache_free(btrfs_free_space_cachep, info);
	if (ret)
		goto out_unlock;

Patch
diff mbox series

diff --git a/fs/btrfs/free-space-cache.c b/fs/btrfs/free-space-cache.c
index 3283da419200..ba2e6cea5233 100644
--- a/fs/btrfs/free-space-cache.c
+++ b/fs/btrfs/free-space-cache.c
@@ -2437,6 +2437,10 @@  int btrfs_remove_free_space(struct btrfs_block_group *block_group,
 			if (info->bytes) {
 				ret = link_free_space(ctl, info);
 				WARN_ON(ret);
+				if (ret) {
+					kmem_cache_free(btrfs_free_space_cachep, info);
+					goto out_lock;
+				}
 			} else {
 				kmem_cache_free(btrfs_free_space_cachep, info);
 			}