1 #include <linux/types.h>
2 #include <linux/errno.h>
3 #include <linux/kmod.h>
4 #include <linux/sched.h>
5 #include <linux/interrupt.h>
7 #include <linux/tty_driver.h>
8 #include <linux/file.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/module.h>
15 #include <linux/device.h>
16 #include <linux/wait.h>
17 #include <linux/bitops.h>
18 #include <linux/seq_file.h>
19 #include <linux/uaccess.h>
20 #include <linux/ratelimit.h>
22 #undef LDISC_DEBUG_HANGUP
24 #ifdef LDISC_DEBUG_HANGUP
25 #define tty_ldisc_debug(tty, f, args...) tty_debug(tty, f, ##args)
27 #define tty_ldisc_debug(tty, f, args...)
30 /* lockdep nested classes for tty->ldisc_sem */
38 * This guards the refcounted line discipline lists. The lock
39 * must be taken with irqs off because there are hangup path
40 * callers who will do ldisc lookups and cannot sleep.
43 static DEFINE_RAW_SPINLOCK(tty_ldiscs_lock);
44 /* Line disc dispatch table */
45 static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS];
48 * tty_register_ldisc - install a line discipline
50 * @new_ldisc: pointer to the ldisc object
52 * Installs a new line discipline into the kernel. The discipline
53 * is set up as unreferenced and then made available to the kernel
54 * from this point onwards.
57 * takes tty_ldiscs_lock to guard against ldisc races
60 int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc)
65 if (disc < N_TTY || disc >= NR_LDISCS)
68 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
69 tty_ldiscs[disc] = new_ldisc;
70 new_ldisc->num = disc;
71 new_ldisc->refcount = 0;
72 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
76 EXPORT_SYMBOL(tty_register_ldisc);
79 * tty_unregister_ldisc - unload a line discipline
81 * @new_ldisc: pointer to the ldisc object
83 * Remove a line discipline from the kernel providing it is not
87 * takes tty_ldiscs_lock to guard against ldisc races
90 int tty_unregister_ldisc(int disc)
95 if (disc < N_TTY || disc >= NR_LDISCS)
98 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
99 if (tty_ldiscs[disc]->refcount)
102 tty_ldiscs[disc] = NULL;
103 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
107 EXPORT_SYMBOL(tty_unregister_ldisc);
109 static struct tty_ldisc_ops *get_ldops(int disc)
112 struct tty_ldisc_ops *ldops, *ret;
114 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
115 ret = ERR_PTR(-EINVAL);
116 ldops = tty_ldiscs[disc];
118 ret = ERR_PTR(-EAGAIN);
119 if (try_module_get(ldops->owner)) {
124 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
128 static void put_ldops(struct tty_ldisc_ops *ldops)
132 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
134 module_put(ldops->owner);
135 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
139 * tty_ldisc_get - take a reference to an ldisc
140 * @disc: ldisc number
142 * Takes a reference to a line discipline. Deals with refcounts and
143 * module locking counts. Returns NULL if the discipline is not available.
144 * Returns a pointer to the discipline and bumps the ref count if it is
148 * takes tty_ldiscs_lock to guard against ldisc races
151 #if defined(CONFIG_LDISC_AUTOLOAD)
152 #define INITIAL_AUTOLOAD_STATE 1
154 #define INITIAL_AUTOLOAD_STATE 0
156 static int tty_ldisc_autoload = INITIAL_AUTOLOAD_STATE;
158 static struct tty_ldisc *tty_ldisc_get(struct tty_struct *tty, int disc)
160 struct tty_ldisc *ld;
161 struct tty_ldisc_ops *ldops;
163 if (disc < N_TTY || disc >= NR_LDISCS)
164 return ERR_PTR(-EINVAL);
167 * Get the ldisc ops - we may need to request them to be loaded
168 * dynamically and try again.
170 ldops = get_ldops(disc);
172 if (!capable(CAP_SYS_MODULE) && !tty_ldisc_autoload)
173 return ERR_PTR(-EPERM);
174 request_module("tty-ldisc-%d", disc);
175 ldops = get_ldops(disc);
177 return ERR_CAST(ldops);
181 * There is no way to handle allocation failure of only 16 bytes.
182 * Let's simplify error handling and save more memory.
184 ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL | __GFP_NOFAIL);
192 * tty_ldisc_put - release the ldisc
194 * Complement of tty_ldisc_get().
196 static inline void tty_ldisc_put(struct tty_ldisc *ld)
198 if (WARN_ON_ONCE(!ld))
205 static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos)
207 return (*pos < NR_LDISCS) ? pos : NULL;
210 static void *tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos)
213 return (*pos < NR_LDISCS) ? pos : NULL;
216 static void tty_ldiscs_seq_stop(struct seq_file *m, void *v)
220 static int tty_ldiscs_seq_show(struct seq_file *m, void *v)
222 int i = *(loff_t *)v;
223 struct tty_ldisc_ops *ldops;
225 ldops = get_ldops(i);
228 seq_printf(m, "%-10s %2d\n", ldops->name ? ldops->name : "???", i);
233 static const struct seq_operations tty_ldiscs_seq_ops = {
234 .start = tty_ldiscs_seq_start,
235 .next = tty_ldiscs_seq_next,
236 .stop = tty_ldiscs_seq_stop,
237 .show = tty_ldiscs_seq_show,
240 static int proc_tty_ldiscs_open(struct inode *inode, struct file *file)
242 return seq_open(file, &tty_ldiscs_seq_ops);
245 const struct file_operations tty_ldiscs_proc_fops = {
246 .owner = THIS_MODULE,
247 .open = proc_tty_ldiscs_open,
250 .release = seq_release,
254 * tty_ldisc_ref_wait - wait for the tty ldisc
257 * Dereference the line discipline for the terminal and take a
258 * reference to it. If the line discipline is in flux then
259 * wait patiently until it changes.
261 * Note: Must not be called from an IRQ/timer context. The caller
262 * must also be careful not to hold other locks that will deadlock
263 * against a discipline change, such as an existing ldisc reference
264 * (which we check for)
266 * Note: only callable from a file_operations routine (which
267 * guarantees tty->ldisc != NULL when the lock is acquired).
270 struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
272 ldsem_down_read(&tty->ldisc_sem, MAX_SCHEDULE_TIMEOUT);
273 WARN_ON(!tty->ldisc);
276 EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
279 * tty_ldisc_ref - get the tty ldisc
282 * Dereference the line discipline for the terminal and take a
283 * reference to it. If the line discipline is in flux then
284 * return NULL. Can be called from IRQ and timer functions.
287 struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
289 struct tty_ldisc *ld = NULL;
291 if (ldsem_down_read_trylock(&tty->ldisc_sem)) {
294 ldsem_up_read(&tty->ldisc_sem);
298 EXPORT_SYMBOL_GPL(tty_ldisc_ref);
301 * tty_ldisc_deref - free a tty ldisc reference
302 * @ld: reference to free up
304 * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
305 * be called in IRQ context.
308 void tty_ldisc_deref(struct tty_ldisc *ld)
310 ldsem_up_read(&ld->tty->ldisc_sem);
312 EXPORT_SYMBOL_GPL(tty_ldisc_deref);
315 static inline int __lockfunc
316 __tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout)
318 return ldsem_down_write(&tty->ldisc_sem, timeout);
321 static inline int __lockfunc
322 __tty_ldisc_lock_nested(struct tty_struct *tty, unsigned long timeout)
324 return ldsem_down_write_nested(&tty->ldisc_sem,
325 LDISC_SEM_OTHER, timeout);
328 static inline void __tty_ldisc_unlock(struct tty_struct *tty)
330 ldsem_up_write(&tty->ldisc_sem);
333 static int __lockfunc
334 tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout)
338 ret = __tty_ldisc_lock(tty, timeout);
341 set_bit(TTY_LDISC_HALTED, &tty->flags);
345 static void tty_ldisc_unlock(struct tty_struct *tty)
347 clear_bit(TTY_LDISC_HALTED, &tty->flags);
348 __tty_ldisc_unlock(tty);
351 static int __lockfunc
352 tty_ldisc_lock_pair_timeout(struct tty_struct *tty, struct tty_struct *tty2,
353 unsigned long timeout)
358 ret = __tty_ldisc_lock(tty, timeout);
360 ret = __tty_ldisc_lock_nested(tty2, timeout);
362 __tty_ldisc_unlock(tty);
365 /* if this is possible, it has lots of implications */
366 WARN_ON_ONCE(tty == tty2);
367 if (tty2 && tty != tty2) {
368 ret = __tty_ldisc_lock(tty2, timeout);
370 ret = __tty_ldisc_lock_nested(tty, timeout);
372 __tty_ldisc_unlock(tty2);
375 ret = __tty_ldisc_lock(tty, timeout);
381 set_bit(TTY_LDISC_HALTED, &tty->flags);
383 set_bit(TTY_LDISC_HALTED, &tty2->flags);
387 static void __lockfunc
388 tty_ldisc_lock_pair(struct tty_struct *tty, struct tty_struct *tty2)
390 tty_ldisc_lock_pair_timeout(tty, tty2, MAX_SCHEDULE_TIMEOUT);
393 static void __lockfunc tty_ldisc_unlock_pair(struct tty_struct *tty,
394 struct tty_struct *tty2)
396 __tty_ldisc_unlock(tty);
398 __tty_ldisc_unlock(tty2);
402 * tty_ldisc_flush - flush line discipline queue
405 * Flush the line discipline queue (if any) and the tty flip buffers
409 void tty_ldisc_flush(struct tty_struct *tty)
411 struct tty_ldisc *ld = tty_ldisc_ref(tty);
413 tty_buffer_flush(tty, ld);
417 EXPORT_SYMBOL_GPL(tty_ldisc_flush);
420 * tty_set_termios_ldisc - set ldisc field
421 * @tty: tty structure
422 * @num: line discipline number
424 * This is probably overkill for real world processors but
425 * they are not on hot paths so a little discipline won't do
428 * The line discipline-related tty_struct fields are reset to
429 * prevent the ldisc driver from re-using stale information for
430 * the new ldisc instance.
432 * Locking: takes termios_rwsem
435 static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
437 down_write(&tty->termios_rwsem);
438 tty->termios.c_line = num;
439 up_write(&tty->termios_rwsem);
441 tty->disc_data = NULL;
442 tty->receive_room = 0;
446 * tty_ldisc_open - open a line discipline
447 * @tty: tty we are opening the ldisc on
448 * @ld: discipline to open
450 * A helper opening method. Also a convenient debugging and check
453 * Locking: always called with BTM already held.
456 static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld)
458 WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags));
461 /* BTM here locks versus a hangup event */
462 ret = ld->ops->open(tty);
464 clear_bit(TTY_LDISC_OPEN, &tty->flags);
466 tty_ldisc_debug(tty, "%p: opened\n", tty->ldisc);
473 * tty_ldisc_close - close a line discipline
474 * @tty: tty we are opening the ldisc on
475 * @ld: discipline to close
477 * A helper close method. Also a convenient debugging and check
481 static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld)
483 WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags));
484 clear_bit(TTY_LDISC_OPEN, &tty->flags);
487 tty_ldisc_debug(tty, "%p: closed\n", tty->ldisc);
491 * tty_ldisc_restore - helper for tty ldisc change
492 * @tty: tty to recover
493 * @old: previous ldisc
495 * Restore the previous line discipline or N_TTY when a line discipline
496 * change fails due to an open error
499 static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
501 struct tty_ldisc *new_ldisc;
504 /* There is an outstanding reference here so this is safe */
505 old = tty_ldisc_get(tty, old->ops->num);
506 WARN_ON(IS_ERR(old));
508 tty_set_termios_ldisc(tty, old->ops->num);
509 if (tty_ldisc_open(tty, old) < 0) {
511 /* This driver is always present */
512 new_ldisc = tty_ldisc_get(tty, N_TTY);
513 if (IS_ERR(new_ldisc))
515 tty->ldisc = new_ldisc;
516 tty_set_termios_ldisc(tty, N_TTY);
517 r = tty_ldisc_open(tty, new_ldisc);
519 panic("Couldn't open N_TTY ldisc for "
526 * tty_set_ldisc - set line discipline
527 * @tty: the terminal to set
528 * @ldisc: the line discipline
530 * Set the discipline of a tty line. Must be called from a process
531 * context. The ldisc change logic has to protect itself against any
532 * overlapping ldisc change (including on the other end of pty pairs),
533 * the close of one side of a tty/pty pair, and eventually hangup.
536 int tty_set_ldisc(struct tty_struct *tty, int ldisc)
539 struct tty_ldisc *old_ldisc, *new_ldisc;
541 new_ldisc = tty_ldisc_get(tty, ldisc);
542 if (IS_ERR(new_ldisc))
543 return PTR_ERR(new_ldisc);
546 retval = tty_ldisc_lock(tty, 5 * HZ);
548 tty_ldisc_put(new_ldisc);
554 * Check the no-op case
557 if (tty->ldisc->ops->num == ldisc) {
558 tty_ldisc_unlock(tty);
559 tty_ldisc_put(new_ldisc);
564 old_ldisc = tty->ldisc;
566 if (test_bit(TTY_HUPPED, &tty->flags)) {
567 /* We were raced by the hangup method. It will have stomped
568 the ldisc data and closed the ldisc down */
569 tty_ldisc_unlock(tty);
570 tty_ldisc_put(new_ldisc);
575 /* Shutdown the old discipline. */
576 tty_ldisc_close(tty, old_ldisc);
578 /* Now set up the new line discipline. */
579 tty->ldisc = new_ldisc;
580 tty_set_termios_ldisc(tty, ldisc);
582 retval = tty_ldisc_open(tty, new_ldisc);
584 /* Back to the old one or N_TTY if we can't */
585 tty_ldisc_put(new_ldisc);
586 tty_ldisc_restore(tty, old_ldisc);
589 if (tty->ldisc->ops->num != old_ldisc->ops->num && tty->ops->set_ldisc) {
590 down_read(&tty->termios_rwsem);
591 tty->ops->set_ldisc(tty);
592 up_read(&tty->termios_rwsem);
595 /* At this point we hold a reference to the new ldisc and a
596 reference to the old ldisc, or we hold two references to
597 the old ldisc (if it was restored as part of error cleanup
598 above). In either case, releasing a single reference from
599 the old ldisc is correct. */
601 tty_ldisc_put(old_ldisc);
604 * Allow ldisc referencing to occur again
606 tty_ldisc_unlock(tty);
608 /* Restart the work queue in case no characters kick it off. Safe if
610 tty_buffer_restart_work(tty->port);
617 * tty_reset_termios - reset terminal state
620 * Restore a terminal to the driver default state.
623 static void tty_reset_termios(struct tty_struct *tty)
625 down_write(&tty->termios_rwsem);
626 tty->termios = tty->driver->init_termios;
627 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
628 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
629 up_write(&tty->termios_rwsem);
634 * tty_ldisc_reinit - reinitialise the tty ldisc
635 * @tty: tty to reinit
636 * @ldisc: line discipline to reinitialize
638 * Switch the tty to a line discipline and leave the ldisc
642 static int tty_ldisc_reinit(struct tty_struct *tty, int ldisc)
644 struct tty_ldisc *ld = tty_ldisc_get(tty, ldisc);
649 tty_ldisc_close(tty, tty->ldisc);
650 tty_ldisc_put(tty->ldisc);
652 * Switch the line discipline back
655 tty_set_termios_ldisc(tty, ldisc);
661 * tty_ldisc_hangup - hangup ldisc reset
662 * @tty: tty being hung up
664 * Some tty devices reset their termios when they receive a hangup
665 * event. In that situation we must also switch back to N_TTY properly
666 * before we reset the termios data.
668 * Locking: We can take the ldisc mutex as the rest of the code is
669 * careful to allow for this.
671 * In the pty pair case this occurs in the close() path of the
672 * tty itself so we must be careful about locking rules.
675 void tty_ldisc_hangup(struct tty_struct *tty)
677 struct tty_ldisc *ld;
678 int reset = tty->driver->flags & TTY_DRIVER_RESET_TERMIOS;
681 tty_ldisc_debug(tty, "%p: closing\n", tty->ldisc);
683 ld = tty_ldisc_ref(tty);
685 if (ld->ops->flush_buffer)
686 ld->ops->flush_buffer(tty);
687 tty_driver_flush_buffer(tty);
688 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
689 ld->ops->write_wakeup)
690 ld->ops->write_wakeup(tty);
692 ld->ops->hangup(tty);
696 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
697 wake_up_interruptible_poll(&tty->read_wait, POLLIN);
700 * Shutdown the current line discipline, and reset it to
703 * Avoid racing set_ldisc or tty_ldisc_release
705 tty_ldisc_lock(tty, MAX_SCHEDULE_TIMEOUT);
709 /* At this point we have a halted ldisc; we want to close it and
710 reopen a new ldisc. We could defer the reopen to the next
711 open but it means auditing a lot of other paths so this is
715 if (!tty_ldisc_reinit(tty, tty->termios.c_line))
716 err = tty_ldisc_open(tty, tty->ldisc);
720 /* If the re-open fails or we reset then go to N_TTY. The
721 N_TTY open cannot fail */
723 BUG_ON(tty_ldisc_reinit(tty, N_TTY));
724 WARN_ON(tty_ldisc_open(tty, tty->ldisc));
727 tty_ldisc_unlock(tty);
729 tty_reset_termios(tty);
731 tty_ldisc_debug(tty, "%p: re-opened\n", tty->ldisc);
735 * tty_ldisc_setup - open line discipline
736 * @tty: tty being shut down
737 * @o_tty: pair tty for pty/tty pairs
739 * Called during the initial open of a tty/pty pair in order to set up the
740 * line disciplines and bind them to the tty. This has no locking issues
741 * as the device isn't yet active.
744 int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
746 struct tty_ldisc *ld = tty->ldisc;
749 retval = tty_ldisc_open(tty, ld);
754 retval = tty_ldisc_open(o_tty, o_tty->ldisc);
756 tty_ldisc_close(tty, ld);
763 static void tty_ldisc_kill(struct tty_struct *tty)
766 * Now kill off the ldisc
768 tty_ldisc_close(tty, tty->ldisc);
769 tty_ldisc_put(tty->ldisc);
770 /* Force an oops if we mess this up */
773 /* Ensure the next open requests the N_TTY ldisc */
774 tty_set_termios_ldisc(tty, N_TTY);
778 * tty_ldisc_release - release line discipline
779 * @tty: tty being shut down (or one end of pty pair)
781 * Called during the final close of a tty or a pty pair in order to shut
782 * down the line discpline layer. On exit, each ldisc assigned is N_TTY and
783 * each ldisc has not been opened.
786 void tty_ldisc_release(struct tty_struct *tty)
788 struct tty_struct *o_tty = tty->link;
791 * Shutdown this line discipline. As this is the final close,
792 * it does not race with the set_ldisc code path.
795 tty_ldisc_lock_pair(tty, o_tty);
798 tty_ldisc_kill(o_tty);
799 tty_ldisc_unlock_pair(tty, o_tty);
801 /* And the memory resources remaining (buffers, termios) will be
802 disposed of when the kref hits zero */
804 tty_ldisc_debug(tty, "released\n");
808 * tty_ldisc_init - ldisc setup for new tty
809 * @tty: tty being allocated
811 * Set up the line discipline objects for a newly allocated tty. Note that
812 * the tty structure is not completely set up when this call is made.
815 int tty_ldisc_init(struct tty_struct *tty)
817 struct tty_ldisc *ld = tty_ldisc_get(tty, N_TTY);
825 * tty_ldisc_init - ldisc cleanup for new tty
826 * @tty: tty that was allocated recently
828 * The tty structure must not becompletely set up (tty_ldisc_setup) when
831 void tty_ldisc_deinit(struct tty_struct *tty)
833 tty_ldisc_put(tty->ldisc);
837 void tty_ldisc_begin(void)
839 /* Setup the default TTY line discipline. */
840 (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
845 static struct ctl_table tty_table[] = {
847 .procname = "ldisc_autoload",
848 .data = &tty_ldisc_autoload,
849 .maxlen = sizeof(tty_ldisc_autoload),
851 .proc_handler = proc_dointvec,
858 static struct ctl_table tty_dir_table[] = {
867 static struct ctl_table tty_root_table[] = {
871 .child = tty_dir_table,
876 void tty_sysctl_init(void)
878 register_sysctl_table(tty_root_table);