Skip to contentSkip to conversation

synchronize UART_IER access against console write

PATCH In review John Ogness 4 participants 16 messages May 25, 2023 – May 26, 2023

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover synchronize UART_IER access against console write

Hi,

In preparation for making the 8250 serial driver the first driver to support the upcoming atomic consoles [0], its console write() callback (serial8250_console_write) was evaluated. For this callback of the 8250 driver there are two critical writes to the UART_IER
register: once to disable all interrupts before transmitting a line of text, and again after transmit to re-enable the previously enabled interrupts. These two writes are performed under a single synchronized section protected by the port lock.

I then checked all other access to UART_IER in the 8250 driver to see if they always occurred under the port lock. If not, it would be possible that the console write() callback could overwrite or restore incorrect values to UART_IER. This is illustrated in the commit message of the first patch.

Indeed several call sites were discovered where UART_IER is accessed without the port lock. This series adds the missing locking in order to ensure UART_IER access is always synchronized against the console write() callback.

For call sites where UART_IER access was already performed under the port lock, this series adds code comments and (when appropriate) lockdep notation to help catch any future issues that may creep in.

Note that some of the new usage of port lock is not strictly necessary, because (for example) the console is disabled before it is suspended. However, these are not hot paths and by taking the port lock it simplifies the synchronization semantics for UART_IER to allow general lockdep usage.

Also note that none of these patches have been tagged for stable. The possible stable candidates do include Fixes tags. But since the fixes are not based on real-world reports, it probably is not necessary to backport them.

John Ogness

[0] https://lore.kernel.org/lkml/20230302195618.156940-1-john.ogness at linutronix.de

John Ogness (8):
serial: 8250: lock port in startup() callbacks
serial: core: lock port for stop_rx() in uart_suspend_port()
serial: 8250: lock port for stop_rx() in omap8250_irq()
serial: core: lock port for start_rx() in uart_resume_port()
serial: 8250: lock port for rx_dma() callback
serial: 8250: lock port for omap8250_restore_regs()
serial: 8250: lock port for UART_IER access in omap8250_irq()
serial: 8250: synchronize and annotate UART_IER access

 drivers/tty/serial/8250/8250.h              |  6 ++
 drivers/tty/serial/8250/8250_aspeed_vuart.c |  3 +
 drivers/tty/serial/8250/8250_bcm7271.c      |  4 ++
 drivers/tty/serial/8250/8250_exar.c         |  4 ++
 drivers/tty/serial/8250/8250_mtk.c          |  9 +++
 drivers/tty/serial/8250/8250_omap.c         | 41 +++++++++++-
 drivers/tty/serial/8250/8250_port.c         | 71 ++++++++++++++++++++-
 drivers/tty/serial/serial_core.c            | 10 ++-
 8 files changed, 141 insertions(+), 7 deletions(-)

base-commit: d5b3d02d0b107345f2a6ecb5b06f98356f5c97ab

Signature · 1 line
2.30.2
#1 lore raw

PATCH 1/8 serial: 8250: lock port in startup() callbacks

uart_ops startup() callback is called without interrupts disabled and without port->lock locked, relatively late during the boot process (from the call path of console_on_rootfs()). If the device is a console, it was already previously registered and could be actively printing messages.

The console printing function serial8250_console_write() modifies the interrupt register (UART_IER) under the port->lock with the
pattern: read, clear, restore.

Since some startup() callbacks are modifying UART_IER without the port->lock locked, it is possible that the value intended to be written by the startup() callback will get overwritten and be lost.

CPU0                           CPU1
serial8250_console_write       omap_8250_startup
--------------------------     -----------------

spin_lock(port->lock)
oldval = read(UART_IER)
uart_console_write()
write(newval, UART_IER)
write(oldval, UART_IER) spin_unlock(port->lock)

Add port->lock synchronization to the 8250 startup() callbacks where they need to access UART_IER. This avoids racing with serial8250_console_write().

---
 drivers/tty/serial/8250/8250_bcm7271.c |  4 ++++
 drivers/tty/serial/8250/8250_exar.c    |  4 ++++
 drivers/tty/serial/8250/8250_omap.c    |  3 +++
 drivers/tty/serial/8250/8250_port.c    | 18 ++++++++++++++++--
 4 files changed, 27 insertions(+), 2 deletions(-)
drivers/tty/serial/8250/8250_bcm7271.c +4 -0
index f801b1f5b46c..2b38bcc5112e 100644
@@ -605,9 +605,13 @@ static int brcmuart_startup(struct uart_port *port)
 	/*
 	 * Disable the Receive Data Interrupt because the DMA engine
 	 * will handle this.
+	 *
+	 * Synchronize UART_IER access against the console.
 	 */
+	spin_lock_irq(&port->lock);
 	up->ier &= ~UART_IER_RDI;
 	serial_port_out(port, UART_IER, up->ier);
+	spin_unlock_irq(&port->lock);
 
 	priv->tx_running = false;
 	priv->dma.rx_dma = NULL;
drivers/tty/serial/8250/8250_exar.c +4 -0
index 64770c62bbec..ae66ef9d863c 100644
@@ -194,8 +194,12 @@ static int xr17v35x_startup(struct uart_port *port)
 	/*
 	 * Make sure all interrups are masked until initialization is
 	 * complete and the FIFOs are cleared
+	 *
+	 * Synchronize UART_IER access against the console.
 	 */
+	spin_lock_irq(&port->lock);
 	serial_port_out(port, UART_IER, 0);
+	spin_unlock_irq(&port->lock);
 
 	return serial8250_do_startup(port);
 }
drivers/tty/serial/8250/8250_omap.c +3 -0
index 5c093dfcee1d..fbca0692aa51 100644
@@ -710,8 +710,11 @@ static int omap_8250_startup(struct uart_port *port)
 		up->dma = NULL;
 	}
 
+	/* Synchronize UART_IER access against the console. */
+	spin_lock_irq(&port->lock);
 	up->ier = UART_IER_RLSI | UART_IER_RDI;
 	serial_out(up, UART_IER, up->ier);
+	spin_unlock_irq(&port->lock);
 
 #ifdef CONFIG_PM
 	up->capabilities |= UART_CAP_RPM;
drivers/tty/serial/8250/8250_port.c +16 -2
index 0cef9bfd0471..b3971302d8e5 100644
@@ -2149,7 +2149,12 @@ int serial8250_do_startup(struct uart_port *port)
 
 	serial8250_rpm_get(up);
 	if (port->type == PORT_16C950) {
-		/* Wake up and initialize UART */
+		/*
+		 * Wake up and initialize UART
+		 *
+		 * Synchronize UART_IER access against the console.
+		 */
+		spin_lock_irqsave(&port->lock, flags);
 		up->acr = 0;
 		serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
 		serial_port_out(port, UART_EFR, UART_EFR_ECB);
@@ -2159,12 +2164,19 @@ int serial8250_do_startup(struct uart_port *port)
 		serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
 		serial_port_out(port, UART_EFR, UART_EFR_ECB);
 		serial_port_out(port, UART_LCR, 0);
+		spin_unlock_irqrestore(&port->lock, flags);
 	}
 
 	if (port->type == PORT_DA830) {
-		/* Reset the port */
+		/*
+		 * Reset the port
+		 *
+		 * Synchronize UART_IER access against the console.
+		 */
+		spin_lock_irqsave(&port->lock, flags);
 		serial_port_out(port, UART_IER, 0);
 		serial_port_out(port, UART_DA830_PWREMU_MGMT, 0);
+		spin_unlock_irqrestore(&port->lock, flags);
 		mdelay(10);
 
 		/* Enable Tx, Rx and free run mode */
@@ -2275,6 +2287,8 @@ int serial8250_do_startup(struct uart_port *port)
 		 * this interrupt whenever the transmitter is idle and
 		 * the interrupt is enabled.  Delays are necessary to
 		 * allow register changes to become visible.
+		 *
+		 * Synchronize UART_IER access against the console.
 		 */
 		spin_lock_irqsave(&port->lock, flags);
 
Signature · 1 line
2.30.2
#2 lore raw

PATCH 2/8 serial: core: lock port for stop_rx() in uart_suspend_port()

In reply to John Ogness

The uarts_ops stop_rx() callback expects that the port->lock is taken and interrupts are disabled.

Fixes c9d2325cdb92 ("serial: core: Do stop_rx in suspend path for console if console_suspend is disabled")
---
 drivers/tty/serial/serial_core.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/tty/serial/serial_core.c +4 -1
index 4b98d13555c0..37ad53616372 100644
@@ -2333,8 +2333,11 @@ int uart_suspend_port(struct uart_driver *drv, struct uart_port *uport)
 	 * able to Re-start_rx later.
 	 */
 	if (!console_suspend_enabled && uart_console(uport)) {
-		if (uport->ops->start_rx)
+		if (uport->ops->start_rx) {
+			spin_lock_irq(&uport->lock);
 			uport->ops->stop_rx(uport);
+			spin_unlock_irq(&uport->lock);
+		}
 		goto unlock;
 	}
 
Signature · 1 line
2.30.2
#3 lore raw

RE: [PATCH tty v1 2/8] serial: core: lock port for stop_rx() in uart_suspend_port()

++Doug

Hi,

Quoted 29 lines

-----Original Message-----
From: John Ogness <[email protected]>
Sent: Thursday, May 25, 2023 3:02 PM
To: Greg Kroah-Hartman <[email protected]>
Cc: Petr Mladek <[email protected]>; Thomas Gleixner <[email protected]>; [email protected]; Jiri Slaby <[email protected]>; Vijaya Krishna Nivarthi (Temp) (QUIC) <[email protected]>; [email protected]
Subject: [PATCH tty v1 2/8] serial: core: lock port for stop_rx() in uart_suspend_port()

WARNING: This email originated from outside of Qualcomm. Please be wary of any links or attachments, and do not enable macros.

The uarts_ops stop_rx() callback expects that the port->lock is taken and interrupts are disabled.

Fixes c9d2325cdb92 ("serial: core: Do stop_rx in suspend path for console if

console_suspend is disabled")

---
 drivers/tty/serial/serial_core.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/tty/serial/serial_core.c +0 -0
index 4b98d13555c0..37ad53616372 100644

drivers/tty/serial/serial_core.c int uart_suspend_port(struct uart_driver *drv, · line 2341

Quoted 10 lines
drivers/tty/serial/serial_core.c +4 -1
@@ -2333,8 +2333,11 @@ int uart_suspend_port(struct uart_driver *drv,
 struct uart_port *uport)
         * able to Re-start_rx later.
         */
        if (!console_suspend_enabled && uart_console(uport)) {
-               if (uport->ops->start_rx)
+               if (uport->ops->start_rx) {
+                       spin_lock_irq(&uport->lock);
                        uport->ops->stop_rx(uport);
+                       spin_unlock_irq(&uport->lock);
+               }

Looks correct to me.
Thank you for the fix.

-Vijay/

Quoted 5 lines
drivers/tty/serial/serial_core.c +0 -1
                goto unlock;
        }
 
--

2.30.2

Reviewed-by on v1 2/8
#4 lore raw

Re: [PATCH tty v1 2/8] serial: core: lock port for stop_rx() in uart_suspend_port()

In reply to John Ogness

Hi,

Quoted 9 lines · John Ogness

The uarts_ops stop_rx() callback expects that the port->lock is taken and interrupts are disabled.

Fixes c9d2325cdb92 ("serial: core: Do stop_rx in suspend path for console if console_suspend is disabled")
---
 drivers/tty/serial/serial_core.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
#5 lore raw

PATCH 3/8 serial: 8250: lock port for stop_rx() in omap8250_irq()

In reply to John Ogness

The uarts_ops stop_rx() callback expects that the port->lock is taken and interrupts are disabled.

Fixes 1fe0e1fa3209 ("serial: 8250_omap: Handle optional overrun-throttle-ms property")
---
 drivers/tty/serial/8250/8250_omap.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/tty/serial/8250/8250_omap.c +2 -0
index fbca0692aa51..c17d98161d5e 100644
@@ -658,7 +658,9 @@ static irqreturn_t omap8250_irq(int irq, void *dev_id)
 
 		up->ier = port->serial_in(port, UART_IER);
 		if (up->ier & (UART_IER_RLSI | UART_IER_RDI)) {
+			spin_lock(&port->lock);
 			port->ops->stop_rx(port);
+			spin_unlock(&port->lock);
 		} else {
 			/* Keep restarting the timer until
 			 * the input overrun subsides.
Signature · 1 line
2.30.2
Reviewed-by on v1 3/8
#6 lore raw

Re: [PATCH tty v1 3/8] serial: 8250: lock port for stop_rx() in omap8250_irq()

Quoted 5 lines · John Ogness

The uarts_ops stop_rx() callback expects that the port->lock is taken and interrupts are disabled.

Fixes 1fe0e1fa3209 ("serial: 8250_omap: Handle optional overrun-throttle-ms property")

Looks good to me:

Quoted 21 lines
---
 drivers/tty/serial/8250/8250_omap.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/tty/serial/8250/8250_omap.c +2 -0
index fbca0692aa51..c17d98161d5e 100644
@@ -658,7 +658,9 @@ static irqreturn_t omap8250_irq(int irq, void *dev_id)
 
 		up->ier = port->serial_in(port, UART_IER);
 		if (up->ier & (UART_IER_RLSI | UART_IER_RDI)) {
+			spin_lock(&port->lock);
 			port->ops->stop_rx(port);
+			spin_unlock(&port->lock);
 		} else {
 			/* Keep restarting the timer until
 			 * the input overrun subsides.
Signature · 1 line
2.30.2
#7 lore raw

PATCH 4/8 serial: core: lock port for start_rx() in uart_resume_port()

In reply to John Ogness

The only user of the start_rx() callback (qcom_geni) directly calls its own stop_rx() callback. Since stop_rx() requires that the port->lock is taken and interrupts are disabled, the start_rx() callback has the same requirement.

Fixes cfab87c2c271 ("serial: core: Introduce callback for start_rx and do stop_rx in suspend only if this callback implementation is present.")
---
 drivers/tty/serial/serial_core.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/tty/serial/serial_core.c +4 -1
index 37ad53616372..f856c7fae2fd 100644
@@ -2430,8 +2430,11 @@ int uart_resume_port(struct uart_driver *drv, struct uart_port *uport)
 		if (console_suspend_enabled)
 			uart_change_pm(state, UART_PM_STATE_ON);
 		uport->ops->set_termios(uport, &termios, NULL);
-		if (!console_suspend_enabled && uport->ops->start_rx)
+		if (!console_suspend_enabled && uport->ops->start_rx) {
+			spin_lock_irq(&uport->lock);
 			uport->ops->start_rx(uport);
+			spin_unlock_irq(&uport->lock);
+		}
 		if (console_suspend_enabled)
 			console_start(uport->cons);
 	}
Signature · 1 line
2.30.2
#8 lore raw

Re: [PATCH tty v1 4/8] serial: core: lock port for start_rx() in uart_resume_port()

Hi,

Quoted 17 lines · John Ogness

The only user of the start_rx() callback (qcom_geni) directly calls its own stop_rx() callback. Since stop_rx() requires that the port->lock is taken and interrupts are disabled, the start_rx() callback has the same requirement.

Fixes cfab87c2c271 ("serial: core: Introduce callback for start_rx and do stop_rx in suspend only if this callback implementation is present.")
---
 drivers/tty/serial/serial_core.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/tty/serial/serial_core.c +0 -0
index 37ad53616372..f856c7fae2fd 100644

drivers/tty/serial/serial_core.c int uart_resume_port(struct uart_driver *drv, struct uart_port *uport) · line 2437

Quoted 9 lines
drivers/tty/serial/serial_core.c +4 -1
@@ -2430,8 +2430,11 @@ int uart_resume_port(struct uart_driver *drv, struct uart_port *uport)
                if (console_suspend_enabled)
                        uart_change_pm(state, UART_PM_STATE_ON);
                uport->ops->set_termios(uport, &termios, NULL);
-               if (!console_suspend_enabled && uport->ops->start_rx)
+               if (!console_suspend_enabled && uport->ops->start_rx) {
+                       spin_lock_irq(&uport->lock);
                        uport->ops->start_rx(uport);
+                       spin_unlock_irq(&uport->lock);
+               }

Seems right, but shouldn't you also fix the call to stop_rx() that the same commit cfab87c2c271 ("serial: core: Introduce callback for start_rx and do stop_rx in suspend only if this callback implementation is present.") added? That one is also missing the lock, right?

-Doug

Reviewed-by
#9 lore raw

Re: [PATCH tty v1 4/8] serial: core: lock port for start_rx() in uart_resume_port()

Hi,

Quoted 28 lines · Doug Anderson

Hi,

Quoted 24 lines · John Ogness

The only user of the start_rx() callback (qcom_geni) directly calls its own stop_rx() callback. Since stop_rx() requires that the port->lock is taken and interrupts are disabled, the start_rx() callback has the same requirement.

Fixes cfab87c2c271 ("serial: core: Introduce callback for start_rx and do stop_rx in suspend only if this callback implementation is present.")
---
 drivers/tty/serial/serial_core.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/tty/serial/serial_core.c +2 -1
index 37ad53616372..f856c7fae2fd 100644
@@ -2430,8 +2430,11 @@ int uart_resume_port(struct uart_driver *drv, struct uart_port *uport)
                if (console_suspend_enabled)
                        uart_change_pm(state, UART_PM_STATE_ON);
                uport->ops->set_termios(uport, &termios, NULL);
-               if (!console_suspend_enabled && uport->ops->start_rx)
+               if (!console_suspend_enabled && uport->ops->start_rx) {
+                       spin_lock_irq(&uport->lock);
                        uport->ops->start_rx(uport);
Quoted 8 lines

drivers/tty/serial/serial_core.c int uart_resume_port(struct uart_driver *drv, struct uart_port *uport) · line 2437

Quoted 2 lines
drivers/tty/serial/serial_core.c +2 -0
+                       spin_unlock_irq(&uport->lock);
+               }

Seems right, but shouldn't you also fix the call to stop_rx() that the same commit cfab87c2c271 ("serial: core: Introduce callback for start_rx and do stop_rx in suspend only if this callback implementation is present.") added? That one is also missing the lock, right?

Ah, I see. You did that in a separate patch and I wasn't CCed. I guess I would have just put the two in one patch, but I don't feel that strongly.

#10 lore raw

Re: [PATCH tty v1 4/8] serial: core: lock port for start_rx() in uart_resume_port()

Quoted 9 lines · Doug Anderson
Quoted 5 lines

Seems right, but shouldn't you also fix the call to stop_rx() that the same commit cfab87c2c271 ("serial: core: Introduce callback for start_rx and do stop_rx in suspend only if this callback implementation is present.") added? That one is also missing the lock, right?

Ah, I see. You did that in a separate patch and I wasn't CCed. I guess I would have just put the two in one patch, but I don't feel that strongly.

Actually stop_rx() was introduced in a different commit. The commit you reference just changed it a bit. My other patch uses a different Fixes tag.

Also, I was concerned about packing too much new spin locking in a single commit in the hopes it will help with any bisecting issues.

Quoted 1 line

Thanks!

John

#11 lore raw

PATCH 5/8 serial: 8250: lock port for rx_dma() callback

In reply to John Ogness

The rx_dma() callback (omap_8250_rx_dma) accesses UART_IER. This register is modified twice by each console write (serial8250_console_write()) under the port lock. However, not all calls to the rx_dma() callback are under the port lock.

Add the missing port locking around rx_dma() callback calls. Add lockdep notation to the omap_8250_rx_dma().

Note that this is not fixing a real problem because:

1. Currently DMA is forced off for 8250_omap consoles.

2. The serial devices are resumed before console printing is enabled.

However, adding this locking allows for clean locking semantics for the rx_dma() callback so that lockdep can be used to identify possible problems in the future. It also simplifies synchronization of UART_IER in general by not needing to rely on implementation details such as 1 and 2.

---
 drivers/tty/serial/8250/8250_omap.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
drivers/tty/serial/8250/8250_omap.c +11 -2
index c17d98161d5e..3cb9cfa62331 100644
@@ -728,8 +728,11 @@ static int omap_8250_startup(struct uart_port *port)
 		priv->wer |= OMAP_UART_TX_WAKEUP_EN;
 	serial_out(up, UART_OMAP_WER, priv->wer);
 
-	if (up->dma && !(priv->habit & UART_HAS_EFR2))
+	if (up->dma && !(priv->habit & UART_HAS_EFR2)) {
+		spin_lock_irq(&port->lock);
 		up->dma->rx_dma(up);
+		spin_unlock_irq(&port->lock);
+	}
 
 	enable_irq(up->port.irq);
 
@@ -1003,6 +1006,9 @@ static int omap_8250_rx_dma(struct uart_8250_port *p)
 	unsigned long			flags;
 	u32				reg;
 
+	/* Port locked to synchronize UART_IER access against the console. */
+	lockdep_assert_held_once(&p->port.lock);
+
 	if (priv->rx_dma_broken)
 		return -EINVAL;
 
@@ -1736,8 +1742,11 @@ static int omap8250_runtime_resume(struct device *dev)
 	if (up && omap8250_lost_context(up))
 		omap8250_restore_regs(up);
 
-	if (up && up->dma && up->dma->rxchan && !(priv->habit & UART_HAS_EFR2))
+	if (up && up->dma && up->dma->rxchan && !(priv->habit & UART_HAS_EFR2)) {
+		spin_lock_irq(&up->port.lock);
 		omap_8250_rx_dma(up);
+		spin_unlock_irq(&up->port.lock);
+	}
 
 	priv->latency = priv->calc_latency;
 	schedule_work(&priv->qos_work);
Signature · 1 line
2.30.2
#12 lore raw

PATCH 6/8 serial: 8250: lock port for omap8250_restore_regs()

In reply to John Ogness

omap8250_restore_regs() accesses UART_IER. This register is modified twice by each console write (serial8250_console_write()) under the port lock. However, not all calls to omap8250_restore_regs() are under the port lock.

Add the missing port locking around omap8250_restore_regs() calls. Add lockdep notation to the omap8250_restore_regs().

Note that this is not fixing a real problem because the serial devices are resumed before console printing is enabled.

However, adding this locking allows for clean locking semantics for omap8250_restore_regs() so that lockdep can be used to identify possible problems in the future. It also simplifies synchronization of UART_IER in general by not needing to rely on such implementation details.

---
 drivers/tty/serial/8250/8250_omap.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/tty/serial/8250/8250_omap.c +7 -1
index 3cb9cfa62331..34939462fd69 100644
@@ -309,6 +309,9 @@ static void omap8250_restore_regs(struct uart_8250_port *up)
 	struct uart_8250_dma	*dma = up->dma;
 	u8 mcr = serial8250_in_MCR(up);
 
+	/* Port locked to synchronize UART_IER access against the console. */
+	lockdep_assert_held_once(&up->port.lock);
+
 	if (dma && dma->tx_running) {
 		/*
 		 * TCSANOW requests the change to occur immediately however if
@@ -1739,8 +1742,11 @@ static int omap8250_runtime_resume(struct device *dev)
 	if (priv->line >= 0)
 		up = serial8250_get_port(priv->line);
 
-	if (up && omap8250_lost_context(up))
+	if (up && omap8250_lost_context(up)) {
+		spin_lock_irq(&up->port.lock);
 		omap8250_restore_regs(up);
+		spin_unlock_irq(&up->port.lock);
+	}
 
 	if (up && up->dma && up->dma->rxchan && !(priv->habit & UART_HAS_EFR2)) {
 		spin_lock_irq(&up->port.lock);
Signature · 1 line
2.30.2
#13 lore raw

PATCH 7/8 serial: 8250: lock port for UART_IER access in omap8250_irq()

In reply to John Ogness

omap8250_irq() accesses UART_IER. This register is modified twice by each console write (serial8250_console_write()) under the port lock. omap8250_irq() must also take the port lock to guanentee synchronized access to UART_IER.

Since the port lock is already being taken for the stop_rx() callback and since it is safe to call cancel_delayed_work() while holding the port lock, simply extend the port lock region to include UART_IER access.

Fixes 1fe0e1fa3209 ("serial: 8250_omap: Handle optional overrun-throttle-ms property")
---
 drivers/tty/serial/8250/8250_omap.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/tty/serial/8250/8250_omap.c +3 -2
index 34939462fd69..3225c95fde1d 100644
@@ -659,17 +659,18 @@ static irqreturn_t omap8250_irq(int irq, void *dev_id)
 	if ((lsr & UART_LSR_OE) && up->overrun_backoff_time_ms > 0) {
 		unsigned long delay;
 
+		/* Synchronize UART_IER access against the console. */
+		spin_lock(&port->lock);
 		up->ier = port->serial_in(port, UART_IER);
 		if (up->ier & (UART_IER_RLSI | UART_IER_RDI)) {
-			spin_lock(&port->lock);
 			port->ops->stop_rx(port);
-			spin_unlock(&port->lock);
 		} else {
 			/* Keep restarting the timer until
 			 * the input overrun subsides.
 			 */
 			cancel_delayed_work(&up->overrun_backoff);
 		}
+		spin_unlock(&port->lock);
 
 		delay = msecs_to_jiffies(up->overrun_backoff_time_ms);
 		schedule_delayed_work(&up->overrun_backoff, delay);
Signature · 1 line
2.30.2
Reviewed-by on v1 7/8
#14 lore raw

Re: [PATCH tty v1 7/8] serial: 8250: lock port for UART_IER access in omap8250_irq()

Quoted 5 lines · John Ogness

omap8250_irq() accesses UART_IER. This register is modified twice by each console write (serial8250_console_write()) under the port lock. omap8250_irq() must also take the port lock to guanentee synchronized access to UART_IER.

Quoted 7 lines

Since the port lock is already being taken for the stop_rx() callback and since it is safe to call cancel_delayed_work() while holding the port lock, simply extend the port lock region to include UART_IER access.

Fixes 1fe0e1fa3209 ("serial: 8250_omap: Handle optional overrun-throttle-ms property")
Quoted 32 lines
---
 drivers/tty/serial/8250/8250_omap.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/tty/serial/8250/8250_omap.c +3 -2
index 34939462fd69..3225c95fde1d 100644
@@ -659,17 +659,18 @@ static irqreturn_t omap8250_irq(int irq, void *dev_id)
 	if ((lsr & UART_LSR_OE) && up->overrun_backoff_time_ms > 0) {
 		unsigned long delay;
 
+		/* Synchronize UART_IER access against the console. */
+		spin_lock(&port->lock);
 		up->ier = port->serial_in(port, UART_IER);
 		if (up->ier & (UART_IER_RLSI | UART_IER_RDI)) {
-			spin_lock(&port->lock);
 			port->ops->stop_rx(port);
-			spin_unlock(&port->lock);
 		} else {
 			/* Keep restarting the timer until
 			 * the input overrun subsides.
 			 */
 			cancel_delayed_work(&up->overrun_backoff);
 		}
+		spin_unlock(&port->lock);
 
 		delay = msecs_to_jiffies(up->overrun_backoff_time_ms);
 		schedule_delayed_work(&up->overrun_backoff, delay);
Signature · 1 line
2.30.2
#15 lore raw

PATCH 8/8 serial: 8250: synchronize and annotate UART_IER access

In reply to John Ogness

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.

---
 drivers/tty/serial/8250/8250.h              |  6 +++
 drivers/tty/serial/8250/8250_aspeed_vuart.c |  3 ++
 drivers/tty/serial/8250/8250_mtk.c          |  9 ++++
 drivers/tty/serial/8250/8250_omap.c         | 14 ++++++
 drivers/tty/serial/8250/8250_port.c         | 53 +++++++++++++++++++++
 5 files changed, 85 insertions(+)
drivers/tty/serial/8250/8250.h +6 -0
index 5418708f4631..471c6bc5f78f 100644
@@ -179,6 +179,9 @@ static inline void serial_dl_write(struct uart_8250_port *up, u32 value)
 
 static inline bool serial8250_set_THRI(struct uart_8250_port *up)
 {
+	/* Port locked to synchronize UART_IER access against the console. */
+	lockdep_assert_held_once(&up->port.lock);
+
 	if (up->ier & UART_IER_THRI)
 		return false;
 	up->ier |= UART_IER_THRI;
@@ -188,6 +191,9 @@ static inline bool serial8250_set_THRI(struct uart_8250_port *up)
 
 static inline bool serial8250_clear_THRI(struct uart_8250_port *up)
 {
+	/* Port locked to synchronize UART_IER access against the console. */
+	lockdep_assert_held_once(&up->port.lock);
+
 	if (!(up->ier & UART_IER_THRI))
 		return false;
 	up->ier &= ~UART_IER_THRI;
drivers/tty/serial/8250/8250_aspeed_vuart.c +3 -0
index 9d2a7856784f..4a9e71b2dbbc 100644
@@ -275,6 +275,9 @@ static void __aspeed_vuart_set_throttle(struct uart_8250_port *up,
 {
 	unsigned char irqs = UART_IER_RLSI | UART_IER_RDI;
 
+	/* Port locked to synchronize UART_IER access against the console. */
+	lockdep_assert_held_once(&up->port.lock);
+
 	up->ier &= ~irqs;
 	if (!throttle)
 		up->ier |= irqs;
drivers/tty/serial/8250/8250_mtk.c +9 -0
index fb1d5ec0940e..aa8e98164d68 100644
@@ -222,11 +222,17 @@ static void mtk8250_shutdown(struct uart_port *port)
 
 static void mtk8250_disable_intrs(struct uart_8250_port *up, int mask)
 {
+	/* Port locked to synchronize UART_IER access against the console. */
+	lockdep_assert_held_once(&up->port.lock);
+
 	serial_out(up, UART_IER, serial_in(up, UART_IER) & (~mask));
 }
 
 static void mtk8250_enable_intrs(struct uart_8250_port *up, int mask)
 {
+	/* Port locked to synchronize UART_IER access against the console. */
+	lockdep_assert_held_once(&up->port.lock);
+
 	serial_out(up, UART_IER, serial_in(up, UART_IER) | mask);
 }
 
@@ -235,6 +241,9 @@ static void mtk8250_set_flow_ctrl(struct uart_8250_port *up, int mode)
 	struct uart_port *port = &up->port;
 	int lcr = serial_in(up, UART_LCR);
 
+	/* Port locked to synchronize UART_IER access against the console. */
+	lockdep_assert_held_once(&port->lock);
+
 	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
 	serial_out(up, MTK_UART_EFR, UART_EFR_ECB);
 	serial_out(up, UART_LCR, lcr);
drivers/tty/serial/8250/8250_omap.c +14 -0
index 3225c95fde1d..0498b9b0e4e9 100644
@@ -533,6 +533,10 @@ static void omap_8250_pm(struct uart_port *port, unsigned int state,
 	u8 efr;
 
 	pm_runtime_get_sync(port->dev);
+
+	/* Synchronize UART_IER access against the console. */
+	spin_lock_irq(&port->lock);
+
 	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
 	efr = serial_in(up, UART_EFR);
 	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,
 	serial_out(up, UART_EFR, efr);
 	serial_out(up, UART_LCR, 0);
 
+	spin_unlock_irq(&port->lock);
+
 	pm_runtime_mark_last_busy(port->dev);
 	pm_runtime_put_autosuspend(port->dev);
 }
@@ -760,8 +766,11 @@ static void omap_8250_shutdown(struct uart_port *port)
 	if (priv->habit & UART_HAS_EFR2)
 		serial_out(up, UART_OMAP_EFR2, 0x0);
 
+	/* Synchronize UART_IER access against the console. */
+	spin_lock_irq(&port->lock);
 	up->ier = 0;
 	serial_out(up, UART_IER, 0);
+	spin_unlock_irq(&port->lock);
 	disable_irq_nosync(up->port.irq);
 	dev_pm_clear_wake_irq(port->dev);
 
@@ -803,6 +812,7 @@ static void omap_8250_unthrottle(struct uart_port *port)
 
 	pm_runtime_get_sync(port->dev);
 
+	/* Synchronize UART_IER access against the console. */
 	spin_lock_irqsave(&port->lock, flags);
 	priv->throttled = false;
 	if (up->dma)
@@ -953,6 +963,7 @@ static void __dma_rx_complete(void *param)
 	struct dma_tx_state     state;
 	unsigned long flags;
 
+	/* Synchronize UART_IER access against the console. */
 	spin_lock_irqsave(&p->port.lock, flags);
 
 	/*
@@ -1227,6 +1238,9 @@ static u16 omap_8250_handle_rx_dma(struct uart_8250_port *up, u8 iir, u16 status
 static void am654_8250_handle_rx_dma(struct uart_8250_port *up, u8 iir,
 				     u16 status)
 {
+	/* Port locked to synchronize UART_IER access against the console. */
+	lockdep_assert_held_once(&up->port.lock);
+
 	/*
 	 * Queue a new transfer if FIFO has data.
 	 */
drivers/tty/serial/8250/8250_port.c +53 -0

164 lines not on this page · raw message

Signature · 1 line
2.30.2