diff mbox series

[net] net: nixge: fix potential memory leak in nixge_start_xmit()

Message ID 1668416136-33530-1-git-send-email-zhangchangzhong@huawei.com (mailing list archive)
State Changes Requested
Delegated to: Netdev Maintainers
Headers show
Series [net] net: nixge: fix potential memory leak in nixge_start_xmit() | expand

Checks

Context Check Description
netdev/tree_selection success Clearly marked for net
netdev/fixes_present success Fixes tag present in non-next series
netdev/subject_prefix success Link
netdev/cover_letter success Single patches do not need cover letters
netdev/patch_count success Link
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/cc_maintainers success CCed 7 of 7 maintainers
netdev/build_clang success Errors and warnings before: 2 this patch: 2
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
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, 17 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Zhang Changzhong Nov. 14, 2022, 8:55 a.m. UTC
The nixge_start_xmit() returns NETDEV_TX_OK but does not free skb on two
error handling cases, which can lead to memory leak.

To fix this, return NETDEV_TX_BUSY in case of nixge_check_tx_bd_space()
fails and add dev_kfree_skb_any() in case of dma_map_single() fails.

Fixes: 492caffa8a1a ("net: ethernet: nixge: Add support for National Instruments XGE netdev")
Signed-off-by: Zhang Changzhong <zhangchangzhong@huawei.com>
---
 drivers/net/ethernet/ni/nixge.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

Comments

Francois Romieu Nov. 14, 2022, 10:40 a.m. UTC | #1
Zhang Changzhong <zhangchangzhong@huawei.com> :
> The nixge_start_xmit() returns NETDEV_TX_OK but does not free skb on two
> error handling cases, which can lead to memory leak.
> 
> To fix this, return NETDEV_TX_BUSY in case of nixge_check_tx_bd_space()
> fails and add dev_kfree_skb_any() in case of dma_map_single() fails.

This patch merge two unrelated changes. Please split.

> Fixes: 492caffa8a1a ("net: ethernet: nixge: Add support for National Instruments XGE netdev")
> Signed-off-by: Zhang Changzhong <zhangchangzhong@huawei.com>
> ---
>  drivers/net/ethernet/ni/nixge.c | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/net/ethernet/ni/nixge.c b/drivers/net/ethernet/ni/nixge.c
> index 19d043b593cc..b9091f9bbc77 100644
> --- a/drivers/net/ethernet/ni/nixge.c
> +++ b/drivers/net/ethernet/ni/nixge.c
> @@ -521,13 +521,15 @@ static netdev_tx_t nixge_start_xmit(struct sk_buff *skb,
>  	if (nixge_check_tx_bd_space(priv, num_frag)) {
>  		if (!netif_queue_stopped(ndev))
>  			netif_stop_queue(ndev);
> -		return NETDEV_TX_OK;
> +		return NETDEV_TX_BUSY;
>  	}

The driver should probably check the available room before returning
from hard_start_xmit and turn the check above unlikely().

Btw there is no lock and the Tx completion is irq driven: the driver
is racy. :o(
Zhang Changzhong Nov. 15, 2022, 1:20 p.m. UTC | #2
On 2022/11/14 18:40, Francois Romieu wrote:
> Zhang Changzhong <zhangchangzhong@huawei.com> :
>> The nixge_start_xmit() returns NETDEV_TX_OK but does not free skb on two
>> error handling cases, which can lead to memory leak.
>>
>> To fix this, return NETDEV_TX_BUSY in case of nixge_check_tx_bd_space()
>> fails and add dev_kfree_skb_any() in case of dma_map_single() fails.
> 
> This patch merge two unrelated changes. Please split.
> 
>> Fixes: 492caffa8a1a ("net: ethernet: nixge: Add support for National Instruments XGE netdev")
>> Signed-off-by: Zhang Changzhong <zhangchangzhong@huawei.com>
>> ---
>>  drivers/net/ethernet/ni/nixge.c | 6 ++++--
>>  1 file changed, 4 insertions(+), 2 deletions(-)
>>
>> diff --git a/drivers/net/ethernet/ni/nixge.c b/drivers/net/ethernet/ni/nixge.c
>> index 19d043b593cc..b9091f9bbc77 100644
>> --- a/drivers/net/ethernet/ni/nixge.c
>> +++ b/drivers/net/ethernet/ni/nixge.c
>> @@ -521,13 +521,15 @@ static netdev_tx_t nixge_start_xmit(struct sk_buff *skb,
>>  	if (nixge_check_tx_bd_space(priv, num_frag)) {
>>  		if (!netif_queue_stopped(ndev))
>>  			netif_stop_queue(ndev);
>> -		return NETDEV_TX_OK;
>> +		return NETDEV_TX_BUSY;
>>  	}
> 
> The driver should probably check the available room before returning
> from hard_start_xmit and turn the check above unlikely().
> 
> Btw there is no lock and the Tx completion is irq driven: the driver
> is racy. :o(
> 

Hi Francois,

Thanks for you review. I'll make v2 according to your suggestion.

Changzhong
Saeed Mahameed Nov. 16, 2022, 10:36 p.m. UTC | #3
On 15 Nov 21:20, Zhang Changzhong wrote:
>On 2022/11/14 18:40, Francois Romieu wrote:
>> Zhang Changzhong <zhangchangzhong@huawei.com> :
>>> The nixge_start_xmit() returns NETDEV_TX_OK but does not free skb on two
>>> error handling cases, which can lead to memory leak.
>>>
>>> To fix this, return NETDEV_TX_BUSY in case of nixge_check_tx_bd_space()
>>> fails and add dev_kfree_skb_any() in case of dma_map_single() fails.
>>
>> This patch merge two unrelated changes. Please split.
>>
>>> Fixes: 492caffa8a1a ("net: ethernet: nixge: Add support for National Instruments XGE netdev")
>>> Signed-off-by: Zhang Changzhong <zhangchangzhong@huawei.com>
>>> ---
>>>  drivers/net/ethernet/ni/nixge.c | 6 ++++--
>>>  1 file changed, 4 insertions(+), 2 deletions(-)
>>>
>>> diff --git a/drivers/net/ethernet/ni/nixge.c b/drivers/net/ethernet/ni/nixge.c
>>> index 19d043b593cc..b9091f9bbc77 100644
>>> --- a/drivers/net/ethernet/ni/nixge.c
>>> +++ b/drivers/net/ethernet/ni/nixge.c
>>> @@ -521,13 +521,15 @@ static netdev_tx_t nixge_start_xmit(struct sk_buff *skb,
>>>  	if (nixge_check_tx_bd_space(priv, num_frag)) {
>>>  		if (!netif_queue_stopped(ndev))
>>>  			netif_stop_queue(ndev);
>>> -		return NETDEV_TX_OK;
>>> +		return NETDEV_TX_BUSY;
>>>  	}
>>
>> The driver should probably check the available room before returning
>> from hard_start_xmit and turn the check above unlikely().
>>
>> Btw there is no lock and the Tx completion is irq driven: the driver
>> is racy. :o(
>>
>
>Hi Francois,
>
>Thanks for you review. I'll make v2 according to your suggestion.
>

you will probably need to check out: Transmit path guidelines:
https://www.kernel.org/doc/Documentation/networking/driver.rst
Zhang Changzhong Nov. 17, 2022, 3:04 a.m. UTC | #4
On 2022/11/17 6:36, Saeed Mahameed wrote:
> On 15 Nov 21:20, Zhang Changzhong wrote:
>> On 2022/11/14 18:40, Francois Romieu wrote:
>>> Zhang Changzhong <zhangchangzhong@huawei.com> :
>>>> The nixge_start_xmit() returns NETDEV_TX_OK but does not free skb on two
>>>> error handling cases, which can lead to memory leak.
>>>>
>>>> To fix this, return NETDEV_TX_BUSY in case of nixge_check_tx_bd_space()
>>>> fails and add dev_kfree_skb_any() in case of dma_map_single() fails.
>>>
>>> This patch merge two unrelated changes. Please split.
>>>
>>>> Fixes: 492caffa8a1a ("net: ethernet: nixge: Add support for National Instruments XGE netdev")
>>>> Signed-off-by: Zhang Changzhong <zhangchangzhong@huawei.com>
>>>> ---
>>>>  drivers/net/ethernet/ni/nixge.c | 6 ++++--
>>>>  1 file changed, 4 insertions(+), 2 deletions(-)
>>>>
>>>> diff --git a/drivers/net/ethernet/ni/nixge.c b/drivers/net/ethernet/ni/nixge.c
>>>> index 19d043b593cc..b9091f9bbc77 100644
>>>> --- a/drivers/net/ethernet/ni/nixge.c
>>>> +++ b/drivers/net/ethernet/ni/nixge.c
>>>> @@ -521,13 +521,15 @@ static netdev_tx_t nixge_start_xmit(struct sk_buff *skb,
>>>>      if (nixge_check_tx_bd_space(priv, num_frag)) {
>>>>          if (!netif_queue_stopped(ndev))
>>>>              netif_stop_queue(ndev);
>>>> -        return NETDEV_TX_OK;
>>>> +        return NETDEV_TX_BUSY;
>>>>      }
>>>
>>> The driver should probably check the available room before returning
>>> from hard_start_xmit and turn the check above unlikely().
>>>
>>> Btw there is no lock and the Tx completion is irq driven: the driver
>>> is racy. :o(
>>>
>>
>> Hi Francois,
>>
>> Thanks for you review. I'll make v2 according to your suggestion.
>>
> 
> you will probably need to check out: Transmit path guidelines:
> https://www.kernel.org/doc/Documentation/networking/driver.rst
> 

Thank! This document is very helpful.

> .
diff mbox series

Patch

diff --git a/drivers/net/ethernet/ni/nixge.c b/drivers/net/ethernet/ni/nixge.c
index 19d043b593cc..b9091f9bbc77 100644
--- a/drivers/net/ethernet/ni/nixge.c
+++ b/drivers/net/ethernet/ni/nixge.c
@@ -521,13 +521,15 @@  static netdev_tx_t nixge_start_xmit(struct sk_buff *skb,
 	if (nixge_check_tx_bd_space(priv, num_frag)) {
 		if (!netif_queue_stopped(ndev))
 			netif_stop_queue(ndev);
-		return NETDEV_TX_OK;
+		return NETDEV_TX_BUSY;
 	}
 
 	cur_phys = dma_map_single(ndev->dev.parent, skb->data,
 				  skb_headlen(skb), DMA_TO_DEVICE);
-	if (dma_mapping_error(ndev->dev.parent, cur_phys))
+	if (dma_mapping_error(ndev->dev.parent, cur_phys)) {
+		dev_kfree_skb_any(skb);
 		goto drop;
+	}
 	nixge_hw_dma_bd_set_phys(cur_p, cur_phys);
 
 	cur_p->cntrl = skb_headlen(skb) | XAXIDMA_BD_CTRL_TXSOF_MASK;