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