diff mbox series

[net] net: ethernet: cortina: Locking fixes

Message ID 20240509-gemini-ethernet-locking-v1-1-afd00a528b95@linaro.org (mailing list archive)
State Accepted
Commit 812552808f7ff71133fc59768cdc253c5b8ca1bf
Delegated to: Netdev Maintainers
Headers show
Series [net] net: ethernet: cortina: Locking fixes | expand

Checks

Context Check Description
netdev/series_format success Single patches do not need cover letters
netdev/tree_selection success Clearly marked for net
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag present in non-next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 926 this patch: 926
netdev/build_tools success No tools touched, skip
netdev/cc_maintainers warning 1 maintainers not CCed: linux-arm-kernel@lists.infradead.org
netdev/build_clang success Errors and warnings before: 937 this patch: 937
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 937 this patch: 937
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 51 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 3 this patch: 3
netdev/source_inline success Was 0 now: 0
netdev/contest success net-next-2024-05-10--18-00 (tests: 1014)

Commit Message

Linus Walleij May 9, 2024, 7:44 a.m. UTC
This fixes a probably long standing problem in the Cortina
Gemini ethernet driver: there are some paths in the code
where the IRQ registers are written without taking the proper
locks.

Fixes: 4d5ae32f5e1e ("net: ethernet: Add a driver for Gemini gigabit ethernet")
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
---
 drivers/net/ethernet/cortina/gemini.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)


---
base-commit: 4cece764965020c22cff7665b18a012006359095
change-id: 20240509-gemini-ethernet-locking-7c1cb004ed49

Best regards,

Comments

Simon Horman May 10, 2024, 1:38 p.m. UTC | #1
On Thu, May 09, 2024 at 09:44:54AM +0200, Linus Walleij wrote:
> This fixes a probably long standing problem in the Cortina
> Gemini ethernet driver: there are some paths in the code
> where the IRQ registers are written without taking the proper
> locks.
> 
> Fixes: 4d5ae32f5e1e ("net: ethernet: Add a driver for Gemini gigabit ethernet")
> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>

Reviewed-by: Simon Horman <horms@kernel.org>
Simon Horman May 10, 2024, 1:46 p.m. UTC | #2
On Thu, May 09, 2024 at 09:44:54AM +0200, Linus Walleij wrote:
> This fixes a probably long standing problem in the Cortina
> Gemini ethernet driver: there are some paths in the code
> where the IRQ registers are written without taking the proper
> locks.
> 
> Fixes: 4d5ae32f5e1e ("net: ethernet: Add a driver for Gemini gigabit ethernet")
> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>

Reviewed-by: Simon Horman <horms@kernel.org>
patchwork-bot+netdevbpf@kernel.org May 11, 2024, 2:30 a.m. UTC | #3
Hello:

This patch was applied to netdev/net.git (main)
by Jakub Kicinski <kuba@kernel.org>:

On Thu, 09 May 2024 09:44:54 +0200 you wrote:
> This fixes a probably long standing problem in the Cortina
> Gemini ethernet driver: there are some paths in the code
> where the IRQ registers are written without taking the proper
> locks.
> 
> Fixes: 4d5ae32f5e1e ("net: ethernet: Add a driver for Gemini gigabit ethernet")
> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
> 
> [...]

Here is the summary with links:
  - [net] net: ethernet: cortina: Locking fixes
    https://git.kernel.org/netdev/net/c/812552808f7f

You are awesome, thank you!
Michał Mirosław May 11, 2024, 9:11 p.m. UTC | #4
On Thu, May 09, 2024 at 09:44:54AM +0200, Linus Walleij wrote:
> This fixes a probably long standing problem in the Cortina
> Gemini ethernet driver: there are some paths in the code
> where the IRQ registers are written without taking the proper
> locks.
> 
> Fixes: 4d5ae32f5e1e ("net: ethernet: Add a driver for Gemini gigabit ethernet")
> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
> ---
>  drivers/net/ethernet/cortina/gemini.c | 12 ++++++++++--
>  1 file changed, 10 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/net/ethernet/cortina/gemini.c b/drivers/net/ethernet/cortina/gemini.c
> index 705c3eb19cd3..d1fbadbf86d4 100644
> --- a/drivers/net/ethernet/cortina/gemini.c
> +++ b/drivers/net/ethernet/cortina/gemini.c
> @@ -1107,10 +1107,13 @@ static void gmac_tx_irq_enable(struct net_device *netdev,
>  {
>  	struct gemini_ethernet_port *port = netdev_priv(netdev);
>  	struct gemini_ethernet *geth = port->geth;
> +	unsigned long flags;
>  	u32 val, mask;
>  
>  	netdev_dbg(netdev, "%s device %d\n", __func__, netdev->dev_id);
>  
> +	spin_lock_irqsave(&geth->irq_lock, flags);
> +
>  	mask = GMAC0_IRQ0_TXQ0_INTS << (6 * netdev->dev_id + txq);
>  
>  	if (en)
> @@ -1119,6 +1122,8 @@ static void gmac_tx_irq_enable(struct net_device *netdev,
>  	val = readl(geth->base + GLOBAL_INTERRUPT_ENABLE_0_REG);
>  	val = en ? val | mask : val & ~mask;
>  	writel(val, geth->base + GLOBAL_INTERRUPT_ENABLE_0_REG);
> +
> +	spin_unlock_irqrestore(&geth->irq_lock, flags);
>  }
>  

Looks good, though spinlock looks necessary only around the ENABLE0
rmw, as the 'if (en)' part is resetting the "triggered" flag of the
interrupt (acking earlier-ignored interrupts).

>  static void gmac_tx_irq(struct net_device *netdev, unsigned int txq_num)
> @@ -1415,15 +1420,19 @@ static unsigned int gmac_rx(struct net_device *netdev, unsigned int budget)
>  	union gmac_rxdesc_3 word3;
>  	struct page *page = NULL;
>  	unsigned int page_offs;
> +	unsigned long flags;
>  	unsigned short r, w;
>  	union dma_rwptr rw;
>  	dma_addr_t mapping;
>  	int frag_nr = 0;
>  
> +	spin_lock_irqsave(&geth->irq_lock, flags);
>  	rw.bits32 = readl(ptr_reg);
>  	/* Reset interrupt as all packages until here are taken into account */
>  	writel(DEFAULT_Q0_INT_BIT << netdev->dev_id,
>  	       geth->base + GLOBAL_INTERRUPT_STATUS_1_REG);
> +	spin_unlock_irqrestore(&geth->irq_lock, flags);
> +

This doesn't look right: one, those are different registers, two it is
an IRQ-acking write.  In this case it is important that readl() is ordered
before writel(), but the spinlock doesn't guarantee that.

> @@ -1726,10 +1735,9 @@ static irqreturn_t gmac_irq(int irq, void *data)
>  		gmac_update_hw_stats(netdev);
>  
>  	if (val & (GMAC0_RX_OVERRUN_INT_BIT << (netdev->dev_id * 8))) {
> +		spin_lock(&geth->irq_lock);
>  		writel(GMAC0_RXDERR_INT_BIT << (netdev->dev_id * 8),
>  		       geth->base + GLOBAL_INTERRUPT_STATUS_4_REG);
> -
> -		spin_lock(&geth->irq_lock);
>  		u64_stats_update_begin(&port->ir_stats_syncp);
>  		++port->stats.rx_fifo_errors;
>  		u64_stats_update_end(&port->ir_stats_syncp);

This, too, is a IRQ-acking write that doesn't seem to gain much by
running inside the spin-locked section.

Best Regards
Michał Mirosław
diff mbox series

Patch

diff --git a/drivers/net/ethernet/cortina/gemini.c b/drivers/net/ethernet/cortina/gemini.c
index 705c3eb19cd3..d1fbadbf86d4 100644
--- a/drivers/net/ethernet/cortina/gemini.c
+++ b/drivers/net/ethernet/cortina/gemini.c
@@ -1107,10 +1107,13 @@  static void gmac_tx_irq_enable(struct net_device *netdev,
 {
 	struct gemini_ethernet_port *port = netdev_priv(netdev);
 	struct gemini_ethernet *geth = port->geth;
+	unsigned long flags;
 	u32 val, mask;
 
 	netdev_dbg(netdev, "%s device %d\n", __func__, netdev->dev_id);
 
+	spin_lock_irqsave(&geth->irq_lock, flags);
+
 	mask = GMAC0_IRQ0_TXQ0_INTS << (6 * netdev->dev_id + txq);
 
 	if (en)
@@ -1119,6 +1122,8 @@  static void gmac_tx_irq_enable(struct net_device *netdev,
 	val = readl(geth->base + GLOBAL_INTERRUPT_ENABLE_0_REG);
 	val = en ? val | mask : val & ~mask;
 	writel(val, geth->base + GLOBAL_INTERRUPT_ENABLE_0_REG);
+
+	spin_unlock_irqrestore(&geth->irq_lock, flags);
 }
 
 static void gmac_tx_irq(struct net_device *netdev, unsigned int txq_num)
@@ -1415,15 +1420,19 @@  static unsigned int gmac_rx(struct net_device *netdev, unsigned int budget)
 	union gmac_rxdesc_3 word3;
 	struct page *page = NULL;
 	unsigned int page_offs;
+	unsigned long flags;
 	unsigned short r, w;
 	union dma_rwptr rw;
 	dma_addr_t mapping;
 	int frag_nr = 0;
 
+	spin_lock_irqsave(&geth->irq_lock, flags);
 	rw.bits32 = readl(ptr_reg);
 	/* Reset interrupt as all packages until here are taken into account */
 	writel(DEFAULT_Q0_INT_BIT << netdev->dev_id,
 	       geth->base + GLOBAL_INTERRUPT_STATUS_1_REG);
+	spin_unlock_irqrestore(&geth->irq_lock, flags);
+
 	r = rw.bits.rptr;
 	w = rw.bits.wptr;
 
@@ -1726,10 +1735,9 @@  static irqreturn_t gmac_irq(int irq, void *data)
 		gmac_update_hw_stats(netdev);
 
 	if (val & (GMAC0_RX_OVERRUN_INT_BIT << (netdev->dev_id * 8))) {
+		spin_lock(&geth->irq_lock);
 		writel(GMAC0_RXDERR_INT_BIT << (netdev->dev_id * 8),
 		       geth->base + GLOBAL_INTERRUPT_STATUS_4_REG);
-
-		spin_lock(&geth->irq_lock);
 		u64_stats_update_begin(&port->ir_stats_syncp);
 		++port->stats.rx_fifo_errors;
 		u64_stats_update_end(&port->ir_stats_syncp);