Skip to contentSkip to conversation

net: macb: implement ethtool set channels count operation

PATCH Superseded Théo Lebrun 3 participants 12 messages Nov 13 – Nov 18

Subsystems (3)

v6 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Théo Lebrun

author maintainer
#0 lore raw

v6 cover net: macb: implement ethtool set channels count operation

Add support for changing the active number of queues. Tested on Mobileye EyeQ5.

Now that context swapping landed, update this series and use the feature on ethtool_ops.set_channels() requests.

- We move num_queues from bp to ctx.

- All readers of the bp->num_queues value now need to pick between:
bp->max_num_queues / bp->configured_num_queues / bp->ctx->num_queues.

- The context swapping in set_channels can fail because netif_set_real_num_queues can fail. Update macb_context_swap_end() to allow taking NULL as new context argument. If that is received, it restarts the interface with the same context as before.

This highlights a few callbacks that are pretty broken if accessed while interface is down, because they access hardware registers while runtime suspended. Most importantly stats and taprio. We don't attempt to fix them in this series! I am mentioning this here to prompt Sashiko. :)

Have a nice day,
Thanks,
Théo

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

---
Changes in v6:
- Rebase onto latest net-next/main (573af8015529). This means rebasing
onto context swapping, which is now used on set_channels requests.
- Move the QUEUE_DISABLE caps patch out into a separate series [0].
- Drop kunit tests. Thanks Jakub.
- Fix the TAPRIO gate test if qdisc is destroyed while the interface is
down (because bp->taprio_gated_queues was stall in that case).
Thanks Nicolai.
- Link to v5: https://patch.msgid.link/[email protected]

Changes in v5:
- New patch: add missing MACB_CAPS_QUEUE_DISABLE to 3 compatibles based
on publicly available datasheets. That means they can benefit from
the set_channels implementation.
- set_channels patch:
- Refuse set_channels if it disables a queue with active TAPRIO.
- Use TBQP.QUEUE_DISABLE bit to also disable transmit; previously we
only disabled the receive engine.
- Migrate code to newly introduced macb_queue_isr_clear() helper.
- Adapt to newly introduced macb_quiesce_start(), where we want to synchronize_irq() on all queues, even the disabled ones.
- IRQ handler now self-disarms on disabled queues.
- Drop old_count unused stack variable.
- Only allow TAPRIO rules on active queues.
- Fix comments style and text in macb_set_channels(); in particular
mention we'll use context-swapping once it is available.
- Distribute SRAM segments patch:
- Introduce GEM_TXQSEGALLOC_MASK(q) and use field_prep|get() macros.
- Introduce capability for this feature, as many publicly available
GEM datasheets don't document the registers.
- Remove upper register support. Lower register is enough to handle 8
queues, which is the max we support.
- Add edge case kunit for num_segments=128.
- Introduce static_assert(MACB_MAX_QUEUES == 8) in gem_sram_distribute_segments() to signal that code will need
updating if/when the constant changes.
- Update To/Cc list: drop Nicolas and Claudiu, add Conor and Nicolai.
- Rebase onto latest net-next/main (6b3e73774815).
- Link to v4: https://patch.msgid.link/[email protected]

Changes in v4:
- macb_set_channels(): drop count == old_count check, it is redundant
with both netlink and ioctl codepaths. See early returns in ethnl_set_channels() and ethtool_set_channels(). Discussion @ V2.
- Rebase to latest net-next (8737d7194d6d); nothing to report.
- Link to v3: https://patch.msgid.link/[email protected]

Changes in v3:
- Makefile: fix compilation of kunit tests. This bug was hidden before
because everything was builtin so it ended up linked together anyway.
Now macb.o is the amalgation of macb_{main,ptp,utils}.o and
macb_test.o is the amalgation of macb_{kunit,utils}.o.
- Kconfig CONFIG_MACB_KUNIT_TEST entry:
- Turn into tristate; no good reason to restrict to bool.
- Drop "depends on MACB". We do not need to compile the full MACB
driver to test gem_sram_distribute_segments().
- Add usual MODULE_*() macros to macb_kunit.c.
- Drop unused init value of ret in macb_set_channels().
- Rebase to latest net-next; nothing to report.
- Link to v2: https://patch.msgid.link/[email protected]

Changes in v2:
- Move gem_sram_distribute_segments() from macb_main.c to macb_utils.c
and kunit tests from macb_main.c to macb_kunit.c.
- macb_set_channels():
- Add comment about MACB_CAPS_QUEUE_DISABLE.
- Refuse operation if netif_running(), with code comment.
- Drop useless sanity checks on ch->{combined,rx,tx}_count.
- Add help text to CONFIG_MACB_KUNIT_TEST.
- Fix checkpatch --max-line-length=80 warnings.
- Rebase onto latest net-next; nothing to report.
- Link to v1: https://lore.kernel.org/r/[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: Russell King <[email protected]>

Cc Nicolai Buchwitz <[email protected]>
Cc Paolo Valerio <[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]>

---
Théo Lebrun (2):
net: macb: implement ethtool_ops.get|set_channels()
net: macb: distribute evenly Tx SRAM segments

 drivers/net/ethernet/cadence/macb.h      |  21 +-
 drivers/net/ethernet/cadence/macb_main.c | 355 ++++++++++++++++++++++++-------
 2 files changed, 294 insertions(+), 82 deletions(-)

---
base-commit: 573af801552999954caa8347e4490b04cf4763f0
change-id: 20260305-macb-set-channels-5bf6e07f3270

Best regards,

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

PATCH v6 1/2 net: macb: implement ethtool_ops.get|set_channels()

bp->num_queues is the total number of queues and is constant from probe. MACB/GEM does not know about Rx/Tx specific queues; it only has combined queues. Move bp->num_queues to ctx->num_queues as it now belongs to the currently active context (bp->ctx).

Also introduce bp->max_num_queues which takes the current role of bp->num_queues and allow 0 < ctx->num_queues <= bp->max_num_queues.

Lastly introduce bp->configured_num_queues to remember queue count across interface down+up cycles.

With all that, implement the .set_channels() operation. It ends up only being an assignment to bp->configured_num_queues plus a call into netif_set_real_num_queues(). However if the interface is running, we use a context swap window to safely realloc on the fly.

Note we refuse the operation if any TAPRIO rules are in place on queues that would get disabled. If we allow the operation to take place, then a transmission window would be reserved but unusable.

Nit: fix an alignment issue inside gem_ethtool_ops which does not deserve its own patch.

---
 drivers/net/ethernet/cadence/macb.h      |   7 +-
 drivers/net/ethernet/cadence/macb_main.c | 279 ++++++++++++++++++++++---------
 2 files changed, 206 insertions(+), 80 deletions(-)
drivers/net/ethernet/cadence/macb.h +6 -1
index b7b4b6a38298..0879fb2b198b 100644
@@ -1292,7 +1292,6 @@ 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;
@@ -1323,6 +1322,7 @@ struct macb_context {
 	unsigned int		rx_buffer_size;
 	unsigned int		rx_ring_size;
 	unsigned int		tx_ring_size;
+	unsigned int		num_queues;
 	struct macb_rxq		rxq[MACB_MAX_QUEUES];
 	struct macb_txq		txq[MACB_MAX_QUEUES];
 };
@@ -1352,6 +1352,11 @@ struct macb {
 	unsigned int		configured_tx_ring_size;
 	struct macb_context	*ctx;
 
+	unsigned int		configured_num_queues;
+	unsigned int		max_num_queues;
+	/* Queue mask gated by the current TAPRIO schedule (RTNL-protected). */
+	u32			taprio_gated_queues;
+
 	struct macb_dma_desc	*rx_ring_tieoff;
 	dma_addr_t		rx_ring_tieoff_dma;
 
drivers/net/ethernet/cadence/macb_main.c +200 -79

636 lines not on this page · raw message

Signature · 1 line
2.56.0
#2 lore raw

Re: [PATCH net-next v6 1/2] net: macb: implement ethtool_ops.get|set_channels()

Hi Théo

Quoted 12 lines · Théo Lebrun

[...]

drivers/net/ethernet/cadence/macb_main.c +4 -1
@@ -3351,16 +3374,21 @@ static void macb_context_swap_end(struct macb 
 *bp,
 
 	lockdep_assert_held(&bp->mac_cfg_lock);
 
-	/* Swap contexts & give buffer pointers to HW. */
+	/* Swap contexts & give buffer pointers to HW.
+	 * !new_ctx means that the swap failed and we keep the old context.
+	 */
+

drivers/net/ethernet/cadence/macb_main.c static void macb_context_swap_end(struct macb · line 3388

Quoted 10 lines
drivers/net/ethernet/cadence/macb_main.c +5 -3
+	if (new_ctx) {
+		old_ctx = bp->ctx;
+		WRITE_ONCE(bp->ctx, new_ctx);
+		wmb(); /* ensure IRQ enabled below read the new context */
+	}
 
-	old_ctx = bp->ctx;
-	WRITE_ONCE(bp->ctx, new_ctx);
-	wmb(); /* ensure IRQ enabled below read the new context */
 	macb_init_buffers(bp);

The NULL case reuses the old rings without mog_init_rings(). The HW restarts at descriptor 0 but head/tail don't, so Tx stalls after a failed ethtool -L? macb_hresp_error_task() reinits the rings first.

Quoted 1 line

[...]

Regards,
Nicolai

author maintainer
#3 lore raw

Re: [PATCH net-next v6 1/2] net: macb: implement ethtool_ops.get|set_channels()

Hello Nicolai,

Quoted 17 lines · Nicolai Buchwitz
Quoted 16 lines · Théo Lebrun
drivers/net/ethernet/cadence/macb_main.c +9 -1
@@ -3351,16 +3374,21 @@ static void macb_context_swap_end(struct macb 
 *bp,
 
 	lockdep_assert_held(&bp->mac_cfg_lock);
 
-	/* Swap contexts & give buffer pointers to HW. */
+	/* Swap contexts & give buffer pointers to HW.
+	 * !new_ctx means that the swap failed and we keep the old context.
+	 */
+
+	if (new_ctx) {
+		old_ctx = bp->ctx;
+		WRITE_ONCE(bp->ctx, new_ctx);
+		wmb(); /* ensure IRQ enabled below read the new context */
+	}
 
Quoted 8 lines

drivers/net/ethernet/cadence/macb_main.c static void macb_context_swap_end(struct macb *bp, · line 3387

Quoted 4 lines
drivers/net/ethernet/cadence/macb_main.c +0 -3
-	old_ctx = bp->ctx;
-	WRITE_ONCE(bp->ctx, new_ctx);
-	wmb(); /* ensure IRQ enabled below read the new context */
 	macb_init_buffers(bp);

The NULL case reuses the old rings without mog_init_rings(). The HW restarts at descriptor 0 but head/tail don't, so Tx stalls after a failed ethtool -L? macb_hresp_error_task() reinits the rings first.

Hum, so you are totally right on that. Both Tx (txq->head + txq->tail) and Rx (rxq->tail + rxq->prepared_head) cursors are wrong by being out of sync with the hardware cursors.

But the approach taken by macb_hresp_error_task() is wrong: it resets the Tx cursors and Tx descriptors in the existing context but by doing so leak all Tx buffers. Rx buffers aren't leaking because gem_init_rx_ring() sets RX_USED on all slots then refills.

Sashiko [0] points out the same issue; I'll wait for the end of the Clashiko [1] embargo period before re-sending, to see what it has to say.

---

There can be two solutions to this:

- We don't touch the HW cursors RBQPH/TBQPH/RBQP/TBQP by not calling macb_init_buffers() at swap_end. So here the swap operation will have been as close as possible to a no-op where we just stopped the HW for a small window.

- We do reset the HW cursors. But that means we need to make sure both rings are in a good shape (which they are not with v6).

It isn't a massive amount of work, but it still is more code (that might be buggy). We need to free pending Tx slots or bring them to the front of the ring. For Rx we should consume RX_USED slots.

I prefer the first approach. It is simpler and alloc-free (ENOMEM is probably the reason why the set_channels context swap failed when calling netif_set_real_num_queues).

[0]: https://sashiko.dev/#/patchset/20261113-macb-set-channels-v6-0-5de59d9e7f02%40bootlin.com?part=1
[1]: https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261113-macb-set-channels-v6-0-5de59d9e7f02%40bootlin.com

Thanks,

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

#4 lore raw

Re: [PATCH net-next v6 1/2] net: macb: implement ethtool_ops.get|set_channels()

In reply to Théo Lebrun
Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider.

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

Critical: 0 · High: 0 · Medium: 7 · Low: 0

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

- [Medium] macb_taprio_destroy() (drivers/net/ethernet/cadence/macb_main.c) now clears the software…
- [Medium] gem_get_rx_ring_count() now returns bp->max_num_queues.
- [Medium] When the interface is running, macb_set_channels() calls macb_context_swap_start(bp) before netif_set_real_num_queues().
- [Medium] macb_interrupt() now runs if (bp->ctx) num_queues = READ_ONCE(bp->ctx->num_queues); (lines 2352-2353) under bp->lock,…
- [Medium] macb_poll_controller() now evaluates q < bp->ctx->num_queues on every loop iteration with only local_irq_save(), without bp->lock,…
- [Medium] net: macb: at91ether_open() leaves bp->ctx->num_queues uninitialized as 0
- [Medium] net: macb: macb_taprio_setup_replace() skips ENST init on disabled queues

--

net: macb: implement ethtool_ops.get|set_channels()

This commit moves the queue count into the active context (ctx->num_queues) and adds bp->max_num_queues and bp->configured_num_queues. It implements .get_channels() and .set_channels(), using the context swap window when the interface is running. It also records the TAPRIO-gated queue mask so that .set_channels() can refuse to disable gated queues.

Quoted 4 lines
drivers/net/ethernet/cadence/macb_main.c +0 -0
index 5bd299fc85a9c..e7325af4b399c 100644

[ ... ]

Quoted 6 lines
drivers/net/ethernet/cadence/macb_main.c +2 -0
@@ -2332,19 +2349,22 @@ static irqreturn_t macb_interrupt(int irq, void *dev_id)
 
 	spin_lock(&bp->lock);
 
+	if (bp->ctx)
+		num_queues = READ_ONCE(bp->ctx->num_queues);

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

Quoted 10 lines
drivers/net/ethernet/cadence/macb_main.c +5 -3
+
 	while (status) {
-		/* self-disarm while the netdev is closed
-		 * or during context swap
+		/* self-disarm while the netdev is closed,
+		 * during context swap, or when the current queue is disabled
 		 */
-		if (unlikely(bp->irq_quiesced)) {
+		if (unlikely(bp->irq_quiesced ||
+			     q >= num_queues)) {

[Severity: Medium]
Can this read a freed context? bp->ctx is now dereferenced before bp->irq_quiesced is checked. The previous handler never touched bp->ctx while quiesced.

macb_close() frees and clears the context outside bp->lock, and the reset_hw error path in macb_open() does the same:

macb_close() {
    ...
	macb_free(bp->ctx);
	kfree(bp->ctx);
	bp->ctx = NULL;
    ...
}

The queue IRQs are requested with IRQF_SHARED in macb_init_dflt() and stay registered while the interface is down. The handler can still be entered after the synchronize_irq() in macb_quiesce_start(), for example when another device on the shared line fires and ISR reads non-zero.

If that happens between kfree() and the NULL store, ->num_queues is read from freed memory.

bp->ctx is also loaded twice with plain loads. If the compiler re-reads it after the closing CPU has stored NULL, would the handler dereference NULL?

Quoted 4 lines
drivers/net/ethernet/cadence/macb_main.c +0 -0
 			queue_writel(queue, IDR, -1);
 			macb_queue_isr_clear(bp, queue, -1);
 			break;
 		}

[ ... ]

drivers/net/ethernet/cadence/macb_main.c static void macb_poll_controller(struct net_device *netdev) · line 2428

Quoted 9 lines
drivers/net/ethernet/cadence/macb_main.c +1 -1
@@ -2402,7 +2422,7 @@ static void macb_poll_controller(struct net_device *netdev)
 	unsigned int q;
 
 	local_irq_save(flags);
-	for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
+	for (q = 0, queue = bp->queues; q < bp->ctx->num_queues; ++q, ++queue)
 		macb_interrupt(netdev->irq, queue);
 	local_irq_restore(flags);
 }

[Severity: Medium]
Is it safe to dereference bp->ctx here? The loop bound used to be the constant bp->num_queues. It is now re-read from bp->ctx on every iteration, protected only by local_irq_save(): no bp->lock, no mac_cfg_lock and no RCU.

netpoll is not serialized against .set_channels(), .set_ringparam() or ndo_change_mtu. Each of these can swap bp->ctx, and macb_context_swap_end() frees the old context right after publishing the new one:

macb_context_swap_end() {
    ...
		WRITE_ONCE(bp->ctx, new_ctx);
    ...
	mutex_unlock(&bp->mac_cfg_lock);
    ...
	if (new_ctx) {
		macb_free(old_ctx);
		kfree(old_ctx);
	}
}

A CPU in macb_poll_controller() that has already loaded the old bp->ctx can then read ->num_queues from freed memory.

If that memory has been reused, could the garbage bound walk bp->queues[] past MACB_MAX_QUEUES and call macb_interrupt() on an out-of-bounds queue?

The new .set_channels() path adds another runtime trigger for this swap.

[ ... ]

drivers/net/ethernet/cadence/macb_main.c static u32 gem_get_rx_ring_count(struct net_device *netdev) · line 4552

Quoted 7 lines
drivers/net/ethernet/cadence/macb_main.c +1 -1
@@ -4513,7 +4548,7 @@ static u32 gem_get_rx_ring_count(struct net_device *netdev)
 {
 	struct macb *bp = netdev_priv(netdev);
 
-	return bp->num_queues;
+	return bp->max_num_queues;
 }

[Severity: Medium]
Should this return bp->configured_num_queues? In the same gem_ethtool_ops table, macb_get_channels() reports combined_count as bp->configured_num_queues. gem_set_rxnfc() also rejects any ring_cookie

Quoted 1 line

= bp->configured_num_queues.

Take 4-queue hardware after "ethtool -L ethX combined 2". ETHTOOL_GRXRINGS (ethtool_get_rxrings() in net/ethtool/ioctl.c) still reports 4 RX rings. Rings 2 and 3 have QUEUE_DISABLE written by macb_init_buffers(), and the driver's own rxnfc insert path rejects steering to them with -EINVAL.

include/linux/ethtool.h documents this callback as returning the number of RX rings.

[ ... ]

Quoted 3 lines
drivers/net/ethernet/cadence/macb_main.c +3 -0
+static int macb_set_channels(struct net_device *netdev,
+			     struct ethtool_channels *ch)
+{

[ ... ]

Quoted 9 lines
drivers/net/ethernet/cadence/macb_main.c +9 -0
+	if (running) {
+		new_ctx = macb_context_alloc(bp, num_queues, netdev->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);
+	}

drivers/net/ethernet/cadence/macb_main.c static int gem_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd) · line 4668

Quoted 10 lines
drivers/net/ethernet/cadence/macb_main.c +10 -0
+
+	ret = netif_set_real_num_queues(bp->netdev, num_queues, num_queues);
+	if (ret) {
+		if (running) {
+			macb_free(new_ctx);
+			kfree(new_ctx);
+			macb_context_swap_end(bp, NULL);
+		}
+		return ret;
+	}

[Severity: Medium]
What state is the old context in if netif_set_real_num_queues() fails here? When the queue count grows on a registered netdev, it can return -ENOMEM from netdev_queue_update_kobjects() or net_rx_queue_update_kobjects().

By that point macb_context_swap_start() has done the following, which only makes sense if the old context is about to be discarded:

- quiesced NAPI and IRQs
- halted Tx and cleared TE/RE
- called netdev_tx_reset_queue() on every old queue

The old rings can still hold in-flight or completed-but-unreaped Tx descriptors between txq->tail and txq->head.

macb_context_swap_end(bp, NULL) then re-activates the old context:

macb_context_swap_end(bp, NULL)
macb_init_buffers(bp) <- TBQP/RBQP rewound to ring index 0
napi_enable()
IER writes, TE/RE re-enabled if link is up

Neither mog_init_rings() nor gem_shuffle_tx_rings() is called. The software txq->head/tail and rxq->tail stay mid-ring while the hardware restarts from the ring base.

Later, macb_tx_complete() or macb_tx_error_task() reaps the old frames and netdev_tx_completed_queue() reports bytes that DQL no longer counts. Could that trip this check?

lib/dynamic_queue_limits.c:dql_completed() {
    ...
	BUG_ON(count > num_queued - dql->num_completed);
    ...
}

Even without the BUG_ON, wouldn't the mismatched hardware and software ring indices stall Tx/Rx?

macb_change_mtu() and macb_set_ringparam() do all of their fallible work before macb_context_swap_start(). This rollback-after-swap path is new in this patch.

Quoted 8 lines
drivers/net/ethernet/cadence/macb_main.c +8 -0
+
+	bp->configured_num_queues = num_queues;
+
+	if (running)
+		macb_context_swap_end(bp, new_ctx);
+
+	return 0;
+}

[ ... ]

drivers/net/ethernet/cadence/macb_main.c static void macb_taprio_destroy(struct net_device *netdev) · line 5030

Quoted 9 lines
drivers/net/ethernet/cadence/macb_main.c +4 -1
@@ -4910,7 +5024,12 @@ static void macb_taprio_destroy(struct net_device *netdev)
 	unsigned int q;
 
 	netdev_reset_tc(netdev);
-	queue_mask = BIT_U32(bp->num_queues) - 1;
+	bp->taprio_gated_queues = 0;
+
+	if (unlikely(pm_runtime_suspended(&bp->pdev->dev)))
+		return;

[Severity: Medium]
Can this leave ENST gating enabled in hardware while bp->taprio_gated_queues says that no queue is gated? When the device is runtime-suspended, the function returns before writing ENST_CONTROL or the per-queue ENST_START_TIME, ENST_ON_TIME and ENST_OFF_TIME registers.

macb_setup_taprio() used to reject every command while runtime-suspended. With this patch only TAPRIO_CMD_REPLACE is rejected:

	case TAPRIO_CMD_REPLACE:
		if (unlikely(pm_runtime_suspended(&bp->pdev->dev))) {

So DESTROY now succeeds silently while suspended. The commit message does not mention this change.

macb_runtime_suspend() only gates clocks, so the ENST registers keep their contents. Only macb_taprio_setup_replace() and macb_taprio_destroy() write ENST_CONTROL. macb_open(), macb_init_hw() and macb_reset_hw() never clear it.

The new guard in macb_set_channels() checks only the software mask:

	if (bp->taprio_gated_queues >> num_queues) {

That would let this sequence through:

1. Interface up, with a taprio offload gating queue 3.
2. ip link set down: macb_close() calls pm_runtime_put() and the device autosuspends.
3. tc qdisc del: the mask is cleared and the hardware is left untouched.
4. ethtool -L ethX combined 2: accepted.
5. ip link set up.

On open, macb_init_buffers() writes QUEUE_DISABLE to RBQP/TBQP for queue 3 while its ENST window is still enabled. Isn't this the case the commit message describes as refused: "a transmission window would be reserved but unusable"?

Separately, this part is a pre-existing issue and not introduced by this patch: the pm_runtime_suspended() check takes no PM reference. The device can therefore suspend between the check and the MMIO writes that follow.

Quoted 4 lines
drivers/net/ethernet/cadence/macb_main.c +2 -0
+
+	queue_mask = BIT_U32(bp->max_num_queues) - 1;
 
 	scoped_guard(spinlock_irqsave, &bp->lock) {

[ ... ]

drivers/net/ethernet/cadence/macb_main.c static struct macb_context *macb_context_alloc(struct macb *bp, · line 3090

Quoted 8 lines
drivers/net/ethernet/cadence/macb_main.c +1 -0
@@ -3063,6 +3084,7 @@ static struct macb_context *macb_context_alloc(struct macb *bp,
 		return ERR_PTR(-ENOMEM);
 
 	ctx->info = &bp->info;
+	ctx->num_queues = num_queues;
 	ctx->rx_buffer_size = macb_rx_buffer_size(bp, mtu);
 	ctx->rx_ring_size = rx_ring_size;
 	ctx->tx_ring_size = tx_ring_size;

[Severity: Medium]
Does this leave ctx->num_queues at zero on EMAC? With this patch, macb_context_alloc() is the only place that initializes the field. at91ether_open() does not call macb_context_alloc(); it allocates the context itself:

at91ether_open() {
    ...
	WRITE_ONCE(bp->ctx, kzalloc_obj(*bp->ctx));
    ...
	bp->ctx->info = &bp->info;
    ...
}

As a result, bp->ctx->num_queues stays 0 for as long as an AT91RM9200 EMAC interface is up. That breaks the invariant
0 < ctx->num_queues <= bp->max_num_queues stated in the commit message.

Before this patch, the loops below were bounded by bp->num_queues, which is 1 on EMAC. They now run zero times:

- macb_suspend(): the WoL loop that writes QUEUE_DISABLE or the tie-off address to RBQP and masks the queue interrupts, and the napi_disable() loop.
- macb_resume(): the matching per-queue loop.
- macb_reset_hw(): the IDR/ISR clear loop, which macb_suspend() calls when WoL is disabled.

On system suspend, queue 0 interrupts are therefore no longer masked by macb_reset_hw(). With WoL enabled, the RX queue is not tied off before RE is turned back on.

Should at91ether_open() set bp->ctx->num_queues to 1 (or to bp->configured_num_queues)? Or is there a reason not to use macb_context_alloc() there?

[ ... ]

Quoted 5 lines
drivers/net/ethernet/cadence/macb_main.c +0 -1
@@ -4876,11 +4986,12 @@ static int macb_taprio_setup_replace(struct net_device *netdev,
 	/* All validations passed - proceed with hardware configuration */
 	scoped_guard(spinlock_irqsave, &bp->lock) {
 		/* Disable ENST queues if running before configuring */
-		queue_mask = BIT_U32(bp->num_queues) - 1;

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

Quoted 10 lines
drivers/net/ethernet/cadence/macb_main.c +3 -1
+		queue_mask = BIT_U32(bp->max_num_queues) - 1;
 		gem_writel(bp, ENST_CONTROL,
 			   queue_mask << GEM_ENST_DISABLE_QUEUE_OFFSET);
 
-		for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
+		for (q = 0, queue = bp->queues; q < bp->configured_num_queues;
+		     ++q, ++queue) {
 			/* Configure queue timing registers */
 			queue_writel(queue, ENST_START_TIME,
 				     enst_queue[q].start_time_mask);

[Severity: Medium]
Should this loop still cover bp->max_num_queues? Other code already does:

- The ENST_CONTROL write just above disables all bp->max_num_queues queues.
- macb_taprio_destroy() clears ENST_START_TIME, ENST_ON_TIME and ENST_OFF_TIME for all bp->max_num_queues queues.

Here, however, enst_queue is sized for bp->configured_num_queues, and only those queues get their timing registers written.

Take 4-queue hardware after "ethtool -L ethX combined 2". A later taprio replace leaves queues 2 and 3 with whatever their ENST registers already hold. On a device that never had a schedule, that is the reset value (0x0001FFFF in ENST_ON_TIME).

commit 04a943067baa ("net: macb: configure ENST registers for all queues") fixed exactly this: leaving the reset values on unused queues disrupts ENST scheduling of the configured queues, even when ENST_CONTROL enables only the configured ones.

Before this patch, every queue below bp->num_queues was written, with zeroes for the unused ones. Would it work to allocate enst_queue with bp->max_num_queues and run this loop up to bp->max_num_queues? The num_entries and queue_id checks would still compare against bp->configured_num_queues.

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261113-macb-set-channels-v6-0-5de59d9e7f02%40bootlin.com
author maintainer
#5 lore raw

Re: [PATCH net-next v6 1/2] net: macb: implement ethtool_ops.get|set_channels()

Hello netdev-bot+sashiko,

Quoted 81 lines · CET,

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

Critical: 0 · High: 0 · Medium: 7 · Low: 0

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

- [Medium] macb_taprio_destroy() (drivers/net/ethernet/cadence/macb_main.c) now clears the software…
- [Medium] gem_get_rx_ring_count() now returns bp->max_num_queues.
- [Medium] When the interface is running, macb_set_channels() calls macb_context_swap_start(bp) before netif_set_real_num_queues().
- [Medium] macb_interrupt() now runs if (bp->ctx) num_queues = READ_ONCE(bp->ctx->num_queues); (lines 2352-2353) under bp->lock,…
- [Medium] macb_poll_controller() now evaluates q < bp->ctx->num_queues on every loop iteration with only local_irq_save(), without bp->lock,…
- [Medium] net: macb: at91ether_open() leaves bp->ctx->num_queues uninitialized as 0
- [Medium] net: macb: macb_taprio_setup_replace() skips ENST init on disabled queues

--

net: macb: implement ethtool_ops.get|set_channels()

This commit moves the queue count into the active context (ctx->num_queues) and adds bp->max_num_queues and bp->configured_num_queues. It implements .get_channels() and .set_channels(), using the context swap window when the interface is running. It also records the TAPRIO-gated queue mask so that .set_channels() can refuse to disable gated queues.

Quoted 4 lines
drivers/net/ethernet/cadence/macb_main.c +0 -0
index 5bd299fc85a9c..e7325af4b399c 100644

[ ... ]

Quoted 6 lines
drivers/net/ethernet/cadence/macb_main.c +2 -0
@@ -2332,19 +2349,22 @@ static irqreturn_t macb_interrupt(int irq, void *dev_id)
 
 	spin_lock(&bp->lock);
 
+	if (bp->ctx)
+		num_queues = READ_ONCE(bp->ctx->num_queues);

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

Quoted 10 lines
drivers/net/ethernet/cadence/macb_main.c +5 -3
+
 	while (status) {
-		/* self-disarm while the netdev is closed
-		 * or during context swap
+		/* self-disarm while the netdev is closed,
+		 * during context swap, or when the current queue is disabled
 		 */
-		if (unlikely(bp->irq_quiesced)) {
+		if (unlikely(bp->irq_quiesced ||
+			     q >= num_queues)) {

[Severity: Medium]
Can this read a freed context? bp->ctx is now dereferenced before bp->irq_quiesced is checked. The previous handler never touched bp->ctx while quiesced.

macb_close() frees and clears the context outside bp->lock, and the reset_hw error path in macb_open() does the same:

macb_close() {
    ...
	macb_free(bp->ctx);
	kfree(bp->ctx);
	bp->ctx = NULL;
    ...
}

The queue IRQs are requested with IRQF_SHARED in macb_init_dflt() and stay registered while the interface is down. The handler can still be entered after the synchronize_irq() in macb_quiesce_start(), for example when another device on the shared line fires and ISR reads non-zero.

Quoted 6 lines

If that happens between kfree() and the NULL store, ->num_queues is read from freed memory.

bp->ctx is also loaded twice with plain loads. If the compiler re-reads it after the closing CPU has stored NULL, would the handler dereference NULL?

Yes this is true. Fixed with:

static irqreturn_t macb_interrupt(int irq, void *dev_id)

{
	// ...

	status = queue_readl(queue, ISR);

	if (unlikely(!status))
		return IRQ_NONE;

	spin_lock(&bp->lock);

	/* Only dereference bp->ctx when not quiesced.
	 * Quiesced state cannot change as we are in a bp->lock section.
	 */
	if (!bp->irq_quiesced && bp->ctx)
		num_queues = READ_ONCE(bp->ctx->num_queues);

	// ...

We only read (and need) num_queues when not quiesced. bp->irq_quiesced cannot change under our feet because we are in a bp->lock section.

Quoted 41 lines
Quoted 5 lines
drivers/net/ethernet/cadence/macb_main.c +0 -0
 			queue_writel(queue, IDR, -1);
 			macb_queue_isr_clear(bp, queue, -1);
 			break;
 		}

[ ... ]

drivers/net/ethernet/cadence/macb_main.c static void macb_poll_controller(struct net_device *netdev) · line 2428

Quoted 9 lines
drivers/net/ethernet/cadence/macb_main.c +1 -1
@@ -2402,7 +2422,7 @@ static void macb_poll_controller(struct net_device *netdev)
 	unsigned int q;
 
 	local_irq_save(flags);
-	for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
+	for (q = 0, queue = bp->queues; q < bp->ctx->num_queues; ++q, ++queue)
 		macb_interrupt(netdev->irq, queue);
 	local_irq_restore(flags);
 }

[Severity: Medium]
Is it safe to dereference bp->ctx here? The loop bound used to be the constant bp->num_queues. It is now re-read from bp->ctx on every iteration, protected only by local_irq_save(): no bp->lock, no mac_cfg_lock and no RCU.

netpoll is not serialized against .set_channels(), .set_ringparam() or ndo_change_mtu. Each of these can swap bp->ctx, and macb_context_swap_end() frees the old context right after publishing the new one:

macb_context_swap_end() {
    ...
		WRITE_ONCE(bp->ctx, new_ctx);
    ...
	mutex_unlock(&bp->mac_cfg_lock);
    ...
	if (new_ctx) {
		macb_free(old_ctx);
		kfree(old_ctx);
	}
}
Quoted 7 lines

A CPU in macb_poll_controller() that has already loaded the old bp->ctx can then read ->num_queues from freed memory.

If that memory has been reused, could the garbage bound walk bp->queues[] past MACB_MAX_QUEUES and call macb_interrupt() on an out-of-bounds queue?

The new .set_channels() path adds another runtime trigger for this swap.

No it isn't safe. Let's replace bp->ctx->num_queues by bp->configured_num_queues. We don't want protect HERE against a race with configured_num_queues changing. The macb_interrupt() function we call is safe to be called on a disabled queue, because it checks for that internally (with bp->lock acquired).

Quoted 17 lines

[ ... ]

drivers/net/ethernet/cadence/macb_main.c static u32 gem_get_rx_ring_count(struct net_device *netdev) · line 4552

Quoted 7 lines
drivers/net/ethernet/cadence/macb_main.c +1 -1
@@ -4513,7 +4548,7 @@ static u32 gem_get_rx_ring_count(struct net_device *netdev)
 {
 	struct macb *bp = netdev_priv(netdev);
 
-	return bp->num_queues;
+	return bp->max_num_queues;
 }

[Severity: Medium]
Should this return bp->configured_num_queues? In the same gem_ethtool_ops table, macb_get_channels() reports combined_count as bp->configured_num_queues. gem_set_rxnfc() also rejects any ring_cookie '>= bp->configured_num_queues'.

Quoted 8 lines

Take 4-queue hardware after "ethtool -L ethX combined 2". ETHTOOL_GRXRINGS (ethtool_get_rxrings() in net/ethtool/ioctl.c) still reports 4 RX rings. Rings 2 and 3 have QUEUE_DISABLE written by macb_init_buffers(), and the driver's own rxnfc insert path rejects steering to them with -EINVAL.

include/linux/ethtool.h documents this callback as returning the number of RX rings.

Correct. I checked all other driver implementations and they all return active queue count.

Quoted 66 lines

[ ... ]

Quoted 3 lines
drivers/net/ethernet/cadence/macb_main.c +3 -0
+static int macb_set_channels(struct net_device *netdev,
+			     struct ethtool_channels *ch)
+{

[ ... ]

Quoted 9 lines
drivers/net/ethernet/cadence/macb_main.c +9 -0
+	if (running) {
+		new_ctx = macb_context_alloc(bp, num_queues, netdev->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);
+	}

drivers/net/ethernet/cadence/macb_main.c static int gem_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd) · line 4668

Quoted 10 lines
drivers/net/ethernet/cadence/macb_main.c +10 -0
+
+	ret = netif_set_real_num_queues(bp->netdev, num_queues, num_queues);
+	if (ret) {
+		if (running) {
+			macb_free(new_ctx);
+			kfree(new_ctx);
+			macb_context_swap_end(bp, NULL);
+		}
+		return ret;
+	}

[Severity: Medium]
What state is the old context in if netif_set_real_num_queues() fails here? When the queue count grows on a registered netdev, it can return -ENOMEM from netdev_queue_update_kobjects() or net_rx_queue_update_kobjects().

By that point macb_context_swap_start() has done the following, which only makes sense if the old context is about to be discarded:

- quiesced NAPI and IRQs
- halted Tx and cleared TE/RE
- called netdev_tx_reset_queue() on every old queue

The old rings can still hold in-flight or completed-but-unreaped Tx descriptors between txq->tail and txq->head.

macb_context_swap_end(bp, NULL) then re-activates the old context:

macb_context_swap_end(bp, NULL)
macb_init_buffers(bp) <- TBQP/RBQP rewound to ring index 0
napi_enable()
IER writes, TE/RE re-enabled if link is up

Neither mog_init_rings() nor gem_shuffle_tx_rings() is called. The software txq->head/tail and rxq->tail stay mid-ring while the hardware restarts from the ring base.

Later, macb_tx_complete() or macb_tx_error_task() reaps the old frames and netdev_tx_completed_queue() reports bytes that DQL no longer counts. Could that trip this check?

lib/dynamic_queue_limits.c:dql_completed() {
    ...
	BUG_ON(count > num_queued - dql->num_completed);
    ...
}
Quoted 6 lines

Even without the BUG_ON, wouldn't the mismatched hardware and software ring indices stall Tx/Rx?

macb_change_mtu() and macb_set_ringparam() do all of their fallible work before macb_context_swap_start(). This rollback-after-swap path is new in this patch.

So this is true (and was reported by Nicolai before). But the solution isn't the obvious "call into mog_init_rings" or "copy what macb_tx_error_task does", because either would be broken.

We do want the swap start to quiesce BH and IRQs. We do want to halt the HW. The change we need is, on swap failure:
- to not touch HW ring cursors
- to not reset BQL

With that, the HW will think we just stopped for a few µs/ms and started back up, with the same rings and same cursors. Notice we don't even trigger a refill, which would probably fail because we are most likely under memory pressure. The next softirq will deal with that.

Quoted 60 lines
Quoted 9 lines
drivers/net/ethernet/cadence/macb_main.c +8 -0
+
+	bp->configured_num_queues = num_queues;
+
+	if (running)
+		macb_context_swap_end(bp, new_ctx);
+
+	return 0;
+}

[ ... ]

drivers/net/ethernet/cadence/macb_main.c static void macb_taprio_destroy(struct net_device *netdev) · line 5030

Quoted 9 lines
drivers/net/ethernet/cadence/macb_main.c +4 -1
@@ -4910,7 +5024,12 @@ static void macb_taprio_destroy(struct net_device *netdev)
 	unsigned int q;
 
 	netdev_reset_tc(netdev);
-	queue_mask = BIT_U32(bp->num_queues) - 1;
+	bp->taprio_gated_queues = 0;
+
+	if (unlikely(pm_runtime_suspended(&bp->pdev->dev)))
+		return;

[Severity: Medium]
Can this leave ENST gating enabled in hardware while bp->taprio_gated_queues says that no queue is gated? When the device is runtime-suspended, the function returns before writing ENST_CONTROL or the per-queue ENST_START_TIME, ENST_ON_TIME and ENST_OFF_TIME registers.

macb_setup_taprio() used to reject every command while runtime-suspended. With this patch only TAPRIO_CMD_REPLACE is rejected:

	case TAPRIO_CMD_REPLACE:
		if (unlikely(pm_runtime_suspended(&bp->pdev->dev))) {

So DESTROY now succeeds silently while suspended. The commit message does not mention this change.

macb_runtime_suspend() only gates clocks, so the ENST registers keep their contents. Only macb_taprio_setup_replace() and macb_taprio_destroy() write ENST_CONTROL. macb_open(), macb_init_hw() and macb_reset_hw() never clear it.

The new guard in macb_set_channels() checks only the software mask:

	if (bp->taprio_gated_queues >> num_queues) {

That would let this sequence through:

1. Interface up, with a taprio offload gating queue 3.
2. ip link set down: macb_close() calls pm_runtime_put() and the device autosuspends.
3. tc qdisc del: the mask is cleared and the hardware is left untouched.
4. ethtool -L ethX combined 2: accepted.
5. ip link set up.

On open, macb_init_buffers() writes QUEUE_DISABLE to RBQP/TBQP for queue 3 while its ENST window is still enabled. Isn't this the case the commit message describes as refused: "a transmission window would be reserved but unusable"?

Quoted 4 lines

Separately, this part is a pre-existing issue and not introduced by this patch: the pm_runtime_suspended() check takes no PM reference. The device can therefore suspend between the check and the MMIO writes that follow.

Yes this is all true.

We fix it by re-applying ENST config on the next link-up if the link speed is the same. To implement that, we need a cached ENST config/schedule. And with that cached state, we'll be able to check properly at set_channels if we can go through or if we should refuse the request.

See the new patch that prefaces the series in the upcoming revision.

Quoted 46 lines
Quoted 5 lines
drivers/net/ethernet/cadence/macb_main.c +2 -0
+
+	queue_mask = BIT_U32(bp->max_num_queues) - 1;
 
 	scoped_guard(spinlock_irqsave, &bp->lock) {

[ ... ]

drivers/net/ethernet/cadence/macb_main.c static struct macb_context *macb_context_alloc(struct macb *bp, · line 3090

Quoted 8 lines
drivers/net/ethernet/cadence/macb_main.c +1 -0
@@ -3063,6 +3084,7 @@ static struct macb_context *macb_context_alloc(struct macb *bp,
 		return ERR_PTR(-ENOMEM);
 
 	ctx->info = &bp->info;
+	ctx->num_queues = num_queues;
 	ctx->rx_buffer_size = macb_rx_buffer_size(bp, mtu);
 	ctx->rx_ring_size = rx_ring_size;
 	ctx->tx_ring_size = tx_ring_size;

[Severity: Medium]
Does this leave ctx->num_queues at zero on EMAC? With this patch, macb_context_alloc() is the only place that initializes the field. at91ether_open() does not call macb_context_alloc(); it allocates the context itself:

at91ether_open() {
    ...
	WRITE_ONCE(bp->ctx, kzalloc_obj(*bp->ctx));
    ...
	bp->ctx->info = &bp->info;
    ...
}

As a result, bp->ctx->num_queues stays 0 for as long as an AT91RM9200 EMAC interface is up. That breaks the invariant
0 < ctx->num_queues <= bp->max_num_queues stated in the commit message.

Before this patch, the loops below were bounded by bp->num_queues, which is 1 on EMAC. They now run zero times:

- macb_suspend(): the WoL loop that writes QUEUE_DISABLE or the tie-off address to RBQP and masks the queue interrupts, and the napi_disable() loop.
- macb_resume(): the matching per-queue loop.
- macb_reset_hw(): the IDR/ISR clear loop, which macb_suspend() calls when WoL is disabled.

Quoted 7 lines

On system suspend, queue 0 interrupts are therefore no longer masked by macb_reset_hw(). With WoL enabled, the RX queue is not tied off before RE is turned back on.

Should at91ether_open() set bp->ctx->num_queues to 1 (or to bp->configured_num_queues)? Or is there a reason not to use macb_context_alloc() there?

Yes, added this:

	// in at91ether_open()
	bp->ctx->num_queues = 1;
Quoted 41 lines

[ ... ]

Quoted 5 lines
drivers/net/ethernet/cadence/macb_main.c +0 -1
@@ -4876,11 +4986,12 @@ static int macb_taprio_setup_replace(struct net_device *netdev,
 	/* All validations passed - proceed with hardware configuration */
 	scoped_guard(spinlock_irqsave, &bp->lock) {
 		/* Disable ENST queues if running before configuring */
-		queue_mask = BIT_U32(bp->num_queues) - 1;

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

Quoted 10 lines
drivers/net/ethernet/cadence/macb_main.c +3 -1
+		queue_mask = BIT_U32(bp->max_num_queues) - 1;
 		gem_writel(bp, ENST_CONTROL,
 			   queue_mask << GEM_ENST_DISABLE_QUEUE_OFFSET);
 
-		for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
+		for (q = 0, queue = bp->queues; q < bp->configured_num_queues;
+		     ++q, ++queue) {
 			/* Configure queue timing registers */
 			queue_writel(queue, ENST_START_TIME,
 				     enst_queue[q].start_time_mask);

[Severity: Medium]
Should this loop still cover bp->max_num_queues? Other code already does:

- The ENST_CONTROL write just above disables all bp->max_num_queues queues.
- macb_taprio_destroy() clears ENST_START_TIME, ENST_ON_TIME and ENST_OFF_TIME for all bp->max_num_queues queues.

Here, however, enst_queue is sized for bp->configured_num_queues, and only those queues get their timing registers written.

Take 4-queue hardware after "ethtool -L ethX combined 2". A later taprio replace leaves queues 2 and 3 with whatever their ENST registers already hold. On a device that never had a schedule, that is the reset value (0x0001FFFF in ENST_ON_TIME).

commit 04a943067baa ("net: macb: configure ENST registers for all queues") fixed exactly this: leaving the reset values on unused queues disrupts ENST scheduling of the configured queues, even when ENST_CONTROL enables only the configured ones.

Quoted 5 lines

Before this patch, every queue below bp->num_queues was written, with zeroes for the unused ones. Would it work to allocate enst_queue with bp->max_num_queues and run this loop up to bp->max_num_queues? The num_entries and queue_id checks would still compare against bp->configured_num_queues.

Yes, the rework of ENST mentioned above fixes this. We now have a function that uses the cached ENST schedule and reapplies it. It does write all schedule timings to all queues, even the disabled ones.

Thanks,

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

author maintainer
#6 lore raw

PATCH v6 2/2 net: macb: distribute evenly Tx SRAM segments

In reply to Théo Lebrun

GEM has registers to configure the Tx SRAM segments distribution across queues. The reset value is apprioriate (even spread) but we need to care if/when number of active queues is modified (or if we inherited unevenly initialised hardware from bootloader).

To distribute segments, we take as input the number of queues (ctx->num_queues) and the number of segments (found inside DCFG6). Its output is a number of segments for each queue, formatted as powers-of-two (eg 2 for queue 0 means it has 2^2=4 segments).

---
 drivers/net/ethernet/cadence/macb.h      | 14 ++++++
 drivers/net/ethernet/cadence/macb_main.c | 76 +++++++++++++++++++++++++++++++-
 2 files changed, 88 insertions(+), 2 deletions(-)
drivers/net/ethernet/cadence/macb.h +14 -0
index 0879fb2b198b..cc57071c869a 100644
@@ -194,6 +194,10 @@
 /* Screener Type 2 match registers */
 #define GEM_SCRT2		0x540
 
+/* Tx queue segment allocation */
+#define GEM_TXQSEGALLOC_LOWER	0x05A0
+#define GEM_TXQSEGALLOC_UPPER	0x05A4
+
 /* EtherType registers */
 #define GEM_ETHT		0x06E0
 
@@ -545,6 +549,8 @@
 #define GEM_PBUF_CUTTHRU_SIZE			1
 #define GEM_DAW64_OFFSET			23
 #define GEM_DAW64_SIZE				1
+#define GEM_SEGMENTS_BIT_SIZE_OFFSET		16
+#define GEM_SEGMENTS_BIT_SIZE_SIZE		3
 
 /* Bitfields in DCFG8. */
 #define GEM_T1SCR_OFFSET			24
@@ -657,6 +663,13 @@
 #define GEM_CMPCEN_OFFSET			30 /* Compare C Enable */
 #define GEM_CMPCEN_SIZE				1
 
+/* Bitfields in TXQSEGALLOC (Q0 at 2:0, Q1 at 6:4, etc.) */
+#define GEM_TXQSEGALLOC_MASK(q_) \
+	({ \
+		typeof(q_) q__ = (q_); \
+		GENMASK(4 * (q__) + 2, 4 * (q__)); \
+	})
+
 /* Bitfields in ETHT */
 #define GEM_ETHTCMP_OFFSET			0 /* EtherType compare value */
 #define GEM_ETHTCMP_SIZE			16
@@ -785,6 +798,7 @@
 #define MACB_CAPS_USRIO_HAS_MII			BIT(26)
 #define MACB_CAPS_USRIO_HAS_REFCLK_SOURCE	BIT(27)
 #define MACB_CAPS_USRIO_HAS_TSUCLK_SOURCE	BIT(28)
+#define MACB_CAPS_TXQSEGALLOC			BIT(29)
 
 /* LSO settings */
 #define MACB_LSO_UFO_ENABLE			0x01
drivers/net/ethernet/cadence/macb_main.c +74 -2

106 lines not on this page · raw message

Signature · 1 line
2.56.0
author maintainer
#7 lore raw

Re: [PATCH net-next v6 2/2] net: macb: distribute evenly Tx SRAM segments

Quoted 46 lines · Théo Lebrun
drivers/net/ethernet/cadence/macb_main.c +46 -0
+/*
+ * Distribute evenly available segments across queues. The computation is
+ * complex because (1) segments are counted in powers of two and (2) a queue
+ * can only use up to 8 segments. There are four types of cases:
+ *  - Sharing all segments equally is doable. Take num_queues=4 and
+ *    num_segments=16. Each queue will get 2^2=4 segments.
+ *  - Sharing all segments is doable. Take num_queues=5 and num_segments=16.
+ *    Three queues will get 2^2=4 segments and two will get 2^1=2 segments.
+ *  - Sharing all segments is not doable because not enough queues are
+ *    available. Take num_queues=1 and num_segments=16; queue 0 can only have 8
+ *    segments.
+ *  - Sharing all segments is not doable because not enough segments are
+ *    available. Take num_queues=4 and num_segments=2.
+ *
+ * We start by computing the power each queue will have. For num_queues=5 and
+ * num_segments=16, each queue will have at least 2^1 segments. That leaves us
+ * with remaining_segments=6. If we increase the power for a queue, we get a
+ * delta of 2 (2^2-2^1). The first three queues will therefore be advantaged
+ * and each have 2^2 segments. The remaining 2 queues will only have 2^1
+ * segments.
+ */
+static u32 gem_sram_distribute_segments(unsigned int num_queues,
+					unsigned int num_segments)
+{
+	unsigned int pow, remaining_segments, i;
+	unsigned int num_advantaged_queues = 0;
+	u32 val = 0;
+
+	/* When growing the max # of queues, make sure to return a u64 here and
+	 * to write into GEM_TXQSEGALLOC_UPPER.
+	 */
+	static_assert(MACB_MAX_QUEUES == 8);
+
+	/* pow=0 for all queues. ilog2(0) is dangerous. */
+	if (num_queues >= num_segments)
+		return 0;
+
+	pow = min_t(unsigned int, ilog2(num_segments / num_queues), 3);
+	remaining_segments = num_segments - num_queues * (1U << pow);
+
+	/* We can only distribute remaining segments if (1) there are remaining
+	 * segments and (2) we did not reach the max segments per queue (2^3).
+	 */
+	if (remaining_segments != 0 && pow != 3) {
+		unsigned int delta = (1U << (pow + 1)) - (1U << pow);
+

drivers/net/ethernet/cadence/macb_main.c static void macb_context_swap_end(struct macb *bp, · line 3490

Quoted 10 lines
drivers/net/ethernet/cadence/macb_main.c +10 -0
+		num_advantaged_queues = remaining_segments / delta;
+	}
+
+	for (i = 0; i < num_advantaged_queues; i++)
+		val |= field_prep(GEM_TXQSEGALLOC_MASK(i), pow + 1);
+	for (i = num_advantaged_queues; i < num_queues; i++)
+		val |= field_prep(GEM_TXQSEGALLOC_MASK(i), pow);
+
+	return val;
+}

This implementation doesn't use all segments if
num_segments > 8*num_queues. Reported by Nicolai and discussed here [0]. I don't think any hardware lands in that case, but let's fix the bug. I will let some time pass before iterating.

[0]: https://lore.kernel.org/all/[email protected]/

Thanks,

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

#8 lore raw

Re: [PATCH net-next v6 2/2] net: macb: distribute evenly Tx SRAM segments

In reply to Théo Lebrun
Quoted 28 lines · Théo Lebrun

GEM has registers to configure the Tx SRAM segments distribution across queues. The reset value is apprioriate (even spread) but we need to care if/when number of active queues is modified (or if we inherited unevenly initialised hardware from bootloader).

To distribute segments, we take as input the number of queues (ctx->num_queues) and the number of segments (found inside DCFG6). Its output is a number of segments for each queue, formatted as powers-of-two (eg 2 for queue 0 means it has 2^2=4 segments).

---
 drivers/net/ethernet/cadence/macb.h      | 14 ++++++
 drivers/net/ethernet/cadence/macb_main.c | 76 

+++++++++++++++++++++++++++++++-

[...]

static void macb_init_hw(struct macb *bp)
{

drivers/net/ethernet/cadence/macb_main.c +1 -0
 	u32 config;
@@ -3465,6 +3522,20 @@ static void macb_init_hw(struct macb *bp)
 	if (bp->caps & MACB_CAPS_JUMBO)
 		bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
 
+	/* Distribute Tx SRAM segments evenly based on active number of 

queues.

drivers/net/ethernet/cadence/macb_main.c +1 -0
+	 * TXQSEGALLOC is only documented for some GEM integrations.

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

Quoted 10 lines
drivers/net/ethernet/cadence/macb_main.c +10 -0
+	 */
+	if (bp->caps & MACB_CAPS_TXQSEGALLOC) {
+		unsigned int num_segments;
+		u32 val;
+
+		num_segments = 1U << GEM_BFEXT(SEGMENTS_BIT_SIZE,
+					       gem_readl(bp, DCFG6));
+		val = gem_sram_distribute_segments(bp->ctx->num_queues,
+						   num_segments);
+		gem_writel(bp, TXQSEGALLOC_LOWER, val);

This only runs on open, not on a context swap. So ethtool -L on a running interface keeps the old split until the next down/up?

Quoted 1 line

[...]

Regards,
Nicolai

author maintainer
#9 lore raw

Re: [PATCH net-next v6 2/2] net: macb: distribute evenly Tx SRAM segments

Hello Nicolai,

Quoted 26 lines · Nicolai Buchwitz
Quoted 25 lines · Théo Lebrun

GEM has registers to configure the Tx SRAM segments distribution across queues. The reset value is apprioriate (even spread) but we need to care if/when number of active queues is modified (or if we inherited unevenly initialised hardware from bootloader).

To distribute segments, we take as input the number of queues (ctx->num_queues) and the number of segments (found inside DCFG6). Its output is a number of segments for each queue, formatted as powers-of-two (eg 2 for queue 0 means it has 2^2=4 segments).

---
 drivers/net/ethernet/cadence/macb.h      | 14 ++++++
 drivers/net/ethernet/cadence/macb_main.c | 76 

+++++++++++++++++++++++++++++++-

[...]

static void macb_init_hw(struct macb *bp)
{

drivers/net/ethernet/cadence/macb_main.c +0 -0
 	u32 config;
@@ -3465,6 +3522,20 @@ static void macb_init_hw(struct macb *bp)
 	if (bp->caps & MACB_CAPS_JUMBO)
 		bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
 
Quoted 16 lines

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

Quoted 13 lines
(quoted diff) +1 -0
+	/* Distribute Tx SRAM segments evenly based on active number of 

queues.

drivers/net/ethernet/cadence/macb_main.c +11 -0
+	 * TXQSEGALLOC is only documented for some GEM integrations.
+	 */
+	if (bp->caps & MACB_CAPS_TXQSEGALLOC) {
+		unsigned int num_segments;
+		u32 val;
+
+		num_segments = 1U << GEM_BFEXT(SEGMENTS_BIT_SIZE,
+					       gem_readl(bp, DCFG6));
+		val = gem_sram_distribute_segments(bp->ctx->num_queues,
+						   num_segments);
+		gem_writel(bp, TXQSEGALLOC_LOWER, val);

This only runs on open, not on a context swap. So ethtool -L on a running interface keeps the old split until the next down/up?

Agreed! How I'll solve this:

- Make gem_sram_distribute_segments() self contained: it does the bp->caps check, figures out num_queues+num_segments on its own and does the writel(val, TXQSEGALLOC_LOWER) at the end.

- With that, we can call gem_sram_distribute_segments(bp) once at macb_init_hw() and once at swap_end.

Thanks,

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

#10 lore raw

Re: [PATCH net-next v6 2/2] net: macb: distribute evenly Tx SRAM segments

In reply to Théo Lebrun
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

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

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

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

- [High] The commit message says TXQSEGALLOC must be re-applied "if/when number of active queues is modified", but the patch only programs it…
- [Medium] gem_sram_distribute_segments() can build a TXQSEGALLOC value whose total segment count is larger than the num_segments that…

--

net: macb: distribute evenly Tx SRAM segments

This patch programs the GEM TXQSEGALLOC register from macb_init_hw(). It spreads the Tx SRAM segments advertised in DCFG6 across the active queues, with each queue's share encoded as a power of two. The new MACB_CAPS_TXQSEGALLOC capability is enabled for ZynqMP, Versal and EyeQ5.

Quoted 5 lines
drivers/net/ethernet/cadence/macb_main.c +0 -0
index e7325af4b399c..dc1624815f71e 100644
@@ -3432,6 +3432,63 @@ static void macb_context_swap_end(struct macb *bp,

[ ... ]

Quoted 3 lines
drivers/net/ethernet/cadence/macb_main.c +3 -0
+static u32 gem_sram_distribute_segments(unsigned int num_queues,
+					unsigned int num_segments)
+{

[ ... ]

Quoted 3 lines
drivers/net/ethernet/cadence/macb_main.c +3 -0
+	/* pow=0 for all queues. ilog2(0) is dangerous. */
+	if (num_queues >= num_segments)
+		return 0;

[ ... ]

drivers/net/ethernet/cadence/macb_main.c static void macb_context_swap_end(struct macb *bp, · line 3490

Quoted 7 lines
drivers/net/ethernet/cadence/macb_main.c +7 -0
+	for (i = 0; i < num_advantaged_queues; i++)
+		val |= field_prep(GEM_TXQSEGALLOC_MASK(i), pow + 1);
+	for (i = num_advantaged_queues; i < num_queues; i++)
+		val |= field_prep(GEM_TXQSEGALLOC_MASK(i), pow);
+
+	return val;
+}

[Severity: Medium]
Can the value built by gem_sram_distribute_segments() add up to more segments than the num_segments read from DCFG6?

A field value of 0 still means 2^0 = 1 segment. There seem to be two ways to over-allocate.

First, when num_queues > num_segments, the early return gives every active queue 1 segment. The comment above uses num_queues=4 and num_segments=2 as an example, which claims 4 segments out of 2.

Second, the loops only fill fields for i < num_queues. macb_init_hw() passes bp->ctx->num_queues, not the hardware queue count. The fields for hardware queues from ctx->num_queues up to bp->max_num_queues stay at 0. Each of those still claims 1 segment, on top of an active split that already uses every segment. With 4 hardware queues and 16 segments:

num_queues=2: 8 + 8 + 1 + 1 = 18
num_queues=3: 8 + 4 + 4 + 1 = 17

On Versal and EyeQ5 (MACB_CAPS_QUEUE_DISABLE), this happens after ethtool -L while the interface is down, followed by an open. The reset value is an even split over all hardware queues, so it adds up to the available segments.

If the GEM requires the total to stay within the DCFG6 count, could this give queues overlapping or out-of-range Tx SRAM regions?

Quoted 7 lines
drivers/net/ethernet/cadence/macb_main.c +3 -0
@@ -3465,6 +3522,20 @@ static void macb_init_hw(struct macb *bp)
 	if (bp->caps & MACB_CAPS_JUMBO)
 		bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
 
+	/* Distribute Tx SRAM segments evenly based on active number of queues.
+	 * TXQSEGALLOC is only documented for some GEM integrations.
+	 */

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

Quoted 10 lines
drivers/net/ethernet/cadence/macb_main.c +10 -0
+	if (bp->caps & MACB_CAPS_TXQSEGALLOC) {
+		unsigned int num_segments;
+		u32 val;
+
+		num_segments = 1U << GEM_BFEXT(SEGMENTS_BIT_SIZE,
+					       gem_readl(bp, DCFG6));
+		val = gem_sram_distribute_segments(bp->ctx->num_queues,
+						   num_segments);
+		gem_writel(bp, TXQSEGALLOC_LOWER, val);
+	}

[Severity: High]
The commit message says the distribution needs care "if/when number of active queues is modified". Is that case covered when the queue count changes on a running interface?

Only macb_open() and the resume path call macb_init_hw(). Running ethtool -L ethX combined N while the interface is up goes through:

macb_set_channels()
macb_context_swap_start()
macb_context_swap_end(bp, new_ctx)
macb_init_buffers()
macb_configure_dma()

macb_context_swap_end() installs a context with the new num_queues and turns RE/TE back on. Nothing on this path rewrites GEM_TXQSEGALLOC_LOWER. The Tx SRAM split stays sized for the old queue count until the next down/up or suspend/resume.

Two examples:

Open with 1 queue. q0 gets 8 segments and the other fields stay at 0 (1 segment each). If the interface then grows to N queues while up, each new queue runs with a single segment while TXPBMS is set in DMACFG.

Going from 2 queues to 1 while up leaves half of the SRAM assigned to the disabled queue.

Versal and EyeQ5 set both MACB_CAPS_QUEUE_DISABLE and MACB_CAPS_TXQSEGALLOC, so this path is reachable there.

Could the write be moved into a helper and also called from macb_context_swap_end() under bp->lock, next to macb_configure_dma()?

[ ... ]

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261113-macb-set-channels-v6-0-5de59d9e7f02%40bootlin.com
author maintainer
#11 lore raw

Re: [PATCH net-next v6 2/2] net: macb: distribute evenly Tx SRAM segments

Hello netdev-bot+sashiko,

Quoted 73 lines · CET,

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

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

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

- [High] The commit message says TXQSEGALLOC must be re-applied "if/when number of active queues is modified", but the patch only programs it…
- [Medium] gem_sram_distribute_segments() can build a TXQSEGALLOC value whose total segment count is larger than the num_segments that…

--

net: macb: distribute evenly Tx SRAM segments

This patch programs the GEM TXQSEGALLOC register from macb_init_hw(). It spreads the Tx SRAM segments advertised in DCFG6 across the active queues, with each queue's share encoded as a power of two. The new MACB_CAPS_TXQSEGALLOC capability is enabled for ZynqMP, Versal and EyeQ5.

Quoted 5 lines
drivers/net/ethernet/cadence/macb_main.c +0 -0
index e7325af4b399c..dc1624815f71e 100644
@@ -3432,6 +3432,63 @@ static void macb_context_swap_end(struct macb *bp,

[ ... ]

Quoted 3 lines
drivers/net/ethernet/cadence/macb_main.c +3 -0
+static u32 gem_sram_distribute_segments(unsigned int num_queues,
+					unsigned int num_segments)
+{

[ ... ]

Quoted 3 lines
drivers/net/ethernet/cadence/macb_main.c +3 -0
+	/* pow=0 for all queues. ilog2(0) is dangerous. */
+	if (num_queues >= num_segments)
+		return 0;

[ ... ]

drivers/net/ethernet/cadence/macb_main.c static void macb_context_swap_end(struct macb *bp, · line 3490

Quoted 7 lines
drivers/net/ethernet/cadence/macb_main.c +7 -0
+	for (i = 0; i < num_advantaged_queues; i++)
+		val |= field_prep(GEM_TXQSEGALLOC_MASK(i), pow + 1);
+	for (i = num_advantaged_queues; i < num_queues; i++)
+		val |= field_prep(GEM_TXQSEGALLOC_MASK(i), pow);
+
+	return val;
+}

[Severity: Medium]
Can the value built by gem_sram_distribute_segments() add up to more segments than the num_segments read from DCFG6?

A field value of 0 still means 2^0 = 1 segment. There seem to be two ways to over-allocate.

First, when num_queues > num_segments, the early return gives every active queue 1 segment. The comment above uses num_queues=4 and num_segments=2 as an example, which claims 4 segments out of 2.

Second, the loops only fill fields for i < num_queues. macb_init_hw() passes bp->ctx->num_queues, not the hardware queue count. The fields for hardware queues from ctx->num_queues up to bp->max_num_queues stay at 0. Each of those still claims 1 segment, on top of an active split that already uses every segment. With 4 hardware queues and 16 segments:

num_queues=2: 8 + 8 + 1 + 1 = 18
num_queues=3: 8 + 4 + 4 + 1 = 17

Quoted 7 lines

On Versal and EyeQ5 (MACB_CAPS_QUEUE_DISABLE), this happens after ethtool -L while the interface is down, followed by an open. The reset value is an even split over all hardware queues, so it adds up to the available segments.

If the GEM requires the total to stay within the DCFG6 count, could this give queues overlapping or out-of-range Tx SRAM regions?

The manual mentions explicitly that only active queues are taken into account. This means writing 0 in the field for a disabled queue won't allocate (1<<0) segments for it. Added a paragraph in the commit message.

As for the num_queues > num_segments case, there is nothing better to do than to allocate one segment per queue, which is what we already do. We won't add code to handle that unreachable edge case. If it happens, edit your verilog!

Quoted 44 lines
Quoted 8 lines
drivers/net/ethernet/cadence/macb_main.c +3 -0
@@ -3465,6 +3522,20 @@ static void macb_init_hw(struct macb *bp)
 	if (bp->caps & MACB_CAPS_JUMBO)
 		bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
 
+	/* Distribute Tx SRAM segments evenly based on active number of queues.
+	 * TXQSEGALLOC is only documented for some GEM integrations.
+	 */

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

Quoted 10 lines
drivers/net/ethernet/cadence/macb_main.c +10 -0
+	if (bp->caps & MACB_CAPS_TXQSEGALLOC) {
+		unsigned int num_segments;
+		u32 val;
+
+		num_segments = 1U << GEM_BFEXT(SEGMENTS_BIT_SIZE,
+					       gem_readl(bp, DCFG6));
+		val = gem_sram_distribute_segments(bp->ctx->num_queues,
+						   num_segments);
+		gem_writel(bp, TXQSEGALLOC_LOWER, val);
+	}

[Severity: High]
The commit message says the distribution needs care "if/when number of active queues is modified". Is that case covered when the queue count changes on a running interface?

Only macb_open() and the resume path call macb_init_hw(). Running ethtool -L ethX combined N while the interface is up goes through:

macb_set_channels()
macb_context_swap_start()
macb_context_swap_end(bp, new_ctx)
macb_init_buffers()
macb_configure_dma()

macb_context_swap_end() installs a context with the new num_queues and turns RE/TE back on. Nothing on this path rewrites GEM_TXQSEGALLOC_LOWER. The Tx SRAM split stays sized for the old queue count until the next down/up or suspend/resume.

Two examples:

Open with 1 queue. q0 gets 8 segments and the other fields stay at 0 (1 segment each). If the interface then grows to N queues while up, each new queue runs with a single segment while TXPBMS is set in DMACFG.

Quoted 8 lines

Going from 2 queues to 1 while up leaves half of the SRAM assigned to the disabled queue.

Versal and EyeQ5 set both MACB_CAPS_QUEUE_DISABLE and MACB_CAPS_TXQSEGALLOC, so this path is reachable there.

Could the write be moved into a helper and also called from macb_context_swap_end() under bp->lock, next to macb_configure_dma()?

Mentioned by Nicolai as well. The fix is to make our distribute function standalone and call it once from open and once from context swap end.

Thanks,

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