linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH net 1/2] net: lantiq_xrx200: confirm skb is allocated before using
@ 2022-07-17 19:48 Aleksander Jan Bajkowski
  2022-07-17 19:48 ` [PATCH net 2/2] net: lantiq_xrx200: fix return value in ENOMEM case Aleksander Jan Bajkowski
  0 siblings, 1 reply; 3+ messages in thread
From: Aleksander Jan Bajkowski @ 2022-07-17 19:48 UTC (permalink / raw)
  To: hauke, davem, edumazet, kuba, pabeni, olek2, netdev, linux-kernel

xrx200_hw_receive() assumes build_skb() always works and goes straight
to skb_reserve(). However, build_skb() can fail under memory pressure.

Add a check in case build_skb() failed to allocate and return NULL.

Fixes: e015593573b3 ("net: lantiq_xrx200: convert to build_skb ")
Reported-by: Eric Dumazet <edumazet@google.com>
Signed-off-by: Aleksander Jan Bajkowski <olek2@wp.pl>
---
 drivers/net/ethernet/lantiq_xrx200.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/drivers/net/ethernet/lantiq_xrx200.c b/drivers/net/ethernet/lantiq_xrx200.c
index 5edb68a8aab1..6a83a6c19484 100644
--- a/drivers/net/ethernet/lantiq_xrx200.c
+++ b/drivers/net/ethernet/lantiq_xrx200.c
@@ -239,6 +239,13 @@ static int xrx200_hw_receive(struct xrx200_chan *ch)
 	}
 
 	skb = build_skb(buf, priv->rx_skb_size);
+	if (!skb) {
+		skb_free_frag(buf);
+		net_dev->stats.rx_dropped++;
+		netdev_err(net_dev, "failed to build skb\n");
+		return -ENOMEM;
+	}
+
 	skb_reserve(skb, NET_SKB_PAD);
 	skb_put(skb, len);
 
-- 
2.30.2


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

* [PATCH net 2/2] net: lantiq_xrx200: fix return value in ENOMEM case
  2022-07-17 19:48 [PATCH net 1/2] net: lantiq_xrx200: confirm skb is allocated before using Aleksander Jan Bajkowski
@ 2022-07-17 19:48 ` Aleksander Jan Bajkowski
  2022-07-19 11:31   ` Paolo Abeni
  0 siblings, 1 reply; 3+ messages in thread
From: Aleksander Jan Bajkowski @ 2022-07-17 19:48 UTC (permalink / raw)
  To: hauke, davem, edumazet, kuba, pabeni, olek2, netdev, linux-kernel

The xrx200_hw_receive() function can return:
* XRX200_DMA_PACKET_IN_PROGRESS (the next descriptor contains the
further part of the packet),
* XRX200_DMA_PACKET_COMPLETE (a complete packet has been received),
* -ENOMEM (a memory allocation error occurred).

Currently, the third of these cases is incorrectly handled. The NAPI
poll function returns then a negative value (-ENOMEM). Correctly in
such a situation, the driver should try to receive next packet in
the hope that this time the memory allocation for the next descriptor
will succeed.

This patch replaces the XRX200_DMA_PACKET_IN_PROGRESS definition with
-EINPROGRESS to simplify the driver.

Fixes: c3e6b2c35b34 ("net: lantiq_xrx200: add ingress SG DMA support")
Signed-off-by: Aleksander Jan Bajkowski <olek2@wp.pl>
---
 drivers/net/ethernet/lantiq_xrx200.c | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)

diff --git a/drivers/net/ethernet/lantiq_xrx200.c b/drivers/net/ethernet/lantiq_xrx200.c
index 6a83a6c19484..2865d07f3fc8 100644
--- a/drivers/net/ethernet/lantiq_xrx200.c
+++ b/drivers/net/ethernet/lantiq_xrx200.c
@@ -27,9 +27,6 @@
 #define XRX200_DMA_TX		1
 #define XRX200_DMA_BURST_LEN	8
 
-#define XRX200_DMA_PACKET_COMPLETE	0
-#define XRX200_DMA_PACKET_IN_PROGRESS	1
-
 /* cpu port mac */
 #define PMAC_RX_IPG		0x0024
 #define PMAC_RX_IPG_MASK	0xf
@@ -272,9 +269,8 @@ static int xrx200_hw_receive(struct xrx200_chan *ch)
 		netif_receive_skb(ch->skb_head);
 		ch->skb_head = NULL;
 		ch->skb_tail = NULL;
-		ret = XRX200_DMA_PACKET_COMPLETE;
 	} else {
-		ret = XRX200_DMA_PACKET_IN_PROGRESS;
+		ret = -EINPROGRESS;
 	}
 
 	return ret;
@@ -292,10 +288,8 @@ static int xrx200_poll_rx(struct napi_struct *napi, int budget)
 
 		if ((desc->ctl & (LTQ_DMA_OWN | LTQ_DMA_C)) == LTQ_DMA_C) {
 			ret = xrx200_hw_receive(ch);
-			if (ret == XRX200_DMA_PACKET_IN_PROGRESS)
+			if (ret)
 				continue;
-			if (ret != XRX200_DMA_PACKET_COMPLETE)
-				return ret;
 			rx++;
 		} else {
 			break;
-- 
2.30.2


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

* Re: [PATCH net 2/2] net: lantiq_xrx200: fix return value in ENOMEM case
  2022-07-17 19:48 ` [PATCH net 2/2] net: lantiq_xrx200: fix return value in ENOMEM case Aleksander Jan Bajkowski
@ 2022-07-19 11:31   ` Paolo Abeni
  0 siblings, 0 replies; 3+ messages in thread
From: Paolo Abeni @ 2022-07-19 11:31 UTC (permalink / raw)
  To: Aleksander Jan Bajkowski, hauke, davem, edumazet, kuba, netdev,
	linux-kernel

On Sun, 2022-07-17 at 21:48 +0200, Aleksander Jan Bajkowski wrote:
> The xrx200_hw_receive() function can return:
> * XRX200_DMA_PACKET_IN_PROGRESS (the next descriptor contains the
> further part of the packet),
> * XRX200_DMA_PACKET_COMPLETE (a complete packet has been received),
> * -ENOMEM (a memory allocation error occurred).
> 
> Currently, the third of these cases is incorrectly handled. The NAPI
> poll function returns then a negative value (-ENOMEM). Correctly in
> such a situation, the driver should try to receive next packet in
> the hope that this time the memory allocation for the next descriptor
> will succeed.

> This patch replaces the XRX200_DMA_PACKET_IN_PROGRESS definition with
> -EINPROGRESS to simplify the driver.
> 
> Fixes: c3e6b2c35b34 ("net: lantiq_xrx200: add ingress SG DMA support")
> Signed-off-by: Aleksander Jan Bajkowski <olek2@wp.pl>
> ---
>  drivers/net/ethernet/lantiq_xrx200.c | 10 ++--------
>  1 file changed, 2 insertions(+), 8 deletions(-)
> 
> diff --git a/drivers/net/ethernet/lantiq_xrx200.c b/drivers/net/ethernet/lantiq_xrx200.c
> index 6a83a6c19484..2865d07f3fc8 100644
> --- a/drivers/net/ethernet/lantiq_xrx200.c
> +++ b/drivers/net/ethernet/lantiq_xrx200.c
> @@ -27,9 +27,6 @@
>  #define XRX200_DMA_TX		1
>  #define XRX200_DMA_BURST_LEN	8
>  
> -#define XRX200_DMA_PACKET_COMPLETE	0
> -#define XRX200_DMA_PACKET_IN_PROGRESS	1
> -
>  /* cpu port mac */
>  #define PMAC_RX_IPG		0x0024
>  #define PMAC_RX_IPG_MASK	0xf
> @@ -272,9 +269,8 @@ static int xrx200_hw_receive(struct xrx200_chan *ch)
>  		netif_receive_skb(ch->skb_head);
>  		ch->skb_head = NULL;
>  		ch->skb_tail = NULL;
> -		ret = XRX200_DMA_PACKET_COMPLETE;
>  	} else {
> -		ret = XRX200_DMA_PACKET_IN_PROGRESS;
> +		ret = -EINPROGRESS;
>  	}
>  
>  	return ret;
> @@ -292,10 +288,8 @@ static int xrx200_poll_rx(struct napi_struct *napi, int budget)
>  
>  		if ((desc->ctl & (LTQ_DMA_OWN | LTQ_DMA_C)) == LTQ_DMA_C) {
>  			ret = xrx200_hw_receive(ch);
> -			if (ret == XRX200_DMA_PACKET_IN_PROGRESS)
> +			if (ret)
>  				continue;
> -			if (ret != XRX200_DMA_PACKET_COMPLETE)
> -				return ret;

Note that in case of persistent pressure and with the device under
flood, 'rx' is not incremented, and this loop can run for an unbound
number of iteration.

If you keep running, you do need to increment 'rx' at every iteration,
even in case of error.

Note that 'rx' is more an estimate of the work done than a 'received
packet counter'.

Paolo


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

end of thread, other threads:[~2022-07-19 11:31 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-17 19:48 [PATCH net 1/2] net: lantiq_xrx200: confirm skb is allocated before using Aleksander Jan Bajkowski
2022-07-17 19:48 ` [PATCH net 2/2] net: lantiq_xrx200: fix return value in ENOMEM case Aleksander Jan Bajkowski
2022-07-19 11:31   ` Paolo Abeni

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