Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Arnd Bergmann | b07471f | 2010-08-06 21:40:30 +0200 | [diff] [blame] | 2 | #include <linux/tty.h> |
3 | #include <linux/module.h> | ||||
4 | #include <linux/kallsyms.h> | ||||
5 | #include <linux/semaphore.h> | ||||
6 | #include <linux/sched.h> | ||||
7 | |||||
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 8 | /* Legacy tty mutex glue */ |
9 | |||||
Arnd Bergmann | b07471f | 2010-08-06 21:40:30 +0200 | [diff] [blame] | 10 | /* |
11 | * Getting the big tty mutex. | ||||
12 | */ | ||||
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 13 | |
Peter Hurley | c2bb524 | 2016-01-09 21:13:51 -0800 | [diff] [blame] | 14 | void tty_lock(struct tty_struct *tty) |
Arnd Bergmann | b07471f | 2010-08-06 21:40:30 +0200 | [diff] [blame] | 15 | { |
Peter Hurley | 6d029c6 | 2015-11-08 13:01:20 -0500 | [diff] [blame] | 16 | if (WARN(tty->magic != TTY_MAGIC, "L Bad %p\n", tty)) |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 17 | return; |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 18 | tty_kref_get(tty); |
Peter Hurley | 2febdb6 | 2014-11-05 12:13:02 -0500 | [diff] [blame] | 19 | mutex_lock(&tty->legacy_mutex); |
Arnd Bergmann | b07471f | 2010-08-06 21:40:30 +0200 | [diff] [blame] | 20 | } |
21 | EXPORT_SYMBOL(tty_lock); | ||||
22 | |||||
Peter Hurley | 0bfd464 | 2016-01-09 21:13:44 -0800 | [diff] [blame] | 23 | int tty_lock_interruptible(struct tty_struct *tty) |
24 | { | ||||
Peter Hurley | e9036d0 | 2016-02-05 10:49:36 -0800 | [diff] [blame] | 25 | int ret; |
26 | |||||
Peter Hurley | 0bfd464 | 2016-01-09 21:13:44 -0800 | [diff] [blame] | 27 | if (WARN(tty->magic != TTY_MAGIC, "L Bad %p\n", tty)) |
28 | return -EIO; | ||||
29 | tty_kref_get(tty); | ||||
Peter Hurley | e9036d0 | 2016-02-05 10:49:36 -0800 | [diff] [blame] | 30 | ret = mutex_lock_interruptible(&tty->legacy_mutex); |
31 | if (ret) | ||||
32 | tty_kref_put(tty); | ||||
33 | return ret; | ||||
Peter Hurley | 0bfd464 | 2016-01-09 21:13:44 -0800 | [diff] [blame] | 34 | } |
35 | |||||
Peter Hurley | c2bb524 | 2016-01-09 21:13:51 -0800 | [diff] [blame] | 36 | void tty_unlock(struct tty_struct *tty) |
Arnd Bergmann | b07471f | 2010-08-06 21:40:30 +0200 | [diff] [blame] | 37 | { |
Peter Hurley | 6d029c6 | 2015-11-08 13:01:20 -0500 | [diff] [blame] | 38 | if (WARN(tty->magic != TTY_MAGIC, "U Bad %p\n", tty)) |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 39 | return; |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 40 | mutex_unlock(&tty->legacy_mutex); |
41 | tty_kref_put(tty); | ||||
Arnd Bergmann | b07471f | 2010-08-06 21:40:30 +0200 | [diff] [blame] | 42 | } |
43 | EXPORT_SYMBOL(tty_unlock); | ||||
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 44 | |
Peter Hurley | c2bb524 | 2016-01-09 21:13:51 -0800 | [diff] [blame] | 45 | void tty_lock_slave(struct tty_struct *tty) |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 46 | { |
Peter Hurley | eef15e2 | 2014-12-30 07:11:11 -0500 | [diff] [blame] | 47 | if (tty && tty != tty->link) |
Peter Hurley | 2febdb6 | 2014-11-05 12:13:02 -0500 | [diff] [blame] | 48 | tty_lock(tty); |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 49 | } |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 50 | |
Peter Hurley | c2bb524 | 2016-01-09 21:13:51 -0800 | [diff] [blame] | 51 | void tty_unlock_slave(struct tty_struct *tty) |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 52 | { |
Peter Hurley | 2aff5e2 | 2014-11-05 12:13:01 -0500 | [diff] [blame] | 53 | if (tty && tty != tty->link) |
54 | tty_unlock(tty); | ||||
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 55 | } |
Peter Hurley | 2febdb6 | 2014-11-05 12:13:02 -0500 | [diff] [blame] | 56 | |
57 | void tty_set_lock_subclass(struct tty_struct *tty) | ||||
58 | { | ||||
Peter Hurley | 3abf87c | 2015-01-17 15:42:04 -0500 | [diff] [blame] | 59 | lockdep_set_subclass(&tty->legacy_mutex, TTY_LOCK_SLAVE); |
Peter Hurley | 2febdb6 | 2014-11-05 12:13:02 -0500 | [diff] [blame] | 60 | } |