Skip to contentSkip to conversation

Serial console and interrupts latency.

DISCUSSION Sergey Organov 3 participants 9 messages Mar 24, 2020 – Apr 14, 2020 TTY layer and serial drivers

lore
Share & tools
lore
#0 lore raw

Hello,

[Extended CC list to try to get some attention]

I was investigating random serial overruns on my embedded board and figured it strongly correlates with serial output (to another serial port) from kernel printk() calls, that forced me to dig into the kernel sources, and now I'm very confused.

I'm reading drivers/tty/serial/8250/8250_port.c, and serial8250_console_write() function in particular (being on tty-next branch).

What I see is that it locks interrupts

3141: spin_lock_irqsave(&port->lock, flags);

and then calls wait_for_xmitr() both indirectly here:

3159: uart_console_write(port, s, count, serial8250_console_putchar);

and then directly as well:

3165: wait_for_xmitr(up, BOTH_EMPTY);

before re-enabling interrupts at:

3179: spin_unlock_irqrestore(&port->lock, flags);

Now, wait_for_xmitr(), even according to comments, could busy-wait for up to 10+1000 milliseconds, and in this case this huge delay will happen at interrupts disabled?

Does it mean any serial console output out of printk() could cause 10 milliseconds or even 1 second interrupts latency? Somehow I can't believe it.

What do I miss?

Thanks,
-- Sergey

#1 lore raw
Quoted 33 lines · Sergey Organov

Hello,

[Extended CC list to try to get some attention]

I was investigating random serial overruns on my embedded board and figured it strongly correlates with serial output (to another serial port) from kernel printk() calls, that forced me to dig into the kernel sources, and now I'm very confused.

I'm reading drivers/tty/serial/8250/8250_port.c, and serial8250_console_write() function in particular (being on tty-next branch).

What I see is that it locks interrupts

3141: spin_lock_irqsave(&port->lock, flags);

and then calls wait_for_xmitr() both indirectly here:

3159: uart_console_write(port, s, count, serial8250_console_putchar);

and then directly as well:

3165: wait_for_xmitr(up, BOTH_EMPTY);

before re-enabling interrupts at:

3179: spin_unlock_irqrestore(&port->lock, flags);

Now, wait_for_xmitr(), even according to comments, could busy-wait for up to 10+1000 milliseconds, and in this case this huge delay will happen at interrupts disabled?

Quoted 5 lines

Does it mean any serial console output out of printk() could cause 10 milliseconds or even 1 second interrupts latency? Somehow I can't believe it.

What do I miss?

1 second _timeout_ is for flow-control-enabled consoles.

10 ms is _timeout_ for a character. With slow 9600 baud console, sending one character takes 0.8 ms. With 115200, it is 70 us.

If you send one line (80 chars), it is really 66 and 5.5 ms, respectively.

So yes, serial consoles can slow down the boot and add latency. Use faster speeds or faster devices for consoles, if you mind. And do not enable flow control. Serial is serial.

You can also try to eliminate the interrupts disablement, but that would be a bit tough task, IMO.

thanks,

Signature · 2 lines
js
suse labs
#2 lore raw
Quoted 45 lines · Jiri Slaby
Quoted 33 lines · Sergey Organov

Hello,

[Extended CC list to try to get some attention]

I was investigating random serial overruns on my embedded board and figured it strongly correlates with serial output (to another serial port) from kernel printk() calls, that forced me to dig into the kernel sources, and now I'm very confused.

I'm reading drivers/tty/serial/8250/8250_port.c, and serial8250_console_write() function in particular (being on tty-next branch).

What I see is that it locks interrupts

3141: spin_lock_irqsave(&port->lock, flags);

and then calls wait_for_xmitr() both indirectly here:

3159: uart_console_write(port, s, count, serial8250_console_putchar);

and then directly as well:

3165: wait_for_xmitr(up, BOTH_EMPTY);

before re-enabling interrupts at:

3179: spin_unlock_irqrestore(&port->lock, flags);

Now, wait_for_xmitr(), even according to comments, could busy-wait for up to 10+1000 milliseconds, and in this case this huge delay will happen at interrupts disabled?

Quoted 5 lines

Does it mean any serial console output out of printk() could cause 10 milliseconds or even 1 second interrupts latency? Somehow I can't believe it.

What do I miss?

1 second _timeout_ is for flow-control-enabled consoles.

10 ms is _timeout_ for a character. With slow 9600 baud console, sending one character takes 0.8 ms. With 115200, it is 70 us.

Quoted 8 lines

If you send one line (80 chars), it is really 66 and 5.5 ms, respectively.

So yes, serial consoles can slow down the boot and add latency. Use faster speeds or faster devices for consoles, if you mind. And do not enable flow control. Serial is serial.

You can also try to eliminate the interrupts disablement, but that would be a bit tough task, IMO.

... and likely difficult to implement, since the spinlock is there to prevent coincident usage from userspace and kernel space to the port. If userspace is outputting a message in unison with a kernel console message, there must be exclusivity, otherwise the two messages will either be intermingled, or completely corrupted due to TX FIFO overflow, making the console quite useless.

Higher latency is something that IMHO has to be accepted as a result of using serial console.

As Jiri says, the way around that is to use as fast a baud rate as possible on the serial console.

Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTC broadband for 0.8mile line in suburbia: sync at 10.2Mbps down 587kbps up
#4 lore raw
In reply to Jiri Slaby
Quoted 34 lines · Jiri Slaby
Quoted 33 lines · Sergey Organov

Hello,

[Extended CC list to try to get some attention]

I was investigating random serial overruns on my embedded board and figured it strongly correlates with serial output (to another serial port) from kernel printk() calls, that forced me to dig into the kernel sources, and now I'm very confused.

I'm reading drivers/tty/serial/8250/8250_port.c, and serial8250_console_write() function in particular (being on tty-next branch).

What I see is that it locks interrupts

3141: spin_lock_irqsave(&port->lock, flags);

and then calls wait_for_xmitr() both indirectly here:

3159: uart_console_write(port, s, count, serial8250_console_putchar);

and then directly as well:

3165: wait_for_xmitr(up, BOTH_EMPTY);

before re-enabling interrupts at:

3179: spin_unlock_irqrestore(&port->lock, flags);

Now, wait_for_xmitr(), even according to comments, could busy-wait for up to 10+1000 milliseconds, and in this case this huge delay will happen at interrupts disabled?

Quoted 7 lines
Quoted 5 lines

Does it mean any serial console output out of printk() could cause 10 milliseconds or even 1 second interrupts latency? Somehow I can't believe it.

What do I miss?

1 second _timeout_ is for flow-control-enabled consoles.

Yeah, sure. So it does mean interrupts could be disabled for up to 1 second, on already up and running system. Too bad.

Actually, I use 8250 only as a reference implementation, my actual chip is handled by imx.c, and the latter even has no timeouts on this path, so apparently may block (the entire kernel) indefinitely!

Quoted 2 lines

10 ms is _timeout_ for a character. With slow 9600 baud console, sending one character takes 0.8 ms. With 115200, it is 70 us.

70us of disabled interrupts is a huge number, and for FIFO-enabled chips the estimate should be multiplied by FIFO size (say, x16) that brings us close to 1ms even on 115200, right?

Anyway, it must cause receiving overruns on another port running at higher or the same baud rate and no DMA, sooner or later, as it does for me.

Quoted 6 lines

If you send one line (80 chars), it is really 66 and 5.5 ms, respectively.

So yes, serial consoles can slow down the boot and add latency. Use faster speeds or faster devices for consoles, if you mind. And do not enable flow control. Serial is serial.

I don't care about slowing-down boot. I care about huge interrupt latency on up and running system, causing loss of characters (overruns) on other serial ports.

To be sure, it is this code that works on already running system as well, not only on boot-time, right? Or is my system somehow misconfigured?

I'm confused as this seems to be a major issue and nobody but me seems to care or to suffer from it, and I can't figure why.

For reference, I figured this locking was introduced by:

commit d8a5a8d7cc32e4474326e0ecc1b959063490efc9

Author: Russell King <[email protected]>
Date:   Tue May 2 16:04:29 2006 +0100

[SERIAL] 8250: add locking to console write function

x86 SMP breaks as a result of the previous change, we have no real option other than to add locking to the 8250 console write function. If an oops is in progress, try to acquire the lock. If we fail to do so, continue anyway.

Quoted 2 lines

You can also try to eliminate the interrupts disablement, but that would be a bit tough task, IMO.

It seems like I need to, and yeah, it'd be a somewhat tough task indeed, but then there is one simple question: why isn't console output handled through usual buffer/ISR paths?

I have an idea to investigate the netconsole module to see how it does similar job for networks. Hopefully it has no such problems and will give some useful tips.

Thanks,
-- Sergey

#3 lore raw
In reply to Russell King - ARM Linux admin
Quoted 62 lines · Russell King - ARM Linux admin
Quoted 45 lines · Jiri Slaby
Quoted 33 lines · Sergey Organov

Hello,

[Extended CC list to try to get some attention]

I was investigating random serial overruns on my embedded board and figured it strongly correlates with serial output (to another serial port) from kernel printk() calls, that forced me to dig into the kernel sources, and now I'm very confused.

I'm reading drivers/tty/serial/8250/8250_port.c, and serial8250_console_write() function in particular (being on tty-next branch).

What I see is that it locks interrupts

3141: spin_lock_irqsave(&port->lock, flags);

and then calls wait_for_xmitr() both indirectly here:

3159: uart_console_write(port, s, count, serial8250_console_putchar);

and then directly as well:

3165: wait_for_xmitr(up, BOTH_EMPTY);

before re-enabling interrupts at:

3179: spin_unlock_irqrestore(&port->lock, flags);

Now, wait_for_xmitr(), even according to comments, could busy-wait for up to 10+1000 milliseconds, and in this case this huge delay will happen at interrupts disabled?

Quoted 5 lines

Does it mean any serial console output out of printk() could cause 10 milliseconds or even 1 second interrupts latency? Somehow I can't believe it.

What do I miss?

1 second _timeout_ is for flow-control-enabled consoles.

10 ms is _timeout_ for a character. With slow 9600 baud console, sending one character takes 0.8 ms. With 115200, it is 70 us.

Quoted 8 lines

If you send one line (80 chars), it is really 66 and 5.5 ms, respectively.

So yes, serial consoles can slow down the boot and add latency. Use faster speeds or faster devices for consoles, if you mind. And do not enable flow control. Serial is serial.

You can also try to eliminate the interrupts disablement, but that would be a bit tough task, IMO.

... and likely difficult to implement, since the spinlock is there to prevent coincident usage from userspace and kernel space to the port. If userspace is outputting a message in unison with a kernel console message, there must be exclusivity, otherwise the two messages will either be intermingled, or completely corrupted due to TX FIFO overflow, making the console quite useless.

Quoted 2 lines

Higher latency is something that IMHO has to be accepted as a result of using serial console.

This (local) interrupts disabling might be not a big problem on SMP, I dunno, as another core could still handle IRQs, but for non-SMP case this is too bad as it causes missed data on other ports.

Can we disable this locking on non-SMP configs as a work-around?

Quoted 2 lines

As Jiri says, the way around that is to use as fast a baud rate as possible on the serial console.

Unfortunately, this is entirely impractical for an application that utilizes maximum baud rates on other serial ports.

Thanks,
-- Sergey

#5 lore raw
In reply to Sergey Organov
Quoted 54 lines · Sergey Organov
Quoted 34 lines · Jiri Slaby
Quoted 33 lines · Sergey Organov

Hello,

[Extended CC list to try to get some attention]

I was investigating random serial overruns on my embedded board and figured it strongly correlates with serial output (to another serial port) from kernel printk() calls, that forced me to dig into the kernel sources, and now I'm very confused.

I'm reading drivers/tty/serial/8250/8250_port.c, and serial8250_console_write() function in particular (being on tty-next branch).

What I see is that it locks interrupts

3141: spin_lock_irqsave(&port->lock, flags);

and then calls wait_for_xmitr() both indirectly here:

3159: uart_console_write(port, s, count, serial8250_console_putchar);

and then directly as well:

3165: wait_for_xmitr(up, BOTH_EMPTY);

before re-enabling interrupts at:

3179: spin_unlock_irqrestore(&port->lock, flags);

Now, wait_for_xmitr(), even according to comments, could busy-wait for up to 10+1000 milliseconds, and in this case this huge delay will happen at interrupts disabled?

Quoted 7 lines
Quoted 5 lines

Does it mean any serial console output out of printk() could cause 10 milliseconds or even 1 second interrupts latency? Somehow I can't believe it.

What do I miss?

1 second _timeout_ is for flow-control-enabled consoles.

Yeah, sure. So it does mean interrupts could be disabled for up to 1 second, on already up and running system. Too bad.

Actually, I use 8250 only as a reference implementation, my actual chip is handled by imx.c, and the latter even has no timeouts on this path, so apparently may block (the entire kernel) indefinitely!

Quoted 2 lines

10 ms is _timeout_ for a character. With slow 9600 baud console, sending one character takes 0.8 ms. With 115200, it is 70 us.

Quoted 7 lines

70us of disabled interrupts is a huge number, and for FIFO-enabled chips the estimate should be multiplied by FIFO size (say, x16) that brings us close to 1ms even on 115200, right?

Anyway, it must cause receiving overruns on another port running at higher or the same baud rate and no DMA, sooner or later, as it does for me.

So, don't use serial console then, it's unsuitable for your use case.

Quoted 23 lines
Quoted 5 lines

If you send one line (80 chars), it is really 66 and 5.5 ms, respectively.

So yes, serial consoles can slow down the boot and add latency. Use faster speeds or faster devices for consoles, if you mind. And do not enable flow control. Serial is serial.

I don't care about slowing-down boot. I care about huge interrupt latency on up and running system, causing loss of characters (overruns) on other serial ports.

To be sure, it is this code that works on already running system as well, not only on boot-time, right? Or is my system somehow misconfigured?

I'm confused as this seems to be a major issue and nobody but me seems to care or to suffer from it, and I can't figure why.

For reference, I figured this locking was introduced by:

commit d8a5a8d7cc32e4474326e0ecc1b959063490efc9

Author: Russell King <[email protected]>
Date:   Tue May 2 16:04:29 2006 +0100
Quoted 6 lines

[SERIAL] 8250: add locking to console write function

x86 SMP breaks as a result of the previous change, we have no real option other than to add locking to the 8250 console write function. If an oops is in progress, try to acquire the lock. If we fail to do so, continue anyway.

Correct, and what I said back then still applies - and more.

Quoted 3 lines

It seems like I need to, and yeah, it'd be a somewhat tough task indeed, but then there is one simple question: why isn't console output handled through usual buffer/ISR paths?

The "usual" paths may not be active, and, in the case of an oops, we want to see the output, which we wouldn't be able to if the oops occurred in interrupt context.

Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTC broadband for 0.8mile line in suburbia: sync at 10.2Mbps down 587kbps up
#6 lore raw

Russell King - ARM Linux admin <[email protected]>:

Quoted 60 lines · Sergey Organov
Quoted 58 lines
Quoted 34 lines · Jiri Slaby
Quoted 33 lines · Sergey Organov

Hello,

[Extended CC list to try to get some attention]

I was investigating random serial overruns on my embedded board and figured it strongly correlates with serial output (to another serial port) from kernel printk() calls, that forced me to dig into the kernel sources, and now I'm very confused.

I'm reading drivers/tty/serial/8250/8250_port.c, and serial8250_console_write() function in particular (being on tty-next branch).

What I see is that it locks interrupts

3141: spin_lock_irqsave(&port->lock, flags);

and then calls wait_for_xmitr() both indirectly here:

3159: uart_console_write(port, s, count, serial8250_console_putchar);

and then directly as well:

3165: wait_for_xmitr(up, BOTH_EMPTY);

before re-enabling interrupts at:

3179: spin_unlock_irqrestore(&port->lock, flags);

Now, wait_for_xmitr(), even according to comments, could busy-wait for up to 10+1000 milliseconds, and in this case this huge delay will happen at interrupts disabled?

Quoted 7 lines
Quoted 5 lines

Does it mean any serial console output out of printk() could cause 10 milliseconds or even 1 second interrupts latency? Somehow I can't believe it.

What do I miss?

1 second _timeout_ is for flow-control-enabled consoles.

Yeah, sure. So it does mean interrupts could be disabled for up to 1 second, on already up and running system. Too bad.

Actually, I use 8250 only as a reference implementation, my actual chip is handled by imx.c, and the latter even has no timeouts on this path, so apparently may block (the entire kernel) indefinitely!

Quoted 2 lines

10 ms is _timeout_ for a character. With slow 9600 baud console, sending one character takes 0.8 ms. With 115200, it is 70 us.

70us of disabled interrupts is a huge number, and for FIFO-enabled chips the estimate should be multiplied by FIFO size (say, x16) that brings us close to 1ms even on 115200, right?

Quoted 5 lines
Quoted 3 lines

Anyway, it must cause receiving overruns on another port running at higher or the same baud rate and no DMA, sooner or later, as it does for me.

So, don't use serial console then, it's unsuitable for your use case.

Well, I'd rather fix it, as serial console is otherwise very suitable for my needs.

If nobody else interested, I'll simply disable the lock you've added for non-smp builds
in my kernel version, rather than trying to fix the issue in general.

That said, finding generic solution would be an interesting quest.

Quoted 24 lines
Quoted 24 lines
Quoted 6 lines

If you send one line (80 chars), it is really 66 and 5.5 ms, respectively.

So yes, serial consoles can slow down the boot and add latency. Use faster speeds or faster devices for consoles, if you mind. And do not enable flow control. Serial is serial.

I don't care about slowing-down boot. I care about huge interrupt latency on up and running system, causing loss of characters (overruns) on other serial ports.

To be sure, it is this code that works on already running system as well, not only on boot-time, right? Or is my system somehow misconfigured?

I'm confused as this seems to be a major issue and nobody but me seems to care or to suffer from it, and I can't figure why.

For reference, I figured this locking was introduced by:

commit d8a5a8d7cc32e4474326e0ecc1b959063490efc9

Author: Russell King <[email protected]>
Date:   Tue May 2 16:04:29 2006 +0100
Quoted 8 lines
Quoted 6 lines

[SERIAL] 8250: add locking to console write function

x86 SMP breaks as a result of the previous change, we have no real option other than to add locking to the 8250 console write function. If an oops is in progress, try to acquire the lock. If we fail to do so, continue anyway.

Correct, and what I said back then still applies - and more.

What bothers me is "we have no real option..." part of this, as it's rarely happens to be the case.

Quoted 8 lines
Quoted 4 lines

It seems like I need to, and yeah, it'd be a somewhat tough task indeed, but then there is one simple question: why isn't console output handled through usual buffer/ISR paths?

The "usual" paths may not be active, and, in the case of an oops, we want to see the output, which we wouldn't be able to if the oops occurred in interrupt context.

The oops part is already special-cased and could be left special-cased, one way or another. What is important is not to keep interrupts disabled for long during normal system operation.

If it could be achieved at all, it probably should then be implemented at the upper level, to save low-level drivers from these complexities.

Thanks,
-- Sergey

#7 lore raw
Quoted 5 lines · Sergey Organov

Russell King - ARM Linux admin <[email protected]>:

Quoted 1 line

Correct, and what I said back then still applies - and more.

What bothers me is "we have no real option..." part of this, as it's rarely happens to be the case.

I don't see you coming forward with a solution beyond "let's revert the commit" or "let's comment out the lock" - neither of which are an option for mainline kernels.

Until *you* do, since you obviously have better ideas, "we have no real option".

But, as I've said, one of the *important* characteristics of serial console is that it is synchronous with the kernel, so it can be relied upon to get complete messages out if the kernel crashes after a printk() has been executed, and that must not be lost.

Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTC broadband for 0.8mile line in suburbia: sync at 10.2Mbps down 587kbps up
#8 lore raw
Quoted 10 lines · Russell King - ARM Linux admin
Quoted 5 lines · Sergey Organov

Russell King - ARM Linux admin <[email protected]>:

Quoted 1 line

Correct, and what I said back then still applies - and more.

What bothers me is "we have no real option..." part of this, as it's rarely happens to be the case.

I don't see you coming forward with a solution beyond "let's revert the commit" or "let's comment out the lock" - neither of which are an option for mainline kernels.

I didn't suggest to revert the commit, as it obviously solves real problem. I asked if and why the lock is needed on non-SMP targets, but either I didn't get a reply or missed it, sorry.

I mean everything I said was to get some help rather than to spread useless critics or even insults.

Quoted 3 lines

Until *you* do, since you obviously have better ideas, "we have no real option".

I'm in the process of making myself familiar with the internals of printk machinery to find generic solution, but if nobody else actually cares, I'll stop bothering you guys with my questions.

Do I get it right that you still think there are no sensible options but to disable interrupts for ages? If so, it would greatly reduce my drive to find one... or maybe the other way round, but it'd be nice to know either way.

Quoted 4 lines

But, as I've said, one of the *important* characteristics of serial console is that it is synchronous with the kernel, so it can be relied upon to get complete messages out if the kernel crashes after a printk() has been executed, and that must not be lost.

Do I get it right that it's covered by the 'oops' case, or are there hidden pitfalls as well?

Thanks,
-- Sergey