aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPeter Hurley <peter@hurleysoftware.com>2013-06-15 09:14:15 -0400
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2013-07-23 19:42:59 -0400
commit24a89d1cb69b6c488cf16d98dd02e7820f62b40c (patch)
treee09745de8a63d9d8039495ed8b9bb79fbb334702
parentda261e7fe7b0e23a0d4d46039d20dc60fa197b49 (diff)
tty: Make ldisc input flow control concurrency-friendly
Although line discipline receiving is single-producer/single-consumer, using tty->receive_room to manage flow control creates unnecessary critical regions requiring additional lock use. Instead, introduce the optional .receive_buf2() ldisc method which returns the # of bytes actually received. Serialization is guaranteed by the caller. In turn, the line discipline should schedule the buffer work item whenever space becomes available; ie., when there is room to receive data and receive_room() previously returned 0 (the buffer work item stops processing if receive_buf2() returns 0). Note the 'no room' state need not be atomic despite concurrent use by two threads because only the buffer work thread can set the state and only the read() thread can clear the state. Add n_tty_receive_buf2() as the receive_buf2() method for N_TTY. Provide a public helper function, tty_ldisc_receive_buf(), to use when directly accessing the receive_buf() methods. Line disciplines not using input flow control can continue to set tty->receive_room to a fixed value and only provide the receive_buf() method. Signed-off-by: Peter Hurley <peter@hurleysoftware.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
-rw-r--r--drivers/tty/n_tty.c72
-rw-r--r--drivers/tty/tty_buffer.c13
-rw-r--r--drivers/tty/vt/selection.c4
-rw-r--r--include/linux/tty.h13
-rw-r--r--include/linux/tty_ldisc.h13
5 files changed, 82 insertions, 33 deletions
diff --git a/drivers/tty/n_tty.c b/drivers/tty/n_tty.c
index 4bf0fc0843d7..eddeb7889e62 100644
--- a/drivers/tty/n_tty.c
+++ b/drivers/tty/n_tty.c
@@ -79,6 +79,9 @@ struct n_tty_data {
79 unsigned long overrun_time; 79 unsigned long overrun_time;
80 int num_overrun; 80 int num_overrun;
81 81
82 /* non-atomic */
83 bool no_room;
84
82 unsigned char lnext:1, erasing:1, raw:1, real_raw:1, icanon:1; 85 unsigned char lnext:1, erasing:1, raw:1, real_raw:1, icanon:1;
83 unsigned char echo_overrun:1; 86 unsigned char echo_overrun:1;
84 87
@@ -114,25 +117,10 @@ static inline int tty_put_user(struct tty_struct *tty, unsigned char x,
114 return put_user(x, ptr); 117 return put_user(x, ptr);
115} 118}
116 119
117/** 120static int receive_room(struct tty_struct *tty)
118 * n_tty_set_room - receive space
119 * @tty: terminal
120 *
121 * Updates tty->receive_room to reflect the currently available space
122 * in the input buffer, and re-schedules the flip buffer work if space
123 * just became available.
124 *
125 * Locks: Concurrent update is protected with read_lock
126 */
127
128static int set_room(struct tty_struct *tty)
129{ 121{
130 struct n_tty_data *ldata = tty->disc_data; 122 struct n_tty_data *ldata = tty->disc_data;
131 int left; 123 int left;
132 int old_left;
133 unsigned long flags;
134
135 raw_spin_lock_irqsave(&ldata->read_lock, flags);
136 124
137 if (I_PARMRK(tty)) { 125 if (I_PARMRK(tty)) {
138 /* Multiply read_cnt by 3, since each byte might take up to 126 /* Multiply read_cnt by 3, since each byte might take up to
@@ -150,18 +138,27 @@ static int set_room(struct tty_struct *tty)
150 */ 138 */
151 if (left <= 0) 139 if (left <= 0)
152 left = ldata->icanon && !ldata->canon_data; 140 left = ldata->icanon && !ldata->canon_data;
153 old_left = tty->receive_room;
154 tty->receive_room = left;
155 141
156 raw_spin_unlock_irqrestore(&ldata->read_lock, flags); 142 return left;
157
158 return left && !old_left;
159} 143}
160 144
145/**
146 * n_tty_set_room - receive space
147 * @tty: terminal
148 *
149 * Re-schedules the flip buffer work if space just became available.
150 *
151 * Locks: Concurrent update is protected with read_lock
152 */
153
161static void n_tty_set_room(struct tty_struct *tty) 154static void n_tty_set_room(struct tty_struct *tty)
162{ 155{
156 struct n_tty_data *ldata = tty->disc_data;
157
163 /* Did this open up the receive buffer? We may need to flip */ 158 /* Did this open up the receive buffer? We may need to flip */
164 if (set_room(tty)) { 159 if (unlikely(ldata->no_room) && receive_room(tty)) {
160 ldata->no_room = 0;
161
165 WARN_RATELIMIT(tty->port->itty == NULL, 162 WARN_RATELIMIT(tty->port->itty == NULL,
166 "scheduling with invalid itty\n"); 163 "scheduling with invalid itty\n");
167 /* see if ldisc has been killed - if so, this means that 164 /* see if ldisc has been killed - if so, this means that
@@ -1408,8 +1405,8 @@ static void n_tty_write_wakeup(struct tty_struct *tty)
1408 * calls one at a time and in order (or using flush_to_ldisc) 1405 * calls one at a time and in order (or using flush_to_ldisc)
1409 */ 1406 */
1410 1407
1411static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp, 1408static void __receive_buf(struct tty_struct *tty, const unsigned char *cp,
1412 char *fp, int count) 1409 char *fp, int count)
1413{ 1410{
1414 struct n_tty_data *ldata = tty->disc_data; 1411 struct n_tty_data *ldata = tty->disc_data;
1415 const unsigned char *p; 1412 const unsigned char *p;
@@ -1464,8 +1461,6 @@ static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1464 tty->ops->flush_chars(tty); 1461 tty->ops->flush_chars(tty);
1465 } 1462 }
1466 1463
1467 set_room(tty);
1468
1469 if ((!ldata->icanon && (ldata->read_cnt >= ldata->minimum_to_wake)) || 1464 if ((!ldata->icanon && (ldata->read_cnt >= ldata->minimum_to_wake)) ||
1470 L_EXTPROC(tty)) { 1465 L_EXTPROC(tty)) {
1471 kill_fasync(&tty->fasync, SIGIO, POLL_IN); 1466 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
@@ -1480,7 +1475,7 @@ static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1480 */ 1475 */
1481 while (1) { 1476 while (1) {
1482 tty_set_flow_change(tty, TTY_THROTTLE_SAFE); 1477 tty_set_flow_change(tty, TTY_THROTTLE_SAFE);
1483 if (tty->receive_room >= TTY_THRESHOLD_THROTTLE) 1478 if (receive_room(tty) >= TTY_THRESHOLD_THROTTLE)
1484 break; 1479 break;
1485 if (!tty_throttle_safe(tty)) 1480 if (!tty_throttle_safe(tty))
1486 break; 1481 break;
@@ -1488,6 +1483,28 @@ static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1488 __tty_set_flow_change(tty, 0); 1483 __tty_set_flow_change(tty, 0);
1489} 1484}
1490 1485
1486static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1487 char *fp, int count)
1488{
1489 __receive_buf(tty, cp, fp, count);
1490}
1491
1492static int n_tty_receive_buf2(struct tty_struct *tty, const unsigned char *cp,
1493 char *fp, int count)
1494{
1495 struct n_tty_data *ldata = tty->disc_data;
1496 int room;
1497
1498 tty->receive_room = room = receive_room(tty);
1499 if (!room)
1500 ldata->no_room = 1;
1501 count = min(count, room);
1502 if (count)
1503 __receive_buf(tty, cp, fp, count);
1504
1505 return count;
1506}
1507
1491int is_ignored(int sig) 1508int is_ignored(int sig)
1492{ 1509{
1493 return (sigismember(&current->blocked, sig) || 1510 return (sigismember(&current->blocked, sig) ||
@@ -2203,6 +2220,7 @@ struct tty_ldisc_ops tty_ldisc_N_TTY = {
2203 .receive_buf = n_tty_receive_buf, 2220 .receive_buf = n_tty_receive_buf,
2204 .write_wakeup = n_tty_write_wakeup, 2221 .write_wakeup = n_tty_write_wakeup,
2205 .fasync = n_tty_fasync, 2222 .fasync = n_tty_fasync,
2223 .receive_buf2 = n_tty_receive_buf2,
2206}; 2224};
2207 2225
2208/** 2226/**
diff --git a/drivers/tty/tty_buffer.c b/drivers/tty/tty_buffer.c
index 6c7a1d043c76..ff1b2e37c3ca 100644
--- a/drivers/tty/tty_buffer.c
+++ b/drivers/tty/tty_buffer.c
@@ -407,11 +407,16 @@ static int
407receive_buf(struct tty_struct *tty, struct tty_buffer *head, int count) 407receive_buf(struct tty_struct *tty, struct tty_buffer *head, int count)
408{ 408{
409 struct tty_ldisc *disc = tty->ldisc; 409 struct tty_ldisc *disc = tty->ldisc;
410 char *p = head->char_buf_ptr + head->read;
411 unsigned char *f = head->flag_buf_ptr + head->read;
410 412
411 count = min_t(int, count, tty->receive_room); 413 if (disc->ops->receive_buf2)
412 if (count) 414 count = disc->ops->receive_buf2(tty, p, f, count);
413 disc->ops->receive_buf(tty, head->char_buf_ptr + head->read, 415 else {
414 head->flag_buf_ptr + head->read, count); 416 count = min_t(int, count, tty->receive_room);
417 if (count)
418 disc->ops->receive_buf(tty, p, f, count);
419 }
415 head->read += count; 420 head->read += count;
416 return count; 421 return count;
417} 422}
diff --git a/drivers/tty/vt/selection.c b/drivers/tty/vt/selection.c
index 60b7b6926059..2ca8d6b6514c 100644
--- a/drivers/tty/vt/selection.c
+++ b/drivers/tty/vt/selection.c
@@ -356,8 +356,8 @@ int paste_selection(struct tty_struct *tty)
356 continue; 356 continue;
357 } 357 }
358 count = sel_buffer_lth - pasted; 358 count = sel_buffer_lth - pasted;
359 count = min(count, tty->receive_room); 359 count = tty_ldisc_receive_buf(ld, sel_buffer + pasted, NULL,
360 ld->ops->receive_buf(tty, sel_buffer + pasted, NULL, count); 360 count);
361 pasted += count; 361 pasted += count;
362 } 362 }
363 remove_wait_queue(&vc->paste_wait, &wait); 363 remove_wait_queue(&vc->paste_wait, &wait);
diff --git a/include/linux/tty.h b/include/linux/tty.h
index 7269daf7632b..8323ee4f95b9 100644
--- a/include/linux/tty.h
+++ b/include/linux/tty.h
@@ -557,6 +557,19 @@ extern void tty_ldisc_init(struct tty_struct *tty);
557extern void tty_ldisc_deinit(struct tty_struct *tty); 557extern void tty_ldisc_deinit(struct tty_struct *tty);
558extern void tty_ldisc_begin(void); 558extern void tty_ldisc_begin(void);
559 559
560static inline int tty_ldisc_receive_buf(struct tty_ldisc *ld, unsigned char *p,
561 char *f, int count)
562{
563 if (ld->ops->receive_buf2)
564 count = ld->ops->receive_buf2(ld->tty, p, f, count);
565 else {
566 count = min_t(int, count, ld->tty->receive_room);
567 if (count)
568 ld->ops->receive_buf(ld->tty, p, f, count);
569 }
570 return count;
571}
572
560 573
561/* n_tty.c */ 574/* n_tty.c */
562extern struct tty_ldisc_ops tty_ldisc_N_TTY; 575extern struct tty_ldisc_ops tty_ldisc_N_TTY;
diff --git a/include/linux/tty_ldisc.h b/include/linux/tty_ldisc.h
index 23bdd9debb84..f15c898ff462 100644
--- a/include/linux/tty_ldisc.h
+++ b/include/linux/tty_ldisc.h
@@ -109,6 +109,17 @@
109 * 109 *
110 * Tells the discipline that the DCD pin has changed its status. 110 * Tells the discipline that the DCD pin has changed its status.
111 * Used exclusively by the N_PPS (Pulse-Per-Second) line discipline. 111 * Used exclusively by the N_PPS (Pulse-Per-Second) line discipline.
112 *
113 * int (*receive_buf2)(struct tty_struct *, const unsigned char *cp,
114 * char *fp, int count);
115 *
116 * This function is called by the low-level tty driver to send
117 * characters received by the hardware to the line discpline for
118 * processing. <cp> is a pointer to the buffer of input
119 * character received by the device. <fp> is a pointer to a
120 * pointer of flag bytes which indicate whether a character was
121 * received with a parity error, etc.
122 * If assigned, prefer this function for automatic flow control.
112 */ 123 */
113 124
114#include <linux/fs.h> 125#include <linux/fs.h>
@@ -195,6 +206,8 @@ struct tty_ldisc_ops {
195 void (*write_wakeup)(struct tty_struct *); 206 void (*write_wakeup)(struct tty_struct *);
196 void (*dcd_change)(struct tty_struct *, unsigned int); 207 void (*dcd_change)(struct tty_struct *, unsigned int);
197 void (*fasync)(struct tty_struct *tty, int on); 208 void (*fasync)(struct tty_struct *tty, int on);
209 int (*receive_buf2)(struct tty_struct *, const unsigned char *cp,
210 char *fp, int count);
198 211
199 struct module *owner; 212 struct module *owner;
200 213