diff mbox series

[net] net/mlx5: DR, fix memory leak in mlx5dr_cmd_create_reformat_ctx

Message ID 20230704033308.3773764-1-shaozhengchao@huawei.com (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series [net] net/mlx5: DR, fix memory leak in mlx5dr_cmd_create_reformat_ctx | expand

Checks

Context Check Description
netdev/series_format success Single patches do not need cover letters
netdev/tree_selection success Clearly marked for net
netdev/fixes_present success Fixes tag present in non-next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 8 this patch: 8
netdev/cc_maintainers success CCed 14 of 14 maintainers
netdev/build_clang fail Errors and warnings before: 18 this patch: 18
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 8 this patch: 8
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 12 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

shaozhengchao July 4, 2023, 3:33 a.m. UTC
when mlx5_cmd_exec failed in mlx5dr_cmd_create_reformat_ctx, the memory
pointed by 'in' is not released, which will cause memory leak. Move memory
release after mlx5_cmd_exec.

Fixes: 1d9186476e12 ("net/mlx5: DR, Add direct rule command utilities")
Signed-off-by: Zhengchao Shao <shaozhengchao@huawei.com>
---
 drivers/net/ethernet/mellanox/mlx5/core/steering/dr_cmd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Simon Horman July 4, 2023, 2:40 p.m. UTC | #1
On Tue, Jul 04, 2023 at 11:33:08AM +0800, Zhengchao Shao wrote:
> when mlx5_cmd_exec failed in mlx5dr_cmd_create_reformat_ctx, the memory
> pointed by 'in' is not released, which will cause memory leak. Move memory
> release after mlx5_cmd_exec.
> 
> Fixes: 1d9186476e12 ("net/mlx5: DR, Add direct rule command utilities")
> Signed-off-by: Zhengchao Shao <shaozhengchao@huawei.com>
> ---
>  drivers/net/ethernet/mellanox/mlx5/core/steering/dr_cmd.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/net/ethernet/mellanox/mlx5/core/steering/dr_cmd.c b/drivers/net/ethernet/mellanox/mlx5/core/steering/dr_cmd.c
> index 7491911ebcb5..cf5820744e99 100644
> --- a/drivers/net/ethernet/mellanox/mlx5/core/steering/dr_cmd.c
> +++ b/drivers/net/ethernet/mellanox/mlx5/core/steering/dr_cmd.c
> @@ -563,11 +563,11 @@ int mlx5dr_cmd_create_reformat_ctx(struct mlx5_core_dev *mdev,
>  		memcpy(pdata, reformat_data, reformat_size);
>  
>  	err = mlx5_cmd_exec(mdev, in, inlen, out, sizeof(out));
> +	kvfree(in);
>  	if (err)
>  		return err;
>  
>  	*reformat_id = MLX5_GET(alloc_packet_reformat_context_out, out, packet_reformat_id);
> -	kvfree(in);
>  
>  	return err;
>  }

Hi Zhengchao Shao,

I agree this is a correct fix.
However, I think a more idiomatic approach to this problem
would be to use a goto label. Something like this (completely untested!).

diff --git a/drivers/net/ethernet/mellanox/mlx5/core/steering/dr_cmd.c b/drivers/net/ethernet/mellanox/mlx5/core/steering/dr_cmd.c
index 7491911ebcb5..8c2a34a0d6be 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/steering/dr_cmd.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/steering/dr_cmd.c
@@ -564,11 +564,12 @@ int mlx5dr_cmd_create_reformat_ctx(struct mlx5_core_dev *mdev,
 
 	err = mlx5_cmd_exec(mdev, in, inlen, out, sizeof(out));
 	if (err)
-		return err;
+		goto err_free_in;
 
 	*reformat_id = MLX5_GET(alloc_packet_reformat_context_out, out, packet_reformat_id);
-	kvfree(in);
 
+err_free_in:
+	kvfree(in);
 	return err;
 }
Leon Romanovsky July 5, 2023, 8:23 a.m. UTC | #2
On Tue, Jul 04, 2023 at 03:40:44PM +0100, Simon Horman wrote:
> On Tue, Jul 04, 2023 at 11:33:08AM +0800, Zhengchao Shao wrote:
> > when mlx5_cmd_exec failed in mlx5dr_cmd_create_reformat_ctx, the memory
> > pointed by 'in' is not released, which will cause memory leak. Move memory
> > release after mlx5_cmd_exec.
> > 
> > Fixes: 1d9186476e12 ("net/mlx5: DR, Add direct rule command utilities")
> > Signed-off-by: Zhengchao Shao <shaozhengchao@huawei.com>
> > ---
> >  drivers/net/ethernet/mellanox/mlx5/core/steering/dr_cmd.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> > 
> > diff --git a/drivers/net/ethernet/mellanox/mlx5/core/steering/dr_cmd.c b/drivers/net/ethernet/mellanox/mlx5/core/steering/dr_cmd.c
> > index 7491911ebcb5..cf5820744e99 100644
> > --- a/drivers/net/ethernet/mellanox/mlx5/core/steering/dr_cmd.c
> > +++ b/drivers/net/ethernet/mellanox/mlx5/core/steering/dr_cmd.c
> > @@ -563,11 +563,11 @@ int mlx5dr_cmd_create_reformat_ctx(struct mlx5_core_dev *mdev,
> >  		memcpy(pdata, reformat_data, reformat_size);
> >  
> >  	err = mlx5_cmd_exec(mdev, in, inlen, out, sizeof(out));
> > +	kvfree(in);
> >  	if (err)
> >  		return err;
> >  
> >  	*reformat_id = MLX5_GET(alloc_packet_reformat_context_out, out, packet_reformat_id);
> > -	kvfree(in);
> >  
> >  	return err;
> >  }
> 
> Hi Zhengchao Shao,
> 
> I agree this is a correct fix.
> However, I think a more idiomatic approach to this problem
> would be to use a goto label. Something like this (completely untested!).

Thanks, your change looks more natural to me.

> 
> diff --git a/drivers/net/ethernet/mellanox/mlx5/core/steering/dr_cmd.c b/drivers/net/ethernet/mellanox/mlx5/core/steering/dr_cmd.c
> index 7491911ebcb5..8c2a34a0d6be 100644
> --- a/drivers/net/ethernet/mellanox/mlx5/core/steering/dr_cmd.c
> +++ b/drivers/net/ethernet/mellanox/mlx5/core/steering/dr_cmd.c
> @@ -564,11 +564,12 @@ int mlx5dr_cmd_create_reformat_ctx(struct mlx5_core_dev *mdev,
>  
>  	err = mlx5_cmd_exec(mdev, in, inlen, out, sizeof(out));
>  	if (err)
> -		return err;
> +		goto err_free_in;
>  
>  	*reformat_id = MLX5_GET(alloc_packet_reformat_context_out, out, packet_reformat_id);
> -	kvfree(in);
>  
> +err_free_in:
> +	kvfree(in);
>  	return err;
>  }
>
shaozhengchao July 5, 2023, 11:51 a.m. UTC | #3
Hi:
On 2023/7/5 16:23, Leon Romanovsky wrote:
> On Tue, Jul 04, 2023 at 03:40:44PM +0100, Simon Horman wrote:
>> On Tue, Jul 04, 2023 at 11:33:08AM +0800, Zhengchao Shao wrote:
>>> when mlx5_cmd_exec failed in mlx5dr_cmd_create_reformat_ctx, the memory
>>> pointed by 'in' is not released, which will cause memory leak. Move memory
>>> release after mlx5_cmd_exec.
>>>
>>> Fixes: 1d9186476e12 ("net/mlx5: DR, Add direct rule command utilities")
>>> Signed-off-by: Zhengchao Shao <shaozhengchao@huawei.com>
>>> ---
>>>   drivers/net/ethernet/mellanox/mlx5/core/steering/dr_cmd.c | 2 +-
>>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>>
>>> diff --git a/drivers/net/ethernet/mellanox/mlx5/core/steering/dr_cmd.c b/drivers/net/ethernet/mellanox/mlx5/core/steering/dr_cmd.c
>>> index 7491911ebcb5..cf5820744e99 100644
>>> --- a/drivers/net/ethernet/mellanox/mlx5/core/steering/dr_cmd.c
>>> +++ b/drivers/net/ethernet/mellanox/mlx5/core/steering/dr_cmd.c
>>> @@ -563,11 +563,11 @@ int mlx5dr_cmd_create_reformat_ctx(struct mlx5_core_dev *mdev,
>>>   		memcpy(pdata, reformat_data, reformat_size);
>>>   
>>>   	err = mlx5_cmd_exec(mdev, in, inlen, out, sizeof(out));
>>> +	kvfree(in);
>>>   	if (err)
>>>   		return err;
>>>   
>>>   	*reformat_id = MLX5_GET(alloc_packet_reformat_context_out, out, packet_reformat_id);
>>> -	kvfree(in);
>>>   
>>>   	return err;
>>>   }
>>
>> Hi Zhengchao Shao,
>>
>> I agree this is a correct fix.
>> However, I think a more idiomatic approach to this problem
>> would be to use a goto label. Something like this (completely untested!).
> 
> Thanks, your change looks more natural to me.
> 
>>
>> diff --git a/drivers/net/ethernet/mellanox/mlx5/core/steering/dr_cmd.c b/drivers/net/ethernet/mellanox/mlx5/core/steering/dr_cmd.c
>> index 7491911ebcb5..8c2a34a0d6be 100644
>> --- a/drivers/net/ethernet/mellanox/mlx5/core/steering/dr_cmd.c
>> +++ b/drivers/net/ethernet/mellanox/mlx5/core/steering/dr_cmd.c
>> @@ -564,11 +564,12 @@ int mlx5dr_cmd_create_reformat_ctx(struct mlx5_core_dev *mdev,
>>   
>>   	err = mlx5_cmd_exec(mdev, in, inlen, out, sizeof(out));
>>   	if (err)
>> -		return err;
>> +		goto err_free_in;
>>   
>>   	*reformat_id = MLX5_GET(alloc_packet_reformat_context_out, out, packet_reformat_id);
>> -	kvfree(in);
>>   
>> +err_free_in:
>> +	kvfree(in);
>>   	return err;
>>   }
>>   

	Thank you for your advice. I will send V2.

Zhengchao Shao
diff mbox series

Patch

diff --git a/drivers/net/ethernet/mellanox/mlx5/core/steering/dr_cmd.c b/drivers/net/ethernet/mellanox/mlx5/core/steering/dr_cmd.c
index 7491911ebcb5..cf5820744e99 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/steering/dr_cmd.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/steering/dr_cmd.c
@@ -563,11 +563,11 @@  int mlx5dr_cmd_create_reformat_ctx(struct mlx5_core_dev *mdev,
 		memcpy(pdata, reformat_data, reformat_size);
 
 	err = mlx5_cmd_exec(mdev, in, inlen, out, sizeof(out));
+	kvfree(in);
 	if (err)
 		return err;
 
 	*reformat_id = MLX5_GET(alloc_packet_reformat_context_out, out, packet_reformat_id);
-	kvfree(in);
 
 	return err;
 }