Skip to contentSkip to conversation

net: sparx5/lan969x: add support for changing the MTU

PATCH Applied Daniel Machon 3 participants 24 messages Nov 20 – Nov 26

Subsystems (4)
lore
Share & tools
lore

Daniel Machon

author maintainer
#0 lore raw

v3 cover net: sparx5/lan969x: add support for changing the MTU

This series adds support for changing the MTU on Sparx5 and lan969x.

Currently, there is no ndo_change_mtu callback, and the MAC maximum frame length, the FDMA buffers, the queue watermarks and the TAS guard band are all fixed for a 1518 byte frame. This series makes them follow the MTU, and sets the maximum MTU from the 14000 byte frame limit of the MACs. As the FDMA is shared by all ports, new buffers are allocated while the FDMA keeps running, and the FDMA is only stopped briefly to replace them. Details are in the commits.

Patch 1 makes the FDMA stop sequence actually stop the channels, and patch 2 frees the FDMA buffers on init errors and deinit. Until now, the stop sequence was only used on deinit. This series also uses it, and the new free functions, to resize the buffers.

Patches 3-4 prepare for the MTU change, by adding a helper for setting the MAC maximum frame length, and by making the FDMA init and alloc functions operate on the rings passed to them.

Patches 5-6 size the FDMA buffers from the port MTUs, and add support for resizing them.

Patches 7-8 scale the queue watermarks and the TAS guard band with the MTU.

Patch 9 adds the ndo_change_mtu callback and sets the maximum MTU.

Tested on Sparx5 (PCB135) and lan969x (EV23X71A), by forwarding frames up to the MTU and checking that frames one byte over the MTU are dropped, by sending jumbo frames to and from the CPU while stepping the MTU up and down between 1500 and 9216, and by changing the MTU repeatedly while flooding the CPU with frames in both directions.

---
Changes in v3:
- Rebase on net-next, which now has commits 81d2cf1c6d56 ("net: sparx5:
fix RX ring stall on frames from inactive ports") and 1a47bc214d00
("net: lan969x: fix RX ring stall on frames from inactive ports").
- Patches 1-2: drop the Fixes tags and the testing notes.
- Patch 2: on Sparx5, clear the RX buffer slot as soon as the skb leaves
the ring, so that a frame dropped because of an inactive source port
is not freed again by sparx5_fdma_rx_free().
- Patch 6: likewise on lan969x, clear the RX buffer slot as soon as the
page leaves the ring, also when it goes back to the page_pool.
- Patch 9: extend the top RMON histogram range to the maximum frame
length.
- Patches 5, 6, 9: use SKB_WITH_OVERHEAD() for the lan969x DB size,
and fit lines to 80 columns.
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Patch 1: add a Fixes tag.
- Patch 2: also release the lan969x TX buffers that are still in flight
on deinit, store the length of the mapped TX buffers so that two-step
PTP skbs are not touched when unmapping, and add Fixes tags.
- Patch 6: reuse the lan969x TX buffer release from patch 2, and mention
in the commit message that frames still in flight are dropped.
- Rebase on net-next, which now has commit 873363ac591a1 ("net:
sparx5/lan969x: release the in-flight slot of a stale TX skb"), so
that a two-step PTP frame dropped by a resize no longer leaks its
timestamp slot.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Daniel Machon (9):
net: sparx5/lan969x: stop the FDMA channels properly
net: sparx5/lan969x: free the FDMA buffers on init errors and deinit
net: sparx5/lan969x: add a helper for setting the MAC max frame length
net: sparx5/lan969x: pass the FDMA rings to the init and alloc functions
net: sparx5/lan969x: size the FDMA buffers from the port MTUs
net: sparx5/lan969x: add support for resizing the FDMA buffers
net: sparx5/lan969x: scale the queue watermarks with the MTU
net: sparx5/lan969x: scale the TAS guard band with the MTU
net: sparx5/lan969x: add support for changing the MTU

 .../ethernet/microchip/sparx5/lan969x/lan969x.c    |  10 +
 .../ethernet/microchip/sparx5/lan969x/lan969x.h    |   2 +
 .../microchip/sparx5/lan969x/lan969x_fdma.c        | 185 ++++++++++--
 .../net/ethernet/microchip/sparx5/sparx5_ethtool.c |   2 +-
 .../net/ethernet/microchip/sparx5/sparx5_fdma.c    | 322 ++++++++++++++++++---
 .../net/ethernet/microchip/sparx5/sparx5_main.c    |   2 +
 .../net/ethernet/microchip/sparx5/sparx5_main.h    |  14 +
 .../ethernet/microchip/sparx5/sparx5_main_regs.h   |  34 +++
 .../net/ethernet/microchip/sparx5/sparx5_netdev.c  |  48 ++-
 .../net/ethernet/microchip/sparx5/sparx5_port.c    | 123 +++++---
 .../net/ethernet/microchip/sparx5/sparx5_port.h    |   5 +
 drivers/net/ethernet/microchip/sparx5/sparx5_tas.c |  43 ++-
 12 files changed, 687 insertions(+), 103 deletions(-)

---
base-commit: 2208ab00f4f213ade91ab775e7f8c4dacdcc856e
change-id: 20260930-sparx5-mtu-change-d7ebb1a7fcaa

Best regards,

Signature · 1 line
Daniel Machon <[email protected]>
author maintainer
#1 lore raw

PATCH v3 1/9 net: sparx5/lan969x: stop the FDMA channels properly

The FDMA channels are activated by setting their bit in FDMA_CH_ACTIVATE, and are disabled through FDMA_CH_DISABLE, after which a channel finishes the DCB it is fetching before it becomes inactive, as shown by FDMA_CH_ACTIVE. The injection and extraction ports are stopped through INJ_STOP and XTR_STOP.

sparx5_fdma_stop() tries to stop the channels by clearing their bit in FDMA_CH_ACTIVATE, which has no effect: the register only acts on the bits that are set, and its bits are cleared immediately by the hardware. It also never stops the injection port, and waits for XTR_BUF_IS_EMPTY to be 0, which means that the extraction buffer is not empty, instead of 1, so the wait always runs until it times out.

Add the FDMA_CH_ACTIVE register, and make sparx5_fdma_stop() disable the channels and wait for them to become inactive, stop the injection port, and wait for the extraction buffer to be empty.

The stop sequence is used by the FDMA deinit, and by the FDMA buffer resize added later in this series.

---
 .../net/ethernet/microchip/sparx5/sparx5_fdma.c    | 46 ++++++++++++++++------
 .../ethernet/microchip/sparx5/sparx5_main_regs.h   | 11 ++++++
 2 files changed, 45 insertions(+), 12 deletions(-)
drivers/net/ethernet/microchip/sparx5/sparx5_fdma.c +34 -12
index 8675702bf1504..8a847ec067de6 100644
@@ -20,6 +20,8 @@
 
 #define FDMA_XTR_BUFFER_SIZE		2048
 #define FDMA_WEIGHT			4
+#define SPX5_FDMA_STOP_SLEEP_US		500
+#define SPX5_FDMA_STOP_TIMEOUT_US	10000
 
 static int sparx5_fdma_tx_dataptr_cb(struct fdma *fdma, int dcb, int db,
 				     u64 *dataptr)
@@ -86,10 +88,6 @@ static void sparx5_fdma_rx_deactivate(struct sparx5 *sparx5, struct sparx5_rx *r
 {
 	struct fdma *fdma = &rx->fdma;
 
-	/* Deactivate the RX channel */
-	spx5_rmw(0, BIT(fdma->channel_id) & FDMA_CH_ACTIVATE_CH_ACTIVATE,
-		 sparx5, FDMA_CH_ACTIVATE);
-
 	/* Disable RX channel DB interrupt */
 	spx5_rmw(0, BIT(fdma->channel_id) & FDMA_INTR_DB_ENA_INTR_DB_ENA,
 		 sparx5, FDMA_INTR_DB_ENA);
@@ -125,9 +123,9 @@ static void sparx5_fdma_tx_activate(struct sparx5 *sparx5, struct sparx5_tx *tx)
 
 static void sparx5_fdma_tx_deactivate(struct sparx5 *sparx5, struct sparx5_tx *tx)
 {
-	/* Disable the channel */
-	spx5_rmw(0, BIT(tx->fdma.channel_id) & FDMA_CH_ACTIVATE_CH_ACTIVATE,
-		 sparx5, FDMA_CH_ACTIVATE);
+	/* Stop TX fdma */
+	spx5_rmw(FDMA_PORT_CTRL_INJ_STOP_SET(1), FDMA_PORT_CTRL_INJ_STOP,
+		 sparx5, FDMA_PORT_CTRL(0));
 }
 
 void sparx5_fdma_reload(struct sparx5 *sparx5, struct fdma *fdma)
@@ -453,6 +451,11 @@ static u32 sparx5_fdma_port_ctrl(struct sparx5 *sparx5)
 	return spx5_rd(sparx5, FDMA_PORT_CTRL(0));
 }
 
+static u32 sparx5_fdma_ch_active(struct sparx5 *sparx5)
+{
+	return spx5_rd(sparx5, FDMA_CH_ACTIVE);
+}
+
 int sparx5_fdma_start(struct sparx5 *sparx5)
 {
 	const struct sparx5_ops *ops = sparx5->data->ops;
@@ -474,20 +477,39 @@ int sparx5_fdma_start(struct sparx5 *sparx5)
 
 int sparx5_fdma_stop(struct sparx5 *sparx5)
 {
+	u32 channels = BIT(sparx5->rx.fdma.channel_id) |
+		       BIT(sparx5->tx.fdma.channel_id);
 	struct sparx5_rx *rx = &sparx5->rx;
 	struct sparx5_tx *tx = &sparx5->tx;
 	u32 val;
 
 	napi_disable(&rx->napi);
 
-	/* Stop the fdma and channel interrupts */
+	/* Disable the channels, and wait for them to finish the current DCB */
+	spx5_wr(FDMA_CH_DISABLE_CH_DISABLE_SET(channels), sparx5,
+		FDMA_CH_DISABLE);
+
+	if (read_poll_timeout(sparx5_fdma_ch_active,
+			      val,
+			      !(FDMA_CH_ACTIVE_CH_ACTIVE_GET(val) & channels),
+			      SPX5_FDMA_STOP_SLEEP_US,
+			      SPX5_FDMA_STOP_TIMEOUT_US,
+			      false,
+			      sparx5))
+		dev_warn(sparx5->dev, "Failed to stop FDMA channels\n");
+
+	/* Stop the fdma and the RX channel interrupt */
 	sparx5_fdma_rx_deactivate(sparx5, rx);
 	sparx5_fdma_tx_deactivate(sparx5, tx);
 
-	/* Wait for the RX channel to stop */
-	read_poll_timeout(sparx5_fdma_port_ctrl, val,
-			  FDMA_PORT_CTRL_XTR_BUF_IS_EMPTY_GET(val) == 0,
-			  500, 10000, 0, sparx5);
+	/* Wait for the extraction buffer to be empty */
+	read_poll_timeout(sparx5_fdma_port_ctrl,
+			  val,
+			  FDMA_PORT_CTRL_XTR_BUF_IS_EMPTY_GET(val),
+			  SPX5_FDMA_STOP_SLEEP_US,
+			  SPX5_FDMA_STOP_TIMEOUT_US,
+			  false,
+			  sparx5);
 
 	return 0;
 }
drivers/net/ethernet/microchip/sparx5/sparx5_main_regs.h +11 -0
index 071d2f013a195..73bc26bf4376e 100644
@@ -4900,6 +4900,17 @@ extern const struct sparx5_regs *regs;
 	__REG(TARGET_FDMA, 0, 1, 8, 0, 1, regs->gsize[GW_FDMA_FDMA], 148, r, 8,\
 	      4)
 
+/* FDMA:FDMA:FDMA_CH_ACTIVE */
+#define FDMA_CH_ACTIVE                                                         \
+	__REG(TARGET_FDMA, 0, 1, 8, 0, 1, regs->gsize[GW_FDMA_FDMA], 180, 0, 1,\
+	      4)
+
+#define FDMA_CH_ACTIVE_CH_ACTIVE                 GENMASK(7, 0)
+#define FDMA_CH_ACTIVE_CH_ACTIVE_SET(x)\
+	FIELD_PREP(FDMA_CH_ACTIVE_CH_ACTIVE, x)
+#define FDMA_CH_ACTIVE_CH_ACTIVE_GET(x)\
+	FIELD_GET(FDMA_CH_ACTIVE_CH_ACTIVE, x)
+
 /* FDMA:FDMA:FDMA_CH_CFG */
 #define FDMA_CH_CFG(r)                                                         \
 	__REG(TARGET_FDMA, 0, 1, 8, 0, 1, regs->gsize[GW_FDMA_FDMA], 224, r, 8,\
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH net-next v3 1/9] net: sparx5/lan969x: stop the FDMA channels properly

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: 0 · Medium: 2 · 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).

- [Medium] The new sparx5_fdma_stop() disables both channels (FDMA_CH_DISABLE) and waits for FDMA_CH_ACTIVE to clear.
- [Medium] sparx5_fdma_stop() now detects when the RX/TX channels fail to go inactive within 10 ms (SPX5_FDMA_STOP_TIMEOUT_US).
- [Low] This commit fixes bugs that already exist in sparx5_fdma_stop(), but it is framed as preparation for a feature and carries no Fixes:…

--

net: sparx5/lan969x: stop the FDMA channels properly

This commit changes sparx5_fdma_stop() so that it disables the RX and TX channels through FDMA_CH_DISABLE and waits for FDMA_CH_ACTIVE to clear. The old code cleared bits in FDMA_CH_ACTIVATE, which has no effect. The commit also stops the injection port with INJ_STOP, fixes the inverted XTR_BUF_IS_EMPTY poll, and adds the FDMA_CH_ACTIVE register definition.

Quoted 2 lines

The stop sequence is used by the FDMA deinit, and by the FDMA buffer resize added later in this series.

[Severity: Low]
The patch is presented as groundwork for the resize later in the series. But the commit message also says the current stop sequence never stops the channels, and that the XTR_BUF_IS_EMPTY wait always runs until it times out.

Both of those problems seem to date back to:

Fixes 10615907e9b5 ("net: sparx5: switchdev: adding frame DMA functionality")

The existing deinit paths free DMA memory right after the stop. sparx5_fdma_deinit() calls fdma_free_phys() on the DCBs, and lan969x_fdma_deinit() frees the coherent rings and the page_pool. So on current kernels, unbind or probe-failure teardown frees DCB memory while the channels are still active, and every stop stalls for 10 ms.

Should this carry a Fixes: tag and go to net on its own, rather than as part of the net-next MTU series? Without the tag, stable kernels will not pick it up.

Could the commit message also mention the risk of the device doing DMA into freed memory?

Quoted 4 lines
drivers/net/ethernet/microchip/sparx5/sparx5_fdma.c +0 -0
index 8675702bf1504..8a847ec067de6 100644

[ ... ]

Quoted 15 lines
drivers/net/ethernet/microchip/sparx5/sparx5_fdma.c +4 -1
@@ -474,20 +477,39 @@ int sparx5_fdma_start(struct sparx5 *sparx5)
 
 int sparx5_fdma_stop(struct sparx5 *sparx5)
 {
+	u32 channels = BIT(sparx5->rx.fdma.channel_id) |
+		       BIT(sparx5->tx.fdma.channel_id);
 	struct sparx5_rx *rx = &sparx5->rx;
 	struct sparx5_tx *tx = &sparx5->tx;
 	u32 val;
 
 	napi_disable(&rx->napi);
 
-	/* Stop the fdma and channel interrupts */
+	/* Disable the channels, and wait for them to finish the current DCB */
+	spx5_wr(FDMA_CH_DISABLE_CH_DISABLE_SET(channels), sparx5,

drivers/net/ethernet/microchip/sparx5/sparx5_fdma.c int sparx5_fdma_start(struct sparx5 *sparx5) · line 499

Quoted 10 lines
drivers/net/ethernet/microchip/sparx5/sparx5_fdma.c +10 -0
+		FDMA_CH_DISABLE);
+
+	if (read_poll_timeout(sparx5_fdma_ch_active,
+			      val,
+			      !(FDMA_CH_ACTIVE_CH_ACTIVE_GET(val) & channels),
+			      SPX5_FDMA_STOP_SLEEP_US,
+			      SPX5_FDMA_STOP_TIMEOUT_US,
+			      false,
+			      sparx5))
+		dev_warn(sparx5->dev, "Failed to stop FDMA channels\n");

[Severity: Medium]
What happens to the DMA memory if this times out? The timeout only causes a dev_warn(), and sparx5_fdma_stop() still returns 0. The deinit callers then free everything without checking the result:

sparx5_fdma_deinit() {
	sparx5_fdma_stop(sparx5);
	fdma_free_phys(&sparx5->rx.fdma);
	fdma_free_phys(&sparx5->tx.fdma);
	...
}
lan969x_fdma_deinit() {
	...
	sparx5_fdma_stop(sparx5);
	fdma_free_coherent(sparx5->dev, &tx->fdma);
	fdma_free_coherent(sparx5->dev, &rx->fdma);
	lan969x_fdma_free_pages(rx);
	page_pool_destroy(rx->page_pool);
	...
}

After the next patch in the series, sparx5_fdma_deinit() also calls consume_skb() on the RX skbs.

If a channel is still active after the 10 ms SPX5_FDMA_STOP_TIMEOUT_US, can the extraction engine write received frame data and DCB status words into freed and reused memory? Can the injection engine also keep reading freed memory?

For comparison, lan966x uses the same CH_DISABLE/CH_ACTIVE sequence with a READL_TIMEOUT_US of 100 s rather than 10 ms.

Later in the series, sparx5_fdma_pause() follows this stop with an FDMA_CTRL_NRESET reset, which covers the resize path. At the end of the series, both deinit paths still have no reset and no error handling.

Would it work to reset the FDMA (FDMA_CTRL_NRESET 0 then 1) after the stop in the deinit paths? Another option is to return the timeout from sparx5_fdma_stop() and not free the DMA memory when it fails.

Quoted 7 lines
drivers/net/ethernet/microchip/sparx5/sparx5_fdma.c +2 -2
+
+	/* Stop the fdma and the RX channel interrupt */
 	sparx5_fdma_rx_deactivate(sparx5, rx);
 	sparx5_fdma_tx_deactivate(sparx5, tx);
 
-	/* Wait for the RX channel to stop */
-	read_poll_timeout(sparx5_fdma_port_ctrl, val,

drivers/net/ethernet/microchip/sparx5/sparx5_fdma.c int sparx5_fdma_start(struct sparx5 *sparx5) · line 512

Quoted 10 lines
drivers/net/ethernet/microchip/sparx5/sparx5_fdma.c +8 -2
-			  FDMA_PORT_CTRL_XTR_BUF_IS_EMPTY_GET(val) == 0,
-			  500, 10000, 0, sparx5);
+	/* Wait for the extraction buffer to be empty */
+	read_poll_timeout(sparx5_fdma_port_ctrl,
+			  val,
+			  FDMA_PORT_CTRL_XTR_BUF_IS_EMPTY_GET(val),
+			  SPX5_FDMA_STOP_SLEEP_US,
+			  SPX5_FDMA_STOP_TIMEOUT_US,
+			  false,
+			  sparx5);

[Severity: Medium]
Can this wait succeed if the extraction buffer is not already empty?

Both channels, including the extraction channel, are disabled and polled until inactive first. Only after that does sparx5_fdma_rx_deactivate() set XTR_STOP:

sparx5_fdma_rx_deactivate() {
	...
	/* Stop RX fdma */
	spx5_rmw(FDMA_PORT_CTRL_XTR_STOP_SET(1), FDMA_PORT_CTRL_XTR_STOP,
		 sparx5, FDMA_PORT_CTRL(0));
}

During the disable window XTR_STOP is still 0, so the extraction port can keep moving frames into the FDMA extraction buffer. Once the extraction channel is inactive, nothing writes that buffer out to memory.

In that case the wait runs for the full 10 ms. Its return value is then dropped without a warning. FDMA_PORT_CTRL_XTR_BUF_RST is also never used.

Would the expected order be to set XTR_STOP, wait for XTR_BUF_IS_EMPTY while the channel is still active, and then do CH_DISABLE and wait for CH_ACTIVE? Resetting the buffer with XTR_BUF_RST could be another option.

Later in the series, sparx5_fdma_pause() drains the CPU queues before calling this stop, and runs FDMA_CTRL_NRESET plus sparx5_xtr_flush() after it. That lowers the risk on the resize path. sparx5_fdma_stop() itself is unchanged at the end of the series, though, so this wait still does not do what the comment and the commit message describe.

Quoted 3 lines
drivers/net/ethernet/microchip/sparx5/sparx5_fdma.c +0 -0
 	return 0;
 }
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261120-sparx5-mtu-change-v3-0-55c272fe1acc%40microchip.com
author maintainer
#3 lore raw

Re: [PATCH net-next v3 1/9] net: sparx5/lan969x: stop the FDMA channels properly

Quoted 120 lines

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

Critical: 0 · High: 0 · Medium: 2 · 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).

- [Medium] The new sparx5_fdma_stop() disables both channels (FDMA_CH_DISABLE) and waits for FDMA_CH_ACTIVE to clear.
- [Medium] sparx5_fdma_stop() now detects when the RX/TX channels fail to go inactive within 10 ms (SPX5_FDMA_STOP_TIMEOUT_US).
- [Low] This commit fixes bugs that already exist in sparx5_fdma_stop(), but it is framed as preparation for a feature and carries no Fixes:…

--

net: sparx5/lan969x: stop the FDMA channels properly

This commit changes sparx5_fdma_stop() so that it disables the RX and TX channels through FDMA_CH_DISABLE and waits for FDMA_CH_ACTIVE to clear. The old code cleared bits in FDMA_CH_ACTIVATE, which has no effect. The commit also stops the injection port with INJ_STOP, fixes the inverted XTR_BUF_IS_EMPTY poll, and adds the FDMA_CH_ACTIVE register definition.

Quoted 2 lines

The stop sequence is used by the FDMA deinit, and by the FDMA buffer resize added later in this series.

[Severity: Low]
The patch is presented as groundwork for the resize later in the series. But the commit message also says the current stop sequence never stops the channels, and that the XTR_BUF_IS_EMPTY wait always runs until it times out.

Both of those problems seem to date back to:

Fixes 10615907e9b5 ("net: sparx5: switchdev: adding frame DMA functionality")

The existing deinit paths free DMA memory right after the stop. sparx5_fdma_deinit() calls fdma_free_phys() on the DCBs, and lan969x_fdma_deinit() frees the coherent rings and the page_pool. So on current kernels, unbind or probe-failure teardown frees DCB memory while the channels are still active, and every stop stalls for 10 ms.

Should this carry a Fixes: tag and go to net on its own, rather than as part of the net-next MTU series? Without the tag, stable kernels will not pick it up.

Could the commit message also mention the risk of the device doing DMA into freed memory?

Quoted 4 lines
drivers/net/ethernet/microchip/sparx5/sparx5_fdma.c +0 -0
index 8675702bf1504..8a847ec067de6 100644

[ ... ]

Quoted 15 lines
drivers/net/ethernet/microchip/sparx5/sparx5_fdma.c +4 -1
@@ -474,20 +477,39 @@ int sparx5_fdma_start(struct sparx5 *sparx5)
 
 int sparx5_fdma_stop(struct sparx5 *sparx5)
 {
+	u32 channels = BIT(sparx5->rx.fdma.channel_id) |
+		       BIT(sparx5->tx.fdma.channel_id);
 	struct sparx5_rx *rx = &sparx5->rx;
 	struct sparx5_tx *tx = &sparx5->tx;
 	u32 val;
 
 	napi_disable(&rx->napi);
 
-	/* Stop the fdma and channel interrupts */
+	/* Disable the channels, and wait for them to finish the current DCB */
+	spx5_wr(FDMA_CH_DISABLE_CH_DISABLE_SET(channels), sparx5,

drivers/net/ethernet/microchip/sparx5/sparx5_fdma.c int sparx5_fdma_start(struct sparx5 *sparx5) · line 499

Quoted 10 lines
drivers/net/ethernet/microchip/sparx5/sparx5_fdma.c +10 -0
+		FDMA_CH_DISABLE);
+
+	if (read_poll_timeout(sparx5_fdma_ch_active,
+			      val,
+			      !(FDMA_CH_ACTIVE_CH_ACTIVE_GET(val) & channels),
+			      SPX5_FDMA_STOP_SLEEP_US,
+			      SPX5_FDMA_STOP_TIMEOUT_US,
+			      false,
+			      sparx5))
+		dev_warn(sparx5->dev, "Failed to stop FDMA channels\n");

[Severity: Medium]
What happens to the DMA memory if this times out? The timeout only causes a dev_warn(), and sparx5_fdma_stop() still returns 0. The deinit callers then free everything without checking the result:

sparx5_fdma_deinit() {
	sparx5_fdma_stop(sparx5);
	fdma_free_phys(&sparx5->rx.fdma);
	fdma_free_phys(&sparx5->tx.fdma);
	...
}
lan969x_fdma_deinit() {
	...
	sparx5_fdma_stop(sparx5);
	fdma_free_coherent(sparx5->dev, &tx->fdma);
	fdma_free_coherent(sparx5->dev, &rx->fdma);
	lan969x_fdma_free_pages(rx);
	page_pool_destroy(rx->page_pool);
	...
}

After the next patch in the series, sparx5_fdma_deinit() also calls consume_skb() on the RX skbs.

If a channel is still active after the 10 ms SPX5_FDMA_STOP_TIMEOUT_US, can the extraction engine write received frame data and DCB status words into freed and reused memory? Can the injection engine also keep reading freed memory?

For comparison, lan966x uses the same CH_DISABLE/CH_ACTIVE sequence with a READL_TIMEOUT_US of 100 s rather than 10 ms.

Quoted 8 lines

Later in the series, sparx5_fdma_pause() follows this stop with an FDMA_CTRL_NRESET reset, which covers the resize path. At the end of the series, both deinit paths still have no reset and no error handling.

Would it work to reset the FDMA (FDMA_CTRL_NRESET 0 then 1) after the stop in the deinit paths? Another option is to return the timeout from sparx5_fdma_stop() and not free the DMA memory when it fails.

This was already dismissed on v2 [1].

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

Quoted 43 lines
Quoted 7 lines
drivers/net/ethernet/microchip/sparx5/sparx5_fdma.c +2 -2
+
+	/* Stop the fdma and the RX channel interrupt */
 	sparx5_fdma_rx_deactivate(sparx5, rx);
 	sparx5_fdma_tx_deactivate(sparx5, tx);
 
-	/* Wait for the RX channel to stop */
-	read_poll_timeout(sparx5_fdma_port_ctrl, val,

drivers/net/ethernet/microchip/sparx5/sparx5_fdma.c int sparx5_fdma_start(struct sparx5 *sparx5) · line 512

Quoted 10 lines
drivers/net/ethernet/microchip/sparx5/sparx5_fdma.c +8 -2
-			  FDMA_PORT_CTRL_XTR_BUF_IS_EMPTY_GET(val) == 0,
-			  500, 10000, 0, sparx5);
+	/* Wait for the extraction buffer to be empty */
+	read_poll_timeout(sparx5_fdma_port_ctrl,
+			  val,
+			  FDMA_PORT_CTRL_XTR_BUF_IS_EMPTY_GET(val),
+			  SPX5_FDMA_STOP_SLEEP_US,
+			  SPX5_FDMA_STOP_TIMEOUT_US,
+			  false,
+			  sparx5);

[Severity: Medium]
Can this wait succeed if the extraction buffer is not already empty?

Both channels, including the extraction channel, are disabled and polled until inactive first. Only after that does sparx5_fdma_rx_deactivate() set XTR_STOP:

sparx5_fdma_rx_deactivate() {
	...
	/* Stop RX fdma */
	spx5_rmw(FDMA_PORT_CTRL_XTR_STOP_SET(1), FDMA_PORT_CTRL_XTR_STOP,
		 sparx5, FDMA_PORT_CTRL(0));
}

During the disable window XTR_STOP is still 0, so the extraction port can keep moving frames into the FDMA extraction buffer. Once the extraction channel is inactive, nothing writes that buffer out to memory.

In that case the wait runs for the full 10 ms. Its return value is then dropped without a warning. FDMA_PORT_CTRL_XTR_BUF_RST is also never used.

Would the expected order be to set XTR_STOP, wait for XTR_BUF_IS_EMPTY while the channel is still active, and then do CH_DISABLE and wait for CH_ACTIVE? Resetting the buffer with XTR_BUF_RST could be another option.

Quoted 5 lines

Later in the series, sparx5_fdma_pause() drains the CPU queues before calling this stop, and runs FDMA_CTRL_NRESET plus sparx5_xtr_flush() after it. That lowers the risk on the resize path. sparx5_fdma_stop() itself is unchanged at the end of the series, though, so this wait still does not do what the comment and the commit message describe.

The datasheet only requires the channel to be disabled before its DBs are discarded, so disabling it before XTR_STOP is fine. No issues observed about this ordering in our tests.

Quoted 7 lines
Quoted 4 lines
drivers/net/ethernet/microchip/sparx5/sparx5_fdma.c +0 -0
 	return 0;
 }
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261120-sparx5-mtu-change-v3-0-55c272fe1acc%40microchip.com
author maintainer
#4 lore raw

PATCH v3 2/9 net: sparx5/lan969x: free the FDMA buffers on init errors and deinit

In reply to Daniel Machon

The FDMA RX buffers are skbs on Sparx5, and pages from a page_pool on lan969x, which are handed to the FDMA through the RX DCBs. On lan969x, the TX DCBs also have an array holding the state of the TX buffers.

These are not all freed. On Sparx5, the RX skbs are never freed, and if the TX buffers cannot be allocated, the RX DCBs are not freed. On lan969x, if the RX DCBs cannot be allocated, the page_pool is not destroyed, if the TX buffers cannot be allocated, only the RX DCBs are freed, and lan969x_fdma_deinit() never frees the TX buffer state, nor the TX buffers that are still in flight. On both, the return value of fdma_dcbs_init() is also ignored for RX, so if a buffer cannot be allocated while filling the RX ring, the FDMA is started with a partly filled ring, and on lan969x, lan969x_fdma_free_pages() later puts the pages that were never allocated.

Add sparx5_fdma_rx_free(), which frees the RX skbs and DCBs, lan969x_fdma_rx_free(), which returns the pages to the page_pool and frees the page_pool and the RX DCBs, and lan969x_fdma_tx_free(), which releases the TX buffers still in flight and frees the TX buffer state and the TX DCBs, and use them on the error paths and on deinit. Check the return value of fdma_dcbs_init() for RX. To know which RX buffers are still owned by the driver, clear the RX buffer slot once the buffer has left the ring on Sparx5, either to the stack or dropped, and make lan969x_fdma_free_pages() skip and clear the empty slots of the ring. The two-step PTP skbs are owned by the PTP code, which may already have freed them when a TX buffer is released, so store the length of the mapped TX buffer, and use it instead of the skb length.

---
 .../microchip/sparx5/lan969x/lan969x_fdma.c        | 85 +++++++++++++++++-----
 .../net/ethernet/microchip/sparx5/sparx5_fdma.c    | 30 +++++++-
 .../net/ethernet/microchip/sparx5/sparx5_main.h    |  1 +
 3 files changed, 95 insertions(+), 21 deletions(-)
drivers/net/ethernet/microchip/sparx5/lan969x/lan969x_fdma.c +68 -17
index fd532c89f9110..5f96910df5383 100644
@@ -67,13 +67,13 @@ static void lan969x_fdma_tx_clear_buf(struct sparx5 *sparx5, int weight)
 		if (!fdma_db_is_done(fdma_db_get(fdma, i, 0)))
 			continue;
 
-		db->dev->stats.tx_bytes += db->skb->len;
+		db->dev->stats.tx_bytes += db->len;
 		db->dev->stats.tx_packets++;
 		sparx5->tx.packets++;
 
 		dma_unmap_single(sparx5->dev,
 				 db->dma_addr,
-				 db->skb->len,
+				 db->len,
 				 DMA_TO_DEVICE);
 
 		if (!db->ptp)
@@ -90,12 +90,26 @@ static void lan969x_fdma_free_pages(struct sparx5_rx *rx)
 	struct fdma *fdma = &rx->fdma;
 
 	for (int i = 0; i < fdma->n_dcbs; ++i) {
-		for (int j = 0; j < fdma->n_dbs; ++j)
+		for (int j = 0; j < fdma->n_dbs; ++j) {
+			if (!rx->page[i][j])
+				continue;
+
 			page_pool_put_full_page(rx->page_pool,
 						rx->page[i][j], false);
+			rx->page[i][j] = NULL;
+		}
 	}
 }
 
+static void lan969x_fdma_rx_free(struct sparx5 *sparx5)
+{
+	struct sparx5_rx *rx = &sparx5->rx;
+
+	lan969x_fdma_free_pages(rx);
+	fdma_free_coherent(sparx5->dev, &rx->fdma);
+	page_pool_destroy(rx->page_pool);
+}
+
 static struct sk_buff *lan969x_fdma_rx_get_frame(struct sparx5 *sparx5,
 						 struct sparx5_rx *rx)
 {
@@ -172,14 +186,18 @@ static int lan969x_fdma_rx_alloc(struct sparx5 *sparx5)
 		return PTR_ERR(rx->page_pool);
 
 	err = fdma_alloc_coherent(sparx5->dev, fdma);
-	if (err)
+	if (err) {
+		page_pool_destroy(rx->page_pool);
 		return err;
+	}
 
-	fdma_dcbs_init(fdma,
-		       FDMA_DCB_INFO_DATAL(fdma->db_size),
-		       FDMA_DCB_STATUS_INTR);
+	err = fdma_dcbs_init(fdma,
+			     FDMA_DCB_INFO_DATAL(fdma->db_size),
+			     FDMA_DCB_STATUS_INTR);
+	if (err)
+		lan969x_fdma_rx_free(sparx5);
 
-	return 0;
+	return err;
 }
 
 static int lan969x_fdma_tx_alloc(struct sparx5 *sparx5)
@@ -205,6 +223,44 @@ static int lan969x_fdma_tx_alloc(struct sparx5 *sparx5)
 	return 0;
 }
 
+/* Release the TX buffers that are still in flight. The two-step PTP skbs are
+ * owned by the port tx_skbs list, so they are only unmapped.
+ */
+static void lan969x_fdma_tx_release(struct sparx5 *sparx5)
+{
+	struct sparx5_tx *tx = &sparx5->tx;
+	struct sparx5_tx_buf *db;
+
+	for (int i = 0; i < tx->fdma.n_dcbs; ++i) {
+		db = &tx->dbs[i];
+
+		if (!db->used)
+			continue;
+
+		db->dev->stats.tx_dropped++;
+		tx->dropped++;
+
+		dma_unmap_single(sparx5->dev,
+				 db->dma_addr,
+				 db->len,
+				 DMA_TO_DEVICE);
+
+		if (!db->ptp)
+			dev_kfree_skb_any(db->skb);
+
+		db->used = false;
+	}
+}
+
+static void lan969x_fdma_tx_free(struct sparx5 *sparx5)
+{
+	struct sparx5_tx *tx = &sparx5->tx;
+
+	lan969x_fdma_tx_release(sparx5);
+	fdma_free_coherent(sparx5->dev, &tx->fdma);
+	kfree(tx->dbs);
+}
+
 static void lan969x_fdma_rx_init(struct sparx5 *sparx5)
 {
 	struct fdma *fdma = &sparx5->rx.fdma;
@@ -331,6 +387,7 @@ int lan969x_fdma_xmit(struct sparx5 *sparx5, u32 *ifh, struct sk_buff *skb,
 
 	db_buf->dev = dev;
 	db_buf->skb = skb;
+	db_buf->len = skb->len;
 	db_buf->ptp = false;
 	db_buf->used = true;
 
@@ -354,7 +411,6 @@ int lan969x_fdma_xmit(struct sparx5 *sparx5, u32 *ifh, struct sk_buff *skb,
 
 int lan969x_fdma_init(struct sparx5 *sparx5)
 {
-	struct sparx5_rx *rx = &sparx5->rx;
 	int err;
 
 	lan969x_fdma_rx_init(sparx5);
@@ -376,7 +432,7 @@ int lan969x_fdma_init(struct sparx5 *sparx5)
 
 	err = lan969x_fdma_tx_alloc(sparx5);
 	if (err) {
-		fdma_free_coherent(sparx5->dev, &rx->fdma);
+		lan969x_fdma_rx_free(sparx5);
 		dev_err(sparx5->dev, "Failed to allocate TX buffers: %d\n",
 			err);
 		return err;
@@ -391,14 +447,9 @@ int lan969x_fdma_init(struct sparx5 *sparx5)
 
 int lan969x_fdma_deinit(struct sparx5 *sparx5)
 {
-	struct sparx5_rx *rx = &sparx5->rx;
-	struct sparx5_tx *tx = &sparx5->tx;
-
 	sparx5_fdma_stop(sparx5);
-	fdma_free_coherent(sparx5->dev, &tx->fdma);
-	fdma_free_coherent(sparx5->dev, &rx->fdma);
-	lan969x_fdma_free_pages(rx);
-	page_pool_destroy(rx->page_pool);
+	lan969x_fdma_tx_free(sparx5);
+	lan969x_fdma_rx_free(sparx5);
 
 	return 0;
 }
drivers/net/ethernet/microchip/sparx5/sparx5_fdma.c +26 -4
index 8a847ec067de6..f1d44e5e080b6 100644
@@ -147,6 +147,10 @@ static bool sparx5_fdma_rx_get_frame(struct sparx5 *sparx5, struct sparx5_rx *rx
 	if (unlikely(!fdma_db_is_done(db_hw)))
 		return false;
 	skb = rx->skb[fdma->dcb_index][fdma->db_index];
+
+	/* The skb leaves the ring here, so clear its slot */
+	rx->skb[fdma->dcb_index][fdma->db_index] = NULL;
+
 	skb_put(skb, fdma_db_len_get(db_hw));
 	/* Now do the normal processing of the skb */
 	sparx5_ifh_parse(sparx5, (u32 *)skb->data, &fi);
@@ -245,6 +249,21 @@ int sparx5_fdma_xmit(struct sparx5 *sparx5, u32 *ifh, struct sk_buff *skb,
 	return NETDEV_TX_OK;
 }
 
+/* Free the RX skbs owned by the driver, and the RX DCBs */
+static void sparx5_fdma_rx_free(struct sparx5_rx *rx)
+{
+	struct fdma *fdma = &rx->fdma;
+
+	for (int i = 0; i < fdma->n_dcbs; i++) {
+		for (int j = 0; j < fdma->n_dbs; j++) {
+			consume_skb(rx->skb[i][j]);
+			rx->skb[i][j] = NULL;
+		}
+	}
+
+	fdma_free_phys(fdma);
+}
+
 static int sparx5_fdma_rx_alloc(struct sparx5 *sparx5)
 {
 	struct sparx5_rx *rx = &sparx5->rx;
@@ -255,10 +274,12 @@ static int sparx5_fdma_rx_alloc(struct sparx5 *sparx5)
 	if (err)
 		return err;
 
-	fdma_dcbs_init(fdma, FDMA_DCB_INFO_DATAL(fdma->db_size),
-		       FDMA_DCB_STATUS_INTR);
+	err = fdma_dcbs_init(fdma, FDMA_DCB_INFO_DATAL(fdma->db_size),
+			     FDMA_DCB_STATUS_INTR);
+	if (err)
+		sparx5_fdma_rx_free(rx);
 
-	return 0;
+	return err;
 }
 
 static int sparx5_fdma_tx_alloc(struct sparx5 *sparx5)
@@ -431,6 +452,7 @@ int sparx5_fdma_init(struct sparx5 *sparx5)
 	}
 	err = sparx5_fdma_tx_alloc(sparx5);
 	if (err) {
+		sparx5_fdma_rx_free(&sparx5->rx);
 		dev_err(sparx5->dev, "Could not allocate TX buffers: %d\n", err);
 		return err;
 	}
@@ -440,7 +462,7 @@ int sparx5_fdma_init(struct sparx5 *sparx5)
 int sparx5_fdma_deinit(struct sparx5 *sparx5)
 {
 	sparx5_fdma_stop(sparx5);
-	fdma_free_phys(&sparx5->rx.fdma);
+	sparx5_fdma_rx_free(&sparx5->rx);
 	fdma_free_phys(&sparx5->tx.fdma);
 
 	return 0;
drivers/net/ethernet/microchip/sparx5/sparx5_main.h +1 -0
index f6316086e6ecb..19b38922823ec 100644
@@ -180,6 +180,7 @@ struct sparx5_tx_buf {
 	dma_addr_t dma_addr;
 	bool used;
 	bool ptp;
+	u32 len;
 };
 
 /* Frame DMA transmit state:
Signature · 1 line
2.34.1
#5 lore raw

Re: [PATCH net-next v3 2/9] net: sparx5/lan969x: free the FDMA buffers on init errors and deinit

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

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

Critical: 0 · High: 0 (1) · Medium: 1 · 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).

- [Medium] At this commit, lan969x_fdma_rx_get_frame() (drivers/net/ethernet/microchip/sparx5/lan969x/lan969x_fdma.c) reads…
- [Low] This patch fixes several real bugs but has no Fixes: tag: - Sparx5 RX skbs leak on every deinit, and the RX DCBs leak when TX…

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but both NAPI pollers ignore fdma_dcb_add() failures when re-arming consumed RX…

--

net: sparx5/lan969x: free the FDMA buffers on init errors and deinit

This patch adds sparx5_fdma_rx_free(), lan969x_fdma_rx_free() and lan969x_fdma_tx_free(), and calls them on the init error paths and on deinit, so the RX skbs/pages, page_pool, DCBs and in-flight TX buffers are released. It also checks the fdma_dcbs_init() return value for RX. It stores the mapped TX length in struct sparx5_tx_buf instead of reading it from the skb.

Quoted 8 lines
drivers/net/ethernet/microchip/sparx5/lan969x/lan969x_fdma.c +0 -0
index fd532c89f9110..5f96910df5383 100644
@@ -67,13 +67,13 @@ static void lan969x_fdma_tx_clear_buf(struct sparx5 *sparx5, int weight)
 		if (!fdma_db_is_done(fdma_db_get(fdma, i, 0)))
 			continue;
 

drivers/net/ethernet/microchip/sparx5/lan969x/lan969x_fdma.c static void lan969x_fdma_tx_clear_buf(struct sparx5 *sparx5, int weight) · line 77

Quoted 10 lines
drivers/net/ethernet/microchip/sparx5/lan969x/lan969x_fdma.c +2 -2
-		db->dev->stats.tx_bytes += db->skb->len;
+		db->dev->stats.tx_bytes += db->len;
 		db->dev->stats.tx_packets++;
 		sparx5->tx.packets++;
 
 		dma_unmap_single(sparx5->dev,
 				 db->dma_addr,
-				 db->skb->len,
+				 db->len,
 				 DMA_TO_DEVICE);

[Severity: Low]
Should this patch carry Fixes: tags?

Before this change, lan969x_fdma_tx_clear_buf() read db->skb->len for two-step PTP skbs in the normal TX completion path. Those skbs stay on port->tx_skbs, and lan969x_ptp_irq_handler() can free them before NAPI reaps the TX descriptor:

lan969x_ptp_irq_handler() {
    ...
			__skb_unlink(skb, &port->tx_skbs);
			skb_match = skb;
    ...
		dev_kfree_skb_any(skb_match);
    ...
}

The PTP timeout purge can free them the same way. That makes the old read a use-after-free, and dma_unmap_single() could be given the wrong size.

The commit message explains the switch to db->len. It doesn't say that this fixes a use-after-free at runtime, and the subject only mentions init errors and deinit.

The other fixes here also have no Fixes: reference:

- the Sparx5 RX skb leak on deinit
- the lan969x page_pool, TX DCB, tx->dbs and in-flight skb leaks
- the put of never-allocated pages after a partial RX fill

Without the tags, stable trees that ship lan969x FDMA probably won't pick up the use-after-free fix. Would it make sense to split the lan969x and Sparx5 fixes, since they go back to different commits, and tag each one?

Quoted 5 lines
drivers/net/ethernet/microchip/sparx5/lan969x/lan969x_fdma.c +0 -1
@@ -90,12 +90,26 @@ static void lan969x_fdma_free_pages(struct sparx5_rx *rx)
 	struct fdma *fdma = &rx->fdma;
 
 	for (int i = 0; i < fdma->n_dcbs; ++i) {
-		for (int j = 0; j < fdma->n_dbs; ++j)

drivers/net/ethernet/microchip/sparx5/lan969x/lan969x_fdma.c static void lan969x_fdma_free_pages(struct sparx5_rx *rx) · line 102

Quoted 10 lines
drivers/net/ethernet/microchip/sparx5/lan969x/lan969x_fdma.c +6 -0
+		for (int j = 0; j < fdma->n_dbs; ++j) {
+			if (!rx->page[i][j])
+				continue;
+
 			page_pool_put_full_page(rx->page_pool,
 						rx->page[i][j], false);
+			rx->page[i][j] = NULL;
+		}
 	}
 }

[Severity: Medium]
Can this still put a page the driver no longer owns?

At this commit, lan969x_fdma_rx_get_frame() reads the slot but never clears it:

lan969x_fdma_rx_get_frame() {
    ...
	page = rx->page[fdma->dcb_index][fdma->db_index];

	sparx5_ifh_parse(sparx5, page_address(page), &fi);
    ...
}

The page then goes to the stack through build_skb() and skb_mark_for_recycle(), or back to the pool through page_pool_recycle_direct().

The slot is only overwritten when lan969x_fdma_rx_dataptr_cb() succeeds:

	page = page_pool_dev_alloc_pages(rx->page_pool);
	if (unlikely(!page))
		return -ENOMEM;

	rx->page[dcb][db] = page;

lan969x_fdma_napi_poll() ignores the fdma_dcb_add() return value during refill. So if that allocation fails, the slot keeps pointing at the consumed page.

On teardown, lan969x_fdma_deinit()->lan969x_fdma_rx_free()-> lan969x_fdma_free_pages() sees a non-NULL slot, and the new NULL check doesn't skip it. Wouldn't page_pool_put_full_page() then put that page a second time?

The Sparx5 half of this patch clears rx->skb[][] in sparx5_fdma_rx_get_frame(), but lan969x gets no matching change. The NULL check therefore can't tell owned slots from stale ones.

This looks to be fixed later in the series by "net: sparx5/lan969x: add support for resizing the FDMA buffers". That patch adds rx->page[fdma->dcb_index][fdma->db_index] = NULL in lan969x_fdma_rx_get_frame(). Could that change move into this patch so the commit works on its own?

[ ... ]

Quoted 12 lines
drivers/net/ethernet/microchip/sparx5/lan969x/lan969x_fdma.c +3 -3
@@ -172,14 +186,18 @@ static int lan969x_fdma_rx_alloc(struct sparx5 *sparx5)
 		return PTR_ERR(rx->page_pool);
 
 	err = fdma_alloc_coherent(sparx5->dev, fdma);
-	if (err)
+	if (err) {
+		page_pool_destroy(rx->page_pool);
 		return err;
+	}
 
-	fdma_dcbs_init(fdma,
-		       FDMA_DCB_INFO_DATAL(fdma->db_size),

drivers/net/ethernet/microchip/sparx5/lan969x/lan969x_fdma.c static int lan969x_fdma_rx_alloc(struct sparx5 *sparx5) · line 201

Quoted 10 lines
drivers/net/ethernet/microchip/sparx5/lan969x/lan969x_fdma.c +6 -2
-		       FDMA_DCB_STATUS_INTR);
+	err = fdma_dcbs_init(fdma,
+			     FDMA_DCB_INFO_DATAL(fdma->db_size),
+			     FDMA_DCB_STATUS_INTR);
+	if (err)
+		lan969x_fdma_rx_free(sparx5);
 
-	return 0;
+	return err;
 }

[Severity: High]
This isn't a bug introduced by this patch, but the same RX buffer allocation failure is now handled at init and is still ignored at runtime. Can RX stall for good when the NAPI refill fails?

Both pollers ignore the fdma_dcb_add() return value when they re-arm consumed RX DCBs:

lan969x_fdma_napi_poll() {
    ...
		fdma_dcb_add(fdma,
			     old_dcb,
			     FDMA_DCB_INFO_DATAL(fdma->db_size),
			     FDMA_DCB_STATUS_INTR);
    ...
}
sparx5_fdma_napi_callback() {
    ...
		fdma_dcb_add(fdma, fdma->dcb_index,
			     FDMA_DCB_INFO_DATAL(fdma->db_size),
			     FDMA_DCB_STATUS_INTR);
    ...
}

In fdma_api.c, __fdma_db_add() writes the status, with DONE cleared, before it calls the dataptr callback:

	db->status = cpu_to_le64(status);

	err = cb(fdma, dcb_idx, db_idx, &dataptr);
	if (unlikely(err))
		return err;

The callback can fail in two places:

- __netdev_alloc_skb(..., GFP_ATOMIC) in sparx5_fdma_rx_dataptr_cb()
- page_pool_dev_alloc_pages() in lan969x_fdma_rx_dataptr_cb()

When it fails, __fdma_dcb_add() returns early. dcb->nextptr/info are not written, the previous DCB is not linked to it, and fdma->last_dcb stays the same. The next successful refill links last_dcb to the following DCB, so the hardware chain skips the failed one.

The software index fdma->dcb_index still walks the ring in order. Once it wraps to the failed DCB, db 0 has DONE clear and the hardware never fills it. After that, fdma_has_frames() (lan969x) and fdma_db_is_done() in sparx5_fdma_rx_get_frame() return false on every poll.

Wouldn't FDMA extraction to the CPU then stop until the driver is reloaded? This is still the case at the end of the series.

Generated by Claude Code Patch Reviewer

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261120-sparx5-mtu-change-v3-0-55c272fe1acc%40microchip.com
author maintainer
#6 lore raw

Re: [PATCH net-next v3 2/9] net: sparx5/lan969x: free the FDMA buffers on init errors and deinit

Quoted 139 lines

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

Critical: 0 · High: 0 (1) · Medium: 1 · 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).

- [Medium] At this commit, lan969x_fdma_rx_get_frame() (drivers/net/ethernet/microchip/sparx5/lan969x/lan969x_fdma.c) reads…
- [Low] This patch fixes several real bugs but has no Fixes: tag: - Sparx5 RX skbs leak on every deinit, and the RX DCBs leak when TX…

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but both NAPI pollers ignore fdma_dcb_add() failures when re-arming consumed RX…

--

net: sparx5/lan969x: free the FDMA buffers on init errors and deinit

This patch adds sparx5_fdma_rx_free(), lan969x_fdma_rx_free() and lan969x_fdma_tx_free(), and calls them on the init error paths and on deinit, so the RX skbs/pages, page_pool, DCBs and in-flight TX buffers are released. It also checks the fdma_dcbs_init() return value for RX. It stores the mapped TX length in struct sparx5_tx_buf instead of reading it from the skb.

Quoted 8 lines
drivers/net/ethernet/microchip/sparx5/lan969x/lan969x_fdma.c +0 -0
index fd532c89f9110..5f96910df5383 100644
@@ -67,13 +67,13 @@ static void lan969x_fdma_tx_clear_buf(struct sparx5 *sparx5, int weight)
 		if (!fdma_db_is_done(fdma_db_get(fdma, i, 0)))
 			continue;
 

drivers/net/ethernet/microchip/sparx5/lan969x/lan969x_fdma.c static void lan969x_fdma_tx_clear_buf(struct sparx5 *sparx5, int weight) · line 77

Quoted 10 lines
drivers/net/ethernet/microchip/sparx5/lan969x/lan969x_fdma.c +2 -2
-		db->dev->stats.tx_bytes += db->skb->len;
+		db->dev->stats.tx_bytes += db->len;
 		db->dev->stats.tx_packets++;
 		sparx5->tx.packets++;
 
 		dma_unmap_single(sparx5->dev,
 				 db->dma_addr,
-				 db->skb->len,
+				 db->len,
 				 DMA_TO_DEVICE);

[Severity: Low]
Should this patch carry Fixes: tags?

Before this change, lan969x_fdma_tx_clear_buf() read db->skb->len for two-step PTP skbs in the normal TX completion path. Those skbs stay on port->tx_skbs, and lan969x_ptp_irq_handler() can free them before NAPI reaps the TX descriptor:

lan969x_ptp_irq_handler() {
    ...
			__skb_unlink(skb, &port->tx_skbs);
			skb_match = skb;
    ...
		dev_kfree_skb_any(skb_match);
    ...
}

The PTP timeout purge can free them the same way. That makes the old read a use-after-free, and dma_unmap_single() could be given the wrong size.

The commit message explains the switch to db->len. It doesn't say that this fixes a use-after-free at runtime, and the subject only mentions init errors and deinit.

The other fixes here also have no Fixes: reference:

- the Sparx5 RX skb leak on deinit
- the lan969x page_pool, TX DCB, tx->dbs and in-flight skb leaks
- the put of never-allocated pages after a partial RX fill

Without the tags, stable trees that ship lan969x FDMA probably won't pick up the use-after-free fix. Would it make sense to split the lan969x and Sparx5 fixes, since they go back to different commits, and tag each one?

Quoted 5 lines
drivers/net/ethernet/microchip/sparx5/lan969x/lan969x_fdma.c +0 -1
@@ -90,12 +90,26 @@ static void lan969x_fdma_free_pages(struct sparx5_rx *rx)
 	struct fdma *fdma = &rx->fdma;
 
 	for (int i = 0; i < fdma->n_dcbs; ++i) {
-		for (int j = 0; j < fdma->n_dbs; ++j)

drivers/net/ethernet/microchip/sparx5/lan969x/lan969x_fdma.c static void lan969x_fdma_free_pages(struct sparx5_rx *rx) · line 102

Quoted 10 lines
drivers/net/ethernet/microchip/sparx5/lan969x/lan969x_fdma.c +6 -0
+		for (int j = 0; j < fdma->n_dbs; ++j) {
+			if (!rx->page[i][j])
+				continue;
+
 			page_pool_put_full_page(rx->page_pool,
 						rx->page[i][j], false);
+			rx->page[i][j] = NULL;
+		}
 	}
 }

[Severity: Medium]
Can this still put a page the driver no longer owns?

At this commit, lan969x_fdma_rx_get_frame() reads the slot but never clears it:

lan969x_fdma_rx_get_frame() {
    ...
	page = rx->page[fdma->dcb_index][fdma->db_index];

	sparx5_ifh_parse(sparx5, page_address(page), &fi);
    ...
}

The page then goes to the stack through build_skb() and skb_mark_for_recycle(), or back to the pool through page_pool_recycle_direct().

The slot is only overwritten when lan969x_fdma_rx_dataptr_cb() succeeds:

	page = page_pool_dev_alloc_pages(rx->page_pool);
	if (unlikely(!page))
		return -ENOMEM;

	rx->page[dcb][db] = page;

lan969x_fdma_napi_poll() ignores the fdma_dcb_add() return value during refill. So if that allocation fails, the slot keeps pointing at the consumed page.

On teardown, lan969x_fdma_deinit()->lan969x_fdma_rx_free()-> lan969x_fdma_free_pages() sees a non-NULL slot, and the new NULL check doesn't skip it. Wouldn't page_pool_put_full_page() then put that page a second time?

The Sparx5 half of this patch clears rx->skb[][] in sparx5_fdma_rx_get_frame(), but lan969x gets no matching change. The NULL check therefore can't tell owned slots from stale ones.

Quoted 5 lines

This looks to be fixed later in the series by "net: sparx5/lan969x: add support for resizing the FDMA buffers". That patch adds rx->page[fdma->dcb_index][fdma->db_index] = NULL in lan969x_fdma_rx_get_frame(). Could that change move into this patch so the commit works on its own?

This needs a failed RX refill first, which is the pre-existing refill issue flagged on this patch, where the lan969x RX ring already stalls. It is also only reachable on deinit. Also, the issue go away with patch 6.

Quoted 82 lines

[ ... ]

Quoted 12 lines
drivers/net/ethernet/microchip/sparx5/lan969x/lan969x_fdma.c +3 -3
@@ -172,14 +186,18 @@ static int lan969x_fdma_rx_alloc(struct sparx5 *sparx5)
 		return PTR_ERR(rx->page_pool);
 
 	err = fdma_alloc_coherent(sparx5->dev, fdma);
-	if (err)
+	if (err) {
+		page_pool_destroy(rx->page_pool);
 		return err;
+	}
 
-	fdma_dcbs_init(fdma,
-		       FDMA_DCB_INFO_DATAL(fdma->db_size),

drivers/net/ethernet/microchip/sparx5/lan969x/lan969x_fdma.c static int lan969x_fdma_rx_alloc(struct sparx5 *sparx5) · line 201

Quoted 10 lines
drivers/net/ethernet/microchip/sparx5/lan969x/lan969x_fdma.c +6 -2
-		       FDMA_DCB_STATUS_INTR);
+	err = fdma_dcbs_init(fdma,
+			     FDMA_DCB_INFO_DATAL(fdma->db_size),
+			     FDMA_DCB_STATUS_INTR);
+	if (err)
+		lan969x_fdma_rx_free(sparx5);
 
-	return 0;
+	return err;
 }

[Severity: High]
This isn't a bug introduced by this patch, but the same RX buffer allocation failure is now handled at init and is still ignored at runtime. Can RX stall for good when the NAPI refill fails?

Both pollers ignore the fdma_dcb_add() return value when they re-arm consumed RX DCBs:

lan969x_fdma_napi_poll() {
    ...
		fdma_dcb_add(fdma,
			     old_dcb,
			     FDMA_DCB_INFO_DATAL(fdma->db_size),
			     FDMA_DCB_STATUS_INTR);
    ...
}
sparx5_fdma_napi_callback() {
    ...
		fdma_dcb_add(fdma, fdma->dcb_index,
			     FDMA_DCB_INFO_DATAL(fdma->db_size),
			     FDMA_DCB_STATUS_INTR);
    ...
}

In fdma_api.c, __fdma_db_add() writes the status, with DONE cleared, before it calls the dataptr callback:

	db->status = cpu_to_le64(status);

	err = cb(fdma, dcb_idx, db_idx, &dataptr);
	if (unlikely(err))
		return err;

The callback can fail in two places:

- __netdev_alloc_skb(..., GFP_ATOMIC) in sparx5_fdma_rx_dataptr_cb()
- page_pool_dev_alloc_pages() in lan969x_fdma_rx_dataptr_cb()

When it fails, __fdma_dcb_add() returns early. dcb->nextptr/info are not written, the previous DCB is not linked to it, and fdma->last_dcb stays the same. The next successful refill links last_dcb to the following DCB, so the hardware chain skips the failed one.

The software index fdma->dcb_index still walks the ring in order. Once it wraps to the failed DCB, db 0 has DONE clear and the hardware never fills it. After that, fdma_has_frames() (lan969x) and fdma_db_is_done() in sparx5_fdma_rx_get_frame() return false on every poll.

Wouldn't FDMA extraction to the CPU then stop until the driver is reloaded? This is still the case at the end of the series.

Generated by Claude Code Patch Reviewer

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261120-sparx5-mtu-change-v3-0-55c272fe1acc%40microchip.com
author maintainer
#7 lore raw

PATCH v3 3/9 net: sparx5/lan969x: add a helper for setting the MAC max frame length

In reply to Daniel Machon

The maximum frame length accepted by a port is set in the MAC_MAXLEN_CFG register of its MAC. All ports have a 2G5 device, the 5G, 10G and 25G ports also have a high speed device, and the lan969x RGMII ports have an RGMII device instead. The register is currently written open-coded in sparx5_port_init(), with a fixed length of 1518 bytes.

Add sparx5_port_max_len_set(), which sets the maximum frame length in the devices of a port, and use it in sparx5_port_init(). To support the RGMII devices, add the DEVRGMII_MAC_MAXLEN_CFG register, and make lan969x_port_dev_mapping() return the RGMII device index for the RGMII ports.

The helper is used to change the maximum frame length with the MTU, later in this series.

No functional change intended.

---
 .../ethernet/microchip/sparx5/lan969x/lan969x.c    |  9 ++++
 .../ethernet/microchip/sparx5/sparx5_main_regs.h   | 11 +++++
 .../net/ethernet/microchip/sparx5/sparx5_port.c    | 50 ++++++++++++++++------
 .../net/ethernet/microchip/sparx5/sparx5_port.h    |  4 ++
 4 files changed, 61 insertions(+), 13 deletions(-)
drivers/net/ethernet/microchip/sparx5/lan969x/lan969x.c +9 -0
index df211e608056a..fd1b55eae9c3a 100644
@@ -181,6 +181,15 @@ static u32 lan969x_port_dev_mapping(struct sparx5 *sparx5, int port)
 		}
 	}
 
+	if (lan969x_port_is_rgmii(port)) {
+		switch (port) {
+		case 28:
+			return 0;
+		case 29:
+			return 1;
+		}
+	}
+
 	/* 2g5 port */
 	return port;
 }
drivers/net/ethernet/microchip/sparx5/sparx5_main_regs.h +11 -0
index 73bc26bf4376e..b2314ce8baf50 100644
@@ -8617,6 +8617,17 @@ extern const struct sparx5_regs *regs;
 #define DEVRGMII_MAC_ENA_CFG_TX_ENA_GET(x)\
 	FIELD_GET(DEVRGMII_MAC_ENA_CFG_TX_ENA, x)
 
+/* LAN969X ONLY */
+/* DEV1G:MAC_CFG_STATUS:MAC_MAXLEN_CFG */
+#define DEVRGMII_MAC_MAXLEN_CFG(t)                                             \
+	__REG(TARGET_DEVRGMII, t, 2, 36, 0, 1, 36, 8, 0, 1, 4)
+
+#define DEVRGMII_MAC_MAXLEN_CFG_MAX_LEN          GENMASK(15, 0)
+#define DEVRGMII_MAC_MAXLEN_CFG_MAX_LEN_SET(x)\
+	FIELD_PREP(DEVRGMII_MAC_MAXLEN_CFG_MAX_LEN, x)
+#define DEVRGMII_MAC_MAXLEN_CFG_MAX_LEN_GET(x)\
+	FIELD_GET(DEVRGMII_MAC_MAXLEN_CFG_MAX_LEN, x)
+
 /* LAN969X ONLY */
 /* DEV1G:MAC_CFG_STATUS:MAC_TAGS_CFG */
 #define DEVRGMII_MAC_TAGS_CFG(t)                                               \
drivers/net/ethernet/microchip/sparx5/sparx5_port.c +37 -13

83 lines not on this page · raw message

drivers/net/ethernet/microchip/sparx5/sparx5_port.h +4 -0
index c8a37468a3d1d..912f97a506768 100644
@@ -88,6 +88,10 @@ int sparx5_port_init(struct sparx5 *sparx5,
 		     struct sparx5_port *spx5_port,
 		     struct sparx5_port_config *conf);
 
+void sparx5_port_max_len_set(struct sparx5 *sparx5,
+			     struct sparx5_port *port,
+			     u32 len);
+
 int sparx5_port_config(struct sparx5 *sparx5,
 		       struct sparx5_port *spx5_port,
 		       struct sparx5_port_config *conf);
Signature · 1 line
2.34.1
author maintainer
#8 lore raw

PATCH v3 4/9 net: sparx5/lan969x: pass the FDMA rings to the init and alloc functions

In reply to Daniel Machon

The FDMA RX and TX rings are initialized and allocated by functions that take struct sparx5, and operate on the rings in it. The RX buffer callbacks also find the RX ring through struct sparx5, and the DB size is computed in the RX and TX init functions.

In preparation for MTU support, where a new set of rings must be set up while the FDMA keeps running on the current ones, make these functions operate on the ring passed to them, and pass the DB size to the Sparx5 init functions. Find the RX ring in the RX buffer callbacks from the FDMA that is passed to them. On lan969x, TX frames are mapped directly from the skbs, so only the RX ring has to be replaced. Therefore, only its RX functions take a ring, and its RX init function keeps computing the DB size.

No functional change intended.

---
 .../microchip/sparx5/lan969x/lan969x_fdma.c        | 28 ++++++++++----------
 .../net/ethernet/microchip/sparx5/sparx5_fdma.c    | 30 ++++++++++------------
 2 files changed, 28 insertions(+), 30 deletions(-)
drivers/net/ethernet/microchip/sparx5/lan969x/lan969x_fdma.c +14 -14
index 5f96910df5383..3036d6b6f043c 100644
@@ -25,7 +25,7 @@ static int lan969x_fdma_tx_dataptr_cb(struct fdma *fdma, int dcb, int db,
 static int lan969x_fdma_rx_dataptr_cb(struct fdma *fdma, int dcb, int db,
 				      u64 *dataptr)
 {
-	struct sparx5_rx *rx = &FDMA_PRIV(fdma)->rx;
+	struct sparx5_rx *rx = container_of(fdma, struct sparx5_rx, fdma);
 	struct page *page;
 
 	page = page_pool_dev_alloc_pages(rx->page_pool);
@@ -101,10 +101,8 @@ static void lan969x_fdma_free_pages(struct sparx5_rx *rx)
 	}
 }
 
-static void lan969x_fdma_rx_free(struct sparx5 *sparx5)
+static void lan969x_fdma_rx_free(struct sparx5 *sparx5, struct sparx5_rx *rx)
 {
-	struct sparx5_rx *rx = &sparx5->rx;
-
 	lan969x_fdma_free_pages(rx);
 	fdma_free_coherent(sparx5->dev, &rx->fdma);
 	page_pool_destroy(rx->page_pool);
@@ -163,9 +161,8 @@ static struct sk_buff *lan969x_fdma_rx_get_frame(struct sparx5 *sparx5,
 	return NULL;
 }
 
-static int lan969x_fdma_rx_alloc(struct sparx5 *sparx5)
+static int lan969x_fdma_rx_alloc(struct sparx5 *sparx5, struct sparx5_rx *rx)
 {
-	struct sparx5_rx *rx = &sparx5->rx;
 	struct fdma *fdma = &rx->fdma;
 	int err;
 
@@ -195,7 +192,7 @@ static int lan969x_fdma_rx_alloc(struct sparx5 *sparx5)
 			     FDMA_DCB_INFO_DATAL(fdma->db_size),
 			     FDMA_DCB_STATUS_INTR);
 	if (err)
-		lan969x_fdma_rx_free(sparx5);
+		lan969x_fdma_rx_free(sparx5, rx);
 
 	return err;
 }
@@ -261,9 +258,9 @@ static void lan969x_fdma_tx_free(struct sparx5 *sparx5)
 	kfree(tx->dbs);
 }
 
-static void lan969x_fdma_rx_init(struct sparx5 *sparx5)
+static void lan969x_fdma_rx_init(struct sparx5 *sparx5, struct sparx5_rx *rx)
 {
-	struct fdma *fdma = &sparx5->rx.fdma;
+	struct fdma *fdma = &rx->fdma;
 
 	fdma->channel_id = FDMA_XTR_CHANNEL;
 	fdma->n_dcbs = FDMA_DCB_MAX;
@@ -279,7 +276,7 @@ static void lan969x_fdma_rx_init(struct sparx5 *sparx5)
 		struct sparx5_port *port = sparx5->ports[idx];
 
 		if (port && port->ndev) {
-			sparx5->rx.ndev = port->ndev;
+			rx->ndev = port->ndev;
 			break;
 		}
 	}
@@ -411,9 +408,10 @@ int lan969x_fdma_xmit(struct sparx5 *sparx5, u32 *ifh, struct sk_buff *skb,
 
 int lan969x_fdma_init(struct sparx5 *sparx5)
 {
+	struct sparx5_rx *rx = &sparx5->rx;
 	int err;
 
-	lan969x_fdma_rx_init(sparx5);
+	lan969x_fdma_rx_init(sparx5, rx);
 	lan969x_fdma_tx_init(sparx5);
 	sparx5_fdma_injection_mode(sparx5);
 
@@ -423,7 +421,7 @@ int lan969x_fdma_init(struct sparx5 *sparx5)
 		return err;
 	}
 
-	err = lan969x_fdma_rx_alloc(sparx5);
+	err = lan969x_fdma_rx_alloc(sparx5, rx);
 	if (err) {
 		dev_err(sparx5->dev, "Failed to allocate RX buffers: %d\n",
 			err);
@@ -432,7 +430,7 @@ int lan969x_fdma_init(struct sparx5 *sparx5)
 
 	err = lan969x_fdma_tx_alloc(sparx5);
 	if (err) {
-		lan969x_fdma_rx_free(sparx5);
+		lan969x_fdma_rx_free(sparx5, rx);
 		dev_err(sparx5->dev, "Failed to allocate TX buffers: %d\n",
 			err);
 		return err;
@@ -447,9 +445,11 @@ int lan969x_fdma_init(struct sparx5 *sparx5)
 
 int lan969x_fdma_deinit(struct sparx5 *sparx5)
 {
+	struct sparx5_rx *rx = &sparx5->rx;
+
 	sparx5_fdma_stop(sparx5);
 	lan969x_fdma_tx_free(sparx5);
-	lan969x_fdma_rx_free(sparx5);
+	lan969x_fdma_rx_free(sparx5, rx);
 
 	return 0;
 }
drivers/net/ethernet/microchip/sparx5/sparx5_fdma.c +14 -16
index f1d44e5e080b6..537c945409bba 100644
@@ -35,8 +35,7 @@ static int sparx5_fdma_tx_dataptr_cb(struct fdma *fdma, int dcb, int db,
 static int sparx5_fdma_rx_dataptr_cb(struct fdma *fdma, int dcb, int db,
 				     u64 *dataptr)
 {
-	struct sparx5 *sparx5 = fdma->priv;
-	struct sparx5_rx *rx = &sparx5->rx;
+	struct sparx5_rx *rx = container_of(fdma, struct sparx5_rx, fdma);
 	struct sk_buff *skb;
 
 	skb = __netdev_alloc_skb(rx->ndev, fdma->db_size, GFP_ATOMIC);
@@ -264,9 +263,8 @@ static void sparx5_fdma_rx_free(struct sparx5_rx *rx)
 	fdma_free_phys(fdma);
 }
 
-static int sparx5_fdma_rx_alloc(struct sparx5 *sparx5)
+static int sparx5_fdma_rx_alloc(struct sparx5_rx *rx)
 {
-	struct sparx5_rx *rx = &sparx5->rx;
 	struct fdma *fdma = &rx->fdma;
 	int err;
 
@@ -282,9 +280,8 @@ static int sparx5_fdma_rx_alloc(struct sparx5 *sparx5)
 	return err;
 }
 
-static int sparx5_fdma_tx_alloc(struct sparx5 *sparx5)
+static int sparx5_fdma_tx_alloc(struct sparx5_tx *tx)
 {
-	struct sparx5_tx *tx = &sparx5->tx;
 	struct fdma *fdma = &tx->fdma;
 	int err;
 
@@ -299,7 +296,7 @@ static int sparx5_fdma_tx_alloc(struct sparx5 *sparx5)
 }
 
 static void sparx5_fdma_rx_init(struct sparx5 *sparx5,
-				struct sparx5_rx *rx, int channel)
+				struct sparx5_rx *rx, int channel, u32 db_size)
 {
 	struct fdma *fdma = &rx->fdma;
 	int idx;
@@ -308,8 +305,8 @@ static void sparx5_fdma_rx_init(struct sparx5 *sparx5,
 	fdma->n_dcbs = FDMA_DCB_MAX;
 	fdma->n_dbs = FDMA_RX_DCB_MAX_DBS;
 	fdma->priv = sparx5;
-	fdma->db_size = ALIGN(FDMA_XTR_BUFFER_SIZE, PAGE_SIZE);
-	fdma->size = fdma_get_size(&sparx5->rx.fdma);
+	fdma->db_size = db_size;
+	fdma->size = fdma_get_size(fdma);
 	fdma->ops.dataptr_cb = &sparx5_fdma_rx_dataptr_cb;
 	fdma->ops.nextptr_cb = &fdma_nextptr_cb;
 	/* Fetch a netdev for SKB and NAPI use, any will do */
@@ -324,7 +321,7 @@ static void sparx5_fdma_rx_init(struct sparx5 *sparx5,
 }
 
 static void sparx5_fdma_tx_init(struct sparx5 *sparx5,
-				struct sparx5_tx *tx, int channel)
+				struct sparx5_tx *tx, int channel, u32 db_size)
 {
 	struct fdma *fdma = &tx->fdma;
 
@@ -332,8 +329,8 @@ static void sparx5_fdma_tx_init(struct sparx5 *sparx5,
 	fdma->n_dcbs = FDMA_DCB_MAX;
 	fdma->n_dbs = FDMA_TX_DCB_MAX_DBS;
 	fdma->priv = sparx5;
-	fdma->db_size = ALIGN(FDMA_XTR_BUFFER_SIZE, PAGE_SIZE);
-	fdma->size = fdma_get_size_contiguous(&sparx5->tx.fdma);
+	fdma->db_size = db_size;
+	fdma->size = fdma_get_size_contiguous(fdma);
 	fdma->ops.dataptr_cb = &sparx5_fdma_tx_dataptr_cb;
 	fdma->ops.nextptr_cb = &fdma_nextptr_cb;
 }
@@ -427,6 +424,7 @@ void sparx5_fdma_injection_mode(struct sparx5 *sparx5)
 
 int sparx5_fdma_init(struct sparx5 *sparx5)
 {
+	u32 db_size = ALIGN(FDMA_XTR_BUFFER_SIZE, PAGE_SIZE);
 	int err;
 
 	/* Reset FDMA state */
@@ -443,14 +441,14 @@ int sparx5_fdma_init(struct sparx5 *sparx5)
 		 sparx5, CPU_PROC_CTRL);
 
 	sparx5_fdma_injection_mode(sparx5);
-	sparx5_fdma_rx_init(sparx5, &sparx5->rx, FDMA_XTR_CHANNEL);
-	sparx5_fdma_tx_init(sparx5, &sparx5->tx, FDMA_INJ_CHANNEL);
-	err = sparx5_fdma_rx_alloc(sparx5);
+	sparx5_fdma_rx_init(sparx5, &sparx5->rx, FDMA_XTR_CHANNEL, db_size);
+	sparx5_fdma_tx_init(sparx5, &sparx5->tx, FDMA_INJ_CHANNEL, db_size);
+	err = sparx5_fdma_rx_alloc(&sparx5->rx);
 	if (err) {
 		dev_err(sparx5->dev, "Could not allocate RX buffers: %d\n", err);
 		return err;
 	}
-	err = sparx5_fdma_tx_alloc(sparx5);
+	err = sparx5_fdma_tx_alloc(&sparx5->tx);
 	if (err) {
 		sparx5_fdma_rx_free(&sparx5->rx);
 		dev_err(sparx5->dev, "Could not allocate TX buffers: %d\n", err);
Signature · 1 line
2.34.1
author maintainer
#9 lore raw

PATCH v3 5/9 net: sparx5/lan969x: size the FDMA buffers from the port MTUs

In reply to Daniel Machon

The FDMA buffers must hold the largest frame that can be received by the CPU, including the IFH, and on Sparx5 also the largest frame that is transmitted, as Sparx5 copies the frames into its TX buffers. On Sparx5, the RX buffers are skbs, and the TX buffers are a contiguous area. On lan969x, the RX buffers are page_pool pages, which are turned into skbs with build_skb(), and TX frames are mapped directly from the skbs. The buffers are currently fixed to a single page, which is not large enough to hold jumbo frames.

Instead, size the buffers from the largest MTU of the ports, plus the Ethernet header, FCS, IFH and two VLAN tags. On Sparx5, use this length, rounded up to the 128 byte granularity required by the FDMA, as the RX and TX buffer size. As the TX buffers can now be smaller than a page, make sparx5_fdma_xmit() drop frames that do not fit. On lan969x, use the page order needed to hold the frame and the skb_shared_info, as lan966x does, and limit the RX buffer size so that the FDMA never writes into the skb_shared_info at the end of the page.

With the default MTU of 1500, the Sparx5 buffers become smaller, and the lan969x pages are unchanged.

---
 .../microchip/sparx5/lan969x/lan969x_fdma.c        | 21 +++++++++----
 .../net/ethernet/microchip/sparx5/sparx5_fdma.c    | 35 ++++++++++++++++++++--
 .../net/ethernet/microchip/sparx5/sparx5_main.h    |  2 ++
 3 files changed, 51 insertions(+), 7 deletions(-)
drivers/net/ethernet/microchip/sparx5/lan969x/lan969x_fdma.c +16 -5
index 3036d6b6f043c..1f7856d067b94 100644
@@ -131,7 +131,7 @@ static struct sk_buff *lan969x_fdma_rx_get_frame(struct sparx5 *sparx5,
 		goto free_page;
 	}
 
-	skb = build_skb(page_address(page), fdma->db_size);
+	skb = build_skb(page_address(page), PAGE_SIZE << rx->page_order);
 	if (unlikely(!skb))
 		goto free_page;
 
@@ -161,21 +161,27 @@ static struct sk_buff *lan969x_fdma_rx_get_frame(struct sparx5 *sparx5,
 	return NULL;
 }
 
+/* Get the page order needed for a page to hold the largest frame */
+static u8 lan969x_fdma_get_page_order(struct sparx5 *sparx5)
+{
+	return get_order(ALIGN(sparx5_fdma_get_max_frame(sparx5), 128) +
+			 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
+}
+
 static int lan969x_fdma_rx_alloc(struct sparx5 *sparx5, struct sparx5_rx *rx)
 {
 	struct fdma *fdma = &rx->fdma;
 	int err;
 
 	struct page_pool_params pp_params = {
-		.order = 0,
+		.order = rx->page_order,
 		.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV,
 		.pool_size = fdma->n_dcbs * fdma->n_dbs,
 		.nid = NUMA_NO_NODE,
 		.dev = sparx5->dev,
 		.dma_dir = DMA_FROM_DEVICE,
 		.offset = 0,
-		.max_len = fdma->db_size -
-			   SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
+		.max_len = fdma->db_size,
 	};
 
 	rx->page_pool = page_pool_create(&pp_params);
@@ -267,7 +273,11 @@ static void lan969x_fdma_rx_init(struct sparx5 *sparx5, struct sparx5_rx *rx)
 	fdma->n_dbs = 1;
 	fdma->priv = sparx5;
 	fdma->size = fdma_get_size(fdma);
-	fdma->db_size = PAGE_SIZE;
+	/* Keep the FDMA from writing into the skb_shared_info at the end of
+	 * the page. The DB size must be a multiple of 128 bytes.
+	 */
+	fdma->db_size = SKB_WITH_OVERHEAD(PAGE_SIZE << rx->page_order);
+	fdma->db_size = ALIGN_DOWN(fdma->db_size, 128);
 	fdma->ops.dataptr_cb = &lan969x_fdma_rx_dataptr_cb;
 	fdma->ops.nextptr_cb = &fdma_nextptr_cb;
 
@@ -411,6 +421,7 @@ int lan969x_fdma_init(struct sparx5 *sparx5)
 	struct sparx5_rx *rx = &sparx5->rx;
 	int err;
 
+	rx->page_order = lan969x_fdma_get_page_order(sparx5);
 	lan969x_fdma_rx_init(sparx5, rx);
 	lan969x_fdma_tx_init(sparx5);
 	sparx5_fdma_injection_mode(sparx5);
drivers/net/ethernet/microchip/sparx5/sparx5_fdma.c +33 -2
index 537c945409bba..b92d249777f0b 100644
@@ -18,7 +18,6 @@
 #include "sparx5_main.h"
 #include "sparx5_port.h"
 
-#define FDMA_XTR_BUFFER_SIZE		2048
 #define FDMA_WEIGHT			4
 #define SPX5_FDMA_STOP_SLEEP_US		500
 #define SPX5_FDMA_STOP_TIMEOUT_US	10000
@@ -224,6 +223,10 @@ int sparx5_fdma_xmit(struct sparx5 *sparx5, u32 *ifh, struct sk_buff *skb,
 	struct fdma *fdma = &tx->fdma;
 	void *virt_addr;
 
+	/* Drop frames that do not fit in the TX buffers */
+	if (unlikely(IFH_LEN * 4 + skb->len + ETH_FCS_LEN > fdma->db_size))
+		return -EMSGSIZE;
+
 	if (!fdma_db_is_done(fdma_db_get(fdma, fdma->dcb_index, 0)))
 		return -EINVAL;
 
@@ -295,6 +298,33 @@ static int sparx5_fdma_tx_alloc(struct sparx5_tx *tx)
 	return 0;
 }
 
+/* Get the size of the largest frame, including IFH and VLAN tags, that can be
+ * received on any of the ports.
+ */
+u32 sparx5_fdma_get_max_frame(struct sparx5 *sparx5)
+{
+	u32 max_mtu = 0;
+
+	for (int i = 0; i < sparx5->data->consts->n_ports; i++) {
+		struct sparx5_port *port = sparx5->ports[i];
+
+		if (!port)
+			continue;
+
+		max_mtu = max(max_mtu, READ_ONCE(port->ndev->mtu));
+	}
+
+	return SPX5_HW_MTU(max_mtu) + IFH_LEN * sizeof(u32) + VLAN_HLEN * 2;
+}
+
+/* Get the DB size needed to hold the largest frame. The DB size must be a
+ * multiple of 128 bytes.
+ */
+static u32 sparx5_fdma_get_db_size(struct sparx5 *sparx5)
+{
+	return ALIGN(sparx5_fdma_get_max_frame(sparx5), 128);
+}
+
 static void sparx5_fdma_rx_init(struct sparx5 *sparx5,
 				struct sparx5_rx *rx, int channel, u32 db_size)
 {
@@ -424,7 +454,7 @@ void sparx5_fdma_injection_mode(struct sparx5 *sparx5)
 
 int sparx5_fdma_init(struct sparx5 *sparx5)
 {
-	u32 db_size = ALIGN(FDMA_XTR_BUFFER_SIZE, PAGE_SIZE);
+	u32 db_size;
 	int err;
 
 	/* Reset FDMA state */
@@ -441,6 +471,7 @@ int sparx5_fdma_init(struct sparx5 *sparx5)
 		 sparx5, CPU_PROC_CTRL);
 
 	sparx5_fdma_injection_mode(sparx5);
+	db_size = sparx5_fdma_get_db_size(sparx5);
 	sparx5_fdma_rx_init(sparx5, &sparx5->rx, FDMA_XTR_CHANNEL, db_size);
 	sparx5_fdma_tx_init(sparx5, &sparx5->tx, FDMA_INJ_CHANNEL, db_size);
 	err = sparx5_fdma_rx_alloc(&sparx5->rx);
drivers/net/ethernet/microchip/sparx5/sparx5_main.h +2 -0
index 19b38922823ec..af2a81cdfeb66 100644
@@ -110,6 +110,7 @@ enum sparx5_feature {
 #define SPX5_BUFFER_CELL_SZ    184   /* Cell size  */
 #define SPX5_BUFFER_MEMORY     4194280 /* 22795 words * 184 bytes */
 #define SPX5_TAS_ENTRIES_PER_PORT 2 /* TAS lists per port */
+#define SPX5_HW_MTU(mtu)       ((mtu) + ETH_HLEN + ETH_FCS_LEN)
 
 #define XTR_QUEUE     0
 #define INJ_QUEUE     0
@@ -515,6 +516,7 @@ int sparx5_fdma_xmit(struct sparx5 *sparx5, u32 *ifh, struct sk_buff *skb,
 irqreturn_t sparx5_fdma_handler(int irq, void *args);
 void sparx5_fdma_reload(struct sparx5 *sparx5, struct fdma *fdma);
 void sparx5_fdma_injection_mode(struct sparx5 *sparx5);
+u32 sparx5_fdma_get_max_frame(struct sparx5 *sparx5);
 
 /* sparx5_mactable.c */
 int sparx5_mact_learn(struct sparx5 *sparx5, int port,
Signature · 1 line
2.34.1
#10 lore raw

Re: [PATCH net-next v3 5/9] net: sparx5/lan969x: size the FDMA buffers from the port MTUs

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 (1) · High: 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] net: sparx5: UAF of two-step PTP skb on TX drop in sparx5_port_xmit_impl()

Pre-existing issues:
- [Critical] Use-after-free and double free of two-step PTP skbs on the Sparx5 TX drop path.

--

net: sparx5/lan969x: size the FDMA buffers from the port MTUs

This commit sizes the Sparx5 and lan969x FDMA buffers from the largest port MTU plus the Ethernet header, FCS, IFH and two VLAN tags, rather than a fixed single page. On Sparx5 the RX and TX DB size is that length rounded up to 128 bytes, and sparx5_fdma_xmit() now drops frames that do not fit. On lan969x the page_pool page order is derived from the max frame size.

Quoted 4 lines
drivers/net/ethernet/microchip/sparx5/sparx5_fdma.c +0 -0
index 537c945409bba..b92d249777f0b 100644

[ ... ]

drivers/net/ethernet/microchip/sparx5/sparx5_fdma.c int sparx5_fdma_xmit(struct sparx5 *sparx5, u32 *ifh, struct sk_buff *skb, · line 229

Quoted 8 lines
drivers/net/ethernet/microchip/sparx5/sparx5_fdma.c +4 -0
@@ -224,6 +223,10 @@ int sparx5_fdma_xmit(struct sparx5 *sparx5, u32 *ifh, struct sk_buff *skb,
 	struct fdma *fdma = &tx->fdma;
 	void *virt_addr;
 
+	/* Drop frames that do not fit in the TX buffers */
+	if (unlikely(IFH_LEN * 4 + skb->len + ETH_FCS_LEN > fdma->db_size))
+		return -EMSGSIZE;
+

[Severity: High]
This is a pre-existing problem in sparx5_port_xmit_impl(), but the new -EMSGSIZE return makes it much easier to hit. What happens to a two-step PTP skb that fails here?

sparx5_port_xmit_impl() calls sparx5_ptp_txtstamp_request() before ops->fdma_xmit(). For two-step PTP, that puts the skb on port->tx_skbs with skb_queue_tail() without taking an extra reference. It also increments sparx5->ptp_skbs and port->ts_id.

If fdma_xmit() then returns anything other than -EBUSY, the code jumps to the drop label, which only calls dev_kfree_skb_any(skb). Only the busy label calls sparx5_ptp_txtstamp_release(port, skb). The freed skb is therefore still linked on port->tx_skbs.

Later walks of port->tx_skbs happen in sparx5_ptp_txtstamp_old_release(), in the sparx5 and lan969x PTP IRQ handlers, and in sparx5_ptp_deinit(). These read SPARX5_SKB_CB() from freed memory, call __skb_unlink() through stale list pointers, and call dev_kfree_skb_any() on the same skb again. Isn't that a use-after-free and a double free?

sparx5->ptp_skbs is also never decremented for these skbs. After SPARX5_MAX_PTP_ID such drops, sparx5_ptp_txtstamp_request() returns -EBUSY forever, and two-step TX timestamping on the switch stalls.

Before this patch the Sparx5 TX DB was PAGE_SIZE. The drop path could only be reached through the -EINVAL ring-full return here, or through the -ENOMEM DMA mapping failure in lan969x_fdma_xmit(). With the default MTU of 1500, db_size is now
ALIGN(1518 + 36 + 8, 128) = 1664. Any two-step frame with skb->len above 1624 now takes the drop path every time.

Such frames can still reach a 1500 MTU port, for example through tc mirred or bpf_redirect from a device with a larger MTU. They can also arrive when the MTU is lowered while larger skbs are already queued, which becomes possible later in this series.

The commit message relies on this drop path. Should the drop label in sparx5_port_xmit_impl() release two-step PTP skbs the same way the busy label does? Or would you rather check the length before sparx5_ptp_txtstamp_request() is called?

[Severity: Critical]
This is a pre-existing issue, but this new return gives a second, deterministic way to hit it. What happens to a two-step PTP skb that gets -EMSGSIZE here?

By this point, sparx5_port_xmit_impl()->sparx5_ptp_txtstamp_request() has already put the skb on port->tx_skbs without taking a reference:

sparx5_ptp.c:sparx5_ptp_txtstamp_request() {
    ...
	skb_queue_tail(&port->tx_skbs, skb);
	SPARX5_SKB_CB(skb)->ts_id = port->ts_id;
	SPARX5_SKB_CB(skb)->jiffies = jiffies;

	sparx5->ptp_skbs++;
	port->ts_id++;
    ...
}

Back in sparx5_port_xmit_impl(), only -EBUSY goes through sparx5_ptp_txtstamp_release(). Any other negative return frees the skb while it is still on the list:

sparx5_packet.c:sparx5_port_xmit_impl() {
    ...
	if (ret == -EBUSY)
		goto busy;
	if (ret < 0)
		goto drop;
    ...
drop:
	stats->tx_dropped++;
	sparx5->tx.dropped++;
	dev_kfree_skb_any(skb);
	return NETDEV_TX_OK;
busy:
	if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
	    SPARX5_SKB_CB(skb)->rew_op == IFH_REW_OP_TWO_STEP_PTP)
		sparx5_ptp_txtstamp_release(port, skb);
	return NETDEV_TX_BUSY;
}

The next two-step request then walks the list in
sparx5_ptp_txtstamp_old_release():

	skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) {
		if time_after(SPARX5_SKB_CB(skb)->jiffies + SPARX5_PTP_TIMEOUT,
			      jiffies)
			break;

		__skb_unlink(skb, &port->tx_skbs);
		dev_kfree_skb_any(skb);

Does this read the cb of the freed skb and then free it again? The TX timestamp IRQ handler can also find the stale entry. ptp_skbs and ts_id also stay incremented, so can the PTP ID pool run out?

This path could already be reached through the -EINVAL return when the next TX DB is not done, i.e. when the TX ring is full.

With this patch and the default MTU of 1500, db_size goes from 4096 to ALIGN(1518 + 36 + 8, 128) = 1664. A two-step timestamped frame longer than 1624 bytes used to be sent, but now it always takes the drop path. Such frames can still reach a 1500 MTU port, for example through tc mirred or bpf_redirect from a device with a larger MTU.

The commit message says it will "make sparx5_fdma_xmit() drop frames that do not fit", so it depends on this drop path. Should the drop label in sparx5_port_xmit_impl() call sparx5_ptp_txtstamp_release() for two-step PTP skbs, the same way the busy label does? sparx5_port_xmit_impl() is still unchanged at the end of the series.

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261120-sparx5-mtu-change-v3-0-55c272fe1acc%40microchip.com
author maintainer
#11 lore raw

Re: [PATCH net-next v3 5/9] net: sparx5/lan969x: size the FDMA buffers from the port MTUs

Quoted 82 lines

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

Critical: 0 (1) · High: 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] net: sparx5: UAF of two-step PTP skb on TX drop in sparx5_port_xmit_impl()

Pre-existing issues:
- [Critical] Use-after-free and double free of two-step PTP skbs on the Sparx5 TX drop path.

--

net: sparx5/lan969x: size the FDMA buffers from the port MTUs

This commit sizes the Sparx5 and lan969x FDMA buffers from the largest port MTU plus the Ethernet header, FCS, IFH and two VLAN tags, rather than a fixed single page. On Sparx5 the RX and TX DB size is that length rounded up to 128 bytes, and sparx5_fdma_xmit() now drops frames that do not fit. On lan969x the page_pool page order is derived from the max frame size.

Quoted 4 lines
drivers/net/ethernet/microchip/sparx5/sparx5_fdma.c +0 -0
index 537c945409bba..b92d249777f0b 100644

[ ... ]

drivers/net/ethernet/microchip/sparx5/sparx5_fdma.c int sparx5_fdma_xmit(struct sparx5 *sparx5, u32 *ifh, struct sk_buff *skb, · line 229

Quoted 8 lines
drivers/net/ethernet/microchip/sparx5/sparx5_fdma.c +4 -0
@@ -224,6 +223,10 @@ int sparx5_fdma_xmit(struct sparx5 *sparx5, u32 *ifh, struct sk_buff *skb,
 	struct fdma *fdma = &tx->fdma;
 	void *virt_addr;
 
+	/* Drop frames that do not fit in the TX buffers */
+	if (unlikely(IFH_LEN * 4 + skb->len + ETH_FCS_LEN > fdma->db_size))
+		return -EMSGSIZE;
+

[Severity: High]
This is a pre-existing problem in sparx5_port_xmit_impl(), but the new -EMSGSIZE return makes it much easier to hit. What happens to a two-step PTP skb that fails here?

sparx5_port_xmit_impl() calls sparx5_ptp_txtstamp_request() before ops->fdma_xmit(). For two-step PTP, that puts the skb on port->tx_skbs with skb_queue_tail() without taking an extra reference. It also increments sparx5->ptp_skbs and port->ts_id.

If fdma_xmit() then returns anything other than -EBUSY, the code jumps to the drop label, which only calls dev_kfree_skb_any(skb). Only the busy label calls sparx5_ptp_txtstamp_release(port, skb). The freed skb is therefore still linked on port->tx_skbs.

Later walks of port->tx_skbs happen in sparx5_ptp_txtstamp_old_release(), in the sparx5 and lan969x PTP IRQ handlers, and in sparx5_ptp_deinit(). These read SPARX5_SKB_CB() from freed memory, call __skb_unlink() through stale list pointers, and call dev_kfree_skb_any() on the same skb again. Isn't that a use-after-free and a double free?

sparx5->ptp_skbs is also never decremented for these skbs. After SPARX5_MAX_PTP_ID such drops, sparx5_ptp_txtstamp_request() returns -EBUSY forever, and two-step TX timestamping on the switch stalls.

Before this patch the Sparx5 TX DB was PAGE_SIZE. The drop path could only be reached through the -EINVAL ring-full return here, or through the -ENOMEM DMA mapping failure in lan969x_fdma_xmit(). With the default MTU of 1500, db_size is now
ALIGN(1518 + 36 + 8, 128) = 1664. Any two-step frame with skb->len above 1624 now takes the drop path every time.

Such frames can still reach a 1500 MTU port, for example through tc mirred or bpf_redirect from a device with a larger MTU. They can also arrive when the MTU is lowered while larger skbs are already queued, which becomes possible later in this series.

Quoted 4 lines

The commit message relies on this drop path. Should the drop label in sparx5_port_xmit_impl() release two-step PTP skbs the same way the busy label does? Or would you rather check the length before sparx5_ptp_txtstamp_request() is called?

This is pre-existing. The drop path already misses the PTP release before this series, e.g when the TX ring is full, so the handling of the PTP tx_skbs is broken regardless of this check. I have two WIP patchsets for lan966x and sparx5/lan969x that will rework the PTP tx_skb handling. These will be submitted in the near future.

Quoted 75 lines

[Severity: Critical]
This is a pre-existing issue, but this new return gives a second, deterministic way to hit it. What happens to a two-step PTP skb that gets -EMSGSIZE here?

By this point, sparx5_port_xmit_impl()->sparx5_ptp_txtstamp_request() has already put the skb on port->tx_skbs without taking a reference:

sparx5_ptp.c:sparx5_ptp_txtstamp_request() {
    ...
	skb_queue_tail(&port->tx_skbs, skb);
	SPARX5_SKB_CB(skb)->ts_id = port->ts_id;
	SPARX5_SKB_CB(skb)->jiffies = jiffies;

	sparx5->ptp_skbs++;
	port->ts_id++;
    ...
}

Back in sparx5_port_xmit_impl(), only -EBUSY goes through sparx5_ptp_txtstamp_release(). Any other negative return frees the skb while it is still on the list:

sparx5_packet.c:sparx5_port_xmit_impl() {
    ...
	if (ret == -EBUSY)
		goto busy;
	if (ret < 0)
		goto drop;
    ...
drop:
	stats->tx_dropped++;
	sparx5->tx.dropped++;
	dev_kfree_skb_any(skb);
	return NETDEV_TX_OK;
busy:
	if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
	    SPARX5_SKB_CB(skb)->rew_op == IFH_REW_OP_TWO_STEP_PTP)
		sparx5_ptp_txtstamp_release(port, skb);
	return NETDEV_TX_BUSY;
}

The next two-step request then walks the list in
sparx5_ptp_txtstamp_old_release():

	skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) {
		if time_after(SPARX5_SKB_CB(skb)->jiffies + SPARX5_PTP_TIMEOUT,
			      jiffies)
			break;

		__skb_unlink(skb, &port->tx_skbs);
		dev_kfree_skb_any(skb);

Does this read the cb of the freed skb and then free it again? The TX timestamp IRQ handler can also find the stale entry. ptp_skbs and ts_id also stay incremented, so can the PTP ID pool run out?

This path could already be reached through the -EINVAL return when the next TX DB is not done, i.e. when the TX ring is full.

With this patch and the default MTU of 1500, db_size goes from 4096 to ALIGN(1518 + 36 + 8, 128) = 1664. A two-step timestamped frame longer than 1624 bytes used to be sent, but now it always takes the drop path. Such frames can still reach a 1500 MTU port, for example through tc mirred or bpf_redirect from a device with a larger MTU.

The commit message says it will "make sparx5_fdma_xmit() drop frames that do not fit", so it depends on this drop path. Should the drop label in sparx5_port_xmit_impl() call sparx5_ptp_txtstamp_release() for two-step PTP skbs, the same way the busy label does? sparx5_port_xmit_impl() is still unchanged at the end of the series.

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261120-sparx5-mtu-change-v3-0-55c272fe1acc%40microchip.com
author maintainer
#12 lore raw

PATCH v3 6/9 net: sparx5/lan969x: add support for resizing the FDMA buffers

In reply to Daniel Machon

The FDMA buffers are sized from the largest MTU of the ports when the FDMA is initialized, so they must be resized when an MTU change affects that size. As the FDMA is shared by all ports, it must be stopped while its buffers are replaced.

Add an fdma_resize operation, which allocates new buffers for the new MTU while the FDMA keeps running on the current ones, so that a failed allocation leaves the FDMA untouched. It then stops the port TX queues and the FDMA, replaces the buffers, and restarts the FDMA and the TX queues. On Sparx5 both the RX and TX buffers are replaced. On lan969x, where TX frames are mapped directly from the skbs, only the RX buffers and page_pool are replaced.

The forwarding to the CPU ports is stopped, and the frames already queued for the CPU are extracted, before the FDMA is stopped. Otherwise, with frames arriving for the CPU, extraction to the CPU can stop for good after the restart.

Frames that are still in the FDMA buffers, or queued for the CPU, when the FDMA is stopped are dropped.

On lan969x, to know which RX pages are still owned by the driver when freeing them, clear the RX buffer slot once the page has left the ring, either to the stack or back to the page_pool.

---
 .../ethernet/microchip/sparx5/lan969x/lan969x.c    |   1 +
 .../ethernet/microchip/sparx5/lan969x/lan969x.h    |   2 +
 .../microchip/sparx5/lan969x/lan969x_fdma.c        |  75 +++++++-
 .../net/ethernet/microchip/sparx5/sparx5_fdma.c    | 199 ++++++++++++++++++++-
 .../net/ethernet/microchip/sparx5/sparx5_main.c    |   1 +
 .../net/ethernet/microchip/sparx5/sparx5_main.h    |   9 +-
 6 files changed, 275 insertions(+), 12 deletions(-)
drivers/net/ethernet/microchip/sparx5/lan969x/lan969x.c +1 -0
index fd1b55eae9c3a..3966f1b74c3d7 100644
@@ -357,6 +357,7 @@ static const struct sparx5_ops lan969x_ops = {
 	.fdma_deinit             = &lan969x_fdma_deinit,
 	.fdma_poll               = &lan969x_fdma_napi_poll,
 	.fdma_xmit               = &lan969x_fdma_xmit,
+	.fdma_resize             = &lan969x_fdma_resize,
 };
 
 const struct sparx5_match_data lan969x_desc = {
drivers/net/ethernet/microchip/sparx5/lan969x/lan969x.h +2 -0
index 529fde3d4deba..c34ce0ed4e555 100644
@@ -78,5 +78,7 @@ int lan969x_fdma_deinit(struct sparx5 *sparx5);
 int lan969x_fdma_napi_poll(struct napi_struct *napi, int weight);
 int lan969x_fdma_xmit(struct sparx5 *sparx5, u32 *ifh, struct sk_buff *skb,
 		      struct net_device *dev);
+int lan969x_fdma_resize(struct sparx5 *sparx5, struct sparx5_port *port,
+			int new_mtu);
 
 #endif
drivers/net/ethernet/microchip/sparx5/lan969x/lan969x_fdma.c +72 -3

97 lines not on this page · raw message

drivers/net/ethernet/microchip/sparx5/sparx5_fdma.c +191 -8

235 lines not on this page · raw message

drivers/net/ethernet/microchip/sparx5/sparx5_main.c +1 -0
index 257778da5956c..58f0ce603ad5c 100644
@@ -1211,6 +1211,7 @@ static const struct sparx5_ops sparx5_ops = {
 	.fdma_deinit             = &sparx5_fdma_deinit,
 	.fdma_poll               = &sparx5_fdma_napi_callback,
 	.fdma_xmit               = &sparx5_fdma_xmit,
+	.fdma_resize             = &sparx5_fdma_resize,
 };
 
 static const struct sparx5_match_data sparx5_desc = {
drivers/net/ethernet/microchip/sparx5/sparx5_main.h +8 -1
index af2a81cdfeb66..f2a7d9116a28d 100644
@@ -392,6 +392,8 @@ struct sparx5_ops {
 	int (*fdma_poll)(struct napi_struct *napi, int weight);
 	int (*fdma_xmit)(struct sparx5 *sparx5, u32 *ifh, struct sk_buff *skb,
 			 struct net_device *dev);
+	int (*fdma_resize)(struct sparx5 *sparx5, struct sparx5_port *port,
+			   int new_mtu);
 };
 
 struct sparx5_main_io_resource {
@@ -516,7 +518,12 @@ int sparx5_fdma_xmit(struct sparx5 *sparx5, u32 *ifh, struct sk_buff *skb,
 irqreturn_t sparx5_fdma_handler(int irq, void *args);
 void sparx5_fdma_reload(struct sparx5 *sparx5, struct fdma *fdma);
 void sparx5_fdma_injection_mode(struct sparx5 *sparx5);
-u32 sparx5_fdma_get_max_frame(struct sparx5 *sparx5);
+u32 sparx5_fdma_get_max_frame(struct sparx5 *sparx5,
+			      struct sparx5_port *port, int new_mtu);
+void sparx5_fdma_pause(struct sparx5 *sparx5);
+void sparx5_fdma_resume(struct sparx5 *sparx5);
+int sparx5_fdma_resize(struct sparx5 *sparx5, struct sparx5_port *port,
+		       int new_mtu);
 
 /* sparx5_mactable.c */
 int sparx5_mact_learn(struct sparx5 *sparx5, int port,
Signature · 1 line
2.34.1
#13 lore raw

Re: [PATCH net-next v3 6/9] net: sparx5/lan969x: add support for resizing the FDMA buffers

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] After a resize, sparx5_fdma_resume() does not turn the injection (TX) channel's DB interrupt back on.
- [Low] The new runtime resize path fills the whole replacement RX ring from sleepable process context, but it uses the NAPI refill callbacks,…

--

net: sparx5/lan969x: add support for resizing the FDMA buffers

This adds an fdma_resize operation for Sparx5 and lan969x. It allocates new FDMA buffers sized for a new MTU, then pauses the FDMA, swaps in the new buffers and resumes. Pausing stops the TX queues and CPU forwarding and drains the queues before stopping and resetting the FDMA. On lan969x only the RX buffers and page_pool are replaced.

Quoted 4 lines
drivers/net/ethernet/microchip/sparx5/sparx5_fdma.c +0 -0
index b92d249777f0b..6c6387cb8a505 100644

[ ... ]

Quoted 1 line
drivers/net/ethernet/microchip/sparx5/sparx5_fdma.c +0 -0
@@ -564,3 +576,174 @@ int sparx5_fdma_stop(struct sparx5 *sparx5)

[ ... ]

Quoted 2 lines
drivers/net/ethernet/microchip/sparx5/sparx5_fdma.c +2 -0
+void sparx5_fdma_pause(struct sparx5 *sparx5)
+{

[ ... ]

Quoted 15 lines
drivers/net/ethernet/microchip/sparx5/sparx5_fdma.c +15 -0
+	sparx5_fdma_stop(sparx5);
+
+	/* Reset FDMA state */
+	spx5_wr(FDMA_CTRL_NRESET_SET(0), sparx5, FDMA_CTRL);
+	spx5_wr(FDMA_CTRL_NRESET_SET(1), sparx5, FDMA_CTRL);
+
+	/* Discard frames queued for the CPU, as they may not fit the new
+	 * buffers.
+	 */
+	sparx5_xtr_flush(sparx5, XTR_QUEUE);
+}
+
+/* Restart the FDMA and the port TX queues after sparx5_fdma_pause() */
+void sparx5_fdma_resume(struct sparx5 *sparx5)
+{

drivers/net/ethernet/microchip/sparx5/sparx5_fdma.c int sparx5_fdma_stop(struct sparx5 *sparx5) · line 688

Quoted 10 lines
drivers/net/ethernet/microchip/sparx5/sparx5_fdma.c +10 -0
+	struct sparx5_rx *rx = &sparx5->rx;
+	struct sparx5_tx *tx = &sparx5->tx;
+
+	/* Enable NAPI before the RX interrupt is enabled, as the interrupt
+	 * handler schedules NAPI, as in sparx5_fdma_start().
+	 */
+	napi_enable(&rx->napi);
+
+	sparx5_fdma_rx_activate(sparx5, rx);
+	sparx5_fdma_tx_activate(sparx5, tx);

[Severity: Low]
On lan969x, does the injection channel's DB interrupt stay masked after a resize?

sparx5_fdma_pause() pulses FDMA_CTRL_NRESET after sparx5_fdma_stop() has already cleared the RX bit in FDMA_INTR_DB_ENA. Here, sparx5_fdma_rx_activate() only sets the XTR channel bit:

	spx5_rmw(BIT(fdma->channel_id),
		 BIT(fdma->channel_id) & FDMA_INTR_DB_ENA_INTR_DB_ENA,
		 sparx5, FDMA_INTR_DB_ENA);

sparx5_fdma_tx_activate() does not touch FDMA_INTR_DB_ENA at all.

On lan969x, the TX channel bit is only enabled in lan969x_fdma_napi_poll(), which runs only after an interrupt has scheduled NAPI:

	if (counter < weight && napi_complete_done(napi, counter))
		spx5_wr(0xff, sparx5, FDMA_INTR_DB_ENA);

lan969x reclaims TX buffers only from NAPI, in lan969x_fdma_tx_clear_buf(). lan969x_fdma_xmit() sets FDMA_DCB_STATUS_INTR on every TX DB and expects that interrupt.

Suppose the NRESET pulse puts FDMA_INTR_DB_ENA back to a cleared default. Could the TX DCBs then fill up, so that lan969x_fdma_get_next_dcb() returns -ENOSPC and sparx5_port_xmit_impl() keeps returning NETDEV_TX_BUSY? That would last until an RX frame raises the IRQ and NAPI writes 0xff again.

This depends on whether NRESET actually clears FDMA_INTR_DB_ENA, which the code alone can't confirm. The state after resize would match the existing post-boot state, since sparx5_fdma_start() also enables only the RX bit. The behaviour is still present at the end of the series:
"net: sparx5/lan969x: add support for changing the MTU" calls fdma_resize from ndo_change_mtu without changing sparx5_fdma_resume().

Quoted 3 lines
drivers/net/ethernet/microchip/sparx5/sparx5_fdma.c +3 -0
+
+	/* Forward frames to the CPU again, now the FDMA can extract them */
+	sparx5_fdma_cpu_fwd_set(sparx5, true);

[ ... ]

Quoted 3 lines
drivers/net/ethernet/microchip/sparx5/sparx5_fdma.c +3 -0
+int sparx5_fdma_resize(struct sparx5 *sparx5, struct sparx5_port *port,
+		       int new_mtu)
+{

[ ... ]

drivers/net/ethernet/microchip/sparx5/sparx5_fdma.c int sparx5_fdma_stop(struct sparx5 *sparx5) · line 722

Quoted 4 lines
drivers/net/ethernet/microchip/sparx5/sparx5_fdma.c +4 -0
+	sparx5_fdma_rx_init(sparx5, rx_new, FDMA_XTR_CHANNEL, db_size);
+	sparx5_fdma_tx_init(sparx5, &tx_new, FDMA_INJ_CHANNEL, db_size);
+
+	err = sparx5_fdma_rx_alloc(rx_new);

[Severity: Low]
This runs from sleepable process context under RTNL. The caller is sparx5_port_change_mtu(), added later in the series. The whole ring is refilled through the NAPI dataptr callback, though:

sparx5_fdma_resize()
sparx5_fdma_rx_alloc(rx_new)

    fdma_dcbs_init()
      fdma_dcb_add()
        __fdma_dcb_add()
          sparx5_fdma_rx_dataptr_cb()
            __netdev_alloc_skb(rx->ndev, fdma->db_size, GFP_ATOMIC);

That is FDMA_DCB_MAX * FDMA_RX_DCB_MAX_DBS = 960 skbs. With a jumbo MTU, db_size is about 14 KB, so each data buffer becomes a 16 KB (order-2) allocation. That adds up to roughly 15 MB of GFP_ATOMIC allocations while the old ring of similar size is still allocated.

lan969x_fdma_resize() does the same thing. lan969x_fdma_rx_alloc(rx_new) fills 64 slots through lan969x_fdma_rx_dataptr_cb() and page_pool_dev_alloc_pages(), with pages of up to order 2.

On a fragmented, long-running system, can an MTU increase fail with -ENOMEM when a GFP_KERNEL allocation, which can reclaim and compact, would have succeeded? Could the burst also use up the atomic reserves that other RX paths depend on?

Quoted 2 lines
drivers/net/ethernet/microchip/sparx5/sparx5_fdma.c +2 -0
+	if (err)
+		goto out;

[ ... ]

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261120-sparx5-mtu-change-v3-0-55c272fe1acc%40microchip.com
author maintainer
#14 lore raw

PATCH v3 7/9 net: sparx5/lan969x: scale the queue watermarks with the MTU

In reply to Daniel Machon

The queue system of a port has pause watermarks (PAUSE_START and PAUSE_STOP), which control when pause frames are sent if TX pause is enabled, and a tail drop watermark (ATOP), above which frames received on the port are dropped. They are set in units of buffer cells, and are currently fixed multiples of a 1518 byte frame. With a larger MTU, a single frame can exceed PAUSE_START, so with TX pause enabled, a pause frame would be sent for every received frame.

Instead, compute the watermarks from the MTU of the port, plus the Ethernet header, FCS and two VLAN tags, rounded up to whole buffer cells. Program them at port init and when pause is resolved, and add sparx5_port_wm_update() for use when the MTU changes. As the MTU and phylink paths can run concurrently, protect the watermarks and the resolved pause mode with a per-port lock.

The top bit of the watermark fields is a x16 multiplier, so take its position from the field width, which is 11 bits on lan969x and 12 bits on Sparx5.

With the default MTU of 1500, the watermarks grow from 8 to 9 cells per frame, as the frame length is now rounded up instead of down.

---
 .../net/ethernet/microchip/sparx5/sparx5_main.c    |  1 +
 .../net/ethernet/microchip/sparx5/sparx5_main.h    |  2 +
 .../net/ethernet/microchip/sparx5/sparx5_port.c    | 73 ++++++++++++++--------
 .../net/ethernet/microchip/sparx5/sparx5_port.h    |  1 +
 4 files changed, 51 insertions(+), 26 deletions(-)
drivers/net/ethernet/microchip/sparx5/sparx5_main.c +1 -0
index 58f0ce603ad5c..2a957d1d18ad9 100644
@@ -340,6 +340,7 @@ static int sparx5_create_port(struct sparx5 *sparx5,
 	spx5_port->phylink_pcs.ops = &sparx5_phylink_pcs_ops;
 	spx5_port->is_mrouter = false;
 	INIT_LIST_HEAD(&spx5_port->tc_templates);
+	mutex_init(&spx5_port->wm_lock);
 	sparx5->ports[config->portno] = spx5_port;
 
 	err = sparx5_port_init(sparx5, spx5_port, &config->conf);
drivers/net/ethernet/microchip/sparx5/sparx5_main.h +2 -0
index f2a7d9116a28d..cfda2c05e99eb 100644
@@ -236,6 +236,8 @@ struct sparx5_port {
 	struct device_node *of_node;
 	struct phy *serdes;
 	struct sparx5_port_config conf;
+	struct mutex wm_lock; /* Protects the QSYS watermarks and wm_pause */
+	u8 wm_pause;
 	struct phylink_config phylink_config;
 	struct phylink *phylink;
 	struct phylink_pcs phylink_pcs;
drivers/net/ethernet/microchip/sparx5/sparx5_port.c +47 -26

128 lines not on this page · raw message

drivers/net/ethernet/microchip/sparx5/sparx5_port.h +1 -0
index 912f97a506768..06d2becfd9aac 100644
@@ -91,6 +91,7 @@ int sparx5_port_init(struct sparx5 *sparx5,
 void sparx5_port_max_len_set(struct sparx5 *sparx5,
 			     struct sparx5_port *port,
 			     u32 len);
+void sparx5_port_wm_update(struct sparx5 *sparx5, struct sparx5_port *port);
 
 int sparx5_port_config(struct sparx5 *sparx5,
 		       struct sparx5_port *spx5_port,
Signature · 1 line
2.34.1
#15 lore raw

Re: [PATCH net-next v3 7/9] net: sparx5/lan969x: scale the queue watermarks with the MTU

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

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

Critical: 0 · High: 0 · Medium: 1 · 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).

- [Medium] At this commit (and at the next one, "scale the TAS guard band with the MTU"), sparx5_port_wm_set() in…
- [Low] This commit adds sparx5_port_wm_update() (sparx5_port.c:717) as a non-static function with a prototype in sparx5_port.h:94, but nothing…

--

net: sparx5/lan969x: scale the queue watermarks with the MTU

The commit now computes the QSYS PAUSE_START, PAUSE_STOP and ATOP watermarks from the port MTU, plus the Ethernet header, FCS and two VLAN tags, rounded up to whole buffer cells. Before, they were fixed multiples of a 1518 byte frame. The watermarks are programmed at port init and when pause is resolved. The commit also adds sparx5_port_wm_update() for MTU changes and a per-port wm_lock.

Quoted 4 lines
drivers/net/ethernet/microchip/sparx5/sparx5_port.c +0 -0
index 701d310df0ba6..87aa0b7841359 100644

[ ... ]

Quoted 1 line
drivers/net/ethernet/microchip/sparx5/sparx5_port.c +0 -0
@@ -676,24 +679,53 @@ int sparx5_port_fwd_urg(struct sparx5 *sparx5, u32 speed)

[ ... ]

drivers/net/ethernet/microchip/sparx5/sparx5_port.c int sparx5_port_fwd_urg(struct sparx5 *sparx5, u32 speed) · line 698

Quoted 6 lines
drivers/net/ethernet/microchip/sparx5/sparx5_port.c +6 -0
+/* Pause start/stop and tail drop at 6/4/20 frames of the port MTU */
+static void sparx5_port_wm_set(struct sparx5 *sparx5, struct sparx5_port *port,
+			       u8 pause)
+{
+	u32 len = SPX5_HW_MTU(READ_ONCE(port->ndev->mtu)) + 2 * VLAN_HLEN;
+	u32 cells = DIV_ROUND_UP(len, SPX5_BUFFER_CELL_SZ);

[Severity: Medium]
At this commit, can port->ndev->mtu drift away from the MAC max frame length?

sparx5_port_init() still programs a fixed max length:

	sparx5_port_max_len_set(sparx5, port, ETH_MAXLEN);

sparx5_port_netdev_ops has no .ndo_change_mtu. With the ether_setup() defaults (min_mtu 68, max_mtu 1500), the core writes dev->mtu directly and no hardware gets reconfigured.

Suppose the MTU is lowered to 68 with ip link. The next link-up then runs:

sparx5_port_config()->sparx5_port_fc_setup()->sparx5_port_wm_set()

That gives len = 94 and cells = 1. PAUSE_START becomes 6 cells, PAUSE_STOP 4 cells and ATOP 20 cells.

The MAC still accepts 1518-byte frames, which take 9 cells each. With TX pause enabled, wouldn't one full-size frame go past PAUSE_START, so that a pause frame is sent for every received frame? ATOP would also drop to about two full-size frames, which means heavy tail drop. That is the problem the commit message describes, only triggered by a smaller MTU instead of a larger one.

The problem is also present at the next patch, "scale the TAS guard band with the MTU". The final patch of the series, "net: sparx5/lan969x: add support for changing the MTU", fixes it. It adds sparx5_port_change_mtu(), which returns -EOPNOTSUPP when FDMA is not in use. Otherwise it calls sparx5_port_max_len_set() with SPX5_HW_MTU(new_mtu) and then sparx5_port_wm_update(). So this only affects the intermediate bisection points.

Could the series be ordered so that the watermarks and the MAC max length stay in sync at every commit?

Quoted 2 lines
drivers/net/ethernet/microchip/sparx5/sparx5_port.c +2 -0
+	u32 pause_stop = 0xFFF - 1; /* FC gen disabled */
+

[ ... ]

drivers/net/ethernet/microchip/sparx5/sparx5_port.c int sparx5_port_fwd_urg(struct sparx5 *sparx5, u32 speed) · line 722

Quoted 8 lines
drivers/net/ethernet/microchip/sparx5/sparx5_port.c +8 -0
+}
+
+void sparx5_port_wm_update(struct sparx5 *sparx5, struct sparx5_port *port)
+{
+	mutex_lock(&port->wm_lock);
+	sparx5_port_wm_set(sparx5, port, port->wm_pause);
+	mutex_unlock(&port->wm_lock);
+}

[Severity: Low]
This isn't a bug, but sparx5_port_wm_update() is non-static and has a prototype in sparx5_port.h, yet nothing calls it at this commit. sparx5_port_netdev_ops has no .ndo_change_mtu yet, so the MTU-change part of this patch is not connected to anything.

This comment in sparx5_port_disable() also refers to an MTU-change path that does not exist yet:

	/* 5: Disable Flowcontrol, and keep an MTU change from re-enabling it */

The first caller is sparx5_port_change_mtu(), added by the final patch of the series, "net: sparx5/lan969x: add support for changing the MTU". It calls sparx5_port_wm_update() after WRITE_ONCE(dev->mtu, new_mtu), so the READ_ONCE() in sparx5_port_wm_set() picks up the new MTU.

Could the helper be added in the same patch as its caller?

[ ... ]

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261120-sparx5-mtu-change-v3-0-55c272fe1acc%40microchip.com
author maintainer
#16 lore raw

Re: [PATCH net-next v3 7/9] net: sparx5/lan969x: scale the queue watermarks with the MTU

Quoted 79 lines

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

Critical: 0 · High: 0 · Medium: 1 · 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).

- [Medium] At this commit (and at the next one, "scale the TAS guard band with the MTU"), sparx5_port_wm_set() in…
- [Low] This commit adds sparx5_port_wm_update() (sparx5_port.c:717) as a non-static function with a prototype in sparx5_port.h:94, but nothing…

--

net: sparx5/lan969x: scale the queue watermarks with the MTU

The commit now computes the QSYS PAUSE_START, PAUSE_STOP and ATOP watermarks from the port MTU, plus the Ethernet header, FCS and two VLAN tags, rounded up to whole buffer cells. Before, they were fixed multiples of a 1518 byte frame. The watermarks are programmed at port init and when pause is resolved. The commit also adds sparx5_port_wm_update() for MTU changes and a per-port wm_lock.

Quoted 4 lines
drivers/net/ethernet/microchip/sparx5/sparx5_port.c +0 -0
index 701d310df0ba6..87aa0b7841359 100644

[ ... ]

Quoted 1 line
drivers/net/ethernet/microchip/sparx5/sparx5_port.c +0 -0
@@ -676,24 +679,53 @@ int sparx5_port_fwd_urg(struct sparx5 *sparx5, u32 speed)

[ ... ]

drivers/net/ethernet/microchip/sparx5/sparx5_port.c int sparx5_port_fwd_urg(struct sparx5 *sparx5, u32 speed) · line 698

Quoted 6 lines
drivers/net/ethernet/microchip/sparx5/sparx5_port.c +6 -0
+/* Pause start/stop and tail drop at 6/4/20 frames of the port MTU */
+static void sparx5_port_wm_set(struct sparx5 *sparx5, struct sparx5_port *port,
+			       u8 pause)
+{
+	u32 len = SPX5_HW_MTU(READ_ONCE(port->ndev->mtu)) + 2 * VLAN_HLEN;
+	u32 cells = DIV_ROUND_UP(len, SPX5_BUFFER_CELL_SZ);

[Severity: Medium]
At this commit, can port->ndev->mtu drift away from the MAC max frame length?

sparx5_port_init() still programs a fixed max length:

	sparx5_port_max_len_set(sparx5, port, ETH_MAXLEN);

sparx5_port_netdev_ops has no .ndo_change_mtu. With the ether_setup() defaults (min_mtu 68, max_mtu 1500), the core writes dev->mtu directly and no hardware gets reconfigured.

Suppose the MTU is lowered to 68 with ip link. The next link-up then runs:

sparx5_port_config()->sparx5_port_fc_setup()->sparx5_port_wm_set()

That gives len = 94 and cells = 1. PAUSE_START becomes 6 cells, PAUSE_STOP 4 cells and ATOP 20 cells.

The MAC still accepts 1518-byte frames, which take 9 cells each. With TX pause enabled, wouldn't one full-size frame go past PAUSE_START, so that a pause frame is sent for every received frame? ATOP would also drop to about two full-size frames, which means heavy tail drop. That is the problem the commit message describes, only triggered by a smaller MTU instead of a larger one.

The problem is also present at the next patch, "scale the TAS guard band with the MTU". The final patch of the series, "net: sparx5/lan969x: add support for changing the MTU", fixes it. It adds sparx5_port_change_mtu(), which returns -EOPNOTSUPP when FDMA is not in use. Otherwise it calls sparx5_port_max_len_set() with SPX5_HW_MTU(new_mtu) and then sparx5_port_wm_update(). So this only affects the intermediate bisection points.

Quoted 2 lines

Could the series be ordered so that the watermarks and the MAC max length stay in sync at every commit?

This goes away with patch 9, which keeps the watermarks and the MAC max frame length in sync. Until then, it needs the MTU to be lowered from userspace, and it does not break anything. At worst the port sends extra pause frames and tail drops earlier. I dont think that is worth reordering the series for.

Quoted 37 lines
Quoted 3 lines
drivers/net/ethernet/microchip/sparx5/sparx5_port.c +2 -0
+	u32 pause_stop = 0xFFF - 1; /* FC gen disabled */
+

[ ... ]

drivers/net/ethernet/microchip/sparx5/sparx5_port.c int sparx5_port_fwd_urg(struct sparx5 *sparx5, u32 speed) · line 722

Quoted 8 lines
drivers/net/ethernet/microchip/sparx5/sparx5_port.c +8 -0
+}
+
+void sparx5_port_wm_update(struct sparx5 *sparx5, struct sparx5_port *port)
+{
+	mutex_lock(&port->wm_lock);
+	sparx5_port_wm_set(sparx5, port, port->wm_pause);
+	mutex_unlock(&port->wm_lock);
+}

[Severity: Low]
This isn't a bug, but sparx5_port_wm_update() is non-static and has a prototype in sparx5_port.h, yet nothing calls it at this commit. sparx5_port_netdev_ops has no .ndo_change_mtu yet, so the MTU-change part of this patch is not connected to anything.

This comment in sparx5_port_disable() also refers to an MTU-change path that does not exist yet:

	/* 5: Disable Flowcontrol, and keep an MTU change from re-enabling it */

The first caller is sparx5_port_change_mtu(), added by the final patch of the series, "net: sparx5/lan969x: add support for changing the MTU". It calls sparx5_port_wm_update() after WRITE_ONCE(dev->mtu, new_mtu), so the READ_ONCE() in sparx5_port_wm_set() picks up the new MTU.

Could the helper be added in the same patch as its caller?

[ ... ]

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261120-sparx5-mtu-change-v3-0-55c272fe1acc%40microchip.com
author maintainer
#17 lore raw

PATCH v3 8/9 net: sparx5/lan969x: scale the TAS guard band with the MTU

In reply to Daniel Machon

The TAS guard band (QMAXSDU_VAL, in units of 64 bytes) is how long before a gate closes the scheduler stops starting frames of a traffic class, so that a frame started just before the close still finishes in time. The driver leaves it at the reset value of 24 x 64 = 1536 bytes, so with a larger MTU, a frame started inside the guard band would overrun the gate close.

The MAC maximum frame length only limits the frames a port receives, so a port can transmit frames of the largest MTU of all ports, for example when forwarding from a port with a larger MTU. Therefore, size the guard band of all ports for a frame of the largest MTU, plus the Ethernet header, FCS and two VLAN tags, rounded up to 64 bytes. Add the HSCH_TAS_QMAXSDU_CFG register, program the guard band at init, and add sparx5_tas_gb_update() for use when the MTU changes.

With the default MTU of 1500 the guard band is unchanged. As the scheduler does not start a frame inside the guard band, a gate window shorter than the guard band at the link speed cannot transmit, as is already the case for the default 1536 byte guard band.

---
 .../net/ethernet/microchip/sparx5/sparx5_main.h    |  1 +
 .../ethernet/microchip/sparx5/sparx5_main_regs.h   | 12 ++++++
 drivers/net/ethernet/microchip/sparx5/sparx5_tas.c | 43 +++++++++++++++++++++-
 3 files changed, 54 insertions(+), 2 deletions(-)
drivers/net/ethernet/microchip/sparx5/sparx5_main.h +1 -0
index cfda2c05e99eb..64ace5c3af5ab 100644
@@ -788,6 +788,7 @@ int sparx5_tas_enable(struct sparx5_port *port,
 		      struct tc_taprio_qopt_offload *qopt);
 int sparx5_tas_disable(struct sparx5_port *port);
 void sparx5_tas_speed(struct sparx5_port *port, int speed);
+void sparx5_tas_gb_update(struct sparx5 *sparx5);
 
 /* sparx5_mirror.c */
 int sparx5_mirror_add(struct sparx5_mall_entry *entry);
drivers/net/ethernet/microchip/sparx5/sparx5_main_regs.h +12 -0
index b2314ce8baf50..f90280714f8c5 100644
@@ -5611,6 +5611,18 @@ extern const struct sparx5_regs *regs;
 #define HSCH_TAS_STATEMACHINE_CFG_REVISIT_DLY_GET(x)\
 	FIELD_GET(HSCH_TAS_STATEMACHINE_CFG_REVISIT_DLY, x)
 
+/* HSCH:TAS_PROFILE_CFG:TAS_QMAXSDU_CFG */
+#define HSCH_TAS_QMAXSDU_CFG(g, r)                                             \
+	__REG(TARGET_HSCH, 0, 1, regs->gaddr[GA_HSCH_TAS_PROFILE_CFG], g,       \
+	      regs->gcnt[GC_HSCH_TAS_PROFILE_CFG],                              \
+	      regs->gsize[GW_HSCH_TAS_PROFILE_CFG], 0, r, 8, 4)
+
+#define HSCH_TAS_QMAXSDU_CFG_QMAXSDU_VAL         GENMASK(7, 0)
+#define HSCH_TAS_QMAXSDU_CFG_QMAXSDU_VAL_SET(x)\
+	FIELD_PREP(HSCH_TAS_QMAXSDU_CFG_QMAXSDU_VAL, x)
+#define HSCH_TAS_QMAXSDU_CFG_QMAXSDU_VAL_GET(x)\
+	FIELD_GET(HSCH_TAS_QMAXSDU_CFG_QMAXSDU_VAL, x)
+
 /* HSCH:TAS_PROFILE_CFG:TAS_PROFILE_CONFIG */
 #define HSCH_TAS_PROFILE_CONFIG(g)                                             \
 	__REG(TARGET_HSCH, 0, 1, regs->gaddr[GA_HSCH_TAS_PROFILE_CFG], g,       \
drivers/net/ethernet/microchip/sparx5/sparx5_tas.c +41 -2

67 lines not on this page · raw message

Signature · 1 line
2.34.1
#18 lore raw

Re: [PATCH net-next v3 8/9] net: sparx5/lan969x: scale the TAS guard band with the MTU

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

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

Critical: 0 · High: 0 · Medium: 1 · 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).

- [Medium] sparx5_tas_gb_update() (drivers/net/ethernet/microchip/sparx5/sparx5_tas.c) takes the largest…
- [Low] In sparx5_tas_gb_update() (drivers/net/ethernet/microchip/sparx5/sparx5_tas.c:738) the guard…

--

net: sparx5/lan969x: scale the TAS guard band with the MTU

This commit programs the TAS guard band (HSCH_TAS_QMAXSDU_CFG) for all ports and traffic classes. The value comes from the largest MTU of all ports, plus the Ethernet header, FCS and two VLAN tags, rounded up to 64 bytes. It adds sparx5_tas_gb_update(), calls it from sparx5_tas_init(), and makes it available for use when the MTU changes.

Quoted 4 lines
drivers/net/ethernet/microchip/sparx5/sparx5_tas.c +0 -0
index ec80bc69371ab..32e502bc4f18f 100644

[ ... ]

Quoted 10 lines
drivers/net/ethernet/microchip/sparx5/sparx5_tas.c +6 -0
@@ -718,6 +721,40 @@ int sparx5_tas_disable(struct sparx5_port *port)
 	return err;
 }
 
+/* Size the guard band of all ports for a frame of the largest MTU of all
+ * ports, as a port can transmit frames forwarded from any port.
+ */
+void sparx5_tas_gb_update(struct sparx5 *sparx5)
+{
+	u32 max_mtu = 0, gb;

drivers/net/ethernet/microchip/sparx5/sparx5_tas.c int sparx5_tas_disable(struct sparx5_port *port) · line 739

Quoted 10 lines
drivers/net/ethernet/microchip/sparx5/sparx5_tas.c +10 -0
+	int i, tc;
+
+	for (i = 0; i < sparx5->data->consts->n_ports; i++) {
+		if (sparx5->ports[i])
+			max_mtu = max_t(u32, max_mtu,
+					READ_ONCE(sparx5->ports[i]->ndev->mtu));
+	}
+
+	gb = DIV_ROUND_UP(SPX5_HW_MTU(max_mtu) + 2 * VLAN_HLEN,
+			  TAS_QMAXSDU_UNIT);

[Severity: Low]
Should the 8 byte preamble/SFD be counted here?

The guard band is sized from the L2 frame only, F = mtu + 26, rounded up to 64 bytes. A frame of F bytes that starts just before the guard band needs F + 8 byte-times on the wire to finish.

When F mod 64 is 0 or 57..63, the rounding leaves less than 8 bytes of slack. For example, mtu=2022 gives F=2048 and mtu=9190 gives F=9216, both with no slack. mtu=4000 gives F=4026, with 6 bytes of slack.

Suppose the HSCH turns QMAXSDU_VAL * 64 directly into time at LINK_SPEED, without adding L1 overhead itself. Could a maximum size double tagged frame then overrun the gate close by up to 8 byte-times? The default MTU of 1500 is fine (1526 vs 1536).

For comparison, felix/vsc9959 adds L1 overhead explicitly when it calculates the guard band. The tree doesn't show whether the sparx5/lan969x hardware adds the preamble time internally.

drivers/net/ethernet/microchip/sparx5/sparx5_tas.c int sparx5_tas_disable(struct sparx5_port *port) · line 752

Quoted 13 lines
drivers/net/ethernet/microchip/sparx5/sparx5_tas.c +13 -0
+
+	mutex_lock(&sparx5->tas_lock);
+
+	/* Each port uses the TAS profile of its own number */
+	for (i = 0; i < sparx5->data->consts->n_ports; i++) {
+		if (!sparx5->ports[i])
+			continue;
+
+		for (tc = 0; tc < SPX5_PRIOS; tc++)
+			spx5_rmw(HSCH_TAS_QMAXSDU_CFG_QMAXSDU_VAL_SET(gb),
+				 HSCH_TAS_QMAXSDU_CFG_QMAXSDU_VAL,
+				 sparx5,
+				 HSCH_TAS_QMAXSDU_CFG(i, tc));

[Severity: Medium]
What happens to taprio schedules already offloaded on other ports when this loop rewrites the guard band?

The loop writes the new guard band into every port's profile for all 8 traffic classes. That includes ports whose lists are already pending or operating. sparx5_tas_init() sets ALWAYS_GUARD_BAND_SCH_Q, so the guard band applies to every class:

sparx5_tas_init() {
    ...
	spx5_rmw(HSCH_TAS_CFG_CTRL_LIST_NUM_MAX_SET(list_num_max) |
		 HSCH_TAS_CFG_CTRL_ALWAYS_GUARD_BAND_SCH_Q_SET(1),
    ...
}

Later in this series, "net: sparx5/lan969x: add support for changing the MTU" calls sparx5_tas_gb_update() from sparx5_port_change_mtu(). After that, "ip link set dev ethB mtu 9000" raises the guard band on every port from 1536 to 142 * 64 = 9088 bytes. That is about 72.7 us at 1 Gbps and 727 us at 100 Mbps. At the maximum MTU it reaches 219 * 64 = 14016 bytes.

Take a schedule on ethA with a 20-50 us window at 1G. It works with the 1536 byte guard band (about 12.3 us). The commit message notes that the scheduler starts no frame inside the guard band.

After the MTU change on ethB, wouldn't that window never open? The queue would then fill and tail-drop, and no error would be reported.

sparx5_tas_gb_update() returns void and doesn't recheck the programmed lists. sparx5_tas_enable() checks intervals only against TAS_MIN_CYCLE_TIME_NS and TAS_MAX_CYCLE_TIME_NS, not against the guard band, so such schedules are also accepted without a warning:

sparx5_tas_enable() {
    ...
		if (interval < TAS_MIN_CYCLE_TIME_NS) {
    ...
}

The taprio max_sdu[] values, which correspond to this register, are also ignored. So the guard band can't be limited per class.

felix/vsc9959 handles this in vsc9959_tas_guard_bands_update(). It recomputes the per-TC QMAXSDU from the schedule's minimum gate lengths whenever the MTU or the schedule changes. It also enables oversize dropping, so short windows can still carry smaller frames.

Using the switch-wide maximum MTU seems reasonable, since ports forward and mirror frames from each other. The concern is only that running schedules can stop transmitting without any error.

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261120-sparx5-mtu-change-v3-0-55c272fe1acc%40microchip.com
author maintainer
#19 lore raw

Re: [PATCH net-next v3 8/9] net: sparx5/lan969x: scale the TAS guard band with the MTU

Quoted 131 lines

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

Critical: 0 · High: 0 · Medium: 1 · 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).

- [Medium] sparx5_tas_gb_update() (drivers/net/ethernet/microchip/sparx5/sparx5_tas.c) takes the largest…
- [Low] In sparx5_tas_gb_update() (drivers/net/ethernet/microchip/sparx5/sparx5_tas.c:738) the guard…

--

net: sparx5/lan969x: scale the TAS guard band with the MTU

This commit programs the TAS guard band (HSCH_TAS_QMAXSDU_CFG) for all ports and traffic classes. The value comes from the largest MTU of all ports, plus the Ethernet header, FCS and two VLAN tags, rounded up to 64 bytes. It adds sparx5_tas_gb_update(), calls it from sparx5_tas_init(), and makes it available for use when the MTU changes.

Quoted 4 lines
drivers/net/ethernet/microchip/sparx5/sparx5_tas.c +0 -0
index ec80bc69371ab..32e502bc4f18f 100644

[ ... ]

Quoted 10 lines
drivers/net/ethernet/microchip/sparx5/sparx5_tas.c +6 -0
@@ -718,6 +721,40 @@ int sparx5_tas_disable(struct sparx5_port *port)
 	return err;
 }
 
+/* Size the guard band of all ports for a frame of the largest MTU of all
+ * ports, as a port can transmit frames forwarded from any port.
+ */
+void sparx5_tas_gb_update(struct sparx5 *sparx5)
+{
+	u32 max_mtu = 0, gb;

drivers/net/ethernet/microchip/sparx5/sparx5_tas.c int sparx5_tas_disable(struct sparx5_port *port) · line 739

Quoted 10 lines
drivers/net/ethernet/microchip/sparx5/sparx5_tas.c +10 -0
+	int i, tc;
+
+	for (i = 0; i < sparx5->data->consts->n_ports; i++) {
+		if (sparx5->ports[i])
+			max_mtu = max_t(u32, max_mtu,
+					READ_ONCE(sparx5->ports[i]->ndev->mtu));
+	}
+
+	gb = DIV_ROUND_UP(SPX5_HW_MTU(max_mtu) + 2 * VLAN_HLEN,
+			  TAS_QMAXSDU_UNIT);

[Severity: Low]
Should the 8 byte preamble/SFD be counted here?

The guard band is sized from the L2 frame only, F = mtu + 26, rounded up to 64 bytes. A frame of F bytes that starts just before the guard band needs F + 8 byte-times on the wire to finish.

When F mod 64 is 0 or 57..63, the rounding leaves less than 8 bytes of slack. For example, mtu=2022 gives F=2048 and mtu=9190 gives F=9216, both with no slack. mtu=4000 gives F=4026, with 6 bytes of slack.

Suppose the HSCH turns QMAXSDU_VAL * 64 directly into time at LINK_SPEED, without adding L1 overhead itself. Could a maximum size double tagged frame then overrun the gate close by up to 8 byte-times? The default MTU of 1500 is fine (1526 vs 1536).

For comparison, felix/vsc9959 adds L1 overhead explicitly when it calculates the guard band. The tree doesn't show whether the sparx5/lan969x hardware adds the preamble time internally.

drivers/net/ethernet/microchip/sparx5/sparx5_tas.c int sparx5_tas_disable(struct sparx5_port *port) · line 752

Quoted 13 lines
drivers/net/ethernet/microchip/sparx5/sparx5_tas.c +13 -0
+
+	mutex_lock(&sparx5->tas_lock);
+
+	/* Each port uses the TAS profile of its own number */
+	for (i = 0; i < sparx5->data->consts->n_ports; i++) {
+		if (!sparx5->ports[i])
+			continue;
+
+		for (tc = 0; tc < SPX5_PRIOS; tc++)
+			spx5_rmw(HSCH_TAS_QMAXSDU_CFG_QMAXSDU_VAL_SET(gb),
+				 HSCH_TAS_QMAXSDU_CFG_QMAXSDU_VAL,
+				 sparx5,
+				 HSCH_TAS_QMAXSDU_CFG(i, tc));

[Severity: Medium]
What happens to taprio schedules already offloaded on other ports when this loop rewrites the guard band?

The loop writes the new guard band into every port's profile for all 8 traffic classes. That includes ports whose lists are already pending or operating. sparx5_tas_init() sets ALWAYS_GUARD_BAND_SCH_Q, so the guard band applies to every class:

sparx5_tas_init() {
    ...
	spx5_rmw(HSCH_TAS_CFG_CTRL_LIST_NUM_MAX_SET(list_num_max) |
		 HSCH_TAS_CFG_CTRL_ALWAYS_GUARD_BAND_SCH_Q_SET(1),
    ...
}

Later in this series, "net: sparx5/lan969x: add support for changing the MTU" calls sparx5_tas_gb_update() from sparx5_port_change_mtu(). After that, "ip link set dev ethB mtu 9000" raises the guard band on every port from 1536 to 142 * 64 = 9088 bytes. That is about 72.7 us at 1 Gbps and 727 us at 100 Mbps. At the maximum MTU it reaches 219 * 64 = 14016 bytes.

Take a schedule on ethA with a 20-50 us window at 1G. It works with the 1536 byte guard band (about 12.3 us). The commit message notes that the scheduler starts no frame inside the guard band.

After the MTU change on ethB, wouldn't that window never open? The queue would then fill and tail-drop, and no error would be reported.

sparx5_tas_gb_update() returns void and doesn't recheck the programmed lists. sparx5_tas_enable() checks intervals only against TAS_MIN_CYCLE_TIME_NS and TAS_MAX_CYCLE_TIME_NS, not against the guard band, so such schedules are also accepted without a warning:

sparx5_tas_enable() {
    ...
		if (interval < TAS_MIN_CYCLE_TIME_NS) {
    ...
}

The taprio max_sdu[] values, which correspond to this register, are also ignored. So the guard band can't be limited per class.

Quoted 8 lines

felix/vsc9959 handles this in vsc9959_tas_guard_bands_update(). It recomputes the per-TC QMAXSDU from the schedule's minimum gate lengths whenever the MTU or the schedule changes. It also enables oversize dropping, so short windows can still carry smaller frames.

Using the switch-wide maximum MTU seems reasonable, since ports forward and mirror frames from each other. The concern is only that running schedules can stop transmitting without any error.

This was already dismissed on v2 [1].

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

Quoted 3 lines
Signature · 2 lines
-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261120-sparx5-mtu-change-v3-0-55c272fe1acc%40microchip.com
author maintainer
#20 lore raw

PATCH v3 9/9 net: sparx5/lan969x: add support for changing the MTU

In reply to Daniel Machon

The MTU of a port is currently fixed to 1500, as the MAC maximum frame length, the FDMA buffers, the queue watermarks and the TAS guard band are all sized for a 1518 byte frame. The previous patches make the buffers, watermarks and guard band follow the MTU, so changing the MTU is a matter of programming the MAC and updating these.

Add an ndo_change_mtu callback that sets the MAC maximum frame length of the port and resizes the FDMA buffers. The MAC must never accept frames that are larger than the RX buffers, so when decreasing the MTU, the maximum frame length is lowered before the buffers are shrunk, and when increasing it, it is only raised once the buffers have been grown. The new MTU is only published once the buffers fit it. If the buffers cannot be grown, the MTU is not changed. If they cannot be shrunk, the current buffers still fit all frames, so the new MTU is kept. Once the new MTU is published, update the queue watermarks of the port and the TAS guard band.

As the FDMA is shared by all ports, resizing its buffers briefly stops CPU traffic on all ports. This only happens when the change affects the buffer size needed for the largest MTU of the ports.

Register based extraction, which is used when the FDMA is not, sizes its skbs from the MTU at the time a frame is read, so frames queued before an MTU decrease would not fit. Therefore, only allow the MTU to be changed when the FDMA is used.

The MACs of both Sparx5 and lan969x support frames of up to 14000 bytes, so set the maximum MTU accordingly when the FDMA is used, and extend the top RMON histogram range to the maximum frame length.

---
 .../net/ethernet/microchip/sparx5/sparx5_ethtool.c |  2 +-
 .../net/ethernet/microchip/sparx5/sparx5_main.h    |  1 +
 .../net/ethernet/microchip/sparx5/sparx5_netdev.c  | 48 +++++++++++++++++++++-
 3 files changed, 49 insertions(+), 2 deletions(-)
drivers/net/ethernet/microchip/sparx5/sparx5_ethtool.c +1 -1
index 2c6a5d3bc2bc6..af1fd32c6cef7 100644
@@ -809,7 +809,7 @@ static const struct ethtool_rmon_hist_range sparx5_rmon_ranges[] = {
 	{  256,   511 },
 	{  512,  1023 },
 	{ 1024,  1518 },
-	{ 1519, 10239 },
+	{ 1519, SPX5_MAX_FRAME_LEN + 2 * VLAN_HLEN },
 	{}
 };
 
drivers/net/ethernet/microchip/sparx5/sparx5_main.h +1 -0
index 64ace5c3af5ab..a94c025ba3c0d 100644
@@ -111,6 +111,7 @@ enum sparx5_feature {
 #define SPX5_BUFFER_MEMORY     4194280 /* 22795 words * 184 bytes */
 #define SPX5_TAS_ENTRIES_PER_PORT 2 /* TAS lists per port */
 #define SPX5_HW_MTU(mtu)       ((mtu) + ETH_HLEN + ETH_FCS_LEN)
+#define SPX5_MAX_FRAME_LEN     14000 /* Max frame length of the MACs */
 
 #define XTR_QUEUE     0
 #define INJ_QUEUE     0
drivers/net/ethernet/microchip/sparx5/sparx5_netdev.c +47 -1

66 lines not on this page · raw message

Signature · 1 line
2.34.1
#21 lore raw
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: 0 (2) · Medium: 1 · Low: 3

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).

- [Medium] This patch raises max_mtu to 13982 in FDMA mode and adds the first caller of ops->fdma_resize (sparx5_port_change_mtu, process…
- [Low] The commit message says "The MTU of a port is currently fixed to 1500".
- [Low] SPX5_MAX_FRAME_LEN (sparx5_main.h) is defined as 14000 with the comment "Max frame length of the MACs", and the commit message says…
- [Low] net: sparx5: sparx5_port_change_mtu() fails MTU increase on failed buffer shrink

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but this patch silently closes it without saying so.
- [High] This problem wasn't introduced by this patch, but it is a crash.

--

net: sparx5/lan969x: add support for changing the MTU

This adds an ndo_change_mtu callback for sparx5 and lan969x ports. The callback programs the MAC maximum frame length, resizes the shared FDMA buffers, and updates the queue watermarks and the TAS guard band. MTU changes are only allowed when the FDMA is used, in which case max_mtu is raised to 14000 - ETH_HLEN - ETH_FCS_LEN and the top RMON histogram bucket is extended.

Quoted 3 lines

The MTU of a port is currently fixed to 1500, as the MAC maximum frame length, the FDMA buffers, the queue watermarks and the TAS guard band are all sized for a 1518 byte frame.

[Severity: Low]
Is this description accurate? The ports come from devm_alloc_etherdev_mqs() -> ether_setup(), so min_mtu is ETH_MIN_MTU (68) and max_mtu is ETH_DATA_LEN (1500).

Before this patch there was no ndo_change_mtu, so __netif_set_mtu() just did WRITE_ONCE(dev->mtu, new_mtu). Userspace could already lower the MTU.

After this patch, sparx5_port_change_mtu() in sparx5_netdev.c returns -EOPNOTSUPP for every MTU change when sparx5->fdma_irq <= 0. That includes decreases that used to succeed, for example on Sparx5 revision 0, where sparx5_frame_io_init() does not use the FDMA.

In FDMA mode, a decrease now also lowers MAC_MAXLEN_CFG. The port then drops hardware forwarded frames larger than the new MTU + 18 that it used to accept.

Rejecting the change in register mode looks correct, since lowering the MTU there was memory unsafe (see below). Could the commit message describe the previous behaviour, and mention that lowering the MTU in register mode now fails?

Quoted 18 lines
drivers/net/ethernet/microchip/sparx5/sparx5_ethtool.c +1 -1
index 2c6a5d3bc2bc6..af1fd32c6cef7 100644
@@ -809,7 +809,7 @@ static const struct ethtool_rmon_hist_range sparx5_rmon_ranges[] = {
 	{  256,   511 },
 	{  512,  1023 },
 	{ 1024,  1518 },
-	{ 1519, 10239 },
+	{ 1519, SPX5_MAX_FRAME_LEN + 2 * VLAN_HLEN },
 	{}
 };
 
drivers/net/ethernet/microchip/sparx5/sparx5_main.h +0 -0
index 64ace5c3af5ab..a94c025ba3c0d 100644

drivers/net/ethernet/microchip/sparx5/sparx5_main.h enum sparx5_feature { · line 114

Quoted 4 lines
drivers/net/ethernet/microchip/sparx5/sparx5_main.h +1 -0
@@ -111,6 +111,7 @@ enum sparx5_feature {
 #define SPX5_BUFFER_MEMORY     4194280 /* 22795 words * 184 bytes */
 #define SPX5_TAS_ENTRIES_PER_PORT 2 /* TAS lists per port */
 #define SPX5_HW_MTU(mtu)       ((mtu) + ETH_HLEN + ETH_FCS_LEN)
+#define SPX5_MAX_FRAME_LEN     14000 /* Max frame length of the MACs */

[Severity: Low]
Does 14000 include VLAN tags or not? The comment calls it the max frame length of the MACs, and the commit message says the MACs "support frames of up to 14000 bytes".

In sparx5_register_netdevs(), max_mtu becomes 14000 - ETH_HLEN - ETH_FCS_LEN = 13982, so sparx5_port_change_mtu() programs MAC_MAXLEN_CFG to 14000.

sparx5_port_max_tags_set() enables VLAN_LEN_AWR_ENA, which lets a tagged frame exceed MAXLEN by 4 bytes per tag. The new RMON bucket above also advertises { 1519, SPX5_MAX_FRAME_LEN + 2 * VLAN_HLEN }, which is 14008.

If 14000 is a hard hardware limit, should max_mtu also subtract 2 * VLAN_HLEN, and should the RMON range stop at 14000? If it is not, should the comment and the commit message be updated?

Quoted 14 lines
drivers/net/ethernet/microchip/sparx5/sparx5_main.h +0 -0
 #define XTR_QUEUE     0
 #define INJ_QUEUE     0
drivers/net/ethernet/microchip/sparx5/sparx5_netdev.c +3 -0
index b3725cf2dd44c..095925752f5f6 100644
@@ -250,6 +250,44 @@ static int sparx5_port_hwtstamp_set(struct net_device *dev,
 	return sparx5_ptp_hwtstamp_set(sparx5_port, cfg, extack);
 }
 
+static int sparx5_port_change_mtu(struct net_device *dev, int new_mtu)
+{
+	struct sparx5_port *port = netdev_priv(dev);

drivers/net/ethernet/microchip/sparx5/sparx5_netdev.c static int sparx5_port_hwtstamp_set(struct net_device *dev, · line 265

Quoted 10 lines
drivers/net/ethernet/microchip/sparx5/sparx5_netdev.c +10 -0
+	struct sparx5 *sparx5 = port->sparx5;
+	bool resize = new_mtu > dev->mtu;
+	int err;
+
+	/* Register based extraction sizes its skbs from the MTU at the time
+	 * a frame is read, so it cannot handle frames queued before an MTU
+	 * decrease.
+	 */
+	if (sparx5->fdma_irq <= 0)
+		return -EOPNOTSUPP;

[Severity: Low]
Can resize be true here while fdma_resize() is actually shrinking the buffers?

resize only compares new_mtu against this port's own dev->mtu. The FDMA buffers, however, are shared and sized for the largest MTU of all ports. A failed shrink is allowed to leave them larger than needed.

Consider a port lowered from 9000 to 1500 where the smaller ring cannot be allocated. The code warns, keeps the larger buffers and commits dev->mtu = 1500.

If the port is later raised to an intermediate value such as 4000, resize is true. But sparx5_fdma_resize() only skips the reallocation when db_size == rx->fdma.db_size, and lan969x_fdma_resize() only when page_order == rx->page_order. Both will therefore try to shrink again.

If that allocation also fails, sparx5_port_change_mtu() returns the error and the MTU stays at 1500. The currently allocated buffers already fit 4000, though.

That contradicts the commit message, which says a failed shrink keeps the new MTU because the current buffers still fit all frames. No memory safety problem results, since the MAC max length is not touched on this path. It does make the increase fail for no real reason.

Should the failure only be treated as fatal when the required buffer size actually grows? For example, fdma_resize() could tell the caller whether it was growing or shrinking. Alternatively, an increase could skip the reallocation when the current buffers are already large enough.

[Severity: High]
This isn't a bug introduced by this patch, but this check also closes a heap overflow in register based extraction. The commit message presents it only as a limitation.

sparx5_xtr_grp() sizes the skb from the current MTU, then copies words until EOF with no bounds check:

drivers/net/ethernet/microchip/sparx5/sparx5_packet.c:sparx5_xtr_grp() {
    ...
	skb = netdev_alloc_skb(netdev, netdev->mtu + ETH_HLEN);
    ...
	rxbuf = (u32 *)skb->data;

	/* Now, pull frame data */
	while (!eof_flag) {
    ...
		default:
			*rxbuf = val;
			byte_cnt += 4;
			rxbuf++;
		}
    ...
}

Before this patch, MAC_MAXLEN_CFG stayed at ETH_MAXLEN (1518) from sparx5_port_init(), while the core allowed dev->mtu down to 68.

After "ip link set ethX mtu 68" on a register mode system, a normal 1518 byte frame from the wire would be written about 1.4 KB past the skb tailroom. That lands in skb_shared_info and adjacent heap memory, and skb_put() only checks the length after the copy.

This has been reachable since commit f3cad2611a77 ("net: sparx5: add hostmode with phylink support"). Could it be fixed in a separate patch with a Fixes: tag, so stable kernels get the fix too?

Bounding the copy loop in sparx5_xtr_grp() against the skb tailroom would also mean its safety no longer depends on the MTU being frozen in register mode.

drivers/net/ethernet/microchip/sparx5/sparx5_netdev.c static int sparx5_port_hwtstamp_set(struct net_device *dev, · line 271

Quoted 6 lines
drivers/net/ethernet/microchip/sparx5/sparx5_netdev.c +6 -0
+
+	/* Never let the MAC accept frames larger than the RX buffers */
+	if (!resize)
+		sparx5_port_max_len_set(sparx5, port, SPX5_HW_MTU(new_mtu));
+
+	err = sparx5->data->ops->fdma_resize(sparx5, port, new_mtu);

[Severity: Medium]
With max_mtu now at 13982, this can reach sparx5_fdma_resize() on Sparx5 with db_size = ALIGN(SPX5_HW_MTU(13982) + IFH_LEN * 4 + 2 * VLAN_HLEN, 128), which is 14080. Is GFP_ATOMIC the right flag for the RX ring allocation on this path?

sparx5_port_change_mtu()
sparx5_fdma_resize()

    sparx5_fdma_rx_alloc(rx_new)
      fdma_dcbs_init()
        sparx5_fdma_rx_dataptr_cb()   /* 64 DCBs * 15 DBs = 960 calls */
          __netdev_alloc_skb(rx->ndev, fdma->db_size, GFP_ATOMIC)

This runs in process context under RTNL and is allowed to sleep. Even so, it makes about 960 order-2 allocations (roughly 15 MB) with __GFP_HIGH, while the old ring is still allocated.

Can that drain the reserves needed by real atomic users, such as the NAPI RX refill?

The TX side then does:

sparx5_fdma_tx_alloc(&tx_new)
fdma_alloc_phys()

    kzalloc(fdma->size, GFP_KERNEL)

At the max MTU, fdma->size is around 904 KB. That is an order-8 physically contiguous allocation without __GFP_NOWARN.

On a fragmented system, can jumbo MTU changes fail unpredictably, with an allocation failure splat? The failure is handled and the MTU is left unchanged.

Would it help to pass a gfp argument down to the RX dataptr callback, so this path can use GFP_KERNEL?

Quoted 7 lines
drivers/net/ethernet/microchip/sparx5/sparx5_netdev.c +7 -0
+	if (err) {
+		if (resize)
+			return err;
+
+		/* The current, larger, buffers still fit all frames */
+		netdev_warn(dev, "Failed to resize FDMA buffers: %d\n", err);
+	}

[ ... ]

Quoted 6 lines
drivers/net/ethernet/microchip/sparx5/sparx5_netdev.c +1 -1
@@ -306,7 +345,14 @@ int sparx5_register_netdevs(struct sparx5 *sparx5)
 
 	for (portno = 0; portno < sparx5->data->consts->n_ports; portno++)
 		if (sparx5->ports[portno]) {
-			err = register_netdev(sparx5->ports[portno]->ndev);
+			struct net_device *ndev = sparx5->ports[portno]->ndev;

drivers/net/ethernet/microchip/sparx5/sparx5_netdev.c int sparx5_register_netdevs(struct sparx5 *sparx5) · line 358

Quoted 10 lines
drivers/net/ethernet/microchip/sparx5/sparx5_netdev.c +7 -0
+
+			/* The MTU can only be changed when FDMA is used */
+			if (sparx5->fdma_irq > 0)
+				ndev->max_mtu = SPX5_MAX_FRAME_LEN - ETH_HLEN -
+						ETH_FCS_LEN;
+
+			err = register_netdev(ndev);
 			if (err) {
 				dev_err(sparx5->dev,
 					"port: %02u: netdev registration failed\n",

[Severity: High]
This is a pre-existing issue, but if register_netdev() fails after earlier ports were registered, this returns without unregistering them. mchp_sparx5_probe() then skips sparx5_unregister_netdevs():

drivers/net/ethernet/microchip/sparx5/sparx5_main.c:mchp_sparx5_probe() {
    ...
	err = sparx5_register_netdevs(sparx5);
	if (err) {
		dev_err(sparx5->dev, "Failed to register net devices\n");
		goto cleanup_ptp;
	}
    ...
cleanup_netdevs:
	sparx5_unregister_netdevs(sparx5);
cleanup_ptp:
	sparx5_ptp_deinit(sparx5);
cleanup_frame_io:
	sparx5_frame_io_deinit(sparx5);
    ...
}

sparx5_frame_io_deinit() frees the FDMA buffers while those ports are still registered. When devres later releases the netdevs, devm_free_netdev() -> free_netdev() hits BUG_ON(dev->reg_state != NETREG_UNREGISTERED).

This patch also adds ndo_change_mtu to those registered ports. During that window, sparx5_fdma_resize() can run under RTNL while the error path runs sparx5_frame_io_deinit() without RTNL.

Could both end up freeing and swapping the sparx5->rx/tx buffers at the same time? Once deinit sets fdma_irq to -ENXIO, the new callback returns -EOPNOTSUPP, so the race is limited to that window.

Should sparx5_register_netdevs() unregister the ports it has already registered before returning the error?

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261120-sparx5-mtu-change-v3-0-55c272fe1acc%40microchip.com
author maintainer
#22 lore raw
Quoted 236 lines · On Mon, Nov 23, 2026 at 02:58:23PM +0000, [email protected] wrote:

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

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

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).

- [Medium] This patch raises max_mtu to 13982 in FDMA mode and adds the first caller of ops->fdma_resize (sparx5_port_change_mtu, process…
- [Low] The commit message says "The MTU of a port is currently fixed to 1500".
- [Low] SPX5_MAX_FRAME_LEN (sparx5_main.h) is defined as 14000 with the comment "Max frame length of the MACs", and the commit message says…
- [Low] net: sparx5: sparx5_port_change_mtu() fails MTU increase on failed buffer shrink

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but this patch silently closes it without saying so.
- [High] This problem wasn't introduced by this patch, but it is a crash.

--

net: sparx5/lan969x: add support for changing the MTU

This adds an ndo_change_mtu callback for sparx5 and lan969x ports. The callback programs the MAC maximum frame length, resizes the shared FDMA buffers, and updates the queue watermarks and the TAS guard band. MTU changes are only allowed when the FDMA is used, in which case max_mtu is raised to 14000 - ETH_HLEN - ETH_FCS_LEN and the top RMON histogram bucket is extended.

Quoted 3 lines

The MTU of a port is currently fixed to 1500, as the MAC maximum frame length, the FDMA buffers, the queue watermarks and the TAS guard band are all sized for a 1518 byte frame.

[Severity: Low]
Is this description accurate? The ports come from devm_alloc_etherdev_mqs() -> ether_setup(), so min_mtu is ETH_MIN_MTU (68) and max_mtu is ETH_DATA_LEN (1500).

Before this patch there was no ndo_change_mtu, so __netif_set_mtu() just did WRITE_ONCE(dev->mtu, new_mtu). Userspace could already lower the MTU.

After this patch, sparx5_port_change_mtu() in sparx5_netdev.c returns -EOPNOTSUPP for every MTU change when sparx5->fdma_irq <= 0. That includes decreases that used to succeed, for example on Sparx5 revision 0, where sparx5_frame_io_init() does not use the FDMA.

In FDMA mode, a decrease now also lowers MAC_MAXLEN_CFG. The port then drops hardware forwarded frames larger than the new MTU + 18 that it used to accept.

Rejecting the change in register mode looks correct, since lowering the MTU there was memory unsafe (see below). Could the commit message describe the previous behaviour, and mention that lowering the MTU in register mode now fails?

Quoted 18 lines
drivers/net/ethernet/microchip/sparx5/sparx5_ethtool.c +1 -1
index 2c6a5d3bc2bc6..af1fd32c6cef7 100644
@@ -809,7 +809,7 @@ static const struct ethtool_rmon_hist_range sparx5_rmon_ranges[] = {
 	{  256,   511 },
 	{  512,  1023 },
 	{ 1024,  1518 },
-	{ 1519, 10239 },
+	{ 1519, SPX5_MAX_FRAME_LEN + 2 * VLAN_HLEN },
 	{}
 };
 
drivers/net/ethernet/microchip/sparx5/sparx5_main.h +0 -0
index 64ace5c3af5ab..a94c025ba3c0d 100644

drivers/net/ethernet/microchip/sparx5/sparx5_main.h enum sparx5_feature { · line 114

Quoted 4 lines
drivers/net/ethernet/microchip/sparx5/sparx5_main.h +1 -0
@@ -111,6 +111,7 @@ enum sparx5_feature {
 #define SPX5_BUFFER_MEMORY     4194280 /* 22795 words * 184 bytes */
 #define SPX5_TAS_ENTRIES_PER_PORT 2 /* TAS lists per port */
 #define SPX5_HW_MTU(mtu)       ((mtu) + ETH_HLEN + ETH_FCS_LEN)
+#define SPX5_MAX_FRAME_LEN     14000 /* Max frame length of the MACs */

[Severity: Low]
Does 14000 include VLAN tags or not? The comment calls it the max frame length of the MACs, and the commit message says the MACs "support frames of up to 14000 bytes".

In sparx5_register_netdevs(), max_mtu becomes 14000 - ETH_HLEN - ETH_FCS_LEN = 13982, so sparx5_port_change_mtu() programs MAC_MAXLEN_CFG to 14000.

sparx5_port_max_tags_set() enables VLAN_LEN_AWR_ENA, which lets a tagged frame exceed MAXLEN by 4 bytes per tag. The new RMON bucket above also advertises { 1519, SPX5_MAX_FRAME_LEN + 2 * VLAN_HLEN }, which is 14008.

If 14000 is a hard hardware limit, should max_mtu also subtract 2 * VLAN_HLEN, and should the RMON range stop at 14000? If it is not, should the comment and the commit message be updated?

Quoted 14 lines
drivers/net/ethernet/microchip/sparx5/sparx5_main.h +0 -0
 #define XTR_QUEUE     0
 #define INJ_QUEUE     0
drivers/net/ethernet/microchip/sparx5/sparx5_netdev.c +3 -0
index b3725cf2dd44c..095925752f5f6 100644
@@ -250,6 +250,44 @@ static int sparx5_port_hwtstamp_set(struct net_device *dev,
 	return sparx5_ptp_hwtstamp_set(sparx5_port, cfg, extack);
 }
 
+static int sparx5_port_change_mtu(struct net_device *dev, int new_mtu)
+{
+	struct sparx5_port *port = netdev_priv(dev);

drivers/net/ethernet/microchip/sparx5/sparx5_netdev.c static int sparx5_port_hwtstamp_set(struct net_device *dev, · line 265

Quoted 10 lines
drivers/net/ethernet/microchip/sparx5/sparx5_netdev.c +10 -0
+	struct sparx5 *sparx5 = port->sparx5;
+	bool resize = new_mtu > dev->mtu;
+	int err;
+
+	/* Register based extraction sizes its skbs from the MTU at the time
+	 * a frame is read, so it cannot handle frames queued before an MTU
+	 * decrease.
+	 */
+	if (sparx5->fdma_irq <= 0)
+		return -EOPNOTSUPP;

[Severity: Low]
Can resize be true here while fdma_resize() is actually shrinking the buffers?

resize only compares new_mtu against this port's own dev->mtu. The FDMA buffers, however, are shared and sized for the largest MTU of all ports. A failed shrink is allowed to leave them larger than needed.

Consider a port lowered from 9000 to 1500 where the smaller ring cannot be allocated. The code warns, keeps the larger buffers and commits dev->mtu = 1500.

If the port is later raised to an intermediate value such as 4000, resize is true. But sparx5_fdma_resize() only skips the reallocation when db_size == rx->fdma.db_size, and lan969x_fdma_resize() only when page_order == rx->page_order. Both will therefore try to shrink again.

If that allocation also fails, sparx5_port_change_mtu() returns the error and the MTU stays at 1500. The currently allocated buffers already fit 4000, though.

That contradicts the commit message, which says a failed shrink keeps the new MTU because the current buffers still fit all frames. No memory safety problem results, since the MAC max length is not touched on this path. It does make the increase fail for no real reason.

Should the failure only be treated as fatal when the required buffer size actually grows? For example, fdma_resize() could tell the caller whether it was growing or shrinking. Alternatively, an increase could skip the reallocation when the current buffers are already large enough.

[Severity: High]
This isn't a bug introduced by this patch, but this check also closes a heap overflow in register based extraction. The commit message presents it only as a limitation.

sparx5_xtr_grp() sizes the skb from the current MTU, then copies words until EOF with no bounds check:

drivers/net/ethernet/microchip/sparx5/sparx5_packet.c:sparx5_xtr_grp() {
    ...
	skb = netdev_alloc_skb(netdev, netdev->mtu + ETH_HLEN);
    ...
	rxbuf = (u32 *)skb->data;

	/* Now, pull frame data */
	while (!eof_flag) {
    ...
		default:
			*rxbuf = val;
			byte_cnt += 4;
			rxbuf++;
		}
    ...
}

Before this patch, MAC_MAXLEN_CFG stayed at ETH_MAXLEN (1518) from sparx5_port_init(), while the core allowed dev->mtu down to 68.

After "ip link set ethX mtu 68" on a register mode system, a normal 1518 byte frame from the wire would be written about 1.4 KB past the skb tailroom. That lands in skb_shared_info and adjacent heap memory, and skb_put() only checks the length after the copy.

This has been reachable since commit f3cad2611a77 ("net: sparx5: add hostmode with phylink support"). Could it be fixed in a separate patch with a Fixes: tag, so stable kernels get the fix too?

Bounding the copy loop in sparx5_xtr_grp() against the skb tailroom would also mean its safety no longer depends on the MTU being frozen in register mode.

drivers/net/ethernet/microchip/sparx5/sparx5_netdev.c static int sparx5_port_hwtstamp_set(struct net_device *dev, · line 271

Quoted 6 lines
drivers/net/ethernet/microchip/sparx5/sparx5_netdev.c +6 -0
+
+	/* Never let the MAC accept frames larger than the RX buffers */
+	if (!resize)
+		sparx5_port_max_len_set(sparx5, port, SPX5_HW_MTU(new_mtu));
+
+	err = sparx5->data->ops->fdma_resize(sparx5, port, new_mtu);

[Severity: Medium]
With max_mtu now at 13982, this can reach sparx5_fdma_resize() on Sparx5 with db_size = ALIGN(SPX5_HW_MTU(13982) + IFH_LEN * 4 + 2 * VLAN_HLEN, 128), which is 14080. Is GFP_ATOMIC the right flag for the RX ring allocation on this path?

sparx5_port_change_mtu()
sparx5_fdma_resize()

    sparx5_fdma_rx_alloc(rx_new)
      fdma_dcbs_init()
        sparx5_fdma_rx_dataptr_cb()   /* 64 DCBs * 15 DBs = 960 calls */
          __netdev_alloc_skb(rx->ndev, fdma->db_size, GFP_ATOMIC)

This runs in process context under RTNL and is allowed to sleep. Even so, it makes about 960 order-2 allocations (roughly 15 MB) with __GFP_HIGH, while the old ring is still allocated.

Can that drain the reserves needed by real atomic users, such as the NAPI RX refill?

The TX side then does:

sparx5_fdma_tx_alloc(&tx_new)
fdma_alloc_phys()

    kzalloc(fdma->size, GFP_KERNEL)

At the max MTU, fdma->size is around 904 KB. That is an order-8 physically contiguous allocation without __GFP_NOWARN.

Quoted 6 lines

On a fragmented system, can jumbo MTU changes fail unpredictably, with an allocation failure splat? The failure is handled and the MTU is left unchanged.

Would it help to pass a gfp argument down to the RX dataptr callback, so this path can use GFP_KERNEL?

The GFP_ATOMIC part was already dismissed on v2 [1]. The TX ring allocation can sleep, and if it fails the MTU is left unchanged.

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

Quoted 67 lines
Quoted 8 lines
drivers/net/ethernet/microchip/sparx5/sparx5_netdev.c +7 -0
+	if (err) {
+		if (resize)
+			return err;
+
+		/* The current, larger, buffers still fit all frames */
+		netdev_warn(dev, "Failed to resize FDMA buffers: %d\n", err);
+	}

[ ... ]

Quoted 6 lines
drivers/net/ethernet/microchip/sparx5/sparx5_netdev.c +1 -1
@@ -306,7 +345,14 @@ int sparx5_register_netdevs(struct sparx5 *sparx5)
 
 	for (portno = 0; portno < sparx5->data->consts->n_ports; portno++)
 		if (sparx5->ports[portno]) {
-			err = register_netdev(sparx5->ports[portno]->ndev);
+			struct net_device *ndev = sparx5->ports[portno]->ndev;

drivers/net/ethernet/microchip/sparx5/sparx5_netdev.c int sparx5_register_netdevs(struct sparx5 *sparx5) · line 358

Quoted 10 lines
drivers/net/ethernet/microchip/sparx5/sparx5_netdev.c +7 -0
+
+			/* The MTU can only be changed when FDMA is used */
+			if (sparx5->fdma_irq > 0)
+				ndev->max_mtu = SPX5_MAX_FRAME_LEN - ETH_HLEN -
+						ETH_FCS_LEN;
+
+			err = register_netdev(ndev);
 			if (err) {
 				dev_err(sparx5->dev,
 					"port: %02u: netdev registration failed\n",

[Severity: High]
This is a pre-existing issue, but if register_netdev() fails after earlier ports were registered, this returns without unregistering them. mchp_sparx5_probe() then skips sparx5_unregister_netdevs():

drivers/net/ethernet/microchip/sparx5/sparx5_main.c:mchp_sparx5_probe() {
    ...
	err = sparx5_register_netdevs(sparx5);
	if (err) {
		dev_err(sparx5->dev, "Failed to register net devices\n");
		goto cleanup_ptp;
	}
    ...
cleanup_netdevs:
	sparx5_unregister_netdevs(sparx5);
cleanup_ptp:
	sparx5_ptp_deinit(sparx5);
cleanup_frame_io:
	sparx5_frame_io_deinit(sparx5);
    ...
}

sparx5_frame_io_deinit() frees the FDMA buffers while those ports are still registered. When devres later releases the netdevs, devm_free_netdev() -> free_netdev() hits BUG_ON(dev->reg_state != NETREG_UNREGISTERED).

This patch also adds ndo_change_mtu to those registered ports. During that window, sparx5_fdma_resize() can run under RTNL while the error path runs sparx5_frame_io_deinit() without RTNL.

Could both end up freeing and swapping the sparx5->rx/tx buffers at the same time? Once deinit sets fdma_irq to -ENXIO, the new callback returns -EOPNOTSUPP, so the race is limited to that window.

Should sparx5_register_netdevs() unregister the ports it has already registered before returning the error?

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261120-sparx5-mtu-change-v3-0-55c272fe1acc%40microchip.com
#23 lore raw
In reply to Daniel Machon
This series was applied to netdev/net-next.git (main) by Willem de Bruijn <[email protected]>:

Hello:

This series was applied to netdev/net-next.git (main) by Willem de Bruijn <[email protected]>:

Quoted 11 lines · On Fri, 20 Nov 2026 12:56:57 +0100 you wrote:

This series adds support for changing the MTU on Sparx5 and lan969x.

Currently, there is no ndo_change_mtu callback, and the MAC maximum frame length, the FDMA buffers, the queue watermarks and the TAS guard band are all fixed for a 1518 byte frame. This series makes them follow the MTU, and sets the maximum MTU from the 14000 byte frame limit of the MACs. As the FDMA is shared by all ports, new buffers are allocated while the FDMA keeps running, and the FDMA is only stopped briefly to replace them. Details are in the commits.

[...]

Here is the summary with links:
- [net-next,v3,1/9] net: sparx5/lan969x: stop the FDMA channels properly

    https://git.kernel.org/netdev/net-next/c/0198a175917a
  - [net-next,v3,2/9] net: sparx5/lan969x: free the FDMA buffers on init errors and deinit
    https://git.kernel.org/netdev/net-next/c/74dc2d0d4706
  - [net-next,v3,3/9] net: sparx5/lan969x: add a helper for setting the MAC max frame length
    https://git.kernel.org/netdev/net-next/c/ff9afebc9cb1
  - [net-next,v3,4/9] net: sparx5/lan969x: pass the FDMA rings to the init and alloc functions
    https://git.kernel.org/netdev/net-next/c/91df1aa502b1
  - [net-next,v3,5/9] net: sparx5/lan969x: size the FDMA buffers from the port MTUs
    https://git.kernel.org/netdev/net-next/c/3a13261eaf43
  - [net-next,v3,6/9] net: sparx5/lan969x: add support for resizing the FDMA buffers
    https://git.kernel.org/netdev/net-next/c/c18f9b51d8fe
  - [net-next,v3,7/9] net: sparx5/lan969x: scale the queue watermarks with the MTU
    https://git.kernel.org/netdev/net-next/c/c634ae1440b7
  - [net-next,v3,8/9] net: sparx5/lan969x: scale the TAS guard band with the MTU
    https://git.kernel.org/netdev/net-next/c/f86caad45529
  - [net-next,v3,9/9] net: sparx5/lan969x: add support for changing the MTU
    https://git.kernel.org/netdev/net-next/c/a1334b92fb6b

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html