Serial console hangs with Linux 2.6.20 HVM guest
I am seeing a problem with the Xen emulated serial console. When running a Linux 2.6.20 HVM guest that has CONFIG_HOTPLUG_CPU=n, the guest blocks on output to the console until it receives input keypresses from xm console. This prevents the guest from booting up without banging on some keys, and makes interactive use of the console difficult.
By bisecting Linux kernel commits, I found that the bug goes away in commit 40b36daad0ac704e6d5c1b75789f371ef5b053c1 (v2.6.21-rc1~261), which is a workaround for buggy UARTs on certain HP machines.
“The patch below works around a minor bug found in the UART of the remote management card used in many HP ia64 and parisc servers (aka the Diva UARTs). The problem is that the UART does not reassert the THRE interrupt if it has been previously cleared and the IIR THRI bit is re-enabled. This can produce a very annoying failure mode when used as a serial console, allowing a boot/reboot to hang indefinitely until an RX interrupt kicks it into working again (ie. an unattended reboot could stall).”
That matches my symptoms exactly, which suggests that the Xen UART probably has a similar bug.
I’ve seen this in Xen 3.2.1 and 3.3.1. (My host is running Debian Lenny amd64, with the Xen dom0 kernel 2.6.24-23-xen from Ubuntu Hardy, on a server with two quad-core Xeons.)
Anders
Anders Kaseorg writes ("[Xen-devel] Serial console hangs with Linux 2.6.20 HVM guest"):
Quoted 1 line
Thanks for this. However looking at the code I'm not sure that's what's actually going on. Very old versions of qemu did have the bug that patch describes, but it was fixed in qemu upstream in 2004 in this commit:
commit 60e336dbb837ef4d5053433f9ee391feb102be36
Author: bellard <bellard@c046a42c-6fe2-441c-8c8c-71466251a162>
Date: Tue Aug 24 21:55:28 2004 +0000
serial interrupt fix (Hampa Hug)
git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@1049 c046a42c-6fe2-441c-8c8c-71466251a162
As far as I can see this fix is included in all relevant versions of qemu. The upstream Linux changeset seems like it would fix any general IRQ loss problem with the UART.
If you can reproduce this problem, I would like to try to debug it.
Probably the best way is to attach to the running qemu with gdb:
* Add
CFLAGS += -g -O0
to xen-unstable.hg/.config (creating it if necessary) and rebuild and install the resulting qemu-dm binary where it will be built.
* Start the VM in the usual way.
* cd to the qemu-xen-unstable tree (probably tools/ioemu-remote); use ps to find the pid of qemu-dm, and say
gdb i386-dm/qemu-dm <pid>
and then at the gdb prompt say
handle SIGUSR2 nostop noprint
break serial_ioport_write if (addr&7)==1
cont
* do whatever it is that makes the VM stuck
* when it next stops it will be in serial_ioport_write setting the IER. So
print val
print *s
and email the output to this list.
NB I haven't tested this recipe. If you get syntax errors or something doesn't work I'll actually do so and let you know what to type.
Ian.
Quoted 8 lines · Ian Jackson
handle SIGUSR2 nostop noprint
breakserial_ioport_writeif (addr&7)==1
cont
* do whatever it is that makes the VM stuck
* when it next stops it will be inserial_ioport_writesetting the IER. So
print val
print *s
This breakpoint is triggered for all messages printed by the kernel, which always showed up with no delay; but it is only occasionally triggered for strings printed by userspace, even after forcing those strings to show up by sending keystrokes.
Here is one of the latter cases. (I am sitting at a
“root@andersk-intrepid:~# ” prompt, repeatedly pressing Enter. Each keypress causes the previous prompt to show up, followed by a newline, and the current prompt is stalled.)
Breakpoint 1, serial_ioport_write (opaque=0xb342e0, addr=1, val=5)
at /home/andersk/xen-3-3.3.1/debian/build/build-utils_amd64/tools/ioemu-dir/hw/serial.c:413
413 {
(gdb) print val
$5 = 5
(gdb) print *s
$6 = {divider = 1, rbr = 0 '\0', thr = 32 ' ', tsr = 32 ' ', ier = 5 '\005', iir = 193 '�',
lcr = 19 '\023', mcr = 11 '\v', lsr = 96 '`', msr = 176 '�', scr = 0 '\0', fcr = 129 '\201', thr_ipending = 1, irq = 0xb1d610, chr = 0xb122a0, last_break_enable = 0, base = 0,
it_shift = 0, baudbase = 115200, tsr_retry = 0, last_xmit_ts = 380482341502, recv_fifo = {
data = '\r' <repeats 16 times>, count = 0 '\0', itl = 8 '\b', tail = 0 '\0',
head = 0 '\0'}, xmit_fifo = {data = "repid:~# rsk-int", count = 0 '\0', itl = 0 '\0',
tail = 9 '\t', head = 9 '\t'}, fifo_timeout_timer = 0xb31ad0, timeout_ipending = 0, transmit_timer = 0xb31b00, char_transmit_time = 78120, poll_msl = -1, modem_status_poll = 0xb327e0}
Anders
In case this is more interesting, here is a log of all the breakpoints hit during a Linux boot, up to the point where it hangs:
http://web.mit.edu/andersk/Public/xen-serial-log
Anders
Anders Kaseorg writes ("Re: [Xen-devel] Serial console hangs with Linux 2.6.20 HVM guest"):
Quoted 4 lines
This breakpoint is triggered for all messages printed by the kernel, which always showed up with no delay; but it is only occasionally triggered for strings printed by userspace, even after forcing those strings to show up by sending keystrokes.
That's interesting ....
Anders Kaseorg writes ("Re: [Xen-devel] Serial console hangs with Linux 2.6.20 HVM guest"):
Quoted 4 lines
In case this is more interesting, here is a log of all the breakpoints hit during a Linux boot, up to the point where it hangs:
.... looking at this, it seems like we definitely don't have the bug that the Linux kernel is trying to work around. In each of the cases where the IER was written there by the kernel, an interrupt was already pending.
But since the Linux change is just to have a failsafe timer for lost interrupts, any other kind of lost interrupt situation might cause it. Perhaps the kernel is expecting us to deassert and reassert the interrupt in some situation when we aren't; since the interrupts are converted from level to edge triggered it's possible that we are failing to _deassert_ the interrupt when we should. I'm just speculating here at the moment ...
Can you recompile your qemu-dm with
#define DEBUG_SERIAL
near the top of serial.c uncommented, and try booting up and get it to the point where some output is supposed to have come out but has got stuck, and then send me your /var/log/xen/qemu-dm-<whatever>.log ?
If it's short post it to the list; otherwise put it up on a webpage or email it to me privately.
Thanks,
Ian.
Quoted 5 lines · Ian Jackson
Can you recompile your qemu-dm with
#defineDEBUG_SERIAL
near the top ofserial.cuncommented, and try booting up and get it to the point where some output is supposed to have come out but has got stuck, and then send me your /var/log/xen/qemu-dm-<whatever>.log ?
Sure: http://web.mit.edu/andersk/Public/qemu-dm-andersk-intrepid.log (3 MB).
Anders
Anders Kaseorg writes ("Re: [Xen-devel] Serial console hangs with Linux 2.6.20 HVM guest"):
Quoted 4 lines
In case this is more interesting, here is a log of all the breakpoints hit during a Linux boot, up to the point where it hangs:
Thanks, that's great. But I think it's a kernel bug. The last thing the kernel does is read the IIR (Interrupt Identification Register) twice at a time when the transmit FIFO is empty. Reading the IIR is (sadly) not a side-effect-free operation; specifically, it cancels any outstanding transmit fifo/buffer empty interrupt[1]. So the first time it gets told Transmit Holding Register Empty interrupt', but that has the effect of clearing the interrupt so the second time it reads the IIRC it gets no interrupt pending'.
[1] I'm getting this out of the National Semiconductor datasheet for the PC16550D, document number TL/C/8652, June 1995. See for example section 8.11 `FIFO Interrupt Mode Operation' item A:
The transmit holding register interrupt (02) occurs when the XMIT FIFO is empty; it is cleared as soon as the transmitter holding register is written to ([...]) or the IIR is read.
As far as I can see qemu-dm is emulating this accurately.
Can you point me at the exact kernel source code you're using ?
Thanks,
Ian.
Quoted 1 line · Ian Jackson
Can you point me at the exact kernel source code you're using ?
Yes, I took Linux v2.6.20 on amd64, ran make defconfig, then ran make menuconfig and turned off CONFIG_HOTPLUG_CPU (Processor type and features → Support for hot-pluggable CPUs).
Anders
PATCH 1/1 IRQ handling race and spurious IIR read in serial/8250.c
Anders Kaseorg writes ("Re: Serial console hangs with Linux 2.6.20 HVM guest"):
Quoted 3 lines
Yes, I took Linux v2.6.20 on amd64, ran
make defconfig, then ranmake menuconfigand turned offCONFIG_HOTPLUG_CPU(Processor type and features ò Support for hot-pluggable CPUs).
Thanks. I think I have tracked down the bug. In drivers/serial/8250.c in Linux there are two bugs:
1. UART_BUG_TXEN can be spuriously set, due to an IRQ race
2. The workaround then applied by the kernel is itself buggy
Anders: can you try two tests for me ? Firstly, in serial8250_startup, delete the section which sets UART_BUG_TXEN (see 2nd patch below); I think this will fix the symptoms for you. Secondly, in serial8250_start_tx delete the read from the IIR and the relevant branch of the text (see 3rd patch below); I think this will also in itself fix your symptoms. I haven't compiled either patch (so you may find that eg I missed deleting some variables).
The bugs in detail (this discussion applies to 2.6.20 and also to 2.6.28.4):
1. The hunk of serial8250_startup I quote below attempts to discover whether writing the IER re-asserts the THRI (transmit ready) interrupt. However the spinlock that it has taken out, port->lock, is not the one that the IRQ service routine takes before reading the IIR (i->lock). As a result, on an SMP system the generated interrupt races with the straight-line code in serial8250_startup.
If serial8250_startup loses the race (perhaps because the system is a VM and its VCPU got preempted), UART_BUG_TXEN is spuriously added to bugs. This is quite unlikely in a normal system but in certain Xen configurations, particularly ones where there is CPU pressure, we may lose the race every time.
It is not exactly clear to me how this ought to be resolved. One possibility is that the UART_BUG_TXEN problem might be worked around perfectly well by the new and very similar workaround UART_BUG_THRE[1] in 2.6.21ish in which case it could just be removed.
2. UART_BUG_TXEN's workaround appears to be intended to be harmless. However what it actually does is to read the IIR, thus clearing any actual interrupt (including incidentally non-THRI), and then only perform the intended servicing if the interrupt was _not_ asserted. That is, it breaks on any serial port with the bug.
As far as I can see there is not much use in UART_BUG_TXEN reading IIR at all, so a suitable change if we want to keep UART_BUG_TXEN might be the first patch I enclose below (again, not compiled or tested).
If UART_BUG_TXEN is retained something along these lines should be done at the very least.
Ian.
[1] http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=commit;h=40b36daad0ac704e6d5c1b75789f371ef5b053c1
in which case UART
Proposed initial band-aid fix (against 2.6.28.4):
Do not read IIR in serial8250_start_tx when UART_BUG_TXEN
Reading the IIR clears some oustanding interrupts so it is not safe. Instead, simply transmit immediately if the buffer is empty without regard to IIR.
linux-2.6.28.4/drivers/serial/8250.c renamed from linux-2.6.28.4/drivers/serial/8250.c~ +3 -5
@@ -1257,14 +1257,12 @@serial_out (up,UART_IER , up->ier);if (up->bugs &UART_BUG_TXEN ) { -unsigned char lsr, iir; +unsigned char lsr; lsr =serial_in (up,UART_LSR ); up->lsr_saved_flags |= lsr &LSR_SAVE_FLAGS ; - iir =serial_in (up,UART_IIR ) &0x0f ;if ((up->port.type ==PORT_RM9000 ) ? - (lsr &UART_LSR_THRE && - (iir ==UART_IIR_NO_INT || iir ==UART_IIR_THRI )) : - (lsr &UART_LSR_TEMT && iir &UART_IIR_NO_INT )) + (lsr &UART_LSR_THRE ) : + (lsr &UART_LSR_TEMT ))transmit_chars (up); } }
Anders - first patch to try (against 2.6.20):
serial/8250.c renamed from serial/8250.c~ +0 -19
@@ -1645,25 +1645,6 @@serial8250_set_mctrl (&up->port, up->port.mctrl); -/* -* Do a quick test to see if we receive an -* interrupt when we enable the TX irq. -*/ -serial_outp (up,UART_IER ,UART_IER_THRI ); - lsr =serial_in (up,UART_LSR ); - iir =serial_in (up,UART_IIR ); -serial_outp (up,UART_IER ,0 ); - -if (lsr &UART_LSR_TEMT && iir &UART_IIR_NO_INT ) { -if (!(up->bugs &UART_BUG_TXEN )) { - up->bugs |=UART_BUG_TXEN ; -pr_debug ("ttyS%d - enabling bad tx status workarounds\n" , - port->line); - } - }else { - up->bugs &= ~UART_BUG_TXEN ; - } -spin_unlock_irqrestore (&up->port.lock, flags);/*
Anders - second patch to try (against 2.6.20):
Fix should be suitable for distribution IMO.
serial/8250.c renamed from serial/8250.c~ +2 -3
@@ -1136,10 +1136,9 @@serial_out (up,UART_IER , up->ier);if (up->bugs &UART_BUG_TXEN ) { -unsigned char lsr, iir; +unsigned char lsr; lsr =serial_in (up,UART_LSR ); - iir =serial_in (up,UART_IIR ); -if (lsr &UART_LSR_TEMT && iir &UART_IIR_NO_INT ) +if (lsr &UART_LSR_TEMT )transmit_chars (up); } }
Re: [PATCH] IRQ handling race and spurious IIR read in serial/8250.c
Quoted 3 lines · I wrote:
In
drivers/serial/8250.cin Linux there are two bugs:
1.UART_BUG_TXENcan be spuriously set, due to an IRQ race
2. The workaround then applied by the kernel is itself buggy
Markus Armbruster has also experienced this problem in a Xen environment and has confirmed that my patch fixes it.
I think at the very least this change:
Quoted 9 lines
Proposed initial band-aid fix (against 2.6.28.4):
Do not read IIR in
serial8250_start_txwhenUART_BUG_TXENReading the IIR clears some oustanding interrupts so it is not safe. Instead, simply transmit immediately if the buffer is empty without regard to IIR.
Signed-off-by Ian Jackson <[email protected]>
should be made right away.
Ian.
Markus Armbruster
Acked-byRe: [Xen-devel] Re: [PATCH] IRQ handling race and spurious IIR read in serial/8250.c
Quoted 7 lines · Ian Jackson
Quoted 3 lines · I wrote:
In
drivers/serial/8250.cin Linux there are two bugs:
1.UART_BUG_TXENcan be spuriously set, due to an IRQ race
2. The workaround then applied by the kernel is itself buggyMarkus Armbruster has also experienced this problem in a Xen environment and has confirmed that my patch fixes it.
Correct.
Quoted 10 lines
I think at the very least this change:
Quoted 8 lines
Proposed initial band-aid fix (against 2.6.28.4):
Do not read IIR in
serial8250_start_txwhenUART_BUG_TXENReading the IIR clears some oustanding interrupts so it is not safe. Instead, simply transmit immediately if the buffer is empty without regard to IIR.
Quoted 5 lines
Quoted 1 line
Signed-off-by Ian Jackson <[email protected]>should be made right away.
Ian.
Patch makes sense to me.
Re: [PATCH] IRQ handling race and spurious IIR read in serial/8250.c
In reply to Ian JacksonAdded cc:
Quoted 154 lines · Ian Jackson
Anders Kaseorg writes ("Re: Serial console hangs with Linux 2.6.20 HVM guest"):
Quoted 2 lines
Yes, I took Linux v2.6.20 on amd64, ran
make defconfig, then ranmake menuconfigand turned offCONFIG_HOTPLUG_CPU(Processor type and features ò Support for hot-pluggable CPUs).Thanks. I think I have tracked down the bug. In
drivers/serial/8250.cin Linux there are two bugs:
1.UART_BUG_TXENcan be spuriously set, due to an IRQ race
2. The workaround then applied by the kernel is itself buggyAnders: can you try two tests for me ? Firstly, in
serial8250_startup, delete the section which setsUART_BUG_TXEN(see 2nd patch below); I think this will fix the symptoms for you. Secondly, inserial8250_start_txdelete the read from the IIR and the relevant branch of the text (see 3rd patch below); I think this will also in itself fix your symptoms. I haven't compiled either patch (so you may find that eg I missed deleting some variables).The bugs in detail (this discussion applies to 2.6.20 and also to 2.6.28.4):
1. The hunk of
serial8250_startupI quote below attempts to discover whether writing the IER re-asserts the THRI (transmit ready) interrupt. However the spinlock that it has taken out,port->lock, is not the one that the IRQ service routine takes before reading the IIR (i->lock). As a result, on an SMP system the generated interrupt races with the straight-line code inserial8250_startup.If
serial8250_startuploses the race (perhaps because the system is a VM and its VCPU got preempted),UART_BUG_TXENis spuriously added to bugs. This is quite unlikely in a normal system but in certain Xen configurations, particularly ones where there is CPU pressure, we may lose the race every time.It is not exactly clear to me how this ought to be resolved. One possibility is that the
UART_BUG_TXENproblem might be worked around perfectly well by the new and very similar workaround UART_BUG_THRE[1] in 2.6.21ish in which case it could just be removed.2. UART_BUG_TXEN's workaround appears to be intended to be harmless. However what it actually does is to read the IIR, thus clearing any actual interrupt (including incidentally non-THRI), and then only perform the intended servicing if the interrupt was _not_ asserted. That is, it breaks on any serial port with the bug.
As far as I can see there is not much use in
UART_BUG_TXENreading IIR at all, so a suitable change if we want to keepUART_BUG_TXENmight be the first patch I enclose below (again, not compiled or tested).If
UART_BUG_TXENis retained something along these lines should be done at the very least.Ian.
[1] http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=commit;h=40b36daad0ac704e6d5c1b75789f371ef5b053c1
in which case UARTProposed initial band-aid fix (against 2.6.28.4):
Do not read IIR in
serial8250_start_txwhenUART_BUG_TXENReading the IIR clears some oustanding interrupts so it is not safe. Instead, simply transmit immediately if the buffer is empty without regard to IIR.
Signed-off-by Ian Jackson <[email protected]>linux-2.6.28.4/drivers/serial/8250.c renamed from linux-2.6.28.4/drivers/serial/8250.c~ +3
-5@@ -1257,14 +1257,12 @@serial_out (up,UART_IER , up->ier);if (up->bugs &UART_BUG_TXEN ) { -unsigned char lsr, iir; +unsigned char lsr; lsr =serial_in (up,UART_LSR ); up->lsr_saved_flags |= lsr &LSR_SAVE_FLAGS ; - iir =serial_in (up,UART_IIR ) &0x0f ;if ((up->port.type ==PORT_RM9000 ) ? - (lsr &UART_LSR_THRE && - (iir ==UART_IIR_NO_INT || iir ==UART_IIR_THRI )) : - (lsr &UART_LSR_TEMT && iir &UART_IIR_NO_INT )) + (lsr &UART_LSR_THRE ) : + (lsr &UART_LSR_TEMT ))transmit_chars (up); } }Anders - first patch to try (against 2.6.20):
serial/8250.c renamed from serial/8250.c~ +0
-19@@ -1645,25 +1645,6 @@serial8250_set_mctrl (&up->port, up->port.mctrl); -/* -* Do a quick test to see if we receive an -* interrupt when we enable the TX irq. -*/ -serial_outp (up,UART_IER ,UART_IER_THRI ); - lsr =serial_in (up,UART_LSR ); - iir =serial_in (up,UART_IIR ); -serial_outp (up,UART_IER ,0 ); - -if (lsr &UART_LSR_TEMT && iir &UART_IIR_NO_INT ) { -if (!(up->bugs &UART_BUG_TXEN )) { - up->bugs |=UART_BUG_TXEN ; -pr_debug ("ttyS%d - enabling bad tx status workarounds\n" , - port->line); - } - }else { - up->bugs &= ~UART_BUG_TXEN ; - } -spin_unlock_irqrestore (&up->port.lock, flags);/* Anders - second patch to try (against 2.6.20):
Fix should be suitable for distribution IMO.Signed-off-by Ian Jackson <[email protected]>serial/8250.c renamed from serial/8250.c~ +2
-4@@ -1136,10 +1136,9 @@serial_out (up,UART_IER , up->ier);if (up->bugs &UART_BUG_TXEN ) { -unsigned char lsr, iir; +unsigned char lsr; lsr =serial_in (up,UART_LSR ); - iir =serial_in (up,UART_IIR ); -if (lsr &UART_LSR_TEMT && iir &UART_IIR_NO_INT ) +if (lsr &UART_LSR_TEMT )transmit_chars (up); } } --To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/