Re: [PATCH 2/2] serial: core: do not touch the port after dropping the last reference

From: Johan Hovold

Date: Wed Nov 11 2026 - 09:56:19 EST


On Wed, Nov 04, 2026 at 10:27:16AM +0000, Tapio Reijonen wrote:
> uart_port_deref() decrements the reference count through uport->state
> and then reads uport->state again for the wake_up(). The last
> decrement is what lets serial_core_remove_one_port() return, and the
> driver may free its struct uart_port right after that. A reference
> holder that drops the last reference can therefore read the freed port
> on its way to the wake_up().
>
> Read the state pointer once, before the decrement. The uart_state
> belongs to the uart_driver and lives until uart_unregister_driver(),
> so the wake_up() target outlives the port. Drivers that unregister the
> uart_driver in the same remove() are a separate matter and not changed
> here.
>
> Found by review of the removal path while fixing the deadlock between
> port removal and a blocked tcdrain(); not reproduced.
>
> Fixes: 9ed19428a51d ("serial: core: Prevent unsafe uart port access, part 2")
> Cc: stable@xxxxxxxxxxxxxxx
> Signed-off-by: Tapio Reijonen <tapio.reijonen@xxxxxxxxxxx>

Also unlikely to ever be an issue, but looks correct nonetheless:

Reviewed-by: Johan Hovold <johan@xxxxxxxxxx>

Johan