net: stmmac: retry a failed RX refill from a timer
Subsystems (7)
PATCH v2 1/1 net: stmmac: retry a failed RX refill from a timer
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.
---
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(-)
drivers/net/ethernet/stmicro/stmmac/stmmac.h +1 -0
@@ -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;
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +66 -6
133 lines not on this page · raw message
drivers/net/ethernet/stmicro/stmmac/stmmac_xdp.c +1 -0
@@ -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
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.
Critical: 0 · High: 1 (1) · Medium: 0 · Low: 0
If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).
- [High] The new full-budget return in stmmac_rx() can leave RX dead in the tc block offload path, which is the failure this patch is meant to…
Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but resume hands RX descriptors without valid buffer addresses back to the DMA, and the…
--
net: stmmac: retry a failed RX refill from a timer
The patch adds a per-channel timer to stmmac. The timer schedules RX NAPI when a page_pool refill failure leaves too few RX descriptors with the DMA. stmmac_rx() now also returns the full budget when it stopped at the descriptor before dirty_rx and the refill then succeeded. That way NAPI polls again instead of waiting for an RX interrupt.
Quoted 4 lines
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +0
-0
[ ... ]
Quoted 9 lines
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +4
-1@@ -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.
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c static void stmmac_enable_all_queues(struct stmmac_priv *priv) · line 377
Quoted 10 lines
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +6
-0+*/ +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); }
[Severity: High]
This isn't a bug introduced by this patch, but the new check here can't catch it. On resume, can the RX DMA get descriptors that have no valid buffer address?
stmmac_resume() calls stmmac_reset_queues_param(), and stmmac_reset_rx_queue() does:
rx_q->cur_rx = 0; rx_q->dirty_rx = 0;
It then calls stmmac_clear_descriptors(). stmmac_clear_rx_descriptors() runs stmmac_init_rx_desc() on every slot without checking buf_pool[i].page.
On dwmac4, dwmac4_rd_init_rx_desc() only calls dwmac4_set_rx_owner(). That ORs OWN and BUFFER1_VALID into des3 and leaves des0/des1 unchanged.
Suppose an earlier stmmac_rx_refill() stopped on a page_pool allocation failure. The slots after it were already consumed: buf->page is NULL, and des0/des1 still hold write-back data (for example the VLAN tag that dwmac4_wrback_get_rx_vlan_tci() reads from des0).
After resume the DMA owns those descriptors. Could it write received frames to an address built from those stale write-back words?
Resume reset the indices, so stmmac_rx_refill_needed() sees CIRC_SPACE(0, 0, size) = size - 1 here and never arms the refill timer.
The notes below the --- line list "resume after a failed refill" as not changed. Is a separate fix planned for it?
[ ... ]
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c static int stmmac_rx(struct stmmac_priv *priv, int limit, u32 queue) · line 6206
Quoted 13 lines
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +7
-0@@ -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; }
[Severity: High]
Can this full-budget return leave the RX DMA interrupt masked for good on the tc block offload path?
stmmac_napi_check() masks the channel's RX DMA interrupt before it schedules rx_napi:
if (napi_schedule_prep(rx_napi)) {
spin_lock_irqsave(&ch->lock, flags);
stmmac_disable_dma_irq(priv, priv->ioaddr, chan, 1, 0);
spin_unlock_irqrestore(&ch->lock, flags);
__napi_schedule(rx_napi);
}Only stmmac_napi_poll_rx() unmasks it, and only when work_done < budget and napi_complete_done() succeeds.
With this change, stmmac_rx() returns limit even when count < limit. If a napi_disable() is pending at that point, __napi_poll() completes NAPI itself:
if (unlikely(napi_disable_pending(n))) {
napi_complete(n);
return work;
}so the driver's unmask never runs.
stmmac_setup_tc_block_cb() (cls_u32 / flower offload) calls __stmmac_disable_all_queues() and then stmmac_enable_all_queues(). Neither unmasks the DMA interrupt or reinitialises the ring.
The new check in stmmac_enable_all_queues() arms the timer only when stmmac_rx_refill_needed() is true. This full-budget return only happens when it is false, and the ring indices don't change in between. After napi_enable() there is no pending timer and no scheduled poll, and the RX interrupt is still masked.
Before this patch, a 64-entry ring (DMA_MIN_RX_SIZE) with the default NAPI weight of 64 could return at most 63, so every poll completed and unmasked the interrupt. The notes below the --- line report 400 of 851 polls returning the full budget with a 64-entry ring. With larger rings the same thing now also happens after an allocation failure followed by a successful refill, which is the case this patch is meant to fix.
The notes say a separate fix will follow, but that text won't reach git, and the patch is tagged for stable. Could the fix be part of this patch? For example, always call napi_schedule(&ch->rx_napi), or arm the refill timer, after napi_enable() in stmmac_enable_all_queues() or stmmac_setup_tc_block_cb().
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/MA0PR01MB92174ED1110DC61766D591C4FD432%40MA0PR01MB9217.INDPRD01.PROD.OUTLOOK.COM
Quoted 4 lines · On Sun, 29 Nov 2026 06:34:35 +0000, [email protected] wrote:
[Severity: High]
This isn't a bug introduced by this patch, but the new check here can't catch it. On resume, can the RX DMA get descriptors that have no valid buffer address?
[...]
Quoted 1 line
Is a separate fix planned for it?
It is pre-existing. Ding Hui's resume series, still under review, fixes
it: his v8 3/4 allocates any missing RX pages and rewrites the buffer addresses before the DMA restarts.
So I do not plan a separate fix.
Quoted 3 lines
[Severity: High]
Can this full-budget return leave the RX DMA interrupt masked for good on the tc block offload path?
[...]
Quoted 4 lines
Could the fix be part of this patch?
For example, always callnapi_schedule(&ch->rx_napi), or arm the refill timer, afternapi_enable()instmmac_enable_all_queues()orstmmac_setup_tc_block_cb().
Agreed that it can, and it is also reachable without this patch on rings larger than the NAPI weight, where full-budget polls are normal under load. On an ADSP-SC598 (dwmac4) with a 6.18-based kernel without this patch, ring 512 and a 64-byte UDP flood, 4 of 4 offloaded tc flower add/del left RIE masked with RI latched, and no poll ran until the board transmitted. With the same flood and no tc changes there was no stall.
So v3 will be a two-patch series. 1/2 is a separate fix for the tc
path: stmmac_enable_all_queues() calls napi_schedule() after napi_enable(), so a poll always runs once the queues are enabled again. 2/2 is this patch, rebased on it.
pw-bot: cr