Skip to contentSkip to conversation

net: xilinx: axienet: Fix race in axienet_stop

PATCH Applied Sean Anderson 2 participants 2 messages Sep 10, 2024 – Sep 13, 2024

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: xilinx: axienet: Fix race in axienet_stop

[ Upstream commit 858430db28a5f5a11f8faa3a6fa805438e6f0851 ]

axienet_dma_err_handler can race with axienet_stop in the following manner:

CPU 1 CPU 2
====================== ==================
axienet_stop()

    napi_disable()
    axienet_dma_stop()
                            axienet_dma_err_handler()
                                napi_disable()
                                axienet_dma_stop()
                                axienet_dma_start()
                                napi_enable()
    cancel_work_sync()
    free_irq()

Fix this by setting a flag in axienet_stop telling axienet_dma_err_handler not to bother doing anything. I chose not to use disable_work_sync to allow for easier backporting.

Fixes 8a3b7a252dca ("drivers/net/ethernet/xilinx: added Xilinx AXI Ethernet driver")
[ Adjusted to apply before dmaengine support ]

---
This patch is adjusted to apply to v6.6 kernels and earlier, which do not contain commit 6a91b846af85 ("net: axienet: Introduce dmaengine support").

 drivers/net/ethernet/xilinx/xilinx_axienet.h      | 3 +++
 drivers/net/ethernet/xilinx/xilinx_axienet_main.c | 8 ++++++++
 2 files changed, 11 insertions(+)
drivers/net/ethernet/xilinx/xilinx_axienet.h +3 -0
index f09f10f17d7e..2facbdfbb319 100644
@@ -419,6 +419,8 @@ struct axidma_bd {
  * @tx_bytes:	TX byte count for statistics
  * @tx_stat_sync: Synchronization object for TX stats
  * @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.
  * @tx_irq:	Axidma TX IRQ number
  * @rx_irq:	Axidma RX IRQ number
  * @eth_irq:	Ethernet core IRQ number
@@ -481,6 +483,7 @@ struct axienet_local {
 	struct u64_stats_sync tx_stat_sync;
 
 	struct work_struct dma_err_task;
+	bool stopping;
 
 	int tx_irq;
 	int rx_irq;
drivers/net/ethernet/xilinx/xilinx_axienet_main.c +8 -0
index 144feb7a2fda..65d7aaad43fe 100644
@@ -1162,6 +1162,7 @@ static int axienet_open(struct net_device *ndev)
 	phylink_start(lp->phylink);
 
 	/* Enable worker thread for Axi DMA error handling */
+	lp->stopping = false;
 	INIT_WORK(&lp->dma_err_task, axienet_dma_err_handler);
 
 	napi_enable(&lp->napi_rx);
@@ -1217,6 +1218,9 @@ static int axienet_stop(struct net_device *ndev)
 
 	dev_dbg(&ndev->dev, "axienet_close()\n");
 
+	WRITE_ONCE(lp->stopping, true);
+	flush_work(&lp->dma_err_task);
+
 	napi_disable(&lp->napi_tx);
 	napi_disable(&lp->napi_rx);
 
@@ -1761,6 +1765,10 @@ static void axienet_dma_err_handler(struct work_struct *work)
 						dma_err_task);
 	struct net_device *ndev = lp->ndev;
 
+	/* Don't bother if we are going to stop anyway */
+	if (READ_ONCE(lp->stopping))
+		return;
+
 	napi_disable(&lp->napi_tx);
 	napi_disable(&lp->napi_rx);
 
Signature · 1 line
2.35.1.1320.gc452695387.dirty
maintainer Applied
#1 lore raw
Quoted 28 lines · Sean Anderson

[ Upstream commit 858430db28a5f5a11f8faa3a6fa805438e6f0851 ]

axienet_dma_err_handler can race with axienet_stop in the following manner:

CPU 1 CPU 2
====================== ==================
axienet_stop()

    napi_disable()
    axienet_dma_stop()
                            axienet_dma_err_handler()
                                napi_disable()
                                axienet_dma_stop()
                                axienet_dma_start()
                                napi_enable()
    cancel_work_sync()
    free_irq()

Fix this by setting a flag in axienet_stop telling axienet_dma_err_handler not to bother doing anything. I chose not to use disable_work_sync to allow for easier backporting.

Fixes 8a3b7a252dca ("drivers/net/ethernet/xilinx: added Xilinx AXI Ethernet driver")
[ Adjusted to apply before dmaengine support ]
Quoted 4 lines

---
This patch is adjusted to apply to v6.6 kernels and earlier, which do not contain commit 6a91b846af85 ("net: axienet: Introduce dmaengine support").

Applied to 6.6.y and 6.1.y, but nothing older as it didn't apply to 5.15.y :(

thanks,

greg k-h