serial: 8250: Set fifo timeout with uart_fifo_timeout()
v2 cover serial: 8250: Set fifo timeout with uart_fifo_timeout()
Hi all,
On Thu, Jan 11, 2024 I submitted a single patch solution for this issue of data overrun because of the default timeout value which is insufficient for very low baud rates with the internal fifo device enabled.
Following the advice of Vamshi Gajjela in the v1 thread, I tried to find a way to limit the amount of math
being done within the write operation, and this is a possibility.
In this series, I'm proposing we bring back the "timeout" member of struct uart_port, and also that we add a new member of struct uart_8250_port in order to store whether the fifo device has been enabled.
This way, in the function wait_for_lsr()
all we need to do is convert the value from jiffies to usecs in order to have the appropriate timeout value.
I'm hoping to get this in during the RC phase of v6.9 and I'm trying to keep this fix as simple as possible, or at least I think this is the appropriate time to bring this issue up again...
Thanks in advance for your time.
--
MCP
Michael Pratt (3):
serial: core: Store fifo timeout again
serial: 8250: Store whether fifo device is enabled
serial: 8250: Set fifo timeout using uart_fifo_timeout()
drivers/tty/serial/8250/8250_port.c | 7 ++++++- drivers/tty/serial/serial_core.c | 3 +++ include/linux/serial_8250.h | 1 + include/linux/serial_core.h | 1 + 4 files changed, 11 insertions(+), 1 deletion(-)
base-commit: 0bbac3facb5d6cc0171c45c9873a2dc96bea9680 --
2.30.2
PATCH v2 1/3 serial: core: Store fifo timeout again
This is a partial revert of Commit f9008285bb69
("serial: Drop timeout from uart_port").
In order to prevent having to calculate a timeout for the fifo device during a write operation, if enabled, calculate it ahead of time and store the value of the timeout in a struct member of uart_port.
---
V1 -> V2: new commit
drivers/tty/serial/serial_core.c | 3 +++ include/linux/serial_core.h | 1 + 2 files changed, 4 insertions(+)
drivers/tty/serial/serial_core.c +3 -0
@@ -414,6 +414,9 @@ uart_update_timeout(struct uart_port *port, unsigned int cflag, temp *=NSEC_PER_SEC ; port->frame_time = (unsigned int )DIV64_U64_ROUND_UP (temp, baud); + +if (port->fifosize >1 ) + port->timeout =uart_fifo_timeout (port); }EXPORT_SYMBOL (uart_update_timeout);
include/linux/serial_core.h +1 -0
@@ -561,6 +561,7 @@ struct uart_port {bool hw_stopped;/* sw-assisted CTS flow state */ unsigned int mctrl;/* current modem ctrl settings */ +unsigned long timeout;/* character-based timeout */ unsigned int frame_time;/* frame timing in ns */ unsigned int type;/* port type */ const struct uart_ops *ops;
Signature · 1 line
2.30.2
Re: [PATCH v2 1/3] serial: core: Store fifo timeout again
Quoted 7 lines · Michael Pratt
This is a partial revert of Commit
f9008285bb69
("serial: Drop timeout fromuart_port").In order to prevent having to calculate a timeout for the fifo device during a write operation, if enabled, calculate it ahead of time and store the value of the timeout in a struct member of
uart_port.
...
drivers/tty/serial/serial_core.c uart_update_timeout(struct uart_port *port, unsigned int cflag, · line 419
Quoted 2 lines
drivers/tty/serial/serial_core.c +2
-0+if (port->fifosize >1 ) + port->timeout =uart_fifo_timeout (port);
else port->timeout = port->frame_time;
?
Quoted 1 line
}
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v2 1/3] serial: core: Store fifo timeout again
Hi Andy,
Quoted 9 lines · Andy Shevchenko
...
Quoted 2 lines
+ if (
port->fifosize> 1)
+port->timeout=uart_fifo_timeout(port);else
port->timeout=port->frame_time;
Consistent with what I said in the other reply, the only reason that I have an if statement here, is to avoid doing extra math for devices without a fifo, as a specifically calculated timeout value would be useless in those cases.
However, if you don't like the 10 ms default timeout, perhaps port->frame_time could actually be a more reasonable default value? That is, provided that we have a process for calculating the proper value already in place...
Quoted 4 lines
--
With Best Regards,
Andy Shevchenko
Thanks for taking a look :D
--
MCP
Re: [PATCH v2 1/3] serial: core: Store fifo timeout again
Quoted 11 lines · Michael Pratt
Quoted 7 lines · Andy Shevchenko
Quoted 2 lines
+ if (
port->fifosize> 1)
+port->timeout=uart_fifo_timeout(port);else
port->timeout=port->frame_time;
Quoted 4 lines
Consistent with what I said in the other reply, the only reason that I have an if statement here, is to avoid doing extra math for devices without a fifo, as a specifically calculated timeout value would be useless in those cases.
Please benchmark to show this actually matters if want to make this claim. Otherwise just do the math always.
Quoted 4 lines
However, if you don't like the 10 ms default timeout, perhaps
port->frame_timecould actually be a more reasonable default value? That is, provided that we have a process
for calculating the proper value already in place...
While it would be a step toward the correct direction, you'd still need to add the safety there which is already done by uart_fifo_timeout(). So no, I don't think there's advantage of using port->frame_time over just calling uart_fifo_timeout() and ensuring uart_fifo_timeout() is always using at least 1 as the FIFO size when it does the calculations.
Signature · 1 line
i.
Re: [PATCH v2 1/3] serial: core: Store fifo timeout again
In reply to Michael PrattQuoted 15 lines · Michael Pratt
Hi Andy,
Quoted 9 lines · Andy Shevchenko
...
Quoted 2 lines
+ if (
port->fifosize> 1)
+port->timeout=uart_fifo_timeout(port);else
port->timeout=port->frame_time;
Quoted 5 lines
Consistent with what I said in the other reply, the only reason that I have an if statement here, is to avoid doing extra math for devices without a fifo, as a specifically calculated timeout value would be useless in those cases.
As we are talking about a device with a scale of KB/s, I am attempted to suggest to call uart_fifo_timeout() unconditionally.
Quoted 16 lines
However, if you don't like the 10 ms default timeout, perhaps
port->frame_timecould actually be a more reasonable default value? That is, provided that we have a process for calculating the proper value already in place...Quoted 4 lines
--
With Best Regards,
Andy ShevchenkoThanks for taking a look :D
--
MCP
Re: [PATCH v2 1/3] serial: core: Store fifo timeout again
In reply to Michael PrattQuoted 7 lines · Michael Pratt
This is a partial revert of Commit
f9008285bb69
("serial: Drop timeout fromuart_port").In order to prevent having to calculate a timeout for the fifo device during a write operation, if enabled, calculate it ahead of time and store the value of the timeout in a struct member of
uart_port.
Hi,
Why is calculating during write bad/wrong, you don't give any real justification for this change? You're talking about "low rates" in cover letter, which makes it even more confusing because writes occur very infrequently so a few math operations are nothing to be concerned of (in timescales UARTs operate in, no math penalty is really worth even discussing IMO).
Signature · 1 line
i.
Quoted 35 lines
Signed-off-by Michael Pratt <[email protected]>---
V1 -> V2: new commitdrivers/tty/serial/serial_core.c | 3 +++ include/linux/serial_core.h | 1 + 2 files changed, 4 insertions(+)drivers/tty/serial/serial_core.c +3
-0@@ -414,6 +414,9 @@ uart_update_timeout(struct uart_port *port, unsigned int cflag, temp *=NSEC_PER_SEC ; port->frame_time = (unsigned int )DIV64_U64_ROUND_UP (temp, baud); + +if (port->fifosize >1 ) + port->timeout =uart_fifo_timeout (port); }EXPORT_SYMBOL (uart_update_timeout);include/linux/serial_core.h +1
-0@@ -561,6 +561,7 @@ struct uart_port {bool hw_stopped;/* sw-assisted CTS flow state */ unsigned int mctrl;/* current modem ctrl settings */ +unsigned long timeout;/* character-based timeout */ unsigned int frame_time;/* frame timing in ns */ unsigned int type;/* port type */ const struct uart_ops *ops;
PATCH v2 2/3 serial: 8250: Store whether fifo device is enabled
In reply to Michael PrattCurrently, there are 7 checks for whether to enable the internal fifo device of a 8250/16550 type uart.
Instead of checking all 7 values again whenever we need to know whether we have the fifo device enabled, store the result as a struct member of uart_8250_port.
This can, for example, lessen the amount
of calculations done during a write operation.
---
V1 -> V2: new commit
drivers/tty/serial/8250/8250_port.c | 2 ++ include/linux/serial_8250.h | 1 + 2 files changed, 3 insertions(+)
drivers/tty/serial/8250/8250_port.c +2 -0
@@ -3392,6 +3392,8 @@ void serial8250_console_write(struct uart_8250_port *up, const char *s,*/ !(up->port.flags &UPF_CONS_FLOW ); + up->fifo_enable = use_fifo; +if (likely (use_fifo))serial8250_console_fifo_write (up, s, count);else
include/linux/serial_8250.h +1 -0
@@ -127,6 +127,7 @@ struct uart_8250_port {struct list_head list;/* ports on this IRQ */ u32 capabilities;/* port capabilities */ u16 bugs;/* port bugs */ +unsigned int fifo_enable;/* fifo enabled if capable */ unsigned int tx_loadsz;/* transmit fifo load size */ unsigned char acr;unsigned char fcr;
Signature · 1 line
2.30.2
Re: [PATCH v2 2/3] serial: 8250: Store whether fifo device is enabled
Quoted 9 lines · Michael Pratt
Currently, there are 7 checks for whether to enable the internal fifo device of a 8250/16550 type uart.
Instead of checking all 7 values again whenever we need to know whether we have the fifo device enabled, store the result as a struct member of
uart_8250_port.This can, for example, lessen the amount
of calculations done during a write operation.
...
drivers/tty/serial/8250/8250_port.c void serial8250_console_write(struct uart_8250_port *up, const char *s, · line 3393
Quoted 3 lines
drivers/tty/serial/8250/8250_port.c +1
-0@@ -3392,6 +3392,8 @@ void serial8250_console_write(struct uart_8250_port *up, const char *s, + up->fifo_enable = use_fifo;
This seems incorrect / not the only one place to assign this. What if the console not enabled at compile time? What if it's not enabled at boot time?
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v2 2/3] serial: 8250: Store whether fifo device is enabled
Hi Andy,
Quoted 9 lines · Andy Shevchenko
drivers/tty/serial/8250/8250_port.c void serial8250_console_write(struct uart_8250_port *up, const char *s, · line 3393
Quoted 4 lines
drivers/tty/serial/8250/8250_port.c +1
-0@@ -3392,6 +3392,8 @@ void serial8250_console_write(struct uart_8250_port *up, const char *s, + up->fifo_enable = use_fifo;This seems incorrect / not the only one place to assign this. What if the console not enabled at compile time? What if it's not enabled at boot time?
This is 8250 specific, and currently, it's the only place there where it's decided whether or not to use the fifo device by checking a bunch of flags and values.
If you're suggesting that these checks are moved out of this function somewhere else, I would probably agree with that, but let's save that idea for the future...
If you're suggesting that there could be a null pointer, I don't think that's possible in this function... (the name of the pointer being "up" might be confusing?)
Sorry if I'm misunderstanding what you mean.
Quoted 3 lines
--
With Best Regards,
Andy Shevchenko
--
MCP
Re: [PATCH v2 2/3] serial: 8250: Store whether fifo device is enabled
Quoted 4 lines · Michael Pratt
Quoted 2 lines · Andy Shevchenko
Quoted 1 line
(quoted diff) +0
-0@@ -3392,6 +3392,8 @@ void serial8250_console_write(struct uart_8250_port *up, const char *s,
Quoted 8 lines
Quoted 4 lines
drivers/tty/serial/8250/8250_port.c void serial8250_console_write(struct uart_8250_port *up, const char *s, · line 3395
Quoted 1 line
drivers/tty/serial/8250/8250_port.c +1
-0+ up->fifo_enable = use_fifo;This seems incorrect / not the only one place to assign this. What if the console not enabled at compile time? What if it's not enabled at boot time?
This is 8250 specific, and currently, it's the only place there where it's decided whether or not to use the fifo device by checking a bunch of flags and values.
Exactly, as initial commit is related to the kernel console _only_. While your code, IIUC (correct me, if I'm wrong) is for any use of the port.
Quoted 2 lines
If you're suggesting that these checks are moved out of this function somewhere else, I would probably agree with that, but let's save that idea for the future...
Not really (again, IIUC above), as console can be not enabled, and hence serial8250_console_write() never been called and you will have false impression that there is no FIFO in use.
Quoted 4 lines
If you're suggesting that there could be a null pointer, I don't think that's possible in this function... (the name of the pointer being "up" might be confusing?)
Sorry if I'm misunderstanding what you mean.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v2 2/3] serial: 8250: Store whether fifo device is enabled
Hi Andy,
Quoted 21 lines · Andy Shevchenko
Quoted 4 lines · Michael Pratt
Quoted 2 lines · On Tuesday, April 16th, 2024 at 14:55, Andy Shevchenko…
Quoted 1 line
(quoted diff) +0
-0@@ -3392,6 +3392,8 @@ void serial8250_console_write(struct uart_8250_port *up, const char *s,Quoted 8 lines
Quoted 4 lines
drivers/tty/serial/8250/8250_port.c void serial8250_console_write(struct uart_8250_port *up, const char *s, · line 3395
Quoted 1 line
drivers/tty/serial/8250/8250_port.c +1
-0+ up->fifo_enable = use_fifo;This seems incorrect / not the only one place to assign this. What if the console not enabled at compile time? What if it's not enabled at boot time?
This is 8250 specific, and currently, it's the only place there where it's decided whether or not to use the fifo device by checking a bunch of flags and values.
Exactly, as initial commit is related to the kernel console only. While your code, IIUC (correct me, if I'm wrong) is for any use of the port.
Quoted 7 lines
Quoted 2 lines
If you're suggesting that these checks are moved out of this function somewhere else, I would probably agree with that, but let's save that idea for the future...
Not really (again, IIUC above), as console can be not enabled, and hence
serial8250_console_write()never been called and you will have false impression that there is no FIFO in use.
Ah ok, I understand now...
So there are cases where the function with the checks will never be called, yet the device itself will be configured the same way and the struct member I am adding will still be instantiated with value of 0 and never be set elsewhere... and because it is declared in a major struct "uart_8250_port", it appears to apply to a larger scope compared to the way it is actually being used...
(or at the very least, the name "fifo_enable" would be misleading).
Thanks for pointing that out, I'll take a deeper dive into the file...
Quoted 4 lines
--
With Best Regards,
Andy Shevchenko
--
MCP
PATCH v2 3/3 serial: 8250: Set fifo timeout using uart_fifo_timeout()
In reply to Michael PrattCommit 8f3631f0f6eb ("serial/8250: Use fifo in 8250 console driver") reworked functions for basic 8250 and 16550 type serial devices in order to enable and use the internal fifo device for buffering, however the default timeout of 10 ms remained, which is proving to be insufficient for low baud rates like 9600, causing data overrun.
Unforunately, that commit was written and accepted just before commit 31f6bd7fad3b ("serial: Store character timing information to uart_port") which introduced the frame_time member of the uart_port struct in order to store the amount of time it takes to send one uart frame relative to the baud rate and other serial port configuration, and commit f9008285bb69 ("serial: Drop timeout from uart_port") which established function uart_fifo_timeout() in order to calculate a reasonable timeout to wait until flushing
the fifo device before writing data again when partially filled, using the now stored frame_time value and size of the buffer.
Fix this by using the stored timeout value made with this new function to calculate the timeout for the fifo device, when enabled, and when the buffer is larger than 1 byte (unknown port default).
The previous 2 commits add the struct members used here in order to store the values, so that the calculations are offloaded from the functions that are called during a write operation for better performance.
Tested on a MIPS device (ar934x) at baud rates 625, 9600, 115200.
---
V1 -> V2:
Use stored values instead of calling uart_fifo_timeout() or checking capability flags.
The existence of the timeout value satisfies fifosize > 1.
drivers/tty/serial/8250/8250_port.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/tty/serial/8250/8250_port.c +4 -1
@@ -2066,7 +2066,10 @@ static void wait_for_lsr(struct uart_8250_port *up, int bits) {unsigned int status, tmout =10000 ; -/* Wait up to 10ms for the character(s) to be sent. */ +/* Wait for a time relative to buffer size and baud */ +if (up->fifo_enable && up->port.timeout) + tmout =jiffies_to_usecs (up->port.timeout); +for (;;) { status =serial_lsr_in (up);
Signature · 1 line
2.30.2
Re: [PATCH v2 3/3] serial: 8250: Set fifo timeout using uart_fifo_timeout()
Quoted 27 lines · Michael Pratt
Commit
8f3631f0f6eb("serial/8250: Use fifo in 8250 console driver") reworked functions for basic 8250 and 16550 type serial devices in order to enable and use the internal fifo device for buffering, however the default timeout of 10 ms remained, which is proving to be insufficient for low baud rates like 9600, causing data overrun.Unforunately, that commit was written and accepted just before commit
31f6bd7fad3b("serial: Store character timing information touart_port") which introduced theframe_timemember of theuart_portstruct in order to store the amount of time it takes to send one uart frame relative to the baud rate and other serial port configuration, and commitf9008285bb69("serial: Drop timeout fromuart_port") which established functionuart_fifo_timeout()in order to calculate a reasonable timeout to wait until flushing
the fifo device before writing data again when partially filled, using the now storedframe_timevalue and size of the buffer.Fix this by using the stored timeout value made with this new function to calculate the timeout for the fifo device, when enabled, and when the buffer is larger than 1 byte (unknown port default).
The previous 2 commits add the struct members used here in order to store the values, so that the calculations are offloaded from the functions that are called during a write operation for better performance.
Tested on a MIPS device (ar934x) at baud rates 625, 9600, 115200.
...
drivers/tty/serial/8250/8250_port.c static void wait_for_lsr(struct uart_8250_port *up, int bits) · line 2071
Quoted 6 lines
drivers/tty/serial/8250/8250_port.c +3
-1unsigned int status, tmout =10000 ; -/* Wait up to 10ms for the character(s) to be sent. */ +/* Wait for a time relative to buffer size and baud */ +if (up->fifo_enable && up->port.timeout) + tmout =jiffies_to_usecs (up->port.timeout);
Why do we still use that default? Can't we calculate timeout even for\
FIFO-less / FIFO-disabled devices?
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v2 3/3] serial: 8250: Set fifo timeout using uart_fifo_timeout()
Hi Andy,
Quoted 11 lines · Andy Shevchenko
Quoted 7 lines
unsigned int status, tmout = 10000;
- /* Wait up to 10ms for the character(s) to be sent. /
+ / Wait for a time relative to buffer size and baud */
+ if (up->fifo_enable&&up->port.timeout)
+ tmout =jiffies_to_usecs(up->port.timeout);Why do we still use that default? Can't we calculate timeout even for\
FIFO-less / FIFO-disabled devices?
Maybe it's possible that there is some kind of rare case where the LSR register is not working or not configured properly for a device in which support is being worked on...without a timeout, that would result in an infinite loop.
AFAIK, when everything is working properly, there is no such thing as needing a timeout for a uart device without fifo, as every single byte written would trigger an interrupt anyway.
Quoted 3 lines
--
With Best Regards,
Andy Shevchenko
--
MCP
Re: [PATCH v2 3/3] serial: 8250: Set fifo timeout using uart_fifo_timeout()
Quoted 4 lines · Michael Pratt
Quoted 2 lines · Andy Shevchenko
Quoted 2 lines
unsigned int status, tmout = 10000;
Quoted 8 lines
Quoted 8 lines
Quoted 4 lines
- /* Wait up to 10ms for the character(s) to be sent. /
+ / Wait for a time relative to buffer size and baud */
+ if (up->fifo_enable&&up->port.timeout)
+ tmout =jiffies_to_usecs(up->port.timeout);Why do we still use that default? Can't we calculate timeout even for\
FIFO-less / FIFO-disabled devices?
Yes we definitely should be able to. Unfortunately these patches just keep coming back not in the form that follows the review feedback, but they come up their own way of doing things which is way worse and ignores the given feedback.
Quoted 3 lines
Maybe it's possible that there is some kind of rare case where the LSR register is not working or not configured properly for a device in which support is being worked on...without a timeout, that would result in an infinite loop.
"without a timeout" is not what Andy said. He said you should always have a timeout, regardless of there being FIFO or not. And that timeout should be derived in the same manner from baudrate and FIFO size (to address the cases w/o FIFO, the fifosize should be lower bounded to 1 while calculating the FIFO timeout).
Quoted 3 lines
AFAIK, when everything is working properly, there is no such thing as needing a timeout for a uart device without fifo, as every single byte written would trigger an interrupt anyway.
While I agree the general principle, that this is backup that should not even be needed, the statement is partly incorrect. We don't get interrupts during console write because they're disabled. But LSR should still change and allow progress without the backup timeout.
Signature · 1 line
i.
Re: [PATCH v2 3/3] serial: 8250: Set fifo timeout using uart_fifo_timeout()
In reply to Michael PrattQuoted 21 lines · Michael Pratt
Commit
8f3631f0f6eb("serial/8250: Use fifo in 8250 console driver") reworked functions for basic 8250 and 16550 type serial devices in order to enable and use the internal fifo device for buffering, however the default timeout of 10 ms remained, which is proving to be insufficient for low baud rates like 9600, causing data overrun.Unforunately, that commit was written and accepted just before commit
31f6bd7fad3b("serial: Store character timing information touart_port") which introduced theframe_timemember of theuart_portstruct in order to store the amount of time it takes to send one uart frame relative to the baud rate and other serial port configuration, and commitf9008285bb69("serial: Drop timeout fromuart_port") which established functionuart_fifo_timeout()in order to calculate a reasonable timeout to wait until flushing
the fifo device before writing data again when partially filled, using the now storedframe_timevalue and size of the buffer.Fix this by using the stored timeout value made with this new function to calculate the timeout for the fifo device, when enabled, and when the buffer is larger than 1 byte (unknown port default).
Quoted 6 lines
The previous 2 commits add the struct members used here in order to store the values, so that the calculations are offloaded from the functions that are called during a write operation for better performance.
Tested on a MIPS device (ar934x) at baud rates 625, 9600, 115200.
Did you actually see some perfomance issue with 115000 bps? Or did you just make the "better performance" claim up?
Quoted 16 lines
Fixes 8f3631f0f6eb ("serial/8250: Use fifo in 8250 console driver")Signed-off-by Michael Pratt <[email protected]>---
V1 -> V2:
Use stored values instead of callinguart_fifo_timeout()or checking capability flags.
The existence of the timeout value satisfies fifosize > 1.drivers/tty/serial/8250/8250_port.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)drivers/tty/serial/8250/8250_port.c +0
-0
drivers/tty/serial/8250/8250_port.c static void wait_for_lsr(struct uart_8250_port *up, int bits) · line 2074
Quoted 10 lines
drivers/tty/serial/8250/8250_port.c +4
-1@@ -2066,7 +2066,10 @@ static void wait_for_lsr(struct uart_8250_port *up, int bits) {unsigned int status, tmout =10000 ; -/* Wait up to 10ms for the character(s) to be sent. */ +/* Wait for a time relative to buffer size and baud */ +if (up->fifo_enable && up->port.timeout) + tmout =jiffies_to_usecs (up->port.timeout); +for (;;) { status =serial_lsr_in (up);
Hi,
Is this the only reason for adding the timeout field? I think you should just refactor the code such that you don't need to recalculate the timeout for each characted when writing n characters?
Signature · 1 line
i.