V2 pci uart - better cope with UART being temporarily unavailable
PATCH 1/1 V2 pci uart - better cope with UART being temporarily unavailable
This happens for example when dom0 disables ioport responses during PCI subsystem initialisation. If a __ns16550_poll() happens to be scheduled during that time, Xen hangs. Detect and exit that condition.
Amended ns16550_ioport_invalid function to only check IER register, which contins 3 reserved (always 0) bits, therefore it's sufficient for that test.
Changes since V1:
* added invalid port test in getc()
* changed ns16550_tx_ready to return -EIO and code in serial.[ch] to cope with that error condition. If port is temporarily unavailable tx_ready will return -EIO and serial driver code will attempt to buffer the character instead of dropping it.
--- xen/drivers/char/ehci-dbgp.c | 2 +- xen/drivers/char/ns16550.c | 33 +++++++++++++++++++-------------- xen/drivers/char/serial.c | 41 ++++++++++++++++++++++++++--------------- xen/include/xen/serial.h | 5 +++-- 4 files changed, 49 insertions(+), 32 deletions(-)
xen/drivers/char/ehci-dbgp.c +1 -1
@@ -1204,7 +1204,7 @@ static void ehci_dbgp_putc(struct serial_port *port, char c)ehci_dbgp_flush (port); } -static unsigned int ehci_dbgp_tx_ready (struct serial_port *port) +static int ehci_dbgp_tx_ready (struct serial_port *port) {struct ehci_dbgp *dbgp = port->uart;
xen/drivers/char/ns16550.c +19 -14
@@ -73,6 +73,11 @@ static void ns_write_reg(struct ns16550 *uart, int reg, char c)writeb (c, uart->remapped_io_base + reg); } +static int ns16550_ioport_invalid (struct ns16550 *uart) +{ +return (unsigned char )ns_read_reg (uart,UART_IER ) ==0xff ; +} +static void ns16550_interrupt (int irq,void *dev_id,struct cpu_user_regs *regs) {
@@ -102,12 +107,17 @@ static void __ns16550_poll(struct cpu_user_regs *regs)if ( uart->intr_works )return ;/* Interrupts work - no more polling */ -while (ns_read_reg (uart,UART_LSR ) &UART_LSR_DR ) -serial_rx_interrupt (port, regs); +while (ns_read_reg (uart,UART_LSR ) &UART_LSR_DR ) + { +serial_rx_interrupt (port, regs); +if (ns16550_ioport_invalid (uart) ) + goto out; + }if (ns_read_reg (uart,UART_LSR ) &UART_LSR_THRE )serial_tx_interrupt (port, regs); +out:set_timer (&uart->timer,NOW () +MILLISECS (uart->timeout_ms)); }
@@ -121,11 +131,14 @@ static void ns16550_poll(void *data)#endif } -static unsigned int ns16550_tx_ready (struct serial_port *port) +static int ns16550_tx_ready (struct serial_port *port) {struct ns16550 *uart = port->uart; -return ns_read_reg (uart,UART_LSR ) &UART_LSR_THRE ? uart->fifo_size :0 ; +if (ns16550_ioport_invalid (uart) ) +return -EIO ; +else +return ns_read_reg (uart,UART_LSR ) &UART_LSR_THRE ? uart->fifo_size :0 ; }static void ns16550_putc (struct serial_port *port,char c)
@@ -138,7 +151,8 @@ static int ns16550_getc(struct serial_port *port, char *pc) {struct ns16550 *uart = port->uart; -if ( !(ns_read_reg (uart,UART_LSR ) &UART_LSR_DR ) ) +if (ns16550_ioport_invalid (uart) || + !(ns_read_reg (uart,UART_LSR ) &UART_LSR_DR ) )return 0 ; *pc =ns_read_reg (uart,UART_RBR );
@@ -305,15 +319,6 @@ static void _ns16550_resume(struct serial_port *port)ns16550_setup_postirq (port->uart); } -static int ns16550_ioport_invalid (struct ns16550 *uart) -{ -return ((((unsigned char )ns_read_reg (uart,UART_LSR )) ==0xff ) && - (((unsigned char )ns_read_reg (uart,UART_MCR )) ==0xff ) && - (((unsigned char )ns_read_reg (uart,UART_IER )) ==0xff ) && - (((unsigned char )ns_read_reg (uart,UART_IIR )) ==0xff ) && - (((unsigned char )ns_read_reg (uart,UART_LCR )) ==0xff )); -} -static int delayed_resume_tries;static void ns16550_delayed_resume (void *data) {
xen/drivers/char/serial.c +26 -15
84 lines not on this page · raw message
xen/include/xen/serial.h +3 -2
@@ -70,8 +70,9 @@ struct uart_driver {/* Driver suspend/resume. */ void (*suspend)(struct serial_port *);void (*resume)(struct serial_port *); -/* Return number of characters the port can hold for transmit. */ -unsigned int (*tx_ready)(struct serial_port *); +/* Return number of characters the port can hold for transmit, +* or -EIO if port is inaccesible */ +int (*tx_ready)(struct serial_port *);/* Put a character onto the serial line. */ void (*putc)(struct serial_port *,char );/* Flush accumulated characters. */
Signature · 1 line
1.7.10.4
Quoted 8 lines
Quoted 1 line
Quoted 1 line · Tomasz Wroblewski
xen/drivers/char/ns16550.c +1
-2@@ -102,12 +107,17 @@ static void __ns16550_poll(struct cpu_user_regs *regs)if ( uart->intr_works )return ;/* Interrupts work - no more polling */ -while (ns_read_reg (uart,UART_LSR ) &UART_LSR_DR ) -serial_rx_interrupt (port, regs); +while (ns_read_reg (uart,UART_LSR ) &UART_LSR_DR )
xen/drivers/char/ns16550.c static void __ns16550_poll(struct cpu_user_regs *regs) · line 120
Quoted 10 lines
xen/drivers/char/ns16550.c +6
-0+ { +serial_rx_interrupt (port, regs); +if (ns16550_ioport_invalid (uart) ) + goto out; + }if (ns_read_reg (uart,UART_LSR ) &UART_LSR_THRE )serial_tx_interrupt (port, regs); +out:
So serial_rx_interrupt() gets run once in that case, but serial_tx_interrupt() not at all? That not only inconsistent, I also can't see why anything would need to be done here at all in this case. Plus doing the check before the loop would shrink patch size.
xen/drivers/char/ns16550.c static void ns16550_poll(void *data) · line 141
Quoted 9 lines
xen/drivers/char/ns16550.c +5
-1+static int ns16550_tx_ready (struct serial_port *port) {struct ns16550 *uart = port->uart; -return ns_read_reg (uart,UART_LSR ) &UART_LSR_THRE ? uart->fifo_size :0 ; +if (ns16550_ioport_invalid (uart) ) +return -EIO ; +else +return ns_read_reg (uart,UART_LSR ) &UART_LSR_THRE ? uart->fifo_size :0 ;
Dropping the unnecessary "else" would again shrink patch size.
Quoted 9 lines
xen/drivers/char/serial.c +1
-1@@ -111,15 +111,18 @@ static void __serial_putc(struct serial_port *port, char c)if ( port->tx_log_everything ) {/* Buffer is full: we spin waiting for space to appear. */ -unsigned int n; +int n;
xen/drivers/char/serial.c static void __serial_putc(struct serial_port *port, char c) · line 120
Quoted 10 lines
xen/drivers/char/serial.c +3
-5while ( (n = port->driver->tx_ready (port)) ==0 )cpu_relax (); -while ( n-- ) - port->driver->putc ( - port, - port->txbuf[mask_serial_txbuf_idx (port->txbufc++)]); - port->txbuf[mask_serial_txbuf_idx (port->txbufp++)] = c; +if (n >0 ) + { +while ( n-- )
"while ( n-- > 0 )" can achieve the same without the extra if() and with just a one line change.
xen/drivers/char/serial.c static void __serial_putc(struct serial_port *port, char c) · line 149
Quoted 13 lines
xen/drivers/char/serial.c +6
-0+ port->driver->putc ( + port, + port->txbuf[mask_serial_txbuf_idx (port->txbufc++)]); + port->txbuf[mask_serial_txbuf_idx (port->txbufp++)] = c; + } }else {@@ -143,10 +146,12 @@ static void __serial_putc(struct serial_port *port, char c) }else if ( port->driver->tx_ready ) { +int n;
Missing blank line.
xen/drivers/char/serial.c static void __serial_putc(struct serial_port *port, char c) · line 153
Quoted 6 lines
xen/drivers/char/serial.c +2
-2/* Synchronous finite-capacity transmitter. */ -while ( !port->driver->tx_ready (port) ) +while ( !(n = port->driver->tx_ready (port)) )cpu_relax (); - port->driver->putc (port, c); +if (n >0 )
Coding style.
xen/drivers/char/serial.c void serial_start_sync(int handle) · line 398
Quoted 10 lines
xen/drivers/char/serial.c +2
-1+ port->driver->putc (port, c); }else {@@ -390,10 +395,16 @@ void serial_start_sync(int handle) {while ( (port->txbufp - port->txbufc) !=0 ) { -while ( !port->driver->tx_ready (port) ) +int n;
Again missing blank line.
xen/drivers/char/serial.c void serial_start_sync(int handle) · line 401
Quoted 5 lines
xen/drivers/char/serial.c +2
-2+while ( !(n = port->driver->tx_ready (port)) )cpu_relax (); - port->driver->putc ( - port, port->txbuf[mask_serial_txbuf_idx (port->txbufc++)]); +if (n >0 )
Again coding style.
xen/drivers/char/serial.c void serial_start_sync(int handle) · line 407
Quoted 6 lines
xen/drivers/char/serial.c +6
-0+ port->driver->putc ( + port, port->txbuf[mask_serial_txbuf_idx (port->txbufc++)]); + else +/* port is unavailable and might not come up until reenabled by +dom0, we can't really do proper sync */ + break;
Once again, patch size would be smaller if you handled the n < 0 case first and without "else".
Jan
Quoted 10 lines
Quoted 10 lines
xen/drivers/char/serial.c +1
-1@@ -111,15 +111,18 @@ static void __serial_putc(struct serial_port *port, char c)if ( port->tx_log_everything ) {/* Buffer is full: we spin waiting for space to appear. */ -unsigned int n; +int n;
Quoted 12 lines
xen/drivers/char/serial.c static void __serial_putc(struct serial_port *port, char c) · line 120
Quoted 10 lines
xen/drivers/char/serial.c +3
-5while ( (n = port->driver->tx_ready (port)) ==0 )cpu_relax (); -while ( n-- ) - port->driver->putc ( - port, - port->txbuf[mask_serial_txbuf_idx (port->txbufc++)]); - port->txbuf[mask_serial_txbuf_idx (port->txbufp++)] = c; +if (n>0 ) + { +while ( n-- )"while ( n--> 0 )" can achieve the same without the extra
if()and with just a one line change.
Thanks for review - agree with most comments except this one, we need to avoid executing "port->txbuf[mask_serial_txbuf_idx(port->txbufp++)] = c" if port is offline, since in this case the while loop will not consume pull any character out of the buffer, and it will stay full, and we should just drop the char. The "if" I added pulled the buffer insert into its body.
Quoted 22 lines
Quoted 5 lines
xen/drivers/char/serial.c static void __serial_putc(struct serial_port *port,
Quoted 5 lines · Tomasz Wroblewski
xen/drivers/char/serial.c +0
-0@@ -111,15 +111,18 @@ static void __serial_putc(struct serial_port *port,char c)
Quoted 16 lines
Quoted 16 lines
xen/drivers/char/serial.c +4
-6if ( port->tx_log_everything ) {/* Buffer is full: we spin waiting for space to appear. */ -unsigned int n; +int n;while ( (n = port->driver->tx_ready (port)) ==0 )cpu_relax (); -while ( n-- ) - port->driver->putc ( - port, - port->txbuf[mask_serial_txbuf_idx (port->txbufc++)]); - port->txbuf[mask_serial_txbuf_idx (port->txbufp++)] = c; +if (n>0 ) + { +while ( n-- )
Quoted 8 lines
Quoted 2 lines
"while ( n--> 0 )" can achieve the same without the extra
if()and with just a one line change.Thanks for review - agree with most comments except this one, we need to avoid executing "port->txbuf[mask_serial_txbuf_idx(port->txbufp++)] = c" if port is offline, since in this case the while loop will not consume pull any character out of the buffer, and it will stay full, and we should just drop the char. The "if" I added pulled the buffer insert into its body.
Indeed. However, we're in a log-everything section, so dropping characters here is not really right. Not sure what to do about this.
Jan
Quoted 17 lines · Jan Beulich
Quoted 17 lines
Quoted 1 line
Quoted 1 line · Tomasz Wroblewski
xen/drivers/char/ns16550.c +6
-2@@ -102,12 +107,17 @@ static void __ns16550_poll(struct cpu_user_regs *regs)if ( uart->intr_works )return ;/* Interrupts work - no more polling */ -while (ns_read_reg (uart,UART_LSR )&UART_LSR_DR ) -serial_rx_interrupt (port, regs); +while (ns_read_reg (uart,UART_LSR )&UART_LSR_DR ) + { +serial_rx_interrupt (port, regs); +if (ns16550_ioport_invalid (uart) ) + goto out; + }if (ns_read_reg (uart,UART_LSR )&UART_LSR_THRE )serial_tx_interrupt (port, regs);
Quoted 6 lines
xen/drivers/char/ns16550.c static void __ns16550_poll(struct cpu_user_regs *regs) · line 120
Quoted 1 line
xen/drivers/char/ns16550.c +1
-0+out:So
serial_rx_interrupt()gets run once in that case, butserial_tx_interrupt()not at all? That not only inconsistent, I also can't see why anything would need to be done here at all in this case. Plus doing the check before the loop would shrink patch size.
So I presume it is impossible for dom0 code to run on another cpu whilst xen is executing the poll routine, I was not sure at all about this? Would like to avoid the possibility of dom0 disabling device whilst this loop is running.
Quoted 25 lines
Quoted 1 line
Quoted 1 line · Tomasz Wroblewski
Quoted 17 lines · Jan Beulich
Quoted 17 lines
Quoted 1 line
Quoted 1 line · Tomasz Wroblewski
xen/drivers/char/ns16550.c +6
-2@@ -102,12 +107,17 @@ static void __ns16550_poll(struct cpu_user_regs *regs)if ( uart->intr_works )return ;/* Interrupts work - no more polling */ -while (ns_read_reg (uart,UART_LSR )&UART_LSR_DR ) -serial_rx_interrupt (port, regs); +while (ns_read_reg (uart,UART_LSR )&UART_LSR_DR ) + { +serial_rx_interrupt (port, regs); +if (ns16550_ioport_invalid (uart) ) + goto out; + }if (ns_read_reg (uart,UART_LSR )&UART_LSR_THRE )serial_tx_interrupt (port, regs);Quoted 6 lines
xen/drivers/char/ns16550.c static void __ns16550_poll(struct cpu_user_regs *regs) · line 120
Quoted 1 line
xen/drivers/char/ns16550.c +1
-0+out:So
serial_rx_interrupt()gets run once in that case, butserial_tx_interrupt()not at all? That not only inconsistent, I also can't see why anything would need to be done here at all in this case. Plus doing the check before the loop would shrink patch size.
Quoted 4 lines
So I presume it is impossible for dom0 code to run on another cpu whilst xen is executing the poll routine, I was not sure at all about this? Would like to avoid the possibility of dom0 disabling device whilst this loop is running.
Of course can a Dom0 vCPU run on another pCPU.
And yes, I hence appreciate reducing the risk window. But then please do the check before calling serial_rx_interrupt().
Jan
Quoted 25 lines · Jan Beulich
Quoted 25 lines
Quoted 1 line
Quoted 1 line · Tomasz Wroblewski
Quoted 23 lines · Jan Beulich
Quoted 8 lines
Quoted 1 line
Quoted 1 line · Tomasz Wroblewski
xen/drivers/char/ns16550.c +1
-2@@ -102,12 +107,17 @@ static void __ns16550_poll(struct cpu_user_regs *regs)if ( uart->intr_works )return ;/* Interrupts work - no more polling */ -while (ns_read_reg (uart,UART_LSR )&UART_LSR_DR ) -serial_rx_interrupt (port, regs); +while (ns_read_reg (uart,UART_LSR )&UART_LSR_DR )xen/drivers/char/ns16550.c static void __ns16550_poll(struct cpu_user_regs *regs) · line 120
Quoted 10 lines
xen/drivers/char/ns16550.c +6
-0+ { +serial_rx_interrupt (port, regs); +if (ns16550_ioport_invalid (uart) ) + goto out; + }if (ns_read_reg (uart,UART_LSR )&UART_LSR_THRE )serial_tx_interrupt (port, regs); +out:So
serial_rx_interrupt()gets run once in that case, butserial_tx_interrupt()not at all? That not only inconsistent, I also can't see why anything would need to be done here at all in this case. Plus doing the check before the loop would shrink patch size.
Quoted 9 lines
Quoted 4 lines
So I presume it is impossible for dom0 code to run on another cpu whilst xen is executing the poll routine, I was not sure at all about this? Would like to avoid the possibility of dom0 disabling device whilst this loop is running.
Of course can a Dom0 vCPU run on another pCPU.
And yes, I hence appreciate reducing the risk window. But then please do the check before calling
serial_rx_interrupt().
Ah sure, will repost v4 soon then