Commit | Line | Data |
---|---|---|
01e1abb2 | 1 | #include <linux/types.h> |
01e1abb2 | 2 | #include <linux/errno.h> |
8b3ffa17 | 3 | #include <linux/kmod.h> |
01e1abb2 AC |
4 | #include <linux/sched.h> |
5 | #include <linux/interrupt.h> | |
6 | #include <linux/tty.h> | |
7 | #include <linux/tty_driver.h> | |
01e1abb2 | 8 | #include <linux/file.h> |
01e1abb2 AC |
9 | #include <linux/mm.h> |
10 | #include <linux/string.h> | |
11 | #include <linux/slab.h> | |
12 | #include <linux/poll.h> | |
13 | #include <linux/proc_fs.h> | |
14 | #include <linux/init.h> | |
15 | #include <linux/module.h> | |
01e1abb2 AC |
16 | #include <linux/device.h> |
17 | #include <linux/wait.h> | |
18 | #include <linux/bitops.h> | |
01e1abb2 | 19 | #include <linux/seq_file.h> |
01e1abb2 | 20 | #include <linux/uaccess.h> |
0c73c08e | 21 | #include <linux/ratelimit.h> |
01e1abb2 AC |
22 | |
23 | /* | |
24 | * This guards the refcounted line discipline lists. The lock | |
25 | * must be taken with irqs off because there are hangup path | |
26 | * callers who will do ldisc lookups and cannot sleep. | |
27 | */ | |
28 | ||
c9739941 | 29 | static DEFINE_RAW_SPINLOCK(tty_ldisc_lock); |
01e1abb2 AC |
30 | static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait); |
31 | /* Line disc dispatch table */ | |
32 | static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS]; | |
33 | ||
65b77046 LT |
34 | static inline struct tty_ldisc *get_ldisc(struct tty_ldisc *ld) |
35 | { | |
36 | if (ld) | |
37 | atomic_inc(&ld->users); | |
38 | return ld; | |
39 | } | |
40 | ||
cbe9352f | 41 | static void put_ldisc(struct tty_ldisc *ld) |
65b77046 | 42 | { |
cbe9352f LT |
43 | unsigned long flags; |
44 | ||
65b77046 LT |
45 | if (WARN_ON_ONCE(!ld)) |
46 | return; | |
47 | ||
48 | /* | |
49 | * If this is the last user, free the ldisc, and | |
50 | * release the ldisc ops. | |
cbe9352f | 51 | * |
c9739941 | 52 | * We really want an "atomic_dec_and_raw_lock_irqsave()", |
cbe9352f | 53 | * but we don't have it, so this does it by hand. |
65b77046 | 54 | */ |
c9739941 IS |
55 | raw_spin_lock_irqsave(&tty_ldisc_lock, flags); |
56 | if (atomic_dec_and_test(&ld->users)) { | |
65b77046 LT |
57 | struct tty_ldisc_ops *ldo = ld->ops; |
58 | ||
65b77046 LT |
59 | ldo->refcount--; |
60 | module_put(ldo->owner); | |
c9739941 | 61 | raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags); |
cbe9352f LT |
62 | |
63 | kfree(ld); | |
64 | return; | |
65b77046 | 65 | } |
c9739941 | 66 | raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags); |
bd5d7ce9 IS |
67 | |
68 | if (waitqueue_active(&ld->wq_idle)) | |
69 | wake_up(&ld->wq_idle); | |
65b77046 LT |
70 | } |
71 | ||
01e1abb2 AC |
72 | /** |
73 | * tty_register_ldisc - install a line discipline | |
74 | * @disc: ldisc number | |
75 | * @new_ldisc: pointer to the ldisc object | |
76 | * | |
77 | * Installs a new line discipline into the kernel. The discipline | |
78 | * is set up as unreferenced and then made available to the kernel | |
79 | * from this point onwards. | |
80 | * | |
81 | * Locking: | |
82 | * takes tty_ldisc_lock to guard against ldisc races | |
83 | */ | |
84 | ||
85 | int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc) | |
86 | { | |
87 | unsigned long flags; | |
88 | int ret = 0; | |
89 | ||
90 | if (disc < N_TTY || disc >= NR_LDISCS) | |
91 | return -EINVAL; | |
92 | ||
c9739941 | 93 | raw_spin_lock_irqsave(&tty_ldisc_lock, flags); |
01e1abb2 AC |
94 | tty_ldiscs[disc] = new_ldisc; |
95 | new_ldisc->num = disc; | |
96 | new_ldisc->refcount = 0; | |
c9739941 | 97 | raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags); |
01e1abb2 AC |
98 | |
99 | return ret; | |
100 | } | |
101 | EXPORT_SYMBOL(tty_register_ldisc); | |
102 | ||
103 | /** | |
104 | * tty_unregister_ldisc - unload a line discipline | |
105 | * @disc: ldisc number | |
106 | * @new_ldisc: pointer to the ldisc object | |
107 | * | |
108 | * Remove a line discipline from the kernel providing it is not | |
109 | * currently in use. | |
110 | * | |
111 | * Locking: | |
112 | * takes tty_ldisc_lock to guard against ldisc races | |
113 | */ | |
114 | ||
115 | int tty_unregister_ldisc(int disc) | |
116 | { | |
117 | unsigned long flags; | |
118 | int ret = 0; | |
119 | ||
120 | if (disc < N_TTY || disc >= NR_LDISCS) | |
121 | return -EINVAL; | |
122 | ||
c9739941 | 123 | raw_spin_lock_irqsave(&tty_ldisc_lock, flags); |
01e1abb2 AC |
124 | if (tty_ldiscs[disc]->refcount) |
125 | ret = -EBUSY; | |
126 | else | |
127 | tty_ldiscs[disc] = NULL; | |
c9739941 | 128 | raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags); |
01e1abb2 AC |
129 | |
130 | return ret; | |
131 | } | |
132 | EXPORT_SYMBOL(tty_unregister_ldisc); | |
133 | ||
f0de0e8d LT |
134 | static struct tty_ldisc_ops *get_ldops(int disc) |
135 | { | |
136 | unsigned long flags; | |
137 | struct tty_ldisc_ops *ldops, *ret; | |
138 | ||
c9739941 | 139 | raw_spin_lock_irqsave(&tty_ldisc_lock, flags); |
f0de0e8d LT |
140 | ret = ERR_PTR(-EINVAL); |
141 | ldops = tty_ldiscs[disc]; | |
142 | if (ldops) { | |
143 | ret = ERR_PTR(-EAGAIN); | |
144 | if (try_module_get(ldops->owner)) { | |
145 | ldops->refcount++; | |
146 | ret = ldops; | |
147 | } | |
148 | } | |
c9739941 | 149 | raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags); |
f0de0e8d LT |
150 | return ret; |
151 | } | |
152 | ||
153 | static void put_ldops(struct tty_ldisc_ops *ldops) | |
154 | { | |
155 | unsigned long flags; | |
156 | ||
c9739941 | 157 | raw_spin_lock_irqsave(&tty_ldisc_lock, flags); |
f0de0e8d LT |
158 | ldops->refcount--; |
159 | module_put(ldops->owner); | |
c9739941 | 160 | raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags); |
f0de0e8d | 161 | } |
01e1abb2 | 162 | |
01e1abb2 AC |
163 | /** |
164 | * tty_ldisc_get - take a reference to an ldisc | |
165 | * @disc: ldisc number | |
01e1abb2 AC |
166 | * |
167 | * Takes a reference to a line discipline. Deals with refcounts and | |
168 | * module locking counts. Returns NULL if the discipline is not available. | |
169 | * Returns a pointer to the discipline and bumps the ref count if it is | |
170 | * available | |
171 | * | |
172 | * Locking: | |
173 | * takes tty_ldisc_lock to guard against ldisc races | |
174 | */ | |
175 | ||
c65c9bc3 | 176 | static struct tty_ldisc *tty_ldisc_get(int disc) |
01e1abb2 | 177 | { |
c65c9bc3 | 178 | struct tty_ldisc *ld; |
182274f8 | 179 | struct tty_ldisc_ops *ldops; |
01e1abb2 AC |
180 | |
181 | if (disc < N_TTY || disc >= NR_LDISCS) | |
c65c9bc3 | 182 | return ERR_PTR(-EINVAL); |
182274f8 LT |
183 | |
184 | /* | |
185 | * Get the ldisc ops - we may need to request them to be loaded | |
186 | * dynamically and try again. | |
187 | */ | |
188 | ldops = get_ldops(disc); | |
189 | if (IS_ERR(ldops)) { | |
01e1abb2 | 190 | request_module("tty-ldisc-%d", disc); |
182274f8 LT |
191 | ldops = get_ldops(disc); |
192 | if (IS_ERR(ldops)) | |
193 | return ERR_CAST(ldops); | |
194 | } | |
195 | ||
196 | ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL); | |
197 | if (ld == NULL) { | |
198 | put_ldops(ldops); | |
199 | return ERR_PTR(-ENOMEM); | |
01e1abb2 | 200 | } |
182274f8 LT |
201 | |
202 | ld->ops = ldops; | |
203 | atomic_set(&ld->users, 1); | |
1541f845 IS |
204 | init_waitqueue_head(&ld->wq_idle); |
205 | ||
c65c9bc3 | 206 | return ld; |
01e1abb2 AC |
207 | } |
208 | ||
852e99d2 | 209 | static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos) |
01e1abb2 AC |
210 | { |
211 | return (*pos < NR_LDISCS) ? pos : NULL; | |
212 | } | |
213 | ||
852e99d2 | 214 | static void *tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos) |
01e1abb2 AC |
215 | { |
216 | (*pos)++; | |
217 | return (*pos < NR_LDISCS) ? pos : NULL; | |
218 | } | |
219 | ||
220 | static void tty_ldiscs_seq_stop(struct seq_file *m, void *v) | |
221 | { | |
222 | } | |
223 | ||
224 | static int tty_ldiscs_seq_show(struct seq_file *m, void *v) | |
225 | { | |
226 | int i = *(loff_t *)v; | |
f0de0e8d | 227 | struct tty_ldisc_ops *ldops; |
852e99d2 | 228 | |
f0de0e8d LT |
229 | ldops = get_ldops(i); |
230 | if (IS_ERR(ldops)) | |
01e1abb2 | 231 | return 0; |
f0de0e8d LT |
232 | seq_printf(m, "%-10s %2d\n", ldops->name ? ldops->name : "???", i); |
233 | put_ldops(ldops); | |
01e1abb2 AC |
234 | return 0; |
235 | } | |
236 | ||
237 | static const struct seq_operations tty_ldiscs_seq_ops = { | |
238 | .start = tty_ldiscs_seq_start, | |
239 | .next = tty_ldiscs_seq_next, | |
240 | .stop = tty_ldiscs_seq_stop, | |
241 | .show = tty_ldiscs_seq_show, | |
242 | }; | |
243 | ||
244 | static int proc_tty_ldiscs_open(struct inode *inode, struct file *file) | |
245 | { | |
246 | return seq_open(file, &tty_ldiscs_seq_ops); | |
247 | } | |
248 | ||
249 | const struct file_operations tty_ldiscs_proc_fops = { | |
250 | .owner = THIS_MODULE, | |
251 | .open = proc_tty_ldiscs_open, | |
252 | .read = seq_read, | |
253 | .llseek = seq_lseek, | |
254 | .release = seq_release, | |
255 | }; | |
256 | ||
257 | /** | |
258 | * tty_ldisc_assign - set ldisc on a tty | |
259 | * @tty: tty to assign | |
260 | * @ld: line discipline | |
261 | * | |
262 | * Install an instance of a line discipline into a tty structure. The | |
8d2ead74 | 263 | * ldisc must have a reference count above zero to ensure it remains. |
01e1abb2 AC |
264 | * The tty instance refcount starts at zero. |
265 | * | |
266 | * Locking: | |
267 | * Caller must hold references | |
268 | */ | |
269 | ||
270 | static void tty_ldisc_assign(struct tty_struct *tty, struct tty_ldisc *ld) | |
271 | { | |
c65c9bc3 | 272 | tty->ldisc = ld; |
01e1abb2 AC |
273 | } |
274 | ||
275 | /** | |
276 | * tty_ldisc_try - internal helper | |
277 | * @tty: the tty | |
278 | * | |
279 | * Make a single attempt to grab and bump the refcount on | |
280 | * the tty ldisc. Return 0 on failure or 1 on success. This is | |
281 | * used to implement both the waiting and non waiting versions | |
282 | * of tty_ldisc_ref | |
283 | * | |
284 | * Locking: takes tty_ldisc_lock | |
285 | */ | |
286 | ||
65b77046 | 287 | static struct tty_ldisc *tty_ldisc_try(struct tty_struct *tty) |
01e1abb2 AC |
288 | { |
289 | unsigned long flags; | |
290 | struct tty_ldisc *ld; | |
01e1abb2 | 291 | |
c9739941 | 292 | raw_spin_lock_irqsave(&tty_ldisc_lock, flags); |
65b77046 LT |
293 | ld = NULL; |
294 | if (test_bit(TTY_LDISC, &tty->flags)) | |
295 | ld = get_ldisc(tty->ldisc); | |
c9739941 | 296 | raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags); |
65b77046 | 297 | return ld; |
01e1abb2 AC |
298 | } |
299 | ||
300 | /** | |
301 | * tty_ldisc_ref_wait - wait for the tty ldisc | |
302 | * @tty: tty device | |
303 | * | |
304 | * Dereference the line discipline for the terminal and take a | |
305 | * reference to it. If the line discipline is in flux then | |
306 | * wait patiently until it changes. | |
307 | * | |
308 | * Note: Must not be called from an IRQ/timer context. The caller | |
309 | * must also be careful not to hold other locks that will deadlock | |
310 | * against a discipline change, such as an existing ldisc reference | |
311 | * (which we check for) | |
312 | * | |
313 | * Locking: call functions take tty_ldisc_lock | |
314 | */ | |
315 | ||
316 | struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty) | |
317 | { | |
65b77046 LT |
318 | struct tty_ldisc *ld; |
319 | ||
01e1abb2 | 320 | /* wait_event is a macro */ |
65b77046 LT |
321 | wait_event(tty_ldisc_wait, (ld = tty_ldisc_try(tty)) != NULL); |
322 | return ld; | |
01e1abb2 | 323 | } |
01e1abb2 AC |
324 | EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait); |
325 | ||
326 | /** | |
327 | * tty_ldisc_ref - get the tty ldisc | |
328 | * @tty: tty device | |
329 | * | |
330 | * Dereference the line discipline for the terminal and take a | |
331 | * reference to it. If the line discipline is in flux then | |
332 | * return NULL. Can be called from IRQ and timer functions. | |
333 | * | |
334 | * Locking: called functions take tty_ldisc_lock | |
335 | */ | |
336 | ||
337 | struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty) | |
338 | { | |
65b77046 | 339 | return tty_ldisc_try(tty); |
01e1abb2 | 340 | } |
01e1abb2 AC |
341 | EXPORT_SYMBOL_GPL(tty_ldisc_ref); |
342 | ||
343 | /** | |
344 | * tty_ldisc_deref - free a tty ldisc reference | |
345 | * @ld: reference to free up | |
346 | * | |
347 | * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May | |
348 | * be called in IRQ context. | |
349 | * | |
350 | * Locking: takes tty_ldisc_lock | |
351 | */ | |
352 | ||
353 | void tty_ldisc_deref(struct tty_ldisc *ld) | |
354 | { | |
65b77046 | 355 | put_ldisc(ld); |
01e1abb2 | 356 | } |
01e1abb2 AC |
357 | EXPORT_SYMBOL_GPL(tty_ldisc_deref); |
358 | ||
65b77046 LT |
359 | static inline void tty_ldisc_put(struct tty_ldisc *ld) |
360 | { | |
361 | put_ldisc(ld); | |
362 | } | |
363 | ||
01e1abb2 AC |
364 | /** |
365 | * tty_ldisc_enable - allow ldisc use | |
366 | * @tty: terminal to activate ldisc on | |
367 | * | |
368 | * Set the TTY_LDISC flag when the line discipline can be called | |
c9b3976e AC |
369 | * again. Do necessary wakeups for existing sleepers. Clear the LDISC |
370 | * changing flag to indicate any ldisc change is now over. | |
01e1abb2 | 371 | * |
c9b3976e AC |
372 | * Note: nobody should set the TTY_LDISC bit except via this function. |
373 | * Clearing directly is allowed. | |
01e1abb2 AC |
374 | */ |
375 | ||
d9121566 | 376 | static void tty_ldisc_enable(struct tty_struct *tty) |
01e1abb2 | 377 | { |
21622939 | 378 | clear_bit(TTY_LDISC_HALTED, &tty->flags); |
01e1abb2 | 379 | set_bit(TTY_LDISC, &tty->flags); |
c9b3976e | 380 | clear_bit(TTY_LDISC_CHANGING, &tty->flags); |
01e1abb2 AC |
381 | wake_up(&tty_ldisc_wait); |
382 | } | |
383 | ||
f2c4c65c AC |
384 | /** |
385 | * tty_ldisc_flush - flush line discipline queue | |
386 | * @tty: tty | |
387 | * | |
388 | * Flush the line discipline queue (if any) for this tty. If there | |
389 | * is no line discipline active this is a no-op. | |
390 | */ | |
391 | ||
392 | void tty_ldisc_flush(struct tty_struct *tty) | |
393 | { | |
394 | struct tty_ldisc *ld = tty_ldisc_ref(tty); | |
395 | if (ld) { | |
396 | if (ld->ops->flush_buffer) | |
397 | ld->ops->flush_buffer(tty); | |
398 | tty_ldisc_deref(ld); | |
399 | } | |
400 | tty_buffer_flush(tty); | |
401 | } | |
f2c4c65c AC |
402 | EXPORT_SYMBOL_GPL(tty_ldisc_flush); |
403 | ||
01e1abb2 AC |
404 | /** |
405 | * tty_set_termios_ldisc - set ldisc field | |
406 | * @tty: tty structure | |
407 | * @num: line discipline number | |
408 | * | |
409 | * This is probably overkill for real world processors but | |
410 | * they are not on hot paths so a little discipline won't do | |
411 | * any harm. | |
412 | * | |
413 | * Locking: takes termios_mutex | |
414 | */ | |
415 | ||
416 | static void tty_set_termios_ldisc(struct tty_struct *tty, int num) | |
417 | { | |
418 | mutex_lock(&tty->termios_mutex); | |
adc8d746 | 419 | tty->termios.c_line = num; |
01e1abb2 AC |
420 | mutex_unlock(&tty->termios_mutex); |
421 | } | |
422 | ||
c65c9bc3 AC |
423 | /** |
424 | * tty_ldisc_open - open a line discipline | |
425 | * @tty: tty we are opening the ldisc on | |
426 | * @ld: discipline to open | |
427 | * | |
428 | * A helper opening method. Also a convenient debugging and check | |
429 | * point. | |
ec79d605 AB |
430 | * |
431 | * Locking: always called with BTM already held. | |
c65c9bc3 AC |
432 | */ |
433 | ||
434 | static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld) | |
435 | { | |
436 | WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags)); | |
f18f9498 AC |
437 | if (ld->ops->open) { |
438 | int ret; | |
ec79d605 | 439 | /* BTM here locks versus a hangup event */ |
f18f9498 | 440 | ret = ld->ops->open(tty); |
7f90cfc5 JS |
441 | if (ret) |
442 | clear_bit(TTY_LDISC_OPEN, &tty->flags); | |
f18f9498 AC |
443 | return ret; |
444 | } | |
c65c9bc3 AC |
445 | return 0; |
446 | } | |
447 | ||
448 | /** | |
449 | * tty_ldisc_close - close a line discipline | |
450 | * @tty: tty we are opening the ldisc on | |
451 | * @ld: discipline to close | |
452 | * | |
453 | * A helper close method. Also a convenient debugging and check | |
454 | * point. | |
455 | */ | |
456 | ||
457 | static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld) | |
458 | { | |
459 | WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags)); | |
460 | clear_bit(TTY_LDISC_OPEN, &tty->flags); | |
461 | if (ld->ops->close) | |
462 | ld->ops->close(tty); | |
463 | } | |
01e1abb2 AC |
464 | |
465 | /** | |
466 | * tty_ldisc_restore - helper for tty ldisc change | |
467 | * @tty: tty to recover | |
468 | * @old: previous ldisc | |
469 | * | |
470 | * Restore the previous line discipline or N_TTY when a line discipline | |
471 | * change fails due to an open error | |
472 | */ | |
473 | ||
474 | static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old) | |
475 | { | |
476 | char buf[64]; | |
c65c9bc3 AC |
477 | struct tty_ldisc *new_ldisc; |
478 | int r; | |
01e1abb2 AC |
479 | |
480 | /* There is an outstanding reference here so this is safe */ | |
c65c9bc3 AC |
481 | old = tty_ldisc_get(old->ops->num); |
482 | WARN_ON(IS_ERR(old)); | |
01e1abb2 AC |
483 | tty_ldisc_assign(tty, old); |
484 | tty_set_termios_ldisc(tty, old->ops->num); | |
c65c9bc3 AC |
485 | if (tty_ldisc_open(tty, old) < 0) { |
486 | tty_ldisc_put(old); | |
01e1abb2 | 487 | /* This driver is always present */ |
852e99d2 | 488 | new_ldisc = tty_ldisc_get(N_TTY); |
c65c9bc3 | 489 | if (IS_ERR(new_ldisc)) |
01e1abb2 | 490 | panic("n_tty: get"); |
c65c9bc3 | 491 | tty_ldisc_assign(tty, new_ldisc); |
01e1abb2 | 492 | tty_set_termios_ldisc(tty, N_TTY); |
c65c9bc3 AC |
493 | r = tty_ldisc_open(tty, new_ldisc); |
494 | if (r < 0) | |
495 | panic("Couldn't open N_TTY ldisc for " | |
496 | "%s --- error %d.", | |
497 | tty_name(tty, buf), r); | |
01e1abb2 AC |
498 | } |
499 | } | |
500 | ||
100eeae2 JS |
501 | /** |
502 | * tty_ldisc_wait_idle - wait for the ldisc to become idle | |
503 | * @tty: tty to wait for | |
df92d056 | 504 | * @timeout: for how long to wait at most |
100eeae2 JS |
505 | * |
506 | * Wait for the line discipline to become idle. The discipline must | |
507 | * have been halted for this to guarantee it remains idle. | |
508 | */ | |
df92d056 | 509 | static int tty_ldisc_wait_idle(struct tty_struct *tty, long timeout) |
100eeae2 | 510 | { |
df92d056 | 511 | long ret; |
1541f845 | 512 | ret = wait_event_timeout(tty->ldisc->wq_idle, |
df92d056 | 513 | atomic_read(&tty->ldisc->users) == 1, timeout); |
100eeae2 JS |
514 | return ret > 0 ? 0 : -EBUSY; |
515 | } | |
516 | ||
11cf48ea PH |
517 | /** |
518 | * tty_ldisc_halt - shut down the line discipline | |
519 | * @tty: tty device | |
f4cf7a38 | 520 | * @o_tty: paired pty device (can be NULL) |
cf528476 | 521 | * @timeout: # of jiffies to wait for ldisc refs to be released |
11cf48ea | 522 | * |
f4cf7a38 PH |
523 | * Shut down the line discipline and work queue for this tty device and |
524 | * its paired pty (if exists). Clearing the TTY_LDISC flag ensures | |
4f98d467 PH |
525 | * no further references can be obtained, while waiting for existing |
526 | * references to be released ensures no more data is fed to the ldisc. | |
cf528476 | 527 | * |
11cf48ea PH |
528 | * You need to do a 'flush_scheduled_work()' (outside the ldisc_mutex) |
529 | * in order to make sure any currently executing ldisc work is also | |
530 | * flushed. | |
531 | */ | |
532 | ||
f4cf7a38 | 533 | static int tty_ldisc_halt(struct tty_struct *tty, struct tty_struct *o_tty, |
4f98d467 | 534 | long timeout) |
11cf48ea | 535 | { |
4f98d467 | 536 | int retval; |
cf528476 | 537 | |
11cf48ea | 538 | clear_bit(TTY_LDISC, &tty->flags); |
f4cf7a38 PH |
539 | if (o_tty) |
540 | clear_bit(TTY_LDISC, &o_tty->flags); | |
541 | ||
cf528476 | 542 | retval = tty_ldisc_wait_idle(tty, timeout); |
f4cf7a38 PH |
543 | if (!retval && o_tty) |
544 | retval = tty_ldisc_wait_idle(o_tty, timeout); | |
cf528476 PH |
545 | if (retval) |
546 | return retval; | |
547 | ||
11cf48ea | 548 | set_bit(TTY_LDISC_HALTED, &tty->flags); |
4f98d467 | 549 | if (o_tty) |
f4cf7a38 | 550 | set_bit(TTY_LDISC_HALTED, &o_tty->flags); |
f4cf7a38 | 551 | |
cf528476 | 552 | return 0; |
11cf48ea PH |
553 | } |
554 | ||
168942c9 | 555 | /** |
76bc35e7 PH |
556 | * tty_ldisc_hangup_halt - halt the line discipline for hangup |
557 | * @tty: tty being hung up | |
168942c9 | 558 | * |
76bc35e7 PH |
559 | * Shut down the line discipline and work queue for the tty device |
560 | * being hungup. Clear the TTY_LDISC flag to ensure no further | |
4f98d467 PH |
561 | * references can be obtained and wait for remaining references to be |
562 | * released to ensure no more data is fed to this ldisc. | |
168942c9 | 563 | * Caller must hold legacy and ->ldisc_mutex. |
2276ad97 PH |
564 | * |
565 | * NB: tty_set_ldisc() is prevented from changing the ldisc concurrently | |
566 | * with this function by checking the TTY_HUPPING flag. | |
168942c9 | 567 | */ |
76bc35e7 | 568 | static bool tty_ldisc_hangup_halt(struct tty_struct *tty) |
168942c9 | 569 | { |
2276ad97 PH |
570 | char cur_n[TASK_COMM_LEN], tty_n[64]; |
571 | long timeout = 3 * HZ; | |
572 | ||
76bc35e7 PH |
573 | clear_bit(TTY_LDISC, &tty->flags); |
574 | ||
2276ad97 PH |
575 | if (tty->ldisc) { /* Not yet closed */ |
576 | tty_unlock(tty); | |
577 | ||
578 | while (tty_ldisc_wait_idle(tty, timeout) == -EBUSY) { | |
579 | timeout = MAX_SCHEDULE_TIMEOUT; | |
580 | printk_ratelimited(KERN_WARNING | |
581 | "%s: waiting (%s) for %s took too long, but we keep waiting...\n", | |
582 | __func__, get_task_comm(cur_n, current), | |
583 | tty_name(tty, tty_n)); | |
168942c9 | 584 | } |
76bc35e7 | 585 | |
76bc35e7 PH |
586 | set_bit(TTY_LDISC_HALTED, &tty->flags); |
587 | ||
2276ad97 PH |
588 | /* must reacquire both locks and preserve lock order */ |
589 | mutex_unlock(&tty->ldisc_mutex); | |
590 | tty_lock(tty); | |
591 | mutex_lock(&tty->ldisc_mutex); | |
168942c9 PH |
592 | } |
593 | return !!tty->ldisc; | |
594 | } | |
595 | ||
01e1abb2 AC |
596 | /** |
597 | * tty_set_ldisc - set line discipline | |
598 | * @tty: the terminal to set | |
599 | * @ldisc: the line discipline | |
600 | * | |
601 | * Set the discipline of a tty line. Must be called from a process | |
c65c9bc3 AC |
602 | * context. The ldisc change logic has to protect itself against any |
603 | * overlapping ldisc change (including on the other end of pty pairs), | |
604 | * the close of one side of a tty/pty pair, and eventually hangup. | |
01e1abb2 | 605 | * |
c65c9bc3 | 606 | * Locking: takes tty_ldisc_lock, termios_mutex |
01e1abb2 AC |
607 | */ |
608 | ||
609 | int tty_set_ldisc(struct tty_struct *tty, int ldisc) | |
610 | { | |
611 | int retval; | |
c65c9bc3 | 612 | struct tty_ldisc *o_ldisc, *new_ldisc; |
01e1abb2 AC |
613 | struct tty_struct *o_tty; |
614 | ||
c65c9bc3 AC |
615 | new_ldisc = tty_ldisc_get(ldisc); |
616 | if (IS_ERR(new_ldisc)) | |
617 | return PTR_ERR(new_ldisc); | |
01e1abb2 | 618 | |
89c8d91e | 619 | tty_lock(tty); |
01e1abb2 | 620 | /* |
c65c9bc3 AC |
621 | * We need to look at the tty locking here for pty/tty pairs |
622 | * when both sides try to change in parallel. | |
01e1abb2 AC |
623 | */ |
624 | ||
c65c9bc3 AC |
625 | o_tty = tty->link; /* o_tty is the pty side or NULL */ |
626 | ||
01e1abb2 | 627 | |
c65c9bc3 AC |
628 | /* |
629 | * Check the no-op case | |
630 | */ | |
631 | ||
632 | if (tty->ldisc->ops->num == ldisc) { | |
89c8d91e | 633 | tty_unlock(tty); |
c65c9bc3 | 634 | tty_ldisc_put(new_ldisc); |
01e1abb2 AC |
635 | return 0; |
636 | } | |
637 | ||
89c8d91e | 638 | tty_unlock(tty); |
c65c9bc3 AC |
639 | /* |
640 | * Problem: What do we do if this blocks ? | |
641 | * We could deadlock here | |
642 | */ | |
643 | ||
644 | tty_wait_until_sent(tty, 0); | |
645 | ||
89c8d91e | 646 | tty_lock(tty); |
c65c9bc3 AC |
647 | mutex_lock(&tty->ldisc_mutex); |
648 | ||
649 | /* | |
650 | * We could be midstream of another ldisc change which has | |
651 | * dropped the lock during processing. If so we need to wait. | |
652 | */ | |
653 | ||
654 | while (test_bit(TTY_LDISC_CHANGING, &tty->flags)) { | |
655 | mutex_unlock(&tty->ldisc_mutex); | |
89c8d91e | 656 | tty_unlock(tty); |
c65c9bc3 AC |
657 | wait_event(tty_ldisc_wait, |
658 | test_bit(TTY_LDISC_CHANGING, &tty->flags) == 0); | |
89c8d91e | 659 | tty_lock(tty); |
c65c9bc3 AC |
660 | mutex_lock(&tty->ldisc_mutex); |
661 | } | |
eeb89d91 | 662 | |
c65c9bc3 | 663 | set_bit(TTY_LDISC_CHANGING, &tty->flags); |
852e99d2 | 664 | |
01e1abb2 AC |
665 | /* |
666 | * No more input please, we are switching. The new ldisc | |
667 | * will update this value in the ldisc open function | |
668 | */ | |
669 | ||
670 | tty->receive_room = 0; | |
671 | ||
672 | o_ldisc = tty->ldisc; | |
eeb89d91 | 673 | |
89c8d91e | 674 | tty_unlock(tty); |
01e1abb2 AC |
675 | /* |
676 | * Make sure we don't change while someone holds a | |
677 | * reference to the line discipline. The TTY_LDISC bit | |
678 | * prevents anyone taking a reference once it is clear. | |
679 | * We need the lock to avoid racing reference takers. | |
c9b3976e AC |
680 | * |
681 | * We must clear the TTY_LDISC bit here to avoid a livelock | |
682 | * with a userspace app continually trying to use the tty in | |
683 | * parallel to the change and re-referencing the tty. | |
01e1abb2 AC |
684 | */ |
685 | ||
4f98d467 | 686 | retval = tty_ldisc_halt(tty, o_tty, 5 * HZ); |
01e1abb2 | 687 | |
01e1abb2 | 688 | /* |
a2965b7b | 689 | * Wait for hangup to complete, if pending. |
c65c9bc3 | 690 | * We must drop the mutex here in case a hangup is also in process. |
01e1abb2 | 691 | */ |
c65c9bc3 AC |
692 | |
693 | mutex_unlock(&tty->ldisc_mutex); | |
694 | ||
a2965b7b | 695 | flush_work(&tty->hangup_work); |
c65c9bc3 | 696 | |
89c8d91e | 697 | tty_lock(tty); |
60af22d2 | 698 | mutex_lock(&tty->ldisc_mutex); |
100eeae2 JS |
699 | |
700 | /* handle wait idle failure locked */ | |
701 | if (retval) { | |
702 | tty_ldisc_put(new_ldisc); | |
703 | goto enable; | |
704 | } | |
705 | ||
40c9f61e | 706 | if (test_bit(TTY_HUPPING, &tty->flags)) { |
c65c9bc3 AC |
707 | /* We were raced by the hangup method. It will have stomped |
708 | the ldisc data and closed the ldisc down */ | |
709 | clear_bit(TTY_LDISC_CHANGING, &tty->flags); | |
710 | mutex_unlock(&tty->ldisc_mutex); | |
711 | tty_ldisc_put(new_ldisc); | |
89c8d91e | 712 | tty_unlock(tty); |
c65c9bc3 AC |
713 | return -EIO; |
714 | } | |
715 | ||
01e1abb2 | 716 | /* Shutdown the current discipline. */ |
c65c9bc3 | 717 | tty_ldisc_close(tty, o_ldisc); |
01e1abb2 AC |
718 | |
719 | /* Now set up the new line discipline. */ | |
c65c9bc3 | 720 | tty_ldisc_assign(tty, new_ldisc); |
01e1abb2 | 721 | tty_set_termios_ldisc(tty, ldisc); |
c65c9bc3 AC |
722 | |
723 | retval = tty_ldisc_open(tty, new_ldisc); | |
01e1abb2 | 724 | if (retval < 0) { |
c65c9bc3 AC |
725 | /* Back to the old one or N_TTY if we can't */ |
726 | tty_ldisc_put(new_ldisc); | |
727 | tty_ldisc_restore(tty, o_ldisc); | |
01e1abb2 | 728 | } |
c65c9bc3 | 729 | |
01e1abb2 AC |
730 | /* At this point we hold a reference to the new ldisc and a |
731 | a reference to the old ldisc. If we ended up flipping back | |
732 | to the existing ldisc we have two references to it */ | |
733 | ||
c65c9bc3 | 734 | if (tty->ldisc->ops->num != o_ldisc->ops->num && tty->ops->set_ldisc) |
01e1abb2 AC |
735 | tty->ops->set_ldisc(tty); |
736 | ||
c65c9bc3 | 737 | tty_ldisc_put(o_ldisc); |
01e1abb2 | 738 | |
100eeae2 | 739 | enable: |
01e1abb2 | 740 | /* |
c65c9bc3 | 741 | * Allow ldisc referencing to occur again |
01e1abb2 AC |
742 | */ |
743 | ||
744 | tty_ldisc_enable(tty); | |
745 | if (o_tty) | |
746 | tty_ldisc_enable(o_tty); | |
747 | ||
c65c9bc3 | 748 | /* Restart the work queue in case no characters kick it off. Safe if |
01e1abb2 | 749 | already running */ |
4f98d467 PH |
750 | schedule_work(&tty->port->buf.work); |
751 | if (o_tty) | |
ecbbfd44 | 752 | schedule_work(&o_tty->port->buf.work); |
4f98d467 | 753 | |
c65c9bc3 | 754 | mutex_unlock(&tty->ldisc_mutex); |
89c8d91e | 755 | tty_unlock(tty); |
01e1abb2 AC |
756 | return retval; |
757 | } | |
758 | ||
c65c9bc3 AC |
759 | /** |
760 | * tty_reset_termios - reset terminal state | |
761 | * @tty: tty to reset | |
762 | * | |
763 | * Restore a terminal to the driver default state. | |
764 | */ | |
765 | ||
766 | static void tty_reset_termios(struct tty_struct *tty) | |
767 | { | |
768 | mutex_lock(&tty->termios_mutex); | |
adc8d746 AC |
769 | tty->termios = tty->driver->init_termios; |
770 | tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios); | |
771 | tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios); | |
c65c9bc3 AC |
772 | mutex_unlock(&tty->termios_mutex); |
773 | } | |
774 | ||
775 | ||
776 | /** | |
777 | * tty_ldisc_reinit - reinitialise the tty ldisc | |
778 | * @tty: tty to reinit | |
638b9648 | 779 | * @ldisc: line discipline to reinitialize |
c65c9bc3 | 780 | * |
638b9648 AC |
781 | * Switch the tty to a line discipline and leave the ldisc |
782 | * state closed | |
c65c9bc3 AC |
783 | */ |
784 | ||
1c95ba1e | 785 | static int tty_ldisc_reinit(struct tty_struct *tty, int ldisc) |
c65c9bc3 | 786 | { |
1c95ba1e PR |
787 | struct tty_ldisc *ld = tty_ldisc_get(ldisc); |
788 | ||
789 | if (IS_ERR(ld)) | |
790 | return -1; | |
c65c9bc3 AC |
791 | |
792 | tty_ldisc_close(tty, tty->ldisc); | |
793 | tty_ldisc_put(tty->ldisc); | |
794 | tty->ldisc = NULL; | |
795 | /* | |
796 | * Switch the line discipline back | |
797 | */ | |
c65c9bc3 | 798 | tty_ldisc_assign(tty, ld); |
638b9648 | 799 | tty_set_termios_ldisc(tty, ldisc); |
1c95ba1e PR |
800 | |
801 | return 0; | |
c65c9bc3 AC |
802 | } |
803 | ||
804 | /** | |
805 | * tty_ldisc_hangup - hangup ldisc reset | |
806 | * @tty: tty being hung up | |
807 | * | |
808 | * Some tty devices reset their termios when they receive a hangup | |
809 | * event. In that situation we must also switch back to N_TTY properly | |
810 | * before we reset the termios data. | |
811 | * | |
812 | * Locking: We can take the ldisc mutex as the rest of the code is | |
813 | * careful to allow for this. | |
814 | * | |
815 | * In the pty pair case this occurs in the close() path of the | |
816 | * tty itself so we must be careful about locking rules. | |
817 | */ | |
818 | ||
819 | void tty_ldisc_hangup(struct tty_struct *tty) | |
820 | { | |
821 | struct tty_ldisc *ld; | |
638b9648 AC |
822 | int reset = tty->driver->flags & TTY_DRIVER_RESET_TERMIOS; |
823 | int err = 0; | |
c65c9bc3 AC |
824 | |
825 | /* | |
826 | * FIXME! What are the locking issues here? This may me overdoing | |
827 | * things... This question is especially important now that we've | |
828 | * removed the irqlock. | |
829 | */ | |
830 | ld = tty_ldisc_ref(tty); | |
831 | if (ld != NULL) { | |
832 | /* We may have no line discipline at this point */ | |
833 | if (ld->ops->flush_buffer) | |
834 | ld->ops->flush_buffer(tty); | |
835 | tty_driver_flush_buffer(tty); | |
836 | if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) && | |
837 | ld->ops->write_wakeup) | |
838 | ld->ops->write_wakeup(tty); | |
839 | if (ld->ops->hangup) | |
840 | ld->ops->hangup(tty); | |
841 | tty_ldisc_deref(ld); | |
842 | } | |
843 | /* | |
844 | * FIXME: Once we trust the LDISC code better we can wait here for | |
845 | * ldisc completion and fix the driver call race | |
846 | */ | |
847 | wake_up_interruptible_poll(&tty->write_wait, POLLOUT); | |
848 | wake_up_interruptible_poll(&tty->read_wait, POLLIN); | |
849 | /* | |
850 | * Shutdown the current line discipline, and reset it to | |
638b9648 AC |
851 | * N_TTY if need be. |
852 | * | |
853 | * Avoid racing set_ldisc or tty_ldisc_release | |
c65c9bc3 | 854 | */ |
638b9648 | 855 | mutex_lock(&tty->ldisc_mutex); |
60af22d2 | 856 | |
638b9648 AC |
857 | /* At this point we have a closed ldisc and we want to |
858 | reopen it. We could defer this to the next open but | |
859 | it means auditing a lot of other paths so this is | |
860 | a FIXME */ | |
76bc35e7 | 861 | if (tty_ldisc_hangup_halt(tty)) { |
638b9648 | 862 | if (reset == 0) { |
1c95ba1e | 863 | |
adc8d746 | 864 | if (!tty_ldisc_reinit(tty, tty->termios.c_line)) |
1c95ba1e PR |
865 | err = tty_ldisc_open(tty, tty->ldisc); |
866 | else | |
867 | err = 1; | |
638b9648 AC |
868 | } |
869 | /* If the re-open fails or we reset then go to N_TTY. The | |
870 | N_TTY open cannot fail */ | |
871 | if (reset || err) { | |
1c95ba1e | 872 | BUG_ON(tty_ldisc_reinit(tty, N_TTY)); |
c8d50041 | 873 | WARN_ON(tty_ldisc_open(tty, tty->ldisc)); |
c8d50041 | 874 | } |
638b9648 | 875 | tty_ldisc_enable(tty); |
c65c9bc3 | 876 | } |
638b9648 AC |
877 | mutex_unlock(&tty->ldisc_mutex); |
878 | if (reset) | |
879 | tty_reset_termios(tty); | |
c65c9bc3 | 880 | } |
01e1abb2 AC |
881 | |
882 | /** | |
883 | * tty_ldisc_setup - open line discipline | |
884 | * @tty: tty being shut down | |
885 | * @o_tty: pair tty for pty/tty pairs | |
886 | * | |
887 | * Called during the initial open of a tty/pty pair in order to set up the | |
c65c9bc3 AC |
888 | * line disciplines and bind them to the tty. This has no locking issues |
889 | * as the device isn't yet active. | |
01e1abb2 AC |
890 | */ |
891 | ||
892 | int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty) | |
893 | { | |
c65c9bc3 | 894 | struct tty_ldisc *ld = tty->ldisc; |
01e1abb2 AC |
895 | int retval; |
896 | ||
c65c9bc3 AC |
897 | retval = tty_ldisc_open(tty, ld); |
898 | if (retval) | |
899 | return retval; | |
900 | ||
901 | if (o_tty) { | |
902 | retval = tty_ldisc_open(o_tty, o_tty->ldisc); | |
01e1abb2 | 903 | if (retval) { |
c65c9bc3 | 904 | tty_ldisc_close(tty, ld); |
01e1abb2 AC |
905 | return retval; |
906 | } | |
907 | tty_ldisc_enable(o_tty); | |
908 | } | |
909 | tty_ldisc_enable(tty); | |
910 | return 0; | |
911 | } | |
89c8d91e AC |
912 | |
913 | static void tty_ldisc_kill(struct tty_struct *tty) | |
914 | { | |
915 | mutex_lock(&tty->ldisc_mutex); | |
916 | /* | |
917 | * Now kill off the ldisc | |
918 | */ | |
919 | tty_ldisc_close(tty, tty->ldisc); | |
920 | tty_ldisc_put(tty->ldisc); | |
921 | /* Force an oops if we mess this up */ | |
922 | tty->ldisc = NULL; | |
923 | ||
924 | /* Ensure the next open requests the N_TTY ldisc */ | |
925 | tty_set_termios_ldisc(tty, N_TTY); | |
926 | mutex_unlock(&tty->ldisc_mutex); | |
927 | } | |
928 | ||
01e1abb2 AC |
929 | /** |
930 | * tty_ldisc_release - release line discipline | |
931 | * @tty: tty being shut down | |
932 | * @o_tty: pair tty for pty/tty pairs | |
933 | * | |
852e99d2 AC |
934 | * Called during the final close of a tty/pty pair in order to shut down |
935 | * the line discpline layer. On exit the ldisc assigned is N_TTY and the | |
c65c9bc3 | 936 | * ldisc has not been opened. |
01e1abb2 AC |
937 | */ |
938 | ||
939 | void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty) | |
940 | { | |
01e1abb2 | 941 | /* |
a2965b7b PH |
942 | * Shutdown this line discipline. As this is the final close, |
943 | * it does not race with the set_ldisc code path. | |
01e1abb2 | 944 | */ |
01e1abb2 | 945 | |
4f98d467 | 946 | tty_ldisc_halt(tty, o_tty, MAX_SCHEDULE_TIMEOUT); |
d155255a | 947 | |
852e4a81 | 948 | tty_lock_pair(tty, o_tty); |
6d31a88c | 949 | /* This will need doing differently if we need to lock */ |
89c8d91e | 950 | tty_ldisc_kill(tty); |
c65c9bc3 | 951 | if (o_tty) |
89c8d91e | 952 | tty_ldisc_kill(o_tty); |
aef29bc2 | 953 | |
89c8d91e | 954 | tty_unlock_pair(tty, o_tty); |
aef29bc2 AC |
955 | /* And the memory resources remaining (buffers, termios) will be |
956 | disposed of when the kref hits zero */ | |
01e1abb2 AC |
957 | } |
958 | ||
959 | /** | |
960 | * tty_ldisc_init - ldisc setup for new tty | |
961 | * @tty: tty being allocated | |
962 | * | |
963 | * Set up the line discipline objects for a newly allocated tty. Note that | |
964 | * the tty structure is not completely set up when this call is made. | |
965 | */ | |
966 | ||
967 | void tty_ldisc_init(struct tty_struct *tty) | |
968 | { | |
c65c9bc3 AC |
969 | struct tty_ldisc *ld = tty_ldisc_get(N_TTY); |
970 | if (IS_ERR(ld)) | |
01e1abb2 | 971 | panic("n_tty: init_tty"); |
c65c9bc3 | 972 | tty_ldisc_assign(tty, ld); |
01e1abb2 AC |
973 | } |
974 | ||
6716671d JS |
975 | /** |
976 | * tty_ldisc_init - ldisc cleanup for new tty | |
977 | * @tty: tty that was allocated recently | |
978 | * | |
979 | * The tty structure must not becompletely set up (tty_ldisc_setup) when | |
980 | * this call is made. | |
981 | */ | |
982 | void tty_ldisc_deinit(struct tty_struct *tty) | |
983 | { | |
984 | put_ldisc(tty->ldisc); | |
985 | tty_ldisc_assign(tty, NULL); | |
986 | } | |
987 | ||
01e1abb2 AC |
988 | void tty_ldisc_begin(void) |
989 | { | |
990 | /* Setup the default TTY line discipline. */ | |
991 | (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY); | |
992 | } |