[PATCH net-next] net/mlx5e: Cleanup error handle in mlx5e_tc_sample_init()

Yue Haibing posted 1 patch 3 months, 2 weeks ago
.../ethernet/mellanox/mlx5/core/en/tc/sample.c   | 16 ++++++++++------
1 file changed, 10 insertions(+), 6 deletions(-)
[PATCH net-next] net/mlx5e: Cleanup error handle in mlx5e_tc_sample_init()
Posted by Yue Haibing 3 months, 2 weeks ago
post_act is initialized in mlx5e_tc_post_act_init(), which never return
NULL. And if it is invalid, no need to alloc tc_psample mem.

Signed-off-by: Yue Haibing <yuehaibing@huawei.com>
---
 .../ethernet/mellanox/mlx5/core/en/tc/sample.c   | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en/tc/sample.c b/drivers/net/ethernet/mellanox/mlx5/core/en/tc/sample.c
index 5db239cae814..1b083afbe1bc 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/en/tc/sample.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/en/tc/sample.c
@@ -619,26 +619,30 @@ mlx5e_tc_sample_init(struct mlx5_eswitch *esw, struct mlx5e_post_act *post_act)
 	struct mlx5e_tc_psample *tc_psample;
 	int err;
 
-	tc_psample = kzalloc(sizeof(*tc_psample), GFP_KERNEL);
-	if (!tc_psample)
-		return ERR_PTR(-ENOMEM);
-	if (IS_ERR_OR_NULL(post_act)) {
+	if (IS_ERR(post_act)) {
 		err = PTR_ERR(post_act);
 		goto err_post_act;
 	}
+
+	tc_psample = kzalloc(sizeof(*tc_psample), GFP_KERNEL);
+	if (!tc_psample) {
+		err = -ENOMEM;
+		goto err_post_act;
+	}
 	tc_psample->post_act = post_act;
 	tc_psample->esw = esw;
 	err = sampler_termtbl_create(tc_psample);
 	if (err)
-		goto err_post_act;
+		goto err_create;
 
 	mutex_init(&tc_psample->ht_lock);
 	mutex_init(&tc_psample->restore_lock);
 
 	return tc_psample;
 
-err_post_act:
+err_create:
 	kfree(tc_psample);
+err_post_act:
 	return ERR_PTR(err);
 }
 
-- 
2.34.1
Re: [PATCH net-next] net/mlx5e: Cleanup error handle in mlx5e_tc_sample_init()
Posted by Yue Haibing 2 months, 2 weeks ago
ping...

On 2025/6/25 18:20, Yue Haibing wrote:
> post_act is initialized in mlx5e_tc_post_act_init(), which never return
> NULL. And if it is invalid, no need to alloc tc_psample mem.
> 
> Signed-off-by: Yue Haibing <yuehaibing@huawei.com>
> ---
>  .../ethernet/mellanox/mlx5/core/en/tc/sample.c   | 16 ++++++++++------
>  1 file changed, 10 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en/tc/sample.c b/drivers/net/ethernet/mellanox/mlx5/core/en/tc/sample.c
> index 5db239cae814..1b083afbe1bc 100644
> --- a/drivers/net/ethernet/mellanox/mlx5/core/en/tc/sample.c
> +++ b/drivers/net/ethernet/mellanox/mlx5/core/en/tc/sample.c
> @@ -619,26 +619,30 @@ mlx5e_tc_sample_init(struct mlx5_eswitch *esw, struct mlx5e_post_act *post_act)
>  	struct mlx5e_tc_psample *tc_psample;
>  	int err;
>  
> -	tc_psample = kzalloc(sizeof(*tc_psample), GFP_KERNEL);
> -	if (!tc_psample)
> -		return ERR_PTR(-ENOMEM);
> -	if (IS_ERR_OR_NULL(post_act)) {
> +	if (IS_ERR(post_act)) {
>  		err = PTR_ERR(post_act);
>  		goto err_post_act;
>  	}
> +
> +	tc_psample = kzalloc(sizeof(*tc_psample), GFP_KERNEL);
> +	if (!tc_psample) {
> +		err = -ENOMEM;
> +		goto err_post_act;
> +	}
>  	tc_psample->post_act = post_act;
>  	tc_psample->esw = esw;
>  	err = sampler_termtbl_create(tc_psample);
>  	if (err)
> -		goto err_post_act;
> +		goto err_create;
>  
>  	mutex_init(&tc_psample->ht_lock);
>  	mutex_init(&tc_psample->restore_lock);
>  
>  	return tc_psample;
>  
> -err_post_act:
> +err_create:
>  	kfree(tc_psample);
> +err_post_act:
>  	return ERR_PTR(err);
>  }
>