aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/tty/n_tty.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/tty/n_tty.c')
-rw-r--r--drivers/tty/n_tty.c172
1 files changed, 85 insertions, 87 deletions
diff --git a/drivers/tty/n_tty.c b/drivers/tty/n_tty.c
index 05e72bea9b07..d655416087b7 100644
--- a/drivers/tty/n_tty.c
+++ b/drivers/tty/n_tty.c
@@ -153,6 +153,12 @@ static void n_tty_set_room(struct tty_struct *tty)
153 if (left && !old_left) { 153 if (left && !old_left) {
154 WARN_RATELIMIT(tty->port->itty == NULL, 154 WARN_RATELIMIT(tty->port->itty == NULL,
155 "scheduling with invalid itty\n"); 155 "scheduling with invalid itty\n");
156 /* see if ldisc has been killed - if so, this means that
157 * even though the ldisc has been halted and ->buf.work
158 * cancelled, ->buf.work is about to be rescheduled
159 */
160 WARN_RATELIMIT(test_bit(TTY_LDISC_HALTED, &tty->flags),
161 "scheduling buffer work for halted ldisc\n");
156 schedule_work(&tty->port->buf.work); 162 schedule_work(&tty->port->buf.work);
157 } 163 }
158} 164}
@@ -189,34 +195,17 @@ static void put_tty_queue(unsigned char c, struct n_tty_data *ldata)
189} 195}
190 196
191/** 197/**
192 * check_unthrottle - allow new receive data
193 * @tty; tty device
194 *
195 * Check whether to call the driver unthrottle functions
196 *
197 * Can sleep, may be called under the atomic_read_lock mutex but
198 * this is not guaranteed.
199 */
200static void check_unthrottle(struct tty_struct *tty)
201{
202 if (tty->count)
203 tty_unthrottle(tty);
204}
205
206/**
207 * reset_buffer_flags - reset buffer state 198 * reset_buffer_flags - reset buffer state
208 * @tty: terminal to reset 199 * @tty: terminal to reset
209 * 200 *
210 * Reset the read buffer counters, clear the flags, 201 * Reset the read buffer counters and clear the flags.
211 * and make sure the driver is unthrottled. Called 202 * Called from n_tty_open() and n_tty_flush_buffer().
212 * from n_tty_open() and n_tty_flush_buffer().
213 * 203 *
214 * Locking: tty_read_lock for read fields. 204 * Locking: tty_read_lock for read fields.
215 */ 205 */
216 206
217static void reset_buffer_flags(struct tty_struct *tty) 207static void reset_buffer_flags(struct n_tty_data *ldata)
218{ 208{
219 struct n_tty_data *ldata = tty->disc_data;
220 unsigned long flags; 209 unsigned long flags;
221 210
222 raw_spin_lock_irqsave(&ldata->read_lock, flags); 211 raw_spin_lock_irqsave(&ldata->read_lock, flags);
@@ -229,36 +218,38 @@ static void reset_buffer_flags(struct tty_struct *tty)
229 218
230 ldata->canon_head = ldata->canon_data = ldata->erasing = 0; 219 ldata->canon_head = ldata->canon_data = ldata->erasing = 0;
231 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE); 220 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
232 n_tty_set_room(tty); 221}
222
223static void n_tty_packet_mode_flush(struct tty_struct *tty)
224{
225 unsigned long flags;
226
227 spin_lock_irqsave(&tty->ctrl_lock, flags);
228 if (tty->link->packet) {
229 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
230 wake_up_interruptible(&tty->link->read_wait);
231 }
232 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
233} 233}
234 234
235/** 235/**
236 * n_tty_flush_buffer - clean input queue 236 * n_tty_flush_buffer - clean input queue
237 * @tty: terminal device 237 * @tty: terminal device
238 * 238 *
239 * Flush the input buffer. Called when the line discipline is 239 * Flush the input buffer. Called when the tty layer wants the
240 * being closed, when the tty layer wants the buffer flushed (eg 240 * buffer flushed (eg at hangup) or when the N_TTY line discipline
241 * at hangup) or when the N_TTY line discipline internally has to 241 * internally has to clean the pending queue (for example some signals).
242 * clean the pending queue (for example some signals).
243 * 242 *
244 * Locking: ctrl_lock, read_lock. 243 * Locking: ctrl_lock, read_lock.
245 */ 244 */
246 245
247static void n_tty_flush_buffer(struct tty_struct *tty) 246static void n_tty_flush_buffer(struct tty_struct *tty)
248{ 247{
249 unsigned long flags; 248 reset_buffer_flags(tty->disc_data);
250 /* clear everything and unthrottle the driver */ 249 n_tty_set_room(tty);
251 reset_buffer_flags(tty);
252
253 if (!tty->link)
254 return;
255 250
256 spin_lock_irqsave(&tty->ctrl_lock, flags); 251 if (tty->link)
257 if (tty->link->packet) { 252 n_tty_packet_mode_flush(tty);
258 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
259 wake_up_interruptible(&tty->link->read_wait);
260 }
261 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
262} 253}
263 254
264/** 255/**
@@ -1032,23 +1023,19 @@ static void eraser(unsigned char c, struct tty_struct *tty)
1032 * isig - handle the ISIG optio 1023 * isig - handle the ISIG optio
1033 * @sig: signal 1024 * @sig: signal
1034 * @tty: terminal 1025 * @tty: terminal
1035 * @flush: force flush
1036 * 1026 *
1037 * Called when a signal is being sent due to terminal input. This 1027 * Called when a signal is being sent due to terminal input.
1038 * may caus terminal flushing to take place according to the termios 1028 * Called from the driver receive_buf path so serialized.
1039 * settings and character used. Called from the driver receive_buf
1040 * path so serialized.
1041 * 1029 *
1042 * Locking: ctrl_lock, read_lock (both via flush buffer) 1030 * Locking: ctrl_lock
1043 */ 1031 */
1044 1032
1045static inline void isig(int sig, struct tty_struct *tty, int flush) 1033static inline void isig(int sig, struct tty_struct *tty)
1046{ 1034{
1047 if (tty->pgrp) 1035 struct pid *tty_pgrp = tty_get_pgrp(tty);
1048 kill_pgrp(tty->pgrp, sig, 1); 1036 if (tty_pgrp) {
1049 if (flush || !L_NOFLSH(tty)) { 1037 kill_pgrp(tty_pgrp, sig, 1);
1050 n_tty_flush_buffer(tty); 1038 put_pid(tty_pgrp);
1051 tty_driver_flush_buffer(tty);
1052 } 1039 }
1053} 1040}
1054 1041
@@ -1069,7 +1056,11 @@ static inline void n_tty_receive_break(struct tty_struct *tty)
1069 if (I_IGNBRK(tty)) 1056 if (I_IGNBRK(tty))
1070 return; 1057 return;
1071 if (I_BRKINT(tty)) { 1058 if (I_BRKINT(tty)) {
1072 isig(SIGINT, tty, 1); 1059 isig(SIGINT, tty);
1060 if (!L_NOFLSH(tty)) {
1061 n_tty_flush_buffer(tty);
1062 tty_driver_flush_buffer(tty);
1063 }
1073 return; 1064 return;
1074 } 1065 }
1075 if (I_PARMRK(tty)) { 1066 if (I_PARMRK(tty)) {
@@ -1236,11 +1227,6 @@ static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
1236 signal = SIGTSTP; 1227 signal = SIGTSTP;
1237 if (c == SUSP_CHAR(tty)) { 1228 if (c == SUSP_CHAR(tty)) {
1238send_signal: 1229send_signal:
1239 /*
1240 * Note that we do not use isig() here because we want
1241 * the order to be:
1242 * 1) flush, 2) echo, 3) signal
1243 */
1244 if (!L_NOFLSH(tty)) { 1230 if (!L_NOFLSH(tty)) {
1245 n_tty_flush_buffer(tty); 1231 n_tty_flush_buffer(tty);
1246 tty_driver_flush_buffer(tty); 1232 tty_driver_flush_buffer(tty);
@@ -1251,8 +1237,7 @@ send_signal:
1251 echo_char(c, tty); 1237 echo_char(c, tty);
1252 process_echoes(tty); 1238 process_echoes(tty);
1253 } 1239 }
1254 if (tty->pgrp) 1240 isig(signal, tty);
1255 kill_pgrp(tty->pgrp, signal, 1);
1256 return; 1241 return;
1257 } 1242 }
1258 } 1243 }
@@ -1483,14 +1468,14 @@ static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1483 * mode. We don't want to throttle the driver if we're in 1468 * mode. We don't want to throttle the driver if we're in
1484 * canonical mode and don't have a newline yet! 1469 * canonical mode and don't have a newline yet!
1485 */ 1470 */
1486 if (tty->receive_room < TTY_THRESHOLD_THROTTLE) 1471 while (1) {
1487 tty_throttle(tty); 1472 tty_set_flow_change(tty, TTY_THROTTLE_SAFE);
1488 1473 if (tty->receive_room >= TTY_THRESHOLD_THROTTLE)
1489 /* FIXME: there is a tiny race here if the receive room check runs 1474 break;
1490 before the other work executes and empties the buffer (upping 1475 if (!tty_throttle_safe(tty))
1491 the receiving room and unthrottling. We then throttle and get 1476 break;
1492 stuck. This has been observed and traced down by Vincent Pillet/ 1477 }
1493 We need to address this when we sort out out the rx path locking */ 1478 __tty_set_flow_change(tty, 0);
1494} 1479}
1495 1480
1496int is_ignored(int sig) 1481int is_ignored(int sig)
@@ -1607,7 +1592,9 @@ static void n_tty_close(struct tty_struct *tty)
1607{ 1592{
1608 struct n_tty_data *ldata = tty->disc_data; 1593 struct n_tty_data *ldata = tty->disc_data;
1609 1594
1610 n_tty_flush_buffer(tty); 1595 if (tty->link)
1596 n_tty_packet_mode_flush(tty);
1597
1611 kfree(ldata->read_buf); 1598 kfree(ldata->read_buf);
1612 kfree(ldata->echo_buf); 1599 kfree(ldata->echo_buf);
1613 kfree(ldata); 1600 kfree(ldata);
@@ -1645,12 +1632,14 @@ static int n_tty_open(struct tty_struct *tty)
1645 goto err_free_bufs; 1632 goto err_free_bufs;
1646 1633
1647 tty->disc_data = ldata; 1634 tty->disc_data = ldata;
1648 reset_buffer_flags(tty); 1635 reset_buffer_flags(tty->disc_data);
1649 tty_unthrottle(tty);
1650 ldata->column = 0; 1636 ldata->column = 0;
1651 n_tty_set_termios(tty, NULL);
1652 tty->minimum_to_wake = 1; 1637 tty->minimum_to_wake = 1;
1653 tty->closing = 0; 1638 tty->closing = 0;
1639 /* indicate buffer work may resume */
1640 clear_bit(TTY_LDISC_HALTED, &tty->flags);
1641 n_tty_set_termios(tty, NULL);
1642 tty_unthrottle(tty);
1654 1643
1655 return 0; 1644 return 0;
1656err_free_bufs: 1645err_free_bufs:
@@ -1740,10 +1729,9 @@ extern ssize_t redirected_tty_write(struct file *, const char __user *,
1740 * and if appropriate send any needed signals and return a negative 1729 * and if appropriate send any needed signals and return a negative
1741 * error code if action should be taken. 1730 * error code if action should be taken.
1742 * 1731 *
1743 * FIXME: 1732 * Locking: redirected write test is safe
1744 * Locking: None - redirected write test is safe, testing 1733 * current->signal->tty check is safe
1745 * current->signal should possibly lock current->sighand 1734 * ctrl_lock to safely reference tty->pgrp
1746 * pgrp locking ?
1747 */ 1735 */
1748 1736
1749static int job_control(struct tty_struct *tty, struct file *file) 1737static int job_control(struct tty_struct *tty, struct file *file)
@@ -1753,19 +1741,22 @@ static int job_control(struct tty_struct *tty, struct file *file)
1753 /* NOTE: not yet done after every sleep pending a thorough 1741 /* NOTE: not yet done after every sleep pending a thorough
1754 check of the logic of this change. -- jlc */ 1742 check of the logic of this change. -- jlc */
1755 /* don't stop on /dev/console */ 1743 /* don't stop on /dev/console */
1756 if (file->f_op->write != redirected_tty_write && 1744 if (file->f_op->write == redirected_tty_write ||
1757 current->signal->tty == tty) { 1745 current->signal->tty != tty)
1758 if (!tty->pgrp) 1746 return 0;
1759 printk(KERN_ERR "n_tty_read: no tty->pgrp!\n"); 1747
1760 else if (task_pgrp(current) != tty->pgrp) { 1748 spin_lock_irq(&tty->ctrl_lock);
1761 if (is_ignored(SIGTTIN) || 1749 if (!tty->pgrp)
1762 is_current_pgrp_orphaned()) 1750 printk(KERN_ERR "n_tty_read: no tty->pgrp!\n");
1763 return -EIO; 1751 else if (task_pgrp(current) != tty->pgrp) {
1764 kill_pgrp(task_pgrp(current), SIGTTIN, 1); 1752 spin_unlock_irq(&tty->ctrl_lock);
1765 set_thread_flag(TIF_SIGPENDING); 1753 if (is_ignored(SIGTTIN) || is_current_pgrp_orphaned())
1766 return -ERESTARTSYS; 1754 return -EIO;
1767 } 1755 kill_pgrp(task_pgrp(current), SIGTTIN, 1);
1756 set_thread_flag(TIF_SIGPENDING);
1757 return -ERESTARTSYS;
1768 } 1758 }
1759 spin_unlock_irq(&tty->ctrl_lock);
1769 return 0; 1760 return 0;
1770} 1761}
1771 1762
@@ -1959,10 +1950,17 @@ do_it_again:
1959 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode, 1950 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1960 * we won't get any more characters. 1951 * we won't get any more characters.
1961 */ 1952 */
1962 if (n_tty_chars_in_buffer(tty) <= TTY_THRESHOLD_UNTHROTTLE) { 1953 while (1) {
1954 tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
1955 if (n_tty_chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
1956 break;
1957 if (!tty->count)
1958 break;
1963 n_tty_set_room(tty); 1959 n_tty_set_room(tty);
1964 check_unthrottle(tty); 1960 if (!tty_unthrottle_safe(tty))
1961 break;
1965 } 1962 }
1963 __tty_set_flow_change(tty, 0);
1966 1964
1967 if (b - buf >= minimum) 1965 if (b - buf >= minimum)
1968 break; 1966 break;