Netdev List
 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 2/2] net: bcm4908_enet: keep the RX buffers when the RX channel does not stop
Date: Wed,  2 Dec 2026 10:56:11 +0100	[thread overview]
Message-ID: <20261202095611.156840-3-kudzo51@gmail.com> (raw)
In-Reply-To: <20261202095611.156840-1-kudzo51@gmail.com>

bcm4908_enet_stop() frees the RX buffers even when
bcm4908_enet_dma_rx_ring_disable() timed out, so the DMA may still
write a frame into a buffer that has been freed.

Do for RX what commit e2785f0ff0a2 ("net: bcm4908_enet: release the TX
ring on stop") did for TX: return the timeout and, when the channel did
not stop, leave the buffers to the DMA. They are leaked: the slots
forget them, so that nothing frees them later, not even the error path
of the next open().

The wait itself can also give up too early. Its 2 ms deadline is counted
in jiffies, which is a single tick at HZ=100 or HZ=250, so it can pass
after the first read of the channel config. With the change above, such
an early timeout would leak all the RX buffers. Wait with
read_poll_timeout() instead, and still clear ENABLE on every read that
finds it set, as the loop did.

Sashiko's reviews of the TX ring series pointed out both problems, the
second one in a TX copy of this loop in v2 of that series. I have not
seen the RX channel time out on the hardware.

bcm4908_enet_remove() still frees the descriptor ring, which the DMA may
also write to, after a stop() that timed out. This patch does not
change that.

Fixes: 4feffeadbcb2 ("net: broadcom: bcm4908enet: add BCM4908 controller driver")
Signed-off-by: Kordian Chmielewski <kudzo51@gmail.com>
---
 drivers/net/ethernet/broadcom/bcm4908_enet.c | 54 +++++++++++++++-----
 1 file changed, 40 insertions(+), 14 deletions(-)

diff --git a/drivers/net/ethernet/broadcom/bcm4908_enet.c b/drivers/net/ethernet/broadcom/bcm4908_enet.c
index d6cbb771b88a..f42ad45a47ea 100644
--- a/drivers/net/ethernet/broadcom/bcm4908_enet.c
+++ b/drivers/net/ethernet/broadcom/bcm4908_enet.c
@@ -357,6 +357,18 @@ static void bcm4908_enet_dma_uninit(struct bcm4908_enet *enet)
 	}
 }
 
+/* Let go of the RX buffers without unmapping or freeing them, for when the
+ * DMA may still write to them: they are leaked, and nothing frees them later
+ */
+static void bcm4908_enet_dma_rx_leak(struct bcm4908_enet *enet)
+{
+	struct bcm4908_enet_dma_ring *rx_ring = &enet->rx_ring;
+	int i;
+
+	for (i = 0; i < rx_ring->length; i++)
+		rx_ring->slots[i].buf = NULL;
+}
+
 static int bcm4908_enet_dma_init(struct bcm4908_enet *enet)
 {
 	struct bcm4908_enet_dma_ring *rx_ring = &enet->rx_ring;
@@ -409,24 +421,32 @@ static void bcm4908_enet_dma_rx_ring_enable(struct bcm4908_enet *enet,
 	enet_set(enet, ring->cfg_block + ENET_DMA_CH_CFG, ENET_DMA_CH_CFG_ENABLE);
 }
 
-static void bcm4908_enet_dma_rx_ring_disable(struct bcm4908_enet *enet,
-					     struct bcm4908_enet_dma_ring *ring)
+/* Read the RX channel config and clear ENABLE again while it is still set */
+static u32 bcm4908_enet_dma_rx_ring_stop(struct bcm4908_enet *enet, u16 offset)
+{
+	u32 tmp = enet_read(enet, offset);
+
+	if (tmp & ENET_DMA_CH_CFG_ENABLE)
+		enet_maskset(enet, offset, ENET_DMA_CH_CFG_ENABLE, 0);
+
+	return tmp;
+}
+
+static int bcm4908_enet_dma_rx_ring_disable(struct bcm4908_enet *enet,
+					    struct bcm4908_enet_dma_ring *ring)
 {
-	unsigned long deadline;
 	u32 tmp;
+	int err;
 
 	enet_maskset(enet, ring->cfg_block + ENET_DMA_CH_CFG, ENET_DMA_CH_CFG_ENABLE, 0);
 
-	deadline = jiffies + usecs_to_jiffies(2000);
-	do {
-		tmp = enet_read(enet, ring->cfg_block + ENET_DMA_CH_CFG);
-		if (!(tmp & ENET_DMA_CH_CFG_ENABLE))
-			return;
-		enet_maskset(enet, ring->cfg_block + ENET_DMA_CH_CFG, ENET_DMA_CH_CFG_ENABLE, 0);
-		usleep_range(10, 30);
-	} while (!time_after_eq(jiffies, deadline));
+	err = read_poll_timeout(bcm4908_enet_dma_rx_ring_stop, tmp,
+				!(tmp & ENET_DMA_CH_CFG_ENABLE), 10, 2000,
+				false, enet, ring->cfg_block + ENET_DMA_CH_CFG);
+	if (err)
+		dev_warn(enet->dev, "Timeout waiting for DMA RX stop\n");
 
-	dev_warn(enet->dev, "Timeout waiting for DMA RX stop\n");
+	return err;
 }
 
 /***
@@ -555,6 +575,7 @@ static int bcm4908_enet_stop(struct net_device *netdev)
 	struct bcm4908_enet *enet = netdev_priv(netdev);
 	struct bcm4908_enet_dma_ring *tx_ring = &enet->tx_ring;
 	struct bcm4908_enet_dma_ring *rx_ring = &enet->rx_ring;
+	int rx_err;
 
 	netif_stop_queue(netdev);
 	netif_carrier_off(netdev);
@@ -562,12 +583,17 @@ static int bcm4908_enet_stop(struct net_device *netdev)
 	napi_disable(&tx_ring->napi);
 	netdev_reset_queue(netdev);
 
-	bcm4908_enet_dma_rx_ring_disable(enet, &enet->rx_ring);
+	rx_err = bcm4908_enet_dma_rx_ring_disable(enet, &enet->rx_ring);
 
 	/* Leave the TX buffers alone if the channel may still read them */
 	if (!bcm4908_enet_dma_tx_ring_disable(enet, &enet->tx_ring))
 		bcm4908_enet_tx_ring_clean(enet);
-	bcm4908_enet_dma_uninit(enet);
+
+	/* Likewise for the RX buffers, which the channel may still write to */
+	if (rx_err)
+		bcm4908_enet_dma_rx_leak(enet);
+	else
+		bcm4908_enet_dma_uninit(enet);
 
 	free_irq(enet->irq_tx, enet);
 	free_irq(enet->netdev->irq, enet);
-- 
2.56.1


      parent 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 ` [PATCH net 1/2] net: bcm4908_enet: keep the RX buffer when the refill fails Kordian Chmielewski
2026-12-02  9:56 ` Kordian Chmielewski [this message]

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-3-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