BUG: soft lockup - CPU#1 stuck for 23s!

A CPU has been running kernel code for more than about twenty seconds without letting anything else run on it. The kernel’s lockup detector noticed and printed where that CPU is stuck.

The line

[  108.332014] watchdog: BUG: soft lockup - CPU#1 stuck for 23s! [kworker/1:2:214]
[  108.339581] Modules linked in: my_driver(O)
[  108.343799] CPU: 1 UID: 0 PID: 214 Comm: kworker/1:2 Tainted: G           O       6.12.0 #1
[  108.359920] pc : my_poll_status+0x1c/0x40 [my_driver]

Shortened and illustrative. The name in the brackets is the task that was on the CPU, with its PID. The hard variant reads Watchdog detected hard LOCKUP on cpu 1.

What it means

The kernel documentation defines a soft lockup as a bug that makes the kernel loop in kernel mode for more than 20 seconds without giving other tasks a chance to run, and a hard lockup as a CPU looping in kernel mode for more than 10 seconds without letting interrupts run. The soft threshold is twice watchdog_thresh, which is 10 seconds by default; that is why the first report usually says 22 or 23 seconds.

By default a soft lockup only prints the report and the system stays as it is, so a loop that never ends repeats the message with a growing time. With softlockup_panic set the kernel panics instead. Hard lockup detection needs a non-maskable interrupt source, which many embedded SoCs do not provide; on those, a CPU stuck with interrupts off produces no message at all, only a hang.

Common causes

A loop in kernel code that never yields

A driver waits for a hardware status bit in a tight loop with no timeout, or processes an unbounded amount of work in one go, on a kernel without full preemption. If the bit never changes, the CPU never leaves the loop. The pc line points straight into it, and it points to the same function in every report.

Sources: Linux kernel docs: Softlockup detector and hardlockup detector (aka nmi_watchdog) · Linux kernel docs: /proc/sys/kernel (watchdog_thresh, softlockup_panic, softlockup_all_cpu_backtrace)

A deadlock on a spinlock

Two paths take the same spinlocks in opposite order, or code takes a lock it already holds. The CPU spins waiting for the lock, and the call trace ends in the spinlock slow path. If interrupts were disabled when it started spinning, this is a hard lockup, and it is reported only where the hard lockup detector is available.

Source: Linux kernel docs: Softlockup detector and hardlockup detector (aka nmi_watchdog)

An interrupt storm

A level-triggered interrupt that its handler does not clear fires again as soon as the handler returns, and the CPU spends all its time in interrupt context. Linux v6.12 can count interrupts in the soft lockup report (CONFIG_SOFTLOCKUP_DETECTOR_INTR_STORM) to show which line is responsible; without that option, /proc/interrupts read twice from another CPU shows the counter racing.

Source: Linux v6.12: kernel/watchdog.c

What to check first

  1. Read the bracket at the end of the line: it names the task that was on the CPU and its PID. A kernel worker or an interrupt thread of your driver narrows it down at once.
  2. Read the pc line and the call trace below it. If every report shows the same function, that function contains the loop; if the trace ends in a spinlock function, look for who holds the lock.
  3. Watch whether the time grows (23s, 49s, 75s). A growing time is a loop that never ends; a single report is a long but finite stall.
  4. Get the other CPUs’ view. Set kernel.softlockup_all_cpu_backtrace=1, or send SysRq l over the serial console (a BREAK, then the letter), to print a backtrace of every active CPU.
  5. Give every hardware wait in the driver a timeout, using the readx_poll_timeout() family in place of an open while loop, and call cond_resched() in long loops that are allowed to sleep.
  6. On an unattended board, set softlockup_panic=1 together with panic= on the command line so that a lockup ends in a reboot with a log, not in a silent hang.

In NeuroTerm

On a serial tab with pattern markers on, NeuroTerm recognises this line as “Board crash: Linux soft lockup”. A coding agent connected through Terminal Use gets that name in its list of tabs and among the markers of the output it reads, with a cursor for reading the lines around it. Setup is in Terminal Use (MCP).