[PATCH 30/67] serial: Change the wait for carrier locking

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Greg Kroah-Hartman
Date: Thursday, August 5, 2010 - 3:22 pm

From: Alan Cox <alan@linux.intel.com>

We want to push the lock/unlock into the helper functions so that we
can prepare to move to using the tty_port helper. The expansion initially
comes out a bit ugly but its worth the temporary expansion IMHO just so
we can produce a nice testable series of changes.

Signed-off-by: Alan Cox <alan@linux.intel.com>
Cc: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
---
 drivers/serial/serial_core.c |   44 +++++++++++++++++++++++++++++++++--------
 1 files changed, 35 insertions(+), 9 deletions(-)

diff --git a/drivers/serial/serial_core.c b/drivers/serial/serial_core.c
index 570dca2..424b1c7 100644
--- a/drivers/serial/serial_core.c
+++ b/drivers/serial/serial_core.c
@@ -1272,6 +1272,7 @@ static void uart_close(struct tty_struct *tty, struct file *filp)
 	struct uart_state *state = tty->driver_data;
 	struct tty_port *port;
 	struct uart_port *uport;
+	unsigned long flags;
 
 	BUG_ON(!kernel_locked());
 
@@ -1284,9 +1285,12 @@ static void uart_close(struct tty_struct *tty, struct file *filp)
 	pr_debug("uart_close(%d) called\n", uport->line);
 
 	mutex_lock(&port->mutex);
+	spin_lock_irqsave(&port->lock, flags);
 
-	if (tty_hung_up_p(filp))
+	if (tty_hung_up_p(filp)) {
+		spin_unlock_irqrestore(&port->lock, flags);
 		goto done;
+	}
 
 	if ((tty->count == 1) && (port->count != 1)) {
 		/*
@@ -1305,8 +1309,10 @@ static void uart_close(struct tty_struct *tty, struct file *filp)
 		       tty->name, port->count);
 		port->count = 0;
 	}
-	if (port->count)
+	if (port->count) {
+		spin_unlock_irqrestore(&port->lock, flags);
 		goto done;
+	}
 
 	/*
 	 * Now we wait for the transmit buffer to clear; and we notify
@@ -1314,6 +1320,7 @@ static void uart_close(struct tty_struct *tty, struct file *filp)
 	 * setting tty->closing.
 	 */
 	tty->closing = 1;
+	spin_unlock_irqrestore(&port->lock, flags);
 
 	if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
 		tty_wait_until_sent(tty, msecs_to_jiffies(port->closing_wait));
@@ -1340,20 +1347,26 @@ static void uart_close(struct tty_struct *tty, struct file *filp)
 
 	tty_ldisc_flush(tty);
 
-	tty->closing = 0;
 	tty_port_tty_set(port, NULL);
+	spin_lock_irqsave(&port->lock, flags);
+	tty->closing = 0;
 
 	if (port->blocked_open) {
+		spin_unlock_irqrestore(&port->lock, flags);
 		if (port->close_delay)
 			msleep_interruptible(port->close_delay);
+		spin_lock_irqsave(&port->lock, flags);
 	} else if (!uart_console(uport)) {
+		spin_unlock_irqrestore(&port->lock, flags);
 		uart_change_pm(state, 3);
+		spin_lock_irqsave(&port->lock, flags);
 	}
 
 	/*
 	 * Wake up anyone trying to open this port.
 	 */
 	clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
+	spin_unlock_irqrestore(&port->lock, flags);
 	wake_up_interruptible(&port->open_wait);
 
 done:
@@ -1429,6 +1442,7 @@ static void uart_hangup(struct tty_struct *tty)
 {
 	struct uart_state *state = tty->driver_data;
 	struct tty_port *port = &state->port;
+	unsigned long flags;
 
 	BUG_ON(!kernel_locked());
 	pr_debug("uart_hangup(%d)\n", state->uart_port->line);
@@ -1437,8 +1451,10 @@ static void uart_hangup(struct tty_struct *tty)
 	if (port->flags & ASYNC_NORMAL_ACTIVE) {
 		uart_flush_buffer(tty);
 		uart_shutdown(tty, state);
+		spin_lock_irqsave(&port->lock, flags);
 		port->count = 0;
 		clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
+		spin_unlock_irqrestore(&port->lock, flags);
 		tty_port_tty_set(port, NULL);
 		wake_up_interruptible(&port->open_wait);
 		wake_up_interruptible(&port->delta_msr_wait);
@@ -1496,9 +1512,13 @@ uart_block_til_ready(struct file *filp, struct uart_state *state)
 	struct uart_port *uport = state->uart_port;
 	struct tty_port *port = &state->port;
 	unsigned int mctrl;
+	unsigned long flags;
 
+	spin_lock_irqsave(&port->lock, flags);
+	if (!tty_hung_up_p(filp))
+		port->count--;
 	port->blocked_open++;
-	port->count--;
+	spin_unlock_irqrestore(&port->lock, flags);
 
 	add_wait_queue(&port->open_wait, &wait);
 	while (1) {
@@ -1535,23 +1555,26 @@ uart_block_til_ready(struct file *filp, struct uart_state *state)
 		 * not set RTS here - we want to make sure we catch
 		 * the data from the modem.
 		 */
-		if (port->tty->termios->c_cflag & CBAUD)
+		if (port->tty->termios->c_cflag & CBAUD) {
+			mutex_lock(&port->mutex);
 			uart_set_mctrl(uport, TIOCM_DTR);
+			mutex_unlock(&port->mutex);
+		}
 
 		/*
 		 * and wait for the carrier to indicate that the
 		 * modem is ready for us.
 		 */
+		mutex_lock(&port->mutex);
 		spin_lock_irq(&uport->lock);
 		uport->ops->enable_ms(uport);
 		mctrl = uport->ops->get_mctrl(uport);
 		spin_unlock_irq(&uport->lock);
+		mutex_unlock(&port->mutex);
 		if (mctrl & TIOCM_CAR)
 			break;
 
-		mutex_unlock(&port->mutex);
 		schedule();
-		mutex_lock(&port->mutex);
 
 		if (signal_pending(current))
 			break;
@@ -1559,8 +1582,11 @@ uart_block_til_ready(struct file *filp, struct uart_state *state)
 	set_current_state(TASK_RUNNING);
 	remove_wait_queue(&port->open_wait, &wait);
 
-	port->count++;
+	spin_lock_irqsave(&port->lock, flags);
+	if (!tty_hung_up_p(filp))
+		port->count++;
 	port->blocked_open--;
+	spin_unlock_irqrestore(&port->lock, flags);
 
 	if (signal_pending(current))
 		return -ERESTARTSYS;
@@ -1677,9 +1703,9 @@ static int uart_open(struct tty_struct *tty, struct file *filp)
 	/*
 	 * If we succeeded, wait until the port is ready.
 	 */
+	mutex_unlock(&port->mutex);
 	if (retval == 0)
 		retval = uart_block_til_ready(filp, state);
-	mutex_unlock(&port->mutex);
 
 	/*
 	 * If this is the first open to succeed, adjust things to suit.
-- 
1.7.2

--
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[GIT PATCH] TTY patches for 2.6.36, Greg KH, (Thu Aug 5, 2:35 pm)
[PATCH 01/67] n_gsm.c: removed duplicated #includes, Greg Kroah-Hartman, (Thu Aug 5, 3:22 pm)
[PATCH 02/67] serial: There's no config CONSOLE, Greg Kroah-Hartman, (Thu Aug 5, 3:22 pm)
[PATCH 03/67] vt: clean up the code - use kernel library, Greg Kroah-Hartman, (Thu Aug 5, 3:22 pm)
[PATCH 04/67] serial: add UART_CAP_EFR and UART_CAP_SLEEP ..., Greg Kroah-Hartman, (Thu Aug 5, 3:22 pm)
[PATCH 05/67] mrst_max3110: add UART driver for Max3110 on ..., Greg Kroah-Hartman, (Thu Aug 5, 3:22 pm)
[PATCH 06/67] max3110 sanity check a register, Greg Kroah-Hartman, (Thu Aug 5, 3:22 pm)
[PATCH 07/67] serial: replace open coded mutex with a real ..., Greg Kroah-Hartman, (Thu Aug 5, 3:22 pm)
[PATCH 08/67] serial: fix wakup races in the mrst_max3110 ..., Greg Kroah-Hartman, (Thu Aug 5, 3:22 pm)
[PATCH 09/67] tty: Remove Hayes ESP ioctls, Greg Kroah-Hartman, (Thu Aug 5, 3:22 pm)
[PATCH 10/67] tty: remove remaining Hayes ESP ioctls, Greg Kroah-Hartman, (Thu Aug 5, 3:22 pm)
[PATCH 11/67] tty: Add EXTPROC support for LINEMODE, Greg Kroah-Hartman, (Thu Aug 5, 3:22 pm)
[PATCH 12/67] vt/console: try harder to print output when ..., Greg Kroah-Hartman, (Thu Aug 5, 3:22 pm)
[PATCH 13/67] stallion: prune lock_kernel calls, Greg Kroah-Hartman, (Thu Aug 5, 3:22 pm)
[PATCH 14/67] istallion: use bit ops for the board flags, Greg Kroah-Hartman, (Thu Aug 5, 3:22 pm)
[PATCH 15/67] riscom8: kill use of lock_kernel, Greg Kroah-Hartman, (Thu Aug 5, 3:22 pm)
[PATCH 16/67] isicom: kill off the BKL, Greg Kroah-Hartman, (Thu Aug 5, 3:22 pm)
[PATCH 17/67] rocket: kill BKL, Greg Kroah-Hartman, (Thu Aug 5, 3:22 pm)
[PATCH 18/67] synclink: kill the big kernel lock, Greg Kroah-Hartman, (Thu Aug 5, 3:22 pm)
[PATCH 19/67] cyclades: Kill off BKL usage, Greg Kroah-Hartman, (Thu Aug 5, 3:22 pm)
[PATCH 20/67] epca: Kill the big kernel lock, Greg Kroah-Hartman, (Thu Aug 5, 3:22 pm)
[PATCH 21/67] specialix: Kill the BKL, Greg Kroah-Hartman, (Thu Aug 5, 3:22 pm)
[PATCH 22/67] tty: Fix the digi acceleport driver NULL checks, Greg Kroah-Hartman, (Thu Aug 5, 3:22 pm)
[PATCH 23/67] synclink: reworking locking a bit, Greg Kroah-Hartman, (Thu Aug 5, 3:22 pm)
[PATCH 24/67] tty: serial - fix various misuses/mishandlin ..., Greg Kroah-Hartman, (Thu Aug 5, 3:22 pm)
[PATCH 25/67] tty: serial - fix tty back references in termios, Greg Kroah-Hartman, (Thu Aug 5, 3:22 pm)
[PATCH 26/67] tty: serial - fix tty referencing in set_ldisc, Greg Kroah-Hartman, (Thu Aug 5, 3:22 pm)
[PATCH 27/67] vc: Locking clean up, Greg Kroah-Hartman, (Thu Aug 5, 3:22 pm)
[PATCH 28/67] tty: Make vt's have a tty_port, Greg Kroah-Hartman, (Thu Aug 5, 3:22 pm)
[PATCH 29/67] tty: Move the vt_tty field from the vc_data ..., Greg Kroah-Hartman, (Thu Aug 5, 3:22 pm)
[PATCH 30/67] serial: Change the wait for carrier locking, Greg Kroah-Hartman, (Thu Aug 5, 3:22 pm)
[PATCH 31/67] serial: add port helpers, Greg Kroah-Hartman, (Thu Aug 5, 3:22 pm)
[PATCH 32/67] serial: trim locking on the helpers, Greg Kroah-Hartman, (Thu Aug 5, 3:22 pm)
[PATCH 33/67] serial: Use block_til_ready helper, Greg Kroah-Hartman, (Thu Aug 5, 3:23 pm)
[PATCH 34/67] serial: fix termios settings in open, Greg Kroah-Hartman, (Thu Aug 5, 3:23 pm)
[PATCH 35/67] tty: replace BKL with a new tty_lock, Greg Kroah-Hartman, (Thu Aug 5, 3:23 pm)
[PATCH 36/67] tty: never hold BTM while getting tty_mutex, Greg Kroah-Hartman, (Thu Aug 5, 3:23 pm)
[PATCH 37/67] tty: fix console_sem lock order, Greg Kroah-Hartman, (Thu Aug 5, 3:23 pm)
[PATCH 38/67] cdc-acm: remove dead code, Greg Kroah-Hartman, (Thu Aug 5, 3:23 pm)
[PATCH 39/67] tty: introduce wait_event_interruptible_tty, Greg Kroah-Hartman, (Thu Aug 5, 3:23 pm)
[PATCH 40/67] tty: reorder ldisc locking, Greg Kroah-Hartman, (Thu Aug 5, 3:23 pm)
[PATCH 41/67] tty: untangle locking of wait_until_sent, Greg Kroah-Hartman, (Thu Aug 5, 3:23 pm)
[PATCH 42/67] tty: remove tty_lock_nested, Greg Kroah-Hartman, (Thu Aug 5, 3:23 pm)
[PATCH 43/67] tty: implement BTM as mutex instead of BKL, Greg Kroah-Hartman, (Thu Aug 5, 3:23 pm)
[PATCH 44/67] tty: release BTM while sleeping in block_til ..., Greg Kroah-Hartman, (Thu Aug 5, 3:23 pm)
[PATCH 45/67] 8250: fix set_ldisc operation, Greg Kroah-Hartman, (Thu Aug 5, 3:23 pm)
[PATCH 46/67] tty: avoid recursive BTM in pty_close, Greg Kroah-Hartman, (Thu Aug 5, 3:23 pm)
[PATCH 47/67] serial: max3107: introduce a max3107 driver, Greg Kroah-Hartman, (Thu Aug 5, 3:23 pm)
[PATCH 48/67] serial: max3107: Abstract out the platform s ..., Greg Kroah-Hartman, (Thu Aug 5, 3:23 pm)
[PATCH 49/67] tty_io: remove casts from void*, Greg Kroah-Hartman, (Thu Aug 5, 3:23 pm)
[PATCH 50/67] vt: Fix warning: statement with no effect du ..., Greg Kroah-Hartman, (Thu Aug 5, 3:23 pm)
[PATCH 51/67] serial: crisv10: formatting of pointers in p ..., Greg Kroah-Hartman, (Thu Aug 5, 3:23 pm)
[PATCH 53/67] serial: fix missing bit coverage of ASYNC_FLAGS, Greg Kroah-Hartman, (Thu Aug 5, 3:23 pm)
[PATCH 54/67] serial: general fixes in the serial_rs485 st ..., Greg Kroah-Hartman, (Thu Aug 5, 3:23 pm)
[PATCH 55/67] serial: mcf: don't take spinlocks in already ..., Greg Kroah-Hartman, (Thu Aug 5, 3:23 pm)
[PATCH 56/67] serial: MMIO32 support for 8250_early.c, Greg Kroah-Hartman, (Thu Aug 5, 3:23 pm)
[PATCH 57/67] timbuart: use __devinit and __devexit macros ..., Greg Kroah-Hartman, (Thu Aug 5, 3:23 pm)
[PATCH 58/67] serial: 68328serial.c: remove dead (ALMA_ANS ..., Greg Kroah-Hartman, (Thu Aug 5, 3:23 pm)
[PATCH 59/67] serial: add support for OX16PCI958 card, Greg Kroah-Hartman, (Thu Aug 5, 3:23 pm)
[PATCH 60/67] mxser: remove unnesesary NULL check, Greg Kroah-Hartman, (Thu Aug 5, 3:23 pm)
[PATCH 61/67] hsu: driver for Medfield High Speed UART device, Greg Kroah-Hartman, (Thu Aug 5, 3:23 pm)
[PATCH 62/67] hsu: add a periodic timer to check dma rx ch ..., Greg Kroah-Hartman, (Thu Aug 5, 3:23 pm)
[PATCH 63/67] hsu: some code cleanup, Greg Kroah-Hartman, (Thu Aug 5, 3:23 pm)
[PATCH 64/67] hsu: call PCI pm hooks in suspend/resume fun ..., Greg Kroah-Hartman, (Thu Aug 5, 3:23 pm)
[PATCH 65/67] serial: max3107: Fix gpiolib support, Greg Kroah-Hartman, (Thu Aug 5, 3:23 pm)
Re: [GIT PATCH] TTY patches for 2.6.36, Linus Torvalds, (Fri Aug 6, 11:40 am)
Re: [GIT PATCH] TTY patches for 2.6.36, Greg KH, (Fri Aug 6, 11:51 am)
Re: [GIT PATCH] TTY patches for 2.6.36, Greg KH, (Fri Aug 6, 12:37 pm)
Re: [GIT PATCH] TTY patches for 2.6.36, Linus Torvalds, (Fri Aug 6, 12:38 pm)
[PATCH retry] tty: implement BTM as mutex instead of BKL, Arnd Bergmann, (Fri Aug 6, 12:40 pm)
Re: [GIT PATCH] TTY patches for 2.6.36, Arnd Bergmann, (Fri Aug 6, 12:40 pm)
Re: [GIT PATCH] TTY patches for 2.6.36, Alan Cox, (Fri Aug 6, 12:45 pm)
Re: [GIT PATCH] TTY patches for 2.6.36, Arnd Bergmann, (Fri Aug 6, 12:58 pm)
Re: [GIT PATCH] TTY patches for 2.6.36, Linus Torvalds, (Fri Aug 6, 1:11 pm)
Re: [GIT PATCH] TTY patches for 2.6.36, Arnd Bergmann, (Fri Aug 6, 1:19 pm)
Re: [GIT PATCH] TTY patches for 2.6.36, Greg KH, (Fri Aug 6, 1:22 pm)
Re: [GIT PATCH] TTY patches for 2.6.36, Andy Whitcroft, (Mon Aug 9, 9:06 am)
Re: [GIT PATCH] TTY patches for 2.6.36, Arnd Bergmann, (Mon Aug 9, 11:38 am)
Re: [GIT PATCH] TTY patches for 2.6.36, Alan Cox, (Mon Aug 9, 12:24 pm)
Re: [GIT PATCH] TTY patches for 2.6.36, Andy Whitcroft, (Tue Aug 10, 12:31 am)