net: arc_emac: fix arc_emac_rx() error paths
Subsystems (2)
PATCH 1/1 net: arc_emac: fix arc_emac_rx() error paths
arc_emac_rx() has some issues found by code review.
In case netdev_alloc_skb_ip_align() or dma_map_single() failure rx fifo entry will not be returned to EMAC.
In case dma_map_single() failure previously allocated skb became lost to driver. At the same time address of newly allocated skb will not be provided to EMAC.
--- drivers/net/ethernet/arc/emac_main.c | 53 ++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 22 deletions(-)
drivers/net/ethernet/arc/emac_main.c +31 -22
@@ -212,39 +212,48 @@ static int arc_emac_rx(struct net_device *ndev, int budget)continue ; } - pktlen = info &LEN_MASK ; - stats->rx_packets++; - stats->rx_bytes += pktlen; - skb = rx_buff->skb; -skb_put (skb, pktlen); - skb->dev = ndev; - skb->protocol =eth_type_trans (skb, ndev); - -dma_unmap_single (&ndev->dev,dma_unmap_addr (rx_buff, addr), -dma_unmap_len (rx_buff, len),DMA_FROM_DEVICE ); - -/* Prepare the BD for next cycle */ - rx_buff->skb =netdev_alloc_skb_ip_align (ndev, -EMAC_BUFFER_SIZE ); -if (unlikely (!rx_buff->skb)) { +/* Prepare the BD for next cycle. netif_receive_skb() +* only if new skb was allocated and mapped to avoid holes +* in the RX fifo. +*/ + skb =netdev_alloc_skb_ip_align (ndev,EMAC_BUFFER_SIZE ); +if (unlikely (!skb)) { +if (net_ratelimit ()) +netdev_err (ndev,"cannot allocate skb\n" ); +/* Return ownership to EMAC */ + rxbd->info =cpu_to_le32 (FOR_EMAC |EMAC_BUFFER_SIZE ); stats->rx_errors++; -/* Because receive_skb is below, increment rx_dropped */ stats->rx_dropped++;continue ; } -/* receive_skb only if new skb was allocated to avoid holes */ -netif_receive_skb (skb); - - addr =dma_map_single (&ndev->dev, (void *)rx_buff->skb->data, + addr =dma_map_single (&ndev->dev, (void *)skb->data,EMAC_BUFFER_SIZE ,DMA_FROM_DEVICE );if (dma_mapping_error (&ndev->dev, addr)) {if (net_ratelimit ()) -netdev_err (ndev,"cannot dma map\n" ); -dev_kfree_skb (rx_buff->skb); +netdev_err (ndev,"cannot map dma buffer\n" ); +dev_kfree_skb (skb); +/* Return ownership to EMAC */ + rxbd->info =cpu_to_le32 (FOR_EMAC |EMAC_BUFFER_SIZE ); stats->rx_errors++; + stats->rx_dropped++;continue ; } + +/* unmap previosly mapped skb */ +dma_unmap_single (&ndev->dev,dma_unmap_addr (rx_buff, addr), +dma_unmap_len (rx_buff, len),DMA_FROM_DEVICE ); + + pktlen = info &LEN_MASK ; + stats->rx_packets++; + stats->rx_bytes += pktlen; +skb_put (rx_buff->skb, pktlen); + rx_buff->skb->dev = ndev; + rx_buff->skb->protocol =eth_type_trans (rx_buff->skb, ndev); + +netif_receive_skb (rx_buff->skb); + + rx_buff->skb = skb;dma_unmap_addr_set (rx_buff, addr, addr);dma_unmap_len_set (rx_buff, len,EMAC_BUFFER_SIZE );
Signature · 1 line
1.7.9.5
PATCH v2 1/1 net: arc_emac: restart stalled EMAC
Under certain conditions EMAC stop reception of incoming packets and continuously increment R_MISS register instead of saving data into provided buffer. The commit implement workaround for such situation. Then the stall detected EMAC will be restarted.
On device the stall looks like the device lost it's dynamic IP address. ifconfig shows that interface error counter rapidly increments. At the same time on the DHCP server we can see continues DHCP-requests from device.
In real network stalls happen really rarely. To make them frequent the broadcast storm[1] should be simulated. For simulation it is necessary to make following connections:
1. connect radxarock to 1st port of switch
2. connect some PC to 2nd port of switch
3. connect two other free ports together using standard ethernet cable, in order to make a switching loop.
After that, is necessary to make a broadcast storm. For example, running on PC 'ping' to some IP address triggers ARP-request storm. After some time (~10sec), EMAC on rk3188 will stall.
Observed and tested on rk3188 radxarock.
[1] https://en.wikipedia.org/wiki/Broadcast_radiation
---
Changes in v2:
- Rebased against stable linux-4.14.y branch
drivers/net/ethernet/arc/emac.h | 2 + drivers/net/ethernet/arc/emac_main.c | 111 ++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+)
drivers/net/ethernet/arc/emac.h +2 -0
@@ -159,6 +159,8 @@ struct arc_emac_priv {unsigned int link;unsigned int duplex;unsigned int speed; + +unsigned int rx_missed_errors; };/**
drivers/net/ethernet/arc/emac_main.c +111 -0
147 lines not on this page · raw message
Signature · 1 line
1.7.9.5
PATCH v2 1/1 net: arc_emac: fix arc_emac_rx() error paths
In reply to Alexander Kochetkovarc_emac_rx() has some issues found by code review.
In case netdev_alloc_skb_ip_align() or dma_map_single() failure rx fifo entry will not be returned to EMAC.
In case dma_map_single() failure previously allocated skb became lost to driver. At the same time address of newly allocated skb will not be provided to EMAC.
---
Changes in v2:
- Rebased against stable linux-4.14.y branch
drivers/net/ethernet/arc/emac_main.c | 53 ++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 22 deletions(-)
drivers/net/ethernet/arc/emac_main.c +31 -22
@@ -212,39 +212,48 @@ static int arc_emac_rx(struct net_device *ndev, int budget)continue ; } - pktlen = info &LEN_MASK ; - stats->rx_packets++; - stats->rx_bytes += pktlen; - skb = rx_buff->skb; -skb_put (skb, pktlen); - skb->dev = ndev; - skb->protocol =eth_type_trans (skb, ndev); - -dma_unmap_single (&ndev->dev,dma_unmap_addr (rx_buff, addr), -dma_unmap_len (rx_buff, len),DMA_FROM_DEVICE ); - -/* Prepare the BD for next cycle */ - rx_buff->skb =netdev_alloc_skb_ip_align (ndev, -EMAC_BUFFER_SIZE ); -if (unlikely (!rx_buff->skb)) { +/* Prepare the BD for next cycle. netif_receive_skb() +* only if new skb was allocated and mapped to avoid holes +* in the RX fifo. +*/ + skb =netdev_alloc_skb_ip_align (ndev,EMAC_BUFFER_SIZE ); +if (unlikely (!skb)) { +if (net_ratelimit ()) +netdev_err (ndev,"cannot allocate skb\n" ); +/* Return ownership to EMAC */ + rxbd->info =cpu_to_le32 (FOR_EMAC |EMAC_BUFFER_SIZE ); stats->rx_errors++; -/* Because receive_skb is below, increment rx_dropped */ stats->rx_dropped++;continue ; } -/* receive_skb only if new skb was allocated to avoid holes */ -netif_receive_skb (skb); - - addr =dma_map_single (&ndev->dev, (void *)rx_buff->skb->data, + addr =dma_map_single (&ndev->dev, (void *)skb->data,EMAC_BUFFER_SIZE ,DMA_FROM_DEVICE );if (dma_mapping_error (&ndev->dev, addr)) {if (net_ratelimit ()) -netdev_err (ndev,"cannot dma map\n" ); -dev_kfree_skb (rx_buff->skb); +netdev_err (ndev,"cannot map dma buffer\n" ); +dev_kfree_skb (skb); +/* Return ownership to EMAC */ + rxbd->info =cpu_to_le32 (FOR_EMAC |EMAC_BUFFER_SIZE ); stats->rx_errors++; + stats->rx_dropped++;continue ; } + +/* unmap previosly mapped skb */ +dma_unmap_single (&ndev->dev,dma_unmap_addr (rx_buff, addr), +dma_unmap_len (rx_buff, len),DMA_FROM_DEVICE ); + + pktlen = info &LEN_MASK ; + stats->rx_packets++; + stats->rx_bytes += pktlen; +skb_put (rx_buff->skb, pktlen); + rx_buff->skb->dev = ndev; + rx_buff->skb->protocol =eth_type_trans (rx_buff->skb, ndev); + +netif_receive_skb (rx_buff->skb); + + rx_buff->skb = skb;dma_unmap_addr_set (rx_buff, addr, addr);dma_unmap_len_set (rx_buff, len,EMAC_BUFFER_SIZE );
Signature · 1 line
1.7.9.5
David Miller
maintainerFrom: Alexander Kochetkov <[email protected]>
Date: Fri, 15 Dec 2017 20:20:06 +0300
Quoted 10 lines
arc_emac_rx()has some issues found by code review.In case
netdev_alloc_skb_ip_align()ordma_map_single()failure rx fifo entry will not be returned to EMAC.In case
dma_map_single()failure previously allocated skb became lost to driver. At the same time address of newly allocated skb will not be provided to EMAC.Signed-off-by Alexander Kochetkov <[email protected]>
This patch adds quite a few bugs, here is one which shows this is not functionally tested:
drivers/net/ethernet/arc/emac_main.c static int arc_emac_rx(struct net_device *ndev, int budget) · line 231
Quoted 6 lines
drivers/net/ethernet/arc/emac_main.c +1
-4-/* receive_skb only if new skb was allocated to avoid holes */ -netif_receive_skb (skb); - - addr =dma_map_single (&ndev->dev, (void *)rx_buff->skb->data, + addr =dma_map_single (&ndev->dev, (void *)skb->data,EMAC_BUFFER_SIZE ,DMA_FROM_DEVICE );
Map the new SKB.
Quoted 6 lines
drivers/net/ethernet/arc/emac_main.c +2
-2if (dma_mapping_error (&ndev->dev, addr)) {if (net_ratelimit ()) -netdev_err (ndev,"cannot dma map\n" ); -dev_kfree_skb (rx_buff->skb); +netdev_err (ndev,"cannot map dma buffer\n" ); +dev_kfree_skb (skb);
drivers/net/ethernet/arc/emac_main.c static int arc_emac_rx(struct net_device *ndev, int budget) · line 245
Quoted 10 lines
drivers/net/ethernet/arc/emac_main.c +7
-0+/* Return ownership to EMAC */ + rxbd->info =cpu_to_le32 (FOR_EMAC |EMAC_BUFFER_SIZE ); stats->rx_errors++; + stats->rx_dropped++;continue ; } + +/* unmap previosly mapped skb */ +dma_unmap_single (&ndev->dev,dma_unmap_addr (rx_buff, addr), +dma_unmap_len (rx_buff, len),DMA_FROM_DEVICE );
And then you unmap it. "addr" is the new DMA mapping, not the old one.
Quoted 11 lines
19 дек. 2017 г., в 18:22, David Miller <[email protected]> написал(а):
From: Alexander Kochetkov <[email protected]>
Date: Fri, 15 Dec 2017 20:20:06 +0300Quoted 5 lines
arc_emac_rx()has some issues found by code review.In case
netdev_alloc_skb_ip_align()ordma_map_single()failure rx fifo entry will not be returned to EMAC.
Quoted 8 lines
Quoted 5 lines
In case
dma_map_single()failure previously allocated skb became lost to driver. At the same time address of newly allocated skb will not be provided to EMAC.Signed-off-by Alexander Kochetkov <[email protected]>This patch adds quite a few bugs, here is one which shows this is not functionally tested:
May be I don’t understand correctly, but I don’t see bug here.
Wrong dma mapping usage should immediately manifest itself (kernel instability, koops and so on). The patch was tested on rk3188 and work fine for me. Also I did simulations of netdev_alloc_skb_ip_align() and dma_map_single() faults and can confirm that new error handling work.
Could someone else with ARC EMAC test the patch? I would be very grateful for the help. Florian or Eric, can you test it on your hardware?
Quoted 6 lines
drivers/net/ethernet/arc/emac_main.c static int arc_emac_rx(struct net_device *ndev, int budget) · line 245
Quoted 4 lines
drivers/net/ethernet/arc/emac_main.c +4
-0+ +/* unmap previosly mapped skb */ +dma_unmap_single (&ndev->dev,dma_unmap_addr (rx_buff, addr), +dma_unmap_len (rx_buff, len),DMA_FROM_DEVICE );And then you unmap it. "addr" is the new DMA mapping, not the old one.
The old mapping should be unmapped here. It refer to old skb what contains already received packet. Because buffer doesn’t belong to EMAC anymore.
That old mapping point to old skb buffer. And netif_receive_skb() will be called for old skb after that.
The new mapping «addr" will be unmapped then EMAC receive
new packet. Later by the code (after netif_receive_skb()) there are lines for saving «addr» value:
rx_buff->skb = skb;
dma_unmap_addr_set(rx_buff, addr, addr);
dma_unmap_len_set(rx_buff, len, EMAC_BUFFER_SIZE);Regards,
Alexander.
David Miller
maintainerFrom: Alexander Kochetkov <[email protected]>
Date: Tue, 19 Dec 2017 18:49:48 +0300
Quoted 6 lines
Quoted 1 line
And then you unmap it. "addr" is the new DMA mapping, not the old one.
The old mapping should be unmapped here. It refer to old skb what contains already received packet. Because buffer doesn’t belong to EMAC anymore.
That old mapping point to old skb buffer. Andnetif_receive_skb()will be called for old skb after that.
I misread the code, sorry. I saw the dma_unmap_addr(xxx, addr) and just considered 'addr' as the local variable. It's not, it's in fact the TX struct member.
Let me look at this some more. ;-)
Re: [PATCH v2] net: arc_emac: restart stalled EMAC
In reply to Alexander KochetkovFrom: Alexander Kochetkov <[email protected]>
Date: Tue, 19 Dec 2017 14:03:57 +0300
Quoted 22 lines
Under certain conditions EMAC stop reception of incoming packets and continuously increment
R_MISSregister instead of saving data into provided buffer. The commit implement workaround for such situation. Then the stall detected EMAC will be restarted.On device the stall looks like the device lost it's dynamic IP address. ifconfig shows that interface error counter rapidly increments. At the same time on the DHCP server we can see continues DHCP-requests from device.
In real network stalls happen really rarely. To make them frequent the broadcast storm[1] should be simulated. For simulation it is necessary to make following connections:
1. connect radxarock to 1st port of switch
2. connect some PC to 2nd port of switch
3. connect two other free ports together using standard ethernet cable, in order to make a switching loop.After that, is necessary to make a broadcast storm. For example, running on PC 'ping' to some IP address triggers ARP-request storm. After some time (~10sec), EMAC on rk3188 will stall.
Quoted 5 lines
Observed and tested on rk3188 radxarock.
[1] https://en.wikipedia.org/wiki/Broadcast_radiation
Signed-off-by Alexander Kochetkov <[email protected]>
Applied.
From: Alexander Kochetkov <[email protected]>
Date: Tue, 19 Dec 2017 14:07:04 +0300
Quoted 10 lines
arc_emac_rx()has some issues found by code review.In case
netdev_alloc_skb_ip_align()ordma_map_single()failure rx fifo entry will not be returned to EMAC.In case
dma_map_single()failure previously allocated skb became lost to driver. At the same time address of newly allocated skb will not be provided to EMAC.Signed-off-by Alexander Kochetkov <[email protected]>
Applied.
Quoted 22 lines · Alexander Kochetkov
Quoted 11 lines
19 дек. 2017 г., в 18:22, David Miller <[email protected]> написал(а):
From: Alexander Kochetkov <[email protected]>
Date: Fri, 15 Dec 2017 20:20:06 +0300Quoted 5 lines
arc_emac_rx()has some issues found by code review.In case
netdev_alloc_skb_ip_align()ordma_map_single()failure rx fifo entry will not be returned to EMAC.Quoted 8 lines
Quoted 5 lines
In case
dma_map_single()failure previously allocated skb became lost to driver. At the same time address of newly allocated skb will not be provided to EMAC.Signed-off-by Alexander Kochetkov <[email protected]>This patch adds quite a few bugs, here is one which shows this is not functionally tested:
May be I don’t understand correctly, but I don’t see bug here.
Quoted 7 lines
Wrong dma mapping usage should immediately manifest itself (kernel instability, koops and so on). The patch was tested on rk3188 and work fine for me. Also I did simulations of
netdev_alloc_skb_ip_align()anddma_map_single()faults and can confirm that new error handling work.Could someone else with ARC EMAC test the patch? I would be very grateful for the help. Florian or Eric, can you test it on your hardware?
I don't actually have access to this hardware, the only change I did to this driver was to use a standard Device Tree property :)
Signature · 1 line
Florian