Commit f48bed16 authored by Doug Berger's avatar Doug Berger Committed by David S. Miller

net: bcmgenet: Free skb after last Tx frag

Since the skb is attached to the first control block of a fragmented
skb it is possible that the skb could be freed when reclaiming that
control block before all fragments of the skb have been consumed by
the hardware and unmapped.

This commit introduces first_cb and last_cb pointers to the skb
control block used by the driver to keep track of which transmit
control blocks within a transmit ring are the first and last ones
associated with the skb.

It then splits the bcmgenet_free_cb() function into transmit
(bcmgenet_free_tx_cb) and receive (bcmgenet_free_rx_cb) versions
that can handle the unmapping of dma mapped memory and cleaning up
the corresponding control block structure so that the skb is only
freed after the last associated transmit control block is reclaimed.

Fixes: 1c1008c7 ("net: bcmgenet: add main driver file")
Signed-off-by: default avatarDoug Berger <opendmb@gmail.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 876dbadd
...@@ -1219,14 +1219,6 @@ static struct enet_cb *bcmgenet_put_txcb(struct bcmgenet_priv *priv, ...@@ -1219,14 +1219,6 @@ static struct enet_cb *bcmgenet_put_txcb(struct bcmgenet_priv *priv,
return tx_cb_ptr; return tx_cb_ptr;
} }
/* Simple helper to free a control block's resources */
static void bcmgenet_free_cb(struct enet_cb *cb)
{
dev_kfree_skb_any(cb->skb);
cb->skb = NULL;
dma_unmap_addr_set(cb, dma_addr, 0);
}
static inline void bcmgenet_rx_ring16_int_disable(struct bcmgenet_rx_ring *ring) static inline void bcmgenet_rx_ring16_int_disable(struct bcmgenet_rx_ring *ring)
{ {
bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_RXDMA_DONE, bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_RXDMA_DONE,
...@@ -1277,18 +1269,72 @@ static inline void bcmgenet_tx_ring_int_disable(struct bcmgenet_tx_ring *ring) ...@@ -1277,18 +1269,72 @@ static inline void bcmgenet_tx_ring_int_disable(struct bcmgenet_tx_ring *ring)
INTRL2_CPU_MASK_SET); INTRL2_CPU_MASK_SET);
} }
/* Simple helper to free a transmit control block's resources
* Returns an skb when the last transmit control block associated with the
* skb is freed. The skb should be freed by the caller if necessary.
*/
static struct sk_buff *bcmgenet_free_tx_cb(struct device *dev,
struct enet_cb *cb)
{
struct sk_buff *skb;
skb = cb->skb;
if (skb) {
cb->skb = NULL;
if (cb == GENET_CB(skb)->first_cb)
dma_unmap_single(dev, dma_unmap_addr(cb, dma_addr),
dma_unmap_len(cb, dma_len),
DMA_TO_DEVICE);
else
dma_unmap_page(dev, dma_unmap_addr(cb, dma_addr),
dma_unmap_len(cb, dma_len),
DMA_TO_DEVICE);
dma_unmap_addr_set(cb, dma_addr, 0);
if (cb == GENET_CB(skb)->last_cb)
return skb;
} else if (dma_unmap_addr(cb, dma_addr)) {
dma_unmap_page(dev,
dma_unmap_addr(cb, dma_addr),
dma_unmap_len(cb, dma_len),
DMA_TO_DEVICE);
dma_unmap_addr_set(cb, dma_addr, 0);
}
return 0;
}
/* Simple helper to free a receive control block's resources */
static struct sk_buff *bcmgenet_free_rx_cb(struct device *dev,
struct enet_cb *cb)
{
struct sk_buff *skb;
skb = cb->skb;
cb->skb = NULL;
if (dma_unmap_addr(cb, dma_addr)) {
dma_unmap_single(dev, dma_unmap_addr(cb, dma_addr),
dma_unmap_len(cb, dma_len), DMA_FROM_DEVICE);
dma_unmap_addr_set(cb, dma_addr, 0);
}
return skb;
}
/* Unlocked version of the reclaim routine */ /* Unlocked version of the reclaim routine */
static unsigned int __bcmgenet_tx_reclaim(struct net_device *dev, static unsigned int __bcmgenet_tx_reclaim(struct net_device *dev,
struct bcmgenet_tx_ring *ring) struct bcmgenet_tx_ring *ring)
{ {
struct bcmgenet_priv *priv = netdev_priv(dev); struct bcmgenet_priv *priv = netdev_priv(dev);
struct device *kdev = &priv->pdev->dev; unsigned int txbds_processed = 0;
struct enet_cb *tx_cb_ptr;
unsigned int pkts_compl = 0;
unsigned int bytes_compl = 0; unsigned int bytes_compl = 0;
unsigned int c_index; unsigned int pkts_compl = 0;
unsigned int txbds_ready; unsigned int txbds_ready;
unsigned int txbds_processed = 0; unsigned int c_index;
struct sk_buff *skb;
/* Clear status before servicing to reduce spurious interrupts */ /* Clear status before servicing to reduce spurious interrupts */
if (ring->index == DESC_INDEX) if (ring->index == DESC_INDEX)
...@@ -1309,21 +1355,12 @@ static unsigned int __bcmgenet_tx_reclaim(struct net_device *dev, ...@@ -1309,21 +1355,12 @@ static unsigned int __bcmgenet_tx_reclaim(struct net_device *dev,
/* Reclaim transmitted buffers */ /* Reclaim transmitted buffers */
while (txbds_processed < txbds_ready) { while (txbds_processed < txbds_ready) {
tx_cb_ptr = &priv->tx_cbs[ring->clean_ptr]; skb = bcmgenet_free_tx_cb(&priv->pdev->dev,
if (tx_cb_ptr->skb) { &priv->tx_cbs[ring->clean_ptr]);
if (skb) {
pkts_compl++; pkts_compl++;
bytes_compl += GENET_CB(tx_cb_ptr->skb)->bytes_sent; bytes_compl += GENET_CB(skb)->bytes_sent;
dma_unmap_single(kdev, dev_kfree_skb_any(skb);
dma_unmap_addr(tx_cb_ptr, dma_addr),
dma_unmap_len(tx_cb_ptr, dma_len),
DMA_TO_DEVICE);
bcmgenet_free_cb(tx_cb_ptr);
} else if (dma_unmap_addr(tx_cb_ptr, dma_addr)) {
dma_unmap_page(kdev,
dma_unmap_addr(tx_cb_ptr, dma_addr),
dma_unmap_len(tx_cb_ptr, dma_len),
DMA_TO_DEVICE);
dma_unmap_addr_set(tx_cb_ptr, dma_addr, 0);
} }
txbds_processed++; txbds_processed++;
...@@ -1533,13 +1570,12 @@ static netdev_tx_t bcmgenet_xmit(struct sk_buff *skb, struct net_device *dev) ...@@ -1533,13 +1570,12 @@ static netdev_tx_t bcmgenet_xmit(struct sk_buff *skb, struct net_device *dev)
if (!i) { if (!i) {
/* Transmit single SKB or head of fragment list */ /* Transmit single SKB or head of fragment list */
tx_cb_ptr->skb = skb; GENET_CB(skb)->first_cb = tx_cb_ptr;
size = skb_headlen(skb); size = skb_headlen(skb);
mapping = dma_map_single(kdev, skb->data, size, mapping = dma_map_single(kdev, skb->data, size,
DMA_TO_DEVICE); DMA_TO_DEVICE);
} else { } else {
/* xmit fragment */ /* xmit fragment */
tx_cb_ptr->skb = NULL;
frag = &skb_shinfo(skb)->frags[i - 1]; frag = &skb_shinfo(skb)->frags[i - 1];
size = skb_frag_size(frag); size = skb_frag_size(frag);
mapping = skb_frag_dma_map(kdev, frag, 0, size, mapping = skb_frag_dma_map(kdev, frag, 0, size,
...@@ -1556,6 +1592,8 @@ static netdev_tx_t bcmgenet_xmit(struct sk_buff *skb, struct net_device *dev) ...@@ -1556,6 +1592,8 @@ static netdev_tx_t bcmgenet_xmit(struct sk_buff *skb, struct net_device *dev)
dma_unmap_addr_set(tx_cb_ptr, dma_addr, mapping); dma_unmap_addr_set(tx_cb_ptr, dma_addr, mapping);
dma_unmap_len_set(tx_cb_ptr, dma_len, size); dma_unmap_len_set(tx_cb_ptr, dma_len, size);
tx_cb_ptr->skb = skb;
len_stat = (size << DMA_BUFLENGTH_SHIFT) | len_stat = (size << DMA_BUFLENGTH_SHIFT) |
(priv->hw_params->qtag_mask << DMA_TX_QTAG_SHIFT); (priv->hw_params->qtag_mask << DMA_TX_QTAG_SHIFT);
...@@ -1570,6 +1608,7 @@ static netdev_tx_t bcmgenet_xmit(struct sk_buff *skb, struct net_device *dev) ...@@ -1570,6 +1608,7 @@ static netdev_tx_t bcmgenet_xmit(struct sk_buff *skb, struct net_device *dev)
dmadesc_set(priv, tx_cb_ptr->bd_addr, mapping, len_stat); dmadesc_set(priv, tx_cb_ptr->bd_addr, mapping, len_stat);
} }
GENET_CB(skb)->last_cb = tx_cb_ptr;
skb_tx_timestamp(skb); skb_tx_timestamp(skb);
/* Decrement total BD count and advance our write pointer */ /* Decrement total BD count and advance our write pointer */
...@@ -1598,18 +1637,7 @@ static netdev_tx_t bcmgenet_xmit(struct sk_buff *skb, struct net_device *dev) ...@@ -1598,18 +1637,7 @@ static netdev_tx_t bcmgenet_xmit(struct sk_buff *skb, struct net_device *dev)
/* Unmap successfully mapped control blocks */ /* Unmap successfully mapped control blocks */
while (i-- > 0) { while (i-- > 0) {
tx_cb_ptr = bcmgenet_put_txcb(priv, ring); tx_cb_ptr = bcmgenet_put_txcb(priv, ring);
if (tx_cb_ptr->skb) bcmgenet_free_tx_cb(kdev, tx_cb_ptr);
dma_unmap_single(kdev,
dma_unmap_addr(tx_cb_ptr, dma_addr),
dma_unmap_len(tx_cb_ptr, dma_len),
DMA_TO_DEVICE);
else
dma_unmap_page(kdev,
dma_unmap_addr(tx_cb_ptr, dma_addr),
dma_unmap_len(tx_cb_ptr, dma_len),
DMA_TO_DEVICE);
dma_unmap_addr_set(tx_cb_ptr, dma_addr, 0);
tx_cb_ptr->skb = NULL;
} }
dev_kfree_skb(skb); dev_kfree_skb(skb);
...@@ -1645,14 +1673,12 @@ static struct sk_buff *bcmgenet_rx_refill(struct bcmgenet_priv *priv, ...@@ -1645,14 +1673,12 @@ static struct sk_buff *bcmgenet_rx_refill(struct bcmgenet_priv *priv,
} }
/* Grab the current Rx skb from the ring and DMA-unmap it */ /* Grab the current Rx skb from the ring and DMA-unmap it */
rx_skb = cb->skb; rx_skb = bcmgenet_free_rx_cb(kdev, cb);
if (likely(rx_skb))
dma_unmap_single(kdev, dma_unmap_addr(cb, dma_addr),
priv->rx_buf_len, DMA_FROM_DEVICE);
/* Put the new Rx skb on the ring */ /* Put the new Rx skb on the ring */
cb->skb = skb; cb->skb = skb;
dma_unmap_addr_set(cb, dma_addr, mapping); dma_unmap_addr_set(cb, dma_addr, mapping);
dma_unmap_len_set(cb, dma_len, priv->rx_buf_len);
dmadesc_set_addr(priv, cb->bd_addr, mapping); dmadesc_set_addr(priv, cb->bd_addr, mapping);
/* Return the current Rx skb to caller */ /* Return the current Rx skb to caller */
...@@ -1859,22 +1885,16 @@ static int bcmgenet_alloc_rx_buffers(struct bcmgenet_priv *priv, ...@@ -1859,22 +1885,16 @@ static int bcmgenet_alloc_rx_buffers(struct bcmgenet_priv *priv,
static void bcmgenet_free_rx_buffers(struct bcmgenet_priv *priv) static void bcmgenet_free_rx_buffers(struct bcmgenet_priv *priv)
{ {
struct device *kdev = &priv->pdev->dev; struct sk_buff *skb;
struct enet_cb *cb; struct enet_cb *cb;
int i; int i;
for (i = 0; i < priv->num_rx_bds; i++) { for (i = 0; i < priv->num_rx_bds; i++) {
cb = &priv->rx_cbs[i]; cb = &priv->rx_cbs[i];
if (dma_unmap_addr(cb, dma_addr)) { skb = bcmgenet_free_rx_cb(&priv->pdev->dev, cb);
dma_unmap_single(kdev, if (skb)
dma_unmap_addr(cb, dma_addr), dev_kfree_skb_any(skb);
priv->rx_buf_len, DMA_FROM_DEVICE);
dma_unmap_addr_set(cb, dma_addr, 0);
}
if (cb->skb)
bcmgenet_free_cb(cb);
} }
} }
...@@ -2458,8 +2478,10 @@ static int bcmgenet_dma_teardown(struct bcmgenet_priv *priv) ...@@ -2458,8 +2478,10 @@ static int bcmgenet_dma_teardown(struct bcmgenet_priv *priv)
static void bcmgenet_fini_dma(struct bcmgenet_priv *priv) static void bcmgenet_fini_dma(struct bcmgenet_priv *priv)
{ {
int i;
struct netdev_queue *txq; struct netdev_queue *txq;
struct sk_buff *skb;
struct enet_cb *cb;
int i;
bcmgenet_fini_rx_napi(priv); bcmgenet_fini_rx_napi(priv);
bcmgenet_fini_tx_napi(priv); bcmgenet_fini_tx_napi(priv);
...@@ -2468,10 +2490,10 @@ static void bcmgenet_fini_dma(struct bcmgenet_priv *priv) ...@@ -2468,10 +2490,10 @@ static void bcmgenet_fini_dma(struct bcmgenet_priv *priv)
bcmgenet_dma_teardown(priv); bcmgenet_dma_teardown(priv);
for (i = 0; i < priv->num_tx_bds; i++) { for (i = 0; i < priv->num_tx_bds; i++) {
if (priv->tx_cbs[i].skb != NULL) { cb = priv->tx_cbs + i;
dev_kfree_skb(priv->tx_cbs[i].skb); skb = bcmgenet_free_tx_cb(&priv->pdev->dev, cb);
priv->tx_cbs[i].skb = NULL; if (skb)
} dev_kfree_skb(skb);
} }
for (i = 0; i < priv->hw_params->tx_queues; i++) { for (i = 0; i < priv->hw_params->tx_queues; i++) {
......
...@@ -544,6 +544,8 @@ struct bcmgenet_hw_params { ...@@ -544,6 +544,8 @@ struct bcmgenet_hw_params {
}; };
struct bcmgenet_skb_cb { struct bcmgenet_skb_cb {
struct enet_cb *first_cb; /* First control block of SKB */
struct enet_cb *last_cb; /* Last control block of SKB */
unsigned int bytes_sent; /* bytes on the wire (no TSB) */ unsigned int bytes_sent; /* bytes on the wire (no TSB) */
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment