[PATCH net 0/1] net: stmmac: use plain TX queue lock in the NAPI poll path
Luke Dashjr
luke at dashjr.org
Wed Dec 2 15:56:15 PST 2026
netconsole over an stmmac interface can self-deadlock the CPU during early
boot. The patch is confined to
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c.
netpoll may run a device NAPI ->poll() callback with hard IRQs disabled
while
it holds that NAPI's poll owner. netconsole takes this path when a transmit
does not complete immediately: __netpoll_send_skb() retries through
netpoll_poll_dev(), which claims napi->poll_owner and runs the driver TX
poll
with a zero budget. stmmac_tx_clean() then releases its TX queue lock
with the
BH-enabling __netif_tx_unlock_bh(), whose local_bh_enable() runs a pending
NET_RX_SOFTIRQ inline. That softirq reaches netpoll_poll_lock() on the same
NAPI and the same CPU that still owns it, and spins forever. On a box whose
only recovery channel is early netconsole, this is unrecoverable.
The path, verified from source:
netpoll_send_skb() local_irq_save()
__netpoll_send_skb() lockdep_assert_irqs_disabled()
netpoll_poll_dev()
poll_napi() cmpxchg(napi->poll_owner, -1, cpu)
poll_one_napi() napi->poll(napi, 0)
stmmac_napi_poll_tx(budget=0)
stmmac_tx_clean(budget=0)
__netif_tx_lock_bh() ... __netif_tx_unlock_bh()
-> local_bh_enable()
-> __local_bh_enable_ip()
do_softirq() [IRQs
still off]
net_rx_action()
napi_poll()
netpoll_poll_lock() <-- spins
netpoll_poll_lock() (include/linux/netpoll.h) is an unbounded same-CPU
busy-wait:
while (cmpxchg(&napi->poll_owner, -1, owner) != -1)
cpu_relax();
__local_bh_enable_ip() only requires !in_interrupt() (it does not
require IRQs
enabled), so do_softirq() runs while poll_owner == cpu; irqs_disabled() does
not imply in_interrupt().
The bug reproduces on vanilla mainline (7.4-rc5, arm64, nr_cpus=1,
preempt=none) with no kernel harness. The only non-mainline piece is a
local,
test-only tweak to QEMU's npcm_gmac device model that defers the
TX-completion
interrupt by NPCM_TX_DELAY_NS nanoseconds, emulating real DMA/interrupt
latency; 0 restores stock behaviour, and no QEMU code is part of this
submission. The instrument is a few lines in npcm_gmac.c:
/* LOCAL TEST INSTRUMENT -- NOT FOR UPSTREAM */
if (!gmac->tx_delay_ns) {
gmac->regs[R_NPCM_DMA_STATUS] |= NPCM_DMA_STATUS_TI;
gmac_update_irq(gmac);
return;
}
if (!gmac->tx_done_pending) {
gmac->tx_done_pending = true;
timer_mod(gmac->tx_done_timer,
qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
gmac->tx_delay_ns);
}
Booting mainline with the console on the extended netconsole,
NPCM_TX_DELAY_NS=200000 qemu-system-aarch64 -M npcm845-evb -m 1G \
-nographic -no-reboot -smp 4 -kernel arch/arm64/boot/Image \
-initrd initramfs.cpio.gz \
-dtb arch/arm64/boot/dts/nuvoton/nuvoton-npcm845-evb.dtb \
-nic user,model=npcm-gmac \
-append "console=ttyS0,115200 earlycon=uart,mmio32,0xf0000000 \
nr_cpus=1 maxcpus=1 loglevel=8 ignore_loglevel \
netconsole=+r6665 at 10.0.2.15/eth0,6666 at 10.0.2.2/"
stalls right after "netconsole: network logging started" in 17 of 20 boots,
with the CPU spinning in netpoll_poll_lock(). With this fix, 20 of 20 boots
reach /init. A 10 us deferral is already enough to hang 8 of 12 unpatched
boots, well within real hardware's completion/coalescing latency. GDB shows
the outer context (the netconsole console thread, hard IRQs off,
transmit not
complete):
netpoll_send_udp() -> netpoll_send_skb() -> __netpoll_send_skb()
-> netpoll_poll_dev() -> poll_napi() -> poll_one_napi()
-> stmmac_napi_poll_tx(budget=0) -> stmmac_tx_clean(budget=0)
and the nested softirq that cannot make progress, spinning on a
napi->poll_owner equal to this CPU (poll_owner == 0):
netpoll_poll_lock include/linux/netpoll.h:76 (spinning)
napi_poll net/core/dev.c:8064
net_rx_action net/core/dev.c:8223
__do_softirq kernel/softirq.c:703
____do_softirq arch/arm64/kernel/irq.c:78
A deterministic test object drives the identical path on demand: it
stops the
TX queue, schedules the TX NAPI, disables hard IRQs and calls the exported
netpoll_poll_dev() -- exactly what __netpoll_send_skb() does after a failed
transmit. 20/20 unpatched boots hung; 20/20 with the fix survived and
reached
/init.
The bug was first noticed on a downstream, forked kernel: a
6.12.112-based tree
carrying out-of-tree iMAPx910 support (CONFIG_PREEMPT_NONE, HZ=100, single
CPU), booted under a custom QEMU model of that SoC
(hw/arm/imapx910-hs5.c + hw/net/imapx910-dwmac.c, which emulates deferred TX
completion via TX coalescing). Neither the SoC nor that machine model
exists in
mainline; that deployment simply hit the race first, and the bug is not
fork-specific -- it reproduces on mainline as described above. An unpatched
image stalled in 2/12 and 3/12 ordinary boots, immediately after the
built-in
extended netconsole console registered and replayed the printk backlog. The
discoverer's GDB sample decoded the interrupted task stack to
netpoll_poll_dev() -> stmmac_napi_poll_tx() -> stmmac_tx_clean() ->
__local_bh_enable_ip(), with napi->poll_owner == 0. No physical iMAPx910
hardware was involved, and no physical-hardware reproduction of this bug is
known.
All boots use QEMU npcm845-evb (-smp 4, guest constrained to one CPU via
nr_cpus=1 maxcpus=1), a mainline 7.4-rc5 arm64 build
(CONFIG_PREEMPT_DYNAMIC,
HZ=250, STMMAC_ETH=y, NETCONSOLE=y, NETPOLL=y, FRAME_POINTER=y), extended
netconsole enabled on the cmdline. "defer" is the local QEMU TX-completion
delay (NPCM_TX_DELAY_NS, ns):
Kernel defer boots result
mainline, unpatched 0 (stock) 12 12/12 booted
to /init
mainline, unpatched 10 us 12 8/12 hung
in netpoll_poll_lock
mainline, unpatched 50 us 12 10/12 hung
mainline, unpatched 100 us 12 11/12 hung
mainline, unpatched 200 us 20 17/20 hung
mainline + this fix 200 us 20 20/20 reached
/init
mainline + this fix 1 ms 12 12/12 reached
/init
deterministic trigger, unpatched 0 20 20/20 hung
deterministic trigger, with this fix 0 20 20/20 reached
/init
The fix drops the BH disable/enable from stmmac_tx_clean(), which is
only ever
called from the two NAPI poll callbacks. Those always run with BHs already
disabled -- softirq, the threaded-NAPI kthread (local_bh_disable() around
->poll()), or netpoll with hard IRQs off -- so the plain lock neither
allows a
softirq inside the callback nor changes any other path, and it is the same
plain lock that HARD_TX_LOCK and other poll callbacks already use. The fix
belongs in the driver rather than netpoll: netpoll legitimately runs a
driver
poll with the NAPI owned and IRQs off, and it is the driver's unlock helper
that re-enables BHs from inside that poll. No functional TX/RX change.
The same class of bug was fixed the same way in the USB CDC drivers, cdc_ncm
(commit 108ca34cf4b4, "net: usb: cdc_ncm: don't enable BHs in tx_fixup under
netpoll") and cdc_mbim: there a _bh unlock reached by netpoll with IRQs
off ran
softirqs inside the netpoll-held section, and the fix was to use the
plain lock
because every caller already has BHs or IRQs off.
Neither test aid is part of the submission: the QEMU npcm_gmac TX
deferral is
a local test-only change (not intended for upstream), and hs5_trigger.c is a
test harness only. The submitted patch touches only stmmac_main.c.
The Fixes: tag names the commit that introduced the offending lock
choice, in
line with the recent cdc_ncm/cdc_mbim fix, whose Fixes: likewise points
at the
driver commit that added the _bh lock to the affected function (for cdc_ncm,
c84ff1d6dff3). 8fce33317023 ("net: stmmac: Rework coalesce timer and fix
multi-queue races") did two things in one commit: it moved stmmac_tx_clean()
from the coalesce timer into the NAPI poll (__napi_schedule()), and
changed the
plain netif_tx_lock() to the BH-enabling __netif_tx_lock_bh(...). Before
it the
TX cleanup never ran in a netpoll-owned poll and used a non-BH lock, so the
self-deadlock was unreachable; after it the driver's poll re-enabled BHs
under
netpoll. That change is what this patch reverts the lock choice of. The
netpoll
side of the hazard is older than the current poll_owner implementation --
89c4b442b78b ("netpoll: more efficient locking") only replaced a
non-recursive
spin_lock with the cmpxchg() loop, and the same-CPU re-entry hangs
either way
-- so the driver-side commit is the sensible anchor rather than a netpoll
commit.
The change is two lines with byte-identical hunk context on every active
stable tree, so it applies as-is from 6.1.y up. Verified with git am, git
apply and GNU patch against v7.3.7, v6.18.66, v6.12.120, v6.6.163 and
v6.1.193 (each yields the tag plus exactly the two-line change; only the
line
offsets shift). The stmmac_tx_clean() signature on 6.1.y/6.6.y lacks the
newer
bool *pending_packets argument, but that signature appears only in the hunk
header, which patch tools ignore. Cc: stable at vger.kernel.org is set so the
stable team can pick it up.
This bug was found by an AI code review assistant (GPT-6.2-Sol, high
reasoning effort); its guardrails refused a follow-up request to help
prepare the report, so a second AI assistant, DeepSeek V4.1 Flash (high
reasoning), produced the source analysis, the reproducer, the arm64 build,
boot and GDB testing, and this write-up. I reviewed the change and its
evidence and certify it with the Signed-off-by in the patch.
The patch is the only artifact sent to the list. Supporting notes (a
deterministic reproducer and a source/policy audit), plus the out-of-tree
iMAPx910 Linux port and the QEMU machine and NIC models behind the original
observation, are available on request.
Signed-off-by: Luke Dashjr <luke-jr+git at utopios.org>
Luke Dashjr (1):
net: stmmac: use plain TX queue lock in the NAPI poll path
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
--
2.43.0
More information about the linux-arm-kernel
mailing list