Merge tag 'overflow-v4.18-rc1-part2' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-block.git] / drivers / tty / tty_mutex.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
b07471fa
AB
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
89c8d91e
AC
8/* Legacy tty mutex glue */
9
b07471fa
AB
10/*
11 * Getting the big tty mutex.
12 */
89c8d91e 13
c2bb524b 14void tty_lock(struct tty_struct *tty)
b07471fa 15{
6d029c68 16 if (WARN(tty->magic != TTY_MAGIC, "L Bad %p\n", tty))
89c8d91e 17 return;
89c8d91e 18 tty_kref_get(tty);
2febdb63 19 mutex_lock(&tty->legacy_mutex);
b07471fa
AB
20}
21EXPORT_SYMBOL(tty_lock);
22
0bfd464d
PH
23int tty_lock_interruptible(struct tty_struct *tty)
24{
e9036d06
PH
25 int ret;
26
0bfd464d
PH
27 if (WARN(tty->magic != TTY_MAGIC, "L Bad %p\n", tty))
28 return -EIO;
29 tty_kref_get(tty);
e9036d06
PH
30 ret = mutex_lock_interruptible(&tty->legacy_mutex);
31 if (ret)
32 tty_kref_put(tty);
33 return ret;
0bfd464d
PH
34}
35
c2bb524b 36void tty_unlock(struct tty_struct *tty)
b07471fa 37{
6d029c68 38 if (WARN(tty->magic != TTY_MAGIC, "U Bad %p\n", tty))
89c8d91e 39 return;
89c8d91e
AC
40 mutex_unlock(&tty->legacy_mutex);
41 tty_kref_put(tty);
b07471fa
AB
42}
43EXPORT_SYMBOL(tty_unlock);
89c8d91e 44
c2bb524b 45void tty_lock_slave(struct tty_struct *tty)
89c8d91e 46{
eef15e2a 47 if (tty && tty != tty->link)
2febdb63 48 tty_lock(tty);
89c8d91e 49}
89c8d91e 50
c2bb524b 51void tty_unlock_slave(struct tty_struct *tty)
89c8d91e 52{
2aff5e2b
PH
53 if (tty && tty != tty->link)
54 tty_unlock(tty);
89c8d91e 55}
2febdb63
PH
56
57void tty_set_lock_subclass(struct tty_struct *tty)
58{
3abf87cd 59 lockdep_set_subclass(&tty->legacy_mutex, TTY_LOCK_SLAVE);
2febdb63 60}