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

tty: replace BKL with a new tty_lock

As a preparation for replacing the big kernel lock
in the TTY layer, wrap all the callers in new
macros tty_lock, tty_lock_nested and tty_unlock.

Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Cc: Alan Cox <alan@lxorguk.ukuu.org.uk>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>

authored by

Arnd Bergmann and committed by
Greg Kroah-Hartman
ec79d605 3f582b8c

+161 -115
+8 -8
drivers/char/amiserial.c
··· 1072 1072 if (!retinfo) 1073 1073 return -EFAULT; 1074 1074 memset(&tmp, 0, sizeof(tmp)); 1075 - lock_kernel(); 1075 + tty_lock(); 1076 1076 tmp.type = state->type; 1077 1077 tmp.line = state->line; 1078 1078 tmp.port = state->port; ··· 1083 1083 tmp.close_delay = state->close_delay; 1084 1084 tmp.closing_wait = state->closing_wait; 1085 1085 tmp.custom_divisor = state->custom_divisor; 1086 - unlock_kernel(); 1086 + tty_unlock(); 1087 1087 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo))) 1088 1088 return -EFAULT; 1089 1089 return 0; ··· 1100 1100 if (copy_from_user(&new_serial,new_info,sizeof(new_serial))) 1101 1101 return -EFAULT; 1102 1102 1103 - lock_kernel(); 1103 + tty_lock(); 1104 1104 state = info->state; 1105 1105 old_state = *state; 1106 1106 1107 1107 change_irq = new_serial.irq != state->irq; 1108 1108 change_port = (new_serial.port != state->port); 1109 1109 if(change_irq || change_port || (new_serial.xmit_fifo_size != state->xmit_fifo_size)) { 1110 - unlock_kernel(); 1110 + tty_unlock(); 1111 1111 return -EINVAL; 1112 1112 } 1113 1113 ··· 1127 1127 } 1128 1128 1129 1129 if (new_serial.baud_base < 9600) { 1130 - unlock_kernel(); 1130 + tty_unlock(); 1131 1131 return -EINVAL; 1132 1132 } 1133 1133 ··· 1163 1163 } 1164 1164 } else 1165 1165 retval = startup(info); 1166 - unlock_kernel(); 1166 + tty_unlock(); 1167 1167 return retval; 1168 1168 } 1169 1169 ··· 1538 1538 1539 1539 orig_jiffies = jiffies; 1540 1540 1541 - lock_kernel(); 1541 + tty_lock_nested(); /* tty_wait_until_sent is called from lots of places */ 1542 1542 /* 1543 1543 * Set the check interval to be 1/5 of the estimated time to 1544 1544 * send a single character, and make it at least 1. The check ··· 1579 1579 break; 1580 1580 } 1581 1581 __set_current_state(TASK_RUNNING); 1582 - unlock_kernel(); 1582 + tty_unlock(); 1583 1583 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT 1584 1584 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies); 1585 1585 #endif
+3 -3
drivers/char/briq_panel.c
··· 67 67 68 68 static int briq_panel_open(struct inode *ino, struct file *filep) 69 69 { 70 - lock_kernel(); 70 + tty_lock(); 71 71 /* enforce single access, vfd_is_open is protected by BKL */ 72 72 if (vfd_is_open) { 73 - unlock_kernel(); 73 + tty_unlock(); 74 74 return -EBUSY; 75 75 } 76 76 vfd_is_open = 1; 77 77 78 - unlock_kernel(); 78 + tty_unlock(); 79 79 return 0; 80 80 } 81 81
+8 -8
drivers/char/n_hdlc.c
··· 598 598 return -EFAULT; 599 599 } 600 600 601 - lock_kernel(); 601 + tty_lock(); 602 602 603 603 for (;;) { 604 604 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) { 605 - unlock_kernel(); 605 + tty_unlock(); 606 606 return -EIO; 607 607 } 608 608 609 609 n_hdlc = tty2n_hdlc (tty); 610 610 if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC || 611 611 tty != n_hdlc->tty) { 612 - unlock_kernel(); 612 + tty_unlock(); 613 613 return 0; 614 614 } 615 615 ··· 619 619 620 620 /* no data */ 621 621 if (file->f_flags & O_NONBLOCK) { 622 - unlock_kernel(); 622 + tty_unlock(); 623 623 return -EAGAIN; 624 624 } 625 625 626 626 interruptible_sleep_on (&tty->read_wait); 627 627 if (signal_pending(current)) { 628 - unlock_kernel(); 628 + tty_unlock(); 629 629 return -EINTR; 630 630 } 631 631 } ··· 648 648 kfree(rbuf); 649 649 else 650 650 n_hdlc_buf_put(&n_hdlc->rx_free_buf_list,rbuf); 651 - unlock_kernel(); 651 + tty_unlock(); 652 652 return ret; 653 653 654 654 } /* end of n_hdlc_tty_read() */ ··· 691 691 count = maxframe; 692 692 } 693 693 694 - lock_kernel(); 694 + tty_lock(); 695 695 696 696 add_wait_queue(&tty->write_wait, &wait); 697 697 set_current_state(TASK_INTERRUPTIBLE); ··· 731 731 n_hdlc_buf_put(&n_hdlc->tx_buf_list,tbuf); 732 732 n_hdlc_send_frames(n_hdlc,tty); 733 733 } 734 - unlock_kernel(); 734 + tty_unlock(); 735 735 return error; 736 736 737 737 } /* end of n_hdlc_tty_write() */
+4 -4
drivers/char/n_r3964.c
··· 1067 1067 1068 1068 TRACE_L("read()"); 1069 1069 1070 - lock_kernel(); 1070 + tty_lock(); 1071 1071 1072 1072 pClient = findClient(pInfo, task_pid(current)); 1073 1073 if (pClient) { ··· 1109 1109 } 1110 1110 ret = -EPERM; 1111 1111 unlock: 1112 - unlock_kernel(); 1112 + tty_unlock(); 1113 1113 return ret; 1114 1114 } 1115 1115 ··· 1158 1158 pHeader->locks = 0; 1159 1159 pHeader->owner = NULL; 1160 1160 1161 - lock_kernel(); 1161 + tty_lock(); 1162 1162 1163 1163 pClient = findClient(pInfo, task_pid(current)); 1164 1164 if (pClient) { ··· 1177 1177 add_tx_queue(pInfo, pHeader); 1178 1178 trigger_transmit(pInfo); 1179 1179 1180 - unlock_kernel(); 1180 + tty_unlock(); 1181 1181 1182 1182 return 0; 1183 1183 }
+2 -2
drivers/char/pty.c
··· 692 692 { 693 693 int ret; 694 694 695 - lock_kernel(); 695 + tty_lock(); 696 696 ret = __ptmx_open(inode, filp); 697 - unlock_kernel(); 697 + tty_unlock(); 698 698 return ret; 699 699 } 700 700
+2 -2
drivers/char/selection.c
··· 313 313 struct tty_ldisc *ld; 314 314 DECLARE_WAITQUEUE(wait, current); 315 315 316 - lock_kernel(); 316 + tty_lock_nested(); /* always called with BTM from vt_ioctl */ 317 317 318 318 acquire_console_sem(); 319 319 poke_blanked_console(); ··· 338 338 __set_current_state(TASK_RUNNING); 339 339 340 340 tty_ldisc_deref(ld); 341 - unlock_kernel(); 341 + tty_unlock(); 342 342 return 0; 343 343 }
+2 -2
drivers/char/serial167.c
··· 1505 1505 printk("cy_ioctl %s, cmd = %x arg = %lx\n", tty->name, cmd, arg); /* */ 1506 1506 #endif 1507 1507 1508 - lock_kernel(); 1508 + tty_lock(); 1509 1509 1510 1510 switch (cmd) { 1511 1511 case CYGETMON: ··· 1561 1561 default: 1562 1562 ret_val = -ENOIOCTLCMD; 1563 1563 } 1564 - unlock_kernel(); 1564 + tty_unlock(); 1565 1565 1566 1566 #ifdef SERIAL_DEBUG_OTHER 1567 1567 printk("cy_ioctl done\n");
+6 -6
drivers/char/sx.c
··· 1699 1699 if (!capable(CAP_SYS_RAWIO)) 1700 1700 return -EPERM; 1701 1701 1702 - lock_kernel(); 1702 + tty_lock(); 1703 1703 1704 1704 sx_dprintk(SX_DEBUG_FIRMWARE, "IOCTL %x: %lx\n", cmd, arg); 1705 1705 ··· 1848 1848 break; 1849 1849 } 1850 1850 out: 1851 - unlock_kernel(); 1851 + tty_unlock(); 1852 1852 func_exit(); 1853 1853 return rc; 1854 1854 } ··· 1859 1859 int rv; 1860 1860 1861 1861 func_enter(); 1862 - lock_kernel(); 1862 + tty_lock(); 1863 1863 1864 1864 if (flag) 1865 1865 rv = sx_send_command(port, HS_START, -1, HS_IDLE_BREAK); ··· 1868 1868 if (rv != 1) 1869 1869 printk(KERN_ERR "sx: couldn't send break (%x).\n", 1870 1870 read_sx_byte(port->board, CHAN_OFFSET(port, hi_hstat))); 1871 - unlock_kernel(); 1871 + tty_unlock(); 1872 1872 func_exit(); 1873 1873 return 0; 1874 1874 } ··· 1909 1909 /* func_enter2(); */ 1910 1910 1911 1911 rc = 0; 1912 - lock_kernel(); 1912 + tty_lock(); 1913 1913 switch (cmd) { 1914 1914 case TIOCGSERIAL: 1915 1915 rc = gs_getserial(&port->gs, argp); ··· 1921 1921 rc = -ENOIOCTLCMD; 1922 1922 break; 1923 1923 } 1924 - unlock_kernel(); 1924 + tty_unlock(); 1925 1925 1926 1926 /* func_exit(); */ 1927 1927 return rc;
+64 -51
drivers/char/tty_io.c
··· 149 149 #else 150 150 #define tty_compat_ioctl NULL 151 151 #endif 152 + static int __tty_fasync(int fd, struct file *filp, int on); 152 153 static int tty_fasync(int fd, struct file *filp, int on); 153 154 static void release_tty(struct tty_struct *tty, int idx); 154 155 static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty); ··· 484 483 * remains intact. 485 484 * 486 485 * Locking: 487 - * BKL 486 + * BTM 488 487 * redirect lock for undoing redirection 489 488 * file list lock for manipulating list of ttys 490 489 * tty_ldisc_lock from called functions ··· 514 513 } 515 514 spin_unlock(&redirect_lock); 516 515 517 - /* inuse_filps is protected by the single kernel lock */ 518 - lock_kernel(); 516 + /* inuse_filps is protected by the single tty lock, 517 + this really needs to change if we want to flush the 518 + workqueue with the lock held */ 519 + tty_lock_nested(); /* called with BTM held from pty_close and 520 + others */ 519 521 check_tty_count(tty, "do_tty_hangup"); 520 522 521 523 file_list_lock(); ··· 529 525 if (filp->f_op->write != tty_write) 530 526 continue; 531 527 closecount++; 532 - tty_fasync(-1, filp, 0); /* can't block */ 528 + __tty_fasync(-1, filp, 0); /* can't block */ 533 529 filp->f_op = &hung_up_tty_fops; 534 530 } 535 531 file_list_unlock(); ··· 598 594 */ 599 595 set_bit(TTY_HUPPED, &tty->flags); 600 596 tty_ldisc_enable(tty); 601 - unlock_kernel(); 597 + tty_unlock(); 602 598 if (f) 603 599 fput(f); 604 600 } ··· 700 696 * exiting; it is 0 if called by the ioctl TIOCNOTTY. 701 697 * 702 698 * Locking: 703 - * BKL is taken for hysterical raisins 699 + * BTM is taken for hysterical raisins, and held when 700 + * called from no_tty(). 704 701 * tty_mutex is taken to protect tty 705 702 * ->siglock is taken to protect ->signal/->sighand 706 703 * tasklist_lock is taken to walk process list for sessions ··· 719 714 tty = get_current_tty(); 720 715 if (tty) { 721 716 tty_pgrp = get_pid(tty->pgrp); 722 - lock_kernel(); 717 + tty_lock_nested(); /* see above */ 723 718 if (on_exit && tty->driver->type != TTY_DRIVER_TYPE_PTY) 724 719 tty_vhangup(tty); 725 - unlock_kernel(); 720 + tty_unlock(); 726 721 tty_kref_put(tty); 727 722 } else if (on_exit) { 728 723 struct pid *old_pgrp; ··· 779 774 void no_tty(void) 780 775 { 781 776 struct task_struct *tsk = current; 782 - lock_kernel(); 777 + tty_lock(); 783 778 disassociate_ctty(0); 784 - unlock_kernel(); 779 + tty_unlock(); 785 780 proc_clear_tty(tsk); 786 781 } 787 782 ··· 1018 1013 * We don't put it into the syslog queue right now maybe in the future if 1019 1014 * really needed. 1020 1015 * 1021 - * We must still hold the BKL and test the CLOSING flag for the moment. 1016 + * We must still hold the BTM and test the CLOSING flag for the moment. 1022 1017 */ 1023 1018 1024 1019 void tty_write_message(struct tty_struct *tty, char *msg) 1025 1020 { 1026 1021 if (tty) { 1027 1022 mutex_lock(&tty->atomic_write_lock); 1028 - lock_kernel(); 1023 + tty_lock(); 1029 1024 if (tty->ops->write && !test_bit(TTY_CLOSING, &tty->flags)) { 1030 - unlock_kernel(); 1025 + tty_unlock(); 1031 1026 tty->ops->write(tty, msg, strlen(msg)); 1032 1027 } else 1033 - unlock_kernel(); 1028 + tty_unlock(); 1034 1029 tty_write_unlock(tty); 1035 1030 } 1036 1031 return; ··· 1213 1208 int ret; 1214 1209 1215 1210 if (driver->ops->install) { 1216 - lock_kernel(); 1211 + tty_lock_nested(); /* already called with BTM held */ 1217 1212 ret = driver->ops->install(driver, tty); 1218 - unlock_kernel(); 1213 + tty_unlock(); 1219 1214 return ret; 1220 1215 } 1221 1216 1222 1217 if (tty_init_termios(tty) == 0) { 1223 - lock_kernel(); 1218 + tty_lock_nested(); 1224 1219 tty_driver_kref_get(driver); 1225 1220 tty->count++; 1226 1221 driver->ttys[idx] = tty; 1227 - unlock_kernel(); 1222 + tty_unlock(); 1228 1223 return 0; 1229 1224 } 1230 1225 return -ENOMEM; ··· 1317 1312 struct tty_struct *tty; 1318 1313 int retval; 1319 1314 1320 - lock_kernel(); 1315 + tty_lock_nested(); /* always called with tty lock held already */ 1316 + 1321 1317 /* Check if pty master is being opened multiple times */ 1322 1318 if (driver->subtype == PTY_TYPE_MASTER && 1323 1319 (driver->flags & TTY_DRIVER_DEVPTS_MEM) && !first_ok) { 1324 - unlock_kernel(); 1320 + tty_unlock(); 1325 1321 return ERR_PTR(-EIO); 1326 1322 } 1327 - unlock_kernel(); 1323 + tty_unlock(); 1328 1324 1329 1325 /* 1330 1326 * First time open is complex, especially for PTY devices. ··· 1369 1363 if (printk_ratelimit()) 1370 1364 printk(KERN_INFO "tty_init_dev: ldisc open failed, " 1371 1365 "clearing slot %d\n", idx); 1372 - lock_kernel(); 1366 + tty_lock_nested(); 1373 1367 release_tty(tty, idx); 1374 - unlock_kernel(); 1368 + tty_unlock(); 1375 1369 return ERR_PTR(retval); 1376 1370 } 1377 1371 ··· 1518 1512 if (tty_paranoia_check(tty, inode, "tty_release_dev")) 1519 1513 return 0; 1520 1514 1521 - lock_kernel(); 1515 + tty_lock(); 1522 1516 check_tty_count(tty, "tty_release_dev"); 1523 1517 1524 - tty_fasync(-1, filp, 0); 1518 + __tty_fasync(-1, filp, 0); 1525 1519 1526 1520 idx = tty->index; 1527 1521 pty_master = (tty->driver->type == TTY_DRIVER_TYPE_PTY && ··· 1533 1527 if (idx < 0 || idx >= tty->driver->num) { 1534 1528 printk(KERN_DEBUG "tty_release_dev: bad idx when trying to " 1535 1529 "free (%s)\n", tty->name); 1536 - unlock_kernel(); 1530 + tty_unlock(); 1537 1531 return 0; 1538 1532 } 1539 1533 if (!devpts) { 1540 1534 if (tty != tty->driver->ttys[idx]) { 1541 - unlock_kernel(); 1535 + tty_unlock(); 1542 1536 printk(KERN_DEBUG "tty_release_dev: driver.table[%d] not tty " 1543 1537 "for (%s)\n", idx, tty->name); 1544 1538 return 0; 1545 1539 } 1546 1540 if (tty->termios != tty->driver->termios[idx]) { 1547 - unlock_kernel(); 1541 + tty_unlock(); 1548 1542 printk(KERN_DEBUG "tty_release_dev: driver.termios[%d] not termios " 1549 1543 "for (%s)\n", 1550 1544 idx, tty->name); ··· 1562 1556 if (tty->driver->other && 1563 1557 !(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) { 1564 1558 if (o_tty != tty->driver->other->ttys[idx]) { 1565 - unlock_kernel(); 1559 + tty_unlock(); 1566 1560 printk(KERN_DEBUG "tty_release_dev: other->table[%d] " 1567 1561 "not o_tty for (%s)\n", 1568 1562 idx, tty->name); 1569 1563 return 0 ; 1570 1564 } 1571 1565 if (o_tty->termios != tty->driver->other->termios[idx]) { 1572 - unlock_kernel(); 1566 + tty_unlock(); 1573 1567 printk(KERN_DEBUG "tty_release_dev: other->termios[%d] " 1574 1568 "not o_termios for (%s)\n", 1575 1569 idx, tty->name); 1576 1570 return 0; 1577 1571 } 1578 1572 if (o_tty->link != tty) { 1579 - unlock_kernel(); 1573 + tty_unlock(); 1580 1574 printk(KERN_DEBUG "tty_release_dev: bad pty pointers\n"); 1581 1575 return 0; 1582 1576 } ··· 1585 1579 if (tty->ops->close) 1586 1580 tty->ops->close(tty, filp); 1587 1581 1588 - unlock_kernel(); 1582 + tty_unlock(); 1589 1583 /* 1590 1584 * Sanity check: if tty->count is going to zero, there shouldn't be 1591 1585 * any waiters on tty->read_wait or tty->write_wait. We test the ··· 1608 1602 opens on /dev/tty */ 1609 1603 1610 1604 mutex_lock(&tty_mutex); 1611 - lock_kernel(); 1605 + tty_lock(); 1612 1606 tty_closing = tty->count <= 1; 1613 1607 o_tty_closing = o_tty && 1614 1608 (o_tty->count <= (pty_master ? 1 : 0)); ··· 1639 1633 1640 1634 printk(KERN_WARNING "tty_release_dev: %s: read/write wait queue " 1641 1635 "active!\n", tty_name(tty, buf)); 1642 - unlock_kernel(); 1636 + tty_unlock(); 1643 1637 mutex_unlock(&tty_mutex); 1644 1638 schedule(); 1645 1639 } ··· 1704 1698 1705 1699 /* check whether both sides are closing ... */ 1706 1700 if (!tty_closing || (o_tty && !o_tty_closing)) { 1707 - unlock_kernel(); 1701 + tty_unlock(); 1708 1702 return 0; 1709 1703 } 1710 1704 ··· 1724 1718 /* Make this pty number available for reallocation */ 1725 1719 if (devpts) 1726 1720 devpts_kill_index(inode, idx); 1727 - unlock_kernel(); 1721 + tty_unlock(); 1728 1722 return 0; 1729 1723 } 1730 1724 ··· 1766 1760 retval = 0; 1767 1761 1768 1762 mutex_lock(&tty_mutex); 1769 - lock_kernel(); 1763 + tty_lock(); 1770 1764 1771 1765 if (device == MKDEV(TTYAUX_MAJOR, 0)) { 1772 1766 tty = get_current_tty(); 1773 1767 if (!tty) { 1774 - unlock_kernel(); 1768 + tty_unlock(); 1775 1769 mutex_unlock(&tty_mutex); 1776 1770 return -ENXIO; 1777 1771 } ··· 1803 1797 goto got_driver; 1804 1798 } 1805 1799 } 1806 - unlock_kernel(); 1800 + tty_unlock(); 1807 1801 mutex_unlock(&tty_mutex); 1808 1802 return -ENODEV; 1809 1803 } 1810 1804 1811 1805 driver = get_tty_driver(device, &index); 1812 1806 if (!driver) { 1813 - unlock_kernel(); 1807 + tty_unlock(); 1814 1808 mutex_unlock(&tty_mutex); 1815 1809 return -ENODEV; 1816 1810 } ··· 1820 1814 tty = tty_driver_lookup_tty(driver, inode, index); 1821 1815 1822 1816 if (IS_ERR(tty)) { 1823 - unlock_kernel(); 1817 + tty_unlock(); 1824 1818 mutex_unlock(&tty_mutex); 1825 1819 return PTR_ERR(tty); 1826 1820 } ··· 1836 1830 mutex_unlock(&tty_mutex); 1837 1831 tty_driver_kref_put(driver); 1838 1832 if (IS_ERR(tty)) { 1839 - unlock_kernel(); 1833 + tty_unlock(); 1840 1834 return PTR_ERR(tty); 1841 1835 } 1842 1836 ··· 1868 1862 #endif 1869 1863 tty_release(inode, filp); 1870 1864 if (retval != -ERESTARTSYS) { 1871 - unlock_kernel(); 1865 + tty_unlock(); 1872 1866 return retval; 1873 1867 } 1874 1868 if (signal_pending(current)) { 1875 - unlock_kernel(); 1869 + tty_unlock(); 1876 1870 return retval; 1877 1871 } 1878 1872 schedule(); ··· 1881 1875 */ 1882 1876 if (filp->f_op == &hung_up_tty_fops) 1883 1877 filp->f_op = &tty_fops; 1884 - unlock_kernel(); 1878 + tty_unlock(); 1885 1879 goto retry_open; 1886 1880 } 1887 - unlock_kernel(); 1881 + tty_unlock(); 1888 1882 1889 1883 1890 1884 mutex_lock(&tty_mutex); 1891 - lock_kernel(); 1885 + tty_lock(); 1892 1886 spin_lock_irq(&current->sighand->siglock); 1893 1887 if (!noctty && 1894 1888 current->signal->leader && ··· 1896 1890 tty->session == NULL) 1897 1891 __proc_set_tty(current, tty); 1898 1892 spin_unlock_irq(&current->sighand->siglock); 1899 - unlock_kernel(); 1893 + tty_unlock(); 1900 1894 mutex_unlock(&tty_mutex); 1901 1895 return 0; 1902 1896 } ··· 1932 1926 return ret; 1933 1927 } 1934 1928 1935 - static int tty_fasync(int fd, struct file *filp, int on) 1929 + static int __tty_fasync(int fd, struct file *filp, int on) 1936 1930 { 1937 1931 struct tty_struct *tty; 1938 1932 unsigned long flags; 1939 1933 int retval = 0; 1940 1934 1941 - lock_kernel(); 1942 1935 tty = (struct tty_struct *)filp->private_data; 1943 1936 if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_fasync")) 1944 1937 goto out; ··· 1971 1966 } 1972 1967 retval = 0; 1973 1968 out: 1974 - unlock_kernel(); 1969 + return retval; 1970 + } 1971 + 1972 + static int tty_fasync(int fd, struct file *filp, int on) 1973 + { 1974 + int retval; 1975 + tty_lock(); 1976 + retval = __tty_fasync(fd, filp, on); 1977 + tty_unlock(); 1975 1978 return retval; 1976 1979 } 1977 1980
+13 -11
drivers/char/tty_ldisc.c
··· 440 440 * 441 441 * A helper opening method. Also a convenient debugging and check 442 442 * point. 443 + * 444 + * Locking: always called with BTM already held. 443 445 */ 444 446 445 447 static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld) ··· 449 447 WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags)); 450 448 if (ld->ops->open) { 451 449 int ret; 452 - /* BKL here locks verus a hangup event */ 453 - lock_kernel(); 450 + /* BTM here locks versus a hangup event */ 451 + tty_lock_nested(); /* always held here already */ 454 452 ret = ld->ops->open(tty); 455 - unlock_kernel(); 453 + tty_unlock(); 456 454 return ret; 457 455 } 458 456 return 0; ··· 555 553 if (IS_ERR(new_ldisc)) 556 554 return PTR_ERR(new_ldisc); 557 555 558 - lock_kernel(); 556 + tty_lock(); 559 557 /* 560 558 * We need to look at the tty locking here for pty/tty pairs 561 559 * when both sides try to change in parallel. ··· 569 567 */ 570 568 571 569 if (tty->ldisc->ops->num == ldisc) { 572 - unlock_kernel(); 570 + tty_unlock(); 573 571 tty_ldisc_put(new_ldisc); 574 572 return 0; 575 573 } 576 574 577 - unlock_kernel(); 575 + tty_unlock(); 578 576 /* 579 577 * Problem: What do we do if this blocks ? 580 578 * We could deadlock here ··· 596 594 mutex_lock(&tty->ldisc_mutex); 597 595 } 598 596 599 - lock_kernel(); 597 + tty_lock(); 600 598 601 599 set_bit(TTY_LDISC_CHANGING, &tty->flags); 602 600 ··· 609 607 610 608 o_ldisc = tty->ldisc; 611 609 612 - unlock_kernel(); 610 + tty_unlock(); 613 611 /* 614 612 * Make sure we don't change while someone holds a 615 613 * reference to the line discipline. The TTY_LDISC bit ··· 635 633 flush_scheduled_work(); 636 634 637 635 mutex_lock(&tty->ldisc_mutex); 638 - lock_kernel(); 636 + tty_lock(); 639 637 if (test_bit(TTY_HUPPED, &tty->flags)) { 640 638 /* We were raced by the hangup method. It will have stomped 641 639 the ldisc data and closed the ldisc down */ 642 640 clear_bit(TTY_LDISC_CHANGING, &tty->flags); 643 641 mutex_unlock(&tty->ldisc_mutex); 644 642 tty_ldisc_put(new_ldisc); 645 - unlock_kernel(); 643 + tty_unlock(); 646 644 return -EIO; 647 645 } 648 646 ··· 684 682 if (o_work) 685 683 schedule_delayed_work(&o_tty->buf.work, 1); 686 684 mutex_unlock(&tty->ldisc_mutex); 687 - unlock_kernel(); 685 + tty_unlock(); 688 686 return retval; 689 687 } 690 688
+2 -2
drivers/char/vc_screen.c
··· 463 463 unsigned int currcons = iminor(inode) & 127; 464 464 int ret = 0; 465 465 466 - lock_kernel(); 466 + tty_lock(); 467 467 if(currcons && !vc_cons_allocated(currcons-1)) 468 468 ret = -ENXIO; 469 - unlock_kernel(); 469 + tty_unlock(); 470 470 return ret; 471 471 } 472 472
+5 -5
drivers/char/vt_ioctl.c
··· 509 509 510 510 console = vc->vc_num; 511 511 512 - lock_kernel(); 512 + tty_lock(); 513 513 514 514 if (!vc_cons_allocated(console)) { /* impossible? */ 515 515 ret = -ENOIOCTLCMD; ··· 1336 1336 ret = -ENOIOCTLCMD; 1337 1337 } 1338 1338 out: 1339 - unlock_kernel(); 1339 + tty_unlock(); 1340 1340 return ret; 1341 1341 eperm: 1342 1342 ret = -EPERM; ··· 1503 1503 1504 1504 console = vc->vc_num; 1505 1505 1506 - lock_kernel(); 1506 + tty_lock(); 1507 1507 1508 1508 if (!vc_cons_allocated(console)) { /* impossible? */ 1509 1509 ret = -ENOIOCTLCMD; ··· 1571 1571 goto fallback; 1572 1572 } 1573 1573 out: 1574 - unlock_kernel(); 1574 + tty_unlock(); 1575 1575 return ret; 1576 1576 1577 1577 fallback: 1578 - unlock_kernel(); 1578 + tty_unlock(); 1579 1579 return vt_ioctl(tty, file, cmd, arg); 1580 1580 } 1581 1581
+2 -2
drivers/serial/68360serial.c
··· 1705 1705 printk("jiff=%lu...", jiffies); 1706 1706 #endif 1707 1707 1708 - lock_kernel(); 1708 + tty_lock_nested(); /* always held already since we come from ->close */ 1709 1709 /* We go through the loop at least once because we can't tell 1710 1710 * exactly when the last character exits the shifter. There can 1711 1711 * be at least two characters waiting to be sent after the buffers ··· 1734 1734 bdp--; 1735 1735 } while (bdp->status & BD_SC_READY); 1736 1736 current->state = TASK_RUNNING; 1737 - unlock_kernel(); 1737 + tty_unlock(); 1738 1738 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT 1739 1739 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies); 1740 1740 #endif
+2 -2
drivers/serial/crisv10.c
··· 3935 3935 * Check R_DMA_CHx_STATUS bit 0-6=number of available bytes in FIFO 3936 3936 * R_DMA_CHx_HWSW bit 31-16=nbr of bytes left in DMA buffer (0=64k) 3937 3937 */ 3938 - lock_kernel(); 3938 + tty_lock_nested(); /* locked already when coming from close */ 3939 3939 orig_jiffies = jiffies; 3940 3940 while (info->xmit.head != info->xmit.tail || /* More in send queue */ 3941 3941 (*info->ostatusadr & 0x007f) || /* more in FIFO */ ··· 3952 3952 curr_time_usec - info->last_tx_active_usec; 3953 3953 } 3954 3954 set_current_state(TASK_RUNNING); 3955 - unlock_kernel(); 3955 + tty_unlock(); 3956 3956 } 3957 3957 3958 3958 /*
+5 -5
drivers/serial/serial_core.c
··· 1274 1274 struct uart_port *uport; 1275 1275 unsigned long flags; 1276 1276 1277 - BUG_ON(!kernel_locked()); 1277 + BUG_ON(!tty_locked()); 1278 1278 1279 1279 if (!state) 1280 1280 return; ··· 1382 1382 if (port->type == PORT_UNKNOWN || port->fifosize == 0) 1383 1383 return; 1384 1384 1385 - lock_kernel(); 1385 + tty_lock_nested(); /* already locked when coming from close */ 1386 1386 1387 1387 /* 1388 1388 * Set the check interval to be 1/5 of the estimated time to ··· 1429 1429 break; 1430 1430 } 1431 1431 set_current_state(TASK_RUNNING); /* might not be needed */ 1432 - unlock_kernel(); 1432 + tty_unlock(); 1433 1433 } 1434 1434 1435 1435 /* ··· 1444 1444 struct tty_port *port = &state->port; 1445 1445 unsigned long flags; 1446 1446 1447 - BUG_ON(!kernel_locked()); 1447 + BUG_ON(!tty_locked()); 1448 1448 pr_debug("uart_hangup(%d)\n", state->uart_port->line); 1449 1449 1450 1450 mutex_lock(&port->mutex); ··· 1578 1578 struct tty_port *port; 1579 1579 int retval, line = tty->index; 1580 1580 1581 - BUG_ON(!kernel_locked()); 1581 + BUG_ON(!tty_locked()); 1582 1582 pr_debug("uart_open(%d) called\n", line); 1583 1583 1584 1584 /*
+2 -2
drivers/video/console/vgacon.c
··· 1108 1108 charmap += 4 * cmapsz; 1109 1109 #endif 1110 1110 1111 - unlock_kernel(); 1111 + tty_unlock(); 1112 1112 spin_lock_irq(&vga_lock); 1113 1113 /* First, the Sequencer */ 1114 1114 vga_wseq(state->vgabase, VGA_SEQ_RESET, 0x1); ··· 1192 1192 vga_wattr(state->vgabase, VGA_AR_ENABLE_DISPLAY, 0); 1193 1193 } 1194 1194 spin_unlock_irq(&vga_lock); 1195 - lock_kernel(); 1195 + tty_lock(); 1196 1196 return 0; 1197 1197 } 1198 1198
+31
include/linux/tty.h
··· 13 13 #include <linux/tty_driver.h> 14 14 #include <linux/tty_ldisc.h> 15 15 #include <linux/mutex.h> 16 + #include <linux/smp_lock.h> 16 17 17 18 #include <asm/system.h> 18 19 ··· 576 575 577 576 extern long vt_compat_ioctl(struct tty_struct *tty, struct file * file, 578 577 unsigned int cmd, unsigned long arg); 578 + 579 + /* functions for preparation of BKL removal */ 580 + 581 + /* 582 + * tty_lock_nested get the tty_lock while potentially holding it 583 + * 584 + * The Big TTY Mutex is a recursive lock, meaning you can take it 585 + * from a thread that is already holding it. 586 + * This is bad for a number of reasons, so tty_lock_nested should 587 + * really be used as rarely as possible. If a code location can 588 + * be shown to never get called with this held already, it should 589 + * use tty_lock() instead. 590 + */ 591 + static inline void __lockfunc tty_lock_nested(void) __acquires(kernel_lock) 592 + { 593 + lock_kernel(); 594 + } 595 + static inline void tty_lock(void) __acquires(kernel_lock) 596 + { 597 + #ifdef CONFIG_LOCK_KERNEL 598 + /* kernel_locked is 1 for !CONFIG_LOCK_KERNEL */ 599 + WARN_ON(kernel_locked()); 600 + #endif 601 + lock_kernel(); 602 + } 603 + static inline void tty_unlock(void) __releases(kernel_lock) 604 + { 605 + unlock_kernel(); 606 + } 607 + #define tty_locked() (kernel_locked()) 579 608 580 609 #endif /* __KERNEL__ */ 581 610 #endif