Skip to content

Commit d0b309a

Browse files
jognessgregkh
authored andcommitted
serial: 8250: synchronize and annotate UART_IER access
The UART_IER register is modified twice by each console write (serial8250_console_write()) under the port lock. Any driver code that accesses UART_IER must do so with the port locked in order to ensure consistent values, even when for read accesses. Add locking, lockdep notation, and/or comments everywhere UART_IER is accessed. The added locking is not fixing a real problem because it occurs where the console is not active. However, adding the locking to these non-critical paths greatly simplifies UART_IER access tracking by establishing a general policy that all UART_IER access is performed with the port locked. Signed-off-by: John Ogness <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 2561473 commit d0b309a

File tree

5 files changed

+85
-0
lines changed

5 files changed

+85
-0
lines changed

drivers/tty/serial/8250/8250.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,9 @@ static inline void serial_dl_write(struct uart_8250_port *up, u32 value)
179179

180180
static inline bool serial8250_set_THRI(struct uart_8250_port *up)
181181
{
182+
/* Port locked to synchronize UART_IER access against the console. */
183+
lockdep_assert_held_once(&up->port.lock);
184+
182185
if (up->ier & UART_IER_THRI)
183186
return false;
184187
up->ier |= UART_IER_THRI;
@@ -188,6 +191,9 @@ static inline bool serial8250_set_THRI(struct uart_8250_port *up)
188191

189192
static inline bool serial8250_clear_THRI(struct uart_8250_port *up)
190193
{
194+
/* Port locked to synchronize UART_IER access against the console. */
195+
lockdep_assert_held_once(&up->port.lock);
196+
191197
if (!(up->ier & UART_IER_THRI))
192198
return false;
193199
up->ier &= ~UART_IER_THRI;

drivers/tty/serial/8250/8250_aspeed_vuart.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,9 @@ static void __aspeed_vuart_set_throttle(struct uart_8250_port *up,
275275
{
276276
unsigned char irqs = UART_IER_RLSI | UART_IER_RDI;
277277

278+
/* Port locked to synchronize UART_IER access against the console. */
279+
lockdep_assert_held_once(&up->port.lock);
280+
278281
up->ier &= ~irqs;
279282
if (!throttle)
280283
up->ier |= irqs;

drivers/tty/serial/8250/8250_mtk.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,17 @@ static void mtk8250_shutdown(struct uart_port *port)
222222

223223
static void mtk8250_disable_intrs(struct uart_8250_port *up, int mask)
224224
{
225+
/* Port locked to synchronize UART_IER access against the console. */
226+
lockdep_assert_held_once(&up->port.lock);
227+
225228
serial_out(up, UART_IER, serial_in(up, UART_IER) & (~mask));
226229
}
227230

228231
static void mtk8250_enable_intrs(struct uart_8250_port *up, int mask)
229232
{
233+
/* Port locked to synchronize UART_IER access against the console. */
234+
lockdep_assert_held_once(&up->port.lock);
235+
230236
serial_out(up, UART_IER, serial_in(up, UART_IER) | mask);
231237
}
232238

@@ -235,6 +241,9 @@ static void mtk8250_set_flow_ctrl(struct uart_8250_port *up, int mode)
235241
struct uart_port *port = &up->port;
236242
int lcr = serial_in(up, UART_LCR);
237243

244+
/* Port locked to synchronize UART_IER access against the console. */
245+
lockdep_assert_held_once(&port->lock);
246+
238247
serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
239248
serial_out(up, MTK_UART_EFR, UART_EFR_ECB);
240249
serial_out(up, UART_LCR, lcr);

drivers/tty/serial/8250/8250_omap.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,10 @@ static void omap_8250_pm(struct uart_port *port, unsigned int state,
533533
u8 efr;
534534

535535
pm_runtime_get_sync(port->dev);
536+
537+
/* Synchronize UART_IER access against the console. */
538+
spin_lock_irq(&port->lock);
539+
536540
serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
537541
efr = serial_in(up, UART_EFR);
538542
serial_out(up, UART_EFR, efr | UART_EFR_ECB);
@@ -543,6 +547,8 @@ static void omap_8250_pm(struct uart_port *port, unsigned int state,
543547
serial_out(up, UART_EFR, efr);
544548
serial_out(up, UART_LCR, 0);
545549

550+
spin_unlock_irq(&port->lock);
551+
546552
pm_runtime_mark_last_busy(port->dev);
547553
pm_runtime_put_autosuspend(port->dev);
548554
}
@@ -760,8 +766,11 @@ static void omap_8250_shutdown(struct uart_port *port)
760766
if (priv->habit & UART_HAS_EFR2)
761767
serial_out(up, UART_OMAP_EFR2, 0x0);
762768

769+
/* Synchronize UART_IER access against the console. */
770+
spin_lock_irq(&port->lock);
763771
up->ier = 0;
764772
serial_out(up, UART_IER, 0);
773+
spin_unlock_irq(&port->lock);
765774
disable_irq_nosync(up->port.irq);
766775
dev_pm_clear_wake_irq(port->dev);
767776

@@ -803,6 +812,7 @@ static void omap_8250_unthrottle(struct uart_port *port)
803812

804813
pm_runtime_get_sync(port->dev);
805814

815+
/* Synchronize UART_IER access against the console. */
806816
spin_lock_irqsave(&port->lock, flags);
807817
priv->throttled = false;
808818
if (up->dma)
@@ -953,6 +963,7 @@ static void __dma_rx_complete(void *param)
953963
struct dma_tx_state state;
954964
unsigned long flags;
955965

966+
/* Synchronize UART_IER access against the console. */
956967
spin_lock_irqsave(&p->port.lock, flags);
957968

958969
/*
@@ -1227,6 +1238,9 @@ static u16 omap_8250_handle_rx_dma(struct uart_8250_port *up, u8 iir, u16 status
12271238
static void am654_8250_handle_rx_dma(struct uart_8250_port *up, u8 iir,
12281239
u16 status)
12291240
{
1241+
/* Port locked to synchronize UART_IER access against the console. */
1242+
lockdep_assert_held_once(&up->port.lock);
1243+
12301244
/*
12311245
* Queue a new transfer if FIFO has data.
12321246
*/

drivers/tty/serial/8250/8250_port.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,9 @@ EXPORT_SYMBOL_GPL(serial8250_rpm_put);
539539
*/
540540
static int serial8250_em485_init(struct uart_8250_port *p)
541541
{
542+
/* Port locked to synchronize UART_IER access against the console. */
543+
lockdep_assert_held_once(&p->port.lock);
544+
542545
if (p->em485)
543546
goto deassert_rts;
544547

@@ -677,6 +680,8 @@ static void serial8250_set_sleep(struct uart_8250_port *p, int sleep)
677680
serial8250_rpm_get(p);
678681

679682
if (p->capabilities & UART_CAP_SLEEP) {
683+
/* Synchronize UART_IER access against the console. */
684+
spin_lock_irq(&p->port.lock);
680685
if (p->capabilities & UART_CAP_EFR) {
681686
lcr = serial_in(p, UART_LCR);
682687
efr = serial_in(p, UART_EFR);
@@ -690,13 +695,17 @@ static void serial8250_set_sleep(struct uart_8250_port *p, int sleep)
690695
serial_out(p, UART_EFR, efr);
691696
serial_out(p, UART_LCR, lcr);
692697
}
698+
spin_unlock_irq(&p->port.lock);
693699
}
694700

695701
serial8250_rpm_put(p);
696702
}
697703

698704
static void serial8250_clear_IER(struct uart_8250_port *up)
699705
{
706+
/* Port locked to synchronize UART_IER access against the console. */
707+
lockdep_assert_held_once(&up->port.lock);
708+
700709
if (up->capabilities & UART_CAP_UUE)
701710
serial_out(up, UART_IER, UART_IER_UUE);
702711
else
@@ -969,6 +978,9 @@ static void autoconfig_16550a(struct uart_8250_port *up)
969978
unsigned char status1, status2;
970979
unsigned int iersave;
971980

981+
/* Port locked to synchronize UART_IER access against the console. */
982+
lockdep_assert_held_once(&up->port.lock);
983+
972984
up->port.type = PORT_16550A;
973985
up->capabilities |= UART_CAP_FIFO;
974986

@@ -1152,6 +1164,8 @@ static void autoconfig(struct uart_8250_port *up)
11521164
/*
11531165
* We really do need global IRQs disabled here - we're going to
11541166
* be frobbing the chips IRQ enable register to see if it exists.
1167+
*
1168+
* Synchronize UART_IER access against the console.
11551169
*/
11561170
spin_lock_irqsave(&port->lock, flags);
11571171

@@ -1324,7 +1338,10 @@ static void autoconfig_irq(struct uart_8250_port *up)
13241338
/* forget possible initially masked and pending IRQ */
13251339
probe_irq_off(probe_irq_on());
13261340
save_mcr = serial8250_in_MCR(up);
1341+
/* Synchronize UART_IER access against the console. */
1342+
spin_lock_irq(&port->lock);
13271343
save_ier = serial_in(up, UART_IER);
1344+
spin_unlock_irq(&port->lock);
13281345
serial8250_out_MCR(up, UART_MCR_OUT1 | UART_MCR_OUT2);
13291346

13301347
irqs = probe_irq_on();
@@ -1336,7 +1353,10 @@ static void autoconfig_irq(struct uart_8250_port *up)
13361353
serial8250_out_MCR(up,
13371354
UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
13381355
}
1356+
/* Synchronize UART_IER access against the console. */
1357+
spin_lock_irq(&port->lock);
13391358
serial_out(up, UART_IER, UART_IER_ALL_INTR);
1359+
spin_unlock_irq(&port->lock);
13401360
serial_in(up, UART_LSR);
13411361
serial_in(up, UART_RX);
13421362
serial_in(up, UART_IIR);
@@ -1346,7 +1366,10 @@ static void autoconfig_irq(struct uart_8250_port *up)
13461366
irq = probe_irq_off(irqs);
13471367

13481368
serial8250_out_MCR(up, save_mcr);
1369+
/* Synchronize UART_IER access against the console. */
1370+
spin_lock_irq(&port->lock);
13491371
serial_out(up, UART_IER, save_ier);
1372+
spin_unlock_irq(&port->lock);
13501373

13511374
if (port->flags & UPF_FOURPORT)
13521375
outb_p(save_ICP, ICP);
@@ -1361,6 +1384,9 @@ static void serial8250_stop_rx(struct uart_port *port)
13611384
{
13621385
struct uart_8250_port *up = up_to_u8250p(port);
13631386

1387+
/* Port locked to synchronize UART_IER access against the console. */
1388+
lockdep_assert_held_once(&port->lock);
1389+
13641390
serial8250_rpm_get(up);
13651391

13661392
up->ier &= ~(UART_IER_RLSI | UART_IER_RDI);
@@ -1380,6 +1406,9 @@ void serial8250_em485_stop_tx(struct uart_8250_port *p)
13801406
{
13811407
unsigned char mcr = serial8250_in_MCR(p);
13821408

1409+
/* Port locked to synchronize UART_IER access against the console. */
1410+
lockdep_assert_held_once(&p->port.lock);
1411+
13831412
if (p->port.rs485.flags & SER_RS485_RTS_AFTER_SEND)
13841413
mcr |= UART_MCR_RTS;
13851414
else
@@ -1429,6 +1458,9 @@ static void __stop_tx_rs485(struct uart_8250_port *p, u64 stop_delay)
14291458
{
14301459
struct uart_8250_em485 *em485 = p->em485;
14311460

1461+
/* Port locked to synchronize UART_IER access against the console. */
1462+
lockdep_assert_held_once(&p->port.lock);
1463+
14321464
stop_delay += (u64)p->port.rs485.delay_rts_after_send * NSEC_PER_MSEC;
14331465

14341466
/*
@@ -1608,6 +1640,9 @@ static void serial8250_start_tx(struct uart_port *port)
16081640
struct uart_8250_port *up = up_to_u8250p(port);
16091641
struct uart_8250_em485 *em485 = up->em485;
16101642

1643+
/* Port locked to synchronize UART_IER access against the console. */
1644+
lockdep_assert_held_once(&port->lock);
1645+
16111646
if (!port->x_char && uart_circ_empty(&port->state->xmit))
16121647
return;
16131648

@@ -1635,6 +1670,9 @@ static void serial8250_disable_ms(struct uart_port *port)
16351670
{
16361671
struct uart_8250_port *up = up_to_u8250p(port);
16371672

1673+
/* Port locked to synchronize UART_IER access against the console. */
1674+
lockdep_assert_held_once(&port->lock);
1675+
16381676
/* no MSR capabilities */
16391677
if (up->bugs & UART_BUG_NOMSR)
16401678
return;
@@ -1649,6 +1687,9 @@ static void serial8250_enable_ms(struct uart_port *port)
16491687
{
16501688
struct uart_8250_port *up = up_to_u8250p(port);
16511689

1690+
/* Port locked to synchronize UART_IER access against the console. */
1691+
lockdep_assert_held_once(&port->lock);
1692+
16521693
/* no MSR capabilities */
16531694
if (up->bugs & UART_BUG_NOMSR)
16541695
return;
@@ -2105,6 +2146,14 @@ static void serial8250_put_poll_char(struct uart_port *port,
21052146
unsigned int ier;
21062147
struct uart_8250_port *up = up_to_u8250p(port);
21072148

2149+
/*
2150+
* Normally the port is locked to synchronize UART_IER access
2151+
* against the console. However, this function is only used by
2152+
* KDB/KGDB, where it may not be possible to acquire the port
2153+
* lock because all other CPUs are quiesced. The quiescence
2154+
* should allow safe lockless usage here.
2155+
*/
2156+
21082157
serial8250_rpm_get(up);
21092158
/*
21102159
* First save the IER then disable the interrupts
@@ -2440,6 +2489,8 @@ void serial8250_do_shutdown(struct uart_port *port)
24402489
serial8250_rpm_get(up);
24412490
/*
24422491
* Disable interrupts from this port
2492+
*
2493+
* Synchronize UART_IER access against the console.
24432494
*/
24442495
spin_lock_irqsave(&port->lock, flags);
24452496
up->ier = 0;
@@ -2739,6 +2790,8 @@ serial8250_do_set_termios(struct uart_port *port, struct ktermios *termios,
27392790
/*
27402791
* Ok, we're now changing the port state. Do it with
27412792
* interrupts disabled.
2793+
*
2794+
* Synchronize UART_IER access against the console.
27422795
*/
27432796
serial8250_rpm_get(up);
27442797
spin_lock_irqsave(&port->lock, flags);

0 commit comments

Comments
 (0)