Skip to content

{bp-19014} driver/serial: Fix/uart tcdrain bugs#19081

Open
jerpelea wants to merge 2 commits into
apache:releases/13.0from
jerpelea:bp-19014
Open

{bp-19014} driver/serial: Fix/uart tcdrain bugs#19081
jerpelea wants to merge 2 commits into
apache:releases/13.0from
jerpelea:bp-19014

Conversation

@jerpelea

Copy link
Copy Markdown
Contributor

Summary

uart_tcdrain() has two latent bugs in its timeout handling:

Cancellation point leak on timeout. uart_tcdrain() enters a
cancellation point on entry (when cancelable=true) and the normal exit
path calls leave_cancellation_point(). The early return -ETIMEDOUT
from inside the FIFO-empty polling loop bypasses this, leaving
tcb->cpcount permanently incremented. Repeated timeouts desync the
counter and break subsequent pthread_cancel() /
pthread_setcancelstate() semantics for the calling thread.
Caller-supplied timeout does not bound the xmit-buffer drain. The
timeout argument (e.g. 10 * TICK_PER_SEC from the TCDRN ioctl
path) was only honored by the final FIFO polling loop. The earlier
xmit-buffer drain loop called nxsem_wait(&dev->xmitsem) with no
timeout, so any condition that prevents the lower half from posting
xmitsem (a stuck DMA-completion path, a wedged hardware-flow-control
stall, etc.) would block tcdrain() indefinitely regardless of the
timeout the caller asked for. The pre-existing comment ("NOTE: There
is no timeout on the following loop. ... the caller should call
tcflush() first") openly acknowledged this hang.

This series fixes both:

Patch 1 moves the missing leave_cancellation_point() onto the
timeout return path so cancellation-point nesting stays balanced.
Patch 2 takes a single timestamp on entry, replaces the bare
nxsem_wait() with nxsem_tickwait() using the remaining time, and
short-circuits to -ETIMEDOUT when the caller's budget is already
exhausted. The caller's timeout now bounds the total time spent in
uart_tcdrain() regardless of which phase stalls.

Impact

RELEASE

Testing

CI

LingaoM added 2 commits June 10, 2026 10:03
uart_tcdrain() registers a cancellation point on entry via
enter_cancellation_point() (when called with cancelable=true), and the
normal exit path calls leave_cancellation_point() before returning.

However the timeout path inside the FIFO drain loop returns -ETIMEDOUT
directly without going through the normal exit path, leaking one
cancellation point reference (tcb->cpcount is left incremented).  Over
repeated timeouts this counter will desync and prevent
pthread_cancel() / pthread_setcancelstate() from behaving correctly
for the calling thread.

Fix by calling leave_cancellation_point() before the early return,
matching the existing exit path.

Signed-off-by: Lingao Meng <menglingao@xiaomi.com>
uart_tcdrain() takes a caller-supplied timeout (e.g. 10s from the
TCDRN ioctl path), but the timeout was only applied to the final
TX-FIFO polling loop.  The earlier xmit-buffer drain loop called
nxsem_wait(&dev->xmitsem) with no timeout, so any condition that
prevents the lower half from posting xmitsem (e.g. a stuck DMA
completion path, a wedged hardware-flow-control stall) would block
tcdrain() indefinitely, regardless of the timeout the caller asked
for.  The pre-existing comment ("NOTE: There is no timeout on the
following loop. ... the caller should call tcflush() first") openly
acknowledged this hang.

Move the start timestamp before both phases and replace the bare
nxsem_wait() with nxsem_tickwait() using the remaining time, so the
total time spent in tcdrain() honors the caller's timeout regardless
of which phase stalls.  When the remaining time is already exhausted,
short-circuit to -ETIMEDOUT without calling into the scheduler.  The
existing exit path (drop critical section, skip the FIFO polling
loop, unlock the xmit mutex, leave the cancellation point) handles
the new -ETIMEDOUT propagation correctly without further changes.

Also fold the "Set up for the timeout" comment into the kludge
REVISIT comment, since the timestamp is no longer set up at that
point.

Signed-off-by: Lingao Meng <menglingao@xiaomi.com>
@jerpelea jerpelea requested review from acassis, anchao, cederom, linguini1, lupyuen and xiaoxiang781216 and removed request for xiaoxiang781216 June 10, 2026 08:05
@github-actions github-actions Bot added Area: Drivers Drivers issues Size: S The size of the change in this PR is small labels Jun 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Drivers Drivers issues Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants