net: keep txq->trans_start on repeated netif_tx_stop_queue()
Subsystems (3)
v3 cover net: keep txq->trans_start on repeated netif_tx_stop_queue()
Since commit 95ecba62e2fd ("net: fix races in netdev_tx_sent_queue()/dev_watchdog()"), every netif_tx_stop_queue() call writes txq->trans_start, also on a queue that is already stopped. Drivers whose Tx hang check stops the queue again on each run (e1000, e1000e, igb, igc) keep moving trans_start forward, dev_watchdog() never times out, and the hung adapter is never reset.
Patch 1 makes netif_tx_stop_queue() return early when the driver has already stopped the queue, so it writes neither trans_start nor the bit. Patch 2 adds KUnit tests for the four cases; one of them fails without patch 1.
The issue was observed on a system in real use. I reproduced it and tested the fix on an AlmaLinux 10 kernel (6.12.0-211.74.2.el10_2) with e1000 on a VMware-emulated 82545EM, using a test-only module parameter that forces the e1000 hang check to fire. Patch 1 has the details. The KUnit tests ran on UML on the net tree.
I wrote the first version of the fix. Claude Code (Anthropic) reviewed it, wrote v2 of the fix, the KUnit tests, the e1000 reproducer and the changelogs.
Changes in v3:
- Rebased on net. Patch 2 did not apply after commit e8c39bfac24d ("net: add a KUnit test for the alloc_skb_with_frags() tail split") added tests at the same place in net_test.c. The tests now come before the netmem section. No code change.
Changes in v2:
- Patch 1: return early when DRV_XOFF is already set instead of only skipping the trans_start write. In v1, a wake between test_bit() and set_bit() let set_bit() stop the queue again with the trans_start of the earlier stop, which could cause a spurious timeout (Sashiko AI review). The early return issues smp_mb() to keep the full barrier that callers get after set_bit().
- Patch 1: rewrite the comment and the changelog paragraph on the race, which claimed a bound that v1 did not have.
- Patch 2: state in the changelog that the tests do not cover a wake racing with a stop.
- Rebased on net.
- Not carried: Xia Yan's Reviewed-by on v1, since patch 1 changed.
v2: https://lore.kernel.org/netdev/[email protected]/
v1: https://lore.kernel.org/netdev/[email protected]/
Curtis McBurney (2):
net: keep txq->trans_start on repeated netif_tx_stop_queue()
net: net_test: add tests for netif_tx_stop_queue() trans_start
include/linux/netdevice.h | 14 +++++ net/core/net_test.c | 112 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+)
base-commit: e1055cbfd6eb578417f3ce983cf44462e8465267
Signature · 1 line
2.52.0
PATCH v3 1/2 net: keep txq->trans_start on repeated netif_tx_stop_queue()
Since commit 95ecba62e2fd ("net: fix races in netdev_tx_sent_queue()/dev_watchdog()"), netif_tx_stop_queue() writes txq->trans_start on every call, also when the queue is already stopped.
Several drivers stop the queue from their periodic Tx hang check and leave the recovery to dev_watchdog(). e1000, e1000e, igb and igc call netif_stop_queue() or netif_stop_subqueue() each time the hang check, requested every 2 seconds by the driver watchdog, finds the hang again, and do not wake the queue. Each call now moves trans_start forward, so dev_watchdog() never sees the queue stopped for longer than watchdog_timeo (5 seconds for these drivers). ndo_tx_timeout() is never called and the adapter is never reset: the driver logs "Detected Tx Unit Hang" every 2 seconds while the NIC stays hung.
Return early from netif_tx_stop_queue() when __QUEUE_STATE_DRV_XOFF is already set, so a stop on a queue that the driver already stopped writes neither trans_start nor the bit. A queue stopped only by BQL (__QUEUE_STATE_STACK_XOFF) still gets a new trans_start when the driver stops it. On the path that sets the bit, trans_start is still written before set_bit(), which keeps the ordering against dev_watchdog() added by 95ecba62e2fd. The early return issues smp_mb(), so callers that follow the stop with smp_mb__after_atomic(), such as netif_txq_try_stop(), still get a full barrier.
netif_tx_wake_queue() can clear the bit between test_bit() and the return. The queue is then running, as if the wake had come after the stop, and the next stop writes a new trans_start. Setting the bit without writing trans_start would instead leave a newly stopped queue with the trans_start of an earlier stop, from any time before if the caller is preempted, and dev_watchdog() could report a spurious timeout. netif_tx_stop_queue() is the only function that sets DRV_XOFF, so every transition of the bit from 0 to 1 follows a trans_start write by the same caller. The time between that write and set_bit() is unchanged from 95ecba62e2fd.
The issue was observed on a system in real use: a hung NIC kept reporting a Tx unit hang and was never reset. Code inspection then pointed to 95ecba62e2fd.
I reproduced the issue and tested the fix on an AlmaLinux 10 kernel (6.12.0-211.74.2.el10_2, which contains 95ecba62e2fd) with the e1000 driver on a VMware-emulated Intel 82545EM. Reproducing required modifying the driver: a test-only module parameter makes e1000_clean_tx_irq() report a Tx unit hang on every hang check of one interface. netif_tx_stop_queue() is inlined into the driver, so the patched variant was e1000 built out of tree against the 6.12 headers with this change applied. With the fake hang held for 30 seconds:
unpatched: 15 "Detected Tx Unit Hang", no NETDEV WATCHDOG,
tx_timeout 0 -> 0
patched: first "NETDEV WATCHDOG ... timed out 5052 ms" 5 seconds
after the first stop, followed by an adapter reset; the cycle repeated 5 times (5052 to 5584 ms), each time followed by a reset; tx_timeout 0 -> 5
This patch was not booted on the net tree. The KUnit tests added in the next patch pass with this change and fail without it.
I wrote the first version of the fix. Claude Code (Anthropic) reviewed it, wrote the current change, built the e1000
reproducer and wrote this changelog.
--- include/linux/netdevice.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
include/linux/netdevice.h +14 -0
@@ -3805,6 +3805,20 @@ static inline void netif_tx_wake_all_queues(struct net_device *dev) static __always_inline void netif_tx_stop_queue(struct netdev_queue *dev_queue) { +/* A queue already stopped by the driver keeps the trans_start of +* the stop that set DRV_XOFF, so repeated stops from a driver hang +* check do not postpone dev_watchdog(). Neither the stamp nor the +* bit is written: a wake racing with this call leaves the queue +* running, as if it came after the stop. +*/ +if (test_bit (__QUEUE_STATE_DRV_XOFF, &dev_queue->state)) { +/* Full barrier, as given by set_bit() followed by +* smp_mb__after_atomic(), see netif_txq_try_stop() +*/ +smp_mb (); +return ; + } +/* Paired with READ_ONCE() from dev_watchdog() */ WRITE_ONCE (dev_queue->trans_start, jiffies);
Signature · 1 line
2.52.0
PATCH v3 2/2 net: net_test: add tests for netif_tx_stop_queue() trans_start
In reply to Curtis McBurneyAdd KUnit tests for the txq->trans_start handling of netif_tx_stop_queue(), which dev_watchdog() uses to detect a stuck queue:
- a stop on a running queue writes trans_start
- a stop on a queue already stopped by the driver keeps trans_start
- a stop after netif_tx_start_queue() writes trans_start again
- a stop on a queue stopped only by BQL (__QUEUE_STATE_STACK_XOFF) writes trans_start
txq_stop_keeps_stamp_stopped_queue fails without the previous patch.
The tests run on a single thread and do not cover a wake that races with a stop. The previous patch removes that race by construction: a stop that finds the bit set writes neither trans_start nor the bit.
Tested on UML with CONFIG_KUNIT, CONFIG_NET and CONFIG_NET_TEST:
tools/testing/kunit/kunit.py run --kunitconfig=<cfg> 'net_core.txq_*'
Claude Code (Anthropic) wrote these tests and this changelog.
--- net/core/net_test.c | 112 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+)
net/core/net_test.c +112 -0
@@ -513,6 +513,114 @@ static void ip_tunnel_flags_test_run(struct kunit *test)KUNIT_ASSERT_TRUE (test,__ipt_flag_op (bitmap_equal, exp, out)); } +/* netif_tx_stop_queue() and txq->trans_start */ + +#include <linux/etherdevice.h> +#include <linux/jiffies.h> +#include <linux/netdevice.h> + +/* dev_watchdog() reports a timeout when a queue stays stopped for longer +* than watchdog_timeo after txq->trans_start. A trans_start value this far +* in the past is distinguishable from a fresh one. +*/ +#define TXQ_TEST_OLD_DELTA (10 * HZ) + +KUNIT_DEFINE_ACTION_WRAPPER (txq_test_free_netdev, free_netdev, + struct net_device *); + +static struct netdev_queue *txq_test_alloc (struct kunit *test) +{ +struct net_device *dev; + + dev =alloc_etherdev (0 ); +KUNIT_ASSERT_NOT_NULL (test, dev); +KUNIT_ASSERT_EQ (test,0 , +kunit_add_action_or_reset (test, txq_test_free_netdev, + dev)); + +return netdev_get_tx_queue (dev,0 ); +} + +static void txq_test_expect_fresh (struct kunit *test, +struct netdev_queue *txq, +unsigned long before) +{ +unsigned long stamp =READ_ONCE (txq->trans_start); + +KUNIT_EXPECT_TRUE_MSG (test,time_in_range (stamp, before, jiffies), +"trans_start=%lu not in [%lu, %lu]" , + stamp, before, jiffies); +} + +/* A stop on a running queue records the stop time in trans_start */ +static void txq_stop_stamps_running_queue (struct kunit *test) +{ +struct netdev_queue *txq =txq_test_alloc (test); +unsigned long before; + +WRITE_ONCE (txq->trans_start, jiffies -TXQ_TEST_OLD_DELTA ); + before = jiffies; +netif_tx_stop_queue (txq); + +KUNIT_EXPECT_TRUE (test,netif_tx_queue_stopped (txq)); +txq_test_expect_fresh (test, txq, before); +} + +/* A stop on a queue that is already stopped keeps the first stop time, so +* repeated stops from a driver hang check do not postpone dev_watchdog() +*/ +static void txq_stop_keeps_stamp_stopped_queue (struct kunit *test) +{ +struct netdev_queue *txq =txq_test_alloc (test); +unsigned long old, stamp; + +netif_tx_stop_queue (txq); + old = jiffies -TXQ_TEST_OLD_DELTA ; +WRITE_ONCE (txq->trans_start, old); +netif_tx_stop_queue (txq); + stamp =READ_ONCE (txq->trans_start); + +KUNIT_EXPECT_TRUE (test,netif_tx_queue_stopped (txq)); +KUNIT_EXPECT_EQ_MSG (test, stamp, old, +"second stop moved trans_start forward by %lu jiffies" , + stamp - old); +} + +/* A stop after a restart records a new stop time */ +static void txq_stop_stamps_restarted_queue (struct kunit *test) +{ +struct netdev_queue *txq =txq_test_alloc (test); +unsigned long before; + +netif_tx_stop_queue (txq); +netif_tx_start_queue (txq); +KUNIT_EXPECT_FALSE (test,netif_tx_queue_stopped (txq)); + +WRITE_ONCE (txq->trans_start, jiffies -TXQ_TEST_OLD_DELTA ); + before = jiffies; +netif_tx_stop_queue (txq); + +KUNIT_EXPECT_TRUE (test,netif_tx_queue_stopped (txq)); +txq_test_expect_fresh (test, txq, before); +} + +/* Only the driver XOFF bit decides whether the stop is a transition. A queue +* stopped by BQL (stack XOFF) gets a new stop time when the driver stops it. +*/ +static void txq_stop_stamps_stack_stopped_queue (struct kunit *test) +{ +struct netdev_queue *txq =txq_test_alloc (test); +unsigned long before; + +set_bit (__QUEUE_STATE_STACK_XOFF, &txq->state); +WRITE_ONCE (txq->trans_start, jiffies -TXQ_TEST_OLD_DELTA ); + before = jiffies; +netif_tx_stop_queue (txq); + +KUNIT_EXPECT_TRUE (test,netif_tx_queue_stopped (txq)); +txq_test_expect_fresh (test, txq, before); +} +/* Netmem & SKB fragment refcounting */ #include <linux/skbuff_ref.h>
@@ -1791,6 +1899,10 @@ static struct kunit_case net_test_cases[] = { KUNIT_CASE(netmem_ref_test_tcp_devmem_recv), #endif #endif + KUNIT_CASE(txq_stop_stamps_running_queue), + KUNIT_CASE(txq_stop_keeps_stamp_stopped_queue), + KUNIT_CASE(txq_stop_stamps_restarted_queue), + KUNIT_CASE(txq_stop_stamps_stack_stopped_queue), { }, };
Signature · 1 line
2.52.0