Skip to contentSkip to conversation

net: stmmac: fix RX descriptors and XSK TX on resume

PATCH New Ding Hui 2 participants 8 messages Nov 19 – Nov 23

Subsystems (4)
lore
Share & tools
lore

Ding Hui

author
#0 lore raw

v8 cover net: stmmac: fix RX descriptors and XSK TX on resume

From: Ding Hui <[email protected]>

This series restores RX descriptor addresses overwritten by hardware writeback between disabling NAPI and stopping RX DMA during suspend. It also fixes an AF_XDP TX-only resume stall found while testing the series.

Patch 1 honors RX allocation flags. Patch 2 keeps unfilled XSK slots CPU-owned and tracks pending refills explicitly, including an empty ring and short refills. Patch 3 restores RX addresses from retained buffers, rebuilds chain pointers and initializes descriptor control fields. It publishes a contiguous XSK prefix and recomputes the tail and pending region. Patch 4 restores the bound XSK TX pool after TX buffer cleanup; otherwise zero-copy transmission can stop after resume.

Commit 9c63faaa931e ("net: stmmac: re-init rx buffers when mac resume back") was reverted by commit 00423969d806 ("Revert "net: stmmac:
re-init rx buffers when mac resume back"") after a Jetson TX2 suspend/resume regression. This series retains existing buffers and DMA mappings instead of recycling and reallocating every buffer. Jetson TX2 has not been tested.

Hardware validation used backports on our XGMAC platform with a vendor kernel based on Linux 6.12.41, not these upstream commits as-is:
- Receiving iperf traffic from a PC while suspending and resuming caused a fatal bus error after resume in almost every run without the RX fix. The same test no longer showed that failure with the fix.
- Native XDP/AF_XDP zero-copy RX continued after two suspend/resume cycles with the receive socket kept open.
- Zero-copy TX-only, with no RX fill buffers, passed one suspend/resume cycle with no fatal bus error.
- Pausing the receive application with SIGSTOP and resuming it with SIGCONT recovered RX without recreating the socket.
- xdpsock is used for native XDP testing.

Additional testing would be welcome for RX sockets initially supplied with 0/1/8/15 fill buffers, replenishment in batches below 16, and those states across suspend/resume. These differ from TX-only with no RX fill buffers. Legacy descriptor cores, GMAC4, chain mode, timestamp-specific descriptor paths, multiple XSK queues and long suspend/resume runs remain untested.

Missing page_pool buffers are still allocated during resume. Allocation failure returns -ENOMEM without restarting DMA and leaves the device detached. This adds a trigger for existing resume error-recovery problems; full close/open recovery, including NAPI and platform power handling, is separate work. The Medium-severity AI review on failure recovery is not fully resolved by this revision.

Changes in v8:
- Define RDES0_OWN, RDES3_OWN and XGMAC_RDES3_OWN as ((u32)BIT(31)), following the existing TX OWN mask convention, to avoid the four sparse constant-truncation warnings in the RX ownership-clearing callbacks.
- Fold the type correction into patch 2; the other patches and the net/main base are unchanged from v7.

Link to v7:
https://lore.kernel.org/netdev/[email protected]/

Changes in v7:
- Rebase onto net/main at aa40afa831eb.
- Adapt the XSK pending-refill accounting to the upstream split-frame and error paths, preserving extended descriptor status handling and the existing per-frame drop accounting.
- Use the upstream stmmac_reset_rx_queue() saved-state cleanup instead of freeing the saved skb separately in the resume helper, avoiding a double free with the new baseline. Update the commit description accordingly.
- Recheck ARM64 STMMAC builds with XDP_SOCKETS enabled and disabled. Hardware validation remains from the Linux 6.12.41 vendor backports; the rebased series has not been retested on hardware.

Link to v6:
https://lore.kernel.org/netdev/[email protected]/

Changes in v6:
- Add a separate fix to restore the bound XSK TX pool after resume cleanup, addressing TX-only stalls found during XGMAC validation.
- Factor resume descriptor work into one TX queue loop and one RX loop, following Thomas's review, and use common error cleanup.
- Restore addresses and chain pointers before initializing RX control fields, preserving legacy fields and RIWT/IOC policy without a separate OWN loop.
- Add format-specific RX ownership clearing for unfilled XSK descriptors.
- Track pending XSK refills independently of circular index equality; retry short refills and prevent consumption of unfilled descriptors.
- Restore a populated XSK prefix and retain later attached buffers for refill; recompute the allocation count, initial tail and pending region.
- Select XSK buffer size independently of the initial allocation count.
- Discard any partially assembled RX frame before resetting the RX ring after resume.

Link to v5:
https://lore.kernel.org/netdev/[email protected]/

Changes in v5:
- Target net and add Fixes tags to both patches.
- Split the allocation-flags fix into a separate patch, with a Fixes tag identifying the page_pool conversion that stopped honoring the flags.
- Share page_pool and XSK buffer preparation with the RX refill paths.
- Allocate missing secondary pages even when the primary page exists, using the current baseline's buffer2 capability check.
- Condense the reinitialization kernel-doc to two paragraphs.
- Restore descriptor addresses before the final dma_wmb()/OWN pass.
- Clarify that unavailable XSK slots remain CPU-owned and may stop DMA with RBU, rather than claiming that hardware skips them.
- Power down legacy SerDes if RX descriptor restoration fails.

Link to v4:
https://lore.kernel.org/netdev/[email protected]/

Changes in v4:
- Document the return value of stmmac_reinit_rx_descriptors().

Link to v3:
https://lore.kernel.org/netdev/[email protected]/

Changes in v3:
- Allocate missing page_pool buffers with GFP_KERNEL and abort resume on allocation failure.
- Try to allocate missing XSK buffers and clear descriptors when the fill ring cannot supply a buffer.
- Restore OWN after programming addresses and rebuilding chain pointers.
- Honor the flags argument of stmmac_init_rx_buffers().
- Restore RX descriptors after stmmac_clear_descriptors().

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

Changes in v2:
- Introduce a helper to restore addresses from existing RX buffers.
- Update the commit message.

Link to v1:
https://lore.kernel.org/netdev/[email protected]/

Ding Hui (4):
net: stmmac: honor allocation flags when initializing RX buffers
net: stmmac: track unfilled XSK RX descriptors explicitly
net: stmmac: restore RX descriptor buffer addresses on resume
net: stmmac: restore the XSK TX pool on resume

 drivers/net/ethernet/stmicro/stmmac/descs.h   |   4 +-
 .../ethernet/stmicro/stmmac/dwmac4_descs.c    |   6 +
 .../ethernet/stmicro/stmmac/dwmac4_descs.h    |   2 +-
 .../net/ethernet/stmicro/stmmac/dwxgmac2.h    |   2 +-
 .../ethernet/stmicro/stmmac/dwxgmac2_descs.c  |   6 +
 .../net/ethernet/stmicro/stmmac/enh_desc.c    |   6 +
 drivers/net/ethernet/stmicro/stmmac/hwif.h    |   3 +
 .../net/ethernet/stmicro/stmmac/norm_desc.c   |   6 +
 drivers/net/ethernet/stmicro/stmmac/stmmac.h  |   2 +
 .../net/ethernet/stmicro/stmmac/stmmac_main.c | 273 +++++++++++++-----
 10 files changed, 228 insertions(+), 82 deletions(-)

base-commit: aa40afa831ebfe45e40a23721711059f2ac584d4

Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v8 1/4 net: stmmac: honor allocation flags when initializing RX buffers

From: Ding Hui <[email protected]>

stmmac_init_rx_buffers() accepts allocation flags but stopped using them when RX buffers were converted to page_pool. As a result, callers passing GFP_KERNEL still allocate with GFP_ATOMIC | __GFP_NOWARN and cannot use reclaim to satisfy an allocation.

Use the supplied flags while retaining GFP_DMA32 for hardware without 64-bit DMA addressing. This also allows the resume path to allocate missing RX pages with GFP_KERNEL.

Fixes 2af6106ae949 ("net: stmmac: Introducing support for Page Pool")
---
 drivers/net/ethernet/stmicro/stmmac/stmmac_main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +1 -1
index 25894771e9e9..8bd244046195 100644
@@ -1689,7 +1689,7 @@ static int stmmac_init_rx_buffers(struct stmmac_priv *priv,
 {
 	struct stmmac_rx_queue *rx_q = &dma_conf->rx_queue[queue];
 	struct stmmac_rx_buffer *buf = &rx_q->buf_pool[i];
-	gfp_t gfp = (GFP_ATOMIC | __GFP_NOWARN);
+	gfp_t gfp = flags;
 
 	if (priv->dma_cap.host_dma_width <= 32)
 		gfp |= GFP_DMA32;
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH v8 2/4 net: stmmac: track unfilled XSK RX descriptors explicitly

In reply to Ding Hui

From: Ding Hui <[email protected]>

An XSK fill ring may supply fewer buffers than the RX ring needs. Those unfilled descriptors must remain CPU-owned, including on legacy cores whose clear callback only clears the buffer address. Add an ownership operation that preserves descriptor size and ring/chain control fields.

Track the number of descriptors awaiting refill independently of the ring indices, so an entirely empty ring can be distinguished from one with no pending refills. Initialize the pending region after the allocated prefix and stop consumption before entering it. Retry pending refills on each poll, including userspace wakeups with fewer than a batch of buffers, and retain batching during packet processing.

Update the tail after successful refills and kick legacy RX DMA. Use the end-of-ring tail address for a refill ending at index zero, including a completely empty ring populated before DMA has advanced from the base. Select the receive buffer size from the XSK pool even if no buffers were available at initialization.

Define the RX OWN masks as u32 before complementing them, following the existing TX OWN mask convention. This avoids sparse truncation warnings in the ownership-clearing callbacks on 64-bit builds.

Fixes bba2556efad6 ("net: stmmac: Enable RX via AF_XDP zero-copy")
---
 drivers/net/ethernet/stmicro/stmmac/descs.h   |  4 +-
 .../ethernet/stmicro/stmmac/dwmac4_descs.c    |  6 +++
 .../ethernet/stmicro/stmmac/dwmac4_descs.h    |  2 +-
 .../net/ethernet/stmicro/stmmac/dwxgmac2.h    |  2 +-
 .../ethernet/stmicro/stmmac/dwxgmac2_descs.c  |  6 +++
 .../net/ethernet/stmicro/stmmac/enh_desc.c    |  6 +++
 drivers/net/ethernet/stmicro/stmmac/hwif.h    |  3 ++
 .../net/ethernet/stmicro/stmmac/norm_desc.c   |  6 +++
 drivers/net/ethernet/stmicro/stmmac/stmmac.h  |  2 +
 .../net/ethernet/stmicro/stmmac/stmmac_main.c | 49 +++++++++++++------
 10 files changed, 67 insertions(+), 19 deletions(-)
drivers/net/ethernet/stmicro/stmmac/descs.h +2 -2
index 0af819a98fda..3053b915f804 100644
@@ -33,8 +33,8 @@
 #define	RDES0_ERROR_SUMMARY	BIT(15)
 #define	RDES0_FRAME_LEN_MASK	GENMASK(29, 16)
 #define	RDES0_DA_FILTER_FAIL	BIT(30)
-#define	RDES0_OWN		BIT(31)
-			/* RDES1 */
+#define	RDES0_OWN		((u32)BIT(31))	/* silence sparse */
+/* RDES1 */
 #define	RDES1_BUFFER1_SIZE_MASK		GENMASK(10, 0)
 #define	RDES1_BUFFER2_SIZE_MASK		GENMASK(21, 11)
 #define	RDES1_SECOND_ADDRESS_CHAINED	BIT(24)
drivers/net/ethernet/stmicro/stmmac/dwmac4_descs.c +6 -0
index c6a8f8d73501..307bbd63dbcb 100644
@@ -176,6 +176,11 @@ static void dwmac4_set_tx_owner(struct dma_desc *p)
 	p->des3 |= cpu_to_le32(TDES3_OWN);
 }
 
+static void dwmac4_clear_rx_owner(struct dma_desc *p)
+{
+	p->des3 &= cpu_to_le32(~RDES3_OWN);
+}
+
 static void dwmac4_set_rx_owner(struct dma_desc *p, int disable_rx_ic)
 {
 	u32 flags = (RDES3_OWN | RDES3_BUFFER1_VALID_ADDR);
@@ -539,6 +544,7 @@ const struct stmmac_desc_ops dwmac4_desc_ops = {
 	.rx_status = dwmac4_wrback_get_rx_status,
 	.set_tx_owner = dwmac4_set_tx_owner,
 	.set_rx_owner = dwmac4_set_rx_owner,
+	.clear_rx_owner = dwmac4_clear_rx_owner,
 	.get_rx_vlan_tci = dwmac4_wrback_get_rx_vlan_tci,
 	.get_rx_vlan_valid = dwmac4_wrback_get_rx_vlan_valid,
 	.get_rx_frame_len = dwmac4_wrback_get_rx_frame_len,
drivers/net/ethernet/stmicro/stmmac/dwmac4_descs.h +1 -1
index fb1fea5b0e6e..c69f95268cd0 100644
@@ -134,7 +134,7 @@
 #define RDES3_INT_ON_COMPLETION_EN	BIT(30)
 
 /* TDS3 use for both format (read and write back) */
-#define RDES3_OWN			BIT(31)
+#define RDES3_OWN			((u32)BIT(31))	/* silence sparse */
 
 extern const struct stmmac_mode_ops dwmac4_ring_mode_ops;
 extern const struct stmmac_desc_ops dwmac4_desc_ops;
drivers/net/ethernet/stmicro/stmmac/dwxgmac2.h +1 -1
index f8ab347f7b5b..50f9d1dfc92d 100644
@@ -434,7 +434,7 @@
 #define XGMAC_RDES2_L4FM		BIT(28)
 #define XGMAC_RDES2_L3FM		BIT(27)
 #define XGMAC_RDES2_HL			GENMASK(9, 0)
-#define XGMAC_RDES3_OWN			BIT(31)
+#define XGMAC_RDES3_OWN			((u32)BIT(31))	/* silence sparse */
 #define XGMAC_RDES3_CTXT		BIT(30)
 #define XGMAC_RDES3_IOC			BIT(30)
 #define XGMAC_RDES3_LD			BIT(28)
drivers/net/ethernet/stmicro/stmmac/dwxgmac2_descs.c +6 -0
index 6719ac6e395b..ad377c76ed4e 100644
@@ -51,6 +51,11 @@ static void dwxgmac2_set_tx_owner(struct dma_desc *p)
 	p->des3 |= cpu_to_le32(XGMAC_TDES3_OWN);
 }
 
+static void dwxgmac2_clear_rx_owner(struct dma_desc *p)
+{
+	p->des3 &= cpu_to_le32(~XGMAC_RDES3_OWN);
+}
+
 static void dwxgmac2_set_rx_owner(struct dma_desc *p, int disable_rx_ic)
 {
 	u32 flags = XGMAC_RDES3_OWN;
@@ -350,6 +355,7 @@ const struct stmmac_desc_ops dwxgmac210_desc_ops = {
 	.rx_status = dwxgmac2_get_rx_status,
 	.set_tx_owner = dwxgmac2_set_tx_owner,
 	.set_rx_owner = dwxgmac2_set_rx_owner,
+	.clear_rx_owner = dwxgmac2_clear_rx_owner,
 	.get_rx_vlan_tci = dwxgmac2_wrback_get_rx_vlan_tci,
 	.get_rx_vlan_valid = dwxgmac2_wrback_get_rx_vlan_valid,
 	.get_rx_frame_len = dwxgmac2_get_rx_frame_len,
drivers/net/ethernet/stmicro/stmmac/enh_desc.c +6 -0
index 7ece8d4157a8..57909a9ba70d 100644
@@ -299,6 +299,11 @@ static void enh_desc_set_tx_owner(struct dma_desc *p)
 	p->des0 |= cpu_to_le32(ETDES0_OWN);
 }
 
+static void enh_desc_clear_rx_owner(struct dma_desc *p)
+{
+	p->des0 &= cpu_to_le32(~RDES0_OWN);
+}
+
 static void enh_desc_set_rx_owner(struct dma_desc *p, int disable_rx_ic)
 {
 	p->des0 |= cpu_to_le32(RDES0_OWN);
@@ -460,6 +465,7 @@ const struct stmmac_desc_ops enh_desc_ops = {
 	.set_tx_ic = enh_desc_set_tx_ic,
 	.set_tx_owner = enh_desc_set_tx_owner,
 	.set_rx_owner = enh_desc_set_rx_owner,
+	.clear_rx_owner = enh_desc_clear_rx_owner,
 	.get_rx_frame_len = enh_desc_get_rx_frame_len,
 	.rx_extended_status = enh_desc_get_ext_status,
 	.enable_tx_timestamp = enh_desc_enable_tx_timestamp,
drivers/net/ethernet/stmicro/stmmac/hwif.h +3 -0
index 8ae95e91a36a..05edec258a8b 100644
@@ -65,6 +65,7 @@ struct stmmac_desc_ops {
 			 struct dma_desc *p, void __iomem *ioaddr);
 	/* Handle extra events on specific interrupts hw dependent */
 	void (*set_rx_owner)(struct dma_desc *p, int disable_rx_ic);
+	void (*clear_rx_owner)(struct dma_desc *p);
 	/* Get the receive frame size */
 	int (*get_rx_frame_len)(struct dma_desc *p, int rx_coe_type);
 	/* Return the reception status looking at the RDES1 */
@@ -121,6 +122,8 @@ struct stmmac_desc_ops {
 	stmmac_do_callback(__priv, desc, get_rx_vlan_valid, __args)
 #define stmmac_tx_status(__priv, __args...) \
 	stmmac_do_callback(__priv, desc, tx_status, __args)
+#define stmmac_clear_rx_owner(__priv, __args...) \
+	stmmac_do_void_callback(__priv, desc, clear_rx_owner, __args)
 #define stmmac_set_rx_owner(__priv, __args...) \
 	stmmac_do_void_callback(__priv, desc, set_rx_owner, __args)
 #define stmmac_get_rx_frame_len(__priv, __args...) \
drivers/net/ethernet/stmicro/stmmac/norm_desc.c +6 -0
index d19bf7ca4db3..473d53c31072 100644
@@ -141,6 +141,11 @@ static void ndesc_set_tx_owner(struct dma_desc *p)
 	p->des0 |= cpu_to_le32(TDES0_OWN);
 }
 
+static void ndesc_clear_rx_owner(struct dma_desc *p)
+{
+	p->des0 &= cpu_to_le32(~RDES0_OWN);
+}
+
 static void ndesc_set_rx_owner(struct dma_desc *p, int disable_rx_ic)
 {
 	p->des0 |= cpu_to_le32(RDES0_OWN);
@@ -284,6 +289,7 @@ const struct stmmac_desc_ops ndesc_ops = {
 	.set_tx_ic = ndesc_set_tx_ic,
 	.set_tx_owner = ndesc_set_tx_owner,
 	.set_rx_owner = ndesc_set_rx_owner,
+	.clear_rx_owner = ndesc_clear_rx_owner,
 	.get_rx_frame_len = ndesc_get_rx_frame_len,
 	.enable_tx_timestamp = ndesc_enable_tx_timestamp,
 	.get_tx_timestamp_status = ndesc_get_tx_timestamp_status,
drivers/net/ethernet/stmicro/stmmac/stmmac.h +2 -0
index b76acfe0d958..b81695453cc6 100644
@@ -128,6 +128,8 @@ struct stmmac_rx_queue {
 	unsigned int cur_rx;
 	unsigned int dirty_rx;
 	unsigned int buf_alloc_num;
+	/* XSK descriptors waiting for refill, including a completely empty ring. */
+	unsigned int xsk_rx_pending;
 	unsigned int napi_skb_frag_size;
 	dma_addr_t dma_rx_phy;
 	unsigned int state_saved;
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +34 -15

139 lines not on this page · raw message

Signature · 1 line
2.34.1
#3 lore raw

Re: [PATCH net v8 2/4] net: stmmac: track unfilled XSK RX descriptors explicitly

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.

Critical: 0 · High: 2 · Medium: 0 · Low: 1

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [High] Permanent RX stall on dwmac4/dwxgmac2 XSK queues after a full- ring refill that ends at a non-zero index.
- [High] net: stmmac: pre-loop refill failure in stmmac_rx_zc() skips later refills
- [Low] The comment added in stmmac_rx_refill_zc() describes a narrower condition than the code under it.

--

net: stmmac: track unfilled XSK RX descriptors explicitly

This commit adds an xsk_rx_pending counter that tracks XSK RX descriptors waiting for refill. It also adds a clear_rx_owner descriptor op so unfilled descriptors stay CPU-owned. stmmac_rx_zc() and stmmac_rx_refill_zc() now retry refills on every poll and write an end-of-ring tail address when a refill ends at index zero.

Quoted 4 lines
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +0 -0
index 8bd2440461959..f9ac82d2e7266 100644

[ ... ]

Quoted 4 lines
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +0 -0
@@ -5626,12 +5632,18 @@ static bool stmmac_rx_refill_zc(struct stmmac_priv *priv, u32 queue, u32 budget)
 		dma_wmb();
 		stmmac_set_rx_owner(priv, rx_desc, use_rx_wd);
 

drivers/net/ethernet/stmicro/stmmac/stmmac_main.c static bool stmmac_rx_refill_zc(struct stmmac_priv *priv, u32 queue, u32 budget) · line 5643

Quoted 10 lines
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +4 -1
+		rx_q->xsk_rx_pending--;
 		entry = STMMAC_NEXT_ENTRY(entry, priv->dma_conf.dma_rx_size);
 	}
 
 	if (rx_desc) {
 		rx_q->dirty_rx = entry;
-		stmmac_set_queue_rx_tail_ptr(priv, rx_q, queue, rx_q->dirty_rx);
+		/* Use the end-of-ring address when filling an initially empty
+		 * ring completely, so the tail differs from its initial base.
+		 */

[Severity: Low]

This isn't a bug, but the comment covers less than the code below it does. The entry ?: dma_rx_size expression applies to every refill whose last entry wraps to 0. That includes ordinary wrap-around on every lap of the ring, not only a complete fill of an initially empty ring.

The commit message calls this "a refill ending at index zero", which matches the code. Could the comment say the same?

It might also help to say why this differs from stmmac_rx_refill(), which writes the base address for index 0:

	stmmac_set_queue_rx_tail_ptr(priv, rx_q, queue, rx_q->dirty_rx);

The later patches in the series leave this comment unchanged.

drivers/net/ethernet/stmicro/stmmac/stmmac_main.c static bool stmmac_rx_refill_zc(struct stmmac_priv *priv, u32 queue, u32 budget) · line 5647

Quoted 4 lines
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +3 -0
+		stmmac_set_queue_rx_tail_ptr(priv, rx_q, queue,
+					     entry ?: priv->dma_conf.dma_rx_size);
+		stmmac_enable_dma_reception(priv, priv->ioaddr, queue);
 	}

[Severity: High]

Can RX DMA stay stalled after a full-ring refill that ends at a non-zero index?

This patch removes the one-slot gap that stmmac_rx_zc() used to keep:

- if (unlikely(next_entry == rx_q->dirty_rx))
- break;

The consume loop now stops only on dma_own or on this check:

		if (rx_q->xsk_rx_pending == priv->dma_conf.dma_rx_size)
			break;

Suppose the XSK fill ring runs dry under traffic. The CPU can then consume every descriptor, and the ring becomes completely empty with cur_rx == dirty_rx == k for any k. The last successful refill wrote the tail as base + k * desc, and DMA has stopped with its current pointer at k.

Now suppose userspace supplies at least dma_rx_size buffers before the next poll. The refill at the top of stmmac_rx_zc() runs:

	if (rx_q->xsk_rx_pending)
		failure = !stmmac_rx_refill_zc(priv, queue, rx_q->xsk_rx_pending);

It fills all N slots and entry wraps back to k. The tail is then written as entry ?: dma_rx_size, which is base + k * desc again. That equals both the stalled DMA current pointer and the previous tail value. The ?: special case only covers k == 0.

The comment above depends on a tail equal to the DMA position not restarting DMA ("so the tail differs from its initial base"). Doesn't the same reasoning apply when k != 0?

After this refill xsk_rx_pending is 0, so no later poll writes the tail again. Every later poll finds descriptor cur_rx DMA-owned and breaks out.

stmmac_enable_dma_reception() seems to be implemented only for dwmac1000. On dwmac4 the RBU interrupt only increments rx_buf_unav_irq, and on xgmac it only schedules NAPI. On dwmac4 and dwxgmac2, would the queue stay dead until it is reinitialized?

The later patches in the series don't seem to change this logic.

[ ... ]

drivers/net/ethernet/stmicro/stmmac/stmmac_main.c static int stmmac_rx_zc(struct stmmac_priv *priv, int limit, u32 queue) · line 5680

Quoted 10 lines
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +3 -0
@@ -5661,6 +5672,9 @@ static int stmmac_rx_zc(struct stmmac_priv *priv, int limit, u32 queue)
 	bool failure = false;
 	int xdp_status = 0;
 
+	if (rx_q->xsk_rx_pending)
+		failure = !stmmac_rx_refill_zc(priv, queue, rx_q->xsk_rx_pending);
+
 	if (netif_msg_rx_status(priv)) {
 		void *rx_head = stmmac_get_rx_desc(priv, rx_q, 0);
 

[Severity: High]
Can a refill failure at the top of stmmac_rx_zc() stop buffers that are freed later in the same poll from being reposted?

The pre-loop call asks stmmac_rx_refill_zc() for all of xsk_rx_pending. Whenever the pool holds fewer free buffers than that, xsk_buff_alloc() returns NULL partway through and failure is latched to true before the receive loop starts. A partially filled fill ring is exactly the case this patch is meant to support.

Both the batch refill inside the loop and the new refill after stmmac_finalize_xdp_rx() are gated on !failure. Neither one runs for the rest of the poll.

Meanwhile several paths call xsk_buff_free() on the buffer:

- XDP_PASS
- STMMAC_XDP_CONSUMED (XDP_DROP, XDP_ABORTED, failed redirect)
- the discard_frame and rx_not_ls error paths
- XDP_TX, via xdp_convert_buff_to_frame()

Each of these puts the buffer back on the pool free_list, where xsk_buff_alloc() could hand it straight back out. The old code started every poll with failure set to false. It would have reposted those buffers once dirty reached STMMAC_RX_FILL_BATCH.

With XDP_USE_NEED_WAKEUP, suppose those paths consume every descriptor that was posted. The poll then ends with xsk_rx_pending equal to dma_rx_size and no DMA-owned descriptor left in the ring. stmmac_rx_zc() returns count < limit and NAPI completes, so no further RX interrupt can arrive. None of these frames reach the XSK RX ring. Doesn't recovery then depend entirely on userspace issuing a wakeup without having received anything?

Without need_wakeup the problem goes the other way. Suppose the pool has fewer than dma_rx_size buffers in total, for example a TX-only socket that never populates the fill ring. Then xsk_rx_pending never reaches zero and the pre-loop refill fails on every poll, so stmmac_rx_zc() returns limit every time. Wouldn't NAPI then never complete, even on an idle link? Before this patch the same setup left failure false and returned count.

Should the later refills still be attempted after an earlier failure, for example by OR-ing the result into failure instead of skipping the call? And is busy polling intended when the fill ring is simply smaller than the RX ring?

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1795096430.git.dinghui%40lixiang.com
author
#4 lore raw

PATCH v8 3/4 net: stmmac: restore RX descriptor buffer addresses on resume

In reply to Ding Hui

From: Ding Hui <[email protected]>

Suspend disables NAPI before stopping RX DMA. Hardware can complete RX descriptors in between, overwriting GMAC4/XGMAC buffer addresses with writeback status. Resume currently rearms those descriptors without restoring their addresses, leading to a fatal bus error.

Share buffer preparation with the refill paths and restore addresses from attached buffers. Allocate missing primary and secondary pages independently with GFP_KERNEL. If allocation fails, return the error without restarting DMA and leave the device detached.

For XSK queues, restore a contiguous populated prefix and leave the rest CPU-owned in the pending refill region. Preserve attached buffers beyond the prefix for later refill. Recompute the initial tail and pending count rather than retaining the allocation count from open.

Rebuild chain pointers and use stmmac_reset_rx_queue() to discard partial receive state from before the DMA reset. Initialize RX descriptors after restoring their addresses,
using the existing per-queue initializer to preserve the watchdog policy and legacy control fields. Keep the unpopulated XSK suffix CPU-owned when DMA restarts. This replaces the separate final ownership-setting loop.

Collect the resume work in stmmac_resume_descriptors(), with one loop over TX queues and one over RX queues. Free outstanding TX buffers before initializing each TX ring, and remove the now-unused all-queue TX wrapper. The RX rings no longer undergo initialization before address restoration.

Unlike the reverted commit 9c63faaa931e ("net: stmmac: re-init rx buffers when mac resume back"), retain attached buffers and their DMA mappings.

Fixes f748be531d70 ("stmmac: support new GMAC4")
---
 .../net/ethernet/stmicro/stmmac/stmmac_main.c | 221 +++++++++++++-----
 1 file changed, 158 insertions(+), 63 deletions(-)
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +158 -63
index f9ac82d2e726..1508cf08f03b 100644
@@ -1677,6 +1677,40 @@ static bool stmmac_rx_check_buf2_cap(struct stmmac_priv *priv)
 	return priv->hw->desc && priv->hw->desc->set_sec_addr;
 }
 
+static int stmmac_prepare_rx_buffer(struct stmmac_priv *priv,
+				    struct stmmac_rx_queue *rx_q,
+				    struct stmmac_rx_buffer *buf,
+				    struct dma_desc *p, gfp_t flags)
+{
+	gfp_t gfp = flags;
+
+	if (priv->dma_cap.host_dma_width <= 32)
+		gfp |= GFP_DMA32;
+
+	if (!buf->page) {
+		buf->page = page_pool_alloc_pages(rx_q->page_pool, gfp);
+		if (!buf->page)
+			return -ENOMEM;
+		buf->page_offset = stmmac_rx_offset(priv);
+	}
+
+	if (stmmac_rx_check_buf2_cap(priv) && !buf->sec_page) {
+		buf->sec_page = page_pool_alloc_pages(rx_q->page_pool, gfp);
+		if (!buf->sec_page)
+			return -ENOMEM;
+
+		buf->sec_addr = page_pool_get_dma_addr(buf->sec_page);
+	}
+
+	buf->addr = page_pool_get_dma_addr(buf->page) + buf->page_offset;
+
+	stmmac_set_desc_addr(priv, p, buf->addr);
+	stmmac_set_desc_sec_addr(priv, p, buf->sec_addr,
+				 stmmac_rx_check_buf2_cap(priv));
+
+	return 0;
+}
+
 /**
  * stmmac_init_rx_buffers - init the RX descriptor buffer.
  * @priv: driver private structure
@@ -1695,32 +1729,100 @@ static int stmmac_init_rx_buffers(struct stmmac_priv *priv,
 {
 	struct stmmac_rx_queue *rx_q = &dma_conf->rx_queue[queue];
 	struct stmmac_rx_buffer *buf = &rx_q->buf_pool[i];
-	gfp_t gfp = flags;
+	int ret;
 
-	if (priv->dma_cap.host_dma_width <= 32)
-		gfp |= GFP_DMA32;
+	ret = stmmac_prepare_rx_buffer(priv, rx_q, buf, p, flags);
+	if (ret)
+		return ret;
 
-	if (!buf->page) {
-		buf->page = page_pool_alloc_pages(rx_q->page_pool, gfp);
-		if (!buf->page)
+	if (dma_conf->dma_buf_sz == BUF_SIZE_16KiB)
+		stmmac_init_desc3(priv, p);
+
+	return 0;
+}
+
+static int stmmac_prepare_rx_buffer_zc(struct stmmac_priv *priv,
+				       struct stmmac_rx_queue *rx_q,
+				       struct stmmac_rx_buffer *buf,
+				       struct dma_desc *p)
+{
+	if (!buf->xdp) {
+		buf->xdp = xsk_buff_alloc(rx_q->xsk_pool);
+		if (!buf->xdp)
 			return -ENOMEM;
-		buf->page_offset = stmmac_rx_offset(priv);
 	}
 
-	if (stmmac_rx_check_buf2_cap(priv) && !buf->sec_page) {
-		buf->sec_page = page_pool_alloc_pages(rx_q->page_pool, gfp);
-		if (!buf->sec_page)
-			return -ENOMEM;
+	stmmac_set_desc_addr(priv, p, xsk_buff_xdp_get_dma(buf->xdp));
+	stmmac_set_desc_sec_addr(priv, p, 0, false);
 
-		buf->sec_addr = page_pool_get_dma_addr(buf->sec_page);
-		stmmac_set_desc_sec_addr(priv, p, buf->sec_addr, true);
+	return 0;
+}
+
+/**
+ * stmmac_reinit_rx_descriptors - restore RX descriptors before restarting DMA
+ * @priv: driver private structure
+ * @dma_conf: DMA configuration
+ * @queue: RX queue index
+ *
+ * Restore addresses overwritten by hardware writeback, retaining attached
+ * buffers and allocating missing page_pool buffers with GFP_KERNEL. Keep
+ * descriptors CPU-owned until addresses and chain pointers are restored.
+ *
+ * For XSK, publish only the contiguous populated prefix. The remainder is
+ * left in the refill region, including any buffers already attached there,
+ * and can be populated by NAPI when userspace supplies more fill buffers.
+ *
+ * Return: 0 on success, or -ENOMEM if a required page cannot be allocated.
+ */
+static int stmmac_reinit_rx_descriptors(struct stmmac_priv *priv,
+					struct stmmac_dma_conf *dma_conf,
+					u32 queue)
+{
+	struct stmmac_rx_queue *rx_q = &dma_conf->rx_queue[queue];
+	struct dma_desc *p;
+	int i, ret;
+
+	for (i = 0; i < dma_conf->dma_rx_size; i++) {
+		p = stmmac_get_rx_desc(priv, rx_q, i);
+		stmmac_clear_rx_owner(priv, p);
 	}
 
-	buf->addr = page_pool_get_dma_addr(buf->page) + buf->page_offset;
+	rx_q->buf_alloc_num = 0;
+	for (i = 0; i < dma_conf->dma_rx_size; i++) {
+		struct stmmac_rx_buffer *buf = &rx_q->buf_pool[i];
 
-	stmmac_set_desc_addr(priv, p, buf->addr);
-	if (dma_conf->dma_buf_sz == BUF_SIZE_16KiB)
-		stmmac_init_desc3(priv, p);
+		p = stmmac_get_rx_desc(priv, rx_q, i);
+		if (rx_q->xsk_pool) {
+			ret = stmmac_prepare_rx_buffer_zc(priv, rx_q, buf, p);
+			if (ret)
+				break;
+		} else {
+			ret = stmmac_init_rx_buffers(priv, dma_conf, p, i,
+						     GFP_KERNEL, queue);
+			if (ret)
+				return ret;
+		}
+		rx_q->buf_alloc_num++;
+	}
+
+	/* Hardware timestamp writeback may have overwritten chain pointers. */
+	if (priv->descriptor_mode == STMMAC_CHAIN_MODE) {
+		void *des = priv->extend_desc ? (void *)rx_q->dma_erx :
+					      (void *)rx_q->dma_rx;
+
+		stmmac_mode_init(priv, des, rx_q->dma_rx_phy,
+				 dma_conf->dma_rx_size, priv->extend_desc);
+	}
+
+	rx_q->rx_count_frames = 0;
+	stmmac_reset_rx_queue(priv, queue);
+
+	/* Initialize control fields and publish ownership only after restoring
+	 * addresses and chain pointers. DMA is stopped; initialization also
+	 * clears OWN again for the unpopulated XSK suffix before DMA restarts.
+	 */
+	dma_wmb();
+	stmmac_clear_rx_descriptors(priv, dma_conf, queue);
 
 	return 0;
 }
@@ -2157,16 +2259,37 @@ static void dma_free_tx_skbufs(struct stmmac_priv *priv,
 }
 
 /**
- * stmmac_free_tx_skbufs - free TX skb buffers
- * @priv: private structure
+ * stmmac_resume_descriptors - prepare TX and RX rings before restarting DMA
+ * @priv: driver private structure
+ *
+ * Release outstanding TX buffers before initializing each TX ring. Restore
+ * RX buffer addresses before initializing RX descriptors and their ownership.
+ * All DMA channels must be stopped throughout this operation.
+ *
+ * Return: 0 on success, or a negative errno if RX restoration fails.
  */
-static void stmmac_free_tx_skbufs(struct stmmac_priv *priv)
+static int stmmac_resume_descriptors(struct stmmac_priv *priv)
 {
-	u8 tx_queue_cnt = priv->plat->tx_queues_to_use;
-	u8 queue;
+	struct stmmac_dma_conf *dma_conf = &priv->dma_conf;
+	u32 queue;
+	int ret;
 
-	for (queue = 0; queue < tx_queue_cnt; queue++)
-		dma_free_tx_skbufs(priv, &priv->dma_conf, queue);
+	for (queue = 0; queue < priv->plat->tx_queues_to_use; queue++) {
+		stmmac_reset_tx_queue(priv, queue);
+		dma_free_tx_skbufs(priv, dma_conf, queue);
+		stmmac_clear_tx_descriptors(priv, dma_conf, queue);
+	}
+
+	for (queue = 0; queue < priv->plat->rx_queues_to_use; queue++) {
+		ret = stmmac_reinit_rx_descriptors(priv, dma_conf, queue);
+		if (ret) {
+			netdev_err(priv->dev, "Failed to restore RX queue %u: %d\n",
+				   queue, ret);
+			return ret;
+		}
+	}
+
+	return 0;
 }
 
 /**
@@ -5172,10 +5295,6 @@ static inline void stmmac_rx_refill(struct stmmac_priv *priv, u32 queue)
 	struct stmmac_rx_queue *rx_q = &priv->dma_conf.rx_queue[queue];
 	int dirty = stmmac_rx_dirty(priv, queue);
 	unsigned int entry = rx_q->dirty_rx;
-	gfp_t gfp = (GFP_ATOMIC | __GFP_NOWARN);
-
-	if (priv->dma_cap.host_dma_width <= 32)
-		gfp |= GFP_DMA32;
 
 	while (dirty-- > 0) {
 		struct stmmac_rx_buffer *buf = &rx_q->buf_pool[entry];
@@ -5184,25 +5303,9 @@ static inline void stmmac_rx_refill(struct stmmac_priv *priv, u32 queue)
 
 		p = stmmac_get_rx_desc(priv, rx_q, entry);
 
-		if (!buf->page) {
-			buf->page = page_pool_alloc_pages(rx_q->page_pool, gfp);
-			if (!buf->page)
-				break;
-		}
-
-		if (stmmac_rx_check_buf2_cap(priv) && !buf->sec_page) {
-			buf->sec_page = page_pool_alloc_pages(rx_q->page_pool, gfp);
-			if (!buf->sec_page)
-				break;
-
-			buf->sec_addr = page_pool_get_dma_addr(buf->sec_page);
-		}
-
-		buf->addr = page_pool_get_dma_addr(buf->page) + buf->page_offset;
-
-		stmmac_set_desc_addr(priv, p, buf->addr);
-		stmmac_set_desc_sec_addr(priv, p, buf->sec_addr,
-					 stmmac_rx_check_buf2_cap(priv));
+		if (stmmac_prepare_rx_buffer(priv, rx_q, buf, p,
+					     GFP_ATOMIC | __GFP_NOWARN))
+			break;
 		stmmac_refill_desc3(priv, rx_q, p);
 
 		use_rx_wd = !stmmac_rx_need_ic(priv, rx_q, queue);
@@ -5609,22 +5712,15 @@ static bool stmmac_rx_refill_zc(struct stmmac_priv *priv, u32 queue, u32 budget)
 
 	while (budget-- > 0) {
 		struct stmmac_rx_buffer *buf = &rx_q->buf_pool[entry];
-		dma_addr_t dma_addr;
+		struct dma_desc *p = stmmac_get_rx_desc(priv, rx_q, entry);
 		bool use_rx_wd;
 
-		if (!buf->xdp) {
-			buf->xdp = xsk_buff_alloc(rx_q->xsk_pool);
-			if (!buf->xdp) {
-				ret = false;
-				break;
-			}
+		if (stmmac_prepare_rx_buffer_zc(priv, rx_q, buf, p)) {
+			ret = false;
+			break;
 		}
 
-		rx_desc = stmmac_get_rx_desc(priv, rx_q, entry);
-
-		dma_addr = xsk_buff_xdp_get_dma(buf->xdp);
-		stmmac_set_desc_addr(priv, rx_desc, dma_addr);
-		stmmac_set_desc_sec_addr(priv, rx_desc, 0, false);
+		rx_desc = p;
 		stmmac_refill_desc3(priv, rx_q, rx_desc);
 
 		use_rx_wd = !stmmac_rx_need_ic(priv, rx_q, queue);
@@ -8563,10 +8659,9 @@ int stmmac_resume(struct device *dev)
 
 	mutex_lock(&priv->lock);
 
-	stmmac_reset_queues_param(priv);
-
-	stmmac_free_tx_skbufs(priv);
-	stmmac_clear_descriptors(priv, &priv->dma_conf);
+	ret = stmmac_resume_descriptors(priv);
+	if (ret)
+		goto error_unlock;
 
 	ret = stmmac_hw_setup(ndev);
 	if (ret < 0) {
Signature · 1 line
2.34.1
#5 lore raw

Re: [PATCH net v8 3/4] net: stmmac: restore RX descriptor buffer addresses on resume

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider.

Critical: 0 · High: 1 · Medium: 1 · Low: 2

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [High] This patch adds a failure point to stmmac_resume(): ret = stmmac_resume_descriptors(priv); if (ret) goto error_unlock;.
- [Medium] The new early failure in stmmac_resume() (stmmac_resume_descriptors() returning -ENOMEM) jumps to error_unlock.
- [Low] Part of the commit message describes code that is not in the tree.
- [Low] The commit message frames the bug only as GMAC4/XGMAC writeback overwriting buffer addresses, which leads to a 'fatal bus error'.

--

net: stmmac: restore RX descriptor buffer addresses on resume

The patch restores RX descriptor buffer addresses on resume. On GMAC4 and XGMAC, hardware writeback during suspend can overwrite them. Buffer preparation is factored into stmmac_prepare_rx_buffer() and stmmac_prepare_rx_buffer_zc(). A new stmmac_reinit_rx_descriptors() allocates any missing pages with GFP_KERNEL, and the per-ring resume work is gathered in stmmac_resume_descriptors().

Quoted 6 lines

Rebuild chain pointers and use stmmac_reset_rx_queue() to discard partial receive state from before the DMA reset. Initialize RX descriptors after restoring their addresses,
using the existing per-queue initializer to preserve the watchdog policy and legacy control fields. Keep the unpopulated XSK suffix CPU-owned when DMA restarts. This replaces the separate final ownership-setting loop.

[ ... ]

Quoted 1 line

The RX rings no longer undergo initialization before address restoration.

[Severity: Low]
Which ownership-setting loop does this replace? Before this patch, stmmac_resume() only called stmmac_reset_queues_param(), stmmac_free_tx_skbufs() and stmmac_clear_descriptors(). stmmac_set_rx_owner() is only used in stmmac_rx_refill() and stmmac_rx_refill_zc().

The second sentence has the same problem. The old resume path never restored RX addresses, so nothing was ever initialized "before address restoration".

Both sentences read like notes against an earlier, unposted version. Could they be reworded to describe what changes relative to the current tree?

Quoted 4 lines

Share buffer preparation with the refill paths and restore addresses from attached buffers. Allocate missing primary and secondary pages independently with GFP_KERNEL. If allocation fails, return the error without restarting DMA and leave the device detached.

[Severity: Low]
The commit message doesn't say why pages can be missing at resume time.

stmmac_rx() hands buf->page to the stack or page_pool and sets buf->page = NULL. If the GFP_ATOMIC refill in stmmac_rx_refill() then fails, that entry is left with no page attached.

The old resume path set OWN on every RX descriptor through stmmac_clear_descriptors() whether a page was attached or not. On cores whose writeback leaves the address word intact, the re-armed descriptor still pointed at a page the driver no longer owned. DMA could then write into recycled memory.

The new stmmac_reinit_rx_descriptors() fixes this too. Could the commit message describe this case, so stable backporters know the patch also fixes it?

Quoted 4 lines
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +0 -0
index f9ac82d2e7266..1508cf08f03b0 100644

[ ... ]

drivers/net/ethernet/stmicro/stmmac/stmmac_main.c int stmmac_resume(struct device *dev) · line 8664

Quoted 11 lines
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +3 -4
@@ -8563,10 +8659,9 @@ int stmmac_resume(struct device *dev)
 
 	mutex_lock(&priv->lock);
 
-	stmmac_reset_queues_param(priv);
-
-	stmmac_free_tx_skbufs(priv);
-	stmmac_clear_descriptors(priv, &priv->dma_conf);
+	ret = stmmac_resume_descriptors(priv);
+	if (ret)
+		goto error_unlock;

[Severity: High]
If stmmac_resume_descriptors() fails here, does NAPI stay disabled while netif_running() is still true?

stmmac_suspend() already called stmmac_disable_all_queues(), and error_unlock skips stmmac_enable_all_queues(). Any later stmmac_suspend() or stmmac_release() would then call napi_disable() a second time on the same instances:

stmmac_release()
__stmmac_release()

    stmmac_disable_all_queues()
      __stmmac_disable_all_queues()
        napi_disable(&ch->rx_napi)
          napi_disable_locked()
net/core/dev.c:napi_disable_locked() {
    ...
		while (val & (NAPIF_STATE_SCHED | NAPIF_STATE_NPSVC)) {
			usleep_range(20, 200);
			val = READ_ONCE(n->state);
		}
    ...
}

SCHED is still set from the first disable. Wouldn't this loop spin forever? In the close path (ip link set down, MTU change, XDP setup, ring resize) it would hold rtnl_lock. On the next system suspend it would hold priv->lock.

The -ENOMEM comes from the GFP_KERNEL page_pool allocations in stmmac_reinit_rx_descriptors() -> stmmac_init_rx_buffers(). These run during resume with the PM gfp restriction in place, so failure looks reachable under memory pressure.

The commit message says "If allocation fails, return the error without restarting DMA and leave the device detached". Is that detached state safe to leave the device in? The stmmac_hw_setup() failure path already behaves this way, but this patch adds a new way to reach it.

[Severity: Medium]
On the same failure path, is the MAC receiver left enabled?

error_unlock only powers down the legacy SerDes and drops the locks. Only error_stop_dma calls stmmac_mac_set(priv, priv->ioaddr, false). With Wake-on-LAN enabled, stmmac_suspend() never disables the MAC:

	if (priv->wolopts) {
		stmmac_pmt(priv, priv->hw, priv->wolopts);
		priv->irq_wake = 1;
	} else {
		stmmac_mac_set(priv, priv->ioaddr, false);

dwmac4_pmt() sets GMAC_CONFIG_RE when it arms wake-up. The stmmac_pmt(priv, priv->hw, 0) call earlier in stmmac_resume() only writes PMT=0 and leaves RE set. Because stmmac_hw_setup() is skipped, no DMA soft reset clears the configuration either.

After the failure, the MAC keeps receiving with the link still up, while RX DMA is stopped. The failing queue's descriptors have already been made CPU-owned by the OWN-clearing loop at the top of stmmac_reinit_rx_descriptors(). Incoming frames fill the MTL FIFO with nothing to drain it. STMMAC_DOWN is not set, so the pre-suspend interrupt enables can still reach stmmac_interrupt().

Should this path disable the MAC (and the DMA interrupts) before returning? Otherwise the device is neither armed for wake-up nor quiesced.

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1795096430.git.dinghui%40lixiang.com
author
#6 lore raw

PATCH v8 4/4 net: stmmac: restore the XSK TX pool on resume

In reply to Ding Hui

From: Ding Hui <[email protected]>

When suspend interrupts AF_XDP zero-copy transmission, resume releases outstanding TX buffers through dma_free_tx_skbufs(). If it completes XSK frames, that helper also clears tx_q->xsk_pool even though the socket remains bound to the queue.

The resume path does not run the TX ring allocation routine that normally sets this pointer. Consequently, stmmac_tx_clean() skips XSK transmission and a running TX-only socket can stop sending after resume, despite the link coming back up.

Restore the TX pool from the queue's existing XSK binding after releasing TX buffers and before restarting DMA and NAPI. Keep the buffer cleanup helper's behavior for teardown unchanged.

Fixes 132c32ee5bc0 ("net: stmmac: Add TX via XDP zero-copy socket")
---
 drivers/net/ethernet/stmicro/stmmac/stmmac_main.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +3 -0
index 1508cf08f03b..550a0bd647e1 100644
@@ -2277,6 +2277,9 @@ static int stmmac_resume_descriptors(struct stmmac_priv *priv)
 	for (queue = 0; queue < priv->plat->tx_queues_to_use; queue++) {
 		stmmac_reset_tx_queue(priv, queue);
 		dma_free_tx_skbufs(priv, dma_conf, queue);
+		/* Buffer cleanup may clear the pool; the socket remains bound. */
+		dma_conf->tx_queue[queue].xsk_pool =
+			stmmac_get_xsk_pool(priv, queue);
 		stmmac_clear_tx_descriptors(priv, dma_conf, queue);
 	}
 
Signature · 1 line
2.34.1
#7 lore raw

Re: [PATCH net v8 4/4] net: stmmac: restore the XSK TX pool on resume

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

Critical: 0 · High: 1 (1) · Medium: 0 · Low: 0

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [High] The fix is incomplete because it is applied to one caller instead of the shared helper.

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but the same resume path leaves corrupted TX next-descriptor pointers in chain mode.

--

net: stmmac: restore the XSK TX pool on resume

When dma_free_tx_skbufs() completes outstanding XSK TX frames, it also clears tx_q->xsk_pool. This commit restores that pointer in stmmac_resume_descriptors() after the call. Without the restore, stmmac_tx_clean() skips zero-copy transmission after resume, even though the AF_XDP socket is still bound.

drivers/net/ethernet/stmicro/stmmac/stmmac_main.c static int stmmac_resume_descriptors(struct stmmac_priv *priv) · line 2282

Quoted 11 lines
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +3 -0
index 1508cf08f03b0..550a0bd647e13 100644
@@ -2277,6 +2277,9 @@ static int stmmac_resume_descriptors(struct stmmac_priv *priv)
 	for (queue = 0; queue < priv->plat->tx_queues_to_use; queue++) {
 		stmmac_reset_tx_queue(priv, queue);
 		dma_free_tx_skbufs(priv, dma_conf, queue);
+		/* Buffer cleanup may clear the pool; the socket remains bound. */
+		dma_conf->tx_queue[queue].xsk_pool =
+			stmmac_get_xsk_pool(priv, queue);

[Severity: High]

Does stmmac_tx_err() run into the same problem? It is not a teardown path, but it also calls dma_free_tx_skbufs() and then restarts the queue:

drivers/net/ethernet/stmicro/stmmac/stmmac_main.c:stmmac_tx_err() {
    ...
	stmmac_stop_tx_dma(priv, chan);
	dma_free_tx_skbufs(priv, &priv->dma_conf, chan);
	stmmac_clear_tx_descriptors(priv, &priv->dma_conf, chan);
	stmmac_reset_tx_queue(priv, chan);
	stmmac_init_tx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
			    tx_q->dma_tx_phy, chan);
	stmmac_start_tx_dma(priv, chan);
    ...
}

stmmac_dma_interrupt() and stmmac_msi_intr_tx() call it on a TX hard error. If xsk_frames_done is non-zero, dma_free_tx_skbufs() does:

	if (tx_q->xsk_pool && tx_q->xsk_frames_done) {
		xsk_tx_completed(tx_q->xsk_pool, tx_q->xsk_frames_done);
		tx_q->xsk_frames_done = 0;
		tx_q->xsk_pool = NULL;
	}

Nothing in stmmac_tx_err() sets the pointer again, even though af_xdp_zc_qps still has the bit set for this queue.

After that, stmmac_tx_clean() skips its if (tx_q->xsk_pool) block, so stmmac_xdp_xmit_zc() never runs. stmmac_napi_check() also starts sending TX interrupts to ch->tx_napi:

	tx_napi = tx_q->xsk_pool ? &ch->rxtx_napi : &ch->tx_napi;

For ZC queues, stmmac_enable_all_queues() enabled rxtx_napi, not tx_napi.

Wouldn't this stall zero-copy TX in the same way as the resume case?

The commit message says:

Keep the buffer cleanup helper's behavior for teardown unchanged.

This suggests the other callers of dma_free_tx_skbufs() are all teardown paths, but stmmac_tx_err() is not one of them.

Would it be better to fix this in the shared helper instead? For example, xsk_pool could be cleared only in __free_dma_tx_desc_resources(), or dma_free_tx_skbufs() could stop clearing it altogether. Either would cover both paths and remove the need for a restore in each caller.

drivers/net/ethernet/stmicro/stmmac/stmmac_main.c static int stmmac_resume_descriptors(struct stmmac_priv *priv) · line 2284

Quoted 2 lines
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +0 -0
 		stmmac_clear_tx_descriptors(priv, dma_conf, queue);
 	}

[Severity: High]

This isn't a bug introduced by this patch, but does the TX loop here need the same chain-pointer rebuild as the RX loop?

In chain mode with non-extended descriptors and hwts_tx_en set, the MAC writes the timestamp seconds into des3 of the last-segment descriptor. In chain mode, des3 also holds the address of the next descriptor. clean_desc3() in chain_mode.c repairs this:

	if (tx_q->tx_skbuff_dma[entry].last_segment && !priv->extend_desc &&
	    priv->hwts_tx_en)
		/* NOTE: Device will overwrite des3 with timestamp value if
		 * 1588-2002 time stamping is enabled, hence reinitialize it
		 * to keep explicit chaining in the descriptor.
		 */

However, clean_desc3() only runs from stmmac_tx_clean(). stmmac_suspend() disables NAPI before it stops DMA. A frame that completes in between is never cleaned, so its des3 still holds the timestamp.

On resume, stmmac_clear_tx_descriptors()->ndesc_init_tx_desc() only clears TDES0_OWN and sets the chain bit. It does not touch des3, and nothing on this path calls stmmac_mode_init() for TX again.

The RX side of this function already handles the same hazard in stmmac_reinit_rx_descriptors():

	/* Hardware timestamp writeback may have overwritten chain pointers. */
	if (priv->descriptor_mode == STMMAC_CHAIN_MODE) {
		...
		stmmac_mode_init(priv, des, rx_q->dma_rx_phy,
				 dma_conf->dma_rx_size, priv->extend_desc);
	}

Could TX DMA then fetch its next descriptor from an address equal to the PTP seconds value? stmmac_tx_err() seems to have the same gap.

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1795096430.git.dinghui%40lixiang.com