[PATCH net v5 2/2] net: atl1c: free the RX buffer when the descriptor reports an error

From: Federico Musto

Date: Wed Nov 25 2026 - 06:32:45 EST


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@xxxxxxxxxxxxxxx
Signed-off-by: Federico Musto <federico@xxxxxxxxxxxxx>
---
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@xxxxxxxxxxxxx/
v3: https://lore.kernel.org/netdev/20261103225218.19206-1-federico@xxxxxxxxxxxxx/
v2: https://lore.kernel.org/netdev/20261102215253.68185-1-federico@xxxxxxxxxxxxx/
v1: https://lore.kernel.org/netdev/20261027115156.17128-1-federico@xxxxxxxxxxxxx/
---
.../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)