Skip to contentSkip to conversation

tty: serial: possible deadlock in uart_remove_one_port() and uart_hangup()

BUG Jia-Ju Bai 2 participants 5 messages Jan 29, 2022 – Feb 5, 2022 1 message not in the archive TTY layer and serial drivers

lore
Share & tools
lore

Jia-Ju Bai

author
#0 lore raw

Hello,

My static analysis tool reports a possible deadlock in the tty driver in Linux 5.10:

uart_remove_one_port()
  mutex_lock(&port->mutex); --> Line 3017 (Lock A) wait_event(state->remove_wait, ...); --> Line 3019 (Wait X) mutex_unlock(&port->mutex); --> Line 3021 (Unlock A)

uart_hangup()
  mutex_lock(&port->mutex); --> Line 1667 (Lock A) uart_flush_buffer()
    uart_port_unlock()
      uart_port_deref() wake_up(&uport->state->remove_wait); --> Line 68 (Wake X) mutex_unlock(&port->mutex); --> Line 1684 (Unlock A)

When uart_remove_one_port() is executed, "Wait X" is performed by holding "Lock A". If uart_hangup() is executed at this time, "Wake X" cannot be performed to wake up "Wait X" in uart_remove_one_port(), because "Lock A" has been already hold by uart_remove_one_port(), causing a possible deadlock.

I am not quite sure whether this possible problem is real and how to fix it if it is real. Maybe we can call wait_event() before mutex_lock() in uart_remove_one_port(). Any feedback would be appreciated, thanks :)

Best wishes,
Jia-Ju Bai

maintainer
#1 lore raw
Quoted 4 lines · Jia-Ju Bai

Hello,

My static analysis tool reports a possible deadlock in the tty driver in Linux 5.10:

5.10 was released over a year ago and over 100 thousand changes ago. Please redo your check on 5.16 at the oldest.

thanks,

greg k-h

author
#2 lore raw
Quoted 7 lines · Greg KH
Quoted 4 lines · Jia-Ju Bai

Hello,

My static analysis tool reports a possible deadlock in the tty driver in Linux 5.10:

5.10 was released over a year ago and over 100 thousand changes ago. Please redo your check on 5.16 at the oldest.

Thanks, Greg.
I will redo my check on 5.16.

Best wishes,
Jia-Ju Bai

author
#3 lore raw
In reply to Greg KH
Quoted 7 lines · Greg KH
Quoted 4 lines · Jia-Ju Bai

Hello,

My static analysis tool reports a possible deadlock in the tty driver in Linux 5.10:

5.10 was released over a year ago and over 100 thousand changes ago. Please redo your check on 5.16 at the oldest.

My static analysis tool checks the tty driver in Linux 5.16, and also finds this possible deadlock:

uart_remove_one_port()
  mutex_lock(&port->mutex); --> Line 3032 (Lock A) wait_event(state->remove_wait, ...); --> Line 3034 (Wait X) mutex_unlock(&port->mutex); --> Line 3036 (Unlock A)

uart_hangup()
  mutex_lock(&port->mutex); --> Line 1669 (Lock A) uart_flush_buffer()
    uart_port_unlock()
      uart_port_deref() wake_up(&uport->state->remove_wait); --> Line 68 (Wake X) mutex_unlock(&port->mutex); --> Line 1686 (Unlock A)

When uart_remove_one_port() is executed, "Wait X" is performed by holding "Lock A". If uart_hangup() is executed at this time, "Wake X" cannot be performed to wake up "Wait X" in uart_remove_one_port(), because "Lock A" has been already hold by uart_remove_one_port(), causing a possible deadlock.

I am not quite sure whether this possible problem is real and how to fix it if it is real. Maybe we can call wait_event() before mutex_lock() in uart_remove_one_port(). Any feedback would be appreciated, thanks :)

Best wishes,
Jia-Ju Bai

A message that is not in the archive (sent off-list or not yet received): [email protected]

author
#5 lore raw
Quoted 40 lines · Hillf Danton
Quoted 37 lines · On Tue, 1 Feb 2022 14:51:09 +0800 Jia-Ju Bai wrote:
Quoted 7 lines · Greg KH
Quoted 4 lines · Jia-Ju Bai

Hello,

My static analysis tool reports a possible deadlock in the tty driver in Linux 5.10:

5.10 was released over a year ago and over 100 thousand changes ago. Please redo your check on 5.16 at the oldest.

My static analysis tool checks the tty driver in Linux 5.16, and also finds this possible deadlock:

uart_remove_one_port()
  mutex_lock(&port->mutex); --> Line 3032 (Lock A) wait_event(state->remove_wait, ...); --> Line 3034 (Wait X) mutex_unlock(&port->mutex); --> Line 3036 (Unlock A)

uart_hangup()
  mutex_lock(&port->mutex); --> Line 1669 (Lock A) uart_flush_buffer()
    uart_port_unlock()
      uart_port_deref() wake_up(&uport->state->remove_wait); --> Line 68 (Wake X) mutex_unlock(&port->mutex); --> Line 1686 (Unlock A)

When uart_remove_one_port() is executed, "Wait X" is performed by holding "Lock A". If uart_hangup() is executed at this time, "Wake X" cannot be performed to wake up "Wait X" in uart_remove_one_port(), because "Lock A" has been already hold by uart_remove_one_port(), causing a possible deadlock.

I am not quite sure whether this possible problem is real and how to fix it if it is real.
Maybe we can call wait_event() before mutex_lock() in uart_remove_one_port().
Any feedback would be appreciated, thanks :)

Quoted 2 lines

Best wishes,
Jia-Ju Bai

Quoted 8 lines

Hey Jia-Ju

Thank you for reporting it.

In uart_flush_buffer(), uart_port_unlock() pairs with uart_port_lock() which bumps refcount up. OTOH no wakep is needed without refcount incremented, so the wakeup above in the hangup path is not waited for in the remove path.

Hi Hillf,

Thanks for the explanation :)
So I wonder which wait_event() can be paired with wake_up(&uport->state->remove_wait) in uart_port_deref()?

Best wishes,
Jia-Ju Bai