Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH net v2] net: stmmac: retry a failed RX refill from a timer
@ 2026-11-25  6:17 Anandu Padinhare
  2026-11-29  6:34 ` netdev-bot+sashiko
  0 siblings, 1 reply; 2+ messages in thread
From: Anandu Padinhare @ 2026-11-25  6:17 UTC (permalink / raw)
  To: maxime.chevallier@bootlin.com, andrew+netdev@lunn.ch,
	davem@davemloft.net, edumazet@kernel.org, kuba@kernel.org,
	pabeni@redhat.com
  Cc: netdev@vger.kernel.org, mcoquelin.stm32@gmail.com,
	alexandre.torgue@foss.st.com, Alexei Starovoitov, Daniel Borkmann,
	Jesper Dangaard Brouer, John Fastabend, Stanislav Fomichev,
	linux-stm32@st-md-mailman.stormreply.com,
	linux-arm-kernel@lists.infradead.org, bpf@vger.kernel.org,
	Russell King, Sam Edwards, Nicolai Buchwitz, David Wu,
	stable@vger.kernel.org

stmmac_rx_refill() stops at the first page_pool allocation failure. The
refill is retried only from the next RX NAPI poll, and only an RX
interrupt schedules that poll. Once the DMA has used the buffers it
still had, it suspends, no RX interrupt comes, and receive can stay dead
even after memory is available again, until the interface is reopened.
Commit 0bb05e6adfa9 ("net: stmmac: Prevent NULL deref when RX memory
exhausted") describes this stall and leaves it for a later change.

When at most STMMAC_RX_REFILL_THRESH (16) slots are left with the DMA
after a poll, arm a per-channel timer that schedules the RX NAPI about
10 ms later. The poll retries the refill and re-arms the timer while
allocations keep failing, so NAPI does not keep polling while memory is
short. This follows the review of an earlier attempt (see Links). The
retry does not rely on the Receive Buffer Unavailable (RBU) interrupt,
which is not enabled on every core and kernel version.

The timer lives in struct stmmac_channel, because __stmmac_open()
replaces priv->dma_conf. It is cancelled after rx_napi is disabled, and
stmmac_enable_all_queues() re-arms it, because
stmmac_setup_tc_block_cb() disables and re-enables the queues without
re-initialising the rings.

Since commit b6cb4541853c ("net: stmmac: avoid rx queue overrun"),
stmmac_rx() leaves the descriptor before dirty_rx to a later poll, and
the NULL deref fix named above keeps that rule. When a poll stops there
and its refill then gives the DMA enough buffers again, return the full
budget, so that NAPI polls again and receives a frame waiting in that
descriptor instead of waiting for an RX interrupt that already fired. A
poll can also stop there without an allocation failure, when it uses up
a small ring. Check for that descriptor before reading its status, so
that the retries do not count the same status again.

Found while debugging a separate AF_XDP zero-copy RX stall, with the
help of an AI code review agent. Reproduced on an ADI ADSP-SC598
(dwmac 5.20) with a kernel based on ADI's 6.18.31, using error
injection: page_pool_alloc_netmems() was made to return NULL on every
call through fail_function (retval 0, probability 100, times -1) for 3 s
while receiving about 4000 frames/s. With the RBU interrupt disabled
(the dwmac4 default in 6.18), receive stayed dead in 6 of 6 runs until
ethtool -G.

Tested on the same board with the fix backported: receive came back by
itself in every run of the reproducer, with the RBU interrupt off and
on. When exactly one ring's worth of frames arrived while every
allocation failed, and then no more, the last frame was received in 20
of 20 runs (23-63 ms after the failures stopped, where timed). Without
the full-budget return, it waited in the ring for the next frame in 20
of 20. Only build-tested on mainline. Other DMA cores were not tested.
Kernel selftests were not run.

Fixes: 47dd7a540b8a ("net: add support for STMicroelectronics Ethernet controllers.")
Cc: stable@vger.kernel.org
Link: https://lore.kernel.org/netdev/20260331195927.3353cc6d@kernel.org/
Link: https://lore.kernel.org/netdev/20260402174049.0ac088cc@kernel.org/
Signed-off-by: Anandu Padinhare <Anandu.Padinhare@electrifex.com>
---
Changes in v2:
- Return the full budget from stmmac_rx() when it stopped at dirty_rx
  and the refill then gave the DMA enough buffers again, so the waiting
  frame is received without another interrupt (Nicolai, Sam).
- Check dirty_rx before reading the descriptor status, so retries do
  not count it again (Sashiko). 0bb05e6adfa9's condition and break are
  kept.
- Reword the comment above stmmac_rx_refill_needed(): dwmac4 does raise
  RBU but only counts it, so no interrupt schedules RX NAPI when the DMA
  suspends (Sashiko).
- Link to v1: https://lore.kernel.org/netdev/MA0PR01MB921746F36D90C7080287ACC9FD782@MA0PR01MB9217.INDPRD01.PROD.OUTLOOK.COM/

Testing (6.18 backport, debug kernel): TCP throughput with 64 and 512
entry rings shows no difference from v1 beyond the run-to-run
variation, and ethtool -t gives the same result as v1 for every test.
Ring 64, 10 s of TCP RX: v1 returned 63, the most a poll can take from
a 64-entry ring, in 400 of 848 polls. v2 returned the full budget in
400 of 851 polls instead. The changelog's 23-63 ms is the time from the
end of the failures until the frame left in the ring was received.
v1's test suite (see the v1 notes) was rerun with v2, with no new
failures.

Not changed: Sashiko's two pre-existing items on v1, resume after a
failed refill, and stmmac_setup_tc_block_cb() leaving the RX interrupt
masked when it disables NAPI right after a full-budget poll. The
full-budget return makes the latter reachable in more cases, for
example with rings of 64 entries or fewer. A separate fix will follow
(schedule rx_napi after napi_enable() there).

 drivers/net/ethernet/stmicro/stmmac/stmmac.h  |  1 +
 .../net/ethernet/stmicro/stmmac/stmmac_main.c | 72 +++++++++++++++++--
 .../net/ethernet/stmicro/stmmac/stmmac_xdp.c  |  1 +
 3 files changed, 68 insertions(+), 6 deletions(-)

diff --git a/drivers/net/ethernet/stmicro/stmmac/stmmac.h b/drivers/net/ethernet/stmicro/stmmac/stmmac.h
index b76acfe0d958..88b83dc3cf0f 100644
--- a/drivers/net/ethernet/stmicro/stmmac/stmmac.h
+++ b/drivers/net/ethernet/stmicro/stmmac/stmmac.h
@@ -142,6 +142,7 @@ struct stmmac_channel {
 	struct napi_struct rx_napi ____cacheline_aligned_in_smp;
 	struct napi_struct tx_napi ____cacheline_aligned_in_smp;
 	struct napi_struct rxtx_napi ____cacheline_aligned_in_smp;
+	struct timer_list rx_refill_timer;
 	struct stmmac_priv *priv_data;
 	spinlock_t lock;
 	u32 index;
diff --git a/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c b/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c
index a685ce4638f6..bd0badb90bc8 100644
--- a/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c
+++ b/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c
@@ -86,6 +86,8 @@ MODULE_PARM_DESC(phyaddr, "Physical device address");
 #define STMMAC_XSK_TX_BUDGET_MAX	256
 #define STMMAC_TX_XSK_AVAIL		16
 #define STMMAC_RX_FILL_BATCH		16
+#define STMMAC_RX_REFILL_THRESH		16
+#define STMMAC_RX_REFILL_RETRY_MS	10
 
 #define STMMAC_XDP_PASS		0
 #define STMMAC_XDP_CONSUMED	BIT(0)
@@ -263,6 +265,33 @@ static void stmmac_verify_args(void)
 		pr_warn("stmmac: module parameter 'flow_ctrl' is obsolete - please remove from your module configuration\n");
 }
 
+/* The RX DMA suspends once it has used all its buffers, and no interrupt
+ * schedules RX NAPI when that happens, so a refill that failed for lack of
+ * memory would never be retried. Retry it from a timer when few slots are left
+ * with the DMA.
+ */
+static bool stmmac_rx_refill_needed(struct stmmac_priv *priv, u32 queue)
+{
+	struct stmmac_rx_queue *rx_q = &priv->dma_conf.rx_queue[queue];
+
+	return CIRC_SPACE(rx_q->cur_rx, rx_q->dirty_rx,
+			  priv->dma_conf.dma_rx_size) < STMMAC_RX_REFILL_THRESH;
+}
+
+static void stmmac_rx_refill_timer_arm(struct stmmac_channel *ch)
+{
+	if (!timer_pending(&ch->rx_refill_timer))
+		mod_timer(&ch->rx_refill_timer,
+			  jiffies + msecs_to_jiffies(STMMAC_RX_REFILL_RETRY_MS));
+}
+
+static void stmmac_rx_refill_timer(struct timer_list *t)
+{
+	struct stmmac_channel *ch = timer_container_of(ch, t, rx_refill_timer);
+
+	napi_schedule(&ch->rx_napi);
+}
+
 static void __stmmac_disable_all_queues(struct stmmac_priv *priv)
 {
 	u8 rx_queues_cnt = priv->plat->rx_queues_to_use;
@@ -284,6 +313,10 @@ static void __stmmac_disable_all_queues(struct stmmac_priv *priv)
 		if (queue < tx_queues_cnt)
 			napi_disable(&ch->tx_napi);
 	}
+
+	/* With NAPI disabled no poll can re-arm the refill timers */
+	for (queue = 0; queue < rx_queues_cnt; queue++)
+		timer_delete_sync(&priv->channel[queue].rx_refill_timer);
 }
 
 /**
@@ -328,8 +361,17 @@ static void stmmac_enable_all_queues(struct stmmac_priv *priv)
 			continue;
 		}
 
-		if (queue < rx_queues_cnt)
+		if (queue < rx_queues_cnt) {
+			/* Disabling the queues cancelled any pending refill
+			 * retry, but the ring may have been kept as it was.
+			 * Check it while no poll can run.
+			 */
+			bool refill = stmmac_rx_refill_needed(priv, queue);
+
 			napi_enable(&ch->rx_napi);
+			if (refill)
+				stmmac_rx_refill_timer_arm(ch);
+		}
 		if (queue < tx_queues_cnt)
 			napi_enable(&ch->tx_napi);
 	}
@@ -5877,6 +5919,7 @@ static int stmmac_rx(struct stmmac_priv *priv, int limit, u32 queue)
 	struct sk_buff *skb = NULL;
 	struct stmmac_xdp_buff ctx;
 	bool fcs_stripped = false;
+	bool at_dirty = false;
 	int xdp_status = 0;
 	int bufsz;
 
@@ -5925,17 +5968,23 @@ static int stmmac_rx(struct stmmac_priv *priv, int limit, u32 queue)
 
 		p = stmmac_get_rx_desc(priv, rx_q, entry);
 
+		/* The descriptor before dirty_rx is only processed once the
+		 * refill has moved dirty_rx on. Check this before reading the
+		 * status, so that a retry does not count the frame again.
+		 */
+		next_entry = STMMAC_NEXT_ENTRY(rx_q->cur_rx,
+					       priv->dma_conf.dma_rx_size);
+		if (unlikely(next_entry == rx_q->dirty_rx)) {
+			at_dirty = true;
+			break;
+		}
+
 		/* read the status of the incoming frame */
 		status = stmmac_rx_status(priv, &priv->xstats, p);
 		/* check if managed by the DMA otherwise go ahead */
 		if (unlikely(status & dma_own))
 			break;
 
-		next_entry = STMMAC_NEXT_ENTRY(rx_q->cur_rx,
-					       priv->dma_conf.dma_rx_size);
-		if (unlikely(next_entry == rx_q->dirty_rx))
-			break;
-
 		rx_q->cur_rx = next_entry;
 
 		np = stmmac_get_rx_desc(priv, rx_q, next_entry);
@@ -6146,6 +6195,13 @@ static int stmmac_rx(struct stmmac_priv *priv, int limit, u32 queue)
 	priv->xstats.rx_dropped += rx_dropped;
 	priv->xstats.rx_errors += rx_errors;
 
+	/* A frame may be waiting in the descriptor we stopped at. If the
+	 * refill freed it, poll again rather than wait for an interrupt
+	 * that already fired. If not, the refill timer polls again.
+	 */
+	if (unlikely(at_dirty) && !stmmac_rx_refill_needed(priv, queue))
+		return limit;
+
 	return count;
 }
 
@@ -6164,6 +6220,8 @@ static int stmmac_napi_poll_rx(struct napi_struct *napi, int budget)
 	u64_stats_update_end(&rxq_stats->napi_syncp);
 
 	work_done = stmmac_rx(priv, budget, chan);
+	if (unlikely(stmmac_rx_refill_needed(priv, chan)))
+		stmmac_rx_refill_timer_arm(ch);
 	if (work_done < budget && napi_complete_done(napi, work_done)) {
 		unsigned long flags;
 
@@ -7779,6 +7837,8 @@ static void stmmac_napi_add(struct net_device *dev)
 
 		if (queue < priv->plat->rx_queues_to_use) {
 			netif_napi_add(dev, &ch->rx_napi, stmmac_napi_poll_rx);
+			timer_setup(&ch->rx_refill_timer,
+				    stmmac_rx_refill_timer, 0);
 		}
 		if (queue < priv->plat->tx_queues_to_use) {
 			netif_napi_add_tx(dev, &ch->tx_napi,
diff --git a/drivers/net/ethernet/stmicro/stmmac/stmmac_xdp.c b/drivers/net/ethernet/stmicro/stmmac/stmmac_xdp.c
index 9af9e0cb366c..c3e85ee1067c 100644
--- a/drivers/net/ethernet/stmicro/stmmac/stmmac_xdp.c
+++ b/drivers/net/ethernet/stmicro/stmmac/stmmac_xdp.c
@@ -35,6 +35,7 @@ static int stmmac_xdp_enable_pool(struct stmmac_priv *priv,
 
 	if (need_update) {
 		napi_disable(&ch->rx_napi);
+		timer_delete_sync(&ch->rx_refill_timer);
 		napi_disable(&ch->tx_napi);
 		stmmac_disable_rx_queue(priv, queue);
 		stmmac_disable_tx_queue(priv, queue);

base-commit: cc7fe6e741730fef549e484379281736d32746f0

^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-29  6:34 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25  6:17 [PATCH net v2] net: stmmac: retry a failed RX refill from a timer Anandu Padinhare
2026-11-29  6:34 ` netdev-bot+sashiko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox