* [PATCH net v5 0/2] net: atl1c: fix the RX buffer leak on errored frames
@ 2026-11-25 11:31 Federico Musto
2026-11-25 11:31 ` [PATCH net v5 1/2] net: atl1c: wait for in-flight RX polls before the link-down ring reset Federico Musto
2026-11-25 11:31 ` [PATCH net v5 2/2] net: atl1c: free the RX buffer when the descriptor reports an error Federico Musto
0 siblings, 2 replies; 5+ messages in thread
From: Federico Musto @ 2026-11-25 11:31 UTC (permalink / raw)
To: Chris Snook, Andrew Lunn, David S . Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, netdev
Cc: linux-kernel, Federico Musto
Patch 2 is v5 of the fix for the skb and the DMA mapping leaked by every
RX frame whose descriptor reports an error. Patch 1 closes the race
between the RX poll and the ring reset of the link-down path.
v5 addresses the Sashiko AI review of v4:
- 1/2 no longer proceeds with the ring reset when it stops waiting
because __AT_DOWN appeared: a concurrent close/down/suspend resets the
rings itself after napi_disable(), so the work item now leaves them
alone. The wait is also bounded (~0.5-1 s, then the reset is skipped
with a ratelimited warning), so a threaded busy-poll cannot stall the
work item with interrupts masked. The wait only runs on the work-item
path; atl1c_up() reaches the handler before napi_enable() and skips
it. The carrier-off store is now ordered before the NAPI_STATE_SCHED
sampling with an explicit smp_mb(): the handshake is a store-buffer
pattern and the scheduling side is ordered by the cmpxchg in
napi_schedule_prep().
- 2/2 adds a dma_rmb() between the valid-bit test on word3 and the rest
of the descriptor. word0 now decides whether a reset is scheduled and
the ring is zeroed after a reset, so without the barrier a stale word0
could schedule a spurious reset on weakly ordered CPUs.
The per-patch changelogs are below the "---" of each patch.
Both problems were found by code inspection. Build-tested with W=1 and
sparse on netdev/net: no new warnings.
Runtime-tested on a MikroTik CCR2004-1G-2XS-PCIe, which exposes emulated
AR8151 functions, with these two patches ported onto the driver of a
7.0.14-29-pve kernel plus diagnostic counters. 27 link-down events were
handled and the link recovered every time: 9 from the first campaign (3
PHY link flaps and 6 link drops that this card produces about 1.3 s
after an MTU change), and 18 from later flap campaigns, partly under
sustained RX load. The load caught an RX poll in flight four times:
the wait loop iterated, the poll finished within a few milliseconds and
the reset proceeded normally. The __AT_DOWN skip, the timeout branch,
the out-of-sync reset and the error path of patch 2 have not been
exercised on hardware. No RRD out-of-sync events across ~4.3 GiB of
traffic plus the load campaigns, no Oops, no warnings.
One observation, reported for completeness: twice, about 1.3 s after an
MTU change and immediately before the device signalled link down, the
IOMMU blocked a DMA read from the device to an unmapped address. Both
faults precede the link-down interrupt, so before the work item touched
by this series ran. I have not established the cause; this card is
known to issue stale DMA around that event.
v4: https://lore.kernel.org/netdev/20261117225521.19689-1-federico@rimpianto.com/
v3: https://lore.kernel.org/netdev/20261103225218.19206-1-federico@rimpianto.com/
v2: https://lore.kernel.org/netdev/20261102215253.68185-1-federico@rimpianto.com/
v1: https://lore.kernel.org/netdev/20261027115156.17128-1-federico@rimpianto.com/
Federico Musto (2):
net: atl1c: wait for in-flight RX polls before the link-down ring
reset
net: atl1c: free the RX buffer when the descriptor reports an error
.../net/ethernet/atheros/atl1c/atl1c_main.c | 169 +++++++++++++-----
1 file changed, 121 insertions(+), 48 deletions(-)
--
2.54.0 (Apple Git-157)
^ permalink raw reply [flat|nested] 5+ messages in thread* [PATCH net v5 1/2] net: atl1c: wait for in-flight RX polls before the link-down ring reset 2026-11-25 11:31 [PATCH net v5 0/2] net: atl1c: fix the RX buffer leak on errored frames Federico Musto @ 2026-11-25 11:31 ` Federico Musto 2026-11-29 11:48 ` netdev-bot+sashiko 2026-11-25 11:31 ` [PATCH net v5 2/2] net: atl1c: free the RX buffer when the descriptor reports an error Federico Musto 1 sibling, 1 reply; 5+ messages in thread From: Federico Musto @ 2026-11-25 11:31 UTC (permalink / raw) To: Chris Snook, Andrew Lunn, David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni, netdev Cc: linux-kernel, Federico Musto, stable When the PHY reports link down, the work item calls atl1c_check_link_status(), which resets the MAC, frees every RX buffer and zeroes the ring indices through atl1c_reset_dma_ring(), then posts fresh buffers through atl1c_configure(). Nothing keeps the RX poll away while it does so: atl1c_irq_disable() masks the device interrupt and waits for the hard interrupt handler only, and atl1c_clean_rx() tests the carrier only when it is entered. A poll that passed that test before the carrier went off keeps walking the RRD ring on another CPU while the work item frees the same buffers. Both can unmap and free the same skb, or the poll can hand the stack an skb the work item has already freed. Once the carrier is off, wait until no RX poll is in flight before touching the rings. NAPI_STATE_SCHED is held for the whole poll, so observing it clear on every RX queue means no poll is running, and a poll that starts later sees the carrier off and returns without looking at the rings. NAPI does not have to be disabled. The handshake is a store-buffer pattern: the work item stores the carrier-off state and then loads NAPI_STATE_SCHED, while the scheduling side stores NAPI_STATE_SCHED (the cmpxchg in napi_schedule_prep()) and the poll then loads the carrier. With a full barrier between store and load on both sides, either the work item sees the poll or the poll sees the carrier off. The cmpxchg provides it on the scheduling side; an explicit smp_mb() after netif_carrier_off() provides it here, so that the wait does not depend on how that function sets its bit. napi_disable() is not used because it cannot be made safe here without serialising the work item against atl1c_down() first: the two can run concurrently (MTU change, ethtool, suspend), a second napi_disable() on an already disabled NAPI sleeps holding the netdev instance lock, and the napi_enable() of the first caller then blocks on that lock. The same concurrency shapes the wait. atl1c_close() and atl1c_down() set __AT_DOWN before quiescing NAPI; if the bit appears while the wait runs, a teardown is under way that will napi_disable() and reset the rings itself, so the work item must leave them alone rather than reset without having waited. The wait is also bounded: a poll that never finishes would otherwise stall the work item with interrupts masked. In both cases the rings are left untouched and the handler runs again on the next link-change event. Only the work item waits. atl1c_up() also calls atl1c_check_link_status(), before napi_enable(): no poll can run there, and a disabled NAPI keeps NAPI_STATE_SCHED set, so the wait would only spin. It passes false and the link-down path runs unchanged. Found by code inspection; the race has not been reproduced. Fixes: 5e5c0964d9b9 ("atl1c: do MAC-reset when PHY link down") Cc: stable@vger.kernel.org Signed-off-by: Federico Musto <federico@rimpianto.com> --- New in v4. The Sashiko review flagged this race on v2 and again on v3 of the next patch: pre-existing, but made worse by a free in the same window. v4 -> v5, after the Sashiko AI review of v4: - on __AT_DOWN, skip the link-down reset instead of proceeding without waiting: the concurrent teardown resets the rings after napi_disable(), so the work item no longer touches them while a poll may be live; - bound the wait (~0.5-1 s) and skip the reset on timeout with a ratelimited warning, so a threaded busy-poll (which keeps NAPI_STATE_SCHED set while the interface is up) cannot hang the work item with interrupts masked; - wait only on the work-item path: atl1c_check_link_status() takes a bool and atl1c_up() passes false, because it runs before napi_enable() where a disabled NAPI keeps NAPI_STATE_SCHED set; - order the carrier-off store before the NAPI_STATE_SCHED sampling with an explicit smp_mb(): the handshake is a store-buffer pattern, the scheduling side is ordered by the cmpxchg in napi_schedule_prep(). Not addressed, both pre-existing: the TX side of the same reset (atl1c_clean_tx() and atl1c_xmit_frame() against atl1c_clean_tx_ring()), and the missing serialisation between the work item and the callers of atl1c_down(). --- .../net/ethernet/atheros/atl1c/atl1c_main.c | 65 ++++++++++++++++++- 1 file changed, 62 insertions(+), 3 deletions(-) diff --git a/drivers/net/ethernet/atheros/atl1c/atl1c_main.c b/drivers/net/ethernet/atheros/atl1c/atl1c_main.c index eddd840..afab3e1 100644 --- a/drivers/net/ethernet/atheros/atl1c/atl1c_main.c +++ b/drivers/net/ethernet/atheros/atl1c/atl1c_main.c @@ -248,7 +248,52 @@ void atl1c_reinit_locked(struct atl1c_adapter *adapter) clear_bit(__AT_RESETTING, &adapter->flags); } -static void atl1c_check_link_status(struct atl1c_adapter *adapter) +#define ATL1C_POLL_SYNC_TRIES 500 + +/* atl1c_clean_rx() looks at the carrier only when it is entered. A poll + * that passed that test before the carrier went off keeps walking the RRD + * ring. NAPI_STATE_SCHED is held for the whole poll, and a poll that + * starts later sees the carrier off and returns without touching the + * rings, so once the bit is observed clear on every RX queue no poll is + * running and none can start. + * + * Only the link-change work item needs the wait: atl1c_up() calls + * atl1c_check_link_status() before napi_enable(), when no poll can run, + * and a disabled NAPI keeps NAPI_STATE_SCHED set, so waiting there would + * only spin. + * + * Returns false when the caller must leave the rings alone: + * - __AT_DOWN is set: a concurrent close, down or suspend is tearing the + * device down and resets the rings itself, after napi_disable(); + * - the wait timed out: a poll is not finishing (threaded busy-poll + * keeps NAPI_STATE_SCHED set while the interface is up). Resetting + * under a running poll is the race this wait exists to avoid. + */ +static bool atl1c_rx_poll_sync(struct atl1c_adapter *adapter) +{ + int i; + + for (i = 0; i < adapter->rx_queue_count; ++i) { + int tries = ATL1C_POLL_SYNC_TRIES; + + while (test_bit(NAPI_STATE_SCHED, + &adapter->rrd_ring[i].napi.state)) { + if (test_bit(__AT_DOWN, &adapter->flags)) + return false; + if (!tries--) { + dev_warn_ratelimited(&adapter->pdev->dev, + "RX poll won't finish, skipping link-down reset\n"); + return false; + } + usleep_range(1000, 2000); + } + } + + return true; +} + +static void atl1c_check_link_status(struct atl1c_adapter *adapter, + bool sync_polls) { struct atl1c_hw *hw = &adapter->hw; struct net_device *netdev = adapter->netdev; @@ -265,6 +310,20 @@ static void atl1c_check_link_status(struct atl1c_adapter *adapter) if (!link) { /* link down */ netif_carrier_off(netdev); + if (sync_polls) { + /* Store-buffer pattern against the poll: we store + * carrier-off and then load NAPI_STATE_SCHED, the + * scheduling side stores NAPI_STATE_SCHED + * (napi_schedule_prep()) and the poll then loads the + * carrier. A full barrier on each side guarantees that + * either we see the poll or the poll sees the carrier + * off. The cmpxchg provides it there; do not depend on + * how netif_carrier_off() sets its bit here. + */ + smp_mb(); + if (!atl1c_rx_poll_sync(adapter)) + return; + } hw->hibernate = true; if (atl1c_reset_mac(hw) != 0) if (netif_msg_hw(adapter)) @@ -349,7 +408,7 @@ static void atl1c_common_task(struct work_struct *work) if (test_and_clear_bit(ATL1C_WORK_EVENT_LINK_CHANGE, &adapter->work_event)) { atl1c_irq_disable(adapter); - atl1c_check_link_status(adapter); + atl1c_check_link_status(adapter, true); atl1c_irq_enable(adapter); } } @@ -2435,7 +2494,7 @@ static int atl1c_up(struct atl1c_adapter *adapter) if (unlikely(err)) goto err_up; - atl1c_check_link_status(adapter); + atl1c_check_link_status(adapter, false); clear_bit(__AT_DOWN, &adapter->flags); for (i = 0; i < adapter->tx_queue_count; ++i) napi_enable(&adapter->tpd_ring[i].napi); -- 2.54.0 (Apple Git-157) ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH net v5 1/2] net: atl1c: wait for in-flight RX polls before the link-down ring reset 2026-11-25 11:31 ` [PATCH net v5 1/2] net: atl1c: wait for in-flight RX polls before the link-down ring reset Federico Musto @ 2026-11-29 11:48 ` netdev-bot+sashiko 0 siblings, 0 replies; 5+ messages in thread From: netdev-bot+sashiko @ 2026-11-29 11:48 UTC (permalink / raw) To: federico Cc: chris.snook, andrew+netdev, davem, edumazet, kuba, pabeni, netdev, linux-kernel Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider. Critical: 0 · High: 0 (2) · Medium: 1 · Low: 1 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). - [Medium] When atl1c_rx_poll_sync() returns false (timeout, or __AT_DOWN), atl1c_check_link_status() returns at line 325, right after… - [Low] The new comment above atl1c_rx_poll_sync() (lines 253-258), and the matching sentence in the commit message, say that… Pre-existing issues: - [High] This problem wasn't introduced by this patch, but the patch leaves it reachable: the link-down reset still frees TX buffers while… - [High] This problem wasn't introduced by this patch, but the new __AT_DOWN gate does not close it. -- net: atl1c: wait for in-flight RX polls before the link-down ring reset This patch makes the link-change work item wait until NAPI_STATE_SCHED is clear on every RX queue before atl1c_check_link_status() resets the MAC and the DMA rings. The wait starts after netif_carrier_off() and an smp_mb(). It is bounded, and it gives up if __AT_DOWN appears, in which case the link-down reset is skipped. atl1c_up() passes false and does not wait. > diff --git a/drivers/net/ethernet/atheros/atl1c/atl1c_main.c b/drivers/net/ethernet/atheros/atl1c/atl1c_main.c > index eddd8406867f7..afab3e16903ba 100644 > --- a/drivers/net/ethernet/atheros/atl1c/atl1c_main.c > +++ b/drivers/net/ethernet/atheros/atl1c/atl1c_main.c > @@ -248,7 +248,52 @@ void atl1c_reinit_locked(struct atl1c_adapter *adapter) > clear_bit(__AT_RESETTING, &adapter->flags); > } > > -static void atl1c_check_link_status(struct atl1c_adapter *adapter) > +#define ATL1C_POLL_SYNC_TRIES 500 > + > +/* atl1c_clean_rx() looks at the carrier only when it is entered. A poll > + * that passed that test before the carrier went off keeps walking the RRD > + * ring. NAPI_STATE_SCHED is held for the whole poll, and a poll that > + * starts later sees the carrier off and returns without touching the > + * rings, so once the bit is observed clear on every RX queue no poll is > + * running and none can start. [Severity: Low] Is this description accurate? The commit message makes the same claim: "NAPI_STATE_SCHED is held for the whole poll, so observing it clear on every RX queue means no poll is running" On every completion path, atl1c_clean_rx() calls napi_complete_done() first, which clears NAPI_STATE_SCHED. Only after that does it write REG_IMR: quit_polling: napi_complete_done(napi, work_done); spin_lock_irqsave(&adapter->hw.intr_mask_lock, flags); adapter->hw.intr_mask |= atl1c_qregs[rrd_ring->num].rx_isr; AT_WRITE_REG(&adapter->hw, REG_IMR, adapter->hw.intr_mask); Every poll takes this carrier-off quit path during the new wait. atl1c_clean_tx() has the same tail. So the poll is still running, and still writing a device register, after the bit being sampled has gone clear. That write re-arms the mask that atl1c_common_task() cleared with atl1c_irq_disable(). It does not check irq_sem. Two cases leave device interrupts live during atl1c_reset_dma_ring() and atl1c_configure(): - the write lands after the soft reset in atl1c_reset_mac(); - atl1c_intr() runs (shared INTx or netpoll) and writes REG_ISR = 0. New polls can also still start through IRQs, busy-poll or netpoll, which contradicts "none can start". RX polls that start later return early once they see the carrier off, so the RX ring argument still holds. The IMR rewrite in the poll tail also predates this patch. Could the comment and the commit message describe the guarantee more narrowly? [ ... ] > +static bool atl1c_rx_poll_sync(struct atl1c_adapter *adapter) > +{ > + int i; > + > + for (i = 0; i < adapter->rx_queue_count; ++i) { > + int tries = ATL1C_POLL_SYNC_TRIES; > + > + while (test_bit(NAPI_STATE_SCHED, > + &adapter->rrd_ring[i].napi.state)) { [Severity: High] This is a pre-existing issue, but the patch leaves it reachable. Only rrd_ring[i].napi is sampled here. Once this returns true, the link-down path calls atl1c_reset_dma_ring(), which frees every TX buffer first: /* release tx-pending skbs and reset tx/rx ring index */ for (i = 0; i < adapter->tx_queue_count; ++i) atl1c_clean_tx_ring(adapter, i); atl1c_irq_disable() only calls synchronize_irq(). It does not wait for a TX NAPI that is already scheduled or running on another CPU. atl1c_clean_tx() has no carrier gate. It walks the same buffer_info[] entries without a lock: atl1c_clean_buffer(pdev, buffer_info, budget); if (++next_to_clean == tpd_ring->count) next_to_clean = 0; atomic_set(&tpd_ring->next_to_clean, next_to_clean); atl1c_clean_buffer() checks ATL1C_BUFFER_FREE, unmaps and calls napi_consume_skb(). Only after that does it clear skb and dma. Can atl1c_clean_tx() and atl1c_clean_tx_ring() both see the same entry as live? That would mean a double DMA unmap and a double napi_consume_skb() of the same skb. The poll could also write a stale next_to_clean back after the reset has zeroed it. The argument that "a poll that starts later sees the carrier off and returns" holds for atl1c_clean_rx(), but not for atl1c_clean_tx(). The Fixes: commit 5e5c0964d9b9 added this reset specifically to release pending TX skbs. The notes below the --- list the TX side as not addressed, but those notes are dropped when the patch is applied. The second patch in the series does not touch the TX side either. Since this is tagged for stable, should the commit message say that the TX half of the reset is still racy? > + if (test_bit(__AT_DOWN, &adapter->flags)) > + return false; [Severity: High] This is a pre-existing issue, but does this check close the race with a concurrent atl1c_down()? __AT_DOWN is only tested while SCHED is observed set. If every RX SCHED bit is clear when sampled, the function returns true without looking at __AT_DOWN. Nothing rechecks it before atl1c_reset_mac() (which includes a msleep(10)), atl1c_reset_dma_ring() and atl1c_configure(). atl1c_down() does: set_bit(__AT_DOWN, &adapter->flags); netif_carrier_off(netdev); for (i = 0; i < adapter->tx_queue_count; ++i) napi_disable(&adapter->tpd_ring[i].napi); for (i = 0; i < adapter->rx_queue_count; ++i) napi_disable(&adapter->rrd_ring[i].napi); ... atl1c_reset_dma_ring(adapter); Only atl1c_close() calls cancel_work_sync(&adapter->common_task) first. These callers reach atl1c_down() while the work item may be running: - atl1c_change_mtu() - atl1c_reinit_locked() (ethtool) - atl1c_suspend() - PCI error recovery The work item takes neither __AT_RESETTING nor RTNL. Case one: atl1c_down() has set __AT_DOWN but has not yet disabled the RX NAPIs, or it starts after the wait has returned true. Both contexts then run atl1c_reset_dma_ring()->atl1c_clean_buffer() on the same buffer_info entries. Could that double-free and double-unmap the skbs? The work item's atl1c_configure() could also refill rings that the teardown is freeing. Case two: a full down/up finishes inside that window. atl1c_up()->atl1c_check_link_status(adapter, false) can turn the carrier back on, then clear __AT_DOWN and call napi_enable(). A new RX poll would then pass the carrier gate and walk the RRD/RFD rings while the work item frees them. The notes below the --- acknowledge the missing serialisation. The commit body, though, presents the __AT_DOWN check as the answer to a concurrent teardown. Does this need real exclusion? One option is to take __AT_RESETTING or the instance lock in the work item and recheck state under it. Another is to have every atl1c_down() caller call cancel_work_sync() first. > + if (!tries--) { > + dev_warn_ratelimited(&adapter->pdev->dev, > + "RX poll won't finish, skipping link-down reset\n"); > + return false; > + } > + usleep_range(1000, 2000); > + } > + } > + > + return true; > +} [ ... ] > @@ -265,6 +310,20 @@ static void atl1c_check_link_status(struct atl1c_adapter *adapter) > if (!link) { > /* link down */ > netif_carrier_off(netdev); > + if (sync_polls) { [ ... ] > + smp_mb(); > + if (!atl1c_rx_poll_sync(adapter)) > + return; [Severity: Medium] When atl1c_rx_poll_sync() returns false, this return skips the whole link-down hardware sequence, not only the ring reset: - hw->hibernate = true - atl1c_reset_mac() - atl1c_set_aspm(hw, SPEED_0) - atl1c_post_phy_linkchg(hw, SPEED_0) - atl1c_reset_dma_ring() - atl1c_configure() Nothing records that this work is still owed. The next link-change event is normally a link-up. atl1c_link_chg_event() has already set adapter->link_speed = SPEED_0, so the link-up branch runs atl1c_set_aspm(), atl1c_post_phy_linkchg() and atl1c_start_mac() on a MAC that was never stopped or reset. Would that leave TX descriptors pending in the DMA engine, the condition 5e5c0964d9b9 was added to fix? On msi_lnkpatch parts, the SPEED_0 branch of atl1c_post_phy_linkchg() restores MIIDBG_SYSMODCTRL and MIIDBG_MSE16DB. That branch is skipped. The link-up branch only rewrites those registers for SPEED_100, so a link that comes back at 1000M would keep the 100M thresholds. hw->hibernate also stays false while the link is down. On l2c_b2 and l1d_2, atl1c_read_phy_core() and atl1c_write_phy_core() then use the fast MDIO clock instead of MDIO_CTRL_CLK_25_128. Under NAPI threaded busy-poll the timeout is not rare, it happens every time. napi_complete_done() returns without clearing NAPI_STATE_SCHED while NAPIF_STATE_IN_BUSY_POLL is set: if (unlikely(n->state & (NAPIF_STATE_NPSVC | NAPIF_STATE_IN_BUSY_POLL))) return false; In that mode every link-down sleeps about 0.5-1 s in the work item with device interrupts masked, logs the warning and skips the reset. The commit message only says "the rings are left untouched and the handler runs again on the next link-change event". Could the steps that don't touch the rings (hibernate, set_aspm(SPEED_0), post_phy_linkchg(SPEED_0)) run before the wait? Alternatively, could the skipped MAC/DMA reset be deferred, for example by setting ATL1C_WORK_EVENT_RESET? > + } > hw->hibernate = true; > if (atl1c_reset_mac(hw) != 0) > if (netif_msg_hw(adapter)) [ ... ] -- Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261125113120.99743-1-federico%40rimpianto.com ^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH net v5 2/2] net: atl1c: free the RX buffer when the descriptor reports an error 2026-11-25 11:31 [PATCH net v5 0/2] net: atl1c: fix the RX buffer leak on errored frames Federico Musto 2026-11-25 11:31 ` [PATCH net v5 1/2] net: atl1c: wait for in-flight RX polls before the link-down ring reset Federico Musto @ 2026-11-25 11:31 ` Federico Musto 2026-11-29 11:48 ` netdev-bot+sashiko 1 sibling, 1 reply; 5+ messages in thread From: Federico Musto @ 2026-11-25 11:31 UTC (permalink / raw) To: Chris Snook, Andrew Lunn, David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni, netdev Cc: linux-kernel, Federico Musto, stable atl1c_clean_rx() drops a frame whose return descriptor (RRD) carries RRS_RX_ERR_SUM or RRS_802_3_LEN_ERR by calling atl1c_clean_rfd(), which only marks the RFD free and clears the skb pointer. Nothing unmaps the DMA mapping and nothing frees the skb, so every errored frame - CRC, alignment, truncation, runt - leaks one skb and one DMA mapping. The driver uses a 32-bit DMA mask, so on a host without an IOMMU each of those mappings also pins a swiotlb slot. Unmap the buffer and free the skb before the RFD is recycled. Freeing makes the RFD index and count found in the RRD matter more than they did. Both are written by the device and neither is checked: the index field is 12 bits wide for a ring of 512 entries, and the count can be 0 to 15 while one RFD per frame is all the driver handles. A wrong index used to leak a buffer; it would now unmap and free one the device still owns, or one outside the ring. So validate the RRD first, the way alx_clean_rx_irq() does for the same descriptor layout: the count must be 1, the index must be the one the driver expects back next (rfd_ring->next_to_clean) and that slot must hold a buffer. Otherwise touch no buffer and schedule a reset through ATL1C_WORK_EVENT_RESET, as the interrupt handler does on ISR_ERROR. After the check the slot is addressed through the driver's own index, in the good receive path too, which took the unchecked index from the descriptor in the same way. Reading the descriptor is now ordered as well: a dma_rmb() separates the valid-bit test on word3 from the rest of the descriptor. The control dependency does not order the loads, and word0 now decides whether a reset is scheduled; without the barrier a stale word0 - the ring is zero after open and after every reset - would schedule a spurious one on weakly ordered CPUs. The check also covers two cases the count was left to cause. An RRD with a count of 0 was never consumed and RX stalled on it, in an endless loop inside the poll when it also had an error bit set. A good RRD with a count above 1 was consumed without recycling its RFDs. Count an errored descriptor as work, so that the refill runs when a poll sees no good frame. Clear the RRD ring on reset: the RRD and RFD rings advance in step, so an RRD left over from before a reset would pass the check above and release a freshly posted buffer the device still owns. The hardware already counts these frames in its own error counters, which atl1c_get_stats() reports, so no software counter is added here. Found by code inspection; the error path has not been exercised on hardware. The RRD validation has been runtime-tested only on a single-queue device presenting as an AR8151 (emulated), not on genuine Atheros hardware and not on a multi-queue device. Fixes: 43250ddd75a3 ("atl1c: Atheros L1C Gigabit Ethernet driver") Cc: stable@vger.kernel.org Signed-off-by: Federico Musto <federico@rimpianto.com> --- v4 -> v5, after the Sashiko AI review of v4: - add dma_rmb() between the valid-bit test on word3 and the reads of the rest of the descriptor; the RRD ring is zeroed after a reset, so a stale word0 would otherwise read as rfd_num 0 and schedule a spurious reset on weakly ordered CPUs; - no other change. Runtime validation of the RRD check so far covers an emulated single-queue AR8151 only; Tested-by: on genuine Atheros hardware, in particular multi-queue, is welcome. This patch applies textually only on top of patch 1/2; please keep the two together. v3 -> v4, after the Sashiko AI review of v3: - validate the RFD count and index of the RRD against rfd_ring->next_to_clean, and that the slot holds a buffer, before any buffer is touched; reset on a mismatch, as alx_clean_rx_irq() does; - address the buffer through the driver's own index in the error path and in the good path; atl1c_clean_rfd() takes nothing from the descriptor any more; - drop the multi-RFD loop added in v3: a count other than 1 resets; - a count of 0 no longer leaves the RRD unconsumed; - new patch 1/2 for the link-down reset race. Pre-existing and left to a separate patch: word3 is __le32 but is read without conversion. v4: https://lore.kernel.org/netdev/20261117225521.19689-1-federico@rimpianto.com/ v3: https://lore.kernel.org/netdev/20261103225218.19206-1-federico@rimpianto.com/ v2: https://lore.kernel.org/netdev/20261102215253.68185-1-federico@rimpianto.com/ v1: https://lore.kernel.org/netdev/20261027115156.17128-1-federico@rimpianto.com/ --- .../net/ethernet/atheros/atl1c/atl1c_main.c | 104 ++++++++++-------- 1 file changed, 59 insertions(+), 45 deletions(-) diff --git a/drivers/net/ethernet/atheros/atl1c/atl1c_main.c b/drivers/net/ethernet/atheros/atl1c/atl1c_main.c index afab3e1..8383203 100644 --- a/drivers/net/ethernet/atheros/atl1c/atl1c_main.c +++ b/drivers/net/ethernet/atheros/atl1c/atl1c_main.c @@ -971,8 +971,9 @@ static void atl1c_clean_rx_ring(struct atl1c_adapter *adapter, u32 queue) buffer_info = &rfd_ring->buffer_info[j]; atl1c_clean_buffer(pdev, buffer_info, 0); } - /* zero out the descriptor ring */ + /* zero out the descriptor rings */ memset(rfd_ring->desc, 0, rfd_ring->size); + memset(rrd_ring->desc, 0, rrd_ring->size); rfd_ring->next_to_clean = 0; rfd_ring->next_to_use = 0; rrd_ring->next_to_use = 0; @@ -1904,23 +1905,16 @@ static void atl1c_clean_rrd(struct atl1c_rrd_ring *rrd_ring, } } -static void atl1c_clean_rfd(struct atl1c_rfd_ring *rfd_ring, - struct atl1c_recv_ret_status *rrs, u16 num) +/* Recycle the RFD the driver expects back next. The caller owns the skb. */ +static void atl1c_clean_rfd(struct atl1c_rfd_ring *rfd_ring) { - u16 i; - u16 rfd_index; - struct atl1c_buffer *buffer_info = rfd_ring->buffer_info; + struct atl1c_buffer *buffer_info; - rfd_index = (rrs->word0 >> RRS_RX_RFD_INDEX_SHIFT) & - RRS_RX_RFD_INDEX_MASK; - for (i = 0; i < num; i++) { - buffer_info[rfd_index].skb = NULL; - ATL1C_SET_BUFFER_STATE(&buffer_info[rfd_index], - ATL1C_BUFFER_FREE); - if (++rfd_index == rfd_ring->count) - rfd_index = 0; - } - rfd_ring->next_to_clean = rfd_index; + buffer_info = &rfd_ring->buffer_info[rfd_ring->next_to_clean]; + buffer_info->skb = NULL; + ATL1C_SET_BUFFER_STATE(buffer_info, ATL1C_BUFFER_FREE); + if (++rfd_ring->next_to_clean == rfd_ring->count) + rfd_ring->next_to_clean = 0; } /** @@ -1934,6 +1928,7 @@ static int atl1c_clean_rx(struct napi_struct *napi, int budget) container_of(napi, struct atl1c_rrd_ring, napi); struct atl1c_adapter *adapter = rrd_ring->adapter; u16 rfd_num, rfd_index; + u32 rrs_word0; u16 length; struct pci_dev *pdev = adapter->pdev; struct net_device *netdev = adapter->netdev; @@ -1952,47 +1947,66 @@ static int atl1c_clean_rx(struct napi_struct *napi, int budget) if (work_done >= budget) break; rrs = ATL1C_RRD_DESC(rrd_ring, rrd_ring->next_to_clean); - if (likely(RRS_RXD_IS_VALID(rrs->word3))) { - rfd_num = (rrs->word0 >> RRS_RX_RFD_CNT_SHIFT) & - RRS_RX_RFD_CNT_MASK; - if (unlikely(rfd_num != 1)) - /* TODO support mul rfd*/ - if (netif_msg_rx_err(adapter)) - dev_warn(&pdev->dev, - "Multi rfd not support yet!\n"); - goto rrs_checked; - } else { + if (!RRS_RXD_IS_VALID(rrs->word3)) + break; + + /* Order the rest of the descriptor after the valid bit: on + * weakly ordered CPUs word0 can otherwise be read before + * the device's write of this RRD is visible, and a stale + * word0 (the ring is zeroed after a reset) would schedule + * a spurious reset. + */ + dma_rmb(); + rrs_word0 = le32_to_cpu(rrs->word0); + rfd_num = (rrs_word0 >> RRS_RX_RFD_CNT_SHIFT) & + RRS_RX_RFD_CNT_MASK; + rfd_index = (rrs_word0 >> RRS_RX_RFD_INDEX_SHIFT) & + RRS_RX_RFD_INDEX_MASK; + buffer_info = &rfd_ring->buffer_info[rfd_ring->next_to_clean]; + + /* The RRD is written by the device. Its RFD index field is + * wider than the ring, and only one RFD per frame is + * supported. Check both against the driver's own ring state, + * and that the slot holds a buffer, before anything is + * unmapped or freed. When they disagree there is no telling + * which buffers the device still owns: leave them all alone + * and reset. + */ + if (unlikely(rfd_num != 1 || + rfd_index != rfd_ring->next_to_clean || + !buffer_info->skb)) { + dev_err_ratelimited(&pdev->dev, + "RRD out of sync: RFD count %u index %u, expected index %u%s; resetting\n", + rfd_num, rfd_index, + rfd_ring->next_to_clean, + buffer_info->skb ? "" : " (no buffer)"); + set_bit(ATL1C_WORK_EVENT_RESET, &adapter->work_event); + schedule_work(&adapter->common_task); break; } -rrs_checked: - atl1c_clean_rrd(rrd_ring, rrs, rfd_num); + + atl1c_clean_rrd(rrd_ring, rrs, 1); + dma_unmap_single(&pdev->dev, buffer_info->dma, + buffer_info->length, DMA_FROM_DEVICE); + skb = buffer_info->skb; + atl1c_clean_rfd(rfd_ring); + if (rrs->word3 & (RRS_RX_ERR_SUM | RRS_802_3_LEN_ERR)) { - atl1c_clean_rfd(rfd_ring, rrs, rfd_num); if (netif_msg_rx_err(adapter)) dev_warn(&pdev->dev, "wrong packet! rrs word3 is %x\n", rrs->word3); + kfree_skb_reason(skb, SKB_DROP_REASON_NOT_SPECIFIED); + /* The RFD has been recycled, so count the drop as + * work: the refill after the loop only runs when + * some was done. + */ + work_done++; continue; } length = le16_to_cpu((rrs->word3 >> RRS_PKT_SIZE_SHIFT) & RRS_PKT_SIZE_MASK); - /* Good Receive */ - if (likely(rfd_num == 1)) { - rfd_index = (rrs->word0 >> RRS_RX_RFD_INDEX_SHIFT) & - RRS_RX_RFD_INDEX_MASK; - buffer_info = &rfd_ring->buffer_info[rfd_index]; - dma_unmap_single(&pdev->dev, buffer_info->dma, - buffer_info->length, DMA_FROM_DEVICE); - skb = buffer_info->skb; - } else { - /* TODO */ - if (netif_msg_rx_err(adapter)) - dev_warn(&pdev->dev, - "Multi rfd not support yet!\n"); - break; - } - atl1c_clean_rfd(rfd_ring, rrs, rfd_num); /* A link partner whose l2mtu is larger than ours can hand us a * frame that does not fit the buffer the hardware was given: -- 2.54.0 (Apple Git-157) ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH net v5 2/2] net: atl1c: free the RX buffer when the descriptor reports an error 2026-11-25 11:31 ` [PATCH net v5 2/2] net: atl1c: free the RX buffer when the descriptor reports an error Federico Musto @ 2026-11-29 11:48 ` netdev-bot+sashiko 0 siblings, 0 replies; 5+ messages in thread From: netdev-bot+sashiko @ 2026-11-29 11:48 UTC (permalink / raw) To: federico Cc: chris.snook, andrew+netdev, davem, edumazet, kuba, pabeni, netdev, linux-kernel Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider. Critical: 0 · High: 0 · Medium: 1 · 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). - [Medium] In atl1c_clean_rx() (drivers/net/ethernet/atheros/atl1c/atl1c_main.c), the new RRD check… -- net: atl1c: free the RX buffer when the descriptor reports an error This commit changes atl1c_clean_rx() so that errored frames no longer leak their skb and DMA mapping: the buffer is now unmapped and freed. It also checks the RFD count and index in each RRD against rfd_ring->next_to_clean. On a mismatch it schedules an adapter reset through ATL1C_WORK_EVENT_RESET. Finally, it adds a dma_rmb() after the valid-bit test and clears the RRD ring in atl1c_clean_rx_ring(). > diff --git a/drivers/net/ethernet/atheros/atl1c/atl1c_main.c b/drivers/net/ethernet/atheros/atl1c/atl1c_main.c > index afab3e16903ba..8383203623c17 100644 > --- a/drivers/net/ethernet/atheros/atl1c/atl1c_main.c > +++ b/drivers/net/ethernet/atheros/atl1c/atl1c_main.c [ ... ] > @@ -1952,47 +1947,66 @@ static int atl1c_clean_rx(struct napi_struct *napi, int budget) > if (work_done >= budget) > break; > rrs = ATL1C_RRD_DESC(rrd_ring, rrd_ring->next_to_clean); > - if (likely(RRS_RXD_IS_VALID(rrs->word3))) { > - rfd_num = (rrs->word0 >> RRS_RX_RFD_CNT_SHIFT) & > - RRS_RX_RFD_CNT_MASK; > - if (unlikely(rfd_num != 1)) > - /* TODO support mul rfd*/ > - if (netif_msg_rx_err(adapter)) > - dev_warn(&pdev->dev, > - "Multi rfd not support yet!\n"); > - goto rrs_checked; > - } else { > + if (!RRS_RXD_IS_VALID(rrs->word3)) > + break; > + [ ... ] > + rrs_word0 = le32_to_cpu(rrs->word0); > + rfd_num = (rrs_word0 >> RRS_RX_RFD_CNT_SHIFT) & > + RRS_RX_RFD_CNT_MASK; > + rfd_index = (rrs_word0 >> RRS_RX_RFD_INDEX_SHIFT) & > + RRS_RX_RFD_INDEX_MASK; > + buffer_info = &rfd_ring->buffer_info[rfd_ring->next_to_clean]; > + [ ... ] > + if (unlikely(rfd_num != 1 || > + rfd_index != rfd_ring->next_to_clean || > + !buffer_info->skb)) { > + dev_err_ratelimited(&pdev->dev, > + "RRD out of sync: RFD count %u index %u, expected index %u%s; resetting\n", > + rfd_num, rfd_index, > + rfd_ring->next_to_clean, > + buffer_info->skb ? "" : " (no buffer)"); > + set_bit(ATL1C_WORK_EVENT_RESET, &adapter->work_event); > + schedule_work(&adapter->common_task); > break; > } [Severity: Medium] Can this check turn a legitimate multi-RFD frame into a full controller reset? For any mtu <= 1522, atl1c_set_rxbufsize() leaves rx_buffer_len at AT_RX_BUF_SIZE (1522): adapter->rx_buffer_len = mtu > AT_RX_BUF_SIZE ? roundup(mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN, 8) : AT_RX_BUF_SIZE; atl1c_configure_rx() writes that value to REG_RX_BUF_SIZE. Meanwhile atl1c_configure_mac() sets the MAC frame limit to mtu + 22: AT_WRITE_REG(hw, REG_MTU, hw->max_frame_size + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN); So with 1501 <= mtu <= 1522 (mtu 1508 for RFC 4638 PPPoE, for example), the MAC accepts frames of up to 1523..1544 bytes into 1522-byte RFDs. Suppose the device splits such a frame over two RFDs and reports RRS_RX_RFD_CNT = 2. This branch then sets ATL1C_WORK_EVENT_RESET, and atl1c_common_task() runs netif_device_detach(), atl1c_down() and atl1c_up() once per frame. Errored multi-RFD frames (RRS_RX_ERR_SUM or RRS_802_3_LEN_ERR) used to be consumed by atl1c_clean_rrd() and atl1c_clean_rfd() with rfd_num, and RX carried on. With this patch they also reset the controller. Nothing limits how often the reset can be scheduled, so a peer that keeps sending such frames could keep the link flapping. The commit message names alx_clean_rx_irq() as the model. alx can treat a count other than 1 as fatal because its rxbuf_size comes from ALX_MAX_FRAME_LEN(mtu), which is always at least the ALX_MTU register value. atl1c does not hold to that for mtu 1501..1522. It isn't confirmed whether the hardware splits an oversized frame across RFDs or truncates it into one. The old "TODO support mul rfd" branches and the 4-bit RFD count field suggest that splitting can happen. Would it work to size rx_buffer_len from the same expression as REG_MTU, for example max(AT_RX_BUF_SIZE, roundup(mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN, 8))? Another option is to drop and recycle a multi-RFD frame whose index matches next_to_clean, and keep the reset for a real desync. [ ... ] -- Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261125113120.99743-1-federico%40rimpianto.com ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-29 11:48 UTC | newest] Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2026-11-25 11:31 [PATCH net v5 0/2] net: atl1c: fix the RX buffer leak on errored frames Federico Musto 2026-11-25 11:31 ` [PATCH net v5 1/2] net: atl1c: wait for in-flight RX polls before the link-down ring reset Federico Musto 2026-11-29 11:48 ` netdev-bot+sashiko 2026-11-25 11:31 ` [PATCH net v5 2/2] net: atl1c: free the RX buffer when the descriptor reports an error Federico Musto 2026-11-29 11:48 ` 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
all inboxes | Powered by JetHome®