pch_gbe: don't come up if we can't allocate buffers
Subsystems (2)
PATCH 1/1 pch_gbe: don't come up if we can't allocate buffers
Currently, even if pch_gbe_alloc_tx/rx_buffers (even partially) fail, we bring the interface up. It mihgt confuse the user (if he requested specific amount of buffers) and can lead to different bugs.
Add error handling to these functions and clean after them in case of failure. It also resolves a possible NULL pointer dereference in pch_gbe_alloc_tx_buffers(), in case of netdev_alloc_skb() failure. It's ok to (paritally) fail for pch_gbe_alloc_rx_buffers() in some situation, so don't clean inside and rather handle skb freeing outside of the function.
This patch also adds tx_alloc_buff_failed ethtool counter.
--- drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe.h | 1 + .../ethernet/oki-semi/pch_gbe/pch_gbe_ethtool.c | 1 + .../net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c | 75 ++++++++++++++----- 3 files changed, 57 insertions(+), 20 deletions(-)
drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe.h +1 -0
@@ -572,6 +572,7 @@ struct pch_gbe_hw_stats {u32 tx_carrier_errors;u32 tx_timeout_count;u32 tx_restart_count; +u32 tx_alloc_buff_failed;u32 intr_rx_dsc_empty_count;u32 intr_rx_frame_err_count;u32 intr_rx_fifo_err_count;
drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_ethtool.c +1 -0
@@ -58,6 +58,7 @@ static const struct pch_gbe_stats pch_gbe_gstrings_stats[] = {PCH_GBE_STAT (tx_carrier_errors),PCH_GBE_STAT (tx_timeout_count),PCH_GBE_STAT (tx_restart_count), +PCH_GBE_STAT (tx_alloc_buff_failed),PCH_GBE_STAT (intr_rx_dsc_empty_count),PCH_GBE_STAT (intr_rx_frame_err_count), PCH_GBE_STAT(intr_rx_fifo_err_count),
drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c +55 -20
163 lines not on this page · raw message
Signature · 1 line
1.7.1
Quoted 11 lines · Veaceslav Falico
Currently, even if pch_gbe_alloc_tx/rx_buffers (even partially) fail, we bring the interface up. It mihgt confuse the user (if he requested specific amount of buffers) and can lead to different bugs.
Add error handling to these functions and clean after them in case of failure. It also resolves a possible NULL pointer dereference in
pch_gbe_alloc_tx_buffers(), in case ofnetdev_alloc_skb()failure. It's ok to (paritally) fail forpch_gbe_alloc_rx_buffers()in some situation, so don't clean inside and rather handle skb freeing outside of the function.This patch also adds
tx_alloc_buff_failedethtool counter.
This should not be needed :
Quoted 14 lines
drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c +2
-1-static void pch_gbe_alloc_tx_buffers (struct pch_gbe_adapter *adapter, +static int pch_gbe_alloc_tx_buffers (struct pch_gbe_adapter *adapter,struct pch_gbe_tx_ring *tx_ring) {struct pch_gbe_buffer *buffer_info;@@ -1506,6 +1521,7 @@ static void pch_gbe_alloc_tx_buffers(struct pch_gbe_adapter *adapter,unsigned int i;unsigned int bufsz;struct pch_gbe_tx_desc *tx_desc; +int err; bufsz = adapter->hw.mac.max_frame_size +PCH_GBE_DMA_ALIGN +NET_IP_ALIGN ;
drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c static void pch_gbe_alloc_tx_buffers(struct pch_gbe_adapter *adapter, · line 1536
Quoted 8 lines
drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c +5
-0@@ -1513,12 +1529,26 @@ static void pch_gbe_alloc_tx_buffers(struct pch_gbe_adapter *adapter,for (i =0 ; i < tx_ring->count; i++) { buffer_info = &tx_ring->buffer_info[i]; skb =netdev_alloc_skb (adapter->netdev, bufsz); +if (unlikely (!skb)) { + adapter->stats.tx_alloc_buff_failed++; + err = -ENOMEM ; + goto freeall; + }
Hmmm
These skbs should be allocated using GFP_KERNEL, to lower risk of OOM, using a mere alloc_skb(bufsz, GFP_KERNEL)
Only skbs for rx 'need' netdev_alloc_skb() to add a NET_SKB_PAD headroom.
skb = alloc_skb(bufsz, GFP_KERNEL); if (!skb) goto fail; skb_reserve(skb, NET_IP_ALIGN);
And BTW, we dont really need skbs here, only a bounce buffer so that we can copy frames on it... (kmalloc() instead of alloc_skb())
drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c static void pch_gbe_alloc_tx_buffers(struct pch_gbe_adapter *adapter, · line 1537
Quoted 1 line
drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c +0
-0skb_reserve (skb,PCH_GBE_DMA_ALIGN );
PCH_GBE_DMA_ALIGN is zero ...
Quoted 7 lines
drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c +2
-1buffer_info->skb = skb; tx_desc =PCH_GBE_TX_DESC (*tx_ring, i); tx_desc->gbec_status = (DSC_INIT16 ); } -return ; +return 0 ; +
drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c static void pch_gbe_alloc_tx_buffers(struct pch_gbe_adapter *adapter, · line 1553
Quoted 10 lines
drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c +8
-0+freeall: +for (i--; i >=0 ; i--) { +dev_kfree_skb_any (buffer_info->skb); + buffer_info->skb =NULL ; + tx_desc =PCH_GBE_TX_DESC (*tx_ring, i); + tx_desc->gbec_status =0 ; + } +return err; }
Also I would call pch_gbe_alloc_tx_buffers() _after_ pch_gbe_alloc_rx_buffers() (Since rx allocations probably are using GFP_ATOMIC allocations)
Quoted 72 lines · Eric Dumazet
Quoted 11 lines · Veaceslav Falico
Currently, even if pch_gbe_alloc_tx/rx_buffers (even partially) fail, we bring the interface up. It mihgt confuse the user (if he requested specific amount of buffers) and can lead to different bugs.
Add error handling to these functions and clean after them in case of failure. It also resolves a possible NULL pointer dereference in
pch_gbe_alloc_tx_buffers(), in case ofnetdev_alloc_skb()failure. It's ok to (paritally) fail forpch_gbe_alloc_rx_buffers()in some situation, so don't clean inside and rather handle skb freeing outside of the function.This patch also adds
tx_alloc_buff_failedethtool counter.This should not be needed :
Quoted 14 lines
drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c +2
-1-static void pch_gbe_alloc_tx_buffers (struct pch_gbe_adapter *adapter, +static int pch_gbe_alloc_tx_buffers (struct pch_gbe_adapter *adapter,struct pch_gbe_tx_ring *tx_ring) {struct pch_gbe_buffer *buffer_info;@@ -1506,6 +1521,7 @@ static void pch_gbe_alloc_tx_buffers(struct pch_gbe_adapter *adapter,unsigned int i;unsigned int bufsz;struct pch_gbe_tx_desc *tx_desc; +int err; bufsz = adapter->hw.mac.max_frame_size +PCH_GBE_DMA_ALIGN +NET_IP_ALIGN ;drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c static void pch_gbe_alloc_tx_buffers(struct pch_gbe_adapter *adapter, · line 1536
Quoted 8 lines
drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c +5
-0@@ -1513,12 +1529,26 @@ static void pch_gbe_alloc_tx_buffers(struct pch_gbe_adapter *adapter,for (i =0 ; i < tx_ring->count; i++) { buffer_info = &tx_ring->buffer_info[i]; skb =netdev_alloc_skb (adapter->netdev, bufsz); +if (unlikely (!skb)) { + adapter->stats.tx_alloc_buff_failed++; + err = -ENOMEM ; + goto freeall; + }Hmmm
These skbs should be allocated using
GFP_KERNEL, to lower risk of OOM, using a merealloc_skb(bufsz, GFP_KERNEL)Only skbs for rx 'need'
netdev_alloc_skb()to add aNET_SKB_PADheadroom.skb = alloc_skb(bufsz, GFP_KERNEL); if (!skb) goto fail; skb_reserve(skb, NET_IP_ALIGN);And BTW, we dont really need skbs here, only a bounce buffer so that we can copy frames on it... (
kmalloc()instead ofalloc_skb())drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c static void pch_gbe_alloc_tx_buffers(struct pch_gbe_adapter *adapter, · line 1537
Quoted 1 line
drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c +0
-0skb_reserve (skb,PCH_GBE_DMA_ALIGN );
PCH_GBE_DMA_ALIGNis zero ...Quoted 14 lines
drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c +9
-1buffer_info->skb = skb; tx_desc =PCH_GBE_TX_DESC (*tx_ring, i); tx_desc->gbec_status = (DSC_INIT16 ); } -return ; +return 0 ; + +freeall: +for (i--; i >=0 ; i--) { +dev_kfree_skb_any (buffer_info->skb); + buffer_info->skb =NULL ; + tx_desc =PCH_GBE_TX_DESC (*tx_ring, i); + tx_desc->gbec_status =0 ; + }
Quoted 8 lines
drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c pch_gbe_alloc_rx_buffers(struct pch_gbe_adapter *adapter, · line 1480
Quoted 3 lines
drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c +1
-0+return err; }Also I would call
pch_gbe_alloc_tx_buffers()_after_pch_gbe_alloc_rx_buffers()(Since rx allocations probably are usingGFP_ATOMICallocations)
Agree with all, will update the patch.
Thank you very much for the review!