Skip to content

Commit 00de977

Browse files
jhovoldgregkh
authored andcommitted
serial: core: fix transmit-buffer reset and memleak
Commit 761ed4a ("tty: serial_core: convert uart_close to use tty_port_close") converted serial core to use tty_port_close() but failed to notice that the transmit buffer still needs to be freed on final close. Not freeing the transmit buffer means that the buffer is no longer cleared on next open so that any ioctl() waiting for the buffer to drain might wait indefinitely (e.g. on termios changes) or that stale data can end up being transmitted in case tx is restarted. Furthermore, the buffer of any port that has been opened would leak on driver unbind. Note that the port lock is held when clearing the buffer pointer due to the ldisc race worked around by commit a5ba1d9 ("uart: fix race between uart_put_char() and uart_shutdown()"). Also note that the tty-port shutdown() callback is not called for console ports so it is not strictly necessary to free the buffer page after releasing the lock (cf. d724021 ("tty/serial: do not free trasnmit buffer page under port lock")). Link: https://lore.kernel.org/r/319321886d97c456203d5c6a576a5480d07c3478.1635781688.git.baruch@tkos.co.il Fixes: 761ed4a ("tty: serial_core: convert uart_close to use tty_port_close") Cc: [email protected] # 4.9 Cc: Rob Herring <[email protected]> Reported-by: Baruch Siach <[email protected]> Tested-by: Baruch Siach <[email protected]> Signed-off-by: Johan Hovold <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent b348399 commit 00de977

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

drivers/tty/serial/serial_core.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1549,6 +1549,7 @@ static void uart_tty_port_shutdown(struct tty_port *port)
15491549
{
15501550
struct uart_state *state = container_of(port, struct uart_state, port);
15511551
struct uart_port *uport = uart_port_check(state);
1552+
char *buf;
15521553

15531554
/*
15541555
* At this point, we stop accepting input. To do this, we
@@ -1570,8 +1571,18 @@ static void uart_tty_port_shutdown(struct tty_port *port)
15701571
*/
15711572
tty_port_set_suspended(port, 0);
15721573

1573-
uart_change_pm(state, UART_PM_STATE_OFF);
1574+
/*
1575+
* Free the transmit buffer.
1576+
*/
1577+
spin_lock_irq(&uport->lock);
1578+
buf = state->xmit.buf;
1579+
state->xmit.buf = NULL;
1580+
spin_unlock_irq(&uport->lock);
15741581

1582+
if (buf)
1583+
free_page((unsigned long)buf);
1584+
1585+
uart_change_pm(state, UART_PM_STATE_OFF);
15751586
}
15761587

15771588
static void uart_wait_until_sent(struct tty_struct *tty, int timeout)

0 commit comments

Comments
 (0)