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