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