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 | |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 7 | /* Legacy tty mutex glue */ |
| 8 | |
| 9 | enum { |
| 10 | TTY_MUTEX_NORMAL, |
| 11 | TTY_MUTEX_NESTED, |
| 12 | }; |
Eric Dumazet | fde86d3 | 2012-05-31 11:35:18 +0200 | [diff] [blame] | 13 | |
Arnd Bergmann | b07471f | 2010-08-06 21:40:30 +0200 | [diff] [blame] | 14 | /* |
| 15 | * Getting the big tty mutex. |
| 16 | */ |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 17 | |
| 18 | static void __lockfunc tty_lock_nested(struct tty_struct *tty, |
| 19 | unsigned int subclass) |
Arnd Bergmann | b07471f | 2010-08-06 21:40:30 +0200 | [diff] [blame] | 20 | { |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 21 | if (tty->magic != TTY_MAGIC) { |
Sangho Yi | 7a0c4ed | 2012-10-18 00:15:13 +0900 | [diff] [blame] | 22 | pr_err("L Bad %p\n", tty); |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 23 | WARN_ON(1); |
| 24 | return; |
| 25 | } |
| 26 | tty_kref_get(tty); |
| 27 | mutex_lock_nested(&tty->legacy_mutex, subclass); |
| 28 | } |
| 29 | |
| 30 | void __lockfunc tty_lock(struct tty_struct *tty) |
| 31 | { |
| 32 | return tty_lock_nested(tty, TTY_MUTEX_NORMAL); |
Arnd Bergmann | b07471f | 2010-08-06 21:40:30 +0200 | [diff] [blame] | 33 | } |
| 34 | EXPORT_SYMBOL(tty_lock); |
| 35 | |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 36 | void __lockfunc tty_unlock(struct tty_struct *tty) |
Arnd Bergmann | b07471f | 2010-08-06 21:40:30 +0200 | [diff] [blame] | 37 | { |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 38 | if (tty->magic != TTY_MAGIC) { |
Sangho Yi | 7a0c4ed | 2012-10-18 00:15:13 +0900 | [diff] [blame] | 39 | pr_err("U Bad %p\n", tty); |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 40 | WARN_ON(1); |
| 41 | return; |
| 42 | } |
| 43 | mutex_unlock(&tty->legacy_mutex); |
| 44 | tty_kref_put(tty); |
Arnd Bergmann | b07471f | 2010-08-06 21:40:30 +0200 | [diff] [blame] | 45 | } |
| 46 | EXPORT_SYMBOL(tty_unlock); |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 47 | |
| 48 | /* |
| 49 | * Getting the big tty mutex for a pair of ttys with lock ordering |
| 50 | * On a non pty/tty pair tty2 can be NULL which is just fine. |
| 51 | */ |
| 52 | void __lockfunc tty_lock_pair(struct tty_struct *tty, |
| 53 | struct tty_struct *tty2) |
| 54 | { |
| 55 | if (tty < tty2) { |
| 56 | tty_lock(tty); |
| 57 | tty_lock_nested(tty2, TTY_MUTEX_NESTED); |
| 58 | } else { |
| 59 | if (tty2 && tty2 != tty) |
| 60 | tty_lock(tty2); |
| 61 | tty_lock_nested(tty, TTY_MUTEX_NESTED); |
| 62 | } |
| 63 | } |
| 64 | EXPORT_SYMBOL(tty_lock_pair); |
| 65 | |
| 66 | void __lockfunc tty_unlock_pair(struct tty_struct *tty, |
| 67 | struct tty_struct *tty2) |
| 68 | { |
| 69 | tty_unlock(tty); |
| 70 | if (tty2 && tty2 != tty) |
| 71 | tty_unlock(tty2); |
| 72 | } |
| 73 | EXPORT_SYMBOL(tty_unlock_pair); |