Skip to contentSkip to conversation

net: axienet: Use NAPI for TX completion path

PATCH In review Robert Hancock 2 participants 3 messages May 9, 2022 – May 11, 2022

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

PATCH v5 1/1 net: axienet: Use NAPI for TX completion path

This driver was using the TX IRQ handler to perform all TX completion tasks. Under heavy TX network load, this can cause significant irqs-off latencies (found to be in the hundreds of microseconds using ftrace). This can cause other issues, such as overrunning serial UART FIFOs when using high baud rates with limited UART FIFO sizes.

Switch to using a NAPI poll handler to perform the TX completion work to get this out of hard IRQ context and avoid the IRQ latency impact. A separate poll handler is used for TX and RX since they have separate IRQs on this controller, so that the completion work for each of them stays on the same CPU as the interrupt.

Testing on a Xilinx MPSoC ZU9EG platform using iperf3 from a Linux PC through a switch at 1G link speed showed no significant change in TX or RX throughput, with approximately 941 Mbps before and after. Hard IRQ time in the TX throughput test was significantly reduced from 12% to below 1% on the CPU handling TX interrupts, with total hard+soft IRQ CPU usage dropping from about 56% down to 48%.

---

Changed since v4: Added locking to protect TX ring tail pointer against concurrent access by TX transmit and TX poll paths.

Changed since v3: Fixed references to renamed function in comments

Changed since v2: Use separate TX and RX NAPI poll handlers to keep completion handling on same CPU as TX/RX IRQ. Added hard/soft IRQ benchmark information to commit message.

Changed since v1: Added benchmark information to commit message, no code changes.

 drivers/net/ethernet/xilinx/xilinx_axienet.h  |  56 ++++---
 .../net/ethernet/xilinx/xilinx_axienet_main.c | 158 +++++++++++-------
 2 files changed, 128 insertions(+), 86 deletions(-)
drivers/net/ethernet/xilinx/xilinx_axienet.h +30 -26
index d5c1e5c4a508..5fe3f269c931 100644
@@ -385,7 +385,6 @@ struct axidma_bd {
  * @phy_node:	Pointer to device node structure
  * @phylink:	Pointer to phylink instance
  * @phylink_config: phylink configuration settings
- * @napi:	NAPI control structure
  * @pcs_phy:	Reference to PCS/PMA PHY if used
  * @pcs:	phylink pcs structure for PCS PHY
  * @switch_x_sgmii: Whether switchable 1000BaseX/SGMII mode is enabled in the core
@@ -396,7 +395,23 @@ struct axidma_bd {
  * @regs_start: Resource start for axienet device addresses
  * @regs:	Base address for the axienet_local device address space
  * @dma_regs:	Base address for the axidma device address space
+ * @napi_rx:	NAPI RX control structure
  * @rx_dma_cr:  Nominal content of RX DMA control register
+ * @rx_bd_v:	Virtual address of the RX buffer descriptor ring
+ * @rx_bd_p:	Physical address(start address) of the RX buffer descr. ring
+ * @rx_bd_num:	Size of RX buffer descriptor ring
+ * @rx_bd_ci:	Stores the index of the Rx buffer descriptor in the ring being
+ *		accessed currently.
+ * @napi_tx:	NAPI TX control structure
+ * @tx_dma_cr:  Nominal content of TX DMA control register
+ * @tx_bd_v:	Virtual address of the TX buffer descriptor ring
+ * @tx_bd_p:	Physical address(start address) of the TX buffer descr. ring
+ * @tx_bd_num:	Size of TX buffer descriptor ring
+ * @tx_bd_ci:	Stores the next Tx buffer descriptor in the ring that may be
+ *		complete. Only updated at runtime by TX NAPI poll.
+ * @tx_bd_tail_lock:	Spinlock to protect TX BD tail pointer value
+ * @tx_bd_tail:	Stores the index of the last Tx buffer descriptor in the ring
+ *              to be populated. Protected by tx_bd_tail_lock.
  * @dma_err_task: Work structure to process Axi DMA errors
  * @tx_irq:	Axidma TX IRQ number
  * @rx_irq:	Axidma RX IRQ number
@@ -404,19 +419,6 @@ struct axidma_bd {
  * @phy_mode:	Phy type to identify between MII/GMII/RGMII/SGMII/1000 Base-X
  * @options:	AxiEthernet option word
  * @features:	Stores the extended features supported by the axienet hw
- * @tx_bd_v:	Virtual address of the TX buffer descriptor ring
- * @tx_bd_p:	Physical address(start address) of the TX buffer descr. ring
- * @tx_bd_num:	Size of TX buffer descriptor ring
- * @rx_bd_v:	Virtual address of the RX buffer descriptor ring
- * @rx_bd_p:	Physical address(start address) of the RX buffer descr. ring
- * @rx_bd_num:	Size of RX buffer descriptor ring
- * @tx_bd_ci:	Stores the index of the Tx buffer descriptor in the ring being
- *		accessed currently. Used while alloc. BDs before a TX starts
- * @tx_bd_tail:	Stores the index of the Tx buffer descriptor in the ring being
- *		accessed currently. Used while processing BDs after the TX
- *		completed.
- * @rx_bd_ci:	Stores the index of the Rx buffer descriptor in the ring being
- *		accessed currently.
  * @max_frm_size: Stores the maximum size of the frame that can be that
  *		  Txed/Rxed in the existing hardware. If jumbo option is
  *		  supported, the maximum frame size would be 9k. Else it is
@@ -436,8 +438,6 @@ struct axienet_local {
 	struct phylink *phylink;
 	struct phylink_config phylink_config;
 
-	struct napi_struct napi;
-
 	struct mdio_device *pcs_phy;
 	struct phylink_pcs pcs;
 
@@ -453,7 +453,21 @@ struct axienet_local {
 	void __iomem *regs;
 	void __iomem *dma_regs;
 
+	struct napi_struct napi_rx;
 	u32 rx_dma_cr;
+	struct axidma_bd *rx_bd_v;
+	dma_addr_t rx_bd_p;
+	u32 rx_bd_num;
+	u32 rx_bd_ci;
+
+	struct napi_struct napi_tx;
+	u32 tx_dma_cr;
+	struct axidma_bd *tx_bd_v;
+	dma_addr_t tx_bd_p;
+	u32 tx_bd_num;
+	u32 tx_bd_ci;
+	spinlock_t tx_bd_tail_lock;
+	u32 tx_bd_tail;
 
 	struct work_struct dma_err_task;
 
@@ -465,16 +479,6 @@ struct axienet_local {
 	u32 options;
 	u32 features;
 
-	struct axidma_bd *tx_bd_v;
-	dma_addr_t tx_bd_p;
-	u32 tx_bd_num;
-	struct axidma_bd *rx_bd_v;
-	dma_addr_t rx_bd_p;
-	u32 rx_bd_num;
-	u32 tx_bd_ci;
-	u32 tx_bd_tail;
-	u32 rx_bd_ci;
-
 	u32 max_frm_size;
 	u32 rxmem;
 
drivers/net/ethernet/xilinx/xilinx_axienet_main.c +98 -60

347 lines not on this page · raw message

Signature · 1 line
2.31.1
maintainer
#1 lore raw
Quoted 19 lines · On Mon, 9 May 2022 11:30:39 -0600 Robert Hancock wrote:

This driver was using the TX IRQ handler to perform all TX completion tasks. Under heavy TX network load, this can cause significant irqs-off latencies (found to be in the hundreds of microseconds using ftrace). This can cause other issues, such as overrunning serial UART FIFOs when using high baud rates with limited UART FIFO sizes.

Switch to using a NAPI poll handler to perform the TX completion work to get this out of hard IRQ context and avoid the IRQ latency impact. A separate poll handler is used for TX and RX since they have separate IRQs on this controller, so that the completion work for each of them stays on the same CPU as the interrupt.

Testing on a Xilinx MPSoC ZU9EG platform using iperf3 from a Linux PC through a switch at 1G link speed showed no significant change in TX or RX throughput, with approximately 941 Mbps before and after. Hard IRQ time in the TX throughput test was significantly reduced from 12% to below 1% on the CPU handling TX interrupts, with total hard+soft IRQ CPU usage dropping from about 56% down to 48%.

Quoted 5 lines

---

Changed since v4: Added locking to protect TX ring tail pointer against concurrent access by TX transmit and TX poll paths.

Hi, sorry for a late reply there's just too many patches to look at lately.

The lock is slightly concerning, the driver follows the usual wake up scheme based on memory barriers. If we add the lock we should probably take the barriers out.

We can also try to avoid the lock and drill into what the issue is. At a quick look it seems like there is a barrier missing between setup of the descriptors and kicking the transfer off:

drivers/net/ethernet/xilinx/xilinx_axienet_main.c +3 -2
index d6fc3f7acdf0..9e244b73a0ca 100644
@@ -878,10 +878,11 @@ axienet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
 	cur_p->skb = skb;
 
 	tail_p = lp->tx_bd_p + sizeof(*lp->tx_bd_v) * lp->tx_bd_tail;
-	/* Start the transfer */
-	axienet_dma_out_addr(lp, XAXIDMA_TX_TDESC_OFFSET, tail_p);
 	if (++lp->tx_bd_tail >= lp->tx_bd_num)
 		lp->tx_bd_tail = 0;
+	wmb(); // possibly dma_wmb()
+	/* Start the transfer */
+	axienet_dma_out_addr(lp, XAXIDMA_TX_TDESC_OFFSET, tail_p);
 
 	/* Stop queue if next transmit may not have space */
 	if (axienet_check_tx_bd_space(lp, MAX_SKB_FRAGS + 1)) {
#2 lore raw
Quoted 26 lines · Jakub Kicinski
Quoted 19 lines · On Mon, 9 May 2022 11:30:39 -0600 Robert Hancock wrote:

This driver was using the TX IRQ handler to perform all TX completion tasks. Under heavy TX network load, this can cause significant irqs-off latencies (found to be in the hundreds of microseconds using ftrace). This can cause other issues, such as overrunning serial UART FIFOs when using high baud rates with limited UART FIFO sizes.

Switch to using a NAPI poll handler to perform the TX completion work to get this out of hard IRQ context and avoid the IRQ latency impact. A separate poll handler is used for TX and RX since they have separate IRQs on this controller, so that the completion work for each of them stays on the same CPU as the interrupt.

Testing on a Xilinx MPSoC ZU9EG platform using iperf3 from a Linux PC through a switch at 1G link speed showed no significant change in TX or RX throughput, with approximately 941 Mbps before and after. Hard IRQ time in the TX throughput test was significantly reduced from 12% to below 1% on the CPU handling TX interrupts, with total hard+soft IRQ CPU usage dropping from about 56% down to 48%.

Quoted 5 lines

---

Changed since v4: Added locking to protect TX ring tail pointer against concurrent access by TX transmit and TX poll paths.

Quoted 6 lines

Hi, sorry for a late reply there's just too many patches to look at lately.

The lock is slightly concerning, the driver follows the usual wake up scheme based on memory barriers. If we add the lock we should probably take the barriers out.

So there's basically two places where there is contention, axienet_start_xmit where it is moving the tail pointer down after adding more entries to the TX ring, and the TX poll function calling axienet_check_tx_bd_space where it is using the tail pointer to see if there is enough space in the TX ring to wake the queue. I suppose barriers are likely sufficient if the code updating the ring pointer is more careful about how it is done - for example in the snippet quoted below, it's moving the pointer down and then moving it back to 0 if it is past the end of the ring; this would need to change to only update the pointer once and not have the intermediate state where it is at an invalid position.

I think the stability issue I saw earlier was not actually due to these changes however, but to similar changes in v1 of the "net: macb: use NAPI for TX completion path" patch. In the case of that driver, it was previously relying on the TX completion path being protected by a spinlock in the IRQ handler, which was lost when the TX completion was moved to a poll function.

Quoted 11 lines

We can also try to avoid the lock and drill into what the issue is. At a quick look it seems like there is a barrier missing between setup of the descriptors and kicking the transfer off:

drivers/net/ethernet/xilinx/xilinx_axienet_main.c +0 -0
index d6fc3f7acdf0..9e244b73a0ca 100644

drivers/net/ethernet/xilinx/xilinx_axienet_main.c axienet_start_xmit(struct sk_buff *skb, struct · line 884

Quoted 9 lines
drivers/net/ethernet/xilinx/xilinx_axienet_main.c +1 -2
@@ -878,10 +878,11 @@ axienet_start_xmit(struct sk_buff *skb, struct
 net_device *ndev)
 	cur_p->skb = skb;
 
 	tail_p = lp->tx_bd_p + sizeof(*lp->tx_bd_v) * lp->tx_bd_tail;
-	/* Start the transfer */
-	axienet_dma_out_addr(lp, XAXIDMA_TX_TDESC_OFFSET, tail_p);
 	if (++lp->tx_bd_tail >= lp->tx_bd_num)
 		lp->tx_bd_tail = 0;
+	wmb(); // possibly dma_wmb()

I think the MMIO write in axienet_dma_out_addr is supposed to be an implicit barrier, so that shouldn't be needed?

Quoted 5 lines
drivers/net/ethernet/xilinx/xilinx_axienet_main.c +2 -0
+	/* Start the transfer */
+	axienet_dma_out_addr(lp, XAXIDMA_TX_TDESC_OFFSET, tail_p);
 
 	/* Stop queue if next transmit may not have space */

if (axienet_check_tx_bd_space(lp, MAX_SKB_FRAGS + 1)) {

Signature · 3 lines
Robert Hancock
Senior Hardware Designer, Calian Advanced Technologies
www.calian.com