blob: 393518a24cfe230e55e623697a5fe10dcd703665 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Arnd Bergmannb07471f2010-08-06 21:40:30 +02002#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-Hartman6c80c0b2021-04-08 14:51:30 +02007#include "tty.h"
Arnd Bergmannb07471f2010-08-06 21:40:30 +02008
Alan Cox89c8d912012-08-08 16:30:13 +01009/* Legacy tty mutex glue */
10
Arnd Bergmannb07471f2010-08-06 21:40:30 +020011/*
12 * Getting the big tty mutex.
13 */
Alan Cox89c8d912012-08-08 16:30:13 +010014
Peter Hurleyc2bb5242016-01-09 21:13:51 -080015void tty_lock(struct tty_struct *tty)
Arnd Bergmannb07471f2010-08-06 21:40:30 +020016{
Peter Hurley6d029c62015-11-08 13:01:20 -050017 if (WARN(tty->magic != TTY_MAGIC, "L Bad %p\n", tty))
Alan Cox89c8d912012-08-08 16:30:13 +010018 return;
Alan Cox89c8d912012-08-08 16:30:13 +010019 tty_kref_get(tty);
Peter Hurley2febdb62014-11-05 12:13:02 -050020 mutex_lock(&tty->legacy_mutex);
Arnd Bergmannb07471f2010-08-06 21:40:30 +020021}
22EXPORT_SYMBOL(tty_lock);
23
Peter Hurley0bfd4642016-01-09 21:13:44 -080024int tty_lock_interruptible(struct tty_struct *tty)
25{
Peter Hurleye9036d02016-02-05 10:49:36 -080026 int ret;
27
Peter Hurley0bfd4642016-01-09 21:13:44 -080028 if (WARN(tty->magic != TTY_MAGIC, "L Bad %p\n", tty))
29 return -EIO;
30 tty_kref_get(tty);
Peter Hurleye9036d02016-02-05 10:49:36 -080031 ret = mutex_lock_interruptible(&tty->legacy_mutex);
32 if (ret)
33 tty_kref_put(tty);
34 return ret;
Peter Hurley0bfd4642016-01-09 21:13:44 -080035}
36
Peter Hurleyc2bb5242016-01-09 21:13:51 -080037void tty_unlock(struct tty_struct *tty)
Arnd Bergmannb07471f2010-08-06 21:40:30 +020038{
Peter Hurley6d029c62015-11-08 13:01:20 -050039 if (WARN(tty->magic != TTY_MAGIC, "U Bad %p\n", tty))
Alan Cox89c8d912012-08-08 16:30:13 +010040 return;
Alan Cox89c8d912012-08-08 16:30:13 +010041 mutex_unlock(&tty->legacy_mutex);
42 tty_kref_put(tty);
Arnd Bergmannb07471f2010-08-06 21:40:30 +020043}
44EXPORT_SYMBOL(tty_unlock);
Alan Cox89c8d912012-08-08 16:30:13 +010045
Peter Hurleyc2bb5242016-01-09 21:13:51 -080046void tty_lock_slave(struct tty_struct *tty)
Alan Cox89c8d912012-08-08 16:30:13 +010047{
Peter Hurleyeef15e22014-12-30 07:11:11 -050048 if (tty && tty != tty->link)
Peter Hurley2febdb62014-11-05 12:13:02 -050049 tty_lock(tty);
Alan Cox89c8d912012-08-08 16:30:13 +010050}
Alan Cox89c8d912012-08-08 16:30:13 +010051
Peter Hurleyc2bb5242016-01-09 21:13:51 -080052void tty_unlock_slave(struct tty_struct *tty)
Alan Cox89c8d912012-08-08 16:30:13 +010053{
Peter Hurley2aff5e22014-11-05 12:13:01 -050054 if (tty && tty != tty->link)
55 tty_unlock(tty);
Alan Cox89c8d912012-08-08 16:30:13 +010056}
Peter Hurley2febdb62014-11-05 12:13:02 -050057
58void tty_set_lock_subclass(struct tty_struct *tty)
59{
Peter Hurley3abf87c2015-01-17 15:42:04 -050060 lockdep_set_subclass(&tty->legacy_mutex, TTY_LOCK_SLAVE);
Peter Hurley2febdb62014-11-05 12:13:02 -050061}