1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/types.h>
3 #include <linux/errno.h>
4 #include <linux/kmod.h>
5 #include <linux/sched.h>
6 #include <linux/interrupt.h>
8 #include <linux/tty_driver.h>
9 #include <linux/file.h>
11 #include <linux/string.h>
12 #include <linux/slab.h>
13 #include <linux/poll.h>
14 #include <linux/proc_fs.h>
15 #include <linux/module.h>
16 #include <linux/device.h>
17 #include <linux/wait.h>
18 #include <linux/bitops.h>
19 #include <linux/seq_file.h>
20 #include <linux/uaccess.h>
21 #include <linux/ratelimit.h>
24 #undef LDISC_DEBUG_HANGUP
26 #ifdef LDISC_DEBUG_HANGUP
27 #define tty_ldisc_debug(tty, f, args...) tty_debug(tty, f, ##args)
29 #define tty_ldisc_debug(tty, f, args...)
32 /* lockdep nested classes for tty->ldisc_sem */
40 * This guards the refcounted line discipline lists. The lock
41 * must be taken with irqs off because there are hangup path
42 * callers who will do ldisc lookups and cannot sleep.
45 static DEFINE_RAW_SPINLOCK(tty_ldiscs_lock);
46 /* Line disc dispatch table */
47 static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS];
50 * tty_register_ldisc - install a line discipline
51 * @new_ldisc: pointer to the ldisc object
53 * Installs a new line discipline into the kernel. The discipline
54 * is set up as unreferenced and then made available to the kernel
55 * from this point onwards.
58 * takes tty_ldiscs_lock to guard against ldisc races
61 int tty_register_ldisc(struct tty_ldisc_ops *new_ldisc)
66 if (new_ldisc->num < N_TTY || new_ldisc->num >= NR_LDISCS)
69 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
70 tty_ldiscs[new_ldisc->num] = new_ldisc;
71 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
75 EXPORT_SYMBOL(tty_register_ldisc);
78 * tty_unregister_ldisc - unload a line discipline
79 * @ldisc: ldisc number
81 * Remove a line discipline from the kernel providing it is not
85 * takes tty_ldiscs_lock to guard against ldisc races
88 void tty_unregister_ldisc(struct tty_ldisc_ops *ldisc)
92 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
93 tty_ldiscs[ldisc->num] = NULL;
94 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
96 EXPORT_SYMBOL(tty_unregister_ldisc);
98 static struct tty_ldisc_ops *get_ldops(int disc)
101 struct tty_ldisc_ops *ldops, *ret;
103 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
104 ret = ERR_PTR(-EINVAL);
105 ldops = tty_ldiscs[disc];
107 ret = ERR_PTR(-EAGAIN);
108 if (try_module_get(ldops->owner))
111 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
115 static void put_ldops(struct tty_ldisc_ops *ldops)
119 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
120 module_put(ldops->owner);
121 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
124 static int tty_ldisc_autoload = IS_BUILTIN(CONFIG_LDISC_AUTOLOAD);
126 * tty_ldisc_get - take a reference to an ldisc
128 * @disc: ldisc number
130 * Takes a reference to a line discipline. Deals with refcounts and
131 * module locking counts.
133 * Returns: -EINVAL if the discipline index is not [N_TTY..NR_LDISCS] or
134 * if the discipline is not registered
135 * -EAGAIN if request_module() failed to load or register the
137 * -ENOMEM if allocation failure
139 * Otherwise, returns a pointer to the discipline and bumps the
143 * takes tty_ldiscs_lock to guard against ldisc races
146 static struct tty_ldisc *tty_ldisc_get(struct tty_struct *tty, int disc)
148 struct tty_ldisc *ld;
149 struct tty_ldisc_ops *ldops;
151 if (disc < N_TTY || disc >= NR_LDISCS)
152 return ERR_PTR(-EINVAL);
155 * Get the ldisc ops - we may need to request them to be loaded
156 * dynamically and try again.
158 ldops = get_ldops(disc);
160 if (!capable(CAP_SYS_MODULE) && !tty_ldisc_autoload)
161 return ERR_PTR(-EPERM);
162 request_module("tty-ldisc-%d", disc);
163 ldops = get_ldops(disc);
165 return ERR_CAST(ldops);
169 * There is no way to handle allocation failure of only 16 bytes.
170 * Let's simplify error handling and save more memory.
172 ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL | __GFP_NOFAIL);
180 * tty_ldisc_put - release the ldisc
182 * Complement of tty_ldisc_get().
184 static void tty_ldisc_put(struct tty_ldisc *ld)
186 if (WARN_ON_ONCE(!ld))
193 static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos)
195 return (*pos < NR_LDISCS) ? pos : NULL;
198 static void *tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos)
201 return (*pos < NR_LDISCS) ? pos : NULL;
204 static void tty_ldiscs_seq_stop(struct seq_file *m, void *v)
208 static int tty_ldiscs_seq_show(struct seq_file *m, void *v)
210 int i = *(loff_t *)v;
211 struct tty_ldisc_ops *ldops;
213 ldops = get_ldops(i);
216 seq_printf(m, "%-10s %2d\n", ldops->name ? ldops->name : "???", i);
221 const struct seq_operations tty_ldiscs_seq_ops = {
222 .start = tty_ldiscs_seq_start,
223 .next = tty_ldiscs_seq_next,
224 .stop = tty_ldiscs_seq_stop,
225 .show = tty_ldiscs_seq_show,
229 * tty_ldisc_ref_wait - wait for the tty ldisc
232 * Dereference the line discipline for the terminal and take a
233 * reference to it. If the line discipline is in flux then
234 * wait patiently until it changes.
236 * Returns: NULL if the tty has been hungup and not re-opened with
237 * a new file descriptor, otherwise valid ldisc reference
239 * Note 1: Must not be called from an IRQ/timer context. The caller
240 * must also be careful not to hold other locks that will deadlock
241 * against a discipline change, such as an existing ldisc reference
242 * (which we check for)
244 * Note 2: a file_operations routine (read/poll/write) should use this
245 * function to wait for any ldisc lifetime events to finish.
248 struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
250 struct tty_ldisc *ld;
252 ldsem_down_read(&tty->ldisc_sem, MAX_SCHEDULE_TIMEOUT);
255 ldsem_up_read(&tty->ldisc_sem);
258 EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
261 * tty_ldisc_ref - get the tty ldisc
264 * Dereference the line discipline for the terminal and take a
265 * reference to it. If the line discipline is in flux then
266 * return NULL. Can be called from IRQ and timer functions.
269 struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
271 struct tty_ldisc *ld = NULL;
273 if (ldsem_down_read_trylock(&tty->ldisc_sem)) {
276 ldsem_up_read(&tty->ldisc_sem);
280 EXPORT_SYMBOL_GPL(tty_ldisc_ref);
283 * tty_ldisc_deref - free a tty ldisc reference
284 * @ld: reference to free up
286 * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
287 * be called in IRQ context.
290 void tty_ldisc_deref(struct tty_ldisc *ld)
292 ldsem_up_read(&ld->tty->ldisc_sem);
294 EXPORT_SYMBOL_GPL(tty_ldisc_deref);
298 __tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout)
300 return ldsem_down_write(&tty->ldisc_sem, timeout);
304 __tty_ldisc_lock_nested(struct tty_struct *tty, unsigned long timeout)
306 return ldsem_down_write_nested(&tty->ldisc_sem,
307 LDISC_SEM_OTHER, timeout);
310 static inline void __tty_ldisc_unlock(struct tty_struct *tty)
312 ldsem_up_write(&tty->ldisc_sem);
315 int tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout)
319 /* Kindly asking blocked readers to release the read side */
320 set_bit(TTY_LDISC_CHANGING, &tty->flags);
321 wake_up_interruptible_all(&tty->read_wait);
322 wake_up_interruptible_all(&tty->write_wait);
324 ret = __tty_ldisc_lock(tty, timeout);
327 set_bit(TTY_LDISC_HALTED, &tty->flags);
331 void tty_ldisc_unlock(struct tty_struct *tty)
333 clear_bit(TTY_LDISC_HALTED, &tty->flags);
334 /* Can be cleared here - ldisc_unlock will wake up writers firstly */
335 clear_bit(TTY_LDISC_CHANGING, &tty->flags);
336 __tty_ldisc_unlock(tty);
340 tty_ldisc_lock_pair_timeout(struct tty_struct *tty, struct tty_struct *tty2,
341 unsigned long timeout)
346 ret = __tty_ldisc_lock(tty, timeout);
348 ret = __tty_ldisc_lock_nested(tty2, timeout);
350 __tty_ldisc_unlock(tty);
353 /* if this is possible, it has lots of implications */
354 WARN_ON_ONCE(tty == tty2);
355 if (tty2 && tty != tty2) {
356 ret = __tty_ldisc_lock(tty2, timeout);
358 ret = __tty_ldisc_lock_nested(tty, timeout);
360 __tty_ldisc_unlock(tty2);
363 ret = __tty_ldisc_lock(tty, timeout);
369 set_bit(TTY_LDISC_HALTED, &tty->flags);
371 set_bit(TTY_LDISC_HALTED, &tty2->flags);
375 static void tty_ldisc_lock_pair(struct tty_struct *tty, struct tty_struct *tty2)
377 tty_ldisc_lock_pair_timeout(tty, tty2, MAX_SCHEDULE_TIMEOUT);
380 static void tty_ldisc_unlock_pair(struct tty_struct *tty,
381 struct tty_struct *tty2)
383 __tty_ldisc_unlock(tty);
385 __tty_ldisc_unlock(tty2);
389 * tty_ldisc_flush - flush line discipline queue
392 * Flush the line discipline queue (if any) and the tty flip buffers
396 void tty_ldisc_flush(struct tty_struct *tty)
398 struct tty_ldisc *ld = tty_ldisc_ref(tty);
400 tty_buffer_flush(tty, ld);
404 EXPORT_SYMBOL_GPL(tty_ldisc_flush);
407 * tty_set_termios_ldisc - set ldisc field
408 * @tty: tty structure
409 * @disc: line discipline number
411 * This is probably overkill for real world processors but
412 * they are not on hot paths so a little discipline won't do
415 * The line discipline-related tty_struct fields are reset to
416 * prevent the ldisc driver from re-using stale information for
417 * the new ldisc instance.
419 * Locking: takes termios_rwsem
422 static void tty_set_termios_ldisc(struct tty_struct *tty, int disc)
424 down_write(&tty->termios_rwsem);
425 tty->termios.c_line = disc;
426 up_write(&tty->termios_rwsem);
428 tty->disc_data = NULL;
429 tty->receive_room = 0;
433 * tty_ldisc_open - open a line discipline
434 * @tty: tty we are opening the ldisc on
435 * @ld: discipline to open
437 * A helper opening method. Also a convenient debugging and check
440 * Locking: always called with BTM already held.
443 static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld)
445 WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags));
448 /* BTM here locks versus a hangup event */
449 ret = ld->ops->open(tty);
451 clear_bit(TTY_LDISC_OPEN, &tty->flags);
453 tty_ldisc_debug(tty, "%p: opened\n", ld);
460 * tty_ldisc_close - close a line discipline
461 * @tty: tty we are opening the ldisc on
462 * @ld: discipline to close
464 * A helper close method. Also a convenient debugging and check
468 static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld)
470 lockdep_assert_held_write(&tty->ldisc_sem);
471 WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags));
472 clear_bit(TTY_LDISC_OPEN, &tty->flags);
475 tty_ldisc_debug(tty, "%p: closed\n", ld);
479 * tty_ldisc_failto - helper for ldisc failback
480 * @tty: tty to open the ldisc on
481 * @ld: ldisc we are trying to fail back to
483 * Helper to try and recover a tty when switching back to the old
484 * ldisc fails and we need something attached.
487 static int tty_ldisc_failto(struct tty_struct *tty, int ld)
489 struct tty_ldisc *disc = tty_ldisc_get(tty, ld);
492 lockdep_assert_held_write(&tty->ldisc_sem);
494 return PTR_ERR(disc);
496 tty_set_termios_ldisc(tty, ld);
497 r = tty_ldisc_open(tty, disc);
504 * tty_ldisc_restore - helper for tty ldisc change
505 * @tty: tty to recover
506 * @old: previous ldisc
508 * Restore the previous line discipline or N_TTY when a line discipline
509 * change fails due to an open error
512 static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
514 /* There is an outstanding reference here so this is safe */
515 if (tty_ldisc_failto(tty, old->ops->num) < 0) {
516 const char *name = tty_name(tty);
518 pr_warn("Falling back ldisc for %s.\n", name);
520 * The traditional behaviour is to fall back to N_TTY, we
521 * want to avoid falling back to N_NULL unless we have no
522 * choice to avoid the risk of breaking anything
524 if (tty_ldisc_failto(tty, N_TTY) < 0 &&
525 tty_ldisc_failto(tty, N_NULL) < 0)
526 panic("Couldn't open N_NULL ldisc for %s.", name);
531 * tty_set_ldisc - set line discipline
532 * @tty: the terminal to set
533 * @disc: the line discipline number
535 * Set the discipline of a tty line. Must be called from a process
536 * context. The ldisc change logic has to protect itself against any
537 * overlapping ldisc change (including on the other end of pty pairs),
538 * the close of one side of a tty/pty pair, and eventually hangup.
541 int tty_set_ldisc(struct tty_struct *tty, int disc)
544 struct tty_ldisc *old_ldisc, *new_ldisc;
546 new_ldisc = tty_ldisc_get(tty, disc);
547 if (IS_ERR(new_ldisc))
548 return PTR_ERR(new_ldisc);
551 retval = tty_ldisc_lock(tty, 5 * HZ);
560 /* Check the no-op case */
561 if (tty->ldisc->ops->num == disc)
564 if (test_bit(TTY_HUPPED, &tty->flags)) {
565 /* We were raced by hangup */
570 old_ldisc = tty->ldisc;
572 /* Shutdown the old discipline. */
573 tty_ldisc_close(tty, old_ldisc);
575 /* Now set up the new line discipline. */
576 tty->ldisc = new_ldisc;
577 tty_set_termios_ldisc(tty, disc);
579 retval = tty_ldisc_open(tty, new_ldisc);
581 /* Back to the old one or N_TTY if we can't */
582 tty_ldisc_put(new_ldisc);
583 tty_ldisc_restore(tty, old_ldisc);
586 if (tty->ldisc->ops->num != old_ldisc->ops->num && tty->ops->set_ldisc) {
587 down_read(&tty->termios_rwsem);
588 tty->ops->set_ldisc(tty);
589 up_read(&tty->termios_rwsem);
593 * At this point we hold a reference to the new ldisc and a
594 * reference to the old ldisc, or we hold two references to
595 * the old ldisc (if it was restored as part of error cleanup
596 * above). In either case, releasing a single reference from
597 * the old ldisc is correct.
599 new_ldisc = old_ldisc;
601 tty_ldisc_unlock(tty);
604 * Restart the work queue in case no characters kick it off. Safe if
607 tty_buffer_restart_work(tty->port);
609 tty_ldisc_put(new_ldisc); /* drop the extra reference */
613 EXPORT_SYMBOL_GPL(tty_set_ldisc);
616 * tty_ldisc_kill - teardown ldisc
617 * @tty: tty being released
619 * Perform final close of the ldisc and reset tty->ldisc
621 static void tty_ldisc_kill(struct tty_struct *tty)
623 lockdep_assert_held_write(&tty->ldisc_sem);
627 * Now kill off the ldisc
629 tty_ldisc_close(tty, tty->ldisc);
630 tty_ldisc_put(tty->ldisc);
631 /* Force an oops if we mess this up */
636 * tty_reset_termios - reset terminal state
639 * Restore a terminal to the driver default state.
642 static void tty_reset_termios(struct tty_struct *tty)
644 down_write(&tty->termios_rwsem);
645 tty->termios = tty->driver->init_termios;
646 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
647 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
648 up_write(&tty->termios_rwsem);
653 * tty_ldisc_reinit - reinitialise the tty ldisc
654 * @tty: tty to reinit
655 * @disc: line discipline to reinitialize
657 * Completely reinitialize the line discipline state, by closing the
658 * current instance, if there is one, and opening a new instance. If
659 * an error occurs opening the new non-N_TTY instance, the instance
660 * is dropped and tty->ldisc reset to NULL. The caller can then retry
661 * with N_TTY instead.
663 * Returns 0 if successful, otherwise error code < 0
666 int tty_ldisc_reinit(struct tty_struct *tty, int disc)
668 struct tty_ldisc *ld;
671 lockdep_assert_held_write(&tty->ldisc_sem);
672 ld = tty_ldisc_get(tty, disc);
674 BUG_ON(disc == N_TTY);
679 tty_ldisc_close(tty, tty->ldisc);
680 tty_ldisc_put(tty->ldisc);
683 /* switch the line discipline */
685 tty_set_termios_ldisc(tty, disc);
686 retval = tty_ldisc_open(tty, tty->ldisc);
688 tty_ldisc_put(tty->ldisc);
695 * tty_ldisc_hangup - hangup ldisc reset
696 * @tty: tty being hung up
697 * @reinit: whether to re-initialise the tty
699 * Some tty devices reset their termios when they receive a hangup
700 * event. In that situation we must also switch back to N_TTY properly
701 * before we reset the termios data.
703 * Locking: We can take the ldisc mutex as the rest of the code is
704 * careful to allow for this.
706 * In the pty pair case this occurs in the close() path of the
707 * tty itself so we must be careful about locking rules.
710 void tty_ldisc_hangup(struct tty_struct *tty, bool reinit)
712 struct tty_ldisc *ld;
714 tty_ldisc_debug(tty, "%p: hangup\n", tty->ldisc);
716 ld = tty_ldisc_ref(tty);
718 if (ld->ops->flush_buffer)
719 ld->ops->flush_buffer(tty);
720 tty_driver_flush_buffer(tty);
721 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
722 ld->ops->write_wakeup)
723 ld->ops->write_wakeup(tty);
725 ld->ops->hangup(tty);
729 wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT);
730 wake_up_interruptible_poll(&tty->read_wait, EPOLLIN);
733 * Shutdown the current line discipline, and reset it to
736 * Avoid racing set_ldisc or tty_ldisc_release
738 tty_ldisc_lock(tty, MAX_SCHEDULE_TIMEOUT);
740 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
741 tty_reset_termios(tty);
745 if (tty_ldisc_reinit(tty, tty->termios.c_line) < 0 &&
746 tty_ldisc_reinit(tty, N_TTY) < 0)
747 WARN_ON(tty_ldisc_reinit(tty, N_NULL) < 0);
751 tty_ldisc_unlock(tty);
755 * tty_ldisc_setup - open line discipline
756 * @tty: tty being shut down
757 * @o_tty: pair tty for pty/tty pairs
759 * Called during the initial open of a tty/pty pair in order to set up the
760 * line disciplines and bind them to the tty. This has no locking issues
761 * as the device isn't yet active.
764 int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
766 int retval = tty_ldisc_open(tty, tty->ldisc);
773 * Called without o_tty->ldisc_sem held, as o_tty has been
774 * just allocated and no one has a reference to it.
776 retval = tty_ldisc_open(o_tty, o_tty->ldisc);
778 tty_ldisc_close(tty, tty->ldisc);
786 * tty_ldisc_release - release line discipline
787 * @tty: tty being shut down (or one end of pty pair)
789 * Called during the final close of a tty or a pty pair in order to shut
790 * down the line discpline layer. On exit, each tty's ldisc is NULL.
793 void tty_ldisc_release(struct tty_struct *tty)
795 struct tty_struct *o_tty = tty->link;
798 * Shutdown this line discipline. As this is the final close,
799 * it does not race with the set_ldisc code path.
802 tty_ldisc_lock_pair(tty, o_tty);
805 tty_ldisc_kill(o_tty);
806 tty_ldisc_unlock_pair(tty, o_tty);
809 * And the memory resources remaining (buffers, termios) will be
810 * disposed of when the kref hits zero
813 tty_ldisc_debug(tty, "released\n");
817 * tty_ldisc_init - ldisc setup for new tty
818 * @tty: tty being allocated
820 * Set up the line discipline objects for a newly allocated tty. Note that
821 * the tty structure is not completely set up when this call is made.
824 int tty_ldisc_init(struct tty_struct *tty)
826 struct tty_ldisc *ld = tty_ldisc_get(tty, N_TTY);
835 * tty_ldisc_deinit - ldisc cleanup for new tty
836 * @tty: tty that was allocated recently
838 * The tty structure must not becompletely set up (tty_ldisc_setup) when
841 void tty_ldisc_deinit(struct tty_struct *tty)
843 /* no ldisc_sem, tty is being destroyed */
845 tty_ldisc_put(tty->ldisc);
849 static struct ctl_table tty_table[] = {
851 .procname = "ldisc_autoload",
852 .data = &tty_ldisc_autoload,
853 .maxlen = sizeof(tty_ldisc_autoload),
855 .proc_handler = proc_dointvec,
856 .extra1 = SYSCTL_ZERO,
857 .extra2 = SYSCTL_ONE,
862 static struct ctl_table tty_dir_table[] = {
871 static struct ctl_table tty_root_table[] = {
875 .child = tty_dir_table,
880 void tty_sysctl_init(void)
882 register_sysctl_table(tty_root_table);