poll_rx() refills a descriptor before it passes the old buffer up the stack, but bcm4908_enet_dma_alloc_rx_buf() writes the results of the allocation and of the mapping into the ring slot before it checks them. When either fails, the slot is left with buf == NULL, the old buffer is lost while still mapped, and read_idx stays on a descriptor the DMA has already completed. A later poll takes that descriptor again and, once its refill works, hands the NULL buffer to build_skb(), or to skb_free_frag() on the bad-frame path. Allocate and map into local variables and update the slot only once both have worked. When either fails, drop the frame and give its buffer back to the DMA, so that the ring keeps going. Sashiko's reviews of the TX ring series pointed this out. I have not seen a refill fail on its own on the hardware. Fixes: 4feffeadbcb2 ("net: broadcom: bcm4908enet: add BCM4908 controller driver") Signed-off-by: Kordian Chmielewski <kudz…@gmail.com> --- drivers/net/ethernet/broadcom/bcm4908_enet.c | 60 +++++++++++++------- 1 file changed, 41 insertions(+), 19 deletions(-) diff --git a/drivers/net/ethernet/broadcom/bcm4908_enet.c b/drivers/net/ethernet/broadcom/bcm4908_enet.c index 8bc620de2ef0..d6cbb771b88a 100644 --- a/drivers/net/ethernet/broadcom/bcm4908_enet.c +++ b/drivers/net/ethernet/broadcom/bcm4908_enet.c @@ -265,36 +265,50 @@ static void bcm4908_enet_dma_reset(struct bcm4908_enet *enet) } } -static int bcm4908_enet_dma_alloc_rx_buf(struct bcm4908_enet *enet, unsigned int idx) +/* Give the descriptor, with the buffer of its slot, to the DMA */ +static void bcm4908_enet_dma_rx_desc_arm(struct bcm4908_enet *enet, + unsigned int idx) { struct bcm4908_enet_dma_ring_bd *buf_desc = &enet->rx_ring.buf_desc[idx]; struct bcm4908_enet_dma_ring_slot *slot = &enet->rx_ring.slots[idx]; - struct device *dev = enet->dev; u32 tmp; + + tmp = ENET_RX_BUF_DMA_SIZE << DMA_CTL_LEN_DESC_BUFLENGTH_SHIFT; + tmp |= DMA_CTL_STATUS_OWN; + if (idx == enet->rx_ring.length - 1) + tmp |= DMA_CTL_STATUS_WRAP; + buf_desc->addr = cpu_to_le32(slot->dma_addr); + /* Hand the descriptor to the DMA only once it holds the address */ + dma_wmb(); + buf_desc->ctl = cpu_to_le32(tmp); +} + +static int bcm4908_enet_dma_alloc_rx_buf(struct bcm4908_enet *enet, + unsigned int idx) +{ + struct bcm4908_enet_dma_ring_slot *slot = &enet->rx_ring.slots[idx]; + struct device *dev = enet->dev; + dma_addr_t dma_addr; + void *buf; int err; - slot->buf = napi_alloc_frag(ENET_RX_SKB_BUF_ALLOC_SIZE); - if (!slot->buf) + buf = napi_alloc_frag(ENET_RX_SKB_BUF_ALLOC_SIZE); + if (!buf) return -ENOMEM; - slot->dma_addr = dma_map_single(dev, slot->buf + ENET_RX_BUF_DMA_OFFSET, - ENET_RX_BUF_DMA_SIZE, DMA_FROM_DEVICE); - err = dma_mapping_error(dev, slot->dma_addr); + dma_addr = dma_map_single(dev, buf + ENET_RX_BUF_DMA_OFFSET, + ENET_RX_BUF_DMA_SIZE, DMA_FROM_DEVICE); + err = dma_mapping_error(dev, dma_addr); if (err) { dev_err(dev, "Failed to map DMA buffer: %d\n", err); - skb_free_frag(slot->buf); - slot->buf = NULL; + skb_free_frag(buf); return err; } - tmp = ENET_RX_BUF_DMA_SIZE << DMA_CTL_LEN_DESC_BUFLENGTH_SHIFT; - tmp |= DMA_CTL_STATUS_OWN; - if (idx == enet->rx_ring.length - 1) - tmp |= DMA_CTL_STATUS_WRAP; - buf_desc->addr = cpu_to_le32(slot->dma_addr); - /* Hand the descriptor to the DMA only once it holds the new buffer */ - dma_wmb(); - buf_desc->ctl = cpu_to_le32(tmp); + /* The slot changes only now, so a failure above leaves it as it was */ + slot->buf = buf; + slot->dma_addr = dma_addr; + bcm4908_enet_dma_rx_desc_arm(enet, idx); return 0; } @@ -662,8 +676,16 @@ static int bcm4908_enet_poll_rx(struct napi_struct *napi, int weight) /* Provide new buffer before unpinning the old one */ err = bcm4908_enet_dma_alloc_rx_buf(enet, enet->rx_ring.read_idx); - if (err) - break; + if (err) { + /* Drop the frame and give its buffer back to the DMA */ + bcm4908_enet_dma_rx_desc_arm(enet, + enet->rx_ring.read_idx); + if (++enet->rx_ring.read_idx == enet->rx_ring.length) + enet->rx_ring.read_idx = 0; + enet->netdev->stats.rx_dropped++; + handled++; + continue; + } if (++enet->rx_ring.read_idx == enet->rx_ring.length) enet->rx_ring.read_idx = 0;
-- 2.56.1