Skip to contentSkip to conversation

tty: Fix low_latency BUG

PATCH In review Peter Hurley 4 participants 7 messages Feb 22, 2014 – Feb 27, 2014

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tty: Fix low_latency BUG

The user-settable knob, low_latency, has been the source of several BUG reports which stem from flush_to_ldisc() running in interrupt context. Since 3.12, which added several sleeping locks (termios_rwsem and buf->lock) to the input processing path, the frequency of these BUG reports has increased.

Note that changes in 3.12 did not introduce this regression; sleeping locks were first added to the input processing path with the removal of the BKL from N_TTY in commit a88a69c91256418c5907c2f1f8a0ec0a36f9e6cc,
'n_tty: Fix loss of echoed characters and remove bkl from n_tty' and later in commit 38db89799bdf11625a831c5af33938dcb11908b6, 'tty: throttling race fix'. Since those changes, executing flush_to_ldisc() in interrupt_context (ie, low_latency set), is unsafe.

However, since most devices do not validate if the low_latency setting is appropriate for the context (process or interrupt) in which they receive data, some reports are due to misconfiguration. Further, serial dma devices for which dma fails, resort to interrupt receiving as a backup without resetting low_latency.

Historically, low_latency was used to force wake-up the reading process rather than wait for the next scheduler tick. The effect was to trim multiple milliseconds of latency from when the process would receive new data.

Recent tests [1] have shown that the reading process now receives data with only 10's of microseconds latency without low_latency set.

Remove the low_latency rx steering from tty_flip_buffer_push(); however, leave the knob as an optional hint to drivers that can tune their rx fifos and such like. Cleanup stale code comments regarding low_latency.

[1] https://lkml.org/lkml/2014/2/20/434

Cc Grant Edwards <[email protected]>
Cc Stanislaw Gruszka <[email protected]>
Cc Hal Murray <[email protected]>
Cc Alan Cox <[email protected]>
Cc <[email protected]> # 3.12.x+
---
 drivers/tty/ipwireless/tty.c  |  3 ---
 drivers/tty/tty_buffer.c      | 20 ++++----------------
 drivers/usb/gadget/u_serial.c |  4 ++--
 include/linux/tty.h           |  2 +-
 4 files changed, 7 insertions(+), 22 deletions(-)
drivers/tty/ipwireless/tty.c +0 -3
index ebd5bff..17ee3bf 100644
@@ -176,9 +176,6 @@ void ipwireless_tty_received(struct ipw_tty *tty, unsigned char *data,
 				": %d chars not inserted to flip buffer!\n",
 				length - work);
 
-	/*
-	 * This may sleep if ->low_latency is set
-	 */
 	if (work)
 		tty_flip_buffer_push(&tty->port);
 }
drivers/tty/tty_buffer.c +4 -16
index 765125d..8ebd9f8 100644
@@ -351,14 +351,11 @@ EXPORT_SYMBOL(tty_insert_flip_string_flags);
  *	Takes any pending buffers and transfers their ownership to the
  *	ldisc side of the queue. It then schedules those characters for
  *	processing by the line discipline.
- *	Note that this function can only be used when the low_latency flag
- *	is unset. Otherwise the workqueue won't be flushed.
  */
 
 void tty_schedule_flip(struct tty_port *port)
 {
 	struct tty_bufhead *buf = &port->buf;
-	WARN_ON(port->low_latency);
 
 	buf->tail->commit = buf->tail->used;
 	schedule_work(&buf->work);
@@ -482,17 +479,15 @@ static void flush_to_ldisc(struct work_struct *work)
  */
 void tty_flush_to_ldisc(struct tty_struct *tty)
 {
-	if (!tty->port->low_latency)
-		flush_work(&tty->port->buf.work);
+	flush_work(&tty->port->buf.work);
 }
 
 /**
  *	tty_flip_buffer_push	-	terminal
  *	@port: tty port to push
  *
- *	Queue a push of the terminal flip buffers to the line discipline. This
- *	function must not be called from IRQ context if port->low_latency is
- *	set.
+ *	Queue a push of the terminal flip buffers to the line discipline.
+ *	Can be called from IRQ/atomic context.
  *
  *	In the event of the queue being busy for flipping the work will be
  *	held off and retried later.
@@ -500,14 +495,7 @@ void tty_flush_to_ldisc(struct tty_struct *tty)
 
 void tty_flip_buffer_push(struct tty_port *port)
 {
-	struct tty_bufhead *buf = &port->buf;
-
-	buf->tail->commit = buf->tail->used;
-
-	if (port->low_latency)
-		flush_to_ldisc(&buf->work);
-	else
-		schedule_work(&buf->work);
+	tty_schedule_flip(port);
 }
 EXPORT_SYMBOL(tty_flip_buffer_push);
 
drivers/usb/gadget/u_serial.c +2 -2
index b369292..ad0aca8 100644
@@ -549,8 +549,8 @@ static void gs_rx_push(unsigned long _port)
 		port->read_started--;
 	}
 
-	/* Push from tty to ldisc; without low_latency set this is handled by
-	 * a workqueue, so we won't get callbacks and can hold port_lock
+	/* Push from tty to ldisc; this is handled by a workqueue,
+	 * so we won't get callbacks and can hold port_lock
 	 */
 	if (do_push)
 		tty_flip_buffer_push(&port->port);
include/linux/tty.h +1 -1
index 4781d7b..1c3316a 100644
@@ -208,7 +208,7 @@ struct tty_port {
 	wait_queue_head_t	delta_msr_wait;	/* Modem status change */
 	unsigned long		flags;		/* TTY flags ASY_*/
 	unsigned char		console:1,	/* port is a console */
-				low_latency:1;	/* direct buffer flush */
+				low_latency:1;	/* optional: tune for latency */
 	struct mutex		mutex;		/* Locking */
 	struct mutex		buf_mutex;	/* Buffer alloc lock */
 	unsigned char		*xmit_buf;	/* Optional buffer */
Signature · 1 line
1.8.1.2
#1 lore raw
Quoted 7 lines

Remove the low_latency rx steering from tty_flip_buffer_push(); however, leave the knob as an optional hint to drivers that can tune their rx fifos and such like. Cleanup stale code comments regarding low_latency.

[1] https://lkml.org/lkml/2014/2/20/434

Quoted 8 lines
Cc Grant Edwards <[email protected]>
Cc Stanislaw Gruszka <[email protected]>
Cc Hal Murray <[email protected]>
Cc Alan Cox <[email protected]>
Cc <[email protected]> # 3.12.x+

Yay.. thats an annoying historical pain in the butt gone.

Acked-by on v1 1/1
#2 lore raw
In reply to Peter Hurley

drivers/tty/ipwireless/tty.c void ipwireless_tty_received(struct ipw_tty *tty, unsigned char *data, · line 181

Quoted 9 lines · Peter Hurley
drivers/tty/ipwireless/tty.c +0 -3
@@ -176,9 +176,6 @@ void ipwireless_tty_received(struct ipw_tty *tty, unsigned char *data,
 				": %d chars not inserted to flip buffer!\n",
 				length - work);
 
-	/*
-	 * This may sleep if ->low_latency is set
-	 */

This is a no-op change for the ipwireless driver itself, nevertheless here goes the maintainer's

Quoted 3 lines
drivers/tty/ipwireless/tty.c +0 -0
 	if (work)
 		tty_flip_buffer_push(&tty->port);
 }
#3 lore raw
In reply to Peter Hurley

Hi Peter,

2014-02-22 20:31 GMT+08:00 Peter Hurley <[email protected]>:

Quoted 21 lines

The user-settable knob, low_latency, has been the source of several BUG reports which stem from flush_to_ldisc() running in interrupt context. Since 3.12, which added several sleeping locks (termios_rwsem and buf->lock) to the input processing path, the frequency of these BUG reports has increased.

Note that changes in 3.12 did not introduce this regression; sleeping locks were first added to the input processing path with the removal of the BKL from N_TTY in commit a88a69c91256418c5907c2f1f8a0ec0a36f9e6cc,
'n_tty: Fix loss of echoed characters and remove bkl from n_tty' and later in commit 38db89799bdf11625a831c5af33938dcb11908b6, 'tty: throttling race fix'. Since those changes, executing flush_to_ldisc() in interrupt_context (ie, low_latency set), is unsafe.

However, since most devices do not validate if the low_latency setting is appropriate for the context (process or interrupt) in which they receive data, some reports are due to misconfiguration. Further, serial dma devices for which dma fails, resort to interrupt receiving as a backup without resetting low_latency.

Quoted 7 lines

Historically, low_latency was used to force wake-up the reading process rather than wait for the next scheduler tick. The effect was to trim multiple milliseconds of latency from when the process would receive new data.

Recent tests [1] have shown that the reading process now receives data with only 10's of microseconds latency without low_latency set.

The 10's of miscroseconds is ok for 115200 bps like device, but it may hurt the high speed device like Bluetooth which runs at 3M/4M bps or higher.

More and more smartphones are using uart as the Bluetooth data interface due to its low-pin, low-power feature, and many of them are using HZ=100 kernel, I'm afraid this added delay may cause some problem.

Thanks,
Feng

#4 lore raw

[ +cc linux-bluetooth ]

Hi Feng,

Quoted 29 lines · Feng Tang

Hi Peter,

2014-02-22 20:31 GMT+08:00 Peter Hurley <[email protected]>:

Quoted 26 lines

The user-settable knob, low_latency, has been the source of several BUG reports which stem from flush_to_ldisc() running in interrupt context. Since 3.12, which added several sleeping locks (termios_rwsem and buf->lock) to the input processing path, the frequency of these BUG reports has increased.

Note that changes in 3.12 did not introduce this regression; sleeping locks were first added to the input processing path with the removal of the BKL from N_TTY in commit a88a69c91256418c5907c2f1f8a0ec0a36f9e6cc,
'n_tty: Fix loss of echoed characters and remove bkl from n_tty' and later in commit 38db89799bdf11625a831c5af33938dcb11908b6, 'tty: throttling race fix'. Since those changes, executing flush_to_ldisc() in interrupt_context (ie, low_latency set), is unsafe.

However, since most devices do not validate if the low_latency setting is appropriate for the context (process or interrupt) in which they receive data, some reports are due to misconfiguration. Further, serial dma devices for which dma fails, resort to interrupt receiving as a backup without resetting low_latency.

Historically, low_latency was used to force wake-up the reading process rather than wait for the next scheduler tick. The effect was to trim multiple milliseconds of latency from when the process would receive new data.

Quoted 6 lines
Quoted 2 lines

Recent tests [1] have shown that the reading process now receives data with only 10's of microseconds latency without low_latency set.

The 10's of miscroseconds is ok for 115200 bps like device, but it may hurt the high speed device like Bluetooth which runs at 3M/4M bps or higher.

The tests were run at 400Mbps, so 3Mbps or 4Mbps is not a problem (but I think you may be confusing throughput with latency).

FWIW, two things affected the latency times of those particular tests;
1) the kernel firewire subsystem handles rx data in a tasklet (so not directly from IRQ) which negatively affected the latency reported, and
2) the ftrace instrumentation is not free and there are several traces per rx.

If you look carefully at the test trace data, you'll see that the timestamps from tty_flip_buffer_push() of the rx data to n_tty_write() of the response averages _~11us_; this is the measured latency from tty driver receiving the rx data to reading of that data *and* the process response (which comes back up through several tty locks).

Naturally, in mainline kernel, the scheduler load will affect the measured latency *but that's true regardless of low_latency rx steering because the user-space process must still be woken to complete the read*.

Quoted 4 lines

More and more smartphones are using uart as the Bluetooth data interface due to its low-pin, low-power feature, and many of them are using HZ=100 kernel, I'm afraid this added delay may cause some problem.

Some hard data showing a real problem would help further this discussion; my belief is that 3.12+ w/o low_latency rx steering will outperform 3.11- w/ low_latency rx steering in every test.

I'm glad to hear that the Bluetooth uart interface is getting some use; that means someone will soon be fixing the hard lockup in hci_uart_tx_wakeup() reported here:

http://www.spinics.net/lists/linux-serial/msg11529.html

Regards,
Peter Hurley

#5 lore raw

Hi Peter,

2014-02-26 23:40 GMT+08:00 Peter Hurley <[email protected]>:

Quoted 12 lines

[ +cc linux-bluetooth ]

Quoted 10 lines
Quoted 8 lines

Historically, low_latency was used to force wake-up the reading process rather than wait for the next scheduler tick. The effect was to trim multiple milliseconds of latency from when the process would receive new data.

Recent tests [1] have shown that the reading process now receives data with only 10's of microseconds latency without low_latency set.

Quoted 7 lines
Quoted 3 lines

The 10's of miscroseconds is ok for 115200 bps like device, but it may hurt the high speed device like Bluetooth which runs at 3M/4M bps or higher.

The tests were run at 400Mbps, so 3Mbps or 4Mbps is not a problem (but I think you may be confusing throughput with latency).

I wrote the serial driver for intel mid platforms which are widely used in current smart phones, so I guess I know what I'm talking :)

Our test and customer test cover both the performance and latency. say like transferring 2 GB file while using BT A2DP to playing music.

Also I heard there is some app which will do real time void/data exchange with paired bluetooth, here microseconds really means something.

Quoted 24 lines

FWIW, two things affected the latency times of those particular tests;
1) the kernel firewire subsystem handles rx data in a tasklet (so not directly from IRQ) which negatively affected the latency reported, and
2) the ftrace instrumentation is not free and there are several traces per rx.

If you look carefully at the test trace data, you'll see that the timestamps from tty_flip_buffer_push() of the rx data to n_tty_write() of the response averages _~11us_; this is the measured latency from tty driver receiving the rx data to reading of that data *and* the process response (which comes back up through several tty locks).

Naturally, in mainline kernel, the scheduler load will affect the measured latency *but that's true regardless of low_latency rx steering because the user-space process must still be woken to complete the read*.

Quoted 4 lines

More and more smartphones are using uart as the Bluetooth data interface due to its low-pin, low-power feature, and many of them are using HZ=100 kernel, I'm afraid this added delay may cause some problem.

Quoted 3 lines

Some hard data showing a real problem would help further this discussion; my belief is that 3.12+ w/o low_latency rx steering will outperform 3.11- w/ low_latency rx steering in every test.

As for measurements, I guess it should cover 2 parts:
1. the performance, like the BT file transfer data rate
2. the latency, like the real time BT communication

I'll try to get some test data soon. Also I'm wondering what devices have you tested? regarding these 2 points.

Quoted 4 lines

I'm glad to hear that the Bluetooth uart interface is getting some use; that means someone will soon be fixing the hard lockup in hci_uart_tx_wakeup() reported here:

I'm not very familiar with the BT devices on our platforms, but most of them are not using the in-kernel BT driver, some just use the n_tty ldisc and the raw data, some use their own ldisc driver.

Thanks,
Feng

#6 lore raw
Quoted 7 lines
Quoted 3 lines

I'm glad to hear that the Bluetooth uart interface is getting some use; that means someone will soon be fixing the hard lockup in hci_uart_tx_wakeup() reported here:

I'm not very familiar with the BT devices on our platforms, but most of them are not using the in-kernel BT driver, some just use the n_tty ldisc and the raw data, some use their own ldisc driver.

Outside of the world of phones/android the Bluetooth on quite a few of the Baytrail/T devices is on the LPSS serial so it probably will be getting tested sooner not later and there will actually be a common platform around that is using the bluetooth uart features.

I'm not anticipating any problems though.

Alan