Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

tty: localise the lock

The termios and other changes mean the other protections needed on the driver
tty arrays should be adequate. Turn it all back on.

This contains pieces folded in from the fixes made to the original patches

| From: Geert Uytterhoeven <geert@linux-m68k.org> (fix m68k)
| From: Paul Gortmaker <paul.gortmaker@windriver.com> (fix cris)
| From: Jiri Kosina <jkosina@suze.cz> (lockdep)
| From: Eric Dumazet <eric.dumazet@gmail.com> (lockdep)

Signed-off-by: Alan Cox <alan@linux.intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Alan Cox and committed by
Greg Kroah-Hartman
89c8d91e dc6802a7

+192 -122
+7 -7
drivers/tty/amiserial.c
··· 1033 1033 if (!retinfo) 1034 1034 return -EFAULT; 1035 1035 memset(&tmp, 0, sizeof(tmp)); 1036 - tty_lock(); 1036 + tty_lock(tty); 1037 1037 tmp.line = tty->index; 1038 1038 tmp.port = state->port; 1039 1039 tmp.flags = state->tport.flags; ··· 1042 1042 tmp.close_delay = state->tport.close_delay; 1043 1043 tmp.closing_wait = state->tport.closing_wait; 1044 1044 tmp.custom_divisor = state->custom_divisor; 1045 - tty_unlock(); 1045 + tty_unlock(tty); 1046 1046 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo))) 1047 1047 return -EFAULT; 1048 1048 return 0; ··· 1059 1059 if (copy_from_user(&new_serial,new_info,sizeof(new_serial))) 1060 1060 return -EFAULT; 1061 1061 1062 - tty_lock(); 1062 + tty_lock(tty); 1063 1063 change_spd = ((new_serial.flags ^ port->flags) & ASYNC_SPD_MASK) || 1064 1064 new_serial.custom_divisor != state->custom_divisor; 1065 1065 if (new_serial.irq || new_serial.port != state->port || 1066 1066 new_serial.xmit_fifo_size != state->xmit_fifo_size) { 1067 - tty_unlock(); 1067 + tty_unlock(tty); 1068 1068 return -EINVAL; 1069 1069 } 1070 1070 ··· 1074 1074 (new_serial.xmit_fifo_size != state->xmit_fifo_size) || 1075 1075 ((new_serial.flags & ~ASYNC_USR_MASK) != 1076 1076 (port->flags & ~ASYNC_USR_MASK))) { 1077 - tty_unlock(); 1077 + tty_unlock(tty); 1078 1078 return -EPERM; 1079 1079 } 1080 1080 port->flags = ((port->flags & ~ASYNC_USR_MASK) | ··· 1084 1084 } 1085 1085 1086 1086 if (new_serial.baud_base < 9600) { 1087 - tty_unlock(); 1087 + tty_unlock(tty); 1088 1088 return -EINVAL; 1089 1089 } 1090 1090 ··· 1116 1116 } 1117 1117 } else 1118 1118 retval = startup(tty, state); 1119 - tty_unlock(); 1119 + tty_unlock(tty); 1120 1120 return retval; 1121 1121 } 1122 1122
+1 -1
drivers/tty/cyclades.c
··· 1599 1599 * If the port is the middle of closing, bail out now 1600 1600 */ 1601 1601 if (tty_hung_up_p(filp) || (info->port.flags & ASYNC_CLOSING)) { 1602 - wait_event_interruptible_tty(info->port.close_wait, 1602 + wait_event_interruptible_tty(tty, info->port.close_wait, 1603 1603 !(info->port.flags & ASYNC_CLOSING)); 1604 1604 return (info->port.flags & ASYNC_HUP_NOTIFY) ? -EAGAIN: -ERESTARTSYS; 1605 1605 }
+5 -5
drivers/tty/n_r3964.c
··· 1065 1065 1066 1066 TRACE_L("read()"); 1067 1067 1068 - tty_lock(); 1068 + tty_lock(tty); 1069 1069 1070 1070 pClient = findClient(pInfo, task_pid(current)); 1071 1071 if (pClient) { ··· 1077 1077 goto unlock; 1078 1078 } 1079 1079 /* block until there is a message: */ 1080 - wait_event_interruptible_tty(pInfo->read_wait, 1080 + wait_event_interruptible_tty(tty, pInfo->read_wait, 1081 1081 (pMsg = remove_msg(pInfo, pClient))); 1082 1082 } 1083 1083 ··· 1107 1107 } 1108 1108 ret = -EPERM; 1109 1109 unlock: 1110 - tty_unlock(); 1110 + tty_unlock(tty); 1111 1111 return ret; 1112 1112 } 1113 1113 ··· 1156 1156 pHeader->locks = 0; 1157 1157 pHeader->owner = NULL; 1158 1158 1159 - tty_lock(); 1159 + tty_lock(tty); 1160 1160 1161 1161 pClient = findClient(pInfo, task_pid(current)); 1162 1162 if (pClient) { ··· 1175 1175 add_tx_queue(pInfo, pHeader); 1176 1176 trigger_transmit(pInfo); 1177 1177 1178 - tty_unlock(); 1178 + tty_unlock(tty); 1179 1179 1180 1180 return 0; 1181 1181 }
+15 -12
drivers/tty/pty.c
··· 47 47 wake_up_interruptible(&tty->read_wait); 48 48 wake_up_interruptible(&tty->write_wait); 49 49 tty->packet = 0; 50 + /* Review - krefs on tty_link ?? */ 50 51 if (!tty->link) 51 52 return; 52 53 tty->link->packet = 0; ··· 63 62 mutex_unlock(&devpts_mutex); 64 63 } 65 64 #endif 66 - tty_unlock(); 65 + tty_unlock(tty); 67 66 tty_vhangup(tty->link); 68 - tty_lock(); 67 + tty_lock(tty); 69 68 } 70 69 } 71 70 ··· 618 617 return retval; 619 618 620 619 /* find a device that is not in use. */ 621 - tty_lock(); 620 + mutex_lock(&devpts_mutex); 622 621 index = devpts_new_index(inode); 623 - tty_unlock(); 624 622 if (index < 0) { 625 623 retval = index; 626 624 goto err_file; 627 625 } 628 626 629 - mutex_lock(&tty_mutex); 630 - mutex_lock(&devpts_mutex); 631 - tty = tty_init_dev(ptm_driver, index); 632 627 mutex_unlock(&devpts_mutex); 633 - tty_lock(); 634 - mutex_unlock(&tty_mutex); 628 + 629 + mutex_lock(&tty_mutex); 630 + tty = tty_init_dev(ptm_driver, index); 635 631 636 632 if (IS_ERR(tty)) { 637 633 retval = PTR_ERR(tty); 638 634 goto out; 639 635 } 636 + 637 + /* The tty returned here is locked so we can safely 638 + drop the mutex */ 639 + mutex_unlock(&tty_mutex); 640 640 641 641 set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */ 642 642 ··· 651 649 if (retval) 652 650 goto err_release; 653 651 654 - tty_unlock(); 652 + tty_unlock(tty); 655 653 return 0; 656 654 err_release: 657 - tty_unlock(); 655 + tty_unlock(tty); 658 656 tty_release(inode, filp); 659 657 return retval; 660 658 out: 659 + mutex_unlock(&tty_mutex); 661 660 devpts_kill_index(inode, index); 662 - tty_unlock(); 663 661 err_file: 662 + mutex_unlock(&devpts_mutex); 664 663 tty_free_file(filp); 665 664 return retval; 666 665 }
+4 -4
drivers/tty/serial/crisv10.c
··· 3976 3976 */ 3977 3977 if (tty_hung_up_p(filp) || 3978 3978 (info->flags & ASYNC_CLOSING)) { 3979 - wait_event_interruptible_tty(info->close_wait, 3979 + wait_event_interruptible_tty(tty, info->close_wait, 3980 3980 !(info->flags & ASYNC_CLOSING)); 3981 3981 #ifdef SERIAL_DO_RESTART 3982 3982 if (info->flags & ASYNC_HUP_NOTIFY) ··· 4052 4052 printk("block_til_ready blocking: ttyS%d, count = %d\n", 4053 4053 info->line, info->count); 4054 4054 #endif 4055 - tty_unlock(); 4055 + tty_unlock(tty); 4056 4056 schedule(); 4057 - tty_lock(); 4057 + tty_lock(tty); 4058 4058 } 4059 4059 set_current_state(TASK_RUNNING); 4060 4060 remove_wait_queue(&info->open_wait, &wait); ··· 4115 4115 */ 4116 4116 if (tty_hung_up_p(filp) || 4117 4117 (info->flags & ASYNC_CLOSING)) { 4118 - wait_event_interruptible_tty(info->close_wait, 4118 + wait_event_interruptible_tty(tty, info->close_wait, 4119 4119 !(info->flags & ASYNC_CLOSING)); 4120 4120 #ifdef SERIAL_DO_RESTART 4121 4121 return ((info->flags & ASYNC_HUP_NOTIFY) ?
+2 -2
drivers/tty/synclink.c
··· 3338 3338 printk("%s(%d):block_til_ready blocking on %s count=%d\n", 3339 3339 __FILE__,__LINE__, tty->driver->name, port->count ); 3340 3340 3341 - tty_unlock(); 3341 + tty_unlock(tty); 3342 3342 schedule(); 3343 - tty_lock(); 3343 + tty_lock(tty); 3344 3344 } 3345 3345 3346 3346 set_current_state(TASK_RUNNING);
+2 -2
drivers/tty/synclinkmp.c
··· 3357 3357 printk("%s(%d):%s block_til_ready() count=%d\n", 3358 3358 __FILE__,__LINE__, tty->driver->name, port->count ); 3359 3359 3360 - tty_unlock(); 3360 + tty_unlock(tty); 3361 3361 schedule(); 3362 - tty_lock(); 3362 + tty_lock(tty); 3363 3363 } 3364 3364 3365 3365 set_current_state(TASK_RUNNING);
+39 -27
drivers/tty/tty_io.c
··· 187 187 put_device(tty->dev); 188 188 kfree(tty->write_buf); 189 189 tty_buffer_free_all(tty); 190 + tty->magic = 0xDEADDEAD; 190 191 kfree(tty); 191 192 } 192 193 ··· 576 575 } 577 576 spin_unlock(&redirect_lock); 578 577 579 - tty_lock(); 578 + tty_lock(tty); 580 579 581 580 /* some functions below drop BTM, so we need this bit */ 582 581 set_bit(TTY_HUPPING, &tty->flags); ··· 669 668 clear_bit(TTY_HUPPING, &tty->flags); 670 669 tty_ldisc_enable(tty); 671 670 672 - tty_unlock(); 671 + tty_unlock(tty); 673 672 674 673 if (f) 675 674 fput(f); ··· 1106 1105 { 1107 1106 if (tty) { 1108 1107 mutex_lock(&tty->atomic_write_lock); 1109 - tty_lock(); 1108 + tty_lock(tty); 1110 1109 if (tty->ops->write && !test_bit(TTY_CLOSING, &tty->flags)) { 1111 - tty_unlock(); 1110 + tty_unlock(tty); 1112 1111 tty->ops->write(tty, msg, strlen(msg)); 1113 1112 } else 1114 - tty_unlock(); 1113 + tty_unlock(tty); 1115 1114 tty_write_unlock(tty); 1116 1115 } 1117 1116 return; ··· 1404 1403 } 1405 1404 initialize_tty_struct(tty, driver, idx); 1406 1405 1406 + tty_lock(tty); 1407 1407 retval = tty_driver_install_tty(driver, tty); 1408 1408 if (retval < 0) 1409 1409 goto err_deinit_tty; ··· 1420 1418 retval = tty_ldisc_setup(tty, tty->link); 1421 1419 if (retval) 1422 1420 goto err_release_tty; 1421 + /* Return the tty locked so that it cannot vanish under the caller */ 1423 1422 return tty; 1424 1423 1425 1424 err_deinit_tty: 1425 + tty_unlock(tty); 1426 1426 deinitialize_tty_struct(tty); 1427 1427 free_tty_struct(tty); 1428 1428 err_module_put: ··· 1433 1429 1434 1430 /* call the tty release_tty routine to clean out this slot */ 1435 1431 err_release_tty: 1432 + tty_unlock(tty); 1436 1433 printk_ratelimited(KERN_INFO "tty_init_dev: ldisc open failed, " 1437 1434 "clearing slot %d\n", idx); 1438 1435 release_tty(tty, idx); ··· 1627 1622 if (tty_paranoia_check(tty, inode, __func__)) 1628 1623 return 0; 1629 1624 1630 - tty_lock(); 1625 + tty_lock(tty); 1631 1626 check_tty_count(tty, __func__); 1632 1627 1633 1628 __tty_fasync(-1, filp, 0); ··· 1636 1631 pty_master = (tty->driver->type == TTY_DRIVER_TYPE_PTY && 1637 1632 tty->driver->subtype == PTY_TYPE_MASTER); 1638 1633 devpts = (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) != 0; 1634 + /* Review: parallel close */ 1639 1635 o_tty = tty->link; 1640 1636 1641 1637 if (tty_release_checks(tty, o_tty, idx)) { 1642 - tty_unlock(); 1638 + tty_unlock(tty); 1643 1639 return 0; 1644 1640 } 1645 1641 ··· 1652 1646 if (tty->ops->close) 1653 1647 tty->ops->close(tty, filp); 1654 1648 1655 - tty_unlock(); 1649 + tty_unlock(tty); 1656 1650 /* 1657 1651 * Sanity check: if tty->count is going to zero, there shouldn't be 1658 1652 * any waiters on tty->read_wait or tty->write_wait. We test the ··· 1675 1669 opens on /dev/tty */ 1676 1670 1677 1671 mutex_lock(&tty_mutex); 1678 - tty_lock(); 1672 + tty_lock_pair(tty, o_tty); 1679 1673 tty_closing = tty->count <= 1; 1680 1674 o_tty_closing = o_tty && 1681 1675 (o_tty->count <= (pty_master ? 1 : 0)); ··· 1706 1700 1707 1701 printk(KERN_WARNING "%s: %s: read/write wait queue active!\n", 1708 1702 __func__, tty_name(tty, buf)); 1709 - tty_unlock(); 1703 + tty_unlock_pair(tty, o_tty); 1710 1704 mutex_unlock(&tty_mutex); 1711 1705 schedule(); 1712 1706 } ··· 1769 1763 } 1770 1764 1771 1765 mutex_unlock(&tty_mutex); 1772 - tty_unlock(); 1766 + tty_unlock_pair(tty, o_tty); 1773 1767 /* At this point the TTY_CLOSING flag should ensure a dead tty 1774 1768 cannot be re-opened by a racing opener */ 1775 1769 ··· 1786 1780 tty_ldisc_release(tty, o_tty); 1787 1781 /* 1788 1782 * The release_tty function takes care of the details of clearing 1789 - * the slots and preserving the termios structure. 1783 + * the slots and preserving the termios structure. The tty_unlock_pair 1784 + * should be safe as we keep a kref while the tty is locked (so the 1785 + * unlock never unlocks a freed tty). 1790 1786 */ 1791 1787 mutex_lock(&tty_mutex); 1792 1788 release_tty(tty, idx); ··· 1797 1789 /* Make this pty number available for reallocation */ 1798 1790 if (devpts) 1799 1791 devpts_kill_index(inode, idx); 1800 - 1801 1792 return 0; 1802 1793 } 1803 1794 ··· 1900 1893 * Locking: tty_mutex protects tty, tty_lookup_driver and tty_init_dev. 1901 1894 * tty->count should protect the rest. 1902 1895 * ->siglock protects ->signal/->sighand 1896 + * 1897 + * Note: the tty_unlock/lock cases without a ref are only safe due to 1898 + * tty_mutex 1903 1899 */ 1904 1900 1905 1901 static int tty_open(struct inode *inode, struct file *filp) ··· 1926 1916 retval = 0; 1927 1917 1928 1918 mutex_lock(&tty_mutex); 1929 - tty_lock(); 1930 - 1919 + /* This is protected by the tty_mutex */ 1931 1920 tty = tty_open_current_tty(device, filp); 1932 1921 if (IS_ERR(tty)) { 1933 1922 retval = PTR_ERR(tty); ··· 1947 1938 } 1948 1939 1949 1940 if (tty) { 1941 + tty_lock(tty); 1950 1942 retval = tty_reopen(tty); 1951 - if (retval) 1943 + if (retval < 0) { 1944 + tty_unlock(tty); 1952 1945 tty = ERR_PTR(retval); 1953 - } else 1946 + } 1947 + } else /* Returns with the tty_lock held for now */ 1954 1948 tty = tty_init_dev(driver, index); 1955 1949 1956 1950 mutex_unlock(&tty_mutex); 1957 1951 if (driver) 1958 1952 tty_driver_kref_put(driver); 1959 1953 if (IS_ERR(tty)) { 1960 - tty_unlock(); 1961 1954 retval = PTR_ERR(tty); 1962 1955 goto err_file; 1963 1956 } ··· 1988 1977 printk(KERN_DEBUG "%s: error %d in opening %s...\n", __func__, 1989 1978 retval, tty->name); 1990 1979 #endif 1991 - tty_unlock(); /* need to call tty_release without BTM */ 1980 + tty_unlock(tty); /* need to call tty_release without BTM */ 1992 1981 tty_release(inode, filp); 1993 1982 if (retval != -ERESTARTSYS) 1994 1983 return retval; ··· 2000 1989 /* 2001 1990 * Need to reset f_op in case a hangup happened. 2002 1991 */ 2003 - tty_lock(); 2004 1992 if (filp->f_op == &hung_up_tty_fops) 2005 1993 filp->f_op = &tty_fops; 2006 - tty_unlock(); 2007 1994 goto retry_open; 2008 1995 } 2009 - tty_unlock(); 1996 + tty_unlock(tty); 2010 1997 2011 1998 2012 1999 mutex_lock(&tty_mutex); 2013 - tty_lock(); 2000 + tty_lock(tty); 2014 2001 spin_lock_irq(&current->sighand->siglock); 2015 2002 if (!noctty && 2016 2003 current->signal->leader && ··· 2016 2007 tty->session == NULL) 2017 2008 __proc_set_tty(current, tty); 2018 2009 spin_unlock_irq(&current->sighand->siglock); 2019 - tty_unlock(); 2010 + tty_unlock(tty); 2020 2011 mutex_unlock(&tty_mutex); 2021 2012 return 0; 2022 2013 err_unlock: 2023 - tty_unlock(); 2024 2014 mutex_unlock(&tty_mutex); 2025 2015 /* after locks to avoid deadlock */ 2026 2016 if (!IS_ERR_OR_NULL(driver)) ··· 2102 2094 2103 2095 static int tty_fasync(int fd, struct file *filp, int on) 2104 2096 { 2097 + struct tty_struct *tty = file_tty(filp); 2105 2098 int retval; 2106 - tty_lock(); 2099 + 2100 + tty_lock(tty); 2107 2101 retval = __tty_fasync(fd, filp, on); 2108 - tty_unlock(); 2102 + tty_unlock(tty); 2103 + 2109 2104 return retval; 2110 2105 } 2111 2106 ··· 2945 2934 tty->pgrp = NULL; 2946 2935 tty->overrun_time = jiffies; 2947 2936 tty_buffer_init(tty); 2937 + mutex_init(&tty->legacy_mutex); 2948 2938 mutex_init(&tty->termios_mutex); 2949 2939 mutex_init(&tty->ldisc_mutex); 2950 2940 init_waitqueue_head(&tty->write_wait);
+40 -31
drivers/tty/tty_ldisc.c
··· 568 568 if (IS_ERR(new_ldisc)) 569 569 return PTR_ERR(new_ldisc); 570 570 571 - tty_lock(); 571 + tty_lock(tty); 572 572 /* 573 573 * We need to look at the tty locking here for pty/tty pairs 574 574 * when both sides try to change in parallel. ··· 582 582 */ 583 583 584 584 if (tty->ldisc->ops->num == ldisc) { 585 - tty_unlock(); 585 + tty_unlock(tty); 586 586 tty_ldisc_put(new_ldisc); 587 587 return 0; 588 588 } 589 589 590 - tty_unlock(); 590 + tty_unlock(tty); 591 591 /* 592 592 * Problem: What do we do if this blocks ? 593 593 * We could deadlock here ··· 595 595 596 596 tty_wait_until_sent(tty, 0); 597 597 598 - tty_lock(); 598 + tty_lock(tty); 599 599 mutex_lock(&tty->ldisc_mutex); 600 600 601 601 /* ··· 605 605 606 606 while (test_bit(TTY_LDISC_CHANGING, &tty->flags)) { 607 607 mutex_unlock(&tty->ldisc_mutex); 608 - tty_unlock(); 608 + tty_unlock(tty); 609 609 wait_event(tty_ldisc_wait, 610 610 test_bit(TTY_LDISC_CHANGING, &tty->flags) == 0); 611 - tty_lock(); 611 + tty_lock(tty); 612 612 mutex_lock(&tty->ldisc_mutex); 613 613 } 614 614 ··· 623 623 624 624 o_ldisc = tty->ldisc; 625 625 626 - tty_unlock(); 626 + tty_unlock(tty); 627 627 /* 628 628 * Make sure we don't change while someone holds a 629 629 * reference to the line discipline. The TTY_LDISC bit ··· 650 650 651 651 retval = tty_ldisc_wait_idle(tty, 5 * HZ); 652 652 653 - tty_lock(); 653 + tty_lock(tty); 654 654 mutex_lock(&tty->ldisc_mutex); 655 655 656 656 /* handle wait idle failure locked */ ··· 665 665 clear_bit(TTY_LDISC_CHANGING, &tty->flags); 666 666 mutex_unlock(&tty->ldisc_mutex); 667 667 tty_ldisc_put(new_ldisc); 668 - tty_unlock(); 668 + tty_unlock(tty); 669 669 return -EIO; 670 670 } 671 671 ··· 708 708 if (o_work) 709 709 schedule_work(&o_tty->buf.work); 710 710 mutex_unlock(&tty->ldisc_mutex); 711 - tty_unlock(); 711 + tty_unlock(tty); 712 712 return retval; 713 713 } 714 714 ··· 816 816 * need to wait for another function taking the BTM 817 817 */ 818 818 clear_bit(TTY_LDISC, &tty->flags); 819 - tty_unlock(); 819 + tty_unlock(tty); 820 820 cancel_work_sync(&tty->buf.work); 821 821 mutex_unlock(&tty->ldisc_mutex); 822 822 retry: 823 - tty_lock(); 823 + tty_lock(tty); 824 824 mutex_lock(&tty->ldisc_mutex); 825 825 826 826 /* At this point we have a closed ldisc and we want to ··· 831 831 if (atomic_read(&tty->ldisc->users) != 1) { 832 832 char cur_n[TASK_COMM_LEN], tty_n[64]; 833 833 long timeout = 3 * HZ; 834 - tty_unlock(); 834 + tty_unlock(tty); 835 835 836 836 while (tty_ldisc_wait_idle(tty, timeout) == -EBUSY) { 837 837 timeout = MAX_SCHEDULE_TIMEOUT; ··· 894 894 tty_ldisc_enable(tty); 895 895 return 0; 896 896 } 897 + 898 + static void tty_ldisc_kill(struct tty_struct *tty) 899 + { 900 + mutex_lock(&tty->ldisc_mutex); 901 + /* 902 + * Now kill off the ldisc 903 + */ 904 + tty_ldisc_close(tty, tty->ldisc); 905 + tty_ldisc_put(tty->ldisc); 906 + /* Force an oops if we mess this up */ 907 + tty->ldisc = NULL; 908 + 909 + /* Ensure the next open requests the N_TTY ldisc */ 910 + tty_set_termios_ldisc(tty, N_TTY); 911 + mutex_unlock(&tty->ldisc_mutex); 912 + } 913 + 897 914 /** 898 915 * tty_ldisc_release - release line discipline 899 916 * @tty: tty being shut down ··· 929 912 * race with the set_ldisc code path. 930 913 */ 931 914 915 + tty_lock_pair(tty, o_tty); 932 916 tty_ldisc_halt(tty); 933 917 tty_ldisc_flush_works(tty); 934 - tty_lock(); 935 - 936 - mutex_lock(&tty->ldisc_mutex); 937 - /* 938 - * Now kill off the ldisc 939 - */ 940 - tty_ldisc_close(tty, tty->ldisc); 941 - tty_ldisc_put(tty->ldisc); 942 - /* Force an oops if we mess this up */ 943 - tty->ldisc = NULL; 944 - 945 - /* Ensure the next open requests the N_TTY ldisc */ 946 - tty_set_termios_ldisc(tty, N_TTY); 947 - mutex_unlock(&tty->ldisc_mutex); 948 - 949 - tty_unlock(); 918 + if (o_tty) { 919 + tty_ldisc_halt(o_tty); 920 + tty_ldisc_flush_works(o_tty); 921 + } 950 922 951 923 /* This will need doing differently if we need to lock */ 952 - if (o_tty) 953 - tty_ldisc_release(o_tty, NULL); 924 + tty_ldisc_kill(tty); 954 925 926 + if (o_tty) 927 + tty_ldisc_kill(o_tty); 928 + 929 + tty_unlock_pair(tty, o_tty); 955 930 /* And the memory resources remaining (buffers, termios) will be 956 931 disposed of when the kref hits zero */ 957 932 }
+56 -15
drivers/tty/tty_mutex.c
··· 4 4 #include <linux/semaphore.h> 5 5 #include <linux/sched.h> 6 6 7 - /* 8 - * The 'big tty mutex' 9 - * 10 - * This mutex is taken and released by tty_lock() and tty_unlock(), 11 - * replacing the older big kernel lock. 12 - * It can no longer be taken recursively, and does not get 13 - * released implicitly while sleeping. 14 - * 15 - * Don't use in new code. 16 - */ 17 - static DEFINE_MUTEX(big_tty_mutex); 7 + /* Legacy tty mutex glue */ 8 + 9 + enum { 10 + TTY_MUTEX_NORMAL, 11 + TTY_MUTEX_NESTED, 12 + }; 18 13 19 14 /* 20 15 * Getting the big tty mutex. 21 16 */ 22 - void __lockfunc tty_lock(void) 17 + 18 + static void __lockfunc tty_lock_nested(struct tty_struct *tty, 19 + unsigned int subclass) 23 20 { 24 - mutex_lock(&big_tty_mutex); 21 + if (tty->magic != TTY_MAGIC) { 22 + printk(KERN_ERR "L Bad %p\n", tty); 23 + WARN_ON(1); 24 + return; 25 + } 26 + tty_kref_get(tty); 27 + mutex_lock_nested(&tty->legacy_mutex, subclass); 28 + } 29 + 30 + void __lockfunc tty_lock(struct tty_struct *tty) 31 + { 32 + return tty_lock_nested(tty, TTY_MUTEX_NORMAL); 25 33 } 26 34 EXPORT_SYMBOL(tty_lock); 27 35 28 - void __lockfunc tty_unlock(void) 36 + void __lockfunc tty_unlock(struct tty_struct *tty) 29 37 { 30 - mutex_unlock(&big_tty_mutex); 38 + if (tty->magic != TTY_MAGIC) { 39 + printk(KERN_ERR "U Bad %p\n", tty); 40 + WARN_ON(1); 41 + return; 42 + } 43 + mutex_unlock(&tty->legacy_mutex); 44 + tty_kref_put(tty); 31 45 } 32 46 EXPORT_SYMBOL(tty_unlock); 47 + 48 + /* 49 + * Getting the big tty mutex for a pair of ttys with lock ordering 50 + * On a non pty/tty pair tty2 can be NULL which is just fine. 51 + */ 52 + void __lockfunc tty_lock_pair(struct tty_struct *tty, 53 + struct tty_struct *tty2) 54 + { 55 + if (tty < tty2) { 56 + tty_lock(tty); 57 + tty_lock_nested(tty2, TTY_MUTEX_NESTED); 58 + } else { 59 + if (tty2 && tty2 != tty) 60 + tty_lock(tty2); 61 + tty_lock_nested(tty, TTY_MUTEX_NESTED); 62 + } 63 + } 64 + EXPORT_SYMBOL(tty_lock_pair); 65 + 66 + void __lockfunc tty_unlock_pair(struct tty_struct *tty, 67 + struct tty_struct *tty2) 68 + { 69 + tty_unlock(tty); 70 + if (tty2 && tty2 != tty) 71 + tty_unlock(tty2); 72 + } 73 + EXPORT_SYMBOL(tty_unlock_pair);
+3 -3
drivers/tty/tty_port.c
··· 239 239 240 240 /* block if port is in the process of being closed */ 241 241 if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) { 242 - wait_event_interruptible_tty(port->close_wait, 242 + wait_event_interruptible_tty(tty, port->close_wait, 243 243 !(port->flags & ASYNC_CLOSING)); 244 244 if (port->flags & ASYNC_HUP_NOTIFY) 245 245 return -EAGAIN; ··· 305 305 retval = -ERESTARTSYS; 306 306 break; 307 307 } 308 - tty_unlock(); 308 + tty_unlock(tty); 309 309 schedule(); 310 - tty_lock(); 310 + tty_lock(tty); 311 311 } 312 312 finish_wait(&port->open_wait, &wait); 313 313
+14 -9
include/linux/tty.h
··· 268 268 struct mutex ldisc_mutex; 269 269 struct tty_ldisc *ldisc; 270 270 271 + struct mutex legacy_mutex; 271 272 struct mutex termios_mutex; 272 273 spinlock_t ctrl_lock; 273 274 /* Termios values are protected by the termios mutex */ ··· 610 609 611 610 /* tty_mutex.c */ 612 611 /* functions for preparation of BKL removal */ 613 - extern void __lockfunc tty_lock(void) __acquires(tty_lock); 614 - extern void __lockfunc tty_unlock(void) __releases(tty_lock); 612 + extern void __lockfunc tty_lock(struct tty_struct *tty); 613 + extern void __lockfunc tty_unlock(struct tty_struct *tty); 614 + extern void __lockfunc tty_lock_pair(struct tty_struct *tty, 615 + struct tty_struct *tty2); 616 + extern void __lockfunc tty_unlock_pair(struct tty_struct *tty, 617 + struct tty_struct *tty2); 615 618 616 619 /* 617 620 * this shall be called only from where BTM is held (like close) ··· 630 625 static inline void tty_wait_until_sent_from_close(struct tty_struct *tty, 631 626 long timeout) 632 627 { 633 - tty_unlock(); /* tty->ops->close holds the BTM, drop it while waiting */ 628 + tty_unlock(tty); /* tty->ops->close holds the BTM, drop it while waiting */ 634 629 tty_wait_until_sent(tty, timeout); 635 - tty_lock(); 630 + tty_lock(tty); 636 631 } 637 632 638 633 /* ··· 647 642 * 648 643 * Do not use in new code. 649 644 */ 650 - #define wait_event_interruptible_tty(wq, condition) \ 645 + #define wait_event_interruptible_tty(tty, wq, condition) \ 651 646 ({ \ 652 647 int __ret = 0; \ 653 648 if (!(condition)) { \ 654 - __wait_event_interruptible_tty(wq, condition, __ret); \ 649 + __wait_event_interruptible_tty(tty, wq, condition, __ret); \ 655 650 } \ 656 651 __ret; \ 657 652 }) 658 653 659 - #define __wait_event_interruptible_tty(wq, condition, ret) \ 654 + #define __wait_event_interruptible_tty(tty, wq, condition, ret) \ 660 655 do { \ 661 656 DEFINE_WAIT(__wait); \ 662 657 \ ··· 665 660 if (condition) \ 666 661 break; \ 667 662 if (!signal_pending(current)) { \ 668 - tty_unlock(); \ 663 + tty_unlock(tty); \ 669 664 schedule(); \ 670 - tty_lock(); \ 665 + tty_lock(tty); \ 671 666 continue; \ 672 667 } \ 673 668 ret = -ERESTARTSYS; \
+2 -2
net/bluetooth/rfcomm/tty.c
··· 705 705 break; 706 706 } 707 707 708 - tty_unlock(); 708 + tty_unlock(tty); 709 709 schedule(); 710 - tty_lock(); 710 + tty_lock(tty); 711 711 } 712 712 set_current_state(TASK_RUNNING); 713 713 remove_wait_queue(&dev->wait, &wait);