Arnd Bergmann | b07471f | 2010-08-06 21:40:30 +0200 | [diff] [blame] | 1 | #include <linux/tty.h> |
2 | #include <linux/module.h> | ||||
3 | #include <linux/kallsyms.h> | ||||
4 | #include <linux/semaphore.h> | ||||
5 | #include <linux/sched.h> | ||||
6 | |||||
Peter Hurley | 2aff5e2 | 2014-11-05 12:13:01 -0500 | [diff] [blame] | 7 | /* |
8 | * Nested tty locks are necessary for releasing pty pairs. | ||||
9 | * The stable lock order is master pty first, then slave pty. | ||||
10 | */ | ||||
11 | |||||
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 12 | /* Legacy tty mutex glue */ |
13 | |||||
14 | enum { | ||||
15 | TTY_MUTEX_NORMAL, | ||||
Peter Hurley | 2febdb6 | 2014-11-05 12:13:02 -0500 | [diff] [blame] | 16 | TTY_MUTEX_SLAVE, |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 17 | }; |
Eric Dumazet | fde86d3 | 2012-05-31 11:35:18 +0200 | [diff] [blame] | 18 | |
Arnd Bergmann | b07471f | 2010-08-06 21:40:30 +0200 | [diff] [blame] | 19 | /* |
20 | * Getting the big tty mutex. | ||||
21 | */ | ||||
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 22 | |
Peter Hurley | 2febdb6 | 2014-11-05 12:13:02 -0500 | [diff] [blame] | 23 | void __lockfunc tty_lock(struct tty_struct *tty) |
Arnd Bergmann | b07471f | 2010-08-06 21:40:30 +0200 | [diff] [blame] | 24 | { |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 25 | if (tty->magic != TTY_MAGIC) { |
Sangho Yi | 7a0c4ed | 2012-10-18 00:15:13 +0900 | [diff] [blame] | 26 | pr_err("L Bad %p\n", tty); |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 27 | WARN_ON(1); |
28 | return; | ||||
29 | } | ||||
30 | tty_kref_get(tty); | ||||
Peter Hurley | 2febdb6 | 2014-11-05 12:13:02 -0500 | [diff] [blame] | 31 | mutex_lock(&tty->legacy_mutex); |
Arnd Bergmann | b07471f | 2010-08-06 21:40:30 +0200 | [diff] [blame] | 32 | } |
33 | EXPORT_SYMBOL(tty_lock); | ||||
34 | |||||
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 35 | void __lockfunc tty_unlock(struct tty_struct *tty) |
Arnd Bergmann | b07471f | 2010-08-06 21:40:30 +0200 | [diff] [blame] | 36 | { |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 37 | if (tty->magic != TTY_MAGIC) { |
Sangho Yi | 7a0c4ed | 2012-10-18 00:15:13 +0900 | [diff] [blame] | 38 | pr_err("U Bad %p\n", tty); |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 39 | WARN_ON(1); |
40 | return; | ||||
41 | } | ||||
42 | mutex_unlock(&tty->legacy_mutex); | ||||
43 | tty_kref_put(tty); | ||||
Arnd Bergmann | b07471f | 2010-08-06 21:40:30 +0200 | [diff] [blame] | 44 | } |
45 | EXPORT_SYMBOL(tty_unlock); | ||||
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 46 | |
Peter Hurley | 2aff5e2 | 2014-11-05 12:13:01 -0500 | [diff] [blame] | 47 | void __lockfunc tty_lock_slave(struct tty_struct *tty) |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 48 | { |
Peter Hurley | 2aff5e2 | 2014-11-05 12:13:01 -0500 | [diff] [blame] | 49 | if (tty && tty != tty->link) { |
50 | WARN_ON(!mutex_is_locked(&tty->link->legacy_mutex) || | ||||
51 | !tty->driver->type == TTY_DRIVER_TYPE_PTY || | ||||
52 | !tty->driver->type == PTY_TYPE_SLAVE); | ||||
Peter Hurley | 2febdb6 | 2014-11-05 12:13:02 -0500 | [diff] [blame] | 53 | tty_lock(tty); |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 54 | } |
55 | } | ||||
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 56 | |
Peter Hurley | 2aff5e2 | 2014-11-05 12:13:01 -0500 | [diff] [blame] | 57 | void __lockfunc tty_unlock_slave(struct tty_struct *tty) |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 58 | { |
Peter Hurley | 2aff5e2 | 2014-11-05 12:13:01 -0500 | [diff] [blame] | 59 | if (tty && tty != tty->link) |
60 | tty_unlock(tty); | ||||
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 61 | } |
Peter Hurley | 2febdb6 | 2014-11-05 12:13:02 -0500 | [diff] [blame] | 62 | |
63 | void tty_set_lock_subclass(struct tty_struct *tty) | ||||
64 | { | ||||
65 | lockdep_set_subclass(&tty->legacy_mutex, TTY_MUTEX_SLAVE); | ||||
66 | } |