Skip to content

arch/xtensa/esp32: fix double spinlock unlock in esp32_txint - #20769

Merged
acassis merged 1 commit into
apache:masterfrom
Swatantra-66:fix/esp32-serial-double-unlock
Oct 18, 2026
Merged

acassis merged 1 commit into
apache:masterfrom
Swatantra-66:fix/esp32-serial-double-unlock

Conversation

@Swatantra-66

Copy link
Copy Markdown
Contributor

Summary

Resolves #14594.

In esp32_txint() (arch/xtensa/src/esp32/esp32_serial.c), when CONFIG_SUPPRESS_SERIAL_INTS was active, spin_unlock_irqrestore(&priv->lock, flags) was called inside the if (enable) block before invoking uart_xmitchars(dev). The execution then exited the if (enable) block and hit an unconditional spin_unlock_irqrestore(&priv->lock, flags) at the bottom of the function, causing consecutive unlocks on the same lock (issue #14594).

This PR restructures esp32_txint() so that:

  1. The spinlock is held across register manipulation in both if (enable) and else branches and released exactly once at the end of the critical section.
  2. Under CONFIG_SUPPRESS_SERIAL_INTS, uart_xmitchars(dev) is called safely after priv->lock is released, preventing recursive spinlock deadlocks when uart_xmitchars calls back into txint(dev, false).
  3. Updated the comment to accurately document that uart_xmitchars() runs after the spinlock is released.

Impact

  • ESP32 SMP & Debugging: Prevents kernel panics and CPU interrupt state corruption caused by unlocking an already-unlocked spinlock.
  • Concurrency: Eliminates deadlock risk during recursive upper-half TX character processing.

Testing

  • Verified with ./tools/checkpatch.sh -u HEAD (0 errors, 0 warnings).
  • Verified with ./tools/nxstyle on arch/xtensa/src/esp32/esp32_serial.c (0 errors, 0 warnings).

@github-actions github-actions Bot added Arch: xtensa Issues related to the Xtensa architecture Size: S The size of the change in this PR is small labels Oct 18, 2026
In esp32_txint(), when CONFIG_SUPPRESS_SERIAL_INTS was enabled,
spin_unlock_irqrestore(&priv->lock, flags) was invoked inside the
if (enable) block before calling uart_xmitchars(dev), and then
called a second time unconditionally after the if/else block.

Restructure esp32_txint() so that the spinlock is released exactly
once across all execution paths, and call uart_xmitchars(dev)
safely after releasing priv->lock to prevent recursive deadlocks.

Resolves apache#14594.

Signed-off-by: Swatantra Yadav <[email protected]>
@Swatantra-66
Swatantra-66 force-pushed the fix/esp32-serial-double-unlock branch from fcd1f2b to 59fa92a Compare October 18, 2026 09:14
@github-actions github-actions Bot added Size: M The size of the change in this PR is medium and removed Size: S The size of the change in this PR is small labels Oct 18, 2026
@github-actions

Copy link
Copy Markdown

MemBrowse Memory Report

No memory changes detected for:

@acassis
acassis merged commit dcd135d into apache:master Oct 18, 2026
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Arch: xtensa Issues related to the Xtensa architecture Size: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] double unlock in esp32_serial.c

3 participants