diff mbox series

[v2] qlcnic: fix a memory leak in qlcnic_sriov_set_guest_vlan_mode()

Message ID 20250305100950.4001113-1-haoxiang_li2024@163.com (mailing list archive)
State Changes Requested
Delegated to: Netdev Maintainers
Headers show
Series [v2] qlcnic: fix a memory leak in qlcnic_sriov_set_guest_vlan_mode() | expand

Checks

Context Check Description
netdev/series_format warning Single patches do not need cover letters; Target tree name not specified in the subject
netdev/tree_selection success Guessed tree name to be net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/build_tools success No tools touched, skip
netdev/cc_maintainers success CCed 10 of 10 maintainers
netdev/build_clang success Errors and warnings before: 0 this patch: 0
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: 0 this patch: 0
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 22 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
netdev/contest success net-next-2025-03-06--03-00 (tests: 894)

Commit Message

Haoxiang Li March 5, 2025, 10:09 a.m. UTC
Add qlcnic_sriov_free_vlans() to free the memory allocated by
qlcnic_sriov_alloc_vlans() if qlcnic_sriov_alloc_vlans() fails
or "sriov->allowed_vlans" fails to be allocated.

Fixes: 91b7282b613d ("qlcnic: Support VLAN id config.")
Cc: stable@vger.kernel.org
Signed-off-by: Haoxiang Li <haoxiang_li2024@163.com>
---
Changes in v2:
- Add qlcnic_sriov_free_vlans() if qlcnic_sriov_alloc_vlans() fails.
- Modify the patch description.
vf_info was allocated by kcalloc, no need to do more checks cause
kfree(NULL) is safe. Thanks, Paolo! 
---
 drivers/net/ethernet/qlogic/qlcnic/qlcnic_sriov_common.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

Comments

Paolo Abeni March 6, 2025, 10:32 a.m. UTC | #1
On 3/5/25 11:09 AM, Haoxiang Li wrote:
> Add qlcnic_sriov_free_vlans() to free the memory allocated by
> qlcnic_sriov_alloc_vlans() if qlcnic_sriov_alloc_vlans() fails
> or "sriov->allowed_vlans" fails to be allocated.
> 
> Fixes: 91b7282b613d ("qlcnic: Support VLAN id config.")
> Cc: stable@vger.kernel.org
> Signed-off-by: Haoxiang Li <haoxiang_li2024@163.com>
> ---
> Changes in v2:
> - Add qlcnic_sriov_free_vlans() if qlcnic_sriov_alloc_vlans() fails.
> - Modify the patch description.
> vf_info was allocated by kcalloc, no need to do more checks cause
> kfree(NULL) is safe. Thanks, Paolo! 
> ---
>  drivers/net/ethernet/qlogic/qlcnic/qlcnic_sriov_common.c | 8 ++++++--
>  1 file changed, 6 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/net/ethernet/qlogic/qlcnic/qlcnic_sriov_common.c b/drivers/net/ethernet/qlogic/qlcnic/qlcnic_sriov_common.c
> index f9dd50152b1e..0dd9d7cb1de9 100644
> --- a/drivers/net/ethernet/qlogic/qlcnic/qlcnic_sriov_common.c
> +++ b/drivers/net/ethernet/qlogic/qlcnic/qlcnic_sriov_common.c
> @@ -446,16 +446,20 @@ static int qlcnic_sriov_set_guest_vlan_mode(struct qlcnic_adapter *adapter,
>  		 sriov->num_allowed_vlans);
>  
>  	ret = qlcnic_sriov_alloc_vlans(adapter);
> -	if (ret)
> +	if (ret) {
> +		qlcnic_sriov_free_vlans(adapter);

I'm sorry for the lack of clarity in my previous reply. I think it would
be better to do this cleanup inside qlcnic_sriov_alloc_vlans(), so that
on error it returns with no vlan allocated.

There is another caller of qlcnic_sriov_alloc_vlans() which AFAICS still
leak memory on error. Handling the deallocation in
qlcnic_sriov_alloc_vlans() will address even that caller.

Thanks,

Paolo
Paolo Abeni March 6, 2025, 10:34 a.m. UTC | #2
On 3/6/25 11:32 AM, Paolo Abeni wrote:
> On 3/5/25 11:09 AM, Haoxiang Li wrote:
>> Add qlcnic_sriov_free_vlans() to free the memory allocated by
>> qlcnic_sriov_alloc_vlans() if qlcnic_sriov_alloc_vlans() fails
>> or "sriov->allowed_vlans" fails to be allocated.
>>
>> Fixes: 91b7282b613d ("qlcnic: Support VLAN id config.")
>> Cc: stable@vger.kernel.org
>> Signed-off-by: Haoxiang Li <haoxiang_li2024@163.com>

Whoops, I forgot: please include the target tree name ('net' in this
case) in the subj prefix - in the next iteration should be 'PATCH net v3'.

Thanks,

Paolo
diff mbox series

Patch

diff --git a/drivers/net/ethernet/qlogic/qlcnic/qlcnic_sriov_common.c b/drivers/net/ethernet/qlogic/qlcnic/qlcnic_sriov_common.c
index f9dd50152b1e..0dd9d7cb1de9 100644
--- a/drivers/net/ethernet/qlogic/qlcnic/qlcnic_sriov_common.c
+++ b/drivers/net/ethernet/qlogic/qlcnic/qlcnic_sriov_common.c
@@ -446,16 +446,20 @@  static int qlcnic_sriov_set_guest_vlan_mode(struct qlcnic_adapter *adapter,
 		 sriov->num_allowed_vlans);
 
 	ret = qlcnic_sriov_alloc_vlans(adapter);
-	if (ret)
+	if (ret) {
+		qlcnic_sriov_free_vlans(adapter);
 		return ret;
+	}
 
 	if (!sriov->any_vlan)
 		return 0;
 
 	num_vlans = sriov->num_allowed_vlans;
 	sriov->allowed_vlans = kcalloc(num_vlans, sizeof(u16), GFP_KERNEL);
-	if (!sriov->allowed_vlans)
+	if (!sriov->allowed_vlans) {
+		qlcnic_sriov_free_vlans(adapter);
 		return -ENOMEM;
+	}
 
 	vlans = (u16 *)&cmd->rsp.arg[3];
 	for (i = 0; i < num_vlans; i++)