Skip to content

Commit fc1be58

Browse files
lukaszraczylopelwell
authored andcommitted
net: macb: add TX stall watchdog as defence-in-depth safety net
Patches 1/3 and 2/3 address two candidate races that could lead to a TCOMP completion being missed on PCIe-attached macb instances. This patch adds a defence-in-depth safety net, in case a further race remains that we have not identified. The watchdog is a per-queue delayed_work that runs once per second. Movement is tracked via a tx_stall_tail_moved boolean: macb_tx_complete() sets it under tx_ptr_lock whenever tx_tail advances, and the watchdog clears it under the same lock at each tick. If the ring is non-empty (tx_head != tx_tail) and the boolean was still false at the next tick, the watchdog calls macb_tx_restart(). A boolean is used in preference to snapshotting tx_tail and comparing across ticks, because per-queue ring indices are bounded and reused; under sustained load a snapshot comparison can false-positive when the index happens to land on the same value between two ticks. Both writes share tx_ptr_lock with the existing tx_head / tx_tail updates, so no atomic is required. No new recovery logic is introduced. macb_tx_restart() already exists in this file, is correctly locked (tx_ptr_lock, bp->lock), and verifies that the hardware's TBQP is behind the driver's head index before re-asserting TSTART. On a healthy ring it is a no-op at the hardware level; the watchdog only supplies the missing trigger. On a healthy queue the per-tick cost is one spin_lock_irqsave() / spin_unlock_irqrestore(), one branch, and one byte store. The delayed_work is only scheduled between macb_open() and macb_close(), and is cancelled synchronously on close. Context for submission: on our 24-node Raspberry Pi 5 fleet, before this series, an out-of-band user-space watchdog (monitoring tx_packets from /sys/class/net/.../statistics and toggling the link down/up when it froze) was required to keep nodes usable. We include this kernel-side watchdog as a cleaner in-kernel equivalent for any residual stall that patches 1 and 2 do not cover. We are willing to drop this patch if the view is that 1 and 2 should stand alone. Link: cilium/cilium#43198 Link: https://bugs.launchpad.net/ubuntu/+source/linux-raspi/+bug/2133877 Signed-off-by: Lukasz Raczylo <lukasz@raczylo.com>
1 parent 78fbe20 commit fc1be58

2 files changed

Lines changed: 76 additions & 0 deletions

File tree

drivers/net/ethernet/cadence/macb.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,6 +1297,17 @@ struct macb_queue {
12971297
struct work_struct tx_error_task;
12981298
bool txubr_pending;
12991299
bool tx_pending;
1300+
1301+
/* TX stall watchdog -- see macb_tx_stall_watchdog() in macb_main.c.
1302+
* tx_stall_tail_moved is set by macb_tx_complete() under tx_ptr_lock
1303+
* whenever tx_tail advances, and cleared by the watchdog tick on the
1304+
* same lock. A bool avoids the index-aliasing false-positive that a
1305+
* snapshot-of-tx_tail comparison would have when the ring index space
1306+
* happens to wrap to the same value between two ticks.
1307+
*/
1308+
struct delayed_work tx_stall_watchdog_work;
1309+
bool tx_stall_tail_moved;
1310+
13001311
struct napi_struct napi_tx;
13011312

13021313
dma_addr_t rx_ring_dma;

drivers/net/ethernet/cadence/macb_main.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,6 +1491,8 @@ static int macb_tx_complete(struct macb_queue *queue, int budget)
14911491
packets, bytes);
14921492

14931493
queue->tx_tail = tail;
1494+
if (packets)
1495+
queue->tx_stall_tail_moved = true;
14941496
if (__netif_subqueue_stopped(bp->dev, queue_index) &&
14951497
CIRC_CNT(queue->tx_head, queue->tx_tail,
14961498
bp->tx_ring_size) <= MACB_TX_WAKEUP_THRESH(bp))
@@ -2023,6 +2025,63 @@ static int macb_tx_poll(struct napi_struct *napi, int budget)
20232025
return work_done;
20242026
}
20252027

2028+
#define MACB_TX_STALL_INTERVAL_MS 1000
2029+
2030+
/* TX stall watchdog.
2031+
*
2032+
* Defence-in-depth against lost TCOMP interrupts. macb already has a
2033+
* recovery chain (tx_pending -> txubr_pending -> macb_tx_restart())
2034+
* that fires on TCOMP; if TCOMP itself is lost the TX ring stalls
2035+
* silently until something else kicks TSTART. This watchdog runs
2036+
* once per second per queue and calls macb_tx_restart() if the ring
2037+
* is non-empty and tx_tail has not advanced since the previous tick.
2038+
*
2039+
* Movement is tracked via the tx_stall_tail_moved boolean rather
2040+
* than by snapshotting tx_tail. Per-queue ring indices are bounded
2041+
* (and reused), so a snapshot comparison can false-positive when the
2042+
* index happens to land on the same value between two ticks under
2043+
* sustained load. The boolean is set by macb_tx_complete() whenever
2044+
* tx_tail advances and cleared by this watchdog after each tick;
2045+
* both writes are under tx_ptr_lock, so no atomic is required.
2046+
*
2047+
* macb_tx_restart() already checks the hardware's TBQP against the
2048+
* driver's head index before re-asserting TSTART, so on a healthy
2049+
* ring this is a no-op at the hardware level. The watchdog only
2050+
* adds the missing trigger.
2051+
*/
2052+
static void macb_tx_stall_watchdog(struct work_struct *work)
2053+
{
2054+
struct macb_queue *queue = container_of(to_delayed_work(work),
2055+
struct macb_queue,
2056+
tx_stall_watchdog_work);
2057+
struct macb *bp = queue->bp;
2058+
unsigned int cur_tail, cur_head;
2059+
bool stalled = false;
2060+
unsigned long flags;
2061+
2062+
if (!netif_running(bp->dev))
2063+
return;
2064+
2065+
spin_lock_irqsave(&queue->tx_ptr_lock, flags);
2066+
cur_tail = queue->tx_tail;
2067+
cur_head = queue->tx_head;
2068+
if (cur_head != cur_tail && !queue->tx_stall_tail_moved)
2069+
stalled = true;
2070+
queue->tx_stall_tail_moved = false;
2071+
spin_unlock_irqrestore(&queue->tx_ptr_lock, flags);
2072+
2073+
if (stalled) {
2074+
netdev_warn_once(bp->dev,
2075+
"TX stall detected on queue %u (tail=%u head=%u); re-kicking TSTART\n",
2076+
(unsigned int)(queue - bp->queues),
2077+
cur_tail, cur_head);
2078+
macb_tx_restart(queue);
2079+
}
2080+
2081+
schedule_delayed_work(&queue->tx_stall_watchdog_work,
2082+
msecs_to_jiffies(MACB_TX_STALL_INTERVAL_MS));
2083+
}
2084+
20262085
static void macb_hresp_error_task(struct work_struct *work)
20272086
{
20282087
struct macb *bp = from_work(bp, work, hresp_err_bh_work);
@@ -3252,6 +3311,9 @@ static int macb_open(struct net_device *dev)
32523311
for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
32533312
napi_enable(&queue->napi_rx);
32543313
napi_enable(&queue->napi_tx);
3314+
queue->tx_stall_tail_moved = true;
3315+
schedule_delayed_work(&queue->tx_stall_watchdog_work,
3316+
msecs_to_jiffies(MACB_TX_STALL_INTERVAL_MS));
32553317
}
32563318

32573319
macb_init_hw(bp);
@@ -3302,6 +3364,7 @@ static int macb_close(struct net_device *dev)
33023364
for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
33033365
napi_disable(&queue->napi_rx);
33043366
napi_disable(&queue->napi_tx);
3367+
cancel_delayed_work_sync(&queue->tx_stall_watchdog_work);
33053368
netdev_tx_reset_queue(netdev_get_tx_queue(dev, q));
33063369
}
33073370

@@ -4914,6 +4977,8 @@ static int macb_init_dflt(struct platform_device *pdev)
49144977
}
49154978

49164979
INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
4980+
INIT_DELAYED_WORK(&queue->tx_stall_watchdog_work,
4981+
macb_tx_stall_watchdog);
49174982
q++;
49184983
}
49194984

0 commit comments

Comments
 (0)