net: atl1c: fix the RX buffer leak on errored frames
Subsystems (4)
v5 cover net: atl1c: fix the RX buffer leak on errored frames
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/[email protected]/
v3: https://lore.kernel.org/netdev/[email protected]/
v2: https://lore.kernel.org/netdev/[email protected]/
v1: https://lore.kernel.org/netdev/[email protected]/
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(-)
Signature · 1 line
2.54.0 (Apple Git-157)
PATCH v5 1/2 net: atl1c: wait for in-flight RX polls before the link-down ring reset
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.
---
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(-)
drivers/net/ethernet/atheros/atl1c/atl1c_main.c +62 -3
@@ -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);
Signature · 1 line
2.54.0 (Apple Git-157)
PATCH v5 2/2 net: atl1c: free the RX buffer when the descriptor reports an error
In reply to Federico Mustoatl1c_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.
---
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/[email protected]/
v3: https://lore.kernel.org/netdev/[email protected]/
v2: https://lore.kernel.org/netdev/[email protected]/
v1: https://lore.kernel.org/netdev/[email protected]/
--- .../net/ethernet/atheros/atl1c/atl1c_main.c | 104 ++++++++++-------- 1 file changed, 59 insertions(+), 45 deletions(-)
drivers/net/ethernet/atheros/atl1c/atl1c_main.c +59 -45
@@ -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:
Signature · 1 line
2.54.0 (Apple Git-157)