Linux Serial subsystem development
 help / color / mirror / Atom feed
* [RFC PATCH] tty: flush buffer on signal interrupted tty close
@ 2021-11-01 15:48 Baruch Siach
  2021-11-08  8:58 ` Johan Hovold
  0 siblings, 1 reply; 3+ messages in thread
From: Baruch Siach @ 2021-11-01 15:48 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Jiri Slaby; +Cc: linux-serial, Baruch Siach

When a signal interrupts tty_wait_until_sent() on a UART device, there
might be data left on the xmit buffer that the UART will never transmit.
This causes set_termios() to wait forever in tty_wait_until_sent().

The hang reproduces easily on my system that is equipped with DesignWare
8250. Run

  while true; do echo -ne 0123456789abcdef01 > /dev/ttyS1; done

Hit Ctrl-C to interrupt the loop. When data is left in the struct
uart_state xmit buffer, the following command hangs:

  stty -F /dev/ttyS1 raw -echo

Call tty_driver_flush_buffer() on signal interrupted wait, to discard
data from the xmit buffer.

Signed-off-by: Baruch Siach <baruch@tkos.co.il>
---
 drivers/tty/tty_ioctl.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/tty/tty_ioctl.c b/drivers/tty/tty_ioctl.c
index 507a25d692bb..0bf6fad7ecb3 100644
--- a/drivers/tty/tty_ioctl.c
+++ b/drivers/tty/tty_ioctl.c
@@ -203,6 +203,8 @@ void tty_wait_until_sent(struct tty_struct *tty, long timeout)
 
 	timeout = wait_event_interruptible_timeout(tty->write_wait,
 			!tty_chars_in_buffer(tty), timeout);
+	if (timeout == -ERESTARTSYS && tty->closing)
+		tty_driver_flush_buffer(tty);
 	if (timeout <= 0)
 		return;
 
-- 
2.33.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [RFC PATCH] tty: flush buffer on signal interrupted tty close
  2021-11-01 15:48 [RFC PATCH] tty: flush buffer on signal interrupted tty close Baruch Siach
@ 2021-11-08  8:58 ` Johan Hovold
  2021-11-08  9:00   ` Johan Hovold
  0 siblings, 1 reply; 3+ messages in thread
From: Johan Hovold @ 2021-11-08  8:58 UTC (permalink / raw)
  To: Baruch Siach; +Cc: Greg Kroah-Hartman, Jiri Slaby, linux-serial

On Mon, Nov 01, 2021 at 05:48:08PM +0200, Baruch Siach wrote:
> When a signal interrupts tty_wait_until_sent() on a UART device, there
> might be data left on the xmit buffer that the UART will never transmit.
> This causes set_termios() to wait forever in tty_wait_until_sent().
> 
> The hang reproduces easily on my system that is equipped with DesignWare
> 8250. Run
> 
>   while true; do echo -ne 0123456789abcdef01 > /dev/ttyS1; done
> 
> Hit Ctrl-C to interrupt the loop. When data is left in the struct
> uart_state xmit buffer, the following command hangs:
> 
>   stty -F /dev/ttyS1 raw -echo

Thanks for reporting this. It should not be possible for there to be
data left in the buffer and indeed we do have a long-standing regression
here since the serdev work five years ago. I'm a bit surprised no one
has noticed and reported this for that long.

> Call tty_driver_flush_buffer() on signal interrupted wait, to discard
> data from the xmit buffer.
>
> Signed-off-by: Baruch Siach <baruch@tkos.co.il>
> ---
>  drivers/tty/tty_ioctl.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/drivers/tty/tty_ioctl.c b/drivers/tty/tty_ioctl.c
> index 507a25d692bb..0bf6fad7ecb3 100644
> --- a/drivers/tty/tty_ioctl.c
> +++ b/drivers/tty/tty_ioctl.c
> @@ -203,6 +203,8 @@ void tty_wait_until_sent(struct tty_struct *tty, long timeout)
>  
>  	timeout = wait_event_interruptible_timeout(tty->write_wait,
>  			!tty_chars_in_buffer(tty), timeout);
> +	if (timeout == -ERESTARTSYS && tty->closing)
> +		tty_driver_flush_buffer(tty);
>  	if (timeout <= 0)
>  		return;

This is however not the right fix since the problem isn't limited to
interrupted waits and in any case should not be handled in
tty_wait_until_sent().

I can reproduce the problem and have posted a fix here:

	https://lore.kernel.org/r/20211108083856.5261-1-johan@kernel.org

Johan

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [RFC PATCH] tty: flush buffer on signal interrupted tty close
  2021-11-08  8:58 ` Johan Hovold
@ 2021-11-08  9:00   ` Johan Hovold
  0 siblings, 0 replies; 3+ messages in thread
From: Johan Hovold @ 2021-11-08  9:00 UTC (permalink / raw)
  To: Baruch Siach; +Cc: Greg Kroah-Hartman, Jiri Slaby, linux-serial

On Mon, Nov 08, 2021 at 09:58:08AM +0100, Johan Hovold wrote:
> On Mon, Nov 01, 2021 at 05:48:08PM +0200, Baruch Siach wrote:

> I can reproduce the problem and have posted a fix here:
> 
> 	https://lore.kernel.org/r/20211108083856.5261-1-johan@kernel.org

Sorry, that should have been:

	https://lore.kernel.org/r/20211108085431.12637-1-johan@kernel.org

Johan

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-11-08  9:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-11-01 15:48 [RFC PATCH] tty: flush buffer on signal interrupted tty close Baruch Siach
2021-11-08  8:58 ` Johan Hovold
2021-11-08  9:00   ` Johan Hovold

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox