Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e359a4e3 authored by Peter Hurley's avatar Peter Hurley Committed by Greg Kroah-Hartman
Browse files

tty: Remove tty_hung_up_p() tests from tty drivers' open()



Since at least before 2.6.30, it has not been possible to observe
a hung up file pointer in a tty driver's open() method unless/until
the driver open() releases the tty_lock() (eg., before blocking).

This is because tty_open() adds the file pointer while holding
the tty_lock() _and_ doesn't release the lock until after calling
the tty driver's open() method. [ Before tty_lock(), this was
lock_kernel(). ]

Since __tty_hangup() first waits on the tty_lock() before
enumerating and hanging up the open file pointers, either
__tty_hangup() will wait for the tty_lock() or tty_open() will
not yet have added the file pointer. For example,

CPU 0                          |  CPU 1
                               |
tty_open                       |  __tty_hangup
  ..                           |    ..
  tty_lock                     |    ..
  tty_reopen                   |    tty_lock  / blocks
  ..                           |
  tty_add_file(tty, filp)      |
  ..                           |
  tty->ops->open(tty, filp)    |
    tty_port_open              |
      tty_port_block_til_ready |
        ..                     |
        while (1)              |
          ..                   |
          tty_unlock           |    / unblocks
          schedule             |    for each filp on tty->tty_files
                               |      f_ops = tty_hung_up_fops;
                               |    ..
                               |    tty_unlock
          tty_lock             |
  ..                           |
  tty_unlock                   |

Note that since tty_port_block_til_ready() and similar drop
the tty_lock while blocking, when woken, the file pointer
must then be tested for having been hung up.

Also, fix bit-rotted drivers that used extra_count to track the
port->count bump.

CC: Mikael Starvik <starvik@axis.com>
CC: Samuel Ortiz <samuel@sortiz.org>
CC: "David S. Miller" <davem@davemloft.net>
Signed-off-by: default avatarPeter Hurley <peter@hurleysoftware.com>
Acked-by: default avatarJesper Nilsson <jesper.nilsson@axis.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 5fda7a0e
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -2510,7 +2510,7 @@ static int mgslpc_open(struct tty_struct *tty, struct file * filp)
			 __FILE__, __LINE__, tty->driver->name, port->count);

	/* If port is closing, signal caller to try again */
	if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING){
	if (port->flags & ASYNC_CLOSING){
		wait_event_interruptible_tty(tty, port->close_wait,
					     !(port->flags & ASYNC_CLOSING));
		retval = ((port->flags & ASYNC_HUP_NOTIFY) ?
+0 −10
Original line number Diff line number Diff line
@@ -631,16 +631,6 @@ static int dgrp_tty_open(struct tty_struct *tty, struct file *file)
	un->un_tty = tty;
	tty->driver_data = un;

	/*
	 * If we are in the middle of hanging up,
	 * then return an error
	 */
	if (tty_hung_up_p(file)) {
		retval = ((un->un_flag & UN_HUP_NOTIFY) ?
			   -EAGAIN : -ERESTARTSYS);
		goto done;
	}

	/*
	 * If the port is in the middle of closing, then block
	 * until it is done, then try again.
+1 −1
Original line number Diff line number Diff line
@@ -1579,7 +1579,7 @@ static int cy_open(struct tty_struct *tty, struct file *filp)
	/*
	 * If the port is the middle of closing, bail out now
	 */
	if (tty_hung_up_p(filp) || (info->port.flags & ASYNC_CLOSING)) {
	if (info->port.flags & ASYNC_CLOSING) {
		wait_event_interruptible_tty(tty, info->port.close_wait,
				!(info->port.flags & ASYNC_CLOSING));
		return (info->port.flags & ASYNC_HUP_NOTIFY) ? -EAGAIN: -ERESTARTSYS;
+5 −10
Original line number Diff line number Diff line
@@ -3831,14 +3831,13 @@ block_til_ready(struct tty_struct *tty, struct file * filp,
	DECLARE_WAITQUEUE(wait, current);
	unsigned long	flags;
	int		retval;
	int		do_clocal = 0, extra_count = 0;
	int		do_clocal = 0;

	/*
	 * If the device is in the middle of being closed, then block
	 * until it's done, and then try again.
	 */
	if (tty_hung_up_p(filp) ||
	    (info->port.flags & ASYNC_CLOSING)) {
	if (info->port.flags & ASYNC_CLOSING) {
		wait_event_interruptible_tty(tty, info->port.close_wait,
			!(info->port.flags & ASYNC_CLOSING));
#ifdef SERIAL_DO_RESTART
@@ -3879,10 +3878,7 @@ block_til_ready(struct tty_struct *tty, struct file * filp,
	       info->line, info->port.count);
#endif
	local_irq_save(flags);
	if (!tty_hung_up_p(filp)) {
		extra_count++;
	info->port.count--;
	}
	local_irq_restore(flags);
	info->port.blocked_open++;
	while (1) {
@@ -3921,7 +3917,7 @@ block_til_ready(struct tty_struct *tty, struct file * filp,
	}
	set_current_state(TASK_RUNNING);
	remove_wait_queue(&info->port.open_wait, &wait);
	if (extra_count)
	if (!tty_hung_up_p(filp))
		info->port.count++;
	info->port.blocked_open--;
#ifdef SERIAL_DEBUG_OPEN
@@ -3976,8 +3972,7 @@ rs_open(struct tty_struct *tty, struct file * filp)
	/*
	 * If the port is in the middle of closing, bail out now
	 */
	if (tty_hung_up_p(filp) ||
	    (info->port.flags & ASYNC_CLOSING)) {
	if (info->port.flags & ASYNC_CLOSING) {
		wait_event_interruptible_tty(tty, info->port.close_wait,
			!(info->port.flags & ASYNC_CLOSING));
#ifdef SERIAL_DO_RESTART
+0 −8
Original line number Diff line number Diff line
@@ -1583,14 +1583,6 @@ static int uart_open(struct tty_struct *tty, struct file *filp)
		(state->uart_port->flags & UPF_LOW_LATENCY) ? 1 : 0;
	tty_port_tty_set(port, tty);

	/*
	 * If the port is in the middle of closing, bail out now.
	 */
	if (tty_hung_up_p(filp)) {
		retval = -EAGAIN;
		goto err_dec_count;
	}

	/*
	 * Start up the serial port.
	 */
Loading