netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH net] net: nixge: fix potential memory leak in nixge_start_xmit()
@ 2022-11-14  8:55 Zhang Changzhong
  2022-11-14 10:40 ` Francois Romieu
  0 siblings, 1 reply; 5+ messages in thread
From: Zhang Changzhong @ 2022-11-14  8:55 UTC (permalink / raw)
  To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Moritz Fischer
  Cc: Zhang Changzhong, netdev, linux-kernel

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(-)

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;
-- 
2.31.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH net] net: nixge: fix potential memory leak in nixge_start_xmit()
  2022-11-14  8:55 [PATCH net] net: nixge: fix potential memory leak in nixge_start_xmit() Zhang Changzhong
@ 2022-11-14 10:40 ` Francois Romieu
  2022-11-15 13:20   ` Zhang Changzhong
  0 siblings, 1 reply; 5+ messages in thread
From: Francois Romieu @ 2022-11-14 10:40 UTC (permalink / raw)
  To: Zhang Changzhong
  Cc: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Moritz Fischer, netdev, linux-kernel

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(

-- 
Ueimor

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH net] net: nixge: fix potential memory leak in nixge_start_xmit()
  2022-11-14 10:40 ` Francois Romieu
@ 2022-11-15 13:20   ` Zhang Changzhong
  2022-11-16 22:36     ` Saeed Mahameed
  0 siblings, 1 reply; 5+ messages in thread
From: Zhang Changzhong @ 2022-11-15 13:20 UTC (permalink / raw)
  To: Francois Romieu
  Cc: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Moritz Fischer, netdev, linux-kernel

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH net] net: nixge: fix potential memory leak in nixge_start_xmit()
  2022-11-15 13:20   ` Zhang Changzhong
@ 2022-11-16 22:36     ` Saeed Mahameed
  2022-11-17  3:04       ` Zhang Changzhong
  0 siblings, 1 reply; 5+ messages in thread
From: Saeed Mahameed @ 2022-11-16 22:36 UTC (permalink / raw)
  To: Zhang Changzhong
  Cc: Francois Romieu, David S. Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Moritz Fischer, netdev, linux-kernel

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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH net] net: nixge: fix potential memory leak in nixge_start_xmit()
  2022-11-16 22:36     ` Saeed Mahameed
@ 2022-11-17  3:04       ` Zhang Changzhong
  0 siblings, 0 replies; 5+ messages in thread
From: Zhang Changzhong @ 2022-11-17  3:04 UTC (permalink / raw)
  To: Saeed Mahameed
  Cc: Francois Romieu, David S. Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Moritz Fischer, netdev, linux-kernel

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.

> .

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-11-17  3:04 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-14  8:55 [PATCH net] net: nixge: fix potential memory leak in nixge_start_xmit() Zhang Changzhong
2022-11-14 10:40 ` Francois Romieu
2022-11-15 13:20   ` Zhang Changzhong
2022-11-16 22:36     ` Saeed Mahameed
2022-11-17  3:04       ` Zhang Changzhong

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).