Skip to contentSkip to conversation

net: xilinx: axienet: Free outstanding DMA buffers on dmaengine stop

PATCH Superseded Suraj Gupta 2 participants 2 messages Sep 24 – Sep 28

Subsystems (4)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 net: xilinx: axienet: Free outstanding DMA buffers on dmaengine stop

In the dmaengine path the driver pre-submits RX buffers and holds in-flight TX buffers whose SKBs are DMA-mapped by the driver and freed only in the completion callbacks. On ndo_stop() dmaengine_terminate_sync() aborts these descriptors without running their callbacks, and the driver then frees only the ring shells, leaking every SKB still owned by the engine and its DMA mapping on each ifdown. With 128 RX buffers pre-posted per channel, the mapping leak can eventually exhaust a limited IOMMU aperture.

Clear the slot's skb in the TX and RX callbacks so a non-NULL skb marks a slot that still owns a live, DMA-mapped buffer, and on stop unmap and free every such buffer.

axienet_dma_rx_cb() runs from the DMA tasklet and re-arms the RX ring on each completion, so it can race axienet_stop(): a completion may submit a fresh buffer after dmaengine_terminate_sync() has returned, leaving the channel armed with a buffer the teardown then frees while the engine may still write into it (dma_release_channel() does not stop it either). Add a lock that axienet_dma_rx_cb() holds across the @stopping check and the resubmit, and axienet_stop() holds to set @stopping before terminating. Once @stopping is set no callback can arm a new buffer, and any armed just before is aborted by the terminate, so teardown only frees buffers the engine no longer owns.

Fixes 6a91b846af85 ("net: axienet: Introduce dmaengine support")

---
Changes in v3:
- Serialize RX descriptor resubmission in axienet_dma_rx_cb() against the stop with a dedicated rx_submit_lock, replacing the bare READ_ONCE/WRITE_ONCE @stopping fence. (reported by the netdev Sashiko AI bot).
- Update the commit message to describe the race and its fix.
v2: https://lore.kernel.org/netdev/[email protected]/

Changes in v2:
- Free outstanding TX/RX buffers on the dmaengine stop path (the original fix), and drop the redundant dmaengine_synchronize() calls that followed dmaengine_terminate_sync() (Jakub Kicinski).
v1: https://lore.kernel.org/netdev/[email protected]/

---
 drivers/net/ethernet/xilinx/xilinx_axienet.h  |  7 ++-
 .../net/ethernet/xilinx/xilinx_axienet_main.c | 51 +++++++++++++++----
 2 files changed, 47 insertions(+), 11 deletions(-)
drivers/net/ethernet/xilinx/xilinx_axienet.h +5 -2
index fcd3aaef27fc..c5c505269ec9 100644
@@ -523,8 +523,9 @@ struct skbuf_dma_descriptor {
  * @stats_work: Work for reading the hardware statistics counters often enough
  *              to catch overflows.
  * @dma_err_task: Work structure to process Axi DMA errors
- * @stopping:   Set when @dma_err_task shouldn't do anything because we are
- *              about to stop the device.
+ * @stopping:   Set when we are about to stop the device: makes @dma_err_task
+ *              a no-op (legacy DMA path) and fences RX descriptor
+ *              resubmission in axienet_dma_rx_cb() (dmaengine path).
  * @tx_irq:	Axidma TX IRQ number
  * @rx_irq:	Axidma RX IRQ number
  * @eth_irq:	Ethernet core IRQ number
@@ -545,6 +546,7 @@ struct skbuf_dma_descriptor {
  * @tx_ring_tail: TX skb ring buffer tail index.
  * @rx_ring_head: RX skb ring buffer head index.
  * @rx_ring_tail: RX skb ring buffer tail index.
+ * @rx_submit_lock: Protects RX ring resubmission vs teardown in dmaengine path.
  */
 struct axienet_local {
 	struct net_device *ndev;
@@ -626,6 +628,7 @@ struct axienet_local {
 	int tx_ring_tail;
 	int rx_ring_head;
 	int rx_ring_tail;
+	spinlock_t rx_submit_lock;
 };
 
 /**
drivers/net/ethernet/xilinx/xilinx_axienet_main.c +42 -9

96 lines not on this page · raw message

Signature · 1 line
2.25.1
reviewer
#1 lore raw
Quoted 27 lines · Suraj Gupta

In the dmaengine path the driver pre-submits RX buffers and holds in-flight TX buffers whose SKBs are DMA-mapped by the driver and freed only in the completion callbacks. On ndo_stop() dmaengine_terminate_sync() aborts these descriptors without running their callbacks, and the driver then frees only the ring shells, leaking every SKB still owned by the engine and its DMA mapping on each ifdown. With 128 RX buffers pre-posted per channel, the mapping leak can eventually exhaust a limited IOMMU aperture.

Clear the slot's skb in the TX and RX callbacks so a non-NULL skb marks a slot that still owns a live, DMA-mapped buffer, and on stop unmap and free every such buffer.

axienet_dma_rx_cb() runs from the DMA tasklet and re-arms the RX ring on each completion, so it can race axienet_stop(): a completion may submit a fresh buffer after dmaengine_terminate_sync() has returned, leaving the channel armed with a buffer the teardown then frees while the engine may still write into it (dma_release_channel() does not stop it either). Add a lock that axienet_dma_rx_cb() holds across the @stopping check and the resubmit, and axienet_stop() holds to set @stopping before terminating. Once @stopping is set no callback can arm a new buffer, and any armed just before is aborted by the terminate, so teardown only frees buffers the engine no longer owns.

Fixes 6a91b846af85 ("net: axienet: Introduce dmaengine support")
Quoted 7 lines

---
Changes in v3:
- Serialize RX descriptor resubmission in axienet_dma_rx_cb() against the stop with a dedicated rx_submit_lock, replacing the bare READ_ONCE/WRITE_ONCE @stopping fence. (reported by the netdev Sashiko AI bot).
- Update the commit message to describe the race and its fix.

The use of a spinlock looks correct to me.
But, if so, I think that plain acecsses to stopping should be used:
READ_ONCE/WRITE_ONCE wrappers should be removed.

I mean, like this:

	spin_lock(&lp->rx_submit_lock);
	if (lp->stopping) {
		spin_unlock(&lp->rx_submit_lock);
		return;
	}
	...
	spin_unlock(&lp->rx_submit_lock);

And:

		spin_lock_bh(&lp->rx_submit_lock);
		lp->stopping = true;
		spin_unlock_bh(&lp->rx_submit_lock);
Quoted 7 lines

v2: https://lore.kernel.org/netdev/[email protected]/

Changes in v2:
- Free outstanding TX/RX buffers on the dmaengine stop path (the original fix), and drop the redundant dmaengine_synchronize() calls that followed dmaengine_terminate_sync() (Jakub Kicinski).
v1: https://lore.kernel.org/netdev/[email protected]/

Signature · 1 line
pw-bot: changes-requested