net: macb: implement context swapping
Subsystems (3)
Théo Lebrun
maintainerv11 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.
---
I wonder if we can get those changes in before the next merge window? The idea being that we can restart iterating 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]/
[4]: https://lore.kernel.org/netdev/[email protected]/
[5]: https://lore.kernel.org/netdev/[email protected]/
---
Changes in v11:
- Address race report by Sashiko in a new patch at the start of the
series; this fix does not deserve to target net/main. Note the commit message triggered get_maintainers to Cc PREEMPT_RT people but I
explicitly left them out.
- Note in patch 2/9 commit message about the ethtool -g value changing
on EMAC/RM9200.
- Update Cc: [email protected] -> [email protected].
- Rebase onto latest net-next/main (d7804253678e); one tiny conflict.
- Link to v10: https://patch.msgid.link/[email protected]
Changes in v10:
- Rebase onto latest net-next/main (47a144672573), dropping first 7
patches applied by Jakub. It also means we reuse the quiesce work introduced by 4fcaed2cb1d8 ("net: macb: quiesce IRQs and drain BH on interface close").
- Drop wrong patch "read ISR inside bp->lock critical section".
- Patch "move printk() calls out of bp->lock critical section"
was sent separately as it is standalone.
https://lore.kernel.org/netdev/[email protected]/
- In commit "change caps helpers signatures", in macb_rx_pending(),
convert missed macb_is_gem() call from bp to bp->info. Restore bisectability.
- Writes to bp->ctx are now made with WRITE_ONCE() + wmb().
- macb_tx_restart() now early exits if context swap is ongoing, to avoid races. For that, we grow its existing bp->lock critical section.
- Link to v9: https://patch.msgid.link/[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]>
---
Théo Lebrun (9):
net: macb: close race between NAPI polls & ndo_stop
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: use context swapping in .set_ringparam()
net: macb: use context swapping in .ndo_change_mtu()
drivers/net/ethernet/cadence/macb.h | 115 ++- drivers/net/ethernet/cadence/macb_main.c | 1142 +++++++++++++++++++----------- drivers/net/ethernet/cadence/macb_ptp.c | 8 +- 3 files changed, 824 insertions(+), 441 deletions(-)
---
base-commit: 590b33dce9c34404cd6a23dadbdb316d6b61fdf6
change-id: 20260401-macb-context-bd0caf20414d
Best regards,
Signature · 1 line
Théo Lebrun <[email protected]>
Théo Lebrun
maintainerPATCH v11 1/9 net: macb: close race between NAPI polls & ndo_stop
In ndo_close, we call napi_disable() then free the ring buffers queue->rx_ring|tx_ring. In the NAPI poll functions, after napi_complete_done() we might enable interrupts and might re-check the next ring buffer slot.
This is a use-after-free race on our ring buffers because napi_disable() only waits for napi_complete_done() to return, it does not wait for NAPI poll functions to finish.
The poll function sequence must stay as-is, see commit 0bf476fc3624 ("net: macb: Fix lost RX packet wakeup race in NAPI receive") which explains why the sequence must be napi_complete_done() -> arm IRQs -> re-check ring slot.
Fix by waiting for full end of NAPI poll functions using synchronize_net() in our ndo_close implementation.
This bug has never been reproduced and was reported by Sashiko. The chances of facing it in practice sound tiny: it needs a system without any phylink attached, running PREEMPT_RT and with a kernel preemption on the NAPI poll at the right instant.
We fix it here before introducing context swapping, which is another place where the race could appear (and Sashiko complains on this series).
--- drivers/net/ethernet/cadence/macb_main.c | 6 ++++++ 1 file changed, 6 insertions(+)
drivers/net/ethernet/cadence/macb_main.c +6 -0
@@ -2059,6 +2059,12 @@ static void macb_quiesce_start(struct macb *bp)napi_disable (&queue->napi_tx); } +/* NAPI instances still access Rx/Tx rings after their +* napi_complete_done() calls! Callers to macb_quiesce_start() +* free rings, so wait for all NAPI handlers to be done. +*/ +synchronize_net (); +/* Must be done after napi_tx is disabled: its completion re-arms * the LPI timer. */
Signature · 1 line
2.56.0
Re: [PATCH net-next v11 1/9] net: macb: close race between NAPI polls & ndo_stop
Hi Théo
Quoted 44 lines · Théo Lebrun
In
ndo_close, we callnapi_disable()then free the ring buffers queue->rx_ring|tx_ring. In the NAPI poll functions, afternapi_complete_done()we might enable interrupts and might re-check the next ring buffer slot.This is a use-after-free race on our ring buffers because
napi_disable()only waits fornapi_complete_done()to return, it does not wait for NAPI poll functions to finish.The poll function sequence must stay as-is, see commit
0bf476fc3624("net: macb: Fix lost RX packet wakeup race in NAPI receive") which explains why the sequence must benapi_complete_done()-> arm IRQs -> re-check ring slot.Fix by waiting for full end of NAPI poll functions using
synchronize_net()in ourndo_closeimplementation.This bug has never been reproduced and was reported by Sashiko. The chances of facing it in practice sound tiny: it needs a system without any phylink attached, running
PREEMPT_RTand with a kernel preemption on the NAPI poll at the right instant.We fix it here before introducing context swapping, which is another place where the race could appear (and Sashiko complains on this series).
Fixes 138badbc21a0 ("net: macb: use NAPI for TX completion path")Fixes 1900e30d0ef7 ("net: macb: simplify/cleanup NAPI reschedule checking")Cc [email protected] # too unlikely and never reported by usersReported-by Sashiko <[email protected]>Signed-off-by Théo Lebrun <[email protected]>--- drivers/net/ethernet/cadence/macb_main.c | 6 ++++++ 1 file changed, 6 insertions(+)drivers/net/ethernet/cadence/macb_main.c +0
-0@@ -2059,6 +2059,12 @@ static void macb_quiesce_start(struct macb *bp)napi_disable (&queue->napi_tx); }
drivers/net/ethernet/cadence/macb_main.c static void macb_quiesce_start(struct macb *bp) · line 2070
Quoted 10 lines
drivers/net/ethernet/cadence/macb_main.c +6
-0+/* NAPI instances still access Rx/Tx rings after their +* napi_complete_done() calls! Callers to macb_quiesce_start() +* free rings, so wait for all NAPI handlers to be done. +*/ +synchronize_net (); +/* Must be done after napi_tx is disabled: its completion re-arms * the LPI timer. */
Thanks,
Nicolai
Théo Lebrun
maintainerPATCH v11 2/9 net: macb: introduce macb_context struct for buffer management
In reply to Théo LebrunWhenever 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.
Note: bp->configured_{rx,tx}_ring_size contain proper values for EMAC, meaning 9/1 on ethtool -G on RM9200 whereas it reported 0/0 before.
--- drivers/net/ethernet/cadence/macb.h | 51 +++- drivers/net/ethernet/cadence/macb_main.c | 506 +++++++++++++++++++------------ 2 files changed, 344 insertions(+), 213 deletions(-)
drivers/net/ethernet/cadence/macb.h +35 -16
@@ -1267,23 +1267,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; };
@@ -1298,6 +1287,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;
@@ -1306,12 +1321,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 +309 -197
1,211 lines not on this page · raw message
Signature · 1 line
2.56.0
Théo Lebrun
maintainerPATCH v11 3/9 net: macb: avoid macb_init_rx_buffer_size() modifying state
In reply to Théo Lebrunmacb_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
@@ -2743,25 +2743,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)
@@ -3346,7 +3347,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);int err;
@@ -3364,7 +3364,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.56.0
Théo Lebrun
maintainerPATCH v11 4/9 net: macb: make `struct macb` subset reachable from macb_context struct
In reply to Théo LebrunFor 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
@@ -1287,6 +1287,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 */
@@ -1306,6 +1317,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;
@@ -1321,6 +1334,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.
@@ -1332,17 +1354,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 {
@@ -1350,15 +1369,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;
@@ -1406,9 +1422,6 @@ struct macb {*/ bool irq_quiesced; -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
@@ -3363,6 +3363,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;
@@ -5305,6 +5307,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 );
@@ -6027,6 +6030,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;
@@ -6072,6 +6076,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.56.0
Théo Lebrun
maintainerPATCH v11 5/9 net: macb: change caps helpers signatures
In reply to Théo LebrunFor 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 | 159 ++++++++++++++++--------------- drivers/net/ethernet/cadence/macb_ptp.c | 8 +- 3 files changed, 98 insertions(+), 90 deletions(-)
drivers/net/ethernet/cadence/macb.h +11 -10
@@ -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); \
@@ -1473,14 +1473,15 @@ static inline void gem_ptp_do_txstamp(struct macb *bp, struct sk_buff *skb, strustatic 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 */
@@ -1496,16 +1497,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 +83 -76
562 lines not on this page · raw message
drivers/net/ethernet/cadence/macb_ptp.c +4 -4
@@ -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 ));
@@ -380,7 +380,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 ;
@@ -399,7 +399,7 @@ int gem_set_hwtst(struct net_device *netdev,u32 ncr_bits =0 ;u32 regval; -if (!macb_dma_ptp (bp)) +if (!macb_dma_ptp (&bp->info))return -EOPNOTSUPP ;switch (tstamp_config->tx_type) {
Signature · 1 line
2.56.0
Théo Lebrun
maintainerPATCH v11 6/9 net: macb: change function signatures to take contexts
In reply to Théo LebrunFor 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 | 391 ++++++++++++++++--------------- 2 files changed, 210 insertions(+), 188 deletions(-)
drivers/net/ethernet/cadence/macb.h +4 -3
@@ -1193,11 +1193,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); -int (*mog_init_rings)(struct macb *bp); +int (*mog_alloc_rx_buffers)(struct macb_context *ctx); +void (*mog_free_rx_buffers)(struct macb_context *ctx); +int (*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 +206 -185
991 lines not on this page · raw message
Signature · 1 line
2.56.0
Théo Lebrun
maintainerPATCH v11 7/9 net: macb: introduce macb_context_alloc() helper
In reply to Théo LebrunMove 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 | 68 +++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 22 deletions(-)
drivers/net/ethernet/cadence/macb_main.c +46 -22
@@ -3006,6 +3006,43 @@ 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); + goto free_ctx; + } + + err = bp->macbgem_ops.mog_init_rings (ctx); +if (err) + goto free_rings; + +return ctx; + +free_rings: +macb_free (ctx); +free_ctx: +kfree (ctx); +return ERR_PTR (err); +} +static int gem_init_rx_ring (struct macb_context *ctx,unsigned int q) {struct macb_rxq *rxq = &ctx->rxq[q];
@@ -3376,6 +3413,7 @@ static void macb_set_rx_mode(struct net_device *netdev)static int macb_open (struct net_device *netdev) {struct macb *bp =netdev_priv (netdev); +struct macb_context *ctx;int err;netdev_dbg (bp->netdev,"open\n" );
@@ -3384,30 +3422,18 @@ static int macb_open(struct net_device *netdev)if (err <0 )return err; -WRITE_ONCE (bp->ctx,kzalloc_obj (*bp->ctx)); -wmb ();/* ensure IRQ enabled at quiesce end read the new context */ -if (!bp->ctx) { - err = -ENOMEM ; + ctx =macb_context_alloc (bp, netdev->mtu, + bp->configured_rx_ring_size, + bp->configured_tx_ring_size); +if (IS_ERR (ctx)) { + err =PTR_ERR (ctx); + bp->ctx =NULL ; goto pm_exit; } - bp->ctx->info = &bp->info; +WRITE_ONCE (bp->ctx, ctx); +wmb ();/* ensure IRQ enabled at quiesce end read the new context */ -/* 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; - } - - err = bp->macbgem_ops.mog_init_rings (bp->ctx); -if (err) - goto free_rings;macb_init_buffers (bp);macb_quiesce_end (bp);
@@ -3441,9 +3467,7 @@ static int macb_open(struct net_device *netdev)macb_quiesce_start (bp);macb_reset_hw (bp); -free_rings:macb_free (bp->ctx); -free_ctx:kfree (bp->ctx); bp->ctx =NULL ; pm_exit:
Signature · 1 line
2.56.0
Théo Lebrun
maintainerPATCH v11 8/9 net: macb: use context swapping in .set_ringparam()
In reply to Théo Lebrunethtool_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 | 7 +- drivers/net/ethernet/cadence/macb_main.c | 188 +++++++++++++++++++++++++++---- 2 files changed, 175 insertions(+), 20 deletions(-)
drivers/net/ethernet/cadence/macb.h +6 -1
@@ -1358,6 +1358,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;
@@ -1419,10 +1421,13 @@ struct macb {u32 tx_lpi_timer;/* ISR must not drive NAPI & BH mechanisms. True when the interface -* is closed. Protected by bp->lock. +* is closed or during context-swap. Protected by bp->lock. */ bool irq_quiesced; +/* 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 +169 -19
307 lines not on this page · raw message
Signature · 1 line
2.56.0
Théo Lebrun
maintainerPATCH v11 9/9 net: macb: use context swapping in .ndo_change_mtu()
In reply to Théo LebrunUse 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
@@ -3658,11 +3658,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.56.0
[email protected]
AI reviewThis is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
Hi!
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
- What hardware the change was tested on. For driver fixes please mention the device (and if relevant firmware version) used for testing, or say that the change was not tested on real hardware.
Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account. If the series needs another revision for other reasons, please include the information in the commit messages then.
The evaluation is done by an LLM so it may be wrong, if you think that is the case please reply and explain.
Théo Lebrun
maintainerHello netdev-bot+sinfo,
Quoted 6 lines · CEST,
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
- What hardware the change was tested on. For driver fixes please mention the device (and if relevant firmware version) used for testing, or say that the change was not tested on real hardware.
This series has been tested on EyeQ5 which has two GEM instances.
Thanks,
--
Théo Lebrun, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
Hi Théo
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_mtuat 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 5 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.
[...]
Tested on a Raspberry Pi CM5 (RP1 GEM) with lockdep and KASAN enabled:
- ring and MTU changes under bidirectional TCP and a UDP flood
- ring and MTU changes while the interface is down
- ring and MTU changes during autoneg
- injected allocation failures in both paths
- PTP and EEE after a swap
Everything worked, and changing either no longer takes the link down.
For the series:
Thanks,
Nicolai
Théo Lebrun
maintainerHello Nicolai,
Quoted 9 lines · Nicolai Buchwitz
On 13.10.2026 15:16, Théo Lebrun wrote:
Tested on a Raspberry Pi CM5 (RP1 GEM) with lockdep and KASAN enabled:- ring and MTU changes under bidirectional TCP and a UDP flood
- ring and MTU changes while the interface is down
- ring and MTU changes during autoneg
- injected allocation failures in both paths
- PTP and EEE after a swap
Quoted 5 lines
Everything worked, and changing either no longer takes the link down.
For the series:
Thanks! I hadn't tested context swapping + PTP or EEE. Did some PTP tests just now and it is all good here as well.
Thanks,
--
Théo Lebrun, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
Théo Lebrun
maintainerHello Théo,
Sashiko has 4 high comments, Clashiko has 3 low comments.
TL;DR: nothing worth a respin because it is all out of scope.
https://sashiko.dev/#/patchset/20261013-macb-context-v11-0-b27785167566%40bootlin.com
https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261013-macb-context-v11-0-b27785167566%40bootlin.com
Quoted 25 lines
commit
0eff5390c4748a085f669ce98d1bc433c0f36ace
Author: Théo Lebrun <[email protected]>
net: macb: introducemacb_contextstruct for buffer managementThis commit introduces a
macb_contextstructure to own all queue buffers and parameters, paving the way for supporting multiple contexts in parallel during buffer reallocation. It updates ring parameters and moves related fields out of the main macb struct into the context struct.Quoted 3 lines
drivers/net/ethernet/cadence/macb_main.c +0
-0[ ... ]
Quoted 1 line
drivers/net/ethernet/cadence/macb_main.c +0
-0@@ -2741,60 +2789,71 @@ static void gem_free_rx_buffers(struct macb *bp)[ ... ]
Quoted 6 lines
static unsigned int
macb_tx_ring_size_per_queue(struct macb *bp){
- returnmacb_dma_desc_get_size(bp)*bp->tx_ring_size+bp->tx_bd_rd_prefetch;
+ returnmacb_dma_desc_get_size(bp)*bp->ctx->tx_ring_size+
+bp->tx_bd_rd_prefetch;
}[Severity: High]
Can this dereference a NULL pointer if the context was freed?
Quoted 6 lines
If
macb_open()fails, it now freesbp->ctxand leaves it NULL. Later, if the interface is closed andmacb_free()calls this function:
macb_close()->macb_free()->macb_tx_ring_size_per_queue()This will dereference
bp->ctx.
Sashiko says there is an issue with a macb_open() failing followed by a macb_close(). Indeed, but the interface won't be closed if its open operation failed.
Quoted 20 lines
[ ... ]
Quoted 12 lines
drivers/net/ethernet/cadence/macb_main.c +2
-2@@ -3856,8 +3946,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;if (reset)macb_open (bp->netdev);Quoted 2 lines
return 0;
}[Severity: High]
Should we be checking the return value ofmacb_open()here?
Quoted 7 lines
If
macb_open()fails, it freesbp->ctx. However, this function unconditionally returns 0, masking the failure and leaving the device in an UP state in the network stack.A subsequent interface down event will trigger
macb_close(), which callsmacb_free(), resulting in the NULL pointer dereference inmacb_tx_ring_size_per_queue()noted above.
We ignore that on purpose. Check out the last two commits. The whole point of this series is to introduce context swapping, to fix the set_ringparam operation (and others).
---
Quoted 20 lines
commit
b435a89b7b7a33cd5d0880d8a83b893da802f106
Author: Théo Lebrun <[email protected]>net: macb: change function signatures to take contexts
This commit changes many function signatures in the MACB driver to take a
macb_contextpointer instead of macb ormacb_queue. This refactoring prepares the driver for supporting parallel MACB contexts while reducing the bug surface area by preventing the use of the wrong context.Quoted 3 lines
drivers/net/ethernet/cadence/macb_main.c +0
-0[ ... ]
drivers/net/ethernet/cadence/macb_main.c
Quoted 5 lines
for (tail = txq->tail; tail != txq->head; tail++) { u32 ctrl;drivers/net/ethernet/cadence/macb_main.c +1
-1- desc =macb_tx_desc (queue, tail); + desc =macb_tx_desc (ctx, q, tail);
Quoted 6 lines
[Severity: High]
Does this traversal and unmapping of the TX ring lack necessary synchronization?When
macb_tx_error_taskis triggered in a workqueue due to a TX error, it callsnetif_tx_stop_all_queues(). This setsQUEUE_STATE_DRV_XOFFbut does not wait for a concurrentmacb_start_xmiton another CPU.
This is out of scope. We ignore suspend, the HRESP error task and the TX error task on purpose.
[...]
Quoted 10 lines
Quoted 3 lines
ctrl = desc->ctrl; tx_skb = macb_tx_skb(queue, tail); skb = tx_skb->skb;[ ... ]
Quoted 5 lines
netif_tx_stop_all_queues(netdev); netif_carrier_off(netdev);-
bp->macbgem_ops.mog_init_rings(bp);
+bp->macbgem_ops.mog_init_rings(ctx);
Quoted 7 lines
[Severity: High]
Could this ring reinitialization lead to a use-after-free or data corruption?When a hardware HRESP error triggers
macb_hresp_error_taskviasystem_bh_wq,mog_init_ringsis called to reset ring pointers and reallocate SKB arrays. However, it does not appear to synchronize with software datapath operations usingnapi_disable()andspin_lock(&queue->tx_ptr_lock).
This is out of scope. We ignore suspend, the HRESP error task and the TX error task on purpose.
[...]
---
Quoted 31 lines
commit
c7aa30e3b67cf259fbc73811fb17ffc27d9be6eb
Author: Théo Lebrun <[email protected]>net: macb: use context swapping in .set_ringparam()
This patch introduces a context swapping mechanism for macb's
set_ringparamethtool operation. It avoids freeing buffers during interface close and reallocating them on open, instead pre-allocating a new context to prevent failures under memory pressure. The swap logic is synchronized against MAC callbacks and NAPI execution using a newmac_cfg_lockmutex and the quiesce state.Quoted 4 lines
drivers/net/ethernet/cadence/macb_main.c +0
-0[ ... ]
Quoted 14 lines
drivers/net/ethernet/cadence/macb_main.c +2
-2@@ -2010,29 +2038,37 @@ static int macb_rx_poll(struct napi_struct *napi, int budget)static void macb_tx_restart (struct macb_queue *queue) { -struct macb_context *ctx = queue->bp->ctx; -struct macb_txq *txq =macb_txq (queue);struct macb *bp = queue->bp;unsigned int head_idx, tbqp; +struct macb_context *ctx; +struct macb_txq *txq;unsigned long flags;spin_lock_irqsave (&queue->tx_ptr_lock, flags);
Quoted 16 lines
drivers/net/ethernet/cadence/macb_main.c static int macb_rx_poll(struct napi_struct *napi, int budget) · line 2059
Quoted 12 lines
drivers/net/ethernet/cadence/macb_main.c +10
-1+spin_lock (&bp->lock); + +/* context swap ongoing? */ +if (bp->irq_quiesced) + goto out_unlock; + + ctx = queue->bp->ctx; + txq =macb_txq (queue); +if (txq->head == txq->tail) - goto out_tx_ptr_unlock; + goto out_unlock;[Severity: High]
Doesmacb_tx_error_task()indrivers/net/ethernet/cadence/macb_main.calso need to acquirequeue->tx_ptr_lockwhen updatingtxq->headandtxq->tail?
This is out of scope. We ignore suspend, the HRESP error task and the TX error task on purpose.
[...]
---
Quoted 28 lines
commit
59119f040d96bc1d81d6e0708a9c738cad768fcd
Author: Théo Lebrun <[email protected]>net: macb: use context swapping in .set_ringparam()
The old close/reopen sequence in
macb_set_ringparam()is replaced. A new context with the requested ring sizes is now allocated before the interface is stopped, and it is swapped in with the new helpersmacb_context_swap_start()andmacb_context_swap_end(). A newmac_cfg_lockmutex serializes the swap against the phylink MAC callbacks, and a newbp->link_upboolean tracks link state under that lock.Quoted 4 lines
drivers/net/ethernet/cadence/macb.h +0
-0[ ... ]
Quoted 9 lines
drivers/net/ethernet/cadence/macb.h +1
-1@@ -1419,10 +1421,13 @@ struct macb {u32 tx_lpi_timer;/* ISR must not drive NAPI & BH mechanisms. True when the interface -* is closed. Protected by bp->lock. +* is closed or during context-swap. Protected by bp->lock. */ bool irq_quiesced;
Quoted 9 lines
drivers/net/ethernet/cadence/macb.h struct macb { · line 1429
Quoted 2 lines
drivers/net/ethernet/cadence/macb.h +2
-0+/* Redundant to netif_carrier_ok(), but set under bp->mac_cfg_lock. */ +bool link_up;[Severity: Low]
[Finding: main-2-3]
[Sources: opus-5-5]
Is the "redundant tonetif_carrier_ok()" claim accurate?macb_hresp_error_task()changes the carrier and the hardware enable bits without takingmac_cfg_lock, and it never reads or updatesbp->link_up:
Technically the "Redundant to netif_carrier_ok()" phrase is wrong yes but I hope it is evident that if it was actually redundant, we wouldn't have introduced a separate flag.
Fixing the many known races inbetween the HRESP task and the rest of the driver execution is out of scope of this series.
[...]
Quoted 16 lines
Quoted 16 lines
drivers/net/ethernet/cadence/macb.h +7
-3@@ -3995,16 +4140,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); +if (IS_ERR (new_ctx)) +return PTR_ERR (new_ctx); + +macb_context_swap_start (bp); }
Quoted 3 lines
drivers/net/ethernet/cadence/macb.h static int macb_set_ringparam(struct net_device *netdev, · line 4153
Quoted 2 lines
drivers/net/ethernet/cadence/macb.h +0
-0bp->configured_rx_ring_size = new_rx_size; bp->configured_tx_ring_size = new_tx_size;
[... pre existing finding ...]
Quoted 10 lines
drivers/net/ethernet/cadence/macb.h static int macb_set_ringparam(struct net_device *netdev, · line 4158
Quoted 9 lines
drivers/net/ethernet/cadence/macb.h +2
-2-if (reset) -macb_open (bp->netdev); +if (running) +macb_context_swap_end (bp, new_ctx);return 0 ; }
Quoted 8 lines
[Severity: Low]
[Finding: gpt-6-astra-1-2]
[Sources: gpt-6-astra, opus-5-5]
The code change looks correct, but the commit message describes the old behaviour only as:Under memory pressure this does not fly: we free our buffers at close and cannot reallocate new ones at open.
I've cut the rambling. The point here is that the commit message is somewhat underselling what we gain from context swapping. I couldn't agree more, context swapping is great!
Clashiko wants to point out all the advantages and add a Fixes trailer. I consider all that useless and see this patch as a new feature rather than a bugfix.
[...]
---
Quoted 36 lines
commit
9410fd54d1f3a19a62c932c210f14be133012c06
Author: Théo Lebrun <[email protected]>net: macb: use context swapping in .ndo_change_mtu()
This patch makes
macb_change_mtu()handle a running interface by swapping
contexts: it allocates a new context sized for the new MTU, quiesces and reconfigures the hardware, then frees the old context. Before this, the function returned-EBUSY. AT91 EMAC devices are rejected defensively, since they use a separate ndo struct and should never reach this path.Quoted 8 lines
drivers/net/ethernet/cadence/macb_main.c +0
-0@@ -3658,11 +3658,29 @@ static int macb_close(struct net_device *netdev)static int macb_change_mtu (struct net_device *netdev,int new_mtu) {drivers/net/ethernet/cadence/macb_main.c static int macb_close(struct net_device *netdev) · line 3668
Quoted 10 lines
drivers/net/ethernet/cadence/macb_main.c +8
-2-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 ;[Severity: Low]
[Finding: main-2-0]
[Sources: opus-5-5]
This isn't a bug, but should this comment say "AT91 EMAC" instead of "AT91"?
Quoted 4 lines
MACB_CAPS_MACB_IS_EMACis set only byemac_config. Other AT91-family configs, such asat91sam9260_config,sama5d2_configandsama5d3_config, don't set it. Those parts usemacb_netdev_ops, so they do get context swapping on a live MTU change. The commit message already says "AT91 EMAC".
On this one, I agree: AT91 is confusing, the proper way to describe the old IP we support would be 'EMAC' or 'RM9200' (only board we support that uses that IP revision).
I won't respin just for this comment tweak. If we wanted to fully remove the confusion, we would also rename all functions from at91ether_* to something else, like macb_emac_* or something of the sort. This feels out of scope.
Thanks,
--
Théo Lebrun, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
This series was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:
Hello:
This series was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:
Quoted 5 lines · On Tue, 13 Oct 2026 15:16:38 +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_mtuat 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,v11,1/9] net: macb: close race between NAPI polls & ndo_stop
https://git.kernel.org/netdev/net-next/c/ba16ad3c8769 - [net-next,v11,2/9] net: macb: introduce macb_context struct for buffer management https://git.kernel.org/netdev/net-next/c/435345c65180 - [net-next,v11,3/9] net: macb: avoid macb_init_rx_buffer_size() modifying state https://git.kernel.org/netdev/net-next/c/b8ab596dabde - [net-next,v11,4/9] net: macb: make `struct macb` subset reachable from macb_context struct https://git.kernel.org/netdev/net-next/c/9bfec20d011f - [net-next,v11,5/9] net: macb: change caps helpers signatures https://git.kernel.org/netdev/net-next/c/dd1aaefee4f6 - [net-next,v11,6/9] net: macb: change function signatures to take contexts https://git.kernel.org/netdev/net-next/c/ed16494c2c50 - [net-next,v11,7/9] net: macb: introduce macb_context_alloc() helper https://git.kernel.org/netdev/net-next/c/88e99640fcd0 - [net-next,v11,8/9] net: macb: use context swapping in .set_ringparam() https://git.kernel.org/netdev/net-next/c/dcc3288b9433 - [net-next,v11,9/9] net: macb: use context swapping in .ndo_change_mtu() https://git.kernel.org/netdev/net-next/c/a98375924d64
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html