mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Kordian Chmielewski <kudzo51@gmail.com>
To: netdev@vger.kernel.org
Cc: "Florian Fainelli" <f.fainelli@gmail.com>,
	"Florian Fainelli" <florian.fainelli@broadcom.com>,
	"Rafał Miłecki" <rafal@milecki.pl>,
	bcm-kernel-feedback-list@broadcom.com,
	"Andrew Lunn" <andrew+netdev@lunn.ch>,
	"David S . Miller" <davem@davemloft.net>,
	"Eric Dumazet" <edumazet@kernel.org>,
	"Jakub Kicinski" <kuba@kernel.org>,
	"Paolo Abeni" <pabeni@redhat.com>,
	linux-kernel@vger.kernel.org
Subject: [PATCH net 1/2] net: bcm4908_enet: keep the RX buffer when the refill fails
Date: Wed,  2 Dec 2026 10:56:10 +0100	[thread overview]
Message-ID: <20261202095611.156840-2-kudzo51@gmail.com> (raw)
In-Reply-To: <20261202095611.156840-1-kudzo51@gmail.com>

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 <kudzo51@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


  reply	other threads:[~2026-12-02  9:56 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02  9:56 [PATCH net 0/2] net: bcm4908_enet: RX buffer fixes Kordian Chmielewski
2026-12-02  9:56 ` Kordian Chmielewski [this message]
2026-12-02  9:56 ` [PATCH net 2/2] net: bcm4908_enet: keep the RX buffers when the RX channel does not stop Kordian Chmielewski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261202095611.156840-2-kudzo51@gmail.com \
    --to=kudzo51@gmail.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=bcm-kernel-feedback-list@broadcom.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=f.fainelli@gmail.com \
    --cc=florian.fainelli@broadcom.com \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=rafal@milecki.pl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®