Skip to contentSkip to conversation

net: macb: implement context swapping

PATCH Superseded Théo Lebrun 4 participants 41 messages Jul 1 – Jul 15

Subsystems (3)

v3 of 11 Superseded Latest is v11 →

lore
Share & tools
lore

Théo Lebrun

author maintainer
#0 lore raw

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

The change is super intrusive so conflicts will be major. Sorry!

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]/

---
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]

---
Théo Lebrun (15):
net: macb: drop "consistent" from alloc/free function names
net: macb: unify device pointer naming convention
net: macb: unify variable naming convention in at91ether functions
net: macb: unify queue index variable naming convention and types
net: macb: enforce reverse christmas tree (RCT) convention
net: macb: allocate tieoff descriptor once across device lifetime
net: macb: 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: re-read ISR inside IRQ handler locked section
net: macb: use context swapping in .set_ringparam()
net: macb: use context swapping in .ndo_change_mtu()

 drivers/net/ethernet/cadence/macb.h      |  127 +-
 drivers/net/ethernet/cadence/macb_main.c | 1849 +++++++++++++++++-------------
 drivers/net/ethernet/cadence/macb_pci.c  |   46 +-
 drivers/net/ethernet/cadence/macb_ptp.c  |   26 +-
 4 files changed, 1199 insertions(+), 849 deletions(-)

---
base-commit: 64e6f7613459a9b344fb09a129584435b281088b
change-id: 20260401-macb-context-bd0caf20414d

Best regards,

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

PATCH v3 1/15 net: macb: drop "consistent" from alloc/free function names

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

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

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

Re: [PATCH net-next v3 01/15] net: macb: drop "consistent" from alloc/free function names

Quoted 64 lines · Théo Lebrun

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

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

---
 drivers/net/ethernet/cadence/macb_main.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/net/ethernet/cadence/macb_main.c +1 -1
index fd282a1700fb..d677a93b9d72 100644
@@ -2646,7 +2646,7 @@ static unsigned int 
 macb_rx_ring_size_per_queue(struct macb *bp)
 	return macb_dma_desc_get_size(bp) * bp->rx_ring_size + 
 bp->rx_bd_rd_prefetch;
 }
 
-static void macb_free_consistent(struct macb *bp)
+static void macb_free(struct macb *bp)
 {
drivers/net/ethernet/cadence/macb_main.c +4 -4
 	struct device *dev = &bp->pdev->dev;
 	struct macb_queue *queue;
@@ -2711,7 +2711,7 @@ static int macb_alloc_rx_buffers(struct macb *bp)
 	return 0;
 }
 
-static int macb_alloc_consistent(struct macb *bp)
+static int macb_alloc(struct macb *bp)
 {
 	struct device *dev = &bp->pdev->dev;
 	dma_addr_t tx_dma, rx_dma;
@@ -2769,7 +2769,7 @@ static int macb_alloc_consistent(struct macb *bp)
 	return 0;
 
 out_err:
-	macb_free_consistent(bp);
+	macb_free(bp);
 	return -ENOMEM;
 }
 
@@ -3157,7 +3157,7 @@ static int macb_open(struct net_device *dev)
 	/* RX buffers initialization */
 	macb_init_rx_buffer_size(bp, bufsz);
 
-	err = macb_alloc_consistent(bp);
+	err = macb_alloc(bp);
 	if (err) {
 		netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
 			   err);
@@ -3202,7 +3202,7 @@ static int macb_open(struct net_device *dev)
 		napi_disable(&queue->napi_rx);
 		napi_disable(&queue->napi_tx);
 	}
-	macb_free_consistent(bp);
+	macb_free(bp);
 pm_exit:
 	pm_runtime_put_sync(&bp->pdev->dev);
 	return err;

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

Quoted 8 lines
drivers/net/ethernet/cadence/macb_main.c +1 -1
@@ -3235,7 +3235,7 @@ static int macb_close(struct net_device *dev)
 	netif_carrier_off(dev);
 	spin_unlock_irqrestore(&bp->lock, flags);
 
-	macb_free_consistent(bp);
+	macb_free(bp);
 
 	if (bp->ptp_info)
 		bp->ptp_info->ptp_remove(dev);

Thanks
Nicolai

#3 lore raw

Re: [PATCH net-next v3 01/15] net: macb: drop "consistent" from alloc/free function names

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

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

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

What does "consistent" even mean? Is it intended to be analogous to coherent?

author maintainer
#4 lore raw

Re: [PATCH net-next v3 01/15] net: macb: drop "consistent" from alloc/free function names

Hello Conor,

Quoted 10 lines · Conor Dooley
Quoted 6 lines · Théo Lebrun

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

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

What does "consistent" even mean? Is it intended to be analogous to coherent?

That's my guess. We used to have pci_alloc_consistent(), being an alias to dma_alloc_coherent().

This alias has been present since forever (since Git):

git show 1da177e4c3f4:include/asm-generic/pci-dma-compat.h

The alias got removed in commit 7968778914e5 ("PCI: Remove the deprecated "pci-dma-compat.h" API", 2022-03-09).

Thanks,

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

Acked-by
#5 lore raw

Re: [PATCH net-next v3 01/15] net: macb: drop "consistent" from alloc/free function names

Quoted 17 lines · Théo Lebrun

Hello Conor,

Quoted 10 lines · Conor Dooley
Quoted 6 lines · Théo Lebrun

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

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

What does "consistent" even mean? Is it intended to be analogous to coherent?

That's my guess. We used to have pci_alloc_consistent(), being an alias to dma_alloc_coherent().

Quoted 6 lines

This alias has been present since forever (since Git):

git show 1da177e4c3f4:include/asm-generic/pci-dma-compat.h

The alias got removed in commit 7968778914e5 ("PCI: Remove the deprecated "pci-dma-compat.h" API", 2022-03-09).

Right, I did figure given the commit you cited as being the end of it that it was something like that.

author maintainer
#6 lore raw

PATCH v3 2/15 net: macb: unify device pointer naming convention

In reply to Théo Lebrun

Here are all device pointer variable permutations inside MACB:

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

Unify to this convention:

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

Ensure nothing slipped through using ctags tooling:

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

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

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

1,868 lines not on this page · raw message

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

109 lines not on this page · raw message

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

Re: [PATCH net-next v3 02/15] net: macb: unify device pointer naming convention

Quoted 12 lines · Théo Lebrun

Here are all device pointer variable permutations inside MACB:

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

Unify to this convention:

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

Oh lovely, OCD approved.

author maintainer
#8 lore raw

PATCH v3 3/15 net: macb: unify variable naming convention in at91ether functions

In reply to Théo Lebrun

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

The latter is to reserve q for queue indexes.

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

Re: [PATCH net-next v3 03/15] net: macb: unify variable naming convention in at91ether functions

Quoted 9 lines · Théo Lebrun

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

The latter is to reserve q for queue indexes.

[...]

Thanks
Nicolai

author maintainer
#11 lore raw

PATCH v3 4/15 net: macb: unify queue index variable naming convention and types

In reply to Théo Lebrun

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

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

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

Re: [PATCH net-next v3 04/15] net: macb: unify queue index variable naming convention and types

Quoted 147 lines · Théo Lebrun

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

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

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

spin_unlock_irqrestore(&queue->tx_ptr_lock, flags);

Thanks
Nicolai

#13 lore raw

Re: [PATCH net-next v3 04/15] net: macb: unify queue index variable naming convention and types

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

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

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

I'm not sure that I agree about the verbosity, and "q" isn't a letter I would naturally associate with indexing, in the way ijk etc are. Perhaps in netdev it is a natural choice however?

Conor.

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

Re: [PATCH net-next v3 04/15] net: macb: unify queue index variable naming convention and types

Hello Conor,

Quoted 11 lines · Conor Dooley
Quoted 6 lines · Théo Lebrun

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

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

I'm not sure that I agree about the verbosity, and "q" isn't a letter I would naturally associate with indexing, in the way ijk etc are. Perhaps in netdev it is a natural choice however?

The question wasn't much if q made sense in netdev land, more about what the MACB code used and to unify the naming convention.

Before After

struct macb_queue * q           1      0
int q                           1      0
u32 queue_index                 1      0
u16 queue_index                 2      0
unsigned int q                  4     12

struct macb_queue * queue 29 25

q that could be an index (5 times) or a struct macb_queue * (29 times) was the most annoying to me. As queue was often the pointer, I used that.

queue_id or queue_index for the stack felt too descriptive for no valid reason. If it was green-field code I'd name it qid but that name doesn't appear anywhere in MACB so I avoided introducing new jargon.

If you have a name proposal I will probably agree with you and go that route!

# command to extract counts
ctags -o - --kinds-c='{local}{member}{parameter}' \
      --fields='{typeref}' drivers/net/ethernet/cadence/* | \
awk -F"\t" '$1=="q" || $1 ~ /queue/ {print $NF, $1}' | \
sort | uniq -c | sort -n

Thanks,

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

author maintainer
#15 lore raw

PATCH v3 5/15 net: macb: enforce reverse christmas tree (RCT) convention

In reply to Théo Lebrun

Enforce the reverse christmas tree convention in those functions:

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

The goal is to minimise unrelated diff in future patches.

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

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

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

Quoted 163 lines · Théo Lebrun

Enforce the reverse christmas tree convention in those functions:

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

The goal is to minimise unrelated diff in future patches.

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

---
 drivers/net/ethernet/cadence/macb_main.c | 61 
++++++++++++++++----------------
 1 file changed, 30 insertions(+), 31 deletions(-)
drivers/net/ethernet/cadence/macb_main.c +12 -13
index 3b75797381b6..8b52122bc134 100644
@@ -1254,20 +1254,19 @@ static dma_addr_t macb_get_addr(struct macb 
 *bp, struct macb_dma_desc *desc)
 
 static void macb_tx_error_task(struct work_struct *work)
 {
-	struct macb_queue	*queue = container_of(work, struct macb_queue,
-						      tx_error_task);
-	bool			halt_timeout = false;
-	struct macb		*bp = queue->bp;
-	unsigned int		q;
-	u32			packets = 0;
-	u32			bytes = 0;
-	struct macb_tx_skb	*tx_skb;
-	struct macb_dma_desc	*desc;
-	struct sk_buff		*skb;
-	unsigned int		tail;
-	unsigned long		flags;
+	struct macb_queue *queue = container_of(work, struct macb_queue,
+						tx_error_task);
+	unsigned int q = queue - queue->bp->queues;
+	struct macb *bp = queue->bp;
+	struct macb_tx_skb *tx_skb;
+	struct macb_dma_desc *desc;
+	bool halt_timeout = false;
+	struct sk_buff *skb;
+	unsigned long flags;
+	unsigned int tail;
+	u32 packets = 0;
+	u32 bytes = 0;
 
-	q = queue - bp->queues;
 	netdev_vdbg(bp->netdev, "macb_tx_error_task: q = %u, t = %u, h = 
 %u\n",
drivers/net/ethernet/cadence/macb_main.c +3 -3
 		    q, queue->tx_tail, queue->tx_head);
@@ -1487,11 +1486,11 @@ static int macb_tx_complete(struct macb_queue 
 *queue, int budget)
 
 static void gem_rx_refill(struct macb_queue *queue)
 {
-	unsigned int		entry;
-	struct sk_buff		*skb;
-	dma_addr_t		paddr;
 	struct macb *bp = queue->bp;
 	struct macb_dma_desc *desc;
+	struct sk_buff *skb;
+	unsigned int entry;
+	dma_addr_t paddr;
 
 	while (CIRC_SPACE(queue->rx_prepared_head, queue->rx_tail,
drivers/net/ethernet/cadence/macb_main.c +5 -5
 			bp->rx_ring_size) > 0) {
@@ -1584,11 +1583,11 @@ static int gem_rx(struct macb_queue *queue, 
 struct napi_struct *napi,
 		  int budget)
 {
 	struct macb *bp = queue->bp;
-	unsigned int		len;
-	unsigned int		entry;
-	struct sk_buff		*skb;
-	struct macb_dma_desc	*desc;
-	int			count = 0;
+	struct macb_dma_desc *desc;
+	struct sk_buff *skb;
+	unsigned int entry;
+	unsigned int len;
+	int count = 0;
 
 	while (count < budget) {
drivers/net/ethernet/cadence/macb_main.c +4 -4
 		u32 ctrl;
@@ -1674,12 +1673,12 @@ static int gem_rx(struct macb_queue *queue, 
 struct napi_struct *napi,
 static int macb_rx_frame(struct macb_queue *queue, struct napi_struct 
 *napi,
 			 unsigned int first_frag, unsigned int last_frag)
 {
-	unsigned int len;
-	unsigned int frag;
+	struct macb *bp = queue->bp;
+	struct macb_dma_desc *desc;
 	unsigned int offset;
 	struct sk_buff *skb;
-	struct macb_dma_desc *desc;
-	struct macb *bp = queue->bp;
+	unsigned int frag;
+	unsigned int len;
 
drivers/net/ethernet/cadence/macb_main.c +1 -1
 	desc = macb_rx_desc(queue, last_frag);
 	len = desc->ctrl & bp->rx_frm_len_mask;
@@ -1755,9 +1754,9 @@ static int macb_rx_frame(struct macb_queue 
 *queue, struct napi_struct *napi,
 
 static inline void macb_init_rx_ring(struct macb_queue *queue)
 {
+	struct macb_dma_desc *desc = NULL;
 	struct macb *bp = queue->bp;
 	dma_addr_t addr;
-	struct macb_dma_desc *desc = NULL;
 	int i;
 
drivers/net/ethernet/cadence/macb_main.c +2 -2
 	addr = queue->rx_buffers_dma;
@@ -1776,9 +1775,9 @@ static int macb_rx(struct macb_queue *queue, 
 struct napi_struct *napi,
 {
 	struct macb *bp = queue->bp;
 	bool reset_rx_queue = false;
-	int received = 0;
-	unsigned int tail;
 	int first_frag = -1;
+	unsigned int tail;
+	int received = 0;
 
 	for (tail = queue->rx_tail; budget > 0; tail++) {
drivers/net/ethernet/cadence/macb_main.c +2 -2
 		struct macb_dma_desc *desc = macb_rx_desc(queue, tail);
@@ -1853,8 +1852,8 @@ static int macb_rx(struct macb_queue *queue, 
 struct napi_struct *napi,
 static bool macb_rx_pending(struct macb_queue *queue)
 {
 	struct macb *bp = queue->bp;
-	unsigned int		entry;
-	struct macb_dma_desc	*desc;
+	struct macb_dma_desc *desc;
+	unsigned int entry;
 
 	entry = macb_rx_ring_wrap(bp, queue->rx_tail);
drivers/net/ethernet/cadence/macb_main.c +1 -1
 	desc = macb_rx_desc(queue, entry);
@@ -2474,10 +2473,10 @@ static netdev_tx_t macb_start_xmit(struct 
 sk_buff *skb,
 	unsigned int q = skb_get_queue_mapping(skb);
 	unsigned int desc_cnt, nr_frags, frag_size, f;
 	struct macb_queue *queue = &bp->queues[q];
+	netdev_tx_t ret = NETDEV_TX_OK;
 	unsigned int hdrlen;
 	unsigned long flags;
 	bool is_lso;
-	netdev_tx_t ret = NETDEV_TX_OK;
 
 	if (macb_clear_csum(skb)) {
Quoted 1 line

dev_kfree_skb_any(skb);

Thanks
Nicolai

author maintainer
#18 lore raw

PATCH v3 6/15 net: macb: allocate tieoff descriptor once across device lifetime

In reply to Théo Lebrun

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

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

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

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

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

Re: [PATCH net-next v3 06/15] net: macb: allocate tieoff descriptor once across device lifetime

Hi Théo

Quoted 41 lines · Théo Lebrun

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

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

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

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

---
 drivers/net/ethernet/cadence/macb_main.c | 75 
+++++++++++++++++---------------
 1 file changed, 41 insertions(+), 34 deletions(-)
drivers/net/ethernet/cadence/macb_main.c +0 -2
index 8b52122bc134..951a7f080225 100644
 
[...]
 
 static void macb_init_rings(struct macb *bp)
@@ -2832,8 +2801,6 @@ static void macb_init_rings(struct macb *bp)
 	bp->queues[0].tx_head = 0;
 	bp->queues[0].tx_tail = 0;
 	desc->ctrl |= MACB_BIT(TX_WRAP);
-
-	macb_init_tieoff(bp);
 }
 
 static void macb_reset_hw(struct macb *bp)

drivers/net/ethernet/cadence/macb_main.c static int eyeq5_init(struct platform_device · line 5494

Quoted 10 lines
drivers/net/ethernet/cadence/macb_main.c +5 -0
@@ -5518,6 +5485,38 @@ static int eyeq5_init(struct platform_device 
 *pdev)
 	return ret;
 }
 
+static int macb_alloc_tieoff(struct macb *bp)
+{
+	/* Tieoff is a workaround in case HW cannot disable queues, for PM. 
 */
+	if (bp->caps & MACB_CAPS_QUEUE_DISABLE)
+		return 0;

Before, the tieoff was allocated in macb_alloc(), which the at91ether path never called. Now it's allocated from macb_probe() for all variants, gated only on MACB_CAPS_QUEUE_DISABLE, so EMAC gets a coherent descriptor it never uses.

Add MACB_CAPS_MACB_IS_EMAC to the if statement?

Quoted 1 line

[...]

Thanks
Nicolai

author maintainer
#20 lore raw

Re: [PATCH net-next v3 06/15] net: macb: allocate tieoff descriptor once across device lifetime

Quoted 61 lines · Nicolai Buchwitz
Quoted 42 lines · Théo Lebrun

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

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

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

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

---
 drivers/net/ethernet/cadence/macb_main.c | 75 
+++++++++++++++++---------------
 1 file changed, 41 insertions(+), 34 deletions(-)
drivers/net/ethernet/cadence/macb_main.c +0 -2
index 8b52122bc134..951a7f080225 100644
 
[...]
 
 static void macb_init_rings(struct macb *bp)
@@ -2832,8 +2801,6 @@ static void macb_init_rings(struct macb *bp)
 	bp->queues[0].tx_head = 0;
 	bp->queues[0].tx_tail = 0;
 	desc->ctrl |= MACB_BIT(TX_WRAP);
-
-	macb_init_tieoff(bp);
 }
 
 static void macb_reset_hw(struct macb *bp)

drivers/net/ethernet/cadence/macb_main.c static int eyeq5_init(struct platform_device · line 5494

Quoted 10 lines
drivers/net/ethernet/cadence/macb_main.c +5 -0
@@ -5518,6 +5485,38 @@ static int eyeq5_init(struct platform_device 
 *pdev)
 	return ret;
 }
 
+static int macb_alloc_tieoff(struct macb *bp)
+{
+	/* Tieoff is a workaround in case HW cannot disable queues, for PM. 
 */
+	if (bp->caps & MACB_CAPS_QUEUE_DISABLE)
+		return 0;

Before, the tieoff was allocated in macb_alloc(), which the at91ether path
never called. Now it's allocated from macb_probe() for all variants, gated only
on MACB_CAPS_QUEUE_DISABLE, so EMAC gets a coherent descriptor it never uses.

Quoted 1 line

Add MACB_CAPS_MACB_IS_EMAC to the if statement?

Clearly. That EMAC distinction keeps being annoying.

Thanks,

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

author maintainer
#21 lore raw

Re: [PATCH net-next v3 06/15] net: macb: allocate tieoff descriptor once across device lifetime

Quoted 62 lines · Théo Lebrun
Quoted 61 lines · Nicolai Buchwitz
Quoted 42 lines · Théo Lebrun

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

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

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

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

---
 drivers/net/ethernet/cadence/macb_main.c | 75 
+++++++++++++++++---------------
 1 file changed, 41 insertions(+), 34 deletions(-)
drivers/net/ethernet/cadence/macb_main.c +0 -2
index 8b52122bc134..951a7f080225 100644
 
[...]
 
 static void macb_init_rings(struct macb *bp)
@@ -2832,8 +2801,6 @@ static void macb_init_rings(struct macb *bp)
 	bp->queues[0].tx_head = 0;
 	bp->queues[0].tx_tail = 0;
 	desc->ctrl |= MACB_BIT(TX_WRAP);
-
-	macb_init_tieoff(bp);
 }
 
 static void macb_reset_hw(struct macb *bp)

drivers/net/ethernet/cadence/macb_main.c static int eyeq5_init(struct platform_device · line 5494

Quoted 10 lines
drivers/net/ethernet/cadence/macb_main.c +5 -0
@@ -5518,6 +5485,38 @@ static int eyeq5_init(struct platform_device 
 *pdev)
 	return ret;
 }
 
+static int macb_alloc_tieoff(struct macb *bp)
+{
+	/* Tieoff is a workaround in case HW cannot disable queues, for PM. 
 */
+	if (bp->caps & MACB_CAPS_QUEUE_DISABLE)
+		return 0;

Before, the tieoff was allocated in macb_alloc(), which the at91ether path
never called. Now it's allocated from macb_probe() for all variants, gated only
on MACB_CAPS_QUEUE_DISABLE, so EMAC gets a coherent descriptor it never uses.

Quoted 3 lines
Quoted 1 line

Add MACB_CAPS_MACB_IS_EMAC to the if statement?

Clearly. That EMAC distinction keeps being annoying.

Well well well. Under AT91 with WOL active there is nothing preventing the tieoff from being used in macb_suspend(). Meaning the tieoff is being used zero-initialised. Not advisable.

So I withdraw my previous comment: we won't shield macb_alloc_tieoff() from IS_EMAC and maybe it will fix a bug.

Thanks,

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

Reviewed-by
#22 lore raw

Re: [PATCH net-next v3 06/15] net: macb: allocate tieoff descriptor once across device lifetime

Hi Théo

Quoted 65 lines · Théo Lebrun
Quoted 64 lines · Théo Lebrun
Quoted 62 lines · Nicolai Buchwitz
Quoted 42 lines · Théo Lebrun

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

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

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

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

---
 drivers/net/ethernet/cadence/macb_main.c | 75 
+++++++++++++++++---------------
 1 file changed, 41 insertions(+), 34 deletions(-)
drivers/net/ethernet/cadence/macb_main.c +0 -2
index 8b52122bc134..951a7f080225 100644
 
[...]
 
 static void macb_init_rings(struct macb *bp)
@@ -2832,8 +2801,6 @@ static void macb_init_rings(struct macb *bp)
 	bp->queues[0].tx_head = 0;
 	bp->queues[0].tx_tail = 0;
 	desc->ctrl |= MACB_BIT(TX_WRAP);
-
-	macb_init_tieoff(bp);
 }
 
 static void macb_reset_hw(struct macb *bp)

drivers/net/ethernet/cadence/macb_main.c static int eyeq5_init(struct platform_device · line 5494

Quoted 10 lines
drivers/net/ethernet/cadence/macb_main.c +5 -0
@@ -5518,6 +5485,38 @@ static int eyeq5_init(struct platform_device 
 *pdev)
 	return ret;
 }
 
+static int macb_alloc_tieoff(struct macb *bp)
+{
+	/* Tieoff is a workaround in case HW cannot disable queues, for PM. 
 */
+	if (bp->caps & MACB_CAPS_QUEUE_DISABLE)
+		return 0;

Before, the tieoff was allocated in macb_alloc(), which the at91ether path
never called. Now it's allocated from macb_probe() for all variants, gated only
on MACB_CAPS_QUEUE_DISABLE, so EMAC gets a coherent descriptor it never uses.

Add MACB_CAPS_MACB_IS_EMAC to the if statement?

Quoted 8 lines
Quoted 1 line

Clearly. That EMAC distinction keeps being annoying.

Well well well. Under AT91 with WOL active there is nothing preventing the tieoff from being used in macb_suspend(). Meaning the tieoff is being used zero-initialised. Not advisable.

So I withdraw my previous comment: we won't shield macb_alloc_tieoff() from IS_EMAC and maybe it will fix a bug.

Sounds reasonable.

Thanks,
Nicolai

Quoted 8 lines

Thanks,

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

author maintainer
#23 lore raw

PATCH v3 7/15 net: macb: introduce macb_context struct for buffer management

In reply to Théo Lebrun

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

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

Steps:

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

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

- macb_init_dflt() / macb_get_ringparam() do not access bp->ctx->{rx,tx}_ring_size as they will/might run while interface is offline and ctx is not 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.

---
 drivers/net/ethernet/cadence/macb.h      |  49 +++-
 drivers/net/ethernet/cadence/macb_main.c | 468 +++++++++++++++++++------------
 2 files changed, 318 insertions(+), 199 deletions(-)
drivers/net/ethernet/cadence/macb.h +34 -15
index 9857df5b57f0..452b2c8f8641 100644
@@ -1272,21 +1272,10 @@ struct macb_queue {
 
 	/* Lock to protect tx_head and tx_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;
 };
@@ -1301,6 +1290,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;
@@ -1309,12 +1324,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 +284 -184

1,132 lines not on this page · raw message

Signature · 1 line
2.55.0
maintainer
#25 lore raw

Re: [PATCH net-next v3 07/15] net: macb: introduce macb_context struct for buffer management

In reply to Théo Lebrun

From: AI Reviewer <[email protected]>

This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible. Full review at: https://netdev-ai.bots.linux.dev/sashiko/
---
net: macb: introduce macb_context struct for buffer management

This patch moves per-queue ring buffers and ring-size parameters out of struct macb / struct macb_queue into a new struct macb_context that is allocated on open and freed on close, so future patches can swap in a new context while the interface is running.

Quoted 4 lines
drivers/net/ethernet/cadence/macb_main.c +0 -0
index 951a7f080225..dfb1d49d841c 100644

[ ... ]

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

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

[High]
Can macb_tx_error_task() dereference a freed context?

macb_tx_error_task is scheduled from macb_interrupt():

    schedule_work(&queue->tx_error_task);

After this patch, the very first thing the worker does is:

    struct macb_txq *txq = macb_txq(queue);

which expands to &bp->ctx->txq[q] and dereferences bp->ctx.

Looking at macb_close():

    macb_reset_hw(bp);
    ...
    macb_free(bp);
    kfree(bp->ctx);
    bp->ctx = NULL;

There is no cancel_work_sync(&queue->tx_error_task), and macb_reset_hw() does not drain the workqueue. If an interrupt scheduled the work just before close, or the work was already pending, the worker can run after kfree(bp->ctx) and hit a NULL dereference (and a use-after-free if macb_open() re-allocates ctx concurrently via ethtool ring reconfig).

The same reasoning applies to bp->hresp_err_bh_work, which is queued from macb_interrupt() and whose handler calls mog_init_rings()/macb_init_buffers() that read bp->ctx->tx_ring_size and bp->ctx->{tx,rx}q[q]. cancel_work_sync(&bp->hresp_err_bh_work) is only called from macb_remove(), not macb_close().

Pre-patch these fields lived inside the embedded struct macb_queue so a late worker would touch stale-but-addressable memory; post-patch the first indirection through bp->ctx faults.

Would adding cancel_work_sync(&queue->tx_error_task) per queue and cancel_work_sync(&bp->hresp_err_bh_work) before kfree(bp->ctx) in macb_close() close this window?

[ ... ]

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

Quoted 11 lines
drivers/net/ethernet/cadence/macb_main.c +2 -2
@@ -3690,8 +3778,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);

[Medium]
Should the return value of macb_open() be checked here?

This patch adds a new failure point inside macb_open():

    bp->ctx = kzalloc_obj(*bp->ctx);
    if (!bp->ctx) {
        err = -ENOMEM;
        goto pm_exit;
    }

On failure, bp->ctx remains NULL and the interface stays down, but macb_set_ringparam() ignores the return value and reports success to userspace. The commit message says the motivation is to improve reliability under memory pressure, but this caller silently swallows the new allocation failure.

[ ... ]

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

Quoted 13 lines
drivers/net/ethernet/cadence/macb_main.c +6 -0
@@ -5039,6 +5125,12 @@ static int at91ether_open(struct net_device *netdev)
 	if (ret < 0)
 		return ret;
 
+	bp->ctx = kzalloc_obj(*bp->ctx);
+	if (!bp->ctx) {
+		ret = -ENOMEM;
+		goto pm_exit;
+	}
+
 	/* Clear internal statistics */
 	ctl = macb_readl(bp, NCR);
 	macb_writel(bp, NCR, ctl | MACB_BIT(CLRSTAT));

[Low]
Should bp->ctx->rx_ring_size, tx_ring_size and rx_buffer_size be initialized on this path?

macb_open() sets them from configured_{rx,tx}_ring_size and calls macb_init_rx_buffer_size(), but at91ether_open() leaves them at zero after kzalloc_obj().

Helpers such as macb_rx_ring_wrap() compute:

    return index & (bp->ctx->rx_ring_size - 1);

which becomes index & 0xFFFFFFFF when rx_ring_size is 0. It happens to be harmless today because the at91ether path constrains indices to [0, AT91ETHER_MAX_RX_DESCR-1] independently and macb_get_regs() reports zeroed head/tail for the tx-less at91ether, but pre-patch macb_init_dflt() unconditionally set bp->rx_ring_size = DEFAULT_RX_RING_SIZE.

Any future shared helper that used tx_ring_size as a stride or divisor would misbehave on at91ether.

[ ... ]

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

Quoted 10 lines
drivers/net/ethernet/cadence/macb_main.c +3 -0
@@ -5076,6 +5171,9 @@ static int at91ether_close(struct net_device *netdev)
 
 	at91ether_stop(bp);
 
+	kfree(bp->ctx);
+	bp->ctx = NULL;
+
 	pm_runtime_put(&bp->pdev->dev);
 
 	return 0;

[High]
Is there IRQ synchronization missing before freeing bp->ctx on this path?

at91ether_stop() disables MAC RX/TX and writes IDR to mask further interrupts, but does not synchronize with a handler already running or already dispatched on another CPU. at91ether_interrupt() calls at91ether_rx(), which after this patch does:

    struct macb_rxq *rxq = &bp->ctx->rxq[0];
    ...
    desc = macb_rx_desc(queue, rxq->tail);
    ...
    p_recv = rxq->buffers + rxq->tail * AT91ETHER_MAX_RBUFF_SZ;

If the handler is in flight when kfree(bp->ctx); bp->ctx = NULL runs, the next access through bp->ctx faults. The macb_open()/macb_close() path is somewhat protected by napi_disable() before macb_free(), but at91ether does not use NAPI and has no equivalent barrier.

Would a synchronize_irq(bp->netdev->irq) between at91ether_stop() and kfree(bp->ctx) close this?

Signature · 1 line
This is an AI-generated review.
author maintainer
#26 lore raw

Re: [PATCH net-next v3 07/15] net: macb: introduce macb_context struct for buffer management

Hello Paolo's LLM,

Quoted 54 lines · Paolo Abeni

From: AI Reviewer <[email protected]>

This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible. Full review at: https://netdev-ai.bots.linux.dev/sashiko/
---
net: macb: introduce macb_context struct for buffer management

This patch moves per-queue ring buffers and ring-size parameters out of struct macb / struct macb_queue into a new struct macb_context that is allocated on open and freed on close, so future patches can swap in a new context while the interface is running.

Quoted 4 lines
drivers/net/ethernet/cadence/macb_main.c +0 -0
index 951a7f080225..dfb1d49d841c 100644

[ ... ]

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

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

[High]
Can macb_tx_error_task() dereference a freed context?

macb_tx_error_task is scheduled from macb_interrupt():

    schedule_work(&queue->tx_error_task);

After this patch, the very first thing the worker does is:

    struct macb_txq *txq = macb_txq(queue);

which expands to &bp->ctx->txq[q] and dereferences bp->ctx.

Looking at macb_close():

    macb_reset_hw(bp);
    ...
    macb_free(bp);
    kfree(bp->ctx);
    bp->ctx = NULL;

There is no cancel_work_sync(&queue->tx_error_task), and macb_reset_hw() does not drain the workqueue. If an interrupt scheduled the work just before close, or the work was already pending, the worker can run after kfree(bp->ctx) and hit a NULL dereference (and a use-after-free if macb_open() re-allocates ctx concurrently via ethtool ring reconfig).

Quoted 5 lines

The same reasoning applies to bp->hresp_err_bh_work, which is queued from macb_interrupt() and whose handler calls mog_init_rings()/macb_init_buffers() that read bp->ctx->tx_ring_size and bp->ctx->{tx,rx}q[q]. cancel_work_sync(&bp->hresp_err_bh_work) is only called from macb_remove(), not macb_close().

All of this is a valid concern.

Quoted 3 lines

Pre-patch these fields lived inside the embedded struct macb_queue so a late worker would touch stale-but-addressable memory; post-patch the first indirection through bp->ctx faults.

This pre-series analysis is wrong however. Current upstream would crash:
macb_tx_error_task() restarts the HW and it even assigns queue->tx_ring_dma which still contains the freed DMA address (macb_free()/macb_close() don't reset that value).

Summary: this patch turns potential memory corruption into a NULL pointer dereference.

Quoted 3 lines

Would adding cancel_work_sync(&queue->tx_error_task) per queue and cancel_work_sync(&bp->hresp_err_bh_work) before kfree(bp->ctx) in macb_close() close this window?

Yes that is the way forward. It will probably be separate and target net/main. Not specifically because I want it backported (it'd be nice but issue is super unlikely and no one reported it) but mostly because this series is already 15 patches long.

Quoted 23 lines

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

Quoted 11 lines
drivers/net/ethernet/cadence/macb_main.c +2 -2
@@ -3690,8 +3778,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);

[Medium]
Should the return value of macb_open() be checked here?

This patch adds a new failure point inside macb_open():

    bp->ctx = kzalloc_obj(*bp->ctx);
    if (!bp->ctx) {
        err = -ENOMEM;
        goto pm_exit;
    }
Quoted 5 lines

On failure, bp->ctx remains NULL and the interface stays down, but macb_set_ringparam() ignores the return value and reports success to userspace. The commit message says the motivation is to improve reliability under memory pressure, but this caller silently swallows the new allocation failure.

No need, this series ends with context swapping and the macb_context_swap_end() operation, which replaces the macb_open() highlighted here, cannot fail.

Quoted 26 lines

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

Quoted 13 lines
drivers/net/ethernet/cadence/macb_main.c +6 -0
@@ -5039,6 +5125,12 @@ static int at91ether_open(struct net_device *netdev)
 	if (ret < 0)
 		return ret;
 
+	bp->ctx = kzalloc_obj(*bp->ctx);
+	if (!bp->ctx) {
+		ret = -ENOMEM;
+		goto pm_exit;
+	}
+
 	/* Clear internal statistics */
 	ctl = macb_readl(bp, NCR);
 	macb_writel(bp, NCR, ctl | MACB_BIT(CLRSTAT));

[Low]
Should bp->ctx->rx_ring_size, tx_ring_size and rx_buffer_size be initialized on this path?

macb_open() sets them from configured_{rx,tx}_ring_size and calls macb_init_rx_buffer_size(), but at91ether_open() leaves them at zero after kzalloc_obj().

Helpers such as macb_rx_ring_wrap() compute:

    return index & (bp->ctx->rx_ring_size - 1);
Quoted 8 lines

which becomes index & 0xFFFFFFFF when rx_ring_size is 0. It happens to be harmless today because the at91ether path constrains indices to [0, AT91ETHER_MAX_RX_DESCR-1] independently and macb_get_regs() reports zeroed head/tail for the tx-less at91ether, but pre-patch macb_init_dflt() unconditionally set bp->rx_ring_size = DEFAULT_RX_RING_SIZE.

Any future shared helper that used tx_ring_size as a stride or divisor would misbehave on at91ether.

Yes. Null impact (but it would be better in theory).

Quoted 26 lines

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

Quoted 10 lines
drivers/net/ethernet/cadence/macb_main.c +3 -0
@@ -5076,6 +5171,9 @@ static int at91ether_close(struct net_device *netdev)
 
 	at91ether_stop(bp);
 
+	kfree(bp->ctx);
+	bp->ctx = NULL;
+
 	pm_runtime_put(&bp->pdev->dev);
 
 	return 0;

[High]
Is there IRQ synchronization missing before freeing bp->ctx on this path?

at91ether_stop() disables MAC RX/TX and writes IDR to mask further interrupts, but does not synchronize with a handler already running or already dispatched on another CPU. at91ether_interrupt() calls at91ether_rx(), which after this patch does:

    struct macb_rxq *rxq = &bp->ctx->rxq[0];
    ...
    desc = macb_rx_desc(queue, rxq->tail);
    ...
    p_recv = rxq->buffers + rxq->tail * AT91ETHER_MAX_RBUFF_SZ;
Quoted 7 lines

If the handler is in flight when kfree(bp->ctx); bp->ctx = NULL runs, the next access through bp->ctx faults. The macb_open()/macb_close() path is somewhat protected by napi_disable() before macb_free(), but at91ether does not use NAPI and has no equivalent barrier.

Would a synchronize_irq(bp->netdev->irq) between at91ether_stop() and kfree(bp->ctx) close this?

Ah at91ether_interrupt() doesn't grab bp->lock as macb_interrupt() does. Indeed synchronize_irq() would solve this.

Thanks,

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

author maintainer
#27 lore raw

PATCH v3 8/15 net: macb: avoid macb_init_rx_buffer_size() modifying state

In reply to Théo Lebrun

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

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

---
 drivers/net/ethernet/cadence/macb_main.c | 26 +++++++++++++-------------
 1 file changed, 13 insertions(+), 13 deletions(-)
drivers/net/ethernet/cadence/macb_main.c +13 -13
index dfb1d49d841c..13a1813fae6b 100644
@@ -2615,25 +2615,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)
@@ -3177,7 +3178,6 @@ static void macb_set_rx_mode(struct net_device *netdev)
 
 static int macb_open(struct net_device *netdev)
 {
-	size_t bufsz = netdev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
 	struct macb *bp = netdev_priv(netdev);
 	struct macb_queue *queue;
 	unsigned int q;
@@ -3196,7 +3196,7 @@ static int macb_open(struct net_device *netdev)
 	}
 
 	/* RX buffers initialization */
-	macb_init_rx_buffer_size(bp, bufsz);
+	bp->ctx->rx_buffer_size = macb_rx_buffer_size(bp, netdev->mtu);
 	bp->ctx->rx_ring_size = bp->configured_rx_ring_size;
 	bp->ctx->tx_ring_size = bp->configured_tx_ring_size;
 
Signature · 1 line
2.55.0
author maintainer
#28 lore raw

PATCH v3 9/15 net: macb: make `struct macb` subset reachable from macb_context struct

In reply to Théo Lebrun

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

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

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

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

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

---
 drivers/net/ethernet/cadence/macb.h      | 30 +++++++++++++++++++++---------
 drivers/net/ethernet/cadence/macb_main.c |  2 ++
 2 files changed, 23 insertions(+), 9 deletions(-)
drivers/net/ethernet/cadence/macb.h +21 -9
index 452b2c8f8641..5ce1b1045e6a 100644
@@ -1290,6 +1290,16 @@ 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 macb_rxq {
 	struct macb_dma_desc	*ring;		/* MACB & GEM */
 	dma_addr_t		ring_dma;	/* MACB & GEM */
@@ -1309,6 +1319,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;
@@ -1324,6 +1336,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.
@@ -1335,17 +1356,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 {
@@ -1353,15 +1371,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;
@@ -1404,9 +1419,6 @@ struct macb {
 	struct delayed_work	tx_lpi_work;
 	u32			tx_lpi_timer;
 
-	int	rx_bd_rd_prefetch;
-	int	tx_bd_rd_prefetch;
-
 	u32	rx_intr_mask;
 
 	struct macb_pm_data pm_data;
drivers/net/ethernet/cadence/macb_main.c +2 -0
index 13a1813fae6b..8933fc1d90ba 100644
@@ -3195,6 +3195,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;
Signature · 1 line
2.55.0
author maintainer
#29 lore raw

PATCH v3 10/15 net: macb: change caps helpers signatures

In reply to Théo Lebrun

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

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

Function list:

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

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 | 138 ++++++++++++++++---------------
 drivers/net/ethernet/cadence/macb_ptp.c  |   8 +-
 3 files changed, 86 insertions(+), 81 deletions(-)
drivers/net/ethernet/cadence/macb.h +11 -10
index 5ce1b1045e6a..165763117a8c 100644
@@ -840,7 +840,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); \
@@ -849,7 +849,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); \
@@ -1470,14 +1470,15 @@ static inline void gem_ptp_do_txstamp(struct macb *bp, struct sk_buff *skb, stru
 static inline void gem_ptp_do_rxstamp(struct macb *bp, struct sk_buff *skb, struct macb_dma_desc *desc) { }
 #endif
 
-static inline bool macb_is_gem(struct macb *bp)
+static inline bool macb_is_gem(const struct macb_info *info)
 {
-	return !!(bp->caps & MACB_CAPS_MACB_IS_GEM);
+	return !!(info->caps & MACB_CAPS_MACB_IS_GEM);
 }
 
-static inline bool gem_has_ptp(struct macb *bp)
+static inline bool gem_has_ptp(const struct macb_info *info)
 {
-	return IS_ENABLED(CONFIG_MACB_USE_HWSTAMP) && (bp->caps & MACB_CAPS_GEM_HAS_PTP);
+	return IS_ENABLED(CONFIG_MACB_USE_HWSTAMP) &&
+	       (info->caps & MACB_CAPS_GEM_HAS_PTP);
 }
 
 /* ENST Helper functions */
@@ -1493,16 +1494,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 +71 -67

494 lines not on this page · raw message

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

PATCH v3 11/15 net: macb: change function signatures to take contexts

In reply to Théo Lebrun

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

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

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

Full list:

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

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

---
 drivers/net/ethernet/cadence/macb.h      |   7 +-
 drivers/net/ethernet/cadence/macb_main.c | 371 ++++++++++++++++---------------
 2 files changed, 201 insertions(+), 177 deletions(-)
drivers/net/ethernet/cadence/macb.h +4 -3
index 165763117a8c..db0d0270c88c 100644
@@ -1196,11 +1196,12 @@ static const struct gem_statistic queue_statistics[] = {
 
 struct macb;
 struct macb_queue;
+struct macb_context;
 
 struct macb_or_gem_ops {
-	int	(*mog_alloc_rx_buffers)(struct macb *bp);
-	void	(*mog_free_rx_buffers)(struct macb *bp);
-	void	(*mog_init_rings)(struct macb *bp);
+	int	(*mog_alloc_rx_buffers)(struct macb_context *ctx);
+	void	(*mog_free_rx_buffers)(struct macb_context *ctx);
+	void	(*mog_init_rings)(struct macb_context *ctx);
 	int	(*mog_rx)(struct macb_queue *queue, struct napi_struct *napi,
 			  int budget);
 };
drivers/net/ethernet/cadence/macb_main.c +197 -174

948 lines not on this page · raw message

Signature · 1 line
2.55.0
author maintainer
#33 lore raw

PATCH v3 12/15 net: macb: introduce macb_context_alloc() helper

In reply to Théo Lebrun

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

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

PATCH v3 13/15 net: macb: re-read ISR inside IRQ handler locked section

In reply to Théo Lebrun

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

If we tried grabbing bp->lock while already acquired, we might have slept and the status might have been updated. Our most likely competitor in this race (condition) is a swap operation, used in change_mtu and set_ringparam. It is the only MACB codepath that resets interrupts and HW inside a bp->lock critical section. Other codepaths that clear HW IRQ status do so outside the bp->lock critical section.

We can only detect spurious interrupts before grabbing bp->lock if MACB_CAPS_ISR_CLEAR_ON_WRITE. If we don't, then we only read ISR once.

---
 drivers/net/ethernet/cadence/macb_main.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
drivers/net/ethernet/cadence/macb_main.c +12 -4
index 7245c345c78f..5a32d5cb759e 100644
@@ -2184,13 +2184,21 @@ static irqreturn_t macb_interrupt(int irq, void *dev_id)
 	struct net_device *netdev = bp->netdev;
 	u32 status;
 
-	status = queue_readl(queue, ISR);
-
-	if (unlikely(!status))
-		return IRQ_NONE;
+	/* detect spurious interrupts without grabbing bp->lock */
+	if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE) {
+		status = queue_readl(queue, ISR);
+		if (unlikely(!status))
+			return IRQ_NONE;
+	}
 
 	spin_lock(&bp->lock);
 
+	status = queue_readl(queue, ISR);
+	if (unlikely(!status)) {
+		spin_unlock(&bp->lock);
+		return IRQ_NONE;
+	}
+
 	while (status) {
 		/* close possible race with dev_close */
 		if (unlikely(!netif_running(netdev))) {
Signature · 1 line
2.55.0
#35 lore raw

Re: [PATCH net-next v3 13/15] net: macb: re-read ISR inside IRQ handler locked section

Hey,

I'll admit to being a bit confused by this patch..

Quoted 6 lines · Théo Lebrun

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

If we tried grabbing bp->lock while already acquired, we might have slept and the status might have been updated. Our most likely

This mention of sleeping I think should be removed, it implies sleeping is required for the status to be updated.

Quoted 4 lines

competitor in this race (condition) is a swap operation, used in change_mtu and set_ringparam. It is the only MACB codepath that resets interrupts and HW inside a bp->lock critical section. Other codepaths that clear HW IRQ status do so outside the bp->lock critical section.

Where do change_mtu and set_ringparam take the bp lock?
As far as I can see, they don't. The commit message should reflect how the code behaves at the time of the patch, not at some point in the future after it.

Quoted 16 lines

We can only detect spurious interrupts before grabbing bp->lock if MACB_CAPS_ISR_CLEAR_ON_WRITE. If we don't, then we only read ISR once.

---
 drivers/net/ethernet/cadence/macb_main.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
drivers/net/ethernet/cadence/macb_main.c +0 -0
index 7245c345c78f..5a32d5cb759e 100644
@@ -2184,13 +2184,21 @@ static irqreturn_t macb_interrupt(int irq, void *dev_id)
 	struct net_device *netdev = bp->netdev;
 	u32 status;
 

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

Quoted 10 lines
drivers/net/ethernet/cadence/macb_main.c +6 -4
-	status = queue_readl(queue, ISR);
-
-	if (unlikely(!status))
-		return IRQ_NONE;
+	/* detect spurious interrupts without grabbing bp->lock */
+	if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE) {
+		status = queue_readl(queue, ISR);
+		if (unlikely(!status))
+			return IRQ_NONE;
+	}

To be honest, this check feels like penalising the likely^2 case* with an extra read favour of the unlikely case where there's contention on the lock and the contending function is capable of affecting the status register. Could we get away with just the single check of the register with the lock taken?

* although I don't know what percentage of hardware supports this cap, so maybe most devices will never run this code

Quoted 17 lines
drivers/net/ethernet/cadence/macb_main.c +6 -0
 	spin_lock(&bp->lock);
 
+	status = queue_readl(queue, ISR);
+	if (unlikely(!status)) {
+		spin_unlock(&bp->lock);
+		return IRQ_NONE;
+	}
+
 	while (status) {
 		/* close possible race with dev_close */
 		if (unlikely(!netif_running(netdev))) {
 
Signature · 1 line
2.55.0
author maintainer
#36 lore raw

Re: [PATCH net-next v3 13/15] net: macb: re-read ISR inside IRQ handler locked section

Hello Conor,

Quoted 9 lines · Conor Dooley

Hey,

I'll admit to being a bit confused by this patch..

Quoted 4 lines · Théo Lebrun

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

Quoted 5 lines
Quoted 2 lines

If we tried grabbing bp->lock while already acquired, we might have slept and the status might have been updated. Our most likely

This mention of sleeping I think should be removed, it implies sleeping is required for the status to be updated.

Yes "slept" wasn't the right word, "waiting" would have been more appropriate. Or it could be a race even if bp->lock acquired immediately.

Quoted 9 lines
Quoted 4 lines

competitor in this race (condition) is a swap operation, used in change_mtu and set_ringparam. It is the only MACB codepath that resets interrupts and HW inside a bp->lock critical section. Other codepaths that clear HW IRQ status do so outside the bp->lock critical section.

Where do change_mtu and set_ringparam take the bp lock?
As far as I can see, they don't. The commit message should reflect how the code behaves at the time of the patch, not at some point in the future after it.

You are correct: this commit doesn't see change_mtu/set_ringparam take bp->lock. It only will happen in the future. Will fix.

Quoted 17 lines
Quoted 17 lines

We can only detect spurious interrupts before grabbing bp->lock if MACB_CAPS_ISR_CLEAR_ON_WRITE. If we don't, then we only read ISR once.

---
 drivers/net/ethernet/cadence/macb_main.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
drivers/net/ethernet/cadence/macb_main.c +0 -0
index 7245c345c78f..5a32d5cb759e 100644
@@ -2184,13 +2184,21 @@ static irqreturn_t macb_interrupt(int irq, void *dev_id)
 	struct net_device *netdev = bp->netdev;
 	u32 status;
 
Quoted 16 lines

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

Quoted 10 lines
drivers/net/ethernet/cadence/macb_main.c +6 -4
-	status = queue_readl(queue, ISR);
-
-	if (unlikely(!status))
-		return IRQ_NONE;
+	/* detect spurious interrupts without grabbing bp->lock */
+	if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE) {
+		status = queue_readl(queue, ISR);
+		if (unlikely(!status))
+			return IRQ_NONE;
+	}

To be honest, this check feels like penalising the likely^2 case* with an extra read favour of the unlikely case where there's contention on the lock and the contending function is capable of affecting the status register. Could we get away with just the single check of the register with the lock taken?

I agree!

Quoted 2 lines

* although I don't know what percentage of hardware supports this cap, so maybe most devices will never run this code

I expect all modern(TM) MACB blocks to be write-to-clear, at least that's what I heard but I don't remember from where.

Thanks,

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

author maintainer
#37 lore raw

PATCH v3 14/15 net: macb: use context swapping in .set_ringparam()

In reply to Théo Lebrun

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

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

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

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

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

Introduce a new locking primitive (mac_cfg_lock mutex) to serialise swap with phylink MAC callbacks. Avoid stopping phylink to avoid a slow PHY retrain. Those callbacks grab phydev->lock if it exists so we could imagine grabbing that from the swap op, but phydev->lock doesn't exist in the SFP case.

AT91 EMAC is handled differently as their buffer management is separate and they don't do NAPI. We refuse them (-EBUSY) to avoid implementing context swapping for them.

---
 drivers/net/ethernet/cadence/macb.h      |   2 +
 drivers/net/ethernet/cadence/macb_main.c | 142 +++++++++++++++++++++++++++++--
 2 files changed, 137 insertions(+), 7 deletions(-)
drivers/net/ethernet/cadence/macb.h +2 -0
index db0d0270c88c..630fe92608bc 100644
@@ -1360,6 +1360,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;
drivers/net/ethernet/cadence/macb_main.c +135 -7

232 lines not on this page · raw message

Signature · 1 line
2.55.0
#38 lore raw

Re: [PATCH net-next v3 14/15] net: macb: use context swapping in .set_ringparam()

Hi Théo

Quoted 62 lines · Théo Lebrun

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

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

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

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

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

Introduce a new locking primitive (mac_cfg_lock mutex) to serialise swap with phylink MAC callbacks. Avoid stopping phylink to avoid a slow PHY retrain. Those callbacks grab phydev->lock if it exists so we could imagine grabbing that from the swap op, but phydev->lock doesn't exist in the SFP case.

AT91 EMAC is handled differently as their buffer management is separate and they don't do NAPI. We refuse them (-EBUSY) to avoid implementing context swapping for them.

---
 drivers/net/ethernet/cadence/macb.h      |   2 +
 drivers/net/ethernet/cadence/macb_main.c | 142 

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

[...]

drivers/net/ethernet/cadence/macb_main.c +19 -0
+static void macb_context_swap_start(struct macb *bp)
+{
+	struct macb_queue *queue;
+	unsigned long flags;
+	unsigned int q;
+	u32 ctrl;
+
+	mutex_lock(&bp->mac_cfg_lock);
+
+	/* Mask interrupts before disabling BH features. */
+	spin_lock_irqsave(&bp->lock, flags);
+	for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
+		queue_writel(queue, IDR, -1);
+		queue_readl(queue, ISR);
+		macb_queue_isr_clear(bp, queue, -1);
+	}
+	spin_unlock_irqrestore(&bp->lock, flags);
+
+	/* Drain BH features. HW is still active and usable at this point. */

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

Quoted 10 lines
drivers/net/ethernet/cadence/macb_main.c +10 -0
+
+	cancel_work_sync(&bp->hresp_err_bh_work);
+	cancel_delayed_work_sync(&bp->tx_lpi_work);
+
+	for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
+		napi_disable(&queue->napi_rx);
+		napi_disable(&queue->napi_tx);
+		cancel_work_sync(&queue->tx_error_task);
+		netdev_tx_reset_queue(netdev_get_tx_queue(bp->netdev, q));
+	}

Can this deadlock against a pending tx_error_task?

AFAIU macb_tx_error_task() does napi_disable(&queue->napi_tx) and later napi_enable() on the same napi, and it can already be queued (macb_interrupt() schedules it on a TX error) by the time the swap runs:

swap_start: napi_disable(napi_tx) /* sets SCHED, returns */
worker: tx_error_task: napi_disable(napi_tx) /* spins on SCHED */
swap_start: cancel_work_sync(tx_error_task) /* waits on worker
*/

napi_disable() spins until napi_enable() clears SCHED, but here the swap won't re-enable until macb_context_swap_end(), and cancel_work_sync() is what's holding it up. Nothing clears it.

Maybe cancel_work_sync() before the napi_disable() calls would work instead? IRQs are masked just above, so AFAICT nothing can reschedule tx_error_task by then.

(Same path via .ndo_change_mtu() in the next patch.)

Quoted 1 line

[...]

Thanks
Nicolai

author maintainer
#39 lore raw

Re: [PATCH net-next v3 14/15] net: macb: use context swapping in .set_ringparam()

Quoted 94 lines · Nicolai Buchwitz
Quoted 64 lines · Théo Lebrun

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

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

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

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

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

Introduce a new locking primitive (mac_cfg_lock mutex) to serialise swap
with phylink MAC callbacks. Avoid stopping phylink to avoid a slow PHY retrain. Those callbacks grab phydev->lock if it exists so we could imagine grabbing that from the swap op, but phydev->lock doesn't exist in the SFP case.

AT91 EMAC is handled differently as their buffer management is separate and they don't do NAPI. We refuse them (-EBUSY) to avoid implementing context swapping for them.

---
 drivers/net/ethernet/cadence/macb.h      |   2 +
 drivers/net/ethernet/cadence/macb_main.c | 142 

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

[...]

drivers/net/ethernet/cadence/macb_main.c +19 -0
+static void macb_context_swap_start(struct macb *bp)
+{
+	struct macb_queue *queue;
+	unsigned long flags;
+	unsigned int q;
+	u32 ctrl;
+
+	mutex_lock(&bp->mac_cfg_lock);
+
+	/* Mask interrupts before disabling BH features. */
+	spin_lock_irqsave(&bp->lock, flags);
+	for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
+		queue_writel(queue, IDR, -1);
+		queue_readl(queue, ISR);
+		macb_queue_isr_clear(bp, queue, -1);
+	}
+	spin_unlock_irqrestore(&bp->lock, flags);
+
+	/* Drain BH features. HW is still active and usable at this point. */

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

Quoted 10 lines
drivers/net/ethernet/cadence/macb_main.c +10 -0
+
+	cancel_work_sync(&bp->hresp_err_bh_work);
+	cancel_delayed_work_sync(&bp->tx_lpi_work);
+
+	for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
+		napi_disable(&queue->napi_rx);
+		napi_disable(&queue->napi_tx);
+		cancel_work_sync(&queue->tx_error_task);
+		netdev_tx_reset_queue(netdev_get_tx_queue(bp->netdev, q));
+	}

Can this deadlock against a pending tx_error_task?

AFAIU macb_tx_error_task() does napi_disable(&queue->napi_tx) and later napi_enable() on the same napi, and it can already be queued (macb_interrupt()
schedules it on a TX error) by the time the swap runs:

swap_start: napi_disable(napi_tx) /* sets SCHED, returns */
worker: tx_error_task: napi_disable(napi_tx) /* spins on SCHED */
swap_start: cancel_work_sync(tx_error_task) /* waits on worker
*/

napi_disable() spins until napi_enable() clears SCHED, but here the swap won't
re-enable until macb_context_swap_end(), and cancel_work_sync() is what's
holding it up. Nothing clears it.

Quoted 4 lines

Maybe cancel_work_sync() before the napi_disable() calls would work instead? IRQs
are masked just above, so AFAICT nothing can reschedule tx_error_task by then.

Ah yes, good catch. macb_tx_error_task() should never enter if NAPI is disabled, else its napi_disable() will hang.

The fix sounds easy enough, as you indicated. We'll move the cancel_work_sync(queue->tx_error_task) call to be above our swap start napi_disable().

Thanks,

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

author maintainer
#40 lore raw

PATCH v3 15/15 net: macb: use context swapping in .ndo_change_mtu()

In reply to Théo Lebrun

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

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

AT91 EMAC is handled differently as their buffer management is separate and they don't do NAPI. We refuse them (-EBUSY) to avoid implementing context swapping for them.

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