Skip to contentSkip to conversation

net: cadence: macb: Fix a possible deadlock in macb_halt_tx.

PATCH Superseded Mathieu Othacehe 2 participants 2 messages May 7, 2025 – May 8, 2025

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: cadence: macb: Fix a possible deadlock in macb_halt_tx.

There is a situation where after THALT is set high, TGO stays high as well. Because jiffies are never updated, as we are in a context with interrupts disabled, we never exit that loop and have a deadlock.

That deadlock was noticed on a sama5d4 device that stayed locked for days.

Use retries instead of jiffies so that the timeout really works and we do not have a deadlock anymore.

---
 drivers/net/ethernet/cadence/macb_main.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
drivers/net/ethernet/cadence/macb_main.c +5 -6
index 1fe8ec37491b1..ffcf569c14f6a 100644
@@ -997,20 +997,19 @@ static void macb_update_stats(struct macb *bp)
 
 static int macb_halt_tx(struct macb *bp)
 {
-	unsigned long	halt_time, timeout;
-	u32		status;
+	unsigned int delay_us = 250;
+	unsigned int retries = MACB_HALT_TIMEOUT / delay_us;
+	u32 status;
 
 	macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
 
-	timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
 	do {
-		halt_time = jiffies;
 		status = macb_readl(bp, TSR);
 		if (!(status & MACB_BIT(TGO)))
 			return 0;
 
-		udelay(250);
-	} while (time_before(halt_time, timeout));
+		udelay(delay_us);
+	} while (retries-- > 0);
 
 	return -ETIMEDOUT;
 }
Signature · 1 line
2.49.0
maintainer
#1 lore raw
Quoted 10 lines · Mathieu Othacehe

There is a situation where after THALT is set high, TGO stays high as well. Because jiffies are never updated, as we are in a context with interrupts disabled, we never exit that loop and have a deadlock.

That deadlock was noticed on a sama5d4 device that stayed locked for days.

Use retries instead of jiffies so that the timeout really works and we do not have a deadlock anymore.

This looks like a fix that should target the net tree and include a fixes tag, see Documentation/process/maintainer-netdev.rst

Quoted 21 lines
---
 drivers/net/ethernet/cadence/macb_main.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
drivers/net/ethernet/cadence/macb_main.c +3 -3
index 1fe8ec37491b1..ffcf569c14f6a 100644
@@ -997,20 +997,19 @@ static void macb_update_stats(struct macb *bp)
 
 static int macb_halt_tx(struct macb *bp)
 {
-	unsigned long	halt_time, timeout;
-	u32		status;
+	unsigned int delay_us = 250;
+	unsigned int retries = MACB_HALT_TIMEOUT / delay_us;
+	u32 status;
 
 	macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
 
-	timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);

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

Quoted 10 lines
drivers/net/ethernet/cadence/macb_main.c +2 -3
 	do {
-		halt_time = jiffies;
 		status = macb_readl(bp, TSR);
 		if (!(status & MACB_BIT(TGO)))
 			return 0;
 
-		udelay(250);
-	} while (time_before(halt_time, timeout));
+		udelay(delay_us);
+	} while (retries-- > 0);

I think it would be better to use read_poll_timeout_atomic() instead of sort-of open-codying it.

Thanks,

Paolo