aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/tty/tty_io.c
diff options
context:
space:
mode:
authorAlan Cox <alan@linux.intel.com>2012-08-08 11:30:13 -0400
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2012-08-10 15:55:47 -0400
commit89c8d91e31f267703e365593f6bfebb9f6d2ad01 (patch)
treeb115c7738762abe4a8a6374debb4991382b2f785 /drivers/tty/tty_io.c
parentdc6802a771e91050fb686dfeeb9de4c6c9cadb79 (diff)
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>
Diffstat (limited to 'drivers/tty/tty_io.c')
-rw-r--r--drivers/tty/tty_io.c66
1 files changed, 39 insertions, 27 deletions
diff --git a/drivers/tty/tty_io.c b/drivers/tty/tty_io.c
index 6784aae210e3..690224483fab 100644
--- a/drivers/tty/tty_io.c
+++ b/drivers/tty/tty_io.c
@@ -187,6 +187,7 @@ void free_tty_struct(struct tty_struct *tty)
187 put_device(tty->dev); 187 put_device(tty->dev);
188 kfree(tty->write_buf); 188 kfree(tty->write_buf);
189 tty_buffer_free_all(tty); 189 tty_buffer_free_all(tty);
190 tty->magic = 0xDEADDEAD;
190 kfree(tty); 191 kfree(tty);
191} 192}
192 193
@@ -575,7 +576,7 @@ void __tty_hangup(struct tty_struct *tty)
575 } 576 }
576 spin_unlock(&redirect_lock); 577 spin_unlock(&redirect_lock);
577 578
578 tty_lock(); 579 tty_lock(tty);
579 580
580 /* some functions below drop BTM, so we need this bit */ 581 /* some functions below drop BTM, so we need this bit */
581 set_bit(TTY_HUPPING, &tty->flags); 582 set_bit(TTY_HUPPING, &tty->flags);
@@ -668,7 +669,7 @@ void __tty_hangup(struct tty_struct *tty)
668 clear_bit(TTY_HUPPING, &tty->flags); 669 clear_bit(TTY_HUPPING, &tty->flags);
669 tty_ldisc_enable(tty); 670 tty_ldisc_enable(tty);
670 671
671 tty_unlock(); 672 tty_unlock(tty);
672 673
673 if (f) 674 if (f)
674 fput(f); 675 fput(f);
@@ -1105,12 +1106,12 @@ void tty_write_message(struct tty_struct *tty, char *msg)
1105{ 1106{
1106 if (tty) { 1107 if (tty) {
1107 mutex_lock(&tty->atomic_write_lock); 1108 mutex_lock(&tty->atomic_write_lock);
1108 tty_lock(); 1109 tty_lock(tty);
1109 if (tty->ops->write && !test_bit(TTY_CLOSING, &tty->flags)) { 1110 if (tty->ops->write && !test_bit(TTY_CLOSING, &tty->flags)) {
1110 tty_unlock(); 1111 tty_unlock(tty);
1111 tty->ops->write(tty, msg, strlen(msg)); 1112 tty->ops->write(tty, msg, strlen(msg));
1112 } else 1113 } else
1113 tty_unlock(); 1114 tty_unlock(tty);
1114 tty_write_unlock(tty); 1115 tty_write_unlock(tty);
1115 } 1116 }
1116 return; 1117 return;
@@ -1403,6 +1404,7 @@ struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx)
1403 } 1404 }
1404 initialize_tty_struct(tty, driver, idx); 1405 initialize_tty_struct(tty, driver, idx);
1405 1406
1407 tty_lock(tty);
1406 retval = tty_driver_install_tty(driver, tty); 1408 retval = tty_driver_install_tty(driver, tty);
1407 if (retval < 0) 1409 if (retval < 0)
1408 goto err_deinit_tty; 1410 goto err_deinit_tty;
@@ -1418,9 +1420,11 @@ struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx)
1418 retval = tty_ldisc_setup(tty, tty->link); 1420 retval = tty_ldisc_setup(tty, tty->link);
1419 if (retval) 1421 if (retval)
1420 goto err_release_tty; 1422 goto err_release_tty;
1423 /* Return the tty locked so that it cannot vanish under the caller */
1421 return tty; 1424 return tty;
1422 1425
1423err_deinit_tty: 1426err_deinit_tty:
1427 tty_unlock(tty);
1424 deinitialize_tty_struct(tty); 1428 deinitialize_tty_struct(tty);
1425 free_tty_struct(tty); 1429 free_tty_struct(tty);
1426err_module_put: 1430err_module_put:
@@ -1429,6 +1433,7 @@ err_module_put:
1429 1433
1430 /* call the tty release_tty routine to clean out this slot */ 1434 /* call the tty release_tty routine to clean out this slot */
1431err_release_tty: 1435err_release_tty:
1436 tty_unlock(tty);
1432 printk_ratelimited(KERN_INFO "tty_init_dev: ldisc open failed, " 1437 printk_ratelimited(KERN_INFO "tty_init_dev: ldisc open failed, "
1433 "clearing slot %d\n", idx); 1438 "clearing slot %d\n", idx);
1434 release_tty(tty, idx); 1439 release_tty(tty, idx);
@@ -1622,7 +1627,7 @@ int tty_release(struct inode *inode, struct file *filp)
1622 if (tty_paranoia_check(tty, inode, __func__)) 1627 if (tty_paranoia_check(tty, inode, __func__))
1623 return 0; 1628 return 0;
1624 1629
1625 tty_lock(); 1630 tty_lock(tty);
1626 check_tty_count(tty, __func__); 1631 check_tty_count(tty, __func__);
1627 1632
1628 __tty_fasync(-1, filp, 0); 1633 __tty_fasync(-1, filp, 0);
@@ -1631,10 +1636,11 @@ int tty_release(struct inode *inode, struct file *filp)
1631 pty_master = (tty->driver->type == TTY_DRIVER_TYPE_PTY && 1636 pty_master = (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1632 tty->driver->subtype == PTY_TYPE_MASTER); 1637 tty->driver->subtype == PTY_TYPE_MASTER);
1633 devpts = (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) != 0; 1638 devpts = (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) != 0;
1639 /* Review: parallel close */
1634 o_tty = tty->link; 1640 o_tty = tty->link;
1635 1641
1636 if (tty_release_checks(tty, o_tty, idx)) { 1642 if (tty_release_checks(tty, o_tty, idx)) {
1637 tty_unlock(); 1643 tty_unlock(tty);
1638 return 0; 1644 return 0;
1639 } 1645 }
1640 1646
@@ -1646,7 +1652,7 @@ int tty_release(struct inode *inode, struct file *filp)
1646 if (tty->ops->close) 1652 if (tty->ops->close)
1647 tty->ops->close(tty, filp); 1653 tty->ops->close(tty, filp);
1648 1654
1649 tty_unlock(); 1655 tty_unlock(tty);
1650 /* 1656 /*
1651 * Sanity check: if tty->count is going to zero, there shouldn't be 1657 * Sanity check: if tty->count is going to zero, there shouldn't be
1652 * any waiters on tty->read_wait or tty->write_wait. We test the 1658 * any waiters on tty->read_wait or tty->write_wait. We test the
@@ -1669,7 +1675,7 @@ int tty_release(struct inode *inode, struct file *filp)
1669 opens on /dev/tty */ 1675 opens on /dev/tty */
1670 1676
1671 mutex_lock(&tty_mutex); 1677 mutex_lock(&tty_mutex);
1672 tty_lock(); 1678 tty_lock_pair(tty, o_tty);
1673 tty_closing = tty->count <= 1; 1679 tty_closing = tty->count <= 1;
1674 o_tty_closing = o_tty && 1680 o_tty_closing = o_tty &&
1675 (o_tty->count <= (pty_master ? 1 : 0)); 1681 (o_tty->count <= (pty_master ? 1 : 0));
@@ -1700,7 +1706,7 @@ int tty_release(struct inode *inode, struct file *filp)
1700 1706
1701 printk(KERN_WARNING "%s: %s: read/write wait queue active!\n", 1707 printk(KERN_WARNING "%s: %s: read/write wait queue active!\n",
1702 __func__, tty_name(tty, buf)); 1708 __func__, tty_name(tty, buf));
1703 tty_unlock(); 1709 tty_unlock_pair(tty, o_tty);
1704 mutex_unlock(&tty_mutex); 1710 mutex_unlock(&tty_mutex);
1705 schedule(); 1711 schedule();
1706 } 1712 }
@@ -1763,7 +1769,7 @@ int tty_release(struct inode *inode, struct file *filp)
1763 } 1769 }
1764 1770
1765 mutex_unlock(&tty_mutex); 1771 mutex_unlock(&tty_mutex);
1766 tty_unlock(); 1772 tty_unlock_pair(tty, o_tty);
1767 /* At this point the TTY_CLOSING flag should ensure a dead tty 1773 /* At this point the TTY_CLOSING flag should ensure a dead tty
1768 cannot be re-opened by a racing opener */ 1774 cannot be re-opened by a racing opener */
1769 1775
@@ -1780,7 +1786,9 @@ int tty_release(struct inode *inode, struct file *filp)
1780 tty_ldisc_release(tty, o_tty); 1786 tty_ldisc_release(tty, o_tty);
1781 /* 1787 /*
1782 * The release_tty function takes care of the details of clearing 1788 * The release_tty function takes care of the details of clearing
1783 * the slots and preserving the termios structure. 1789 * the slots and preserving the termios structure. The tty_unlock_pair
1790 * should be safe as we keep a kref while the tty is locked (so the
1791 * unlock never unlocks a freed tty).
1784 */ 1792 */
1785 mutex_lock(&tty_mutex); 1793 mutex_lock(&tty_mutex);
1786 release_tty(tty, idx); 1794 release_tty(tty, idx);
@@ -1789,7 +1797,6 @@ int tty_release(struct inode *inode, struct file *filp)
1789 /* Make this pty number available for reallocation */ 1797 /* Make this pty number available for reallocation */
1790 if (devpts) 1798 if (devpts)
1791 devpts_kill_index(inode, idx); 1799 devpts_kill_index(inode, idx);
1792
1793 return 0; 1800 return 0;
1794} 1801}
1795 1802
@@ -1893,6 +1900,9 @@ static struct tty_driver *tty_lookup_driver(dev_t device, struct file *filp,
1893 * Locking: tty_mutex protects tty, tty_lookup_driver and tty_init_dev. 1900 * Locking: tty_mutex protects tty, tty_lookup_driver and tty_init_dev.
1894 * tty->count should protect the rest. 1901 * tty->count should protect the rest.
1895 * ->siglock protects ->signal/->sighand 1902 * ->siglock protects ->signal/->sighand
1903 *
1904 * Note: the tty_unlock/lock cases without a ref are only safe due to
1905 * tty_mutex
1896 */ 1906 */
1897 1907
1898static int tty_open(struct inode *inode, struct file *filp) 1908static int tty_open(struct inode *inode, struct file *filp)
@@ -1916,8 +1926,7 @@ retry_open:
1916 retval = 0; 1926 retval = 0;
1917 1927
1918 mutex_lock(&tty_mutex); 1928 mutex_lock(&tty_mutex);
1919 tty_lock(); 1929 /* This is protected by the tty_mutex */
1920
1921 tty = tty_open_current_tty(device, filp); 1930 tty = tty_open_current_tty(device, filp);
1922 if (IS_ERR(tty)) { 1931 if (IS_ERR(tty)) {
1923 retval = PTR_ERR(tty); 1932 retval = PTR_ERR(tty);
@@ -1938,17 +1947,19 @@ retry_open:
1938 } 1947 }
1939 1948
1940 if (tty) { 1949 if (tty) {
1950 tty_lock(tty);
1941 retval = tty_reopen(tty); 1951 retval = tty_reopen(tty);
1942 if (retval) 1952 if (retval < 0) {
1953 tty_unlock(tty);
1943 tty = ERR_PTR(retval); 1954 tty = ERR_PTR(retval);
1944 } else 1955 }
1956 } else /* Returns with the tty_lock held for now */
1945 tty = tty_init_dev(driver, index); 1957 tty = tty_init_dev(driver, index);
1946 1958
1947 mutex_unlock(&tty_mutex); 1959 mutex_unlock(&tty_mutex);
1948 if (driver) 1960 if (driver)
1949 tty_driver_kref_put(driver); 1961 tty_driver_kref_put(driver);
1950 if (IS_ERR(tty)) { 1962 if (IS_ERR(tty)) {
1951 tty_unlock();
1952 retval = PTR_ERR(tty); 1963 retval = PTR_ERR(tty);
1953 goto err_file; 1964 goto err_file;
1954 } 1965 }
@@ -1977,7 +1988,7 @@ retry_open:
1977 printk(KERN_DEBUG "%s: error %d in opening %s...\n", __func__, 1988 printk(KERN_DEBUG "%s: error %d in opening %s...\n", __func__,
1978 retval, tty->name); 1989 retval, tty->name);
1979#endif 1990#endif
1980 tty_unlock(); /* need to call tty_release without BTM */ 1991 tty_unlock(tty); /* need to call tty_release without BTM */
1981 tty_release(inode, filp); 1992 tty_release(inode, filp);
1982 if (retval != -ERESTARTSYS) 1993 if (retval != -ERESTARTSYS)
1983 return retval; 1994 return retval;
@@ -1989,17 +2000,15 @@ retry_open:
1989 /* 2000 /*
1990 * Need to reset f_op in case a hangup happened. 2001 * Need to reset f_op in case a hangup happened.
1991 */ 2002 */
1992 tty_lock();
1993 if (filp->f_op == &hung_up_tty_fops) 2003 if (filp->f_op == &hung_up_tty_fops)
1994 filp->f_op = &tty_fops; 2004 filp->f_op = &tty_fops;
1995 tty_unlock();
1996 goto retry_open; 2005 goto retry_open;
1997 } 2006 }
1998 tty_unlock(); 2007 tty_unlock(tty);
1999 2008
2000 2009
2001 mutex_lock(&tty_mutex); 2010 mutex_lock(&tty_mutex);
2002 tty_lock(); 2011 tty_lock(tty);
2003 spin_lock_irq(&current->sighand->siglock); 2012 spin_lock_irq(&current->sighand->siglock);
2004 if (!noctty && 2013 if (!noctty &&
2005 current->signal->leader && 2014 current->signal->leader &&
@@ -2007,11 +2016,10 @@ retry_open:
2007 tty->session == NULL) 2016 tty->session == NULL)
2008 __proc_set_tty(current, tty); 2017 __proc_set_tty(current, tty);
2009 spin_unlock_irq(&current->sighand->siglock); 2018 spin_unlock_irq(&current->sighand->siglock);
2010 tty_unlock(); 2019 tty_unlock(tty);
2011 mutex_unlock(&tty_mutex); 2020 mutex_unlock(&tty_mutex);
2012 return 0; 2021 return 0;
2013err_unlock: 2022err_unlock:
2014 tty_unlock();
2015 mutex_unlock(&tty_mutex); 2023 mutex_unlock(&tty_mutex);
2016 /* after locks to avoid deadlock */ 2024 /* after locks to avoid deadlock */
2017 if (!IS_ERR_OR_NULL(driver)) 2025 if (!IS_ERR_OR_NULL(driver))
@@ -2094,10 +2102,13 @@ out:
2094 2102
2095static int tty_fasync(int fd, struct file *filp, int on) 2103static int tty_fasync(int fd, struct file *filp, int on)
2096{ 2104{
2105 struct tty_struct *tty = file_tty(filp);
2097 int retval; 2106 int retval;
2098 tty_lock(); 2107
2108 tty_lock(tty);
2099 retval = __tty_fasync(fd, filp, on); 2109 retval = __tty_fasync(fd, filp, on);
2100 tty_unlock(); 2110 tty_unlock(tty);
2111
2101 return retval; 2112 return retval;
2102} 2113}
2103 2114
@@ -2934,6 +2945,7 @@ void initialize_tty_struct(struct tty_struct *tty,
2934 tty->pgrp = NULL; 2945 tty->pgrp = NULL;
2935 tty->overrun_time = jiffies; 2946 tty->overrun_time = jiffies;
2936 tty_buffer_init(tty); 2947 tty_buffer_init(tty);
2948 mutex_init(&tty->legacy_mutex);
2937 mutex_init(&tty->termios_mutex); 2949 mutex_init(&tty->termios_mutex);
2938 mutex_init(&tty->ldisc_mutex); 2950 mutex_init(&tty->ldisc_mutex);
2939 init_waitqueue_head(&tty->write_wait); 2951 init_waitqueue_head(&tty->write_wait);