xref: /linux/drivers/tty/tty_ldisc.c (revision 03b3b1a2405ccd71570cd5ec1fe4abd7bb4891cb)
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>
7 #include <linux/tty.h>
8 #include <linux/tty_driver.h>
9 #include <linux/file.h>
10 #include <linux/mm.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>
22 #include "tty.h"
23 
24 #undef LDISC_DEBUG_HANGUP
25 
26 #ifdef LDISC_DEBUG_HANGUP
27 #define tty_ldisc_debug(tty, f, args...)	tty_debug(tty, f, ##args)
28 #else
29 #define tty_ldisc_debug(tty, f, args...)
30 #endif
31 
32 /* lockdep nested classes for tty->ldisc_sem */
33 enum {
34 	LDISC_SEM_NORMAL,
35 	LDISC_SEM_OTHER,
36 };
37 
38 
39 /*
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.
43  */
44 
45 static DEFINE_RAW_SPINLOCK(tty_ldiscs_lock);
46 /* Line disc dispatch table */
47 static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS];
48 
49 /**
50  *	tty_register_ldisc	-	install a line discipline
51  *	@disc: ldisc number
52  *	@new_ldisc: pointer to the ldisc object
53  *
54  *	Installs a new line discipline into the kernel. The discipline
55  *	is set up as unreferenced and then made available to the kernel
56  *	from this point onwards.
57  *
58  *	Locking:
59  *		takes tty_ldiscs_lock to guard against ldisc races
60  */
61 
62 int tty_register_ldisc(struct tty_ldisc_ops *new_ldisc)
63 {
64 	unsigned long flags;
65 	int ret = 0;
66 
67 	if (new_ldisc->num < N_TTY || new_ldisc->num >= NR_LDISCS)
68 		return -EINVAL;
69 
70 	raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
71 	tty_ldiscs[new_ldisc->num] = new_ldisc;
72 	raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
73 
74 	return ret;
75 }
76 EXPORT_SYMBOL(tty_register_ldisc);
77 
78 /**
79  *	tty_unregister_ldisc	-	unload a line discipline
80  *	@disc: ldisc number
81  *
82  *	Remove a line discipline from the kernel providing it is not
83  *	currently in use.
84  *
85  *	Locking:
86  *		takes tty_ldiscs_lock to guard against ldisc races
87  */
88 
89 void tty_unregister_ldisc(struct tty_ldisc_ops *ldisc)
90 {
91 	unsigned long flags;
92 
93 	raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
94 	tty_ldiscs[ldisc->num] = NULL;
95 	raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
96 }
97 EXPORT_SYMBOL(tty_unregister_ldisc);
98 
99 static struct tty_ldisc_ops *get_ldops(int disc)
100 {
101 	unsigned long flags;
102 	struct tty_ldisc_ops *ldops, *ret;
103 
104 	raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
105 	ret = ERR_PTR(-EINVAL);
106 	ldops = tty_ldiscs[disc];
107 	if (ldops) {
108 		ret = ERR_PTR(-EAGAIN);
109 		if (try_module_get(ldops->owner))
110 			ret = ldops;
111 	}
112 	raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
113 	return ret;
114 }
115 
116 static void put_ldops(struct tty_ldisc_ops *ldops)
117 {
118 	unsigned long flags;
119 
120 	raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
121 	module_put(ldops->owner);
122 	raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
123 }
124 
125 static int tty_ldisc_autoload = IS_BUILTIN(CONFIG_LDISC_AUTOLOAD);
126 /**
127  *	tty_ldisc_get		-	take a reference to an ldisc
128  *	@tty: tty device
129  *	@disc: ldisc number
130  *
131  *	Takes a reference to a line discipline. Deals with refcounts and
132  *	module locking counts.
133  *
134  *	Returns: -EINVAL if the discipline index is not [N_TTY..NR_LDISCS] or
135  *			 if the discipline is not registered
136  *		 -EAGAIN if request_module() failed to load or register the
137  *			 discipline
138  *		 -ENOMEM if allocation failure
139  *
140  *		 Otherwise, returns a pointer to the discipline and bumps the
141  *		 ref count
142  *
143  *	Locking:
144  *		takes tty_ldiscs_lock to guard against ldisc races
145  */
146 
147 static struct tty_ldisc *tty_ldisc_get(struct tty_struct *tty, int disc)
148 {
149 	struct tty_ldisc *ld;
150 	struct tty_ldisc_ops *ldops;
151 
152 	if (disc < N_TTY || disc >= NR_LDISCS)
153 		return ERR_PTR(-EINVAL);
154 
155 	/*
156 	 * Get the ldisc ops - we may need to request them to be loaded
157 	 * dynamically and try again.
158 	 */
159 	ldops = get_ldops(disc);
160 	if (IS_ERR(ldops)) {
161 		if (!capable(CAP_SYS_MODULE) && !tty_ldisc_autoload)
162 			return ERR_PTR(-EPERM);
163 		request_module("tty-ldisc-%d", disc);
164 		ldops = get_ldops(disc);
165 		if (IS_ERR(ldops))
166 			return ERR_CAST(ldops);
167 	}
168 
169 	/*
170 	 * There is no way to handle allocation failure of only 16 bytes.
171 	 * Let's simplify error handling and save more memory.
172 	 */
173 	ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL | __GFP_NOFAIL);
174 	ld->ops = ldops;
175 	ld->tty = tty;
176 
177 	return ld;
178 }
179 
180 /*
181  *	tty_ldisc_put		-	release the ldisc
182  *
183  *	Complement of tty_ldisc_get().
184  */
185 static void tty_ldisc_put(struct tty_ldisc *ld)
186 {
187 	if (WARN_ON_ONCE(!ld))
188 		return;
189 
190 	put_ldops(ld->ops);
191 	kfree(ld);
192 }
193 
194 static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos)
195 {
196 	return (*pos < NR_LDISCS) ? pos : NULL;
197 }
198 
199 static void *tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos)
200 {
201 	(*pos)++;
202 	return (*pos < NR_LDISCS) ? pos : NULL;
203 }
204 
205 static void tty_ldiscs_seq_stop(struct seq_file *m, void *v)
206 {
207 }
208 
209 static int tty_ldiscs_seq_show(struct seq_file *m, void *v)
210 {
211 	int i = *(loff_t *)v;
212 	struct tty_ldisc_ops *ldops;
213 
214 	ldops = get_ldops(i);
215 	if (IS_ERR(ldops))
216 		return 0;
217 	seq_printf(m, "%-10s %2d\n", ldops->name ? ldops->name : "???", i);
218 	put_ldops(ldops);
219 	return 0;
220 }
221 
222 const struct seq_operations tty_ldiscs_seq_ops = {
223 	.start	= tty_ldiscs_seq_start,
224 	.next	= tty_ldiscs_seq_next,
225 	.stop	= tty_ldiscs_seq_stop,
226 	.show	= tty_ldiscs_seq_show,
227 };
228 
229 /**
230  *	tty_ldisc_ref_wait	-	wait for the tty ldisc
231  *	@tty: tty device
232  *
233  *	Dereference the line discipline for the terminal and take a
234  *	reference to it. If the line discipline is in flux then
235  *	wait patiently until it changes.
236  *
237  *	Returns: NULL if the tty has been hungup and not re-opened with
238  *		 a new file descriptor, otherwise valid ldisc reference
239  *
240  *	Note 1: Must not be called from an IRQ/timer context. The caller
241  *	must also be careful not to hold other locks that will deadlock
242  *	against a discipline change, such as an existing ldisc reference
243  *	(which we check for)
244  *
245  *	Note 2: a file_operations routine (read/poll/write) should use this
246  *	function to wait for any ldisc lifetime events to finish.
247  */
248 
249 struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
250 {
251 	struct tty_ldisc *ld;
252 
253 	ldsem_down_read(&tty->ldisc_sem, MAX_SCHEDULE_TIMEOUT);
254 	ld = tty->ldisc;
255 	if (!ld)
256 		ldsem_up_read(&tty->ldisc_sem);
257 	return ld;
258 }
259 EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
260 
261 /**
262  *	tty_ldisc_ref		-	get the tty ldisc
263  *	@tty: tty device
264  *
265  *	Dereference the line discipline for the terminal and take a
266  *	reference to it. If the line discipline is in flux then
267  *	return NULL. Can be called from IRQ and timer functions.
268  */
269 
270 struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
271 {
272 	struct tty_ldisc *ld = NULL;
273 
274 	if (ldsem_down_read_trylock(&tty->ldisc_sem)) {
275 		ld = tty->ldisc;
276 		if (!ld)
277 			ldsem_up_read(&tty->ldisc_sem);
278 	}
279 	return ld;
280 }
281 EXPORT_SYMBOL_GPL(tty_ldisc_ref);
282 
283 /**
284  *	tty_ldisc_deref		-	free a tty ldisc reference
285  *	@ld: reference to free up
286  *
287  *	Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
288  *	be called in IRQ context.
289  */
290 
291 void tty_ldisc_deref(struct tty_ldisc *ld)
292 {
293 	ldsem_up_read(&ld->tty->ldisc_sem);
294 }
295 EXPORT_SYMBOL_GPL(tty_ldisc_deref);
296 
297 
298 static inline int
299 __tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout)
300 {
301 	return ldsem_down_write(&tty->ldisc_sem, timeout);
302 }
303 
304 static inline int
305 __tty_ldisc_lock_nested(struct tty_struct *tty, unsigned long timeout)
306 {
307 	return ldsem_down_write_nested(&tty->ldisc_sem,
308 				       LDISC_SEM_OTHER, timeout);
309 }
310 
311 static inline void __tty_ldisc_unlock(struct tty_struct *tty)
312 {
313 	ldsem_up_write(&tty->ldisc_sem);
314 }
315 
316 int tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout)
317 {
318 	int ret;
319 
320 	/* Kindly asking blocked readers to release the read side */
321 	set_bit(TTY_LDISC_CHANGING, &tty->flags);
322 	wake_up_interruptible_all(&tty->read_wait);
323 	wake_up_interruptible_all(&tty->write_wait);
324 
325 	ret = __tty_ldisc_lock(tty, timeout);
326 	if (!ret)
327 		return -EBUSY;
328 	set_bit(TTY_LDISC_HALTED, &tty->flags);
329 	return 0;
330 }
331 
332 void tty_ldisc_unlock(struct tty_struct *tty)
333 {
334 	clear_bit(TTY_LDISC_HALTED, &tty->flags);
335 	/* Can be cleared here - ldisc_unlock will wake up writers firstly */
336 	clear_bit(TTY_LDISC_CHANGING, &tty->flags);
337 	__tty_ldisc_unlock(tty);
338 }
339 
340 static int
341 tty_ldisc_lock_pair_timeout(struct tty_struct *tty, struct tty_struct *tty2,
342 			    unsigned long timeout)
343 {
344 	int ret;
345 
346 	if (tty < tty2) {
347 		ret = __tty_ldisc_lock(tty, timeout);
348 		if (ret) {
349 			ret = __tty_ldisc_lock_nested(tty2, timeout);
350 			if (!ret)
351 				__tty_ldisc_unlock(tty);
352 		}
353 	} else {
354 		/* if this is possible, it has lots of implications */
355 		WARN_ON_ONCE(tty == tty2);
356 		if (tty2 && tty != tty2) {
357 			ret = __tty_ldisc_lock(tty2, timeout);
358 			if (ret) {
359 				ret = __tty_ldisc_lock_nested(tty, timeout);
360 				if (!ret)
361 					__tty_ldisc_unlock(tty2);
362 			}
363 		} else
364 			ret = __tty_ldisc_lock(tty, timeout);
365 	}
366 
367 	if (!ret)
368 		return -EBUSY;
369 
370 	set_bit(TTY_LDISC_HALTED, &tty->flags);
371 	if (tty2)
372 		set_bit(TTY_LDISC_HALTED, &tty2->flags);
373 	return 0;
374 }
375 
376 static void tty_ldisc_lock_pair(struct tty_struct *tty, struct tty_struct *tty2)
377 {
378 	tty_ldisc_lock_pair_timeout(tty, tty2, MAX_SCHEDULE_TIMEOUT);
379 }
380 
381 static void tty_ldisc_unlock_pair(struct tty_struct *tty,
382 				  struct tty_struct *tty2)
383 {
384 	__tty_ldisc_unlock(tty);
385 	if (tty2)
386 		__tty_ldisc_unlock(tty2);
387 }
388 
389 /**
390  *	tty_ldisc_flush	-	flush line discipline queue
391  *	@tty: tty
392  *
393  *	Flush the line discipline queue (if any) and the tty flip buffers
394  *	for this tty.
395  */
396 
397 void tty_ldisc_flush(struct tty_struct *tty)
398 {
399 	struct tty_ldisc *ld = tty_ldisc_ref(tty);
400 
401 	tty_buffer_flush(tty, ld);
402 	if (ld)
403 		tty_ldisc_deref(ld);
404 }
405 EXPORT_SYMBOL_GPL(tty_ldisc_flush);
406 
407 /**
408  *	tty_set_termios_ldisc		-	set ldisc field
409  *	@tty: tty structure
410  *	@disc: line discipline number
411  *
412  *	This is probably overkill for real world processors but
413  *	they are not on hot paths so a little discipline won't do
414  *	any harm.
415  *
416  *	The line discipline-related tty_struct fields are reset to
417  *	prevent the ldisc driver from re-using stale information for
418  *	the new ldisc instance.
419  *
420  *	Locking: takes termios_rwsem
421  */
422 
423 static void tty_set_termios_ldisc(struct tty_struct *tty, int disc)
424 {
425 	down_write(&tty->termios_rwsem);
426 	tty->termios.c_line = disc;
427 	up_write(&tty->termios_rwsem);
428 
429 	tty->disc_data = NULL;
430 	tty->receive_room = 0;
431 }
432 
433 /**
434  *	tty_ldisc_open		-	open a line discipline
435  *	@tty: tty we are opening the ldisc on
436  *	@ld: discipline to open
437  *
438  *	A helper opening method. Also a convenient debugging and check
439  *	point.
440  *
441  *	Locking: always called with BTM already held.
442  */
443 
444 static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld)
445 {
446 	WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags));
447 	if (ld->ops->open) {
448 		int ret;
449 		/* BTM here locks versus a hangup event */
450 		ret = ld->ops->open(tty);
451 		if (ret)
452 			clear_bit(TTY_LDISC_OPEN, &tty->flags);
453 
454 		tty_ldisc_debug(tty, "%p: opened\n", ld);
455 		return ret;
456 	}
457 	return 0;
458 }
459 
460 /**
461  *	tty_ldisc_close		-	close a line discipline
462  *	@tty: tty we are opening the ldisc on
463  *	@ld: discipline to close
464  *
465  *	A helper close method. Also a convenient debugging and check
466  *	point.
467  */
468 
469 static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld)
470 {
471 	lockdep_assert_held_write(&tty->ldisc_sem);
472 	WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags));
473 	clear_bit(TTY_LDISC_OPEN, &tty->flags);
474 	if (ld->ops->close)
475 		ld->ops->close(tty);
476 	tty_ldisc_debug(tty, "%p: closed\n", ld);
477 }
478 
479 /**
480  *	tty_ldisc_failto	-	helper for ldisc failback
481  *	@tty: tty to open the ldisc on
482  *	@ld: ldisc we are trying to fail back to
483  *
484  *	Helper to try and recover a tty when switching back to the old
485  *	ldisc fails and we need something attached.
486  */
487 
488 static int tty_ldisc_failto(struct tty_struct *tty, int ld)
489 {
490 	struct tty_ldisc *disc = tty_ldisc_get(tty, ld);
491 	int r;
492 
493 	lockdep_assert_held_write(&tty->ldisc_sem);
494 	if (IS_ERR(disc))
495 		return PTR_ERR(disc);
496 	tty->ldisc = disc;
497 	tty_set_termios_ldisc(tty, ld);
498 	r = tty_ldisc_open(tty, disc);
499 	if (r < 0)
500 		tty_ldisc_put(disc);
501 	return r;
502 }
503 
504 /**
505  *	tty_ldisc_restore	-	helper for tty ldisc change
506  *	@tty: tty to recover
507  *	@old: previous ldisc
508  *
509  *	Restore the previous line discipline or N_TTY when a line discipline
510  *	change fails due to an open error
511  */
512 
513 static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
514 {
515 	/* There is an outstanding reference here so this is safe */
516 	if (tty_ldisc_failto(tty, old->ops->num) < 0) {
517 		const char *name = tty_name(tty);
518 
519 		pr_warn("Falling back ldisc for %s.\n", name);
520 		/*
521 		 * The traditional behaviour is to fall back to N_TTY, we
522 		 * want to avoid falling back to N_NULL unless we have no
523 		 * choice to avoid the risk of breaking anything
524 		 */
525 		if (tty_ldisc_failto(tty, N_TTY) < 0 &&
526 		    tty_ldisc_failto(tty, N_NULL) < 0)
527 			panic("Couldn't open N_NULL ldisc for %s.", name);
528 	}
529 }
530 
531 /**
532  *	tty_set_ldisc		-	set line discipline
533  *	@tty: the terminal to set
534  *	@disc: the line discipline number
535  *
536  *	Set the discipline of a tty line. Must be called from a process
537  *	context. The ldisc change logic has to protect itself against any
538  *	overlapping ldisc change (including on the other end of pty pairs),
539  *	the close of one side of a tty/pty pair, and eventually hangup.
540  */
541 
542 int tty_set_ldisc(struct tty_struct *tty, int disc)
543 {
544 	int retval;
545 	struct tty_ldisc *old_ldisc, *new_ldisc;
546 
547 	new_ldisc = tty_ldisc_get(tty, disc);
548 	if (IS_ERR(new_ldisc))
549 		return PTR_ERR(new_ldisc);
550 
551 	tty_lock(tty);
552 	retval = tty_ldisc_lock(tty, 5 * HZ);
553 	if (retval)
554 		goto err;
555 
556 	if (!tty->ldisc) {
557 		retval = -EIO;
558 		goto out;
559 	}
560 
561 	/* Check the no-op case */
562 	if (tty->ldisc->ops->num == disc)
563 		goto out;
564 
565 	if (test_bit(TTY_HUPPED, &tty->flags)) {
566 		/* We were raced by hangup */
567 		retval = -EIO;
568 		goto out;
569 	}
570 
571 	old_ldisc = tty->ldisc;
572 
573 	/* Shutdown the old discipline. */
574 	tty_ldisc_close(tty, old_ldisc);
575 
576 	/* Now set up the new line discipline. */
577 	tty->ldisc = new_ldisc;
578 	tty_set_termios_ldisc(tty, disc);
579 
580 	retval = tty_ldisc_open(tty, new_ldisc);
581 	if (retval < 0) {
582 		/* Back to the old one or N_TTY if we can't */
583 		tty_ldisc_put(new_ldisc);
584 		tty_ldisc_restore(tty, old_ldisc);
585 	}
586 
587 	if (tty->ldisc->ops->num != old_ldisc->ops->num && tty->ops->set_ldisc) {
588 		down_read(&tty->termios_rwsem);
589 		tty->ops->set_ldisc(tty);
590 		up_read(&tty->termios_rwsem);
591 	}
592 
593 	/*
594 	 * At this point we hold a reference to the new ldisc and a
595 	 * reference to the old ldisc, or we hold two references to
596 	 * the old ldisc (if it was restored as part of error cleanup
597 	 * above). In either case, releasing a single reference from
598 	 * the old ldisc is correct.
599 	 */
600 	new_ldisc = old_ldisc;
601 out:
602 	tty_ldisc_unlock(tty);
603 
604 	/*
605 	 * Restart the work queue in case no characters kick it off. Safe if
606 	 * already running
607 	 */
608 	tty_buffer_restart_work(tty->port);
609 err:
610 	tty_ldisc_put(new_ldisc);	/* drop the extra reference */
611 	tty_unlock(tty);
612 	return retval;
613 }
614 EXPORT_SYMBOL_GPL(tty_set_ldisc);
615 
616 /**
617  *	tty_ldisc_kill	-	teardown ldisc
618  *	@tty: tty being released
619  *
620  *	Perform final close of the ldisc and reset tty->ldisc
621  */
622 static void tty_ldisc_kill(struct tty_struct *tty)
623 {
624 	lockdep_assert_held_write(&tty->ldisc_sem);
625 	if (!tty->ldisc)
626 		return;
627 	/*
628 	 * Now kill off the ldisc
629 	 */
630 	tty_ldisc_close(tty, tty->ldisc);
631 	tty_ldisc_put(tty->ldisc);
632 	/* Force an oops if we mess this up */
633 	tty->ldisc = NULL;
634 }
635 
636 /**
637  *	tty_reset_termios	-	reset terminal state
638  *	@tty: tty to reset
639  *
640  *	Restore a terminal to the driver default state.
641  */
642 
643 static void tty_reset_termios(struct tty_struct *tty)
644 {
645 	down_write(&tty->termios_rwsem);
646 	tty->termios = tty->driver->init_termios;
647 	tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
648 	tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
649 	up_write(&tty->termios_rwsem);
650 }
651 
652 
653 /**
654  *	tty_ldisc_reinit	-	reinitialise the tty ldisc
655  *	@tty: tty to reinit
656  *	@disc: line discipline to reinitialize
657  *
658  *	Completely reinitialize the line discipline state, by closing the
659  *	current instance, if there is one, and opening a new instance. If
660  *	an error occurs opening the new non-N_TTY instance, the instance
661  *	is dropped and tty->ldisc reset to NULL. The caller can then retry
662  *	with N_TTY instead.
663  *
664  *	Returns 0 if successful, otherwise error code < 0
665  */
666 
667 int tty_ldisc_reinit(struct tty_struct *tty, int disc)
668 {
669 	struct tty_ldisc *ld;
670 	int retval;
671 
672 	lockdep_assert_held_write(&tty->ldisc_sem);
673 	ld = tty_ldisc_get(tty, disc);
674 	if (IS_ERR(ld)) {
675 		BUG_ON(disc == N_TTY);
676 		return PTR_ERR(ld);
677 	}
678 
679 	if (tty->ldisc) {
680 		tty_ldisc_close(tty, tty->ldisc);
681 		tty_ldisc_put(tty->ldisc);
682 	}
683 
684 	/* switch the line discipline */
685 	tty->ldisc = ld;
686 	tty_set_termios_ldisc(tty, disc);
687 	retval = tty_ldisc_open(tty, tty->ldisc);
688 	if (retval) {
689 		tty_ldisc_put(tty->ldisc);
690 		tty->ldisc = NULL;
691 	}
692 	return retval;
693 }
694 
695 /**
696  *	tty_ldisc_hangup		-	hangup ldisc reset
697  *	@tty: tty being hung up
698  *	@reinit: whether to re-initialise the tty
699  *
700  *	Some tty devices reset their termios when they receive a hangup
701  *	event. In that situation we must also switch back to N_TTY properly
702  *	before we reset the termios data.
703  *
704  *	Locking: We can take the ldisc mutex as the rest of the code is
705  *	careful to allow for this.
706  *
707  *	In the pty pair case this occurs in the close() path of the
708  *	tty itself so we must be careful about locking rules.
709  */
710 
711 void tty_ldisc_hangup(struct tty_struct *tty, bool reinit)
712 {
713 	struct tty_ldisc *ld;
714 
715 	tty_ldisc_debug(tty, "%p: hangup\n", tty->ldisc);
716 
717 	ld = tty_ldisc_ref(tty);
718 	if (ld != NULL) {
719 		if (ld->ops->flush_buffer)
720 			ld->ops->flush_buffer(tty);
721 		tty_driver_flush_buffer(tty);
722 		if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
723 		    ld->ops->write_wakeup)
724 			ld->ops->write_wakeup(tty);
725 		if (ld->ops->hangup)
726 			ld->ops->hangup(tty);
727 		tty_ldisc_deref(ld);
728 	}
729 
730 	wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT);
731 	wake_up_interruptible_poll(&tty->read_wait, EPOLLIN);
732 
733 	/*
734 	 * Shutdown the current line discipline, and reset it to
735 	 * N_TTY if need be.
736 	 *
737 	 * Avoid racing set_ldisc or tty_ldisc_release
738 	 */
739 	tty_ldisc_lock(tty, MAX_SCHEDULE_TIMEOUT);
740 
741 	if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
742 		tty_reset_termios(tty);
743 
744 	if (tty->ldisc) {
745 		if (reinit) {
746 			if (tty_ldisc_reinit(tty, tty->termios.c_line) < 0 &&
747 			    tty_ldisc_reinit(tty, N_TTY) < 0)
748 				WARN_ON(tty_ldisc_reinit(tty, N_NULL) < 0);
749 		} else
750 			tty_ldisc_kill(tty);
751 	}
752 	tty_ldisc_unlock(tty);
753 }
754 
755 /**
756  *	tty_ldisc_setup			-	open line discipline
757  *	@tty: tty being shut down
758  *	@o_tty: pair tty for pty/tty pairs
759  *
760  *	Called during the initial open of a tty/pty pair in order to set up the
761  *	line disciplines and bind them to the tty. This has no locking issues
762  *	as the device isn't yet active.
763  */
764 
765 int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
766 {
767 	int retval = tty_ldisc_open(tty, tty->ldisc);
768 
769 	if (retval)
770 		return retval;
771 
772 	if (o_tty) {
773 		/*
774 		 * Called without o_tty->ldisc_sem held, as o_tty has been
775 		 * just allocated and no one has a reference to it.
776 		 */
777 		retval = tty_ldisc_open(o_tty, o_tty->ldisc);
778 		if (retval) {
779 			tty_ldisc_close(tty, tty->ldisc);
780 			return retval;
781 		}
782 	}
783 	return 0;
784 }
785 
786 /**
787  *	tty_ldisc_release		-	release line discipline
788  *	@tty: tty being shut down (or one end of pty pair)
789  *
790  *	Called during the final close of a tty or a pty pair in order to shut
791  *	down the line discpline layer. On exit, each tty's ldisc is NULL.
792  */
793 
794 void tty_ldisc_release(struct tty_struct *tty)
795 {
796 	struct tty_struct *o_tty = tty->link;
797 
798 	/*
799 	 * Shutdown this line discipline. As this is the final close,
800 	 * it does not race with the set_ldisc code path.
801 	 */
802 
803 	tty_ldisc_lock_pair(tty, o_tty);
804 	tty_ldisc_kill(tty);
805 	if (o_tty)
806 		tty_ldisc_kill(o_tty);
807 	tty_ldisc_unlock_pair(tty, o_tty);
808 
809 	/*
810 	 * And the memory resources remaining (buffers, termios) will be
811 	 * disposed of when the kref hits zero
812 	 */
813 
814 	tty_ldisc_debug(tty, "released\n");
815 }
816 EXPORT_SYMBOL_GPL(tty_ldisc_release);
817 
818 /**
819  *	tty_ldisc_init		-	ldisc setup for new tty
820  *	@tty: tty being allocated
821  *
822  *	Set up the line discipline objects for a newly allocated tty. Note that
823  *	the tty structure is not completely set up when this call is made.
824  */
825 
826 int tty_ldisc_init(struct tty_struct *tty)
827 {
828 	struct tty_ldisc *ld = tty_ldisc_get(tty, N_TTY);
829 
830 	if (IS_ERR(ld))
831 		return PTR_ERR(ld);
832 	tty->ldisc = ld;
833 	return 0;
834 }
835 
836 /**
837  *	tty_ldisc_deinit	-	ldisc cleanup for new tty
838  *	@tty: tty that was allocated recently
839  *
840  *	The tty structure must not becompletely set up (tty_ldisc_setup) when
841  *      this call is made.
842  */
843 void tty_ldisc_deinit(struct tty_struct *tty)
844 {
845 	/* no ldisc_sem, tty is being destroyed */
846 	if (tty->ldisc)
847 		tty_ldisc_put(tty->ldisc);
848 	tty->ldisc = NULL;
849 }
850 
851 static struct ctl_table tty_table[] = {
852 	{
853 		.procname	= "ldisc_autoload",
854 		.data		= &tty_ldisc_autoload,
855 		.maxlen		= sizeof(tty_ldisc_autoload),
856 		.mode		= 0644,
857 		.proc_handler	= proc_dointvec,
858 		.extra1		= SYSCTL_ZERO,
859 		.extra2		= SYSCTL_ONE,
860 	},
861 	{ }
862 };
863 
864 static struct ctl_table tty_dir_table[] = {
865 	{
866 		.procname	= "tty",
867 		.mode		= 0555,
868 		.child		= tty_table,
869 	},
870 	{ }
871 };
872 
873 static struct ctl_table tty_root_table[] = {
874 	{
875 		.procname	= "dev",
876 		.mode		= 0555,
877 		.child		= tty_dir_table,
878 	},
879 	{ }
880 };
881 
882 void tty_sysctl_init(void)
883 {
884 	register_sysctl_table(tty_root_table);
885 }
886