Skip to contentSkip to conversation

net: macb: implement context swapping

PATCH Superseded Théo Lebrun 3 participants 26 messages Aug 12 – Sep 17

Subsystems (3)

v9 of 11 Superseded 7 of 17 applied Latest is v11 →

lore
Share & tools
lore

Théo Lebrun

author maintainer
#0 lore raw

v9 cover net: macb: implement context swapping

MACB has a pretty primitive approach to buffer management. They are all stored in struct macb *bp. On operations that require buffer realloc (set_ringparam & change_mtu at the moment), the only option is to close the interface, change our global state and re-open the interface.

Two issues:
- It doesn't fly on memory pressured systems; we free our precious buffers and don't manage to reallocate fully, meaning our machine just lost its network access.
- Anecdotally, it is pretty slow because it implies a full PHY reinit.

Instead, we shall:
- allocate a new context (including buffers) first
- if it fails, early return without any impact to the interface
- stop interface
- update global state (bp, netdev, etc)
- pass newly allocated buffer pointers to the hardware
- start interface
- free old context

This is what we implement here. Both .set_ringparam() and .ndo_change_mtu() are covered by this series. In the future, at least .set_channels() [0], XDP [1] and XSK [2] would benefit.

---
What your state of mind on this series? Past three iterations were about fixing Sashiko edge-cases.

The most important fix was netconsole related and only lightly related to this series. For the rest, it is nice that they are fixed but overall I couldn't find a way to reproduce any of them, seeing how unlikely they all are.

Can we get this in net-next? Goal being to work on XDP [1] & XSK [2] during the next kernel cycle, bringing those long awaited features into MACB.

Thanks,
Have a nice day,
Théo

[0]: https://lore.kernel.org/netdev/[email protected]/
[1]: https://lore.kernel.org/netdev/[email protected]/
[2]: https://lore.kernel.org/netdev/[email protected]/
[3]: https://lore.kernel.org/all/[email protected]/

---
Changes in v9:
- Drop unused netdev stack variable in macb_interrupt_misc().
- Rebase onto latest net-next/main (ac155a26750a), nothing to report.
- Link to v8: https://patch.msgid.link/[email protected]

Changes in v8:
- Avoid growing existing race condition window inbetween macb_tx_error_task() and macb_interrupt() by moving printks.
- On swap end, don't start tx queues if link-down.
- On swap end, wake tx queues instead of only starting them.
- Rebase onto latest net-next/main (a23b36233d41), nothing to report.
- Link to v7: https://patch.msgid.link/[email protected]

Changes in v7:
- queue->tx_{head,tail} got renamed: fixup queue->tx_ptr_lock field comment and netdev_dbg() call in macb_start_xmit().
- Ensure bp->configured_{rx,tx}_ring_size have correct values in the EMAC case so that get_ringparam doesn't lie.
- Refuse set_ringparam on EMAC, which is invalid (sizes are hardcoded).
- Fix whitespace issue in "change function signatures to take contexts".
- Fix rebase mistake: macb_tx_unmap() signature change moved from patch "change function signatures to take contexts" to "change caps helpers signatures".
- Patch "move printk() calls out of bp->lock critical section":
- Undrop netdev_vdbg() in macb_tx_error_task(); let's ignore the race for debug printk calls.
- Move netdev_err() from macb_interrupt_misc() into macb_interrupt()

    so it can live outside the bp->lock critical section.
  - Add details to commit message.
- Patch "use context swapping in .set_ringparam()":
  - Re-schedule LPI after swap with delay=1s. Pick 1s so that if
    link-up -> swap instantly, we still respect the IEEE spec.

- Add bp->link_up set under bp->mac_cfg_lock. netif_carrier_ok() bit
is unsafe because not set inside a critical section we can enter.
- Drop mac_cfg_lock from macb_hresp_error_task(); made useless by
above.
- Move macb_halt_tx() out of bp->lock to avoid 14ms latency spike possibility. Safe to do so.
- Patch "use context swapping in .ndo_change_mtu()":
- Clarify commit message about EMAC.
- Rebase onto latest net-next/main (69963a0678a3), nothing to report.
- Link to v6: https://patch.msgid.link/[email protected]

Changes in v6:
- Non Sashiko based:
- Take 3x Reviewed-By: Nicolai.
- Silence checkpatch warnings in [02/15] about printk(..., __func__).
- Rebase to latest net-next/main (a5c6ae8de11e), nothing to report.
- Fix netconsole/netpoll deadlock coming from macb_tx_error_task()
calling printk() inside the bp->lock critical section.
- Move at91ether synchronize_irq() on close to protect more kfree().
- Clarify commit message for [11/15]: functions operating on the
interface have no risk of using the wrong interface.
- On swap end, only re-enable the interface if !netif_carrier_ok().
- macb_hresp_error_task() grabs bp->mac_cfg_lock as it toggles the
carrier state, to avoid race with context swap.
- On swap end, move netif_tx_start_all_queues() into bp->mac_cfg_lock.
- Link to v5: https://patch.msgid.link/[email protected]

Changes in v5:
- Fix build on [PATCH 02/15]; bp->dev got renamed to bp->netdev.
- Fix one word in commit message of [PATCH 07/15].
- Move bp->ctx->info assignment in at91ether_open() from [PATCH 12/15]
to [PATCH 09/15].
- In [PATCH 14/15], reset DQL (dynamic queue limits) after tx disable,
not before.
- Take 7 Reviewed-by: Nicolai (weirdly not all detected by b4).
- Rebase on latest net-next/main (89d8006259b8), nothing to report.
- Link to v4: https://patch.msgid.link/[email protected]

Changes in v4:
- Disable tx_error_task then disable NAPI because error task does a napi_disable() which deadlocks if NAPI is already disabled.
- Disable NAPI then disable tx_lpi_work, because NAPI might re-arm the latter.
- Last iteration did mask-irqs then disable-and-wait-for-bh then disable-hw. This is flawed because BH rearm IRQs once done. We don't
have a good way to signal to them they shouldn't do so (we don't want
to lock from NAPI context). So instead we add a bp->ctx_swap flag, shielded by bp->lock, to indicate to our IRQ handler to ignore IRQs
and self-disarm.
- at91ether_close(): synchronize_irq() before freeing context.
- macb_interrupt(): drop double ISR read (outside & inside bp->lock).
- Rebase on latest net-next/main (f6f3b36c15ed). macb_free() since
commit 27f575836cfe ("net: macb: drop in-flight Tx SKBs on close")
needs access to queue stats => add them to macb_info.
- Take trailers.
- I did NOT init ctx->rx_ring_size/tx_ring_size/rx_buffer_size from at91ether_open(), as recommended by the netdev LLM. They are not the first fields in MACB that are present in both instances and that are uninitialised in one case. AT91 is almost a different driver. [3]
- Link to v3: https://patch.msgid.link/[email protected]

Changes in v3:
- Use const struct macb_info *info instead of bare u32 caps as
helper arguments, for type safety.
- macb_interrupt(): the pre-lock readl(ISR) to detect spurious
interrupts is only done if CLEAR_ON_WRITE.
- Don't forget allocating context in at91ether_open().
- swap:
- Refuse swap for EMAC HW; it would crash because codepaths are so different.
- Grab new bp->mac_cfg_lock to serialise with phylink MAC callbacks.
We cannot rely on phydev->lock because it isn't present in the SFP
or fixed-link cases. We also want to avoid phylink_stop() which triggers a slow PHY retrain.
- swap start:
- We used to do disable-irqs-and-hw then drain-all-bh-features, but
then HW might be raced against. Instead we disable-irqs then drain-all-bh then disable-hw which means at disable-hw step no BH context can be active.
- Use macb_halt_tx() helper to properly stop HW.
- Disable BH features before netif_tx_disable() call to avoid queue wakeup races.
- Use macb_queue_isr_clear() helper instead of manual if-then-writel.
- swap end:
- Grab bp->lock for the hardware reinit sequence composed of DMACFG
and NCR writes.
- Drop now useless EMAC check (we refuse EMAC HW before swapping).
- nits:
- New patch to rename macb_{alloc,free}_consistent() which don't only allocate consistent buffers since a long time ago.
- Fix the start_xmit verbose netdev_vdbg() format string from %hu to %u because the queue index type changed.
- Strong commit reword from "unify struct macb * naming convention"
to "unify variable naming convention in at91ether functions" which
was underselling the changes.
- Rebase upon latest net-next/main (1c664ec4b9ea).
- Link to v2: https://patch.msgid.link/[email protected]

Changes in v2:
- Patch "add subset of struct macb to struct macb_context" was
messed up. It contained much more than what the name implied. Split
into three commits (I caused trouble by rebase reordering).
- Fix tieoff; V1 allocated it without initialisation.
- Fix NULL pointer dereference on context in mab_get_regs() and macb_get_ringparam() when interface is offline.
- Patch "unify device pointer naming convention":
- Fix build issue when CONFIG_NETCONSOLE=y.
- Rename struct net_device *dev to netdev in macb.h.
- Rename struct phy_device *phy to phydev in macb_main.c.
- On swap, call netdev_tx_reset_queue() to reset all DQL counters.
- At end of swap, add missing kfree(old_ctx).
- During HW disabling in swap, grab bp->lock to protect against IRQ handler.
- On swap, cancel the three BH features MACB has:
bp->hresp_err_bh_work, bp->tx_lpi_work and queue->tx_error_task.
- On swap, call macb_configure_dma() which writes buffer size to
hardware registers. This is important because the change_mtu codepath changes the buffer size.
- Rebase onto latest net-next/main (58dd34dbd5b0) & resolve conflicts.
- Link to v1: https://patch.msgid.link/[email protected]

To: Théo Lebrun <[email protected]>
To: Conor Dooley <[email protected]>
To: Andrew Lunn <[email protected]>
To: "David S. Miller" <[email protected]>
To: Eric Dumazet <[email protected]>
To: Jakub Kicinski <[email protected]>
To: Paolo Abeni <[email protected]>
To: Richard Cochran <[email protected]>
To: Russell King <[email protected]>

Cc Nicolas Ferre <[email protected]>
Cc Claudiu Beznea <[email protected]>
Cc Paolo Valerio <[email protected]>
Cc Nicolai Buchwitz <[email protected]>
Cc Vladimir Kondratiev <[email protected]>
Cc Gregory CLEMENT <[email protected]>
Cc Benoît Monin <[email protected]>
Cc Tawfik Bayouk <[email protected]>
Cc Thomas Petazzoni <[email protected]>
Cc Maxime Chevallier <[email protected]>

---
Théo Lebrun (17):
net: macb: drop "consistent" from alloc/free function names
net: macb: unify device pointer naming convention
net: macb: unify variable naming convention in at91ether functions
net: macb: unify queue index variable naming convention and types
net: macb: enforce reverse christmas tree (RCT) convention
net: macb: allocate tieoff descriptor once across device lifetime
net: macb: refuse set_ringparam on EMAC
net: macb: introduce macb_context struct for buffer management
net: macb: avoid macb_init_rx_buffer_size() modifying state
net: macb: make struct macb subset reachable from macb_context struct
net: macb: change caps helpers signatures
net: macb: change function signatures to take contexts
net: macb: introduce macb_context_alloc() helper
net: macb: move printk() calls out of bp->lock critical section
net: macb: read ISR inside bp->lock critical section
net: macb: use context swapping in .set_ringparam()
net: macb: use context swapping in .ndo_change_mtu()

 drivers/net/ethernet/cadence/macb.h      |  134 +-
 drivers/net/ethernet/cadence/macb_main.c | 1963 ++++++++++++++++++------------
 drivers/net/ethernet/cadence/macb_pci.c  |   46 +-
 drivers/net/ethernet/cadence/macb_ptp.c  |   26 +-
 4 files changed, 1292 insertions(+), 877 deletions(-)

---
base-commit: f0ad822788b076e5f62ada627fc2a993a51060f3
change-id: 20260401-macb-context-bd0caf20414d

Best regards,

Signature · 1 line
Théo Lebrun <[email protected]>
author maintainer
#1 lore raw

PATCH v9 1/17 net: macb: drop "consistent" from alloc/free function names

Since commit 4df95131ea80 ("net/macb: change RX path for GEM") those functions have not been only allocating or freeing consistent memory mappings.

Rename from macb_alloc_consistent() to macb_alloc() and
from macb_free_consistent() to macb_free().

---
 drivers/net/ethernet/cadence/macb_main.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/net/ethernet/cadence/macb_main.c +6 -6
index d394f1f43b68..ae7d4bb2706b 100644
@@ -2646,7 +2646,7 @@ static unsigned int macb_rx_ring_size_per_queue(struct macb *bp)
 	return macb_dma_desc_get_size(bp) * bp->rx_ring_size + bp->rx_bd_rd_prefetch;
 }
 
-static void macb_free_consistent(struct macb *bp)
+static void macb_free(struct macb *bp)
 {
 	struct device *dev = &bp->pdev->dev;
 	struct macb_queue *queue;
@@ -2728,7 +2728,7 @@ static int macb_alloc_rx_buffers(struct macb *bp)
 	return 0;
 }
 
-static int macb_alloc_consistent(struct macb *bp)
+static int macb_alloc(struct macb *bp)
 {
 	struct device *dev = &bp->pdev->dev;
 	dma_addr_t tx_dma, rx_dma;
@@ -2786,7 +2786,7 @@ static int macb_alloc_consistent(struct macb *bp)
 	return 0;
 
 out_err:
-	macb_free_consistent(bp);
+	macb_free(bp);
 	return -ENOMEM;
 }
 
@@ -3174,7 +3174,7 @@ static int macb_open(struct net_device *dev)
 	/* RX buffers initialization */
 	macb_init_rx_buffer_size(bp, bufsz);
 
-	err = macb_alloc_consistent(bp);
+	err = macb_alloc(bp);
 	if (err) {
 		netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
 			   err);
@@ -3219,7 +3219,7 @@ static int macb_open(struct net_device *dev)
 		napi_disable(&queue->napi_rx);
 		napi_disable(&queue->napi_tx);
 	}
-	macb_free_consistent(bp);
+	macb_free(bp);
 pm_exit:
 	pm_runtime_put_sync(&bp->pdev->dev);
 	return err;
@@ -3252,7 +3252,7 @@ static int macb_close(struct net_device *dev)
 	netif_carrier_off(dev);
 	spin_unlock_irqrestore(&bp->lock, flags);
 
-	macb_free_consistent(bp);
+	macb_free(bp);
 
 	if (bp->ptp_info)
 		bp->ptp_info->ptp_remove(dev);
Signature · 1 line
2.55.0
author maintainer
#2 lore raw

PATCH v9 2/17 net: macb: unify device pointer naming convention

In reply to Théo Lebrun

Here are all device pointer variable permutations inside MACB:

struct device *dev;
struct net_device *dev;
struct net_device *ndev;
struct net_device *netdev;
struct pci_dev *pdev; // inside macb_pci.c
struct phy_device *phy;
struct phy_device *phydev;
struct platform_device *pdev;
struct platform_device *plat_dev; // inside macb_pci.c

Unify to this convention:

struct device *dev;
struct net_device *netdev;
struct pci_dev *pci;
struct phy_device *phydev;
struct platform_device *pdev;

Ensure nothing slipped through using ctags tooling:

⟩ ctags -o - --kinds-c='{local}{member}{parameter}' \
    --fields='{typeref}' drivers/net/ethernet/cadence/* | \
  awk -F"\t" '
    $NF~/struct:.*(device|dev) / {print $NF, $1}' | \

sort -u
typeref:struct:device * dev
typeref:struct:in_device * idev // ignored
typeref:struct:net_device * netdev
typeref:struct:pci_dev * pci
typeref:struct:phy_device * phydev
typeref:struct:platform_device * pdev

Also fix some printk() calls to use __func__ instead of hardcoding. This silences some checkpatch.pl warnings and doesn't deserve a separate commit.

---
 drivers/net/ethernet/cadence/macb.h      |  20 +-
 drivers/net/ethernet/cadence/macb_main.c | 642 ++++++++++++++++---------------
 drivers/net/ethernet/cadence/macb_pci.c  |  46 +--
 drivers/net/ethernet/cadence/macb_ptp.c  |  18 +-
 4 files changed, 365 insertions(+), 361 deletions(-)
drivers/net/ethernet/cadence/macb.h +10 -10
index a11052565436..1e1f52285a39 100644
@@ -1201,11 +1201,11 @@ struct macb_or_gem_ops {
 
 /* MACB-PTP interface: adapt to platform needs. */
 struct macb_ptp_info {
-	void (*ptp_init)(struct net_device *ndev);
-	void (*ptp_remove)(struct net_device *ndev);
+	void (*ptp_init)(struct net_device *netdev);
+	void (*ptp_remove)(struct net_device *netdev);
 	s32 (*get_ptp_max_adj)(void);
 	unsigned int (*get_tsu_rate)(struct macb *bp);
-	int (*get_ts_info)(struct net_device *dev,
+	int (*get_ts_info)(struct net_device *netdev,
 			   struct kernel_ethtool_ts_info *info);
 	int (*get_hwtst)(struct net_device *netdev,
 			 struct kernel_hwtstamp_config *tstamp_config);
@@ -1320,7 +1320,7 @@ struct macb {
 	struct clk		*tx_clk;
 	struct clk		*rx_clk;
 	struct clk		*tsu_clk;
-	struct net_device	*dev;
+	struct net_device	*netdev;
 	/* Protects hw_stats and ethtool_stats */
 	spinlock_t		stats_lock;
 	union {
@@ -1400,8 +1400,8 @@ enum macb_bd_control {
 	TSTAMP_ALL_FRAMES,
 };
 
-void gem_ptp_init(struct net_device *ndev);
-void gem_ptp_remove(struct net_device *ndev);
+void gem_ptp_init(struct net_device *netdev);
+void gem_ptp_remove(struct net_device *netdev);
 void gem_ptp_txstamp(struct macb *bp, struct sk_buff *skb, struct macb_dma_desc *desc);
 void gem_ptp_rxstamp(struct macb *bp, struct sk_buff *skb, struct macb_dma_desc *desc);
 static inline void gem_ptp_do_txstamp(struct macb *bp, struct sk_buff *skb, struct macb_dma_desc *desc)
@@ -1420,14 +1420,14 @@ static inline void gem_ptp_do_rxstamp(struct macb *bp, struct sk_buff *skb, stru
 	gem_ptp_rxstamp(bp, skb, desc);
 }
 
-int gem_get_hwtst(struct net_device *dev,
+int gem_get_hwtst(struct net_device *netdev,
 		  struct kernel_hwtstamp_config *tstamp_config);
-int gem_set_hwtst(struct net_device *dev,
+int gem_set_hwtst(struct net_device *netdev,
 		  struct kernel_hwtstamp_config *tstamp_config,
 		  struct netlink_ext_ack *extack);
 #else
-static inline void gem_ptp_init(struct net_device *ndev) { }
-static inline void gem_ptp_remove(struct net_device *ndev) { }
+static inline void gem_ptp_init(struct net_device *netdev) { }
+static inline void gem_ptp_remove(struct net_device *netdev) { }
 
 static inline void gem_ptp_do_txstamp(struct macb *bp, struct sk_buff *skb, struct macb_dma_desc *desc) { }
 static inline void gem_ptp_do_rxstamp(struct macb *bp, struct sk_buff *skb, struct macb_dma_desc *desc) { }
drivers/net/ethernet/cadence/macb_main.c +323 -319

1,880 lines not on this page · raw message

drivers/net/ethernet/cadence/macb_pci.c +23 -23

109 lines not on this page · raw message

drivers/net/ethernet/cadence/macb_ptp.c +9 -9
index d91f7b1aa39c..e5195d7dac1d 100644
@@ -324,9 +324,9 @@ void gem_ptp_txstamp(struct macb *bp, struct sk_buff *skb,
 	skb_tstamp_tx(skb, &shhwtstamps);
 }
 
-void gem_ptp_init(struct net_device *dev)
+void gem_ptp_init(struct net_device *netdev)
 {
-	struct macb *bp = netdev_priv(dev);
+	struct macb *bp = netdev_priv(netdev);
 
 	bp->ptp_clock_info = gem_ptp_caps_template;
 
@@ -334,7 +334,7 @@ void gem_ptp_init(struct net_device *dev)
 	bp->tsu_rate = bp->ptp_info->get_tsu_rate(bp);
 	bp->ptp_clock_info.max_adj = bp->ptp_info->get_ptp_max_adj();
 	gem_ptp_init_timer(bp);
-	bp->ptp_clock = ptp_clock_register(&bp->ptp_clock_info, &dev->dev);
+	bp->ptp_clock = ptp_clock_register(&bp->ptp_clock_info, &netdev->dev);
 	if (IS_ERR(bp->ptp_clock)) {
 		pr_err("ptp clock register failed: %ld\n",
 			PTR_ERR(bp->ptp_clock));
@@ -353,9 +353,9 @@ void gem_ptp_init(struct net_device *dev)
 		 GEM_PTP_TIMER_NAME);
 }
 
-void gem_ptp_remove(struct net_device *ndev)
+void gem_ptp_remove(struct net_device *netdev)
 {
-	struct macb *bp = netdev_priv(ndev);
+	struct macb *bp = netdev_priv(netdev);
 
 	if (bp->ptp_clock) {
 		ptp_clock_unregister(bp->ptp_clock);
@@ -378,10 +378,10 @@ static int gem_ptp_set_ts_mode(struct macb *bp,
 	return 0;
 }
 
-int gem_get_hwtst(struct net_device *dev,
+int gem_get_hwtst(struct net_device *netdev,
 		  struct kernel_hwtstamp_config *tstamp_config)
 {
-	struct macb *bp = netdev_priv(dev);
+	struct macb *bp = netdev_priv(netdev);
 
 	*tstamp_config = bp->tstamp_config;
 	if (!macb_dma_ptp(bp))
@@ -402,13 +402,13 @@ static void gem_ptp_set_one_step_sync(struct macb *bp, u8 enable)
 		macb_writel(bp, NCR, reg_val & ~MACB_BIT(OSSMODE));
 }
 
-int gem_set_hwtst(struct net_device *dev,
+int gem_set_hwtst(struct net_device *netdev,
 		  struct kernel_hwtstamp_config *tstamp_config,
 		  struct netlink_ext_ack *extack)
 {
 	enum macb_bd_control tx_bd_control = TSTAMP_DISABLED;
 	enum macb_bd_control rx_bd_control = TSTAMP_DISABLED;
-	struct macb *bp = netdev_priv(dev);
+	struct macb *bp = netdev_priv(netdev);
 	u32 regval;
 
 	if (!macb_dma_ptp(bp))
Signature · 1 line
2.55.0
author maintainer
#3 lore raw

PATCH v9 3/17 net: macb: unify variable naming convention in at91ether functions

In reply to Théo Lebrun

Follow MACB naming convention throughout on two aspects:
- Always name struct macb *bp rather than lp.
- Always name struct macb_queue *queue rather than q.

The latter is to reserve q for queue indexes.

---
 drivers/net/ethernet/cadence/macb_main.c | 176 ++++++++++++++++---------------
 1 file changed, 91 insertions(+), 85 deletions(-)
drivers/net/ethernet/cadence/macb_main.c +91 -85
index 3ae76d1d2a0d..e01a5e0bf481 100644
@@ -4965,71 +4965,72 @@ static const struct macb_usrio_config at91_default_usrio = {
 
 static struct sifive_fu540_macb_mgmt *mgmt;
 
-static int at91ether_alloc_coherent(struct macb *lp)
+static int at91ether_alloc_coherent(struct macb *bp)
 {
-	struct macb_queue *q = &lp->queues[0];
+	struct macb_queue *queue = &bp->queues[0];
 
-	q->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
-					 (AT91ETHER_MAX_RX_DESCR *
-					  macb_dma_desc_get_size(lp)),
-					 &q->rx_ring_dma, GFP_KERNEL);
-	if (!q->rx_ring)
+	queue->rx_ring = dma_alloc_coherent(&bp->pdev->dev,
+					    (AT91ETHER_MAX_RX_DESCR *
+					     macb_dma_desc_get_size(bp)),
+					    &queue->rx_ring_dma, GFP_KERNEL);
+	if (!queue->rx_ring)
 		return -ENOMEM;
 
-	q->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
-					    AT91ETHER_MAX_RX_DESCR *
-					    AT91ETHER_MAX_RBUFF_SZ,
-					    &q->rx_buffers_dma, GFP_KERNEL);
-	if (!q->rx_buffers) {
-		dma_free_coherent(&lp->pdev->dev,
+	queue->rx_buffers = dma_alloc_coherent(&bp->pdev->dev,
+					       AT91ETHER_MAX_RX_DESCR *
+					       AT91ETHER_MAX_RBUFF_SZ,
+					       &queue->rx_buffers_dma,
+					       GFP_KERNEL);
+	if (!queue->rx_buffers) {
+		dma_free_coherent(&bp->pdev->dev,
 				  AT91ETHER_MAX_RX_DESCR *
-				  macb_dma_desc_get_size(lp),
-				  q->rx_ring, q->rx_ring_dma);
-		q->rx_ring = NULL;
+				  macb_dma_desc_get_size(bp),
+				  queue->rx_ring, queue->rx_ring_dma);
+		queue->rx_ring = NULL;
 		return -ENOMEM;
 	}
 
 	return 0;
 }
 
-static void at91ether_free_coherent(struct macb *lp)
+static void at91ether_free_coherent(struct macb *bp)
 {
-	struct macb_queue *q = &lp->queues[0];
+	struct macb_queue *queue = &bp->queues[0];
 
-	if (q->rx_ring) {
-		dma_free_coherent(&lp->pdev->dev,
+	if (queue->rx_ring) {
+		dma_free_coherent(&bp->pdev->dev,
 				  AT91ETHER_MAX_RX_DESCR *
-				  macb_dma_desc_get_size(lp),
-				  q->rx_ring, q->rx_ring_dma);
-		q->rx_ring = NULL;
+				  macb_dma_desc_get_size(bp),
+				  queue->rx_ring, queue->rx_ring_dma);
+		queue->rx_ring = NULL;
 	}
 
-	if (q->rx_buffers) {
-		dma_free_coherent(&lp->pdev->dev,
+	if (queue->rx_buffers) {
+		dma_free_coherent(&bp->pdev->dev,
 				  AT91ETHER_MAX_RX_DESCR *
 				  AT91ETHER_MAX_RBUFF_SZ,
-				  q->rx_buffers, q->rx_buffers_dma);
-		q->rx_buffers = NULL;
+				  queue->rx_buffers, queue->rx_buffers_dma);
+		queue->rx_buffers = NULL;
 	}
 }
 
 /* Initialize and start the Receiver and Transmit subsystems */
-static int at91ether_start(struct macb *lp)
+static int at91ether_start(struct macb *bp)
 {
-	struct macb_queue *q = &lp->queues[0];
+	struct macb_queue *queue = &bp->queues[0];
 	struct macb_dma_desc *desc;
 	dma_addr_t addr;
 	u32 ctl;
 	int i, ret;
 
-	ret = at91ether_alloc_coherent(lp);
+	ret = at91ether_alloc_coherent(bp);
 	if (ret)
 		return ret;
 
-	addr = q->rx_buffers_dma;
+	addr = queue->rx_buffers_dma;
 	for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
-		desc = macb_rx_desc(q, i);
-		macb_set_addr(lp, desc, addr);
+		desc = macb_rx_desc(queue, i);
+		macb_set_addr(bp, desc, addr);
 		desc->ctrl = 0;
 		addr += AT91ETHER_MAX_RBUFF_SZ;
 	}
@@ -5038,17 +5039,17 @@ static int at91ether_start(struct macb *lp)
 	desc->addr |= MACB_BIT(RX_WRAP);
 
 	/* Reset buffer index */
-	q->rx_tail = 0;
+	queue->rx_tail = 0;
 
 	/* Program address of descriptor list in Rx Buffer Queue register */
-	macb_writel(lp, RBQP, q->rx_ring_dma);
+	macb_writel(bp, RBQP, queue->rx_ring_dma);
 
 	/* Enable Receive and Transmit */
-	ctl = macb_readl(lp, NCR);
-	macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
+	ctl = macb_readl(bp, NCR);
+	macb_writel(bp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
 
 	/* Enable MAC interrupts */
-	macb_writel(lp, IER, MACB_BIT(RCOMP)	|
+	macb_writel(bp, IER, MACB_BIT(RCOMP)	|
 			     MACB_BIT(RXUBR)	|
 			     MACB_BIT(ISR_TUND)	|
 			     MACB_BIT(ISR_RLE)	|
@@ -5059,12 +5060,12 @@ static int at91ether_start(struct macb *lp)
 	return 0;
 }
 
-static void at91ether_stop(struct macb *lp)
+static void at91ether_stop(struct macb *bp)
 {
 	u32 ctl;
 
 	/* Disable MAC interrupts */
-	macb_writel(lp, IDR, MACB_BIT(RCOMP)	|
+	macb_writel(bp, IDR, MACB_BIT(RCOMP)	|
 			     MACB_BIT(RXUBR)	|
 			     MACB_BIT(ISR_TUND)	|
 			     MACB_BIT(ISR_RLE)	|
@@ -5073,35 +5074,35 @@ static void at91ether_stop(struct macb *lp)
 			     MACB_BIT(HRESP));
 
 	/* Disable Receiver and Transmitter */
-	ctl = macb_readl(lp, NCR);
-	macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
+	ctl = macb_readl(bp, NCR);
+	macb_writel(bp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
 
 	/* Free resources. */
-	at91ether_free_coherent(lp);
+	at91ether_free_coherent(bp);
 }
 
 /* Open the ethernet interface */
 static int at91ether_open(struct net_device *netdev)
 {
-	struct macb *lp = netdev_priv(netdev);
+	struct macb *bp = netdev_priv(netdev);
 	u32 ctl;
 	int ret;
 
-	ret = pm_runtime_resume_and_get(&lp->pdev->dev);
+	ret = pm_runtime_resume_and_get(&bp->pdev->dev);
 	if (ret < 0)
 		return ret;
 
 	/* Clear internal statistics */
-	ctl = macb_readl(lp, NCR);
-	macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
+	ctl = macb_readl(bp, NCR);
+	macb_writel(bp, NCR, ctl | MACB_BIT(CLRSTAT));
 
-	macb_set_hwaddr(lp);
+	macb_set_hwaddr(bp);
 
-	ret = at91ether_start(lp);
+	ret = at91ether_start(bp);
 	if (ret)
 		goto pm_exit;
 
-	ret = macb_phylink_connect(lp);
+	ret = macb_phylink_connect(bp);
 	if (ret)
 		goto stop;
 
@@ -5110,25 +5111,25 @@ static int at91ether_open(struct net_device *netdev)
 	return 0;
 
 stop:
-	at91ether_stop(lp);
+	at91ether_stop(bp);
 pm_exit:
-	pm_runtime_put_sync(&lp->pdev->dev);
+	pm_runtime_put_sync(&bp->pdev->dev);
 	return ret;
 }
 
 /* Close the interface */
 static int at91ether_close(struct net_device *netdev)
 {
-	struct macb *lp = netdev_priv(netdev);
+	struct macb *bp = netdev_priv(netdev);
 
 	netif_stop_queue(netdev);
 
-	phylink_stop(lp->phylink);
-	phylink_disconnect_phy(lp->phylink);
+	phylink_stop(bp->phylink);
+	phylink_disconnect_phy(bp->phylink);
 
-	at91ether_stop(lp);
+	at91ether_stop(bp);
 
-	pm_runtime_put(&lp->pdev->dev);
+	pm_runtime_put(&bp->pdev->dev);
 
 	return 0;
 }
@@ -5137,19 +5138,21 @@ static int at91ether_close(struct net_device *netdev)
 static netdev_tx_t at91ether_start_xmit(struct sk_buff *skb,
 					struct net_device *netdev)
 {
-	struct macb *lp = netdev_priv(netdev);
+	struct macb *bp = netdev_priv(netdev);
+	struct device *dev = &bp->pdev->dev;
 
-	if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
+	if (macb_readl(bp, TSR) & MACB_BIT(RM9200_BNQ)) {
 		int desc = 0;
 
 		netif_stop_queue(netdev);
 
 		/* Store packet information (to free when Tx completed) */
-		lp->rm9200_txq[desc].skb = skb;
-		lp->rm9200_txq[desc].size = skb->len;
-		lp->rm9200_txq[desc].mapping = dma_map_single(&lp->pdev->dev, skb->data,
-							      skb->len, DMA_TO_DEVICE);
-		if (dma_mapping_error(&lp->pdev->dev, lp->rm9200_txq[desc].mapping)) {
+		bp->rm9200_txq[desc].skb = skb;
+		bp->rm9200_txq[desc].size = skb->len;
+		bp->rm9200_txq[desc].mapping = dma_map_single(dev, skb->data,
+							      skb->len,
+							      DMA_TO_DEVICE);
+		if (dma_mapping_error(dev, bp->rm9200_txq[desc].mapping)) {
 			dev_kfree_skb_any(skb);
 			netdev->stats.tx_dropped++;
 			netdev_err(netdev, "%s: DMA mapping error\n", __func__);
@@ -5157,9 +5160,9 @@ static netdev_tx_t at91ether_start_xmit(struct sk_buff *skb,
 		}
 
 		/* Set address of the data in the Transmit Address register */
-		macb_writel(lp, TAR, lp->rm9200_txq[desc].mapping);
+		macb_writel(bp, TAR, bp->rm9200_txq[desc].mapping);
 		/* Set length of the packet in the Transmit Control register */
-		macb_writel(lp, TCR, skb->len);
+		macb_writel(bp, TCR, skb->len);
 
 	} else {
 		netdev_err(netdev, "%s called, but device is busy!\n",
@@ -5175,16 +5178,17 @@ static netdev_tx_t at91ether_start_xmit(struct sk_buff *skb,
  */
 static void at91ether_rx(struct net_device *netdev)
 {
-	struct macb *lp = netdev_priv(netdev);
-	struct macb_queue *q = &lp->queues[0];
+	struct macb *bp = netdev_priv(netdev);
+	struct macb_queue *queue = &bp->queues[0];
 	struct macb_dma_desc *desc;
 	unsigned char *p_recv;
 	struct sk_buff *skb;
 	unsigned int pktlen;
 
-	desc = macb_rx_desc(q, q->rx_tail);
+	desc = macb_rx_desc(queue, queue->rx_tail);
 	while (desc->addr & MACB_BIT(RX_USED)) {
-		p_recv = q->rx_buffers + q->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
+		p_recv = queue->rx_buffers +
+			 queue->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
 		pktlen = MACB_BF(RX_FRMLEN, desc->ctrl);
 		skb = netdev_alloc_skb(netdev, pktlen + 2);
 		if (skb) {
@@ -5206,12 +5210,12 @@ static void at91ether_rx(struct net_device *netdev)
 		desc->addr &= ~MACB_BIT(RX_USED);
 
 		/* wrap after last buffer */
-		if (q->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
-			q->rx_tail = 0;
+		if (queue->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
+			queue->rx_tail = 0;
 		else
-			q->rx_tail++;
+			queue->rx_tail++;
 
-		desc = macb_rx_desc(q, q->rx_tail);
+		desc = macb_rx_desc(queue, queue->rx_tail);
 	}
 }
 
@@ -5219,14 +5223,14 @@ static void at91ether_rx(struct net_device *netdev)
 static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
 {
 	struct net_device *netdev = dev_id;
-	struct macb *lp = netdev_priv(netdev);
+	struct macb *bp = netdev_priv(netdev);
 	u32 intstatus, ctl;
 	unsigned int desc;
 
 	/* MAC Interrupt Status register indicates what interrupts are pending.
 	 * It is automatically cleared once read.
 	 */
-	intstatus = macb_readl(lp, ISR);
+	intstatus = macb_readl(bp, ISR);
 
 	/* Receive complete */
 	if (intstatus & MACB_BIT(RCOMP))
@@ -5239,23 +5243,25 @@ static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
 			netdev->stats.tx_errors++;
 
 		desc = 0;
-		if (lp->rm9200_txq[desc].skb) {
-			dev_consume_skb_irq(lp->rm9200_txq[desc].skb);
-			lp->rm9200_txq[desc].skb = NULL;
-			dma_unmap_single(&lp->pdev->dev, lp->rm9200_txq[desc].mapping,
-					 lp->rm9200_txq[desc].size, DMA_TO_DEVICE);
+		if (bp->rm9200_txq[desc].skb) {
+			dev_consume_skb_irq(bp->rm9200_txq[desc].skb);
+			bp->rm9200_txq[desc].skb = NULL;
+			dma_unmap_single(&bp->pdev->dev,
+					 bp->rm9200_txq[desc].mapping,
+					 bp->rm9200_txq[desc].size,
+					 DMA_TO_DEVICE);
 			netdev->stats.tx_packets++;
-			netdev->stats.tx_bytes += lp->rm9200_txq[desc].size;
+			netdev->stats.tx_bytes += bp->rm9200_txq[desc].size;
 		}
 		netif_wake_queue(netdev);
 	}
 
 	/* Work-around for EMAC Errata section 41.3.1 */
 	if (intstatus & MACB_BIT(RXUBR)) {
-		ctl = macb_readl(lp, NCR);
-		macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
+		ctl = macb_readl(bp, NCR);
+		macb_writel(bp, NCR, ctl & ~MACB_BIT(RE));
 		wmb();
-		macb_writel(lp, NCR, ctl | MACB_BIT(RE));
+		macb_writel(bp, NCR, ctl | MACB_BIT(RE));
 	}
 
 	if (intstatus & MACB_BIT(ISR_ROVR))
Signature · 1 line
2.55.0
author maintainer
#4 lore raw

PATCH v9 4/17 net: macb: unify queue index variable naming convention and types

In reply to Théo Lebrun

Variables are named q or queue_index. Types are int, unsigned int, u32 and u16. Use unsigned int q everywhere.

Skip over taprio functions. They use u8 queue_id which fits with the struct macb_queue_enst_config field. Using queue_id everywhere would be too verbose.

---
 drivers/net/ethernet/cadence/macb_main.c | 32 ++++++++++++++++----------------
 1 file changed, 16 insertions(+), 16 deletions(-)
drivers/net/ethernet/cadence/macb_main.c +16 -16
index e01a5e0bf481..77053cb9d8f7 100644
@@ -877,7 +877,7 @@ static void gem_shuffle_tx_one_ring(struct macb_queue *queue)
 static void gem_shuffle_tx_rings(struct macb *bp)
 {
 	struct macb_queue *queue;
-	int q;
+	unsigned int q;
 
 	for (q = 0, queue = bp->queues; q < bp->num_queues; q++, queue++)
 		gem_shuffle_tx_one_ring(queue);
@@ -1258,7 +1258,7 @@ static void macb_tx_error_task(struct work_struct *work)
 						      tx_error_task);
 	bool			halt_timeout = false;
 	struct macb		*bp = queue->bp;
-	u32			queue_index;
+	unsigned int		q;
 	u32			packets = 0;
 	u32			bytes = 0;
 	struct macb_tx_skb	*tx_skb;
@@ -1267,9 +1267,9 @@ static void macb_tx_error_task(struct work_struct *work)
 	unsigned int		tail;
 	unsigned long		flags;
 
-	queue_index = queue - bp->queues;
+	q = queue - bp->queues;
 	netdev_vdbg(bp->netdev, "%s: q = %u, t = %u, h = %u\n",
-		    __func__, queue_index, queue->tx_tail, queue->tx_head);
+		    __func__, q, queue->tx_tail, queue->tx_head);
 
 	/* Prevent the queue NAPI TX poll from running, as it calls
 	 * macb_tx_complete(), which in turn may call netif_wake_subqueue().
@@ -1342,7 +1342,7 @@ static void macb_tx_error_task(struct work_struct *work)
 		macb_tx_unmap(bp, tx_skb, 0);
 	}
 
-	netdev_tx_completed_queue(netdev_get_tx_queue(bp->netdev, queue_index),
+	netdev_tx_completed_queue(netdev_get_tx_queue(bp->netdev, q),
 				  packets, bytes);
 
 	/* Set end of TX queue */
@@ -1407,7 +1407,7 @@ static bool ptp_one_step_sync(struct sk_buff *skb)
 static int macb_tx_complete(struct macb_queue *queue, int budget)
 {
 	struct macb *bp = queue->bp;
-	u16 queue_index = queue - bp->queues;
+	unsigned int q = queue - bp->queues;
 	unsigned long flags;
 	unsigned int tail;
 	unsigned int head;
@@ -1469,14 +1469,14 @@ static int macb_tx_complete(struct macb_queue *queue, int budget)
 		}
 	}
 
-	netdev_tx_completed_queue(netdev_get_tx_queue(bp->netdev, queue_index),
+	netdev_tx_completed_queue(netdev_get_tx_queue(bp->netdev, q),
 				  packets, bytes);
 
 	queue->tx_tail = tail;
-	if (__netif_subqueue_stopped(bp->netdev, queue_index) &&
+	if (__netif_subqueue_stopped(bp->netdev, q) &&
 	    CIRC_CNT(queue->tx_head, queue->tx_tail,
 		     bp->tx_ring_size) <= MACB_TX_WAKEUP_THRESH(bp))
-		netif_wake_subqueue(bp->netdev, queue_index);
+		netif_wake_subqueue(bp->netdev, q);
 	spin_unlock_irqrestore(&queue->tx_ptr_lock, flags);
 
 	if (packets)
@@ -2472,10 +2472,10 @@ static int macb_pad_and_fcs(struct sk_buff **skb, struct net_device *netdev)
 static netdev_tx_t macb_start_xmit(struct sk_buff *skb,
 				   struct net_device *netdev)
 {
-	u16 queue_index = skb_get_queue_mapping(skb);
 	struct macb *bp = netdev_priv(netdev);
-	struct macb_queue *queue = &bp->queues[queue_index];
+	unsigned int q = skb_get_queue_mapping(skb);
 	unsigned int desc_cnt, nr_frags, frag_size, f;
+	struct macb_queue *queue = &bp->queues[q];
 	unsigned int hdrlen;
 	unsigned long flags;
 	bool is_lso;
@@ -2514,8 +2514,8 @@ static netdev_tx_t macb_start_xmit(struct sk_buff *skb,
 
 #if defined(DEBUG) && defined(VERBOSE_DEBUG)
 	netdev_vdbg(bp->netdev,
-		    "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
-		    queue_index, skb->len, skb->head, skb->data,
+		    "start_xmit: queue %u len %u head %p data %p tail %p end %p\n",
+		    q, skb->len, skb->head, skb->data,
 		    skb_tail_pointer(skb), skb_end_pointer(skb));
 	print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
 		       skb->data, 16, true);
@@ -2541,7 +2541,7 @@ static netdev_tx_t macb_start_xmit(struct sk_buff *skb,
 	/* This is a hard error, log it. */
 	if (CIRC_SPACE(queue->tx_head, queue->tx_tail,
 		       bp->tx_ring_size) < desc_cnt) {
-		netif_stop_subqueue(netdev, queue_index);
+		netif_stop_subqueue(netdev, q);
 		netdev_dbg(netdev, "tx_head = %u, tx_tail = %u\n",
 			   queue->tx_head, queue->tx_tail);
 		ret = NETDEV_TX_BUSY;
@@ -2557,7 +2557,7 @@ static netdev_tx_t macb_start_xmit(struct sk_buff *skb,
 	/* Make newly initialized descriptor visible to hardware */
 	wmb();
 	skb_tx_timestamp(skb);
-	netdev_tx_sent_queue(netdev_get_tx_queue(bp->netdev, queue_index),
+	netdev_tx_sent_queue(netdev_get_tx_queue(bp->netdev, q),
 			     skb->len);
 
 	spin_lock(&bp->lock);
@@ -2566,7 +2566,7 @@ static netdev_tx_t macb_start_xmit(struct sk_buff *skb,
 	spin_unlock(&bp->lock);
 
 	if (CIRC_SPACE(queue->tx_head, queue->tx_tail, bp->tx_ring_size) < 1)
-		netif_stop_subqueue(netdev, queue_index);
+		netif_stop_subqueue(netdev, q);
 
 unlock:
 	spin_unlock_irqrestore(&queue->tx_ptr_lock, flags);
Signature · 1 line
2.55.0
author maintainer
#5 lore raw

PATCH v9 5/17 net: macb: enforce reverse christmas tree (RCT) convention

In reply to Théo Lebrun

Enforce the reverse christmas tree convention in those functions:

macb_tx_error_task()
gem_rx_refill()
gem_rx()
macb_rx_frame()
macb_init_rx_ring()
macb_rx()
macb_rx_pending()
macb_start_xmit()

The goal is to minimise unrelated diff in future patches.

In macb_tx_error_task(), we fold the assignment into the declaration statement.

---
 drivers/net/ethernet/cadence/macb_main.c | 61 ++++++++++++++++----------------
 1 file changed, 30 insertions(+), 31 deletions(-)
drivers/net/ethernet/cadence/macb_main.c +30 -31
index 77053cb9d8f7..b138b94ea0d8 100644
@@ -1254,20 +1254,19 @@ static dma_addr_t macb_get_addr(struct macb *bp, struct macb_dma_desc *desc)
 
 static void macb_tx_error_task(struct work_struct *work)
 {
-	struct macb_queue	*queue = container_of(work, struct macb_queue,
-						      tx_error_task);
-	bool			halt_timeout = false;
-	struct macb		*bp = queue->bp;
-	unsigned int		q;
-	u32			packets = 0;
-	u32			bytes = 0;
-	struct macb_tx_skb	*tx_skb;
-	struct macb_dma_desc	*desc;
-	struct sk_buff		*skb;
-	unsigned int		tail;
-	unsigned long		flags;
+	struct macb_queue *queue = container_of(work, struct macb_queue,
+						tx_error_task);
+	unsigned int q = queue - queue->bp->queues;
+	struct macb *bp = queue->bp;
+	struct macb_tx_skb *tx_skb;
+	struct macb_dma_desc *desc;
+	bool halt_timeout = false;
+	struct sk_buff *skb;
+	unsigned long flags;
+	unsigned int tail;
+	u32 packets = 0;
+	u32 bytes = 0;
 
-	q = queue - bp->queues;
 	netdev_vdbg(bp->netdev, "%s: q = %u, t = %u, h = %u\n",
 		    __func__, q, queue->tx_tail, queue->tx_head);
 
@@ -1487,11 +1486,11 @@ static int macb_tx_complete(struct macb_queue *queue, int budget)
 
 static void gem_rx_refill(struct macb_queue *queue)
 {
-	unsigned int		entry;
-	struct sk_buff		*skb;
-	dma_addr_t		paddr;
 	struct macb *bp = queue->bp;
 	struct macb_dma_desc *desc;
+	struct sk_buff *skb;
+	unsigned int entry;
+	dma_addr_t paddr;
 
 	while (CIRC_SPACE(queue->rx_prepared_head, queue->rx_tail,
 			bp->rx_ring_size) > 0) {
@@ -1584,11 +1583,11 @@ static int gem_rx(struct macb_queue *queue, struct napi_struct *napi,
 		  int budget)
 {
 	struct macb *bp = queue->bp;
-	unsigned int		len;
-	unsigned int		entry;
-	struct sk_buff		*skb;
-	struct macb_dma_desc	*desc;
-	int			count = 0;
+	struct macb_dma_desc *desc;
+	struct sk_buff *skb;
+	unsigned int entry;
+	unsigned int len;
+	int count = 0;
 
 	while (count < budget) {
 		u32 ctrl;
@@ -1675,12 +1674,12 @@ static int gem_rx(struct macb_queue *queue, struct napi_struct *napi,
 static int macb_rx_frame(struct macb_queue *queue, struct napi_struct *napi,
 			 unsigned int first_frag, unsigned int last_frag)
 {
-	unsigned int len;
-	unsigned int frag;
+	struct macb *bp = queue->bp;
+	struct macb_dma_desc *desc;
 	unsigned int offset;
 	struct sk_buff *skb;
-	struct macb_dma_desc *desc;
-	struct macb *bp = queue->bp;
+	unsigned int frag;
+	unsigned int len;
 
 	desc = macb_rx_desc(queue, last_frag);
 	len = desc->ctrl & bp->rx_frm_len_mask;
@@ -1757,9 +1756,9 @@ static int macb_rx_frame(struct macb_queue *queue, struct napi_struct *napi,
 
 static inline void macb_init_rx_ring(struct macb_queue *queue)
 {
+	struct macb_dma_desc *desc = NULL;
 	struct macb *bp = queue->bp;
 	dma_addr_t addr;
-	struct macb_dma_desc *desc = NULL;
 	int i;
 
 	addr = queue->rx_buffers_dma;
@@ -1778,9 +1777,9 @@ static int macb_rx(struct macb_queue *queue, struct napi_struct *napi,
 {
 	struct macb *bp = queue->bp;
 	bool reset_rx_queue = false;
-	int received = 0;
-	unsigned int tail;
 	int first_frag = -1;
+	unsigned int tail;
+	int received = 0;
 
 	for (tail = queue->rx_tail; budget > 0; tail++) {
 		struct macb_dma_desc *desc = macb_rx_desc(queue, tail);
@@ -1855,8 +1854,8 @@ static int macb_rx(struct macb_queue *queue, struct napi_struct *napi,
 static bool macb_rx_pending(struct macb_queue *queue)
 {
 	struct macb *bp = queue->bp;
-	unsigned int		entry;
-	struct macb_dma_desc	*desc;
+	struct macb_dma_desc *desc;
+	unsigned int entry;
 
 	entry = macb_rx_ring_wrap(bp, queue->rx_tail);
 	desc = macb_rx_desc(queue, entry);
@@ -2476,10 +2475,10 @@ static netdev_tx_t macb_start_xmit(struct sk_buff *skb,
 	unsigned int q = skb_get_queue_mapping(skb);
 	unsigned int desc_cnt, nr_frags, frag_size, f;
 	struct macb_queue *queue = &bp->queues[q];
+	netdev_tx_t ret = NETDEV_TX_OK;
 	unsigned int hdrlen;
 	unsigned long flags;
 	bool is_lso;
-	netdev_tx_t ret = NETDEV_TX_OK;
 
 	if (macb_clear_csum(skb)) {
 		dev_kfree_skb_any(skb);
Signature · 1 line
2.55.0
author maintainer
#6 lore raw

Re: [PATCH net-next v9 05/17] net: macb: enforce reverse christmas tree (RCT) convention

Replying to non-net sashiko
https://sashiko.dev/#/patchset/20260812-macb-context-v9-0-7ddbf5f715e0%40bootlin.com

Quoted 38 lines

commit da95f184645150278bd5328618e0592f867e39bb
Author: Théo Lebrun <[email protected]>

net: macb: enforce reverse christmas tree (RCT) convention

This patch enforces the reverse christmas tree convention for variable declarations in several macb functions. The goal is to minimize unrelated diffs in future patches.

Quoted 4 lines
drivers/net/ethernet/cadence/macb_main.c +0 -0
index 77053cb9d8f7b..b138b94ea0d89 100644

[ ... ]

drivers/net/ethernet/cadence/macb_main.c static netdev_tx_t macb_start_xmit(struct sk_buff *skb, · line 2482

Quoted 9 lines
drivers/net/ethernet/cadence/macb_main.c +1 -1
@@ -2476,10 +2475,10 @@ static netdev_tx_t macb_start_xmit(struct sk_buff *skb,
 	unsigned int q = skb_get_queue_mapping(skb);
 	unsigned int desc_cnt, nr_frags, frag_size, f;
 	struct macb_queue *queue = &bp->queues[q];
+	netdev_tx_t ret = NETDEV_TX_OK;
 	unsigned int hdrlen;
 	unsigned long flags;
 	bool is_lso;
-	netdev_tx_t ret = NETDEV_TX_OK;

[Severity: Low]
This isn't a bug, but it looks like the first few variable declarations in macb_start_xmit() are still not in reverse christmas tree order.

With this patch applied, the top declarations in macb_start_xmit() are:

	struct macb *bp = netdev_priv(dev);
	unsigned int q = skb_get_queue_mapping(skb);
	unsigned int desc_cnt, nr_frags, frag_size, f;
	struct macb_queue *queue = &bp->queues[q];
	netdev_tx_t ret = NETDEV_TX_OK;
Quoted 6 lines

Is it intended that these first few variables remain in their original order due to initialization dependencies?

The commit message mentions enforcing RCT here, but to fully sort them, the initializations might need to be split from the declarations similar to what was done in macb_tx_error_task().

It sounds like Sashiko doesn't visualise the tree trunk. I'll ignore this review!

Thanks,

Signature · 3 lines
Théo Lebrun, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
author maintainer
#7 lore raw

PATCH v9 6/17 net: macb: allocate tieoff descriptor once across device lifetime

In reply to Théo Lebrun

The tieoff descriptor is a RX DMA descriptor ring of size one. It gets configured onto queues for Wake-on-LAN during system-wide suspend when hardware does not support disabling individual queues (MACB_CAPS_QUEUE_DISABLE).

MACB/GEM driver allocates it alongside the main RX ring inside macb_alloc() at open. Free is done by macb_free() at close.

Change to allocate once at probe and free on probe failure or device removal. This makes the tieoff descriptor lifetime much longer, avoiding repeating coherent buffer allocation on each open/close cycle.

Main benefit: we dissociate its lifetime from the main ring's lifetime. That way there is less work to be doing on resources (re)alloc. This currently happens on close/open, but will soon also happen on context swap operations (set_ringparam, change_mtu, set_channels, etc).

---
 drivers/net/ethernet/cadence/macb_main.c | 75 +++++++++++++++++---------------
 1 file changed, 41 insertions(+), 34 deletions(-)
drivers/net/ethernet/cadence/macb_main.c +41 -34
index b138b94ea0d8..af351d30adab 100644
@@ -2655,12 +2655,6 @@ static void macb_free(struct macb *bp)
 	unsigned int q;
 	size_t size;
 
-	if (bp->rx_ring_tieoff) {
-		dma_free_coherent(dev, macb_dma_desc_get_size(bp),
-				  bp->rx_ring_tieoff, bp->rx_ring_tieoff_dma);
-		bp->rx_ring_tieoff = NULL;
-	}
-
 	bp->macbgem_ops.mog_free_rx_buffers(bp);
 
 	size = bp->num_queues * macb_tx_ring_size_per_queue(bp);
@@ -2775,16 +2769,6 @@ static int macb_alloc(struct macb *bp)
 	if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
 		goto out_err;
 
-	/* Required for tie off descriptor for PM cases */
-	if (!(bp->caps & MACB_CAPS_QUEUE_DISABLE)) {
-		bp->rx_ring_tieoff = dma_alloc_coherent(&bp->pdev->dev,
-							macb_dma_desc_get_size(bp),
-							&bp->rx_ring_tieoff_dma,
-							GFP_KERNEL);
-		if (!bp->rx_ring_tieoff)
-			goto out_err;
-	}
-
 	return 0;
 
 out_err:
@@ -2792,19 +2776,6 @@ static int macb_alloc(struct macb *bp)
 	return -ENOMEM;
 }
 
-static void macb_init_tieoff(struct macb *bp)
-{
-	struct macb_dma_desc *desc = bp->rx_ring_tieoff;
-
-	if (bp->caps & MACB_CAPS_QUEUE_DISABLE)
-		return;
-	/* Setup a wrapping descriptor with no free slots
-	 * (WRAP and USED) to tie off/disable unused RX queues.
-	 */
-	macb_set_addr(bp, desc, MACB_BIT(RX_WRAP) | MACB_BIT(RX_USED));
-	desc->ctrl = 0;
-}
-
 static void gem_init_rx_ring(struct macb_queue *queue)
 {
 	queue->rx_tail = 0;
@@ -2832,8 +2803,6 @@ static void gem_init_rings(struct macb *bp)
 
 		gem_init_rx_ring(queue);
 	}
-
-	macb_init_tieoff(bp);
 }
 
 static void macb_init_rings(struct macb *bp)
@@ -2851,8 +2820,6 @@ static void macb_init_rings(struct macb *bp)
 	bp->queues[0].tx_head = 0;
 	bp->queues[0].tx_tail = 0;
 	desc->ctrl |= MACB_BIT(TX_WRAP);
-
-	macb_init_tieoff(bp);
 }
 
 static void macb_reset_hw(struct macb *bp)
@@ -5537,6 +5504,38 @@ static int eyeq5_init(struct platform_device *pdev)
 	return ret;
 }
 
+static int macb_alloc_tieoff(struct macb *bp)
+{
+	/* Tieoff is a workaround in case HW cannot disable queues, for PM. */
+	if (bp->caps & MACB_CAPS_QUEUE_DISABLE)
+		return 0;
+
+	bp->rx_ring_tieoff = dma_alloc_coherent(&bp->pdev->dev,
+						macb_dma_desc_get_size(bp),
+						&bp->rx_ring_tieoff_dma,
+						GFP_KERNEL);
+	if (!bp->rx_ring_tieoff)
+		return -ENOMEM;
+
+	macb_set_addr(bp, bp->rx_ring_tieoff,
+		      MACB_BIT(RX_WRAP) | MACB_BIT(RX_USED));
+
+	bp->rx_ring_tieoff->ctrl = 0;
+
+	return 0;
+}
+
+static void macb_free_tieoff(struct macb *bp)
+{
+	if (!bp->rx_ring_tieoff)
+		return;
+
+	dma_free_coherent(&bp->pdev->dev, macb_dma_desc_get_size(bp),
+			  bp->rx_ring_tieoff,
+			  bp->rx_ring_tieoff_dma);
+	bp->rx_ring_tieoff = NULL;
+}
+
 static const struct macb_usrio_config mpfs_usrio = {
 	.tsu_source = 0,
 };
@@ -5946,10 +5945,14 @@ static int macb_probe(struct platform_device *pdev)
 
 	netif_carrier_off(netdev);
 
+	err = macb_alloc_tieoff(bp);
+	if (err)
+		goto err_out_unregister_mdio;
+
 	err = register_netdev(netdev);
 	if (err) {
 		dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
-		goto err_out_unregister_mdio;
+		goto err_out_free_tieoff;
 	}
 
 	INIT_WORK(&bp->hresp_err_bh_work, macb_hresp_error_task);
@@ -5963,6 +5966,9 @@ static int macb_probe(struct platform_device *pdev)
 
 	return 0;
 
+err_out_free_tieoff:
+	macb_free_tieoff(bp);
+
 err_out_unregister_mdio:
 	mdiobus_unregister(bp->mii_bus);
 	mdiobus_free(bp->mii_bus);
@@ -5992,6 +5998,7 @@ static void macb_remove(struct platform_device *pdev)
 	if (netdev) {
 		bp = netdev_priv(netdev);
 		unregister_netdev(netdev);
+		macb_free_tieoff(bp);
 		phy_exit(bp->phy);
 		mdiobus_unregister(bp->mii_bus);
 		mdiobus_free(bp->mii_bus);
Signature · 1 line
2.55.0
author maintainer
#8 lore raw

PATCH v9 7/17 net: macb: refuse set_ringparam on EMAC

In reply to Théo Lebrun

EMAC has never supported changing ring sizes: RX is hardcoded to 9 and TX is the tiniest ring buffer you can imagine.

Make sure the operation fails early rather than silently succeed and storing values in bp->configured_{rx,tx}_ring_size that are never read in the EMAC case.

---
 drivers/net/ethernet/cadence/macb_main.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/net/ethernet/cadence/macb_main.c +3 -0
index af351d30adab..1476bce77f34 100644
@@ -3687,6 +3687,9 @@ static int macb_set_ringparam(struct net_device *netdev,
 	u32 new_rx_size, new_tx_size;
 	unsigned int reset = 0;
 
+	if (bp->caps & MACB_CAPS_MACB_IS_EMAC)
+		return -EOPNOTSUPP;
+
 	if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
 		return -EINVAL;
 
Signature · 1 line
2.55.0
author maintainer
#9 lore raw

PATCH v9 8/17 net: macb: introduce macb_context struct for buffer management

In reply to Théo Lebrun

Whenever an operation requires buffer realloc, we close the interface, update parameters and reopen. To improve reliability under memory pressure, we should rather alloc new buffers, reconfigure HW and free old buffers. This requires MACB to support having multiple "contexts" in parallel.

Introduce this concept by adding the macb_context struct, which owns all queue buffers and the parameters associated. We do not yet support multiple contexts in parallel, because all functions access bp->ctx (the currently active context) directly.

Steps:

- Introduce struct macb_context and its children struct macb_rxq and struct macb_txq. Context fields are stolen from struct macb and rxq/txq fields are from struct macb_queue.

Making it two separate structs per queue simplifies accesses: we grab a txq/rxq local variable and access fields like txq->head instead of queue->tx_head. It also anecdotally improves data locality.

- macb_init_dflt() / macb_get_ringparam() do not access bp->ctx->{rx,tx}_ring_size as they will/might run while interface is offline and ctx is NULL. Instead, introduce bp->configured_{rx,tx}_ring_size which get updated on user requests.

- macb_open() starts by allocating bp->ctx. It gets freed in the open error codepath or by macb_close().

- Guided by compile errors, update all codepaths. Most diff is changing queue->tx_* to txq->* and queue->rx_* to rxq->*, with a new local variable. Also rx_buffer_size / rx_ring_size / tx_ring_size move from bp to bp->ctx.

Introduce two helpers macb_txq|rxq() functions to convert macb_queue pointers.

- macb_get_regs() is tweaked to support being ran while interface is offline (and context is NULL). Use default values at zero and override them only if context is present.

- Protect our ctx freeing in at91ether code with a synchronize_irq() call.

- Move #define AT91ETHER_MAX_RX_DESCR 9 out of
#if defined(CONFIG_OF) to have it unconditionally available to macb_probe(), to init bp->configured_rx_ring_size in the EMAC case.

---
 drivers/net/ethernet/cadence/macb.h      |  51 +++-
 drivers/net/ethernet/cadence/macb_main.c | 491 +++++++++++++++++++------------
 2 files changed, 335 insertions(+), 207 deletions(-)
drivers/net/ethernet/cadence/macb.h +35 -16
index 1e1f52285a39..95cb494190c5 100644
@@ -1264,23 +1264,12 @@ struct macb_queue {
 	unsigned int		ENST_ON_TIME;
 	unsigned int		ENST_OFF_TIME;
 
-	/* Lock to protect tx_head and tx_tail */
+	/* Lock to protect ctx->txq[q].head and ctx->txq[q].tail */
 	spinlock_t		tx_ptr_lock;
-	unsigned int		tx_head, tx_tail;
-	struct macb_dma_desc	*tx_ring;
-	struct macb_tx_skb	*tx_skb;
-	dma_addr_t		tx_ring_dma;
 	struct work_struct	tx_error_task;
 	bool			txubr_pending;
 	struct napi_struct	napi_tx;
 
-	dma_addr_t		rx_ring_dma;
-	dma_addr_t		rx_buffers_dma;
-	unsigned int		rx_tail;
-	unsigned int		rx_prepared_head;
-	struct macb_dma_desc	*rx_ring;
-	struct sk_buff		**rx_skbuff;
-	void			*rx_buffers;
 	struct napi_struct	napi_rx;
 	struct queue_stats stats;
 };
@@ -1295,6 +1284,32 @@ struct ethtool_rx_fs_list {
 	unsigned int count;
 };
 
+struct macb_rxq {
+	struct macb_dma_desc	*ring;		/* MACB & GEM */
+	dma_addr_t		ring_dma;	/* MACB & GEM */
+	unsigned int		tail;		/* MACB & GEM */
+	unsigned int		prepared_head;	/* GEM */
+	struct sk_buff		**skbuff;	/* GEM */
+	dma_addr_t		buffers_dma;	/* MACB */
+	void			*buffers;	/* MACB */
+};
+
+struct macb_txq {
+	unsigned int		head;
+	unsigned int		tail;
+	struct macb_dma_desc	*ring;
+	dma_addr_t		ring_dma;
+	struct macb_tx_skb	*skb;
+};
+
+struct macb_context {
+	unsigned int		rx_buffer_size;
+	unsigned int		rx_ring_size;
+	unsigned int		tx_ring_size;
+	struct macb_rxq		rxq[MACB_MAX_QUEUES];
+	struct macb_txq		txq[MACB_MAX_QUEUES];
+};
+
 struct macb {
 	void __iomem		*regs;
 	bool			native_io;
@@ -1303,12 +1318,16 @@ struct macb {
 	u32	(*macb_reg_readl)(struct macb *bp, int offset);
 	void	(*macb_reg_writel)(struct macb *bp, int offset, u32 value);
 
+	/*
+	 * Context stores all its parameters.
+	 * But we must remember them across closure.
+	 */
+	unsigned int		configured_rx_ring_size;
+	unsigned int		configured_tx_ring_size;
+	struct macb_context	*ctx;
+
 	struct macb_dma_desc	*rx_ring_tieoff;
 	dma_addr_t		rx_ring_tieoff_dma;
-	size_t			rx_buffer_size;
-
-	unsigned int		rx_ring_size;
-	unsigned int		tx_ring_size;
 
 	unsigned int		num_queues;
 	struct macb_queue	queues[MACB_MAX_QUEUES];
drivers/net/ethernet/cadence/macb_main.c +300 -191

1,179 lines not on this page · raw message

Signature · 1 line
2.55.0
author maintainer
#10 lore raw

Re: [PATCH net-next v9 08/17] net: macb: introduce macb_context struct for buffer management

Replying to non-net sashiko
https://sashiko.dev/#/patchset/20260812-macb-context-v9-0-7ddbf5f715e0%40bootlin.com

Quoted 29 lines

commit 64775f904ae680bc33819f27120daf167d287348
Author: Théo Lebrun <[email protected]>
Subject: net: macb: introduce macb_context struct for buffer management

Whenever an operation requires buffer realloc, we close the interface, update parameters and reopen. This commit introduces the macb_context struct to own all queue buffers and parameters, paving the way for supporting multiple contexts in parallel to improve reliability under memory pressure.

Quoted 3 lines
drivers/net/ethernet/cadence/macb_main.c +0 -0

[ ... ]

drivers/net/ethernet/cadence/macb_main.c static void macb_tx_error_task(struct work_struct *work) · line 1294

Quoted 8 lines
drivers/net/ethernet/cadence/macb_main.c +1 -0
@@ -1257,6 +1288,7 @@ static void macb_tx_error_task(struct work_struct *work)
 	struct macb_queue *queue = container_of(work, struct macb_queue,
 						tx_error_task);
 	unsigned int q = queue - queue->bp->queues;
+	struct macb_txq *txq = macb_txq(queue);
 	struct macb *bp = queue->bp;
 	struct macb_tx_skb *tx_skb;
 	struct macb_dma_desc *desc;

[Severity: High]
Does this asynchronously executing work item dereference the bp->ctx pointer unconditionally via the macb_txq() helper?

Quoted 5 lines

If the interface is brought down, macb_close() is called, which frees bp->ctx and sets it to NULL. If a hardware error interrupt had just fired and scheduled queue->tx_error_task, it appears this task could continue to execute because it is not cancelled via cancel_work_sync() in macb_close(). Could this result in a NULL pointer dereference or use-after-free?

Yes! This is all unrelated to the context swapping series but as the context swapping codepath does it properly we can fix the macb_close codepath along the way.

The sequence will be something like:
- quiesce IRQs to make sure they cannot re-schedule our BH tasks
- synchronize_irq(bp->queues[i].irq) for all i
- drain all BH tasks (in the right order!)
- bp->hresp_err_bh_work
- queue->tx_error_task
- queue->napi_rx
- queue->napi_tx
- bp->tx_lpi_work

Here is my "what schedules what?" table which explains the ordering:

|               |   enable/disable   |           schedule            |
|               |----|-------|-------|------|----|----|--------|-----|
|               |IRQs|napi_tx|napi_rx|tx_lpi|napi|napi|tx_error|hresp|
| Context       |    |       |       | task | rx | tx |  task  |task |
|===============|====|=======|=======|======|====|====|========|=====|
| open          | X  |   X   |   X   |      |    |    |        |     |
| link_up       | X  |       |       |      |    |    |        |     |
| link_down     | X  |       |       |      |    |    |        |     |
| close         | X  |   X   |   X   |      |    |    |        |     |
| enable_tx_lpi |    |       |       |  X   |    |    |        |     |
| swap          | X  |   X   |   X   |  X   |    |    |        |     |
| suspend       | X  |   X   |   X   |      |    |    |        |     |
| resume        | X  |   X   |   X   |      |    |    |        |     |
|---------------|----|-------|-------|------|----|----|--------|-----|
| irq & netpoll | X  |       |       |      | X  | X  |   X    | X   |
|---------------|----|-------|-------|------|----|----|--------|-----|
| napi_rx       | X  |       |       |      | X  |    |        |     |
| napi_tx       | X  |       |       |  X   |    | X  |        |     |
|---------------|----|-------|-------|------|----|----|--------|-----|
| tx_error_task | X  |   X   |       |      |    |    |        |     |
| hresp task    | X  |       |       |      |    |    |        |     |
Quoted 6 lines

[ ... ]

Quoted 3 lines
drivers/net/ethernet/cadence/macb_main.c +0 -0
@@ -3222,6 +3300,8 @@ static int macb_close(struct net_device *netdev)
 	spin_unlock_irqrestore(&bp->lock, flags);
 
Quoted 7 lines

drivers/net/ethernet/cadence/macb_main.c static int macb_open(struct net_device *netdev) · line 3269

Quoted 3 lines
drivers/net/ethernet/cadence/macb_main.c +2 -0
 	macb_free(bp);
+	kfree(bp->ctx);
+	bp->ctx = NULL;

[Severity: High]
Is it possible that queue->tx_error_task or bp->hresp_err_bh_work are still running or pending when we reach this point?

Yes, see above.

[...]

Quoted 3 lines

[Severity: High]
This is a pre-existing issue, but does macb_close() need to call synchronize_irq() before freeing resources?

Yes, see above.

[...]

Quoted 28 lines

drivers/net/ethernet/cadence/macb_main.c static int macb_close(struct net_device *netdev) · line 3307

Quoted 4 lines
drivers/net/ethernet/cadence/macb_main.c +0 -0
 	if (bp->ptp_info)
 		bp->ptp_info->ptp_remove(netdev);

[ ... ]

[Severity: High]
This is a pre-existing issue, but does macb_interrupt() safely handle shared interrupts when the device is runtime suspended?

drivers/net/ethernet/cadence/macb_main.c:macb_interrupt() {
    ...
    spin_lock(&bp->lock);

    status = queue_readl(queue, ISR);
    if (unlikely(!status)) {
        spin_unlock(&bp->lock);
        return IRQ_NONE;
    ...
}

If the device enters runtime suspend and its clocks are disabled, and another device sharing the same IRQ line triggers an interrupt, this handler executes and performs queue_readl(queue, ISR) without verifying the device's power management state. Could this trigger a synchronous external abort (bus fault)?

Quoted 3 lines

Using pm_runtime_get_if_active() before accessing hardware registers in shared IRQ handlers is typically required to prevent accessing powered-off hardware.

Too unrelated to context swapping. Also it only crashes if shared lanes are with separate interfaces. I hope no hardware like that exists in the wild. We supported shared interrupts only because some HW uses a single lane for all queues.

Long term I'd like to have a single handler for those IP blocks. Goal being to reduce overhead and remove that pesky IRQF_SHARED flag.

---

Replying to net sashiko
https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260812-macb-context-v9-0-7ddbf5f715e0%40bootlin.com

Quoted 134 lines

commit d54f7e79d0f10ea8e363d1bb3711374a51ae9c90
Author: Théo Lebrun <[email protected]>

net: macb: introduce macb_context struct for buffer management

This moves the per-queue ring/buffer pointers and the ring size parameters out of struct macb / struct macb_queue into a new heap-allocated struct macb_context, allocated in macb_open() and at91ether_open() and freed in the close paths. All accessors are converted to reach the state through bp->ctx.

Quoted 4 lines
drivers/net/ethernet/cadence/macb.h +0 -0
index 1e1f52285a39..95cb494190c5 100644

[ ... ]

Quoted 30 lines
drivers/net/ethernet/cadence/macb.h +26 -0
@@ -1295,6 +1284,32 @@ struct ethtool_rx_fs_list {
 	unsigned int count;
 };
 
+struct macb_rxq {
+	struct macb_dma_desc	*ring;		/* MACB & GEM */
+	dma_addr_t		ring_dma;	/* MACB & GEM */
+	unsigned int		tail;		/* MACB & GEM */
+	unsigned int		prepared_head;	/* GEM */
+	struct sk_buff		**skbuff;	/* GEM */
+	dma_addr_t		buffers_dma;	/* MACB */
+	void			*buffers;	/* MACB */
+};
+
+struct macb_txq {
+	unsigned int		head;
+	unsigned int		tail;
+	struct macb_dma_desc	*ring;
+	dma_addr_t		ring_dma;
+	struct macb_tx_skb	*skb;
+};
+
+struct macb_context {
+	unsigned int		rx_buffer_size;
+	unsigned int		rx_ring_size;
+	unsigned int		tx_ring_size;
+	struct macb_rxq		rxq[MACB_MAX_QUEUES];
+	struct macb_txq		txq[MACB_MAX_QUEUES];
+};
+

[ ... ]

Quoted 4 lines
drivers/net/ethernet/cadence/macb_main.c +0 -0
index 1476bce77f34..380ee72e26ac 100644

[ ... ]

Quoted 8 lines
drivers/net/ethernet/cadence/macb_main.c +1 -0
@@ -1257,6 +1288,7 @@ static void macb_tx_error_task(struct work_struct *work)
 	struct macb_queue *queue = container_of(work, struct macb_queue,
 						tx_error_task);
 	unsigned int q = queue - queue->bp->queues;
+	struct macb_txq *txq = macb_txq(queue);
 	struct macb *bp = queue->bp;
 	struct macb_tx_skb *tx_skb;
 	struct macb_dma_desc *desc;

[ ... ]

Quoted 11 lines
drivers/net/ethernet/cadence/macb_main.c +3 -3
@@ -1353,10 +1385,10 @@ static void macb_tx_error_task(struct work_struct *work)
 	wmb();
 
 	/* Reinitialize the TX desc queue */
-	queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
+	queue_writel(queue, TBQP, lower_32_bits(txq->ring_dma));
 	/* Make TX ring reflect state of hardware */
-	queue->tx_head = 0;
-	queue->tx_tail = 0;
+	txq->head = 0;
+	txq->tail = 0;

[ ... ]

Quoted 1 line
drivers/net/ethernet/cadence/macb_main.c +0 -0
@@ -2615,60 +2663,71 @@ static void gem_free_rx_buffers(struct macb *bp)

[ ... ]

Quoted 20 lines

static void macb_free(struct macb *bp)
{

drivers/net/ethernet/cadence/macb_main.c +6 -2
 	struct device *dev = &bp->pdev->dev;
 	struct macb_queue *queue;
+	struct macb_txq *txq;
+	struct macb_rxq *rxq;
 	unsigned int q;
 	size_t size;
 
 	bp->macbgem_ops.mog_free_rx_buffers(bp);
 
+	txq = &bp->ctx->txq[0];
 	size = bp->num_queues * macb_tx_ring_size_per_queue(bp);
-	dma_free_coherent(dev, size, bp->queues[0].tx_ring, bp->queues[0].tx_ring_dma);
+	dma_free_coherent(dev, size, txq->ring, txq->ring_dma);
 
+	rxq = &bp->ctx->rxq[0];
 	size = bp->num_queues * macb_rx_ring_size_per_queue(bp);
-	dma_free_coherent(dev, size, bp->queues[0].rx_ring, bp->queues[0].rx_ring_dma);
+	dma_free_coherent(dev, size, rxq->ring, rxq->ring_dma);

[ ... ]

Quoted 24 lines
drivers/net/ethernet/cadence/macb_main.c +8 -4
@@ -2728,7 +2786,8 @@ static int macb_alloc(struct macb *bp)
 {
 	struct device *dev = &bp->pdev->dev;
 	dma_addr_t tx_dma, rx_dma;
-	struct macb_queue *queue;
+	struct macb_txq *txq;
+	struct macb_rxq *rxq;
 	unsigned int q;
 	void *tx, *rx;
 	size_t size;
@@ -2754,16 +2813,19 @@ static int macb_alloc(struct macb *bp)
 	netdev_dbg(bp->netdev, "Allocated %zu bytes for %u RX rings at %08lx (mapped %p)\n",
 		   size, bp->num_queues, (unsigned long)rx_dma, rx);
 
-	for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
-		queue->tx_ring = tx + macb_tx_ring_size_per_queue(bp) * q;
-		queue->tx_ring_dma = tx_dma + macb_tx_ring_size_per_queue(bp) * q;
+	for (q = 0; q < bp->num_queues; ++q) {
+		txq = &bp->ctx->txq[q];
+		rxq = &bp->ctx->rxq[q];
+
+		txq->ring = tx + macb_tx_ring_size_per_queue(bp) * q;
+		txq->ring_dma = tx_dma + macb_tx_ring_size_per_queue(bp) * q;
 
Quoted 11 lines

drivers/net/ethernet/cadence/macb_main.c static int macb_alloc(struct macb *bp) · line 2824

Quoted 4 lines
drivers/net/ethernet/cadence/macb_main.c +2 -2
-		queue->rx_ring = rx + macb_rx_ring_size_per_queue(bp) * q;
-		queue->rx_ring_dma = rx_dma + macb_rx_ring_size_per_queue(bp) * q;
+		rxq->ring = rx + macb_rx_ring_size_per_queue(bp) * q;
+		rxq->ring_dma = rx_dma + macb_rx_ring_size_per_queue(bp) * q;

[Severity: Medium]
[Finding: gpt-5-6-sol-4-12]
[Sources: gpt-5-6-sol, opus-5]
This isn't a bug introduced by this patch, but while macb_alloc() is being reshaped here, does the tx coherent block get leaked on the early error paths?

Yes indeed. I've got a fix for that incoming. But completely unrelated.

[...]

Quoted 37 lines
Quoted 37 lines
drivers/net/ethernet/cadence/macb_main.c +12 -1
@@ -3140,14 +3207,22 @@ static int macb_open(struct net_device *netdev)
 	if (err < 0)
 		return err;
 
+	bp->ctx = kzalloc_obj(*bp->ctx);
+	if (!bp->ctx) {
+		err = -ENOMEM;
+		goto pm_exit;
+	}
+
 	/* RX buffers initialization */
 	macb_init_rx_buffer_size(bp, bufsz);
+	bp->ctx->rx_ring_size = bp->configured_rx_ring_size;
+	bp->ctx->tx_ring_size = bp->configured_tx_ring_size;
 
 	err = macb_alloc(bp);
 	if (err) {
 		netdev_err(netdev, "Unable to allocate DMA memory (error %d)\n",
 			   err);
-		goto pm_exit;
+		goto free_ctx;
 	}
 
 	bp->macbgem_ops.mog_init_rings(bp);
@@ -3189,6 +3264,9 @@ static int macb_open(struct net_device *netdev)
 		napi_disable(&queue->napi_tx);
 	}
 	macb_free(bp);
+free_ctx:
+	kfree(bp->ctx);
+	bp->ctx = NULL;
 pm_exit:
 	pm_runtime_put_sync(&bp->pdev->dev);
 	return err;
@@ -3222,6 +3300,8 @@ static int macb_close(struct net_device *netdev)
 	spin_unlock_irqrestore(&bp->lock, flags);
 
Quoted 9 lines

drivers/net/ethernet/cadence/macb_main.c static int macb_open(struct net_device *netdev) · line 3269

Quoted 3 lines
drivers/net/ethernet/cadence/macb_main.c +2 -0
 	macb_free(bp);
+	kfree(bp->ctx);
+	bp->ctx = NULL;

[Severity: High]
[Finding: gpt-5-6-sol-4-11]
[Sources: gpt-5-6-sol, opus-5]
Can a pending queue->tx_error_task still reference the context after this kfree()?

Yes, see above.

[...]

Quoted 9 lines
Quoted 9 lines
drivers/net/ethernet/cadence/macb_main.c +2 -2
@@ -3712,8 +3799,8 @@ static int macb_set_ringparam(struct net_device *netdev,
 		macb_close(bp->netdev);
 	}
 
-	bp->rx_ring_size = new_rx_size;
-	bp->tx_ring_size = new_tx_size;
+	bp->configured_rx_ring_size = new_rx_size;
+	bp->configured_tx_ring_size = new_tx_size;
 
Quoted 8 lines

drivers/net/ethernet/cadence/macb_main.c static int macb_set_ringparam(struct net_device *netdev, · line 3806

Quoted 2 lines
drivers/net/ethernet/cadence/macb_main.c +0 -0
 	if (reset)
 		macb_open(bp->netdev);

[Severity: High]
[Finding: gpt-5-6-sol-3-7]
[Sources: gpt-5-6-sol, opus-5]
What happens here if macb_open() fails now that it can fail at the new kzalloc_obj() of bp->ctx?

We ignore this error path because at the end of the series this gets turned into a context swap operation and the context_swap_end cannot fail!

[...]

Quoted 4 lines
Quoted 4 lines
drivers/net/ethernet/cadence/macb_main.c +0 -0
@@ -5061,6 +5149,12 @@ static int at91ether_open(struct net_device *netdev)
 	if (ret < 0)
 		return ret;
 
Quoted 12 lines

drivers/net/ethernet/cadence/macb_main.c static int at91ether_open(struct net_device *netdev) · line 5157

Quoted 6 lines
drivers/net/ethernet/cadence/macb_main.c +6 -0
+	bp->ctx = kzalloc_obj(*bp->ctx);
+	if (!bp->ctx) {
+		ret = -ENOMEM;
+		goto pm_exit;
+	}
+

[Severity: Low]
[Finding: gpt-5-6-sol-2-3]
[Sources: gpt-5-6-sol, opus-5]
This isn't a bug today, but should at91ether_open() also initialise the context parameters the way macb_open() does?

No because "configured_*_ring_size" does not mean anything on at91ether which has 1-sized tx ring or 4-sized rx ring. We refuse the ring size config ops, as expected.

[...]

Thanks,

Signature · 3 lines
Théo Lebrun, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
author maintainer
#11 lore raw

PATCH v9 9/17 net: macb: avoid macb_init_rx_buffer_size() modifying state

In reply to Théo Lebrun

macb_init_rx_buffer_size() takes the macb private data struct and overrides its bp->ctx->rx_buffer_size. To make it usable with multiple contexts, make it return its value.

Also, move the bufsz computation into it. The value is only used if GEM, and for historical reason it currently lives in macb_open().

---
 drivers/net/ethernet/cadence/macb_main.c | 26 +++++++++++++-------------
 1 file changed, 13 insertions(+), 13 deletions(-)
drivers/net/ethernet/cadence/macb_main.c +13 -13
index 380ee72e26ac..d2acf44bb6ae 100644
@@ -2617,25 +2617,26 @@ static netdev_tx_t macb_start_xmit(struct sk_buff *skb,
 	return ret;
 }
 
-static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
+static unsigned int macb_rx_buffer_size(struct macb *bp, unsigned int mtu)
 {
-	if (!macb_is_gem(bp)) {
-		bp->ctx->rx_buffer_size = MACB_RX_BUFFER_SIZE;
-	} else {
-		bp->ctx->rx_buffer_size = MIN(size, RX_BUFFER_MAX);
+	unsigned int size;
 
-		if (bp->ctx->rx_buffer_size % RX_BUFFER_MULTIPLE) {
+	if (!macb_is_gem(bp)) {
+		size = MACB_RX_BUFFER_SIZE;
+	} else {
+		size = mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
+		size = MIN(size, RX_BUFFER_MAX);
+
+		if (size % RX_BUFFER_MULTIPLE) {
 			netdev_dbg(bp->netdev,
 				   "RX buffer must be multiple of %d bytes, expanding\n",
 				   RX_BUFFER_MULTIPLE);
-			bp->ctx->rx_buffer_size =
-				roundup(bp->ctx->rx_buffer_size,
-					RX_BUFFER_MULTIPLE);
+			size = roundup(size, RX_BUFFER_MULTIPLE);
 		}
 	}
 
-	netdev_dbg(bp->netdev, "mtu [%u] rx_buffer_size [%u]\n",
-		   bp->netdev->mtu, bp->ctx->rx_buffer_size);
+	netdev_dbg(bp->netdev, "mtu [%u] rx_buffer_size [%u]\n", mtu, size);
+	return size;
 }
 
 static void gem_free_rx_buffers(struct macb *bp)
@@ -3195,7 +3196,6 @@ static void macb_set_rx_mode(struct net_device *netdev)
 
 static int macb_open(struct net_device *netdev)
 {
-	size_t bufsz = netdev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
 	struct macb *bp = netdev_priv(netdev);
 	struct macb_queue *queue;
 	unsigned int q;
@@ -3214,7 +3214,7 @@ static int macb_open(struct net_device *netdev)
 	}
 
 	/* RX buffers initialization */
-	macb_init_rx_buffer_size(bp, bufsz);
+	bp->ctx->rx_buffer_size = macb_rx_buffer_size(bp, netdev->mtu);
 	bp->ctx->rx_ring_size = bp->configured_rx_ring_size;
 	bp->ctx->tx_ring_size = bp->configured_tx_ring_size;
 
Signature · 1 line
2.55.0
author maintainer
#12 lore raw

PATCH v9 10/17 net: macb: make `struct macb` subset reachable from macb_context struct

In reply to Théo Lebrun

For parallel MACB context to start become a reality, many functions need to stop operating on bp->ctx (the currently active context) and instead work on a context they get passed. That context might be
(1) the new one that is getting allocated and initialised, or,
(2) the old one to be freed.

To reduce bug surface area, we will taint those functions to *only* take a context and no struct macb *bp. That way, no bug of using bp->ctx instead of ctx will ever occur.

For that, we need to embed a subset of struct macb information into each context so that all helpers can still do their jobs. That subset must be constant once probe is completed. Do this by taking a pointer to a subset of macb called struct macb_info.

That subset is accessible from context (ctx->info->caps) or from bp (bp->caps) using -fms-extensions option, thanks to commit c4781dc3d1cf ("Kbuild: enable -fms-extensions").
https://gcc.gnu.org/onlinedocs/gcc/Unnamed-Fields.html

Add the structure and assign ctx->info at alloc,
but nothing uses it yet.

---
 drivers/net/ethernet/cadence/macb.h      | 31 ++++++++++++++++++++++---------
 drivers/net/ethernet/cadence/macb_main.c |  6 ++++++
 2 files changed, 28 insertions(+), 9 deletions(-)
drivers/net/ethernet/cadence/macb.h +22 -9
index 95cb494190c5..b4ae9e335709 100644
@@ -1284,6 +1284,17 @@ struct ethtool_rx_fs_list {
 	unsigned int count;
 };
 
+struct macb_info {
+	struct platform_device	*pdev;
+	struct net_device	*netdev;
+	struct macb_or_gem_ops	macbgem_ops;
+	unsigned int		num_queues;
+	u32			caps;
+	int			rx_bd_rd_prefetch;
+	int			tx_bd_rd_prefetch;
+	struct queue_stats	*queue_stats[MACB_MAX_QUEUES];
+};
+
 struct macb_rxq {
 	struct macb_dma_desc	*ring;		/* MACB & GEM */
 	dma_addr_t		ring_dma;	/* MACB & GEM */
@@ -1303,6 +1314,8 @@ struct macb_txq {
 };
 
 struct macb_context {
+	const struct macb_info	*info;
+
 	unsigned int		rx_buffer_size;
 	unsigned int		rx_ring_size;
 	unsigned int		tx_ring_size;
@@ -1318,6 +1331,15 @@ struct macb {
 	u32	(*macb_reg_readl)(struct macb *bp, int offset);
 	void	(*macb_reg_writel)(struct macb *bp, int offset, u32 value);
 
+	/*
+	 * Give direct access (bp->caps) and
+	 * allow taking a pointer to it (&bp->info) for contexts.
+	 */
+	union {
+		struct macb_info;
+		struct macb_info info;
+	};
+
 	/*
 	 * Context stores all its parameters.
 	 * But we must remember them across closure.
@@ -1329,17 +1351,14 @@ struct macb {
 	struct macb_dma_desc	*rx_ring_tieoff;
 	dma_addr_t		rx_ring_tieoff_dma;
 
-	unsigned int		num_queues;
 	struct macb_queue	queues[MACB_MAX_QUEUES];
 
 	spinlock_t		lock;
-	struct platform_device	*pdev;
 	struct clk		*pclk;
 	struct clk		*hclk;
 	struct clk		*tx_clk;
 	struct clk		*rx_clk;
 	struct clk		*tsu_clk;
-	struct net_device	*netdev;
 	/* Protects hw_stats and ethtool_stats */
 	spinlock_t		stats_lock;
 	union {
@@ -1347,15 +1366,12 @@ struct macb {
 		struct gem_stats	gem;
 	}			hw_stats;
 
-	struct macb_or_gem_ops	macbgem_ops;
-
 	struct mii_bus		*mii_bus;
 	struct phylink		*phylink;
 	struct phylink_config	phylink_config;
 	struct phylink_pcs	phylink_usx_pcs;
 	struct phylink_pcs	phylink_sgmii_pcs;
 
-	u32			caps;
 	unsigned int		dma_burst_length;
 
 	phy_interface_t		phy_interface;
@@ -1398,9 +1414,6 @@ struct macb {
 	struct delayed_work	tx_lpi_work;
 	u32			tx_lpi_timer;
 
-	int	rx_bd_rd_prefetch;
-	int	tx_bd_rd_prefetch;
-
 	u32	rx_intr_mask;
 
 	struct macb_pm_data pm_data;
drivers/net/ethernet/cadence/macb_main.c +6 -0
index d2acf44bb6ae..6c4f4f40157a 100644
@@ -3213,6 +3213,8 @@ static int macb_open(struct net_device *netdev)
 		goto pm_exit;
 	}
 
+	bp->ctx->info = &bp->info;
+
 	/* RX buffers initialization */
 	bp->ctx->rx_buffer_size = macb_rx_buffer_size(bp, netdev->mtu);
 	bp->ctx->rx_ring_size = bp->configured_rx_ring_size;
@@ -5154,6 +5156,7 @@ static int at91ether_open(struct net_device *netdev)
 		ret = -ENOMEM;
 		goto pm_exit;
 	}
+	bp->ctx->info = &bp->info;
 
 	/* Clear internal statistics */
 	ctl = macb_readl(bp, NCR);
@@ -5877,6 +5880,7 @@ static int macb_probe(struct platform_device *pdev)
 	u32 wtrmrk_rst_val;
 	void __iomem *mem;
 	struct macb *bp;
+	unsigned int q;
 	int num_queues;
 	bool native_io;
 	int err, val;
@@ -5922,6 +5926,8 @@ static int macb_probe(struct platform_device *pdev)
 	bp->netdev = netdev;
 	bp->regs = mem;
 	bp->native_io = native_io;
+	for (q = 0; q < MACB_MAX_QUEUES; q++)
+		bp->info.queue_stats[q] = &bp->queues[q].stats;
 	if (native_io) {
 		bp->macb_reg_readl = hw_readl_native;
 		bp->macb_reg_writel = hw_writel_native;
Signature · 1 line
2.55.0
author maintainer
#13 lore raw

PATCH v9 11/17 net: macb: change caps helpers signatures

In reply to Théo Lebrun

For parallel MACB context to start becoming a reality, many functions will soon not have access to struct macb *bp. Those will still have access to caps through ctx->info->caps.

Change all caps helpers signatures, from taking struct macb *bp to taking struct macb_info *info. Info is accessible in both bp and ctx and it ensures type safety (versus passing raw u32 caps).

Function list:

macb_is_gem()
gem_has_ptp()
macb_dma64()
macb_dma_ptp()
macb_dma_desc_get_size()
macb_set_addr()
macb_get_addr()
macb_64b_desc()
macb_tx_unmap()

Note: drop macb_64b_desc(bp, ...) parameter; it is unused and it must be dropped as macb_{set,get}_addr() call macb_64b_desc().

---
 drivers/net/ethernet/cadence/macb.h      |  21 +++--
 drivers/net/ethernet/cadence/macb_main.c | 157 ++++++++++++++++---------------
 drivers/net/ethernet/cadence/macb_ptp.c  |   8 +-
 3 files changed, 97 insertions(+), 89 deletions(-)
drivers/net/ethernet/cadence/macb.h +11 -10
index b4ae9e335709..5b082e071737 100644
@@ -834,7 +834,7 @@
  */
 #define macb_or_gem_writel(__bp, __reg, __value) \
 	({ \
-		if (macb_is_gem((__bp))) \
+		if (macb_is_gem(&(__bp)->info)) \
 			gem_writel((__bp), __reg, __value); \
 		else \
 			macb_writel((__bp), __reg, __value); \
@@ -843,7 +843,7 @@
 #define macb_or_gem_readl(__bp, __reg) \
 	({ \
 		u32 __v; \
-		if (macb_is_gem((__bp))) \
+		if (macb_is_gem(&(__bp)->info)) \
 			__v = gem_readl((__bp), __reg); \
 		else \
 			__v = macb_readl((__bp), __reg); \
@@ -1465,14 +1465,15 @@ static inline void gem_ptp_do_txstamp(struct macb *bp, struct sk_buff *skb, stru
 static inline void gem_ptp_do_rxstamp(struct macb *bp, struct sk_buff *skb, struct macb_dma_desc *desc) { }
 #endif
 
-static inline bool macb_is_gem(struct macb *bp)
+static inline bool macb_is_gem(const struct macb_info *info)
 {
-	return !!(bp->caps & MACB_CAPS_MACB_IS_GEM);
+	return !!(info->caps & MACB_CAPS_MACB_IS_GEM);
 }
 
-static inline bool gem_has_ptp(struct macb *bp)
+static inline bool gem_has_ptp(const struct macb_info *info)
 {
-	return IS_ENABLED(CONFIG_MACB_USE_HWSTAMP) && (bp->caps & MACB_CAPS_GEM_HAS_PTP);
+	return IS_ENABLED(CONFIG_MACB_USE_HWSTAMP) &&
+	       (info->caps & MACB_CAPS_GEM_HAS_PTP);
 }
 
 /* ENST Helper functions */
@@ -1488,16 +1489,16 @@ static inline u64 enst_max_hw_interval(u32 speed_mbps)
 			    ENST_TIME_GRANULARITY_NS * 1000, (speed_mbps));
 }
 
-static inline bool macb_dma64(struct macb *bp)
+static inline bool macb_dma64(const struct macb_info *info)
 {
 	return IS_ENABLED(CONFIG_ARCH_DMA_ADDR_T_64BIT) &&
-	       bp->caps & MACB_CAPS_DMA_64B;
+	       info->caps & MACB_CAPS_DMA_64B;
 }
 
-static inline bool macb_dma_ptp(struct macb *bp)
+static inline bool macb_dma_ptp(const struct macb_info *info)
 {
 	return IS_ENABLED(CONFIG_MACB_USE_HWSTAMP) &&
-	       bp->caps & MACB_CAPS_DMA_PTP;
+	       info->caps & MACB_CAPS_DMA_PTP;
 }
 
 static inline void macb_queue_isr_clear(struct macb *bp,
drivers/net/ethernet/cadence/macb_main.c +82 -75

554 lines not on this page · raw message

drivers/net/ethernet/cadence/macb_ptp.c +4 -4
index e5195d7dac1d..8765c4782509 100644
@@ -28,10 +28,10 @@
 static struct macb_dma_desc_ptp *macb_ptp_desc(struct macb *bp,
 					       struct macb_dma_desc *desc)
 {
-	if (!macb_dma_ptp(bp))
+	if (!macb_dma_ptp(&bp->info))
 		return NULL;
 
-	if (macb_dma64(bp))
+	if (macb_dma64(&bp->info))
 		return (struct macb_dma_desc_ptp *)
 				((u8 *)desc + sizeof(struct macb_dma_desc)
 				+ sizeof(struct macb_dma_desc_64));
@@ -384,7 +384,7 @@ int gem_get_hwtst(struct net_device *netdev,
 	struct macb *bp = netdev_priv(netdev);
 
 	*tstamp_config = bp->tstamp_config;
-	if (!macb_dma_ptp(bp))
+	if (!macb_dma_ptp(&bp->info))
 		return -EOPNOTSUPP;
 
 	return 0;
@@ -411,7 +411,7 @@ int gem_set_hwtst(struct net_device *netdev,
 	struct macb *bp = netdev_priv(netdev);
 	u32 regval;
 
-	if (!macb_dma_ptp(bp))
+	if (!macb_dma_ptp(&bp->info))
 		return -EOPNOTSUPP;
 
 	switch (tstamp_config->tx_type) {
Signature · 1 line
2.55.0
author maintainer
#14 lore raw

PATCH v9 12/17 net: macb: change function signatures to take contexts

In reply to Théo Lebrun

For parallel MACB context to start become a reality, many functions need to stop operating on bp->ctx (the currently active context) and instead work on a context they get passed. That context might be
(1) the new one that is getting allocated and initialised, or,
(2) the old one to be freed.

To reduce bug surface area, taint those functions to *only* take a context struct macb_context *ctx and no struct macb *bp. That way, no bug of using bp->ctx instead of ctx will ever occur.

We also convert functions that take a struct macb_queue *queue to instead take struct macb_context *ctx, unsigned int q, with q indexing ctx->txq[] and ctx->rxq[].

Full list:

macb_adj_dma_desc_idx()
macb_tx_ring_wrap()
macb_tx_desc()
macb_rx_ring_wrap()
macb_rx_desc()
macb_get_addr()
gem_rx_refill()
macb_init_rx_ring()
gem_free_rx_buffers()
macb_free_rx_buffers()
macb_tx_ring_size_per_queue()
macb_rx_ring_size_per_queue()
macb_free()
gem_alloc_rx_buffers()
macb_alloc_rx_buffers()
macb_alloc()
gem_init_rx_ring()
gem_init_rings()
macb_init_rings()

Note about gem_rx_refill(): it ends with a netdev_vdbg() that prints the queue pointer. Change to print the queue index because we do not have access to the queue anymore.

Note: functions that operate on the interface during its normal lifetime do not need to be converted and have no risk of using the wrong context. We do not allow concurrent swaps. When interface is alive, there can only be one context alive.

---
 drivers/net/ethernet/cadence/macb.h      |   7 +-
 drivers/net/ethernet/cadence/macb_main.c | 382 ++++++++++++++++---------------
 2 files changed, 206 insertions(+), 183 deletions(-)
drivers/net/ethernet/cadence/macb.h +4 -3
index 5b082e071737..ed9bd58d2e26 100644
@@ -1190,11 +1190,12 @@ static const struct gem_statistic queue_statistics[] = {
 
 struct macb;
 struct macb_queue;
+struct macb_context;
 
 struct macb_or_gem_ops {
-	int	(*mog_alloc_rx_buffers)(struct macb *bp);
-	void	(*mog_free_rx_buffers)(struct macb *bp);
-	void	(*mog_init_rings)(struct macb *bp);
+	int	(*mog_alloc_rx_buffers)(struct macb_context *ctx);
+	void	(*mog_free_rx_buffers)(struct macb_context *ctx);
+	void	(*mog_init_rings)(struct macb_context *ctx);
 	int	(*mog_rx)(struct macb_queue *queue, struct napi_struct *napi,
 			  int budget);
 };
drivers/net/ethernet/cadence/macb_main.c +202 -180

962 lines not on this page · raw message

Signature · 1 line
2.55.0
author maintainer
#15 lore raw

PATCH v9 13/17 net: macb: introduce macb_context_alloc() helper

In reply to Théo Lebrun

Move the context allocation sequence from inline macb_open() to its own helper function called macb_context_alloc(). All ops doing context swapping (set_ringparam, change_mtu, etc) will use this helper.

---
 drivers/net/ethernet/cadence/macb_main.c | 55 +++++++++++++++++++++-----------
 1 file changed, 36 insertions(+), 19 deletions(-)
drivers/net/ethernet/cadence/macb_main.c +36 -19
index f0a4a9dcc460..5a4eb87f5a97 100644
@@ -2868,6 +2868,36 @@ static int macb_alloc(struct macb_context *ctx)
 	return -ENOMEM;
 }
 
+static struct macb_context *macb_context_alloc(struct macb *bp,
+					       unsigned int mtu,
+					       unsigned int rx_ring_size,
+					       unsigned int tx_ring_size)
+{
+	struct macb_context *ctx;
+	int err;
+
+	ctx = kzalloc_obj(*ctx);
+	if (!ctx)
+		return ERR_PTR(-ENOMEM);
+
+	ctx->info = &bp->info;
+	ctx->rx_buffer_size = macb_rx_buffer_size(bp, mtu);
+	ctx->rx_ring_size = rx_ring_size;
+	ctx->tx_ring_size = tx_ring_size;
+
+	err = macb_alloc(ctx);
+	if (err) {
+		netdev_err(bp->netdev,
+			   "Unable to allocate DMA memory (error %d)\n", err);
+		kfree(ctx);
+		return ERR_PTR(err);
+	}
+
+	bp->macbgem_ops.mog_init_rings(ctx);
+
+	return ctx;
+}
+
 static void gem_init_rx_ring(struct macb_context *ctx, unsigned int q)
 {
 	struct macb_rxq *rxq = &ctx->rxq[q];
@@ -3236,27 +3266,15 @@ static int macb_open(struct net_device *netdev)
 	if (err < 0)
 		return err;
 
-	bp->ctx = kzalloc_obj(*bp->ctx);
-	if (!bp->ctx) {
-		err = -ENOMEM;
+	bp->ctx = macb_context_alloc(bp, netdev->mtu,
+				     bp->configured_rx_ring_size,
+				     bp->configured_tx_ring_size);
+	if (IS_ERR(bp->ctx)) {
+		err = PTR_ERR(bp->ctx);
+		bp->ctx = NULL;
 		goto pm_exit;
 	}
 
-	bp->ctx->info = &bp->info;
-
-	/* RX buffers initialization */
-	bp->ctx->rx_buffer_size = macb_rx_buffer_size(bp, netdev->mtu);
-	bp->ctx->rx_ring_size = bp->configured_rx_ring_size;
-	bp->ctx->tx_ring_size = bp->configured_tx_ring_size;
-
-	err = macb_alloc(bp->ctx);
-	if (err) {
-		netdev_err(netdev, "Unable to allocate DMA memory (error %d)\n",
-			   err);
-		goto free_ctx;
-	}
-
-	bp->macbgem_ops.mog_init_rings(bp->ctx);
 	macb_init_buffers(bp);
 
 	for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
@@ -3295,7 +3313,6 @@ static int macb_open(struct net_device *netdev)
 		napi_disable(&queue->napi_tx);
 	}
 	macb_free(bp->ctx);
-free_ctx:
 	kfree(bp->ctx);
 	bp->ctx = NULL;
 pm_exit:
Signature · 1 line
2.55.0
author maintainer
#16 lore raw

PATCH v9 14/17 net: macb: move printk() calls out of bp->lock critical section

In reply to Théo Lebrun

printk() while bp->lock is acquired is dangerous if netconsole is active on the interface. In that setup, we might land in macb_poll_controller() -> macb_interrupt() -> spin_lock(&bp->lock) but bp->lock is already acquired.

This is not an issue currently because macb_interrupt() first checks IRQ status, potentially early returns, then grabs lock. This early exit is expected in netpoll scenario.

However if we came to reading the status inside the bp->lock critical section (as it should be to avoid races), then this would turn into a deadlock. And we will.

Solution: move printk() calls out of the critical section, to ensure we can never netpoll under bp->lock's reign. Added benefit is a smaller and simpler atomic section.

--

In macb_tx_error_task(), defer netdev_err("halt tx timed out") call to after the critical section. Update the message to highlight it occurred in the past. Inherit the buffer exhaustion boolean variable name from the old code comment.

In macb_tx_error_task(), defer the netdev_err("TX buffers exhausted mid-frame") call out of the loop. This also means it goes from 1-per-error to 1-per-task-invocation.

In IRQ handling, move HRESP error printing out of macb_interrupt_misc() into macb_interrupt(). Again, it means we dedup error reporting if status is read multiple times in a row with HRESP bit set. This is fine as from past instances I've seen, this message spams our log if it occurs.

Notice we *ignore* debug printks; if you are debugging MACB maybe don't use netconsole...

---
 drivers/net/ethernet/cadence/macb_main.c | 26 ++++++++++++++++++--------
 1 file changed, 18 insertions(+), 8 deletions(-)
drivers/net/ethernet/cadence/macb_main.c +18 -8
index 5a4eb87f5a97..d23a0d08c276 100644
@@ -1303,6 +1303,7 @@ static void macb_tx_error_task(struct work_struct *work)
 	struct macb_tx_skb *tx_skb;
 	struct macb_dma_desc *desc;
 	bool halt_timeout = false;
+	bool buggy_driver = false;
 	struct sk_buff *skb;
 	unsigned long flags;
 	unsigned int tail;
@@ -1329,7 +1330,6 @@ static void macb_tx_error_task(struct work_struct *work)
 	 * macb/gem must be halted to write TBQP register
 	 */
 	if (macb_halt_tx(bp)) {
-		netdev_err(bp->netdev, "BUG: halt tx timed out\n");
 		macb_writel(bp, NCR, macb_readl(bp, NCR) & (~MACB_BIT(TE)));
 		halt_timeout = true;
 	}
@@ -1374,8 +1374,7 @@ static void macb_tx_error_task(struct work_struct *work)
 			 * those. Statistics are updated by hardware.
 			 */
 			if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
-				netdev_err(bp->netdev,
-					   "BUG: TX buffers exhausted mid-frame\n");
+				buggy_driver = true;
 
 			desc->ctrl = ctrl | MACB_BIT(TX_USED);
 		}
@@ -1412,7 +1411,14 @@ static void macb_tx_error_task(struct work_struct *work)
 	macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
 
 	spin_unlock_irqrestore(&bp->lock, flags);
+
 	napi_enable(&queue->napi_tx);
+
+	if (halt_timeout)
+		netdev_err(bp->netdev, "BUG: halt tx timed out, we ignored it\n");
+
+	if (buggy_driver)
+		netdev_err(bp->netdev, "BUG: TX buffers exhausted mid-frame\n");
 }
 
 static bool ptp_one_step_sync(struct sk_buff *skb)
@@ -2121,11 +2127,8 @@ static void gem_wol_interrupt(struct macb_queue *queue, u32 status)
 static int macb_interrupt_misc(struct macb_queue *queue, u32 status)
 {
 	struct macb *bp = queue->bp;
-	struct net_device *netdev;
 	u32 ctrl;
 
-	netdev = bp->netdev;
-
 	if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
 		queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
 		schedule_work(&queue->tx_error_task);
@@ -2165,7 +2168,6 @@ static int macb_interrupt_misc(struct macb_queue *queue, u32 status)
 
 	if (status & MACB_BIT(HRESP)) {
 		queue_work(system_bh_wq, &bp->hresp_err_bh_work);
-		netdev_err(netdev, "DMA bus error: HRESP not OK\n");
 		macb_queue_isr_clear(bp, queue, MACB_BIT(HRESP));
 	}
 
@@ -2185,6 +2187,7 @@ static irqreturn_t macb_interrupt(int irq, void *dev_id)
 	struct macb_queue *queue = dev_id;
 	struct macb *bp = queue->bp;
 	struct net_device *netdev = bp->netdev;
+	bool hresp_err = false;
 	u32 status;
 
 	status = queue_readl(queue, ISR);
@@ -2231,15 +2234,22 @@ static irqreturn_t macb_interrupt(int irq, void *dev_id)
 			napi_schedule_irqoff(&queue->napi_tx);
 		}
 
-		if (unlikely(status & MACB_INT_MISC_FLAGS))
+		if (unlikely(status & MACB_INT_MISC_FLAGS)) {
 			if (macb_interrupt_misc(queue, status))
 				break;
 
+			if (status & MACB_BIT(HRESP))
+				hresp_err = true;
+		}
+
 		status = queue_readl(queue, ISR);
 	}
 
 	spin_unlock(&bp->lock);
 
+	if (hresp_err)
+		netdev_err(netdev, "DMA bus error: HRESP not OK\n");
+
 	return IRQ_HANDLED;
 }
 
Signature · 1 line
2.55.0
author maintainer
#17 lore raw

PATCH v9 15/17 net: macb: read ISR inside bp->lock critical section

In reply to Théo Lebrun

The IRQ handler reads ISR register into the status stack variable. If empty, it early returns. Else, it grabs bp->lock and iterates on the status bits.

We risk a race on spinlock acquire; status might have changed. Move the readl(ISR) inside the bp->lock critical section.

In practice, we protect ourselves from interpreting the same ISR status twice. To understand issues, read macb_interrupt() body and imagine each codepath being performed twice:
- Two napi_schedule_irqoff() are idempotent *if fast enough*.
- Two schedule_work(tx_error_task) are idempotent *if fast enough*.
- Double increment of rx_overruns.
- Two queue_work(hresp_err_bh_work) are idempotent *if fast enough*.
- Two pm_wakeup_event() is safe.

One risk remains with spurious interrupts that would, in addition to taking excessive CPU time, also create lock contention. How bad is it? Probably not too bad.

---
 drivers/net/ethernet/cadence/macb_main.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
drivers/net/ethernet/cadence/macb_main.c +6 -5
index d23a0d08c276..6d79663e42bf 100644
@@ -2190,13 +2190,14 @@ static irqreturn_t macb_interrupt(int irq, void *dev_id)
 	bool hresp_err = false;
 	u32 status;
 
-	status = queue_readl(queue, ISR);
-
-	if (unlikely(!status))
-		return IRQ_NONE;
-
 	spin_lock(&bp->lock);
 
+	status = queue_readl(queue, ISR);
+	if (unlikely(!status)) {
+		spin_unlock(&bp->lock);
+		return IRQ_NONE;
+	}
+
 	while (status) {
 		/* close possible race with dev_close */
 		if (unlikely(!netif_running(netdev))) {
Signature · 1 line
2.55.0
author maintainer
#18 lore raw

Re: [PATCH net-next v9 15/17] net: macb: read ISR inside bp->lock critical section

Replying to non-net sashiko
https://sashiko.dev/#/patchset/20260812-macb-context-v9-0-7ddbf5f715e0%40bootlin.com

Quoted 31 lines

commit 48c8a049fccfd5d7c55b15a7fd08dc4eca66d836

Author: Théo Lebrun <[email protected]>
Date:   Wed Aug 12 08:03:29 2026 +0000

net: macb: read ISR inside bp->lock critical section

The IRQ handler reads ISR register into the status stack variable. If empty, it early returns. Else, it grabs bp->lock and iterates on the status bits.

We risk a race on spinlock acquire; status might have changed. Move the readl(ISR) inside the bp->lock critical section.

drivers/net/ethernet/cadence/macb_main.c static irqreturn_t macb_interrupt(int irq, void *dev_id) · line 2193

Quoted 10 lines
drivers/net/ethernet/cadence/macb_main.c +1 -1
index d23a0d08c2769..6d79663e42bfe 100644
@@ -2190,12 +2190,13 @@ static irqreturn_t macb_interrupt(int irq, void *dev_id)
 	bool hresp_err = false;
 	u32 status;
 
-	status = queue_readl(queue, ISR);
+	spin_lock(&bp->lock);

[Severity: High]
Does taking this lock unconditionally cause severe latency spikes for other devices on a shared interrupt line?

If a shared interrupt fires while the driver's error handling task is running, this CPU might spin with local interrupts disabled for up to 14ms:

Quoted 6 lines

macb_tx_error_task()

    spin_lock(&bp->lock);
    macb_halt_tx()
        read_poll_timeout_atomic(..., MACB_HALT_TIMEOUT, ...)

(MACB_HALT_TIMEOUT is 14000us)

Yes this is correct. And something we will live with.
- Our IRQ line is shared only with other queues of the same MAC.
- That timeout is scaled to 10 Mbits/s, on 1G this timeout should be 100 times smaller. We just don't bother scaling it with link speed.

[...]

Quoted 8 lines

drivers/net/ethernet/cadence/macb_main.c static irqreturn_t macb_interrupt(int irq, void *dev_id) · line 2195

Quoted 2 lines
drivers/net/ethernet/cadence/macb_main.c +1 -1
-	if (unlikely(!status))
+	status = queue_readl(queue, ISR);

[Severity: High]
This is a pre-existing issue, but can this unconditional hardware register read trigger a synchronous external abort (bus fault) if the MAC is in runtime suspend?

Quoted 4 lines

If another device on the same shared IRQ line triggers an interrupt while the MAC's clocks are gated via macb_clks_disable() in macb_runtime_suspend(), accessing memory-mapped registers of a clock-gated peripheral typically causes a bus fault.

Again, we have IRQF_SHARED only to share with other queues of the same MAC. So that theory is moot. Long term I'd like to drop IRQF_SHARED.

Quoted 8 lines
Quoted 8 lines
drivers/net/ethernet/cadence/macb_main.c +3 -2
+	if (unlikely(!status)) {
+		spin_unlock(&bp->lock);
 		return IRQ_NONE;
-
-	spin_lock(&bp->lock);
+	}
 
Quoted 6 lines

drivers/net/ethernet/cadence/macb_main.c static irqreturn_t macb_interrupt(int irq, void *dev_id) · line 2201

Quoted 1 line
drivers/net/ethernet/cadence/macb_main.c +0 -0
 	while (status) {

[Severity: High]
This is a pre-existing issue, but will this loop infinitely if an unhandled interrupt bit (like PFR) is set on hardware with Write-1-to-Clear ISR behavior?

Yes. Long term I'd like to introduce a max iteration count. Completely unrelated to context swapping.

Quoted 6 lines

[Severity: High]
This isn't a bug introduced by this patch, but does the driver have a use-after-free risk during interface closure or driver removal?

In macb_close(), the context structure is freed without synchronizing with or cancelling the asynchronous error tasks:

Yes. This one I'll fix. See previous email.

Re: [PATCH net-next v9 08/17] net: macb: introduce macb_context struct for buffer management
https://lore.kernel.org/netdev/[email protected]/

[...]

---

Replying to net sashiko
https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260812-macb-context-v9-0-7ddbf5f715e0%40bootlin.com

Quoted 13 lines

commit 4cc0cf203c223199f230330ef1014c2cac4c4841
Author: Théo Lebrun <[email protected]>

net: macb: read ISR inside bp->lock critical section

This moves the queue_readl(queue, ISR) in macb_interrupt() from before the bp->lock acquisition to inside the critical section, and unlocks on the early return path when status is zero. The stated goal is to avoid acting on a stale ISR sample.

Quoted 3 lines

We risk a race on spinlock acquire; status might have changed. Move the readl(ISR) inside the bp->lock critical section.

Quoted 8 lines
Quoted 2 lines

In practice, we protect ourselves from interpreting the same ISR status twice.

[Severity: Low]
[Finding: gpt-5-6-sol-1-3]
[Sources: gpt-5-6-sol, opus-5]
Could the changelog name the two contexts that can observe the same ISR sample, and the hardware precondition?

If any other context clears an IRQ then we race against that. Grep for macb_queue_isr_clear in the code; for example the suspend !WOL case. Maybe the commit message is badly written; it isn't a notion of interpreting the same bit twice.

[...]

Quoted 25 lines
Quoted 9 lines
drivers/net/ethernet/cadence/macb_main.c +0 -1
index d23a0d08c276..6d79663e42bf 100644
@@ -2190,12 +2190,13 @@ static irqreturn_t macb_interrupt(int irq, void *dev_id)
 	bool hresp_err = false;
 	u32 status;
 
-	status = queue_readl(queue, ISR);

drivers/net/ethernet/cadence/macb_main.c static irqreturn_t macb_interrupt(int irq, void *dev_id) · line 2199

Quoted 10 lines
drivers/net/ethernet/cadence/macb_main.c +5 -3
+	spin_lock(&bp->lock);
 
-	if (unlikely(!status))
+	status = queue_readl(queue, ISR);
+	if (unlikely(!status)) {
+		spin_unlock(&bp->lock);
 		return IRQ_NONE;
-
-	spin_lock(&bp->lock);
+	}

[Severity: Medium]
[Finding: main-3-6]
[Sources: opus-5]
Does this remove the lock-free "not my interrupt" fast path?

Quoted 1 line

The handler is registered shared in macb_init():

Yes! Once again, we share only with other queues of the same MAC. We don't support running alongside other devices.

[...]

Thanks,

Signature · 3 lines
Théo Lebrun, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
author maintainer
#19 lore raw

PATCH v9 16/17 net: macb: use context swapping in .set_ringparam()

In reply to Théo Lebrun

ethtool_ops.set_ringparam() is implemented using the primitive close / update ring size / reopen sequence. Under memory pressure this does not
fly: we free our buffers at close and cannot reallocate new ones at open. Also, it triggers a slow PHY reinit.

Instead, exploit the new context mechanism and improve our sequence to:
- allocate a new context (including buffers) first
- if it fails, early return without any impact to the interface
- stop interface
- update global state (bp, netdev, etc)
- pass buffer pointers to the hardware
- start interface
- free old context.

The HW disable sequence is inspired by macb_reset_hw() but avoids
(1) setting NCR bit CLRSTAT and (2) clearing register PBUFRXCUT.

The HW re-enable sequence is inspired by macb_mac_link_up(), skipping over register writes which would be redundant (because values have not changed).

The generic context swapping parts are isolated into helper functions macb_context_swap_start|end(), reusable by other operations (change_mtu, set_channels, etc).

Introduce a new locking primitive (mac_cfg_lock mutex) to serialise swap with phylink MAC callbacks. Avoid stopping phylink to avoid a slow PHY retrain. We cannot sync to phylink ops using phydev->lock because it is not available in the SFP case. We cannot check link state using netif_carrier_ok() because we could race with its changes; so we use a redundant bp->link_up boolean that is mac_cfg_lock protected.

AT91 EMAC is handled differently as their buffer management is separate and they don't do NAPI. They must never call swap_start/end(). Anyway they do not implement set_ringparam (-EOPNOTSUPP) so we are safe.

---
 drivers/net/ethernet/cadence/macb.h      |   8 ++
 drivers/net/ethernet/cadence/macb_main.c | 190 ++++++++++++++++++++++++++++---
 2 files changed, 185 insertions(+), 13 deletions(-)
drivers/net/ethernet/cadence/macb.h +8 -0
index ed9bd58d2e26..35564f2ed662 100644
@@ -1355,6 +1355,8 @@ struct macb {
 	struct macb_queue	queues[MACB_MAX_QUEUES];
 
 	spinlock_t		lock;
+	/* Serializes context swap against phylink MAC callbacks. */
+	struct mutex		mac_cfg_lock;
 	struct clk		*pclk;
 	struct clk		*hclk;
 	struct clk		*tx_clk;
@@ -1415,6 +1417,12 @@ struct macb {
 	struct delayed_work	tx_lpi_work;
 	u32			tx_lpi_timer;
 
+	/* ISR must not drive NAPI & BH mechanisms. Protected by bp->lock. */
+	bool			ctx_swap;
+
+	/* Redundant to netif_carrier_ok(), but set under bp->mac_cfg_lock. */
+	bool			link_up;
+
 	u32	rx_intr_mask;
 
 	struct macb_pm_data pm_data;
drivers/net/ethernet/cadence/macb_main.c +177 -13

287 lines not on this page · raw message

Signature · 1 line
2.55.0
author maintainer
#20 lore raw

Re: [PATCH net-next v9 16/17] net: macb: use context swapping in .set_ringparam()

Replying to non-net sashiko
https://sashiko.dev/#/patchset/20260812-macb-context-v9-0-7ddbf5f715e0%40bootlin.com

Quoted 18 lines

commit 34a47bcab06bd390622de5b8628e1ca4b22fa12e
Author: Théo Lebrun <[email protected]>
Subject: net: macb: use context swapping in .set_ringparam()

This commit modifies the ethtool set_ringparam implementation to use a new context swapping mechanism instead of the previous close and reopen sequence. It introduces macb_context_swap_start() and macb_context_swap_end() helpers to handle the transition smoothly, along with a new mac_cfg_lock to serialize changes against phylink MAC callbacks.

Quoted 8 lines
drivers/net/ethernet/cadence/macb_main.c +0 -0
index 6d79663e42bfe..55c97f2035e76 100644
@@ -814,6 +831,10 @@ static void macb_mac_link_down(struct phylink_config *config, unsigned int mode,
 	unsigned int q;
 	u32 ctrl;
 
Quoted 13 lines

drivers/net/ethernet/cadence/macb_main.c static void macb_mac_link_down(struct phylink_config *config, unsigned int mode, · line 845

Quoted 9 lines
drivers/net/ethernet/cadence/macb_main.c +4 -0
+	mutex_lock(&bp->mac_cfg_lock);
+
+	bp->link_up = false;
+
 	if (!(bp->caps & MACB_CAPS_MACB_IS_EMAC))
 		for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
 			queue_writel(queue, IDR,
@@ -824,6 +845,8 @@ static void macb_mac_link_down(struct phylink_config *config, unsigned int mode,
 	macb_writel(bp, NCR, ctrl);

[Severity: High]
This is a pre-existing issue, but does this read-modify-write of NCR need protection to avoid dropping hardware configuration?

Indeed. Unrelated but will do.

Quoted 34 lines
Quoted 4 lines
drivers/net/ethernet/cadence/macb_main.c +2 -0
 	netif_tx_stop_all_queues(netdev);
+
+	mutex_unlock(&bp->mac_cfg_lock);
 }

[ ... ]

Quoted 27 lines
drivers/net/ethernet/cadence/macb_main.c +23 -0
@@ -3105,6 +3135,134 @@ static void macb_configure_dma(struct macb *bp)
 	}
 }
 
+static void macb_context_swap_start(struct macb *bp)
+{
+	struct macb_queue *queue;
+	unsigned long flags;
+	unsigned int q;
+	u32 ctrl;
+
+	mutex_lock(&bp->mac_cfg_lock);
+
+	/* We cannot mask IRQs because they'll get re-armed by BH. So instead we
+	 * signal to IRQ handler it shouldn't drive BH features and should
+	 * self-disarm.
+	 */
+	spin_lock_irqsave(&bp->lock, flags);
+	bp->ctx_swap = true;
+	spin_unlock_irqrestore(&bp->lock, flags);
+
+	/* Drain BH features. HW is still active and usable at this point but
+	 * IRQs are being ignored.
+	 */
+
+	cancel_work_sync(&bp->hresp_err_bh_work);
+
Quoted 8 lines

drivers/net/ethernet/cadence/macb_main.c static void macb_configure_dma(struct macb *bp) · line 3163

Quoted 3 lines
drivers/net/ethernet/cadence/macb_main.c +3 -0
+	for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
+		/* Must be done before NAPI is disabled. */
+		cancel_work_sync(&queue->tx_error_task);

[Severity: High]
This is a pre-existing issue, but since we are carefully cancelling worker threads here during context swap, should we also be cancelling them during macb_close() and macb_suspend()?

Yes I'll integrate all that for next revision. See this email where I went into more details.
https://lore.kernel.org/netdev/[email protected]/

[...]

Quoted 16 lines

drivers/net/ethernet/cadence/macb_main.c static void macb_configure_dma(struct macb *bp) · line 3175

Quoted 12 lines
drivers/net/ethernet/cadence/macb_main.c +12 -0
+
+		napi_disable(&queue->napi_rx);
+		napi_disable(&queue->napi_tx);
+	}
+
+	/* Must be done after napi_tx is disabled. */
+	cancel_delayed_work_sync(&bp->tx_lpi_work);
+
+	/* Can finally disable software Tx; need to wait until napi_tx and
+	 * tx_error_task cannot be scheduled as either might wakeup Tx.
+	 */
+	netif_tx_disable(bp->netdev);

[Severity: High]
Does this sequence leave the interface vulnerable to a false TX timeout?

Quoted 3 lines

When macb_context_swap_start() stops the software TX queues via netif_tx_disable(), it does not suspend the watchdog via netif_carrier_off() or update the queue's trans_start time.

Well actually netif_tx_disable() does reset dev_queue->trans_start of all queues. So a race does exist but it is pretty slim (not the full context swap): either before this netif_tx_disable() line or if context swap lasts more than 5s.

I'll protect this by (1) grabbing bp->lock inside macb_tx_restart() and
(2) checking the state of our boolean flag to know if context swap is ongoing.

Quoted 11 lines
Quoted 2 lines
drivers/net/ethernet/cadence/macb_main.c +2 -0
+
+	/* Now that everything is stopped, clear DQL. */

[ ... ]

Quoted 3 lines
drivers/net/ethernet/cadence/macb_main.c +3 -0
+static void macb_context_swap_end(struct macb *bp,
+				  struct macb_context *new_ctx)
+{

[ ... ]

Quoted 8 lines

drivers/net/ethernet/cadence/macb_main.c static void macb_configure_dma(struct macb *bp) · line 3263

Quoted 4 lines
drivers/net/ethernet/cadence/macb_main.c +4 -0
+	/* Free old context. */
+
+	macb_free(old_ctx);
+	kfree(old_ctx);

[Severity: Critical]
This is a pre-existing issue, but could this initialization sequence lead to severe memory corruption if we are under memory pressure?

Yes, that's the whole point.

Quoted 6 lines

macb_init_buffers() is called earlier in this function to swap to the new context. When initializing DMA rings, gem_init_rings() calls gem_init_rx_ring(), which leaves the newly allocated descriptors completely zeroed, and then calls gem_rx_refill().

Quoted 7 lines

If netdev_alloc_skb() fails due to memory pressure, gem_rx_refill() aborts early:

    if (unlikely(!skb)) {
        netdev_err(bp->netdev, "Unable to allocate sk_buff\n");
        break;
    }

That's the main issue: gem_rx_refill() should propagate the error and make the context swap fail. This patch exists but was part of the XDP series. I'll take it along the ride as it's needed to make context swap reasonable.

[...]

Quoted 42 lines
Quoted 14 lines
drivers/net/ethernet/cadence/macb_main.c +3 -2
@@ -3829,9 +3987,10 @@ static int macb_set_ringparam(struct net_device *netdev,
 			      struct kernel_ethtool_ringparam *kernel_ring,
 			      struct netlink_ext_ack *extack)
 {
+	unsigned int new_rx_size, new_tx_size;
 	struct macb *bp = netdev_priv(netdev);
-	u32 new_rx_size, new_tx_size;
-	unsigned int reset = 0;
+	bool running = netif_running(netdev);
+	struct macb_context *new_ctx;
 
 	if (bp->caps & MACB_CAPS_MACB_IS_EMAC)
 		return -EOPNOTSUPP;

drivers/net/ethernet/cadence/macb_main.c static int macb_set_ringparam(struct net_device *netdev, · line 4017

Quoted 9 lines
drivers/net/ethernet/cadence/macb_main.c +3 -3
@@ -3853,16 +4012,20 @@ static int macb_set_ringparam(struct net_device *netdev,
 		return 0;
 	}
 
-	if (netif_running(bp->netdev)) {
-		reset = 1;
-		macb_close(bp->netdev);
+	if (running) {
+		new_ctx = macb_context_alloc(bp, netdev->mtu,
+					     new_rx_size, new_tx_size);

[Severity: High]
Can this allocation path lead to a kernel panic and memory leak on failure?

When macb_context_alloc() is called here under memory pressure, it calls macb_alloc().

In macb_alloc(), if the RX DMA allocation fails (or boundary check fails), it jumps to the error path after allocating tx:

drivers/net/ethernet/cadence/macb_main.c:macb_alloc() {
    ...
    if (!rx || upper_32_bits(rx_dma) != upper_32_bits(rx_dma + size - 1))
        goto out_err;
    ...
out_err:
    macb_free(ctx);
}
Quoted 7 lines

At this point, txq->ring is still NULL. macb_free() will unconditionally call:

    dma_free_coherent(dev, size, txq->ring, txq->ring_dma);

Will this trigger a panic on architectures where dma_free_coherent() lacks tolerance for a NULL virtual address, and permanently leak the successfully allocated TX DMA ring?

I don't see where dma_free_coherent() "lacks tolerance for a NULL virtual address". That would be surprising.

However we do have a leak yes, which I plan on fixing (still unrelated to context swapping).

Quoted 6 lines

drivers/net/ethernet/cadence/macb_main.c static int macb_set_ringparam(struct net_device *netdev, · line 4022

Quoted 6 lines
drivers/net/ethernet/cadence/macb_main.c +4 -0
+		if (IS_ERR(new_ctx))
+			return PTR_ERR(new_ctx);
+
+		macb_context_swap_start(bp);
 	}

---

Replying to net sashiko
https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260812-macb-context-v9-0-7ddbf5f715e0%40bootlin.com

Quoted 31 lines

commit c12dbce3061c66a490797075684d70f4e544b34f
Author: Théo Lebrun <[email protected]>

net: macb: use context swapping in .set_ringparam()

This replaces the close / resize / reopen sequence in ethtool_ops.set_ringparam() with a context swap: a new context (rings and buffers) is allocated first, the datapath is quiesced via a new bp->ctx_swap flag plus NAPI/work draining, the new context is handed to the hardware, and the old context is freed afterwards. A new mac_cfg_lock mutex serialises the swap against the phylink MAC callbacks, and a redundant bp->link_up boolean tracks link state under that mutex.

A few questions below, first on the changelog and then inline in the diff.

[Severity: Low]
[Finding: main-1-0]
[Sources: opus-5]
The changelog describes the new mutex and the redundant link state boolean:

Introduce a new locking primitive (mac_cfg_lock mutex) to serialise swap with phylink MAC callbacks. [...] so we use a redundant bp->link_up boolean that is mac_cfg_lock protected.

but the second new state flag, bp->ctx_swap, is not mentioned anywhere, and neither is the change of macb_interrupt() semantics that goes with it. While a swap is in progress the handler treats every pending cause as if the device were down (IDR -1, ISR clear, break), so MACB_INT_MISC_FLAGS handling and HRESP DMA bus errors are acknowledged and discarded for the duration of the window.

Quoted 3 lines

Could the changelog say a word about bp->ctx_swap and about the interrupt handler behaviour during the swap, since correctness of the sequence depends on both?

Good point, this is missing!

Quoted 50 lines
Quoted 5 lines
drivers/net/ethernet/cadence/macb.h +0 -0
index ed9bd58d2e26..35564f2ed662 100644

[ ... ]

Quoted 17 lines
drivers/net/ethernet/cadence/macb.h +6 -0
@@ -1415,6 +1417,12 @@ struct macb {
 	struct delayed_work	tx_lpi_work;
 	u32			tx_lpi_timer;
 
+	/* ISR must not drive NAPI & BH mechanisms. Protected by bp->lock. */
+	bool			ctx_swap;
+
+	/* Redundant to netif_carrier_ok(), but set under bp->mac_cfg_lock. */
+	bool			link_up;
+
 	u32	rx_intr_mask;
 
 	struct macb_pm_data pm_data;
drivers/net/ethernet/cadence/macb_main.c +0 -0
index 6d79663e42bf..55c97f2035e7 100644

[ ... ]

drivers/net/ethernet/cadence/macb_main.c static irqreturn_t macb_interrupt(int irq, void *dev_id) · line 2236

Quoted 13 lines
drivers/net/ethernet/cadence/macb_main.c +4 -2
@@ -2199,8 +2227,10 @@ static irqreturn_t macb_interrupt(int irq, void *dev_id)
 	}
 
 	while (status) {
-		/* close possible race with dev_close */
-		if (unlikely(!netif_running(netdev))) {
+		/* close possible race with dev_close, and with context-swap
+		 * teardown
+		 */
+		if (unlikely(!netif_running(netdev) || bp->ctx_swap)) {
 			queue_writel(queue, IDR, -1);
 			macb_queue_isr_clear(bp, queue, -1);
 			break;

[Severity: Medium]
[Finding: main-5-8]
[Sources: opus-5]
bp->ctx_swap is set on entry to the swap region in macb_context_swap_start() and cleared on exit in macb_context_swap_end(), purely to keep the interrupt handler away from state the swap is mutating. Is a boolean the right mechanism here rather than masking the per-queue IRQs?

Quoted 3 lines

The driver already owns queue->irq, so disable_irq()/enable_irq(), or IMR masking plus synchronize_irq(), would enforce the exclusion instead of relying on the handler observing the flag.

No the masking+synchronize_irq() doesn't work. If we start context swapping by masking interrupts, NAPI/BH is still active and might re-arm IRQs. See how napi_rx/napi_tx/tx_error_task/hresp_task all can enable/disable IRQs, and they are not yet disabled/cancelled.

So then we might think "we'll disable NAPI/BH features first then disable IRQs". But that doesn't fly either! Notice how IRQs schedule the NAPI/BH features and we can't have that if they are disabled. Eg napi_disable on an already disabled NAPI is a deadlock.

|               |   enable/disable   |           schedule            |
|               |----|-------|-------|------|----|----|--------|-----|
|               |IRQs|napi_tx|napi_rx|tx_lpi|napi|napi|tx_error|hresp|
| Context       |    |       |       | task | rx | tx |  task  |task |
|===============|====|=======|=======|======|====|====|========|=====|
| open          | X  |   X   |   X   |      |    |    |        |     |
| link_up       | X  |       |       |      |    |    |        |     |
| link_down     | X  |       |       |      |    |    |        |     |
| close         | X  |   X   |   X   |      |    |    |        |     |
| enable_tx_lpi |    |       |       |  X   |    |    |        |     |
| swap          | X  |   X   |   X   |  X   |    |    |        |     |
| suspend       | X  |   X   |   X   |      |    |    |        |     |
| resume        | X  |   X   |   X   |      |    |    |        |     |
|---------------|----|-------|-------|------|----|----|--------|-----|
| irq & netpoll | X  |       |       |      | X  | X  |   X    | X   |
|---------------|----|-------|-------|------|----|----|--------|-----|
| napi_rx       | X  |       |       |      | X  |    |        |     |
| napi_tx       | X  |       |       |  X   |    | X  |        |     |
|---------------|----|-------|-------|------|----|----|--------|-----|
| tx_error_task | X  |   X   |       |      |    |    |        |     |
| hresp task    | X  |       |       |      |    |    |        |     |

The reason disable_irq()/enable_irq() doesn't work is more long term. We also need the same "shutdown IRQs atomically" mechanism for the suspend WOL path. In that case we need to shutdown all but one IRQ. There's the same thing for when the interface is down. We'll use this "quiesce IRQs" mechanism as well, but might want to keep some activated.

We're not the first ones using such a mechanism. Check out tg3 for example and its tg3_irq_quiesce() function.
https://elixir.bootlin.com/linux/v7.2.2/source/drivers/net/ethernet/broadcom/tg3.c#L7488

[...]

Quoted 10 lines
Quoted 7 lines
drivers/net/ethernet/cadence/macb_main.c +2 -0
@@ -3105,6 +3135,134 @@ static void macb_configure_dma(struct macb *bp)
 	}
 }
 
+static void macb_context_swap_start(struct macb *bp)
+{

[ ... ]

Quoted 14 lines

drivers/net/ethernet/cadence/macb_main.c static void macb_configure_dma(struct macb *bp) · line 3175

Quoted 7 lines
drivers/net/ethernet/cadence/macb_main.c +7 -0
+	/* Must be done after napi_tx is disabled. */
+	cancel_delayed_work_sync(&bp->tx_lpi_work);
+
+	/* Can finally disable software Tx; need to wait until napi_tx and
+	 * tx_error_task cannot be scheduled as either might wakeup Tx.
+	 */
+	netif_tx_disable(bp->netdev);

[Severity: High]
[Finding: gpt-5-6-sol-5-11]
[Sources: gpt-5-6-sol, opus-5]
This stops all TX queues while netif_running() and the carrier both stay set, since this is an ethtool op and phylink is deliberately not stopped. Does that leave the netdev TX watchdog free to run during the swap?

Yes! See above (grep for "^Well actually") where I explained how we fix this TX watchdog race. And we explicitely don't touch phylink because we don't want any PHY retrain. Gotta go fast.

[...]

Quoted 21 lines
Quoted 10 lines
drivers/net/ethernet/cadence/macb_main.c +9 -0
+	spin_unlock_irqrestore(&bp->lock, flags);
+}
+
+static void macb_context_swap_end(struct macb *bp,
+				  struct macb_context *new_ctx)
+{
+	struct macb_context *old_ctx;
+	struct macb_queue *queue;
+	unsigned long flags;

drivers/net/ethernet/cadence/macb_main.c static void macb_configure_dma(struct macb *bp) · line 3221

Quoted 10 lines
drivers/net/ethernet/cadence/macb_main.c +10 -0
+	unsigned int q;
+	u32 ctrl;
+
+	lockdep_assert_held(&bp->mac_cfg_lock);
+
+	/* Swap contexts & give buffer pointers to HW. */
+
+	old_ctx = bp->ctx;
+	bp->ctx = new_ctx;
+	macb_init_buffers(bp);
Quoted 7 lines

[Severity: Medium]
[Finding: main-5-9]
[Sources: opus-5]
bp->ctx is published here with a plain store, with no bp->lock, no WRITE_ONCE() and no barrier, and old_ctx is dma-freed and kfree()d at the end of this function after mac_cfg_lock has been dropped and after NAPI, software TX and hardware RX/TX have been restarted.

Yes that's a mistake. Should be WRITE_ONCE() followed by mb(). All codepaths that assign bp->ctx should look like that.

Btw, I learnt something reading a good chunk of Documentation/memory-barriers.txt: "Memory operations issued before the ACQUIRE may be completed after the ACQUIRE operation has completed". So here we do

   bp->ctx = ...;
   spin_lock(...);
   relaxed_writel(...);

And the spin_lock isn't implicitely introducing a mandatory mb. TIL.

Quoted 7 lines

Every other non-rtnl reader of bp->ctx runs from hardirq, softirq or work context under bp->lock or queue->tx_ptr_lock, for example macb_tx_lpi_work_fn() -> macb_tx_all_queues_idle():

    for (q = 0; q < bp->num_queues; ++q) {
        txq = &bp->ctx->txq[q];
Quoted 7 lines

so the safety of the swap rests entirely on the drain list in macb_context_swap_start() being exhaustive. The macb_tx_timeout() -> macb_tx_restart() path noted above is one reader that is not on it.

Would doing the pointer swap under bp->lock (taken a few lines below anyway), and freeing old_ctx only after a synchronisation point, make that contract enforced rather than assumed?

No let's not do this. It is "taken a few lines below anyway" but inbetween there's napi_enable() and it mustn't be done in bp->lock critical section and the sequence ordering must stay as-is.

And doing the ctx write in a bp->lock critical section doesn't help anything if in the same section we also write some MMIO registers (taking the same example as below).

Quoted 28 lines
Quoted 17 lines
drivers/net/ethernet/cadence/macb_main.c +16 -0
+
+	/* Start NAPI, HW Tx/Rx and software Tx. */
+
+	for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
+		napi_enable(&queue->napi_rx);
+		napi_enable(&queue->napi_tx);
+	}
+
+	spin_lock_irqsave(&bp->lock, flags);
+
+	/* Re-arm normal interrupt processing before enabling IRQs. */
+	bp->ctx_swap = false;
+
+	macb_configure_dma(bp);
+
+	if (bp->link_up) {

drivers/net/ethernet/cadence/macb_main.c static void macb_configure_dma(struct macb *bp) · line 3247

Quoted 10 lines
drivers/net/ethernet/cadence/macb_main.c +10 -0
+		for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
+			queue_writel(queue, IER,
+				     bp->rx_intr_mask |
+				     MACB_TX_INT_FLAGS |
+				     MACB_BIT(HRESP));
+		}
+
+		ctrl = macb_readl(bp, NCR);
+		macb_writel(bp, NCR, ctrl | MACB_BIT(RE) | MACB_BIT(TE));
+	}
Quoted 7 lines

[Severity: Medium]
[Finding: gpt-5-6-sol-7-14]
[Sources: gpt-5-6-sol, opus-5]
The descriptors for new_ctx were written earlier, in macb_context_alloc() -> mog_init_rings(), and the ring base registers were handed over just above by macb_init_buffers(); RE|TE are then enabled here. Is a wmb() missing between the descriptor stores and those register writes?

Covered above.

[...]

Quoted 7 lines
Quoted 7 lines
drivers/net/ethernet/cadence/macb_main.c +7 -0
+	spin_unlock_irqrestore(&bp->lock, flags);
+
+	if (bp->link_up) {
+		netif_tx_wake_all_queues(bp->netdev);
+
+		if (bp->eee_active)
+			macb_txp_lpi_initial_defer(bp);
Quoted 8 lines

drivers/net/ethernet/cadence/macb_main.c

Quoted 1 line
drivers/net/ethernet/cadence/macb_main.c +1 -0
+	}

[Severity: Medium]
[Finding: main-2-3]
[Sources: opus-5]
bp->link_up is documented in macb.h as "Redundant to netif_carrier_ok(), but set under bp->mac_cfg_lock", and it gates both IER/RE|TE re-arming and the TX wake here. Is macb_hresp_error_task() able to break that redundancy?

hresp_error is not even taking bp->lock to writel to the hardware. It races against every other context in MACB. One we get in, the right primitives will be there to fix this, as it is almost a context swap operation (just with old_ctx free before new_ctx alloc).

[...]

Thanks,

Signature · 3 lines
Théo Lebrun, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
author maintainer
#21 lore raw

PATCH v9 17/17 net: macb: use context swapping in .ndo_change_mtu()

In reply to Théo Lebrun

Use newly introduced context buffer management to implement .ndo_change_mtu() as a context swap: allocate new context -> reconfigure HW -> free old context.

This resists memory pressure well by failing without closing the interface and it is much faster by avoiding PHY reinit.

AT91 EMAC is handled differently as their buffer management is separate and they don't do NAPI. They cannot land in macb_change_mtu as they use a different ndo struct. We still defensively protect against it in code.

---
 drivers/net/ethernet/cadence/macb_main.c | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)
drivers/net/ethernet/cadence/macb_main.c +20 -2
index 55c97f2035e7..3160e815a89a 100644
@@ -3530,11 +3530,29 @@ static int macb_close(struct net_device *netdev)
 
 static int macb_change_mtu(struct net_device *netdev, int new_mtu)
 {
-	if (netif_running(netdev))
-		return -EBUSY;
+	struct macb *bp = netdev_priv(netdev);
+	bool running = netif_running(netdev);
+	struct macb_context *new_ctx;
+
+	if (running) {
+		/* Context swapping is not supported for AT91. */
+		if (bp->caps & MACB_CAPS_MACB_IS_EMAC)
+			return -EBUSY;
+
+		new_ctx = macb_context_alloc(bp, new_mtu,
+					     bp->configured_rx_ring_size,
+					     bp->configured_tx_ring_size);
+		if (IS_ERR(new_ctx))
+			return PTR_ERR(new_ctx);
+
+		macb_context_swap_start(bp);
+	}
 
 	WRITE_ONCE(netdev->mtu, new_mtu);
 
+	if (running)
+		macb_context_swap_end(bp, new_ctx);
+
 	return 0;
 }
 
Signature · 1 line
2.55.0
author maintainer
#22 lore raw

Re: [PATCH net-next v9 17/17] net: macb: use context swapping in .ndo_change_mtu()

Replying to net sashiko
https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260812-macb-context-v9-0-7ddbf5f715e0%40bootlin.com

Quoted 39 lines

commit 6e5157b79656fb39cc93533094703e19ecdcef41
Author: Théo Lebrun <[email protected]>

net: macb: use context swapping in .ndo_change_mtu()

This converts macb_change_mtu() from returning -EBUSY on a running interface into a context swap: allocate a new context, quiesce and reconfigure the hardware, then free the old context. AT91 EMAC keeps the -EBUSY behaviour via a MACB_CAPS_MACB_IS_EMAC check.

Quoted 14 lines
drivers/net/ethernet/cadence/macb_main.c +4 -2
index 55c97f2035e7..3160e815a89a 100644
@@ -3530,11 +3530,29 @@ static int macb_close(struct net_device *netdev)
 
 static int macb_change_mtu(struct net_device *netdev, int new_mtu)
 {
-	if (netif_running(netdev))
-		return -EBUSY;
+	struct macb *bp = netdev_priv(netdev);
+	bool running = netif_running(netdev);
+	struct macb_context *new_ctx;
+

drivers/net/ethernet/cadence/macb_main.c static int macb_close(struct net_device *netdev) · line 3546

Quoted 10 lines
drivers/net/ethernet/cadence/macb_main.c +10 -0
+	if (running) {
+		/* Context swapping is not supported for AT91. */
+		if (bp->caps & MACB_CAPS_MACB_IS_EMAC)
+			return -EBUSY;
+
+		new_ctx = macb_context_alloc(bp, new_mtu,
+					     bp->configured_rx_ring_size,
+					     bp->configured_tx_ring_size);
+		if (IS_ERR(new_ctx))
+			return PTR_ERR(new_ctx);

[Severity: Medium]
[Finding: gpt-5-6-sol-4-5]
[Sources: gpt-5-6-sol]

Quoted 5 lines

This isn't a bug introduced by this patch, but can this error path leak the coherent Tx ring? The same structure already exists at the series baseline in macb_alloc_consistent()/macb_free_consistent() and was reachable through macb_open(); this hunk adds a second, easily repeatable trigger from userspace.

Yes there is a bug in macb_alloc() and it'll be fixed.

[...]

Quoted 15 lines

drivers/net/ethernet/cadence/macb_main.c static int macb_close(struct net_device *netdev) · line 3555

Quoted 10 lines
drivers/net/ethernet/cadence/macb_main.c +6 -0
+
+		macb_context_swap_start(bp);
+	}
 
 	WRITE_ONCE(netdev->mtu, new_mtu);
 
+	if (running)
+		macb_context_swap_end(bp, new_ctx);
+

[Severity: High]
[Finding: main-5-6]
[Sources: opus-5]

Quoted 1 line

Is the netdev Tx watchdog also covered by the swap window?

Yes. Talked about in reply to [PATCH net-next v9 16/17]:
https://lore.kernel.org/all/[email protected]/

Copy/pasting here:

Well actually netif_tx_disable() does reset dev_queue->trans_start of all queues. So a race does exist but it is pretty slim (not the full context swap): either before this netif_tx_disable() line or if context swap lasts more than 5s.

I'll protect this by (1) grabbing bp->lock inside macb_tx_restart() and
(2) checking the state of our boolean flag to know if context swap is ongoing.

[...]

Thanks,

Signature · 3 lines
Théo Lebrun, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
author maintainer
#23 lore raw
In reply to Théo Lebrun
Quoted 20 lines · Théo Lebrun

MACB has a pretty primitive approach to buffer management. They are all stored in struct macb *bp. On operations that require buffer realloc (set_ringparam & change_mtu at the moment), the only option is to close the interface, change our global state and re-open the interface.

Two issues:
- It doesn't fly on memory pressured systems; we free our precious buffers and don't manage to reallocate fully, meaning our machine just lost its network access.
- Anecdotally, it is pretty slow because it implies a full PHY reinit.

Instead, we shall:
- allocate a new context (including buffers) first
- if it fails, early return without any impact to the interface
- stop interface
- update global state (bp, netdev, etc)
- pass newly allocated buffer pointers to the hardware
- start interface
- free old context

Quoted 3 lines

This is what we implement here. Both .set_ringparam() and .ndo_change_mtu() are covered by this series. In the future, at least .set_channels() [0], XDP [1] and XSK [2] would benefit.

Sorry for the insistence, but do we have a chance of seeing this merged before the next merge window? Context swapping is a nice feature to get into v7.3 and it would allow us to send XDP (& XSK) series that are dependent on context swapping.

Thanks!

Have a nice week-end,

--
Théo Lebrun, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com

maintainer
#24 lore raw
Quoted 4 lines · On Sat, 15 Aug 2026 11:31:06 +0200 Théo Lebrun wrote:

Sorry for the insistence, but do we have a chance of seeing this merged before the next merge window? Context swapping is a nice feature to get into v7.3 and it would allow us to send XDP (& XSK) series that are dependent on context swapping.

Clashiko reports a number of races with the Tx reset handler. I think they are legit and should be addressed.
https://netdev-ai.bots.linux.dev/sashiko/#/patchset/[email protected]

More trivial issue on patch 14 with your SoB being cut off.

I will *exceptionally* take the first 7 patches. Please try to avoid making the series this large.

#25 lore raw
In reply to Théo Lebrun
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

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

Quoted 5 lines · On Wed, 12 Aug 2026 10:03:14 +0200 you wrote:

MACB has a pretty primitive approach to buffer management. They are all stored in struct macb *bp. On operations that require buffer realloc (set_ringparam & change_mtu at the moment), the only option is to close the interface, change our global state and re-open the interface.

Quoted 7 lines

Two issues:
- It doesn't fly on memory pressured systems; we free our precious buffers and don't manage to reallocate fully, meaning our machine just lost its network access.
- Anecdotally, it is pretty slow because it implies a full PHY reinit.

[...]

Here is the summary with links:
- [net-next,v9,01/17] net: macb: drop "consistent" from alloc/free function names

    https://git.kernel.org/netdev/net-next/c/ba6f76db61fc
  - [net-next,v9,02/17] net: macb: unify device pointer naming convention
    https://git.kernel.org/netdev/net-next/c/07362f68e61d
  - [net-next,v9,03/17] net: macb: unify variable naming convention in at91ether functions
    https://git.kernel.org/netdev/net-next/c/075663a6ceb6
  - [net-next,v9,04/17] net: macb: unify queue index variable naming convention and types
    https://git.kernel.org/netdev/net-next/c/2434dc6e1c9c
  - [net-next,v9,05/17] net: macb: enforce reverse christmas tree (RCT) convention
    https://git.kernel.org/netdev/net-next/c/2cedfce238c3
  - [net-next,v9,06/17] net: macb: allocate tieoff descriptor once across device lifetime
    https://git.kernel.org/netdev/net-next/c/5262eab9462a
  - [net-next,v9,07/17] net: macb: refuse set_ringparam on EMAC
    https://git.kernel.org/netdev/net-next/c/227fdb2fe6f9

- [net-next,v9,08/17] net: macb: introduce macb_context struct for buffer management (no matching commit)
- [net-next,v9,09/17] net: macb: avoid macb_init_rx_buffer_size() modifying state (no matching commit)
- [net-next,v9,10/17] net: macb: make struct macb subset reachable from macb_context struct (no matching commit)
- [net-next,v9,11/17] net: macb: change caps helpers signatures
(no matching commit)
- [net-next,v9,12/17] net: macb: change function signatures to take contexts (no matching commit)
- [net-next,v9,13/17] net: macb: introduce macb_context_alloc() helper
(no matching commit)
- [net-next,v9,14/17] net: macb: move printk() calls out of bp->lock critical section (no matching commit)
- [net-next,v9,15/17] net: macb: read ISR inside bp->lock critical section
(no matching commit)
- [net-next,v9,16/17] net: macb: use context swapping in .set_ringparam()
(no matching commit)
- [net-next,v9,17/17] net: macb: use context swapping in .ndo_change_mtu()
(no matching commit)

You are awesome, thank you!

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