aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/char/pty.c
diff options
context:
space:
mode:
authorTejun Heo <tj@kernel.org>2009-08-14 01:41:02 -0400
committerTejun Heo <tj@kernel.org>2009-08-14 01:45:31 -0400
commit384be2b18a5f9475eab9ca2bdfa95cc1a04ef59c (patch)
tree04c93f391a1b65c8bf8d7ba8643c07d26c26590a /drivers/char/pty.c
parenta76761b621bcd8336065c4fe3a74f046858bc34c (diff)
parent142d44b0dd6741a64a7bdbe029110e7c1dcf1d23 (diff)
Merge branch 'percpu-for-linus' into percpu-for-next
Conflicts: arch/sparc/kernel/smp_64.c arch/x86/kernel/cpu/perf_counter.c arch/x86/kernel/setup_percpu.c drivers/cpufreq/cpufreq_ondemand.c mm/percpu.c Conflicts in core and arch percpu codes are mostly from commit ed78e1e078dd44249f88b1dd8c76dafb39567161 which substituted many num_possible_cpus() with nr_cpu_ids. As for-next branch has moved all the first chunk allocators into mm/percpu.c, the changes are moved from arch code to mm/percpu.c. Signed-off-by: Tejun Heo <tj@kernel.org>
Diffstat (limited to 'drivers/char/pty.c')
-rw-r--r--drivers/char/pty.c155
1 files changed, 61 insertions, 94 deletions
diff --git a/drivers/char/pty.c b/drivers/char/pty.c
index daebe1ba43d4..d083c73d784a 100644
--- a/drivers/char/pty.c
+++ b/drivers/char/pty.c
@@ -22,6 +22,7 @@
22#include <linux/major.h> 22#include <linux/major.h>
23#include <linux/mm.h> 23#include <linux/mm.h>
24#include <linux/init.h> 24#include <linux/init.h>
25#include <linux/smp_lock.h>
25#include <linux/sysctl.h> 26#include <linux/sysctl.h>
26#include <linux/device.h> 27#include <linux/device.h>
27#include <linux/uaccess.h> 28#include <linux/uaccess.h>
@@ -75,114 +76,90 @@ static void pty_close(struct tty_struct *tty, struct file *filp)
75 */ 76 */
76static void pty_unthrottle(struct tty_struct *tty) 77static void pty_unthrottle(struct tty_struct *tty)
77{ 78{
78 struct tty_struct *o_tty = tty->link; 79 tty_wakeup(tty->link);
79
80 if (!o_tty)
81 return;
82
83 tty_wakeup(o_tty);
84 set_bit(TTY_THROTTLED, &tty->flags); 80 set_bit(TTY_THROTTLED, &tty->flags);
85} 81}
86 82
87/* 83/**
88 * WSH 05/24/97: modified to 84 * pty_space - report space left for writing
89 * (1) use space in tty->flip instead of a shared temp buffer 85 * @to: tty we are writing into
90 * The flip buffers aren't being used for a pty, so there's lots
91 * of space available. The buffer is protected by a per-pty
92 * semaphore that should almost never come under contention.
93 * (2) avoid redundant copying for cases where count >> receive_room
94 * N.B. Calls from user space may now return an error code instead of
95 * a count.
96 * 86 *
97 * FIXME: Our pty_write method is called with our ldisc lock held but 87 * The tty buffers allow 64K but we sneak a peak and clip at 8K this
98 * not our partners. We can't just wait on the other one blindly without 88 * allows a lot of overspill room for echo and other fun messes to
99 * risking deadlocks. At some point when everything has settled down we need 89 * be handled properly
100 * to look into making pty_write at least able to sleep over an ldisc change. 90 */
91
92static int pty_space(struct tty_struct *to)
93{
94 int n = 8192 - to->buf.memory_used;
95 if (n < 0)
96 return 0;
97 return n;
98}
99
100/**
101 * pty_write - write to a pty
102 * @tty: the tty we write from
103 * @buf: kernel buffer of data
104 * @count: bytes to write
101 * 105 *
102 * The return on no ldisc is a bit counter intuitive but the logic works 106 * Our "hardware" write method. Data is coming from the ldisc which
103 * like this. During an ldisc change the other end will flush its buffers. We 107 * may be in a non sleeping state. We simply throw this at the other
104 * thus return the full length which is identical to the case where we had 108 * end of the link as if we were an IRQ handler receiving stuff for
105 * proper locking and happened to queue the bytes just before the flush during 109 * the other side of the pty/tty pair.
106 * the ldisc change.
107 */ 110 */
111
108static int pty_write(struct tty_struct *tty, const unsigned char *buf, 112static int pty_write(struct tty_struct *tty, const unsigned char *buf,
109 int count) 113 int count)
110{ 114{
111 struct tty_struct *to = tty->link; 115 struct tty_struct *to = tty->link;
112 struct tty_ldisc *ld; 116 int c;
113 int c = count;
114 117
115 if (!to || tty->stopped) 118 if (tty->stopped)
116 return 0; 119 return 0;
117 ld = tty_ldisc_ref(to); 120
118 121 /* This isn't locked but our 8K is quite sloppy so no
119 if (ld) { 122 big deal */
120 c = to->receive_room; 123
121 if (c > count) 124 c = pty_space(to);
122 c = count; 125 if (c > count)
123 ld->ops->receive_buf(to, buf, NULL, c); 126 c = count;
124 tty_ldisc_deref(ld); 127 if (c > 0) {
128 /* Stuff the data into the input queue of the other end */
129 c = tty_insert_flip_string(to, buf, c);
130 /* And shovel */
131 tty_flip_buffer_push(to);
132 tty_wakeup(tty);
125 } 133 }
126 return c; 134 return c;
127} 135}
128 136
137/**
138 * pty_write_room - write space
139 * @tty: tty we are writing from
140 *
141 * Report how many bytes the ldisc can send into the queue for
142 * the other device.
143 */
144
129static int pty_write_room(struct tty_struct *tty) 145static int pty_write_room(struct tty_struct *tty)
130{ 146{
131 struct tty_struct *to = tty->link; 147 if (tty->stopped)
132
133 if (!to || tty->stopped)
134 return 0; 148 return 0;
135 149 return pty_space(tty->link);
136 return to->receive_room;
137} 150}
138 151
139/* 152/**
140 * WSH 05/24/97: Modified for asymmetric MASTER/SLAVE behavior 153 * pty_chars_in_buffer - characters currently in our tx queue
141 * The chars_in_buffer() value is used by the ldisc select() function 154 * @tty: our tty
142 * to hold off writing when chars_in_buffer > WAKEUP_CHARS (== 256).
143 * The pty driver chars_in_buffer() Master/Slave must behave differently:
144 *
145 * The Master side needs to allow typed-ahead commands to accumulate
146 * while being canonicalized, so we report "our buffer" as empty until
147 * some threshold is reached, and then report the count. (Any count >
148 * WAKEUP_CHARS is regarded by select() as "full".) To avoid deadlock
149 * the count returned must be 0 if no canonical data is available to be
150 * read. (The N_TTY ldisc.chars_in_buffer now knows this.)
151 * 155 *
152 * The Slave side passes all characters in raw mode to the Master side's 156 * Report how much we have in the transmit queue. As everything is
153 * buffer where they can be read immediately, so in this case we can 157 * instantly at the other end this is easy to implement.
154 * return the true count in the buffer.
155 */ 158 */
159
156static int pty_chars_in_buffer(struct tty_struct *tty) 160static int pty_chars_in_buffer(struct tty_struct *tty)
157{ 161{
158 struct tty_struct *to = tty->link; 162 return 0;
159 struct tty_ldisc *ld;
160 int count = 0;
161
162 /* We should get the line discipline lock for "tty->link" */
163 if (!to)
164 return 0;
165 /* We cannot take a sleeping reference here without deadlocking with
166 an ldisc change - but it doesn't really matter */
167 ld = tty_ldisc_ref(to);
168 if (ld == NULL)
169 return 0;
170
171 /* The ldisc must report 0 if no characters available to be read */
172 if (ld->ops->chars_in_buffer)
173 count = ld->ops->chars_in_buffer(to);
174
175 tty_ldisc_deref(ld);
176
177 if (tty->driver->subtype == PTY_TYPE_SLAVE)
178 return count;
179
180 /* Master side driver ... if the other side's read buffer is less than
181 * half full, return 0 to allow writers to proceed; otherwise return
182 * the count. This leaves a comfortable margin to avoid overflow,
183 * and still allows half a buffer's worth of typed-ahead commands.
184 */
185 return (count < N_TTY_BUF_SIZE/2) ? 0 : count;
186} 163}
187 164
188/* Set the lock flag on a pty */ 165/* Set the lock flag on a pty */
@@ -202,20 +179,10 @@ static void pty_flush_buffer(struct tty_struct *tty)
202{ 179{
203 struct tty_struct *to = tty->link; 180 struct tty_struct *to = tty->link;
204 unsigned long flags; 181 unsigned long flags;
205 struct tty_ldisc *ld;
206 182
207 if (!to) 183 if (!to)
208 return; 184 return;
209 ld = tty_ldisc_ref(to); 185 /* tty_buffer_flush(to); FIXME */
210
211 /* The other end is changing discipline */
212 if (!ld)
213 return;
214
215 if (ld->ops->flush_buffer)
216 to->ldisc->ops->flush_buffer(to);
217 tty_ldisc_deref(ld);
218
219 if (to->packet) { 186 if (to->packet) {
220 spin_lock_irqsave(&tty->ctrl_lock, flags); 187 spin_lock_irqsave(&tty->ctrl_lock, flags);
221 tty->ctrl_status |= TIOCPKT_FLUSHWRITE; 188 tty->ctrl_status |= TIOCPKT_FLUSHWRITE;