Skip to contentSkip to conversation

serial: mcf: Don't take spinlocks in already protected functions

PATCH In review [email protected] 2 participants 3 messages Jun 9, 2010 – Jun 14, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 serial: mcf: Don't take spinlocks in already protected functions

From: Yury Georgievskiy <[email protected]>

Don't take the port spinlock in uart functions where the serial core already takes care of locking/unlocking them.

The code would actually lock up on architectures where spinlocks are implemented.

Also protect calling mcf_rx_chars/mcf_tx_chars in the interrupt handler by the port spinlock and use IRQ_RETVAL to return from isr.

---
 drivers/serial/mcf.c |   22 ++++++----------------
 1 files changed, 6 insertions(+), 16 deletions(-)
drivers/serial/mcf.c +6 -16
index b5aaef9..6235444 100644
@@ -70,16 +70,14 @@ static unsigned int mcf_tx_empty(struct uart_port *port)
 static unsigned int mcf_get_mctrl(struct uart_port *port)
 {
 	struct mcf_uart *pp = container_of(port, struct mcf_uart, port);
-	unsigned long flags;
 	unsigned int sigs;
 
-	spin_lock_irqsave(&port->lock, flags);
 	sigs = (readb(port->membase + MCFUART_UIPR) & MCFUART_UIPR_CTS) ?
 		0 : TIOCM_CTS;
 	sigs |= (pp->sigs & TIOCM_RTS);
 	sigs |= (mcf_getppdcd(port->line) ? TIOCM_CD : 0);
 	sigs |= (mcf_getppdtr(port->line) ? TIOCM_DTR : 0);
-	spin_unlock_irqrestore(&port->lock, flags);
+
 	return sigs;
 }
 
@@ -88,16 +86,13 @@ static unsigned int mcf_get_mctrl(struct uart_port *port)
 static void mcf_set_mctrl(struct uart_port *port, unsigned int sigs)
 {
 	struct mcf_uart *pp = container_of(port, struct mcf_uart, port);
-	unsigned long flags;
 
-	spin_lock_irqsave(&port->lock, flags);
 	pp->sigs = sigs;
 	mcf_setppdtr(port->line, (sigs & TIOCM_DTR));
 	if (sigs & TIOCM_RTS)
 		writeb(MCFUART_UOP_RTS, port->membase + MCFUART_UOP1);
 	else
 		writeb(MCFUART_UOP_RTS, port->membase + MCFUART_UOP0);
-	spin_unlock_irqrestore(&port->lock, flags);
 }
 
 /****************************************************************************/
@@ -105,12 +100,9 @@ static void mcf_set_mctrl(struct uart_port *port, unsigned int sigs)
 static void mcf_start_tx(struct uart_port *port)
 {
 	struct mcf_uart *pp = container_of(port, struct mcf_uart, port);
-	unsigned long flags;
 
-	spin_lock_irqsave(&port->lock, flags);
 	pp->imr |= MCFUART_UIR_TXREADY;
 	writeb(pp->imr, port->membase + MCFUART_UIMR);
-	spin_unlock_irqrestore(&port->lock, flags);
 }
 
 /****************************************************************************/
@@ -118,12 +110,9 @@ static void mcf_start_tx(struct uart_port *port)
 static void mcf_stop_tx(struct uart_port *port)
 {
 	struct mcf_uart *pp = container_of(port, struct mcf_uart, port);
-	unsigned long flags;
 
-	spin_lock_irqsave(&port->lock, flags);
 	pp->imr &= ~MCFUART_UIR_TXREADY;
 	writeb(pp->imr, port->membase + MCFUART_UIMR);
-	spin_unlock_irqrestore(&port->lock, flags);
 }
 
 /****************************************************************************/
@@ -131,12 +120,9 @@ static void mcf_stop_tx(struct uart_port *port)
 static void mcf_stop_rx(struct uart_port *port)
 {
 	struct mcf_uart *pp = container_of(port, struct mcf_uart, port);
-	unsigned long flags;
 
-	spin_lock_irqsave(&port->lock, flags);
 	pp->imr &= ~MCFUART_UIR_RXREADY;
 	writeb(pp->imr, port->membase + MCFUART_UIMR);
-	spin_unlock_irqrestore(&port->lock, flags);
 }
 
 /****************************************************************************/
@@ -368,11 +354,15 @@ static irqreturn_t mcf_interrupt(int irq, void *data)
 	unsigned int isr;
 
 	isr = readb(port->membase + MCFUART_UISR) & pp->imr;
+
+	spin_lock(&port->lock);
 	if (isr & MCFUART_UIR_RXREADY)
 		mcf_rx_chars(pp);
 	if (isr & MCFUART_UIR_TXREADY)
 		mcf_tx_chars(pp);
-	return IRQ_HANDLED;
+	spin_unlock(&port->lock);
+
+	return IRQ_RETVAL(isr);
 }
 
 /****************************************************************************/
Signature · 1 line
1.7.0
#1 lore raw
Quoted 5 lines · On Wed, 9 Jun 2010 09:56:26 +0200 [email protected] wrote:

From: Yury Georgievskiy <[email protected]>

Don't take the port spinlock in uart functions where the serial core already takes care of locking/unlocking them.

Quoted 7 lines

The code would actually lock up on architectures where spinlocks are implemented.

Also protect calling mcf_rx_chars/mcf_tx_chars in the interrupt handler by the port spinlock and use IRQ_RETVAL to return from isr.

Thanks. Did you runtime test this?

Quoted 5 lines
(quoted diff) +1 -0
@@ -368,11 +354,15 @@ static irqreturn_t mcf_interrupt(int irq, void *data)
 	unsigned int isr;
 
 	isr = readb(port->membase + MCFUART_UISR) & pp->imr;
+

Review static irqreturn_t mcf_interrupt(int irq, void *data) · line 366

Quoted 10 lines
(quoted diff) +4 -1
+	spin_lock(&port->lock);
 	if (isr & MCFUART_UIR_RXREADY)
 		mcf_rx_chars(pp);
 	if (isr & MCFUART_UIR_TXREADY)
 		mcf_tx_chars(pp);
-	return IRQ_HANDLED;
+	spin_unlock(&port->lock);
+
+	return IRQ_RETVAL(isr);
 }

I think this is a little abusive of IRQ_RETVAL. If there are some bits set in `isr' other than MCFUART_UIR_RXREADY and MCFUART_UIR_TXREADY, we claim we handled it, only we didn't.

Probably the code works OK, but it all seems a bit uncomfortable. Perhaps make it more explicit?

drivers/serial/mcf.c renamed from drivers/serial/mcf.c~serial-mcf-dont-take-spinlocks-in-already-protected-functions-fix +8 -3
@@ -352,17 +352,22 @@ static irqreturn_t mcf_interrupt(int irq
 	struct uart_port *port = data;
 	struct mcf_uart *pp = container_of(port, struct mcf_uart, port);
 	unsigned int isr;
+	irqreturn_t ret = IRQ_NONE;
 
 	isr = readb(port->membase + MCFUART_UISR) & pp->imr;
 
 	spin_lock(&port->lock);
-	if (isr & MCFUART_UIR_RXREADY)
+	if (isr & MCFUART_UIR_RXREADY) {
 		mcf_rx_chars(pp);
-	if (isr & MCFUART_UIR_TXREADY)
+		ret = IRQ_HANDLED;
+	}
+	if (isr & MCFUART_UIR_TXREADY) {
 		mcf_tx_chars(pp);
+		ret = IRQ_HANDLED;
+	}
 	spin_unlock(&port->lock);
 
-	return IRQ_RETVAL(isr);
+	return ret;
 }
 
 /****************************************************************************/

_

#2 lore raw
Quoted 11 lines · Andrew Morton
Quoted 8 lines · On Wed, 9 Jun 2010 09:56:26 +0200 [email protected] wrote:

From: Yury Georgievskiy <[email protected]>

Don't take the port spinlock in uart functions where the serial core already takes care of locking/unlocking them.

The code would actually lock up on architectures where spinlocks are implemented.

Quoted 6 lines
Quoted 4 lines

Also protect calling mcf_rx_chars/mcf_tx_chars in the interrupt handler by the port spinlock and use IRQ_RETVAL to return from isr.

Thanks. Did you runtime test this?

Unfortunately not.

I spotted it as I am now writing the UART driver for Philips SCC2698B integrated circuit and was looking for serial drivers examples.

And also came across d8d721f4c005f9a69bd1b5d5c6ba99b7e1d464de commit that has patched a similar problem.

Quoted 21 lines
Quoted 6 lines
(quoted diff) +1 -0
@@ -368,11 +354,15 @@ static irqreturn_t mcf_interrupt(int irq, void *data)
 	unsigned int isr;
 
 	isr = readb(port->membase + MCFUART_UISR) & pp->imr;
+

Review static irqreturn_t mcf_interrupt(int irq, void *data) · line 366

Quoted 10 lines
(quoted diff) +4 -1
+	spin_lock(&port->lock);
 	if (isr & MCFUART_UIR_RXREADY)
 		mcf_rx_chars(pp);
 	if (isr & MCFUART_UIR_TXREADY)
 		mcf_tx_chars(pp);
-	return IRQ_HANDLED;
+	spin_unlock(&port->lock);
+
+	return IRQ_RETVAL(isr);
 }

I think this is a little abusive of IRQ_RETVAL. If there are some bits set in `isr' other than MCFUART_UIR_RXREADY and MCFUART_UIR_TXREADY, we claim we handled it, only we didn't.

Quoted 3 lines

Probably the code works OK, but it all seems a bit uncomfortable. Perhaps make it more explicit?

Fair enough.
The code looks more relevant.

Quoted 31 lines
drivers/serial/mcf.c renamed from drivers/serial/mcf.c~serial-mcf-dont-take-spinlocks-in-already-protected-functions-fix +8 -3
@@ -352,17 +352,22 @@ static irqreturn_t mcf_interrupt(int irq
 	struct uart_port *port = data;
 	struct mcf_uart *pp = container_of(port, struct mcf_uart, port);
 	unsigned int isr;
+	irqreturn_t ret = IRQ_NONE;
 
 	isr = readb(port->membase + MCFUART_UISR) & pp->imr;
 
 	spin_lock(&port->lock);
-	if (isr & MCFUART_UIR_RXREADY)
+	if (isr & MCFUART_UIR_RXREADY) {
 		mcf_rx_chars(pp);
-	if (isr & MCFUART_UIR_TXREADY)
+		ret = IRQ_HANDLED;
+	}
+	if (isr & MCFUART_UIR_TXREADY) {
 		mcf_tx_chars(pp);
+		ret = IRQ_HANDLED;
+	}
 	spin_unlock(&port->lock);
 
-	return IRQ_RETVAL(isr);
+	return ret;
 }
 
 /****************************************************************************/

_