veth: fix lost NAPI wakeup with threaded NAPI
Subsystems (3)
cover veth: fix lost NAPI wakeup with threaded NAPI
From: Jesper Dangaard Brouer <[email protected]>
On ARM64 servers (Ampere Altra Max) with threaded NAPI, veth TX queues can stall permanently. The peer rq has rx_notify_masked set while its NAPI is idle. Producers then skip scheduling NAPI, and nothing drains the ptr_ring. With a qdisc, the TXQ stays stopped, because of commit dc82a33297fc ("veth: apply qdisc backpressure on full ptr_ring to reduce TX drops"). Without a qdisc, veth silently drops all packets for that queue.
The cause is a missing write barrier. __veth_xdp_flush() stores rx_notify_masked = true, then __napi_schedule() sets NAPI_STATE_SCHED_THREADED with a relaxed set_bit(). On ARM64 the second store can become visible first. An awake NAPI kthread on another CPU then runs a full poll and stores rx_notify_masked = false, and the late true store lands after it. Commit 68468d8c4cd4 ("veth: fix races around rq->rx_notify_masked") opened this window, when it moved napi_schedule_prep() before the store.
Patch 1 is a failsafe. It enables dev_watchdog for veth and recovers the peer NAPI on TX timeout. A TXQ wake alone does not help, because veth_xmit() hits the full ring and stops the TXQ again. We found the bug fixed by commit 5442a9da6978 ("veth: more robust handing of race to avoid txq getting stuck") with a similar watchdog patch in production.
Patch 2 is the fix: smp_wmb() in __veth_xdp_flush().
Patch 3 adds the same barrier to the re-arm path in veth_poll(). That path can only race when a socket busy-polls a threaded NAPI. The patch moves the schedule sequence into one helper, so both paths share it.
herd7 litmus tests back both patches, for LKMM and for herd7's arm64 model. Without a barrier, the bad state is reachable. With patch 2 only, it is still reachable through the busy poll re-arm. With both patches, no test reaches it. Tests and results:
https://github.com/netoptimizer/veth-backpressure-performance-testing/pull/15
Testing: in virtme-ng we starved the peer NAPI kthread for 12 seconds under a UDP flood, on 6.18 and on the net tree. The watchdog fired, the queue recovered, and the kernel log had no warnings. The test cannot reproduce the ARM64 race, but it shows that the watchdog recovery works. selftests/net/veth.sh passes. The test is here:
https://github.com/netoptimizer/veth-backpressure-performance-testing/pull/14
Jesper Dangaard Brouer (2):
veth: enable dev_watchdog and recover stalled peer NAPI
veth: order rx_notify_masked store in the veth_poll() re-arm path
Marek Kroemeke (1):
veth: order rx_notify_masked store before __napi_schedule()
drivers/net/veth.c | 63 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 52 insertions(+), 11 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/3 veth: enable dev_watchdog and recover stalled peer NAPI
From: Jesper Dangaard Brouer <[email protected]>
Since commit dc82a33297fc ("veth: apply qdisc backpressure on full ptr_ring to reduce TX drops"), veth stops the TXQ when the peer ptr_ring is full, and only the peer veth_poll() wakes it again. If a race loses the NAPI wakeup, the TXQ stays stopped and the qdisc cannot send.
We now hit such a race on ARM64: the peer rq has rx_notify_masked set while NAPI is idle, so __veth_xdp_flush() never schedules NAPI and nothing drains the ring. The patches "veth: order rx_notify_masked store before __napi_schedule()" and "veth: order rx_notify_masked store in the veth_poll() re-arm path" fix that race.
Enable dev_watchdog as a failsafe, with a 5 second timeout. On timeout, if the peer NAPI is not scheduled, clear rx_notify_masked on the peer rq and call __veth_xdp_flush() to schedule NAPI. Then wake the TXQ. A TXQ wake alone does not help, because veth_xmit() hits the full ring and stops the TXQ again. If the peer NAPI is already scheduled, rx_notify_masked is correct, and the handler only wakes the TXQ. The timer runs on every veth with carrier, like on any driver with ndo_tx_timeout. Each run scans dev->num_tx_queues, which for veth is num_possible_cpus().
This watchdog is a fix in its own right, and it belongs in stable. Lost wakeups in this path keep showing up.
Commit 5442a9da6978 ("veth: more robust handing of race to avoid txq getting stuck") and commit 60db47f02bfa ("veth: fix queue index used to wake the peer txq in veth_poll") each fixed one. Each of them caused a permanent TXQ stall, because veth has no ndo_tx_timeout. We found the first one with a watchdog like this one, which never went upstream. The watchdog covers the whole class on veths with a qdisc. If a race we do not know about yet loses a wakeup, the TXQ stalls for about 5 seconds instead of permanently, and the log shows the state needed to debug it. Without a qdisc the TXQ never stops, so the watchdog cannot help there. dev_watchdog() handles one timed-out queue per run, so with several stalled queues, each one waits for its own run.
The log line prints the peer rq state from before the recovery, to show which lost wakeup happened. The state is the raw napi_struct state word, for diagnostics only (hinic and hns3 print it the same way):
veth0: NETDEV WATCHDOG: CPU: 34: transmit queue 0 timed out 5393 ms
veth0: veth backpressure stalled(n:1) TXQ(0) re-enable peer napi:1 masked:1 state:0x110
--- drivers/net/veth.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+)
drivers/net/veth.c +37 -0
@@ -1403,6 +1403,41 @@ static int veth_set_channels(struct net_device *dev, goto out; } +static void veth_tx_timeout (struct net_device *dev,unsigned int txqueue) +{ +struct netdev_queue *txq =netdev_get_tx_queue (dev, txqueue); +struct veth_priv *priv =netdev_priv (dev); +bool masked = false, napi = false; +unsigned long napi_state =0 ; +struct net_device *rcv; + +rcu_read_lock (); + rcv =rcu_dereference (priv->peer); +if (rcv && txqueue < rcv->real_num_rx_queues) { +struct veth_priv *rcv_priv =netdev_priv (rcv); +struct veth_rq *rq = &rcv_priv->rq[txqueue]; + +if (rcu_access_pointer (rq->napi)) { + napi = true; + masked =READ_ONCE (rq->rx_notify_masked); + napi_state =READ_ONCE (rq->xdp_napi.state); +/* Recover lost NAPI wakeup if rx_notify_masked stuck */ +if (!(napi_state &NAPIF_STATE_SCHED )) { +WRITE_ONCE (rq->rx_notify_masked, false); +__veth_xdp_flush (rq); + } + } + } +rcu_read_unlock (); + +netdev_err (dev, +"veth backpressure stalled(n:%ld) TXQ(%u) re-enable peer napi:%d masked:%d state:%#lx\n" , +atomic_long_read (&txq->trans_timeout), txqueue, + napi, masked, napi_state); + +netif_tx_wake_queue (txq); +} +static int veth_open (struct net_device *dev) {struct veth_priv *priv =netdev_priv (dev);
@@ -1741,6 +1776,7 @@ static const struct net_device_ops veth_netdev_ops = { .ndo_bpf = veth_xdp, .ndo_xdp_xmit = veth_ndo_xdp_xmit, .ndo_get_peer_dev = veth_peer_dev, + .ndo_tx_timeout = veth_tx_timeout, };static const struct xdp_metadata_ops veth_xdp_metadata_ops = {
@@ -1779,6 +1815,7 @@ static void veth_setup(struct net_device *dev) dev->priv_destructor = veth_dev_free; dev->pcpu_stat_type =NETDEV_PCPU_STAT_TSTATS ; dev->max_mtu =ETH_MAX_MTU ; + dev->watchdog_timeo =msecs_to_jiffies (5000 ); dev->hw_features =VETH_FEATURES ; dev->hw_enc_features =VETH_FEATURES ;
Signature · 1 line
2.43.0
PATCH 2/3 veth: order rx_notify_masked store before __napi_schedule()
In reply to [email protected]From: Marek Kroemeke <[email protected]>
__veth_xdp_flush() stores rx_notify_masked = true and then calls __napi_schedule(). With threaded NAPI, ____napi_schedule() sets NAPI_STATE_SCHED_THREADED with a relaxed set_bit() before wake_up_process(). An awake NAPI kthread in napi_thread_wait() sees the bit with test_bit(), without the barriers in wake_up_process().
On weakly ordered CPUs such as ARM64, the rx_notify_masked store can become visible after the set_bit(). The kthread can then complete a full poll in between (masked is rq->rx_notify_masked):
producer CPU NAPI kthread
------------ ------------
WRITE_ONCE(masked, true)
set_bit(SCHED_THREADED)
test_bit(SCHED_THREADED)
veth_poll()
napi_complete_done()
smp_store_mb(masked, false)store masked = true lands, overwrites false
any producer CPU, later
__veth_xdp_flush()
masked is true: skip NAPI
Now rx_notify_masked is true while NAPI is idle. __veth_xdp_flush() never schedules NAPI again, and the ring stays full. We saw this on ARM64 (Ampere Altra Max) with threaded NAPI and a qdisc on the veth:
the TXQ stayed stopped. With the previous patch, the watchdog recovers it after 5 seconds.
Without a qdisc, veth_xmit() drops the packet instead of stopping the TXQ. The stuck queue then silently drops all its packets, until the receiving veth goes down or the last of XDP and GRO is turned off on it. Kernels before commit dc82a33297fc ("veth: apply qdisc backpressure on full ptr_ring to reduce TX drops") drop in the same way, with or without a qdisc.
Commit 68468d8c4cd4 ("veth: fix races around rq->rx_notify_masked") opened this window in v5.17, when it moved napi_schedule_prep(), a fully ordered atomic, before the store.
Add smp_wmb() between the store and __napi_schedule(). On the kthread side, the test_bit() control dependency and the napi_complete_done() cmpxchg order the later smp_store_mb(). On x86_64 smp_wmb() is only a compiler barrier.
The ordering gap is in the threaded NAPI handoff: ____napi_schedule() sets NAPI_STATE_SCHED_THREADED with a relaxed set_bit(). A barrier there would also cover other drivers with the same pattern. This patch fixes veth only, to keep the stable backport small. A core change can follow separately.
Verified with herd7 7.58, with the LKMM of v6.18.65 and with herd7's arm64 model. Without smp_wmb(), every test reaches the bad state:rx_notify_masked is true while NAPI is idle. With smp_wmb() in __veth_xdp_flush(), the producer handoff tests report Never, also with two producers, with two polls by the kthread, and for arm64 with dmb ishst. The tests and results are here:
https://github.com/netoptimizer/veth-backpressure-performance-testing/pull/15
--- drivers/net/veth.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/net/veth.c +2 -0
@@ -306,6 +306,8 @@ static void __veth_xdp_flush(struct veth_rq *rq)if (!READ_ONCE (rq->rx_notify_masked) &&napi_schedule_prep (&rq->xdp_napi)) {WRITE_ONCE (rq->rx_notify_masked, true); +/* Order rx_notify_masked before NAPI_STATE_SCHED_THREADED */ +smp_wmb ();__napi_schedule (&rq->xdp_napi); } }
Signature · 1 line
2.43.0
PATCH 3/3 veth: order rx_notify_masked store in the veth_poll() re-arm path
In reply to [email protected]From: Jesper Dangaard Brouer <[email protected]>
The re-arm path at the end of veth_poll() has the same store sequence as __veth_xdp_flush(), without the barrier added in the previous patch.
Normally this is safe. The next poll runs in the same NAPI kthread, or in softirq on the same CPU, so program order keeps the two stores in order. Busy polling breaks this. veth GRO sets the NAPI ID on the skb, so a socket can busy-poll the veth NAPI from another CPU. When the busy poll loop ends, busy_poll_stop() calls veth_poll() on the busy-polling CPU, and the re-arm hands NAPI to the kthread on another CPU:
busy-poll CPU NAPI kthread
------------- ------------
busy_poll_stop()
veth_poll()
napi_schedule_prep()
WRITE_ONCE(masked, true)
set_bit(SCHED_THREADED)
test_bit(SCHED_THREADED)
veth_poll()
napi_complete_done()
smp_store_mb(masked, false)store masked = true lands, overwrites false
The race needs all three: threaded NAPI, busy polling on the same queue, and an awake kthread. A kthread that sleeps is woken by wake_up_process(), which orders the stores. The result is the same latch as in the previous patch. We found this by code review, not in production. herd7 confirms it: with the barrier only in __veth_xdp_flush(), the veth-busypoll-rearm litmus tests still reach the latch. With the helper, they do not. The tests are here:
https://github.com/netoptimizer/veth-backpressure-performance-testing/pull/15
Move the schedule sequence into a helper, veth_napi_schedule(), and use it in both places. Both callers schedule NAPI the same way, so the ordering rule now lives in one place.
No functional change for __veth_xdp_flush().
--- drivers/net/veth.c | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-)
drivers/net/veth.c +15 -13
@@ -299,17 +299,23 @@ static void veth_ptr_free(void *ptr)kfree_skb (ptr); } +static void veth_napi_schedule (struct veth_rq *rq) +{ +if (!napi_schedule_prep (&rq->xdp_napi)) +return ; + +WRITE_ONCE (rq->rx_notify_masked, true); +/* Order rx_notify_masked before NAPI_STATE_SCHED_THREADED */ +smp_wmb (); +__napi_schedule (&rq->xdp_napi); +} +static void __veth_xdp_flush (struct veth_rq *rq) {/* Write ptr_ring before reading rx_notify_masked */ smp_mb (); -if (!READ_ONCE (rq->rx_notify_masked) && -napi_schedule_prep (&rq->xdp_napi)) { -WRITE_ONCE (rq->rx_notify_masked, true); -/* Order rx_notify_masked before NAPI_STATE_SCHED_THREADED */ -smp_wmb (); -__napi_schedule (&rq->xdp_napi); - } +if (!READ_ONCE (rq->rx_notify_masked)) +veth_napi_schedule (rq); }static int veth_xdp_rx (struct veth_rq *rq,struct sk_buff *skb)
@@ -1001,12 +1007,8 @@ static int veth_poll(struct napi_struct *napi, int budget)if (done < budget &&napi_complete_done (napi, done)) {/* Write rx_notify_masked before reading ptr_ring */ smp_store_mb (rq->rx_notify_masked, false); -if (unlikely (!__ptr_ring_empty (&rq->xdp_ring))) { -if (napi_schedule_prep (&rq->xdp_napi)) { -WRITE_ONCE (rq->rx_notify_masked, true); -__napi_schedule (&rq->xdp_napi); - } - } +if (unlikely (!__ptr_ring_empty (&rq->xdp_ring))) +veth_napi_schedule (rq); }/* Release backpressure per NAPI poll */
Signature · 1 line
2.43.0
Quoted 7 lines · On 11/30/26 10:39, [email protected] wrote:
herd7 litmus tests back both patches, for LKMM and for herd7's arm64 model. Without a barrier, the bad state is reachable. With patch 2 only, it is still reachable through the busy poll re-arm. With both patches, no test reaches it. Tests and results:
https://github.com/netoptimizer/veth-backpressure-performance-testing/
pull/15
Merged and avail as: [1] [2]
- https://github.com/netoptimizer/veth-backpressure-performance-testing/tree/main/memory-model
Full disclosure, it was the the LLM (Claude Fable 5.2) that went above and beyond analyzing the memory barrier against the Linux Kernel memory model (tools/memory-model/). I don't claim to understand the herd7 litmus tests myself.
--Jesper
[1] https://github.com/netoptimizer/veth-backpressure-performance-testing/tree/main/memory-model#readme
[2] https://github.com/netoptimizer/veth-backpressure-performance-testing/blob/main/memory-model/RESULTS.md