{bp-19014} driver/serial: Fix/uart tcdrain bugs#19081
Open
jerpelea wants to merge 2 commits into
Open
Conversation
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>
lupyuen
approved these changes
Jun 10, 2026
xiaoxiang781216
approved these changes
Jun 10, 2026
acassis
approved these changes
Jun 10, 2026
linguini1
approved these changes
Jun 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
uart_tcdrain() has two latent bugs in its timeout handling:
This series fixes both:
Impact
RELEASE
Testing
CI